一般情况下,我们下载openwrt源码之后,我们都会基于openwrt的环境,定制化开发一些功能,openwrt里面不一定包含我们所有需要的库,可能我们需要的用到的库要自己加上去,这就涉及到如何我把我需要的模块添加到openwrt中编译。
网上也有许多关于openwrt下makefile编写的文章,但是恰好工作需要,我也从不懂到熟悉,现在把学习的过程及结果分享给大家,希望给有需要帮助的朋友,有表达不当的地方,也请前辈帮忙指出。下面是增加libglog的过程。
1.我们首先新建我们库的目录
openwrt/package/libs/libglog
2.在libglog中新建文件Makefile,然后我们需要完成Makefile的功能,先把内容贴出来,我再来解释每一步流程:
Makefile总共由两大部分构成,且这两大部分,由两个include分开,现在详细的分析每个部分。
2.1 include $(TOPDIR)/rules.mk
就是将rules.mk中的内容包含进来,rules.mk主要是一些变量的定义:一些路径的定义,路径的自动扩展,编译 的选项添加,编译中需要使用的一些命令的路径等。这个是Makefile的重要的操作,必须包含。
2.2 模块1,这部分是关于新增库的描述信息。
我们可以从官方获取这些PKG_变量的含义:
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_REV - the svn revision to use, must be specified if proto is "svn"
PKG_SOURCE - The filename of the original sources
PKG_SOURCE_URL - Where to download the sources from (directory)
PKG_SOURCE_VERSION - must be specified if proto is "git", the commit hash to check out
PKG_SOURCE_SUBDIR - must be specified if proto is "svn" or "git"
PKG_SOURCE_PROTO - the protocol to use for fetching the sources (git, svn)
PKG_INSTALL - Setting it to "1" will call the package's original "make install" with prefix set to PKG_INSTALL_DIR
PKG_LICENSE - The license(s) the package is available under, SPDX form.
PKG_LICENSE_FILE- file containing the license textPKG_BUILD_DIR - Where to compile the package
PKG_INSTALL_DIR - Where "make install" copies the compiled files
关于PKG_BUILD_PARALLEL没有找到相关的描述,通过名称,猜测是并行编译的意思,但是我们需要确认这个猜测,在include/host-build.mk中我们找到了相关的定义:
到这里就明白了,其实就是类似我们make的时候的参数 -j,PKG_BUILD_PARALLEL就是多线程编译。
了解了这些我们再来看,如何给这些PKG_的变量赋值:
我们通过页面上面的信息,可以定义这些变量:
PKG_NAME:=libglog PKG_VERSION:=0.3.3
PKG_REV:=da816ea70645e463aa04f9564544939fa327d5a7
PKG_SOURCE_URL:=git://github.com/google/glog.git
PKG_SOURCE_PROTO:=git
扩展变量定义:
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_VERSION:=$(PKG_REV)
PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)
其他相关变量定义:
PKG_INSTALL:=1 需要make install PKG_BUILD_PARALLEL:=1 允许并行编译 PKG_LICENSE:=LGPL-2.1 license
至此,所以需要编译的变量定义完毕。
2.3 include $(INCLUDE_DIR)/package.mk
就是将package.mk里面的内容包含进来,主要包含以下几功能:
* 定义一些默认的变量(PKG_INSTALL_DIR ?= $(PKG_BUILD_DIR)/ipkg-install)
* 包含其他的mk文件 (include $(INCLUDE_DIR)/download.mk)
* 定义默认的宏(define Build/Prepare/Default)
* 定义特殊的宏(define BuildPackage)
2.4 模块2,这部分是我们定义一些自定义的宏
package.mk里面定义了一些通用的宏,我们还需要为我们的package定义自定义宏,我们先了解下官方的定义:
define Package/<包名> //eg : define Package/libglog
SECTION -- 包的种类
CATEGORY -- 显示在menuconfig下面的目录名称
TITLE -- 简单的介绍
DESCRIPTION -- 详细描述
URL -- 源码网址
DEPENDS -- 需要依赖的包
endif
我们根据实际的情况,给这个包定义自定义宏描述。
define Build/installDev
For things needed to compile packages against it (static libs, header files), but that are of no use on the target device
其他的静态库或者头文件需要编译用到的,但是不需要安装到target device中
define Package/libglog/install
A set of commands to copy files into the ipkg which is represented by the $(1) directory. As source you can use relative paths which will install from the unpacked and compiled source, or $(PKG_INSTALL_DIR) which is where the files in the Build/Install step above end up.
主要是包含一些命令,软件包的安装方法,包括一系列拷贝编译好的文件到指定位置。调用时会带一个参数,就是嵌入系统的镜像文件系统目录,因此$(1)表示嵌入系统的镜像目录。
2.5 $(eval $(call BuildPackage,libglog))
使用eval函数实现各种定义,基本上不需要修改。到此,Makefile基本完成。
3. 完成Makefile之后,我们即可编译我们增加的库了。
make package/libs/libglog/compile -j1 V=s
在执行的过程,发现提示依赖库不存在:
Package libglog is missing dependencies for the following libraries:
libstdc++.so.6
这里需要把libstdc++当做libglog的依赖,然后把libstdc++.so.6拷贝到相同的目录即可
define Package/libglog
DEPENDS:=+libstdc++ //增加依赖库
endif
define Package/libglog/install
$(cp) xxx/xxx/xxx/libstdc++.so.6 $(1)/usr/lib //增加拷贝
endif
4. 在make menuconfig中配置libglog
我们执行make menuconfig,选择我们增加的库即可。
Libraries --->
<*> libglog