openwrt插件都是以.ipk方式发布的,ipk可以直接通过opkg安装到系统。上文我们编译了简单的hello,本文探究hello.ipk的编译过程。
一. 环境准备
cd ~/openwrt/bin/ar71xx tar -xjvf OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz #解压SDK包 cd OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2/package #进入package目录mkdir hello #建立软件包目录
目录层次:
package/hello
|— /src/hello.c
|— /src/Makefile
|— Makefile
二. 编码
1. 编辑编译hello的Makefile
vi hello/src/Makefile
hello: hello.o$(CC) $(LDFLAGS) hello.o -o hello hello.o: hello.c$(CC) $(CFLAGS) -c hello.cclean:rm *.o hello
2. 编辑发布hello.ipk的Makefile
vi hello/Makefile
#Makefile include $(TOPDIR)/rules.mk# Nameand release number of this package PKG_NAME:=hello PKG_RELEASE:=1# This specifies the directory where we're going to build the program. # The root build directory, $(BUILD_DIR), is by default the build_mipsel # directory in your OpenWrt SDK directory PKG_BUILD_DIR:= $(BUILD_DIR)/$(PKG_NAME)include $(INCLUDE_DIR)/package.mk# Specify package information for this program. # The variables defined here should be self explanatory. # If youare running Kamikaze, delete the DESCRIPTION # variable below and uncomment the Kamikaze define # directivefor the description below define Package/helloSECTION:=utilsCATEGORY:=UtilitiesTITLE:=Helloworld-- prints a snarky message endef# Specifywhat needs to be done to prepare for building the package. # In ourcase, we need to copy the source files to the build directory. # This isNOT the default. The default uses thePKG_SOURCE_URL and the #PKG_SOURCE which is not defined here to download the source from the web. # Inorder to just build a simple program that we have just written, it is # mucheasier to do it this way. define Build/Preparemkdir -p $(PKG_BUILD_DIR)$(CP) ./src/* $(PKG_BUILD_DIR)/ endef# We donot need to define Build/Configure or Build/Compile directives # Thedefaults are appropriate for compiling a simple program such as this one # Specifywhere and how to install the program. Since we only have one file, # thehello executable, install it by copying it to the /bin directory on # therouter. The $(1) variable represents the root directory on the router running #OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install #directory if it does not already exist. Likewise $(INSTALL_BIN) contains the # commandto copy the binary file from its current location (in our case the build #directory) to the install directory. define Package/hello/install$(INSTALL_DIR) $(1)/bin$(INSTALL_BIN) $(PKG_BUILD_DIR)/hello $(1)/bin/ endef# This line executes the necessary commands to compile our program. # The above define directives specify all the information needed, but this # line calls BuildPackage which in turn actually uses this information to # build apackage. $(eval $(call BuildPackage,hello))
三. 编译
cd ~/openwrt/bin/ar71xx/OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2 #sdk根目录 make #编译
如果顺利,会出现以下信息:
make[1] world make[2] package/compile make[3] -C package/hello compile make[2] package/index
编译成功会在 bin/arr71xx/packages 下生成 hello_1_ar71xx.ipk
四. 上传并测试