Openwrt simple ipk generation and Makefile explanation
1. Create a helloworld project. First, create the helloworld. c file and the Makefile file $ mkdir-p ~ /Temp/hellworld/src $ cd ~ /Temp/helloworld/src $ touch helloworld. c Makefile is the content of helloworld. c as follows:
#include <stdio.h>int main(){ printf("This is my helloworld!\n"); return 0;}
The content of Makefile is as follows:
helloworld : helloworld.o$(CC) $(LDFLAGS) helloworld.o -o helloworldhelloworld.o : helloworld.c$(CC) $(CFLAGS) -c helloworld.cclean :rm *.o helloworld$(CC)
: This value is specified by other Makefile files, indicating that we use the compiler. \ $ (LDFLAGS) \ & $ (CFLAGS): This indicates some compiler options, which are optional here and can be removed. enter $ make to check whether there is any problem. Pay attention to the Makefile format. finally, enter $ make clean to clear the generated binary file. because the compiler used in make in the previous step is not our cross-compilation chain, the generated binary file cannot be run on the Development Board. the previous step only verifies whether the content in src is correct. 2. create a helloworld package. Next we will create a new Makefile file, which describes the information of the helloworld package, such as how to configure, compile, and package, installation location. $ cd ~ /Temp/helloworld $ touch Makefile is as follows:
include $(TOPDIR)/rules.mkPKG_NAME:=helloworldPKG_RELEASE:=1PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)include $(INCLUDE_DIR)/package.mkdefine Package/helloworldSECTION:=utilsCATEGORY:=UtilitiesTITLE:=Helloworld -- prints a snarky messageendefdefine Package/helloworld/descriptionIt's my first package demo.endefdefine Build/Prepareecho "Here is Package/Prepare"mkdir -p $(PKG_BUILD_DIR) $(CP) ./src/* $(PKG_BUILD_DIR)/endefdefine Package/helloworld/installecho "Here is Package/install"$(INSTALL_DIR) $(1)/bin$(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/endef$(eval $(call BuildPackage,helloworld))
The following is the final file tree structure: 3. makefile comment 1st rows include $ (TOPDIR)/rules. mk: generally starts with Makefile and contains basic package information, such as $ (BUILD_DIR), $ (INCLUDE_DIR), $ (CP), $ (INSTALL_DIR ), $ (INSTALL_BIN) is defined here. for details, go to the main directory of the source code to view the rules. mk file. 3 ~ In line 5, the package information starts with "PKG _". The meanings and functions are as follows: PKG_NAME: package name, which can be seen in menuconfig and ipkg. PKG_VERSION: The software version. PKG_RELEASE: Version PKG_SOURCE of Makefile: name of the source code file. PKG_SOURCE_URL: the location of the source code download website. PKG_MD5SUM: verification code of the source code file. Used to check whether the software package is downloaded correctly. PKG_CAT: Decompress the source code file. Including zcat, bzcat, and unzip. PKG_BUILD_DIR: Package compilation directory. Its parent directory is $ (BUILD_DIR ). Row 7th include $ (INCLUDE_DIR)/package. mk: Generally, after the basic information of the software package is complete, it defines the rules of the user State software package. The Package is divided into user mode and kernel module. The user mode Package is used, and the kernel module uses KernelPackage. $ (INCLUDE_DIR)/Kernel. the mk file is indispensable when the software package is the kernel, $ (INCLUDE_DIR)/package. mk is applied to the user State. Next we will introduce the user-mode software package. The user program compilation Package starts with Package/, and then the software name. The software name in the Package definition can be different from the software Package name and can be defined multiple. 9 ~ Row 13: defines the package name as helloworldSECTION. The package type is utilsCATEGORY. The directory is Utilitis, that is, the position of the file in menuconfig. Sometimes there is a SUBMENU item, that is, a subdirectory. TITLE: a brief description of the software package, which is displayed in menuconfig. URL: the download location of the software package. MAINTAINER: the MAINTAINER option. DEPENDS: dependencies with other software. That is, if other software is required for compilation or installation, it must be specified. If multiple dependencies exist, each dependency must be separated by a space. Use the "+" sign before dependency to display objects by default, that is, objects are displayed if they are not selected, that is, they are displayed only when the dependent objects are selected. 15 ~ Row 17: Detailed description of the software package, which will be displayed in make menuconfig 19 ~ Row 23: Compilation preparation method, which does not need to be described for software packages downloaded online. Compilation preparation methods must be described for software packages that are not downloaded from the internet or are self-developed. The preparation method used in this article is to first create the package directory, and then copy the source code to the directory just created. According to the habit of OpenWrt, we usually put all the programs we designed under the src directory. 25 ~ Line 29: installation method of the software package, including a series of copied compiled files to the specified location. A parameter is included in the image file system directory of the embedded system. Therefore, $ (1) indicates the image directory of the embedded system. INSTALL_DIR: = install-d-m0755: Create a readable and executable directory named INSTALL_BIN: = install-m0755: lines 31 of the compiled file to the image file directory $ (eval $ (call BuildPackage, helloworld): After completing the preceding definition, you must use the eval function to implement various definitions. The format is: for general software packages: $ (eval $ (call Package, $ (PKG_NAME) or for kernel modules: $ (eval $ (call KernelPackage, $ (PKG_NAME) If a software package has multiple programs, for example, an application has its own kernel module, the above PKG_NAME needs to be flexible. Multiple eval functions may be designed. It can also be processed as multiple software packages. Makefile is briefly explained here. For more details, refer to 4. so far, our software has been basically completed. Compile the software below. First, copy the file folder to the package file in the openwrt directory. Here, my source code directory is ~ /Openwrt, You need to replace the openwrt directory with your openwrt source code directory. $ mv ~ /Temp/helloworld ~ /Openwrt/package and return to the project home directory to run make menuconfig $ cd ~ /Openwrt $ make menuconfig press "/", enter helloworld, search for the corresponding path, go to the Utilities directory, find helloworld, and press the space to open it; Save and exit; $ cd ~ /Openwrt $ make package/helloworld/compile V = s after compilation, ipk should have generated $ find bin/-name "helloworld *. ipk "now we have generated a simple ipk. Congratulations :) Finally, you can install ipk in the Development Board through winscp.