The openwrt plug-ins are all released in the form of .ipk, and ipk can be installed directly to the system through opkg. We compiled a simple hello above, and this article explores the compilation process of hello.ipk.
1. Environmental preparation
cd ~/openwrt/bin/ar71xx tar -xjvf OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz #Unzip the SDK package cd OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2/package #Enter the package directory mkdir hello #Create a package directory
Directory Hierarchy:
package/hello
|--- /src/hello.c
|--- /src/Makefile
|--- Makefile
2. Coding
1. Edit the Makefile for compiling hello
vi hello/src/Makefile
hello: hello.o $(CC) $(LDFLAGS) hello.o -o hello hello.o: hello.c $(CC) $(CFLAGS) -c hello.c clean: rm * .o hello
2. Edit and publish the Makefile
vi hello/Makefile of hello.ipk
#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/hello SECTION:=utils CATEGORY:=Utilities TITLE:=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/Prepare mkdir -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))
3. Compile
cd ~/openwrt/bin/ar71xx/OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2 #sdk根目录 make #compile
If successful, the following message will appear:
make[1] world make[2] package/compile make[3] -C package/hello compile make[2] package/index
Successful compilation will generate hello_1_ar71xx.ipk under bin/arr71xx/packages
4. Upload and test