例子
include $(TOPDIR)/rules.mk
PKG_NAME:=helloworld
PKG_RELEASE:=1
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
include $(INCLUDE_DIR)/package.mk
define Package/helloworld
SECTION:=utils
CATEGORY:=Utilities
TITLE:=Helloworld -- prints a snarky message
endef
define Package/helloworld/description
It's my first package demo.
endef
define Build/Prepare
echo "Here is Package/Prepare"
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
define Package/helloworld/install
echo "Here is Package/install"
$(INSTALL_DIR) $(1)/bin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
endef
$(eval $(call BuildPackage,helloworld))
大概我们可以将简代为如下的结构:
include $(TOPDIR)/rules.mk
# 这里定义一系列的 PKG_XX
include $(INCLUDE_DIR)/package.mk
# 定义各种 Package, Build 宏
$(eval $(call BuildPackage,包名))
下面,我们来一一拆解。
include $(TOPDIR)/rules.mk
首先,include $(TOPDIR)/rules.mk,也就是将 SDK/rules.mk 文件中的内容导入进来。
TOPDIR就是SDK的路径。
在 SDK/rules.mk 文件中,定义了许多变量。
我们可以看出,在Makefile中,赋值是用 := ,而不是等号。
比如上面的 BUILD_DIR, INCLUDE_DIR 等,都在这里定义。还有:
还有关于 TARGET_CC, TARGET_CXX 等非常有用的变量定义
还有 TAR, FIND, INSTALL_BIN, INSTALL_DIR, INSTALL_DATA等等非常重要的变量定义。
自定义 PKG_XXXX 变量
官网上指定有如下变量需要设置:
PKG_NAME - The name of the package, as seen via menuconfig and ipkg
PKG_VERSION - The upstream version number that we're downloading
PKG_RELEASE - The version of this package Makefile
PKG_LICENSE - The license(s) the package is available under, SPDX form.
PKG_LICENSE_FILE- file containing the license text
PKG_BUILD_DIR - Where to compile the package
PKG_SOURCE - The filename of the original sources
PKG_SOURCE_URL- Where to download the sources from (directory)
PKG_MD5SUM - A checksum to validate the download
PKG_CAT - How to decompress the sources (zcat, bzcat, unzip)
PKG_BUILD_DEPENDS - Packages that need to be built before this package, but are not required at runtime. Uses the same syntax as DEPENDS below.
PKG_INSTALL - Setting it to "1" will call the package's original "make install" with prefix set to PKG_INSTALL_DIR
PKG_INSTALL_DIR - Where "make install" copies the compiled files
PKG_FIXUP - ???
PKG_SOURCE_PROTO - the protocol to use for fetching the sources (git, svn)
PKG_REV - the svn revision to use, must be specified if proto is "svn"
PKG_SOURCE_SUBDIR - must be specified if proto is "svn" or "git", e.g. "PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)"
PKG_SOURCE_VERSION - must be specified if proto is "git", the commit hash to check out
PKG_CONFIG_DEPENDS - specifies which config options depend on this package being selected
include $(INCLUDE_DIR)/package.mk
跟上面的 include $(TOPDIR)/rules.mk 是一样的。就是把这个文件包含进来。
INCLUDE_DIR这个变量在 rules.mk 里已经定义了
那就是 SDK/include/package.mk 文件了,打开看看。
如果某个变量我们没有在上一部分里定义,那里在这个文件里,它就会被指定为默认
上面的用 ?= 来表示给未定义的变量赋默认值。比如,如果没有指定 PKG_MD5SUM,那么就默认为 unknow。
----------------------------------------------------------------------------
宏的格式
define XXX/xxxx
<宏的实体...>
endef
必须要定义的宏:
Package/<包名> #包的参数
如Package/helloworld宏
可选定义:
Build/Prepare 编译最开始要运行的宏
Package
/helloworld/install 安装
生效
$(
eval
$(call BuildPackage,helloworld))