openwrt 编译外部源码 过程与问题解决

1、openwrt 基本介绍

openwrt 是一个编译工具,帮助自定义编译内核和应用(我的理解)。所以如果有定制自己需要的内核,并基于这个定制化内核编译一些应用的话,可以使用openwrt,openwrt 还是很成熟,很方便的。

(PS:最开始不知道openwrt的时候,我是自己配置编译环境,然后自己编译应用,当然是很复杂的,又要应用和内核版本一致,又有各种规范,一个OVS编了好久才编好;而使用openwrt ,只需要勾选上自己想要的应用就可以了(openwrt 应用源中的应用很多,基本满足我的需求),所以推荐大家使用openwrt)

我使用的openwrt 版本是:19.07.1,想要移植到的平台是:Xilinx ZC702。

2、编译前准备

openwrt源码下载:

        网址是:https://github.com/openwrt/openwrt

                这个不用记,直接谷歌里面输:openwrt github 第一个就是。

        下载:git clone https://github.com.cnpmjs.org/openwrt/openwrt.git .

外部源码准备:

        你需要使用的源码,放在一个位置。

        .config 配置文件

其他应用或者库:

        这个是说有些库或者软件openwrt 确实没有,那么你可以准备下这些库或者软件的源码,借助 openwrt去帮你编译。

        (PS:当然,你不可能一上来就知道哪个库或者软件openwrt 没有,都是先进入到openwrt 里面,make menuconfig ,找一找,搜一搜,发现确实没有的话,你再去准备下这些东西。)

3、开始编译工作

选择版本:

        git tag 看看有哪些版本,我是因为openwrt 对ZC702 的支持从19.07.1 才开始,所以选择这个版本,大家可以根据需求选择版本。

        git checkout 版本号(直接复制git tag给出的某一个版本)

更新openwrt 库并下载:

        ./scripts/feeds update -a         #更新feeds软件源

        ./scripts/feeds install -a        #下载更新

功能选择:

        make menuconfig 

 .config - OpenWrt Configuration
 qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
  lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq OpenWrt Configuration qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk
  x  Arrow keys navigate the menu.  <Enter> selects submenus ---> (or empty submenus ----).  Highlighted letters x  
  x  are hotkeys.  Pressing <Y> includes, <N> excludes, <M> modularizes features.  Press <Esc><Esc> to exit, <?> x  
  x  for Help, </> for Search.  Legend: [*] built-in  [ ] excluded  <M> module  < > module capable               x  
  x                                                                                                              x  
  x lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk x  
  x x                      Target System (Xilinx Zynq 7000 SoCs)  --->                                         x x  
  x x                      Target Profile (Xilinx ZC702)  --->                                                 x x  
  x x                      Target Images  --->                                                                 x x  
  x x                      Global build settings  --->                                                         x x  
  x x                  [*] Advanced configuration options (for developers)  --->                               x x  
  x x                  [ ] Build the OpenWrt Image Builder                                                     x x  
  x x                  [ ] Build the OpenWrt SDK                                                               x x  
  x x                  [ ] Package the OpenWrt-based Toolchain                                                 x x  
  x x                  [ ] Image configuration  --->                                                           x x  
  x x                      Base system  --->                                                                   x x  
  x x                      Administration  --->                                                                x x  
  x x                      Boot Loaders  ----                                                                  x x  
  x x                      Development  --->                                                                   x x  
  x x                      Extra packages  --->                                                                x x  
  x mqqqqqqqqqqqqqqqqqqv(+)qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj x  
  tqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu  
  x                           <Select>    < Exit >    < Help >    < Save >    < Load >                           x  
  mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj  
                                                                                                                    

        target system 选择目标平台

        target profile 选择具体型号

                这两个是必选项

        然后就是外部源码路径的配置:

                

 .config - OpenWrt Configuration
 > Advanced configuration options (for developers) qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
  lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq Advanced configuration options (for developers) qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk
  x  Arrow keys navigate the menu.  <Enter> selects submenus ---> (or empty submenus ----).  Highlighted letters x  
  x  are hotkeys.  Pressing <Y> includes, <N> excludes, <M> modularizes features.  Press <Esc><Esc> to exit, <?> x  
  x  for Help, </> for Search.  Legend: [*] built-in  [ ] excluded  <M> module  < > module capable               x  
  x                                                                                                              x  
  x lqqqqqqqqqqqqqqqqqq^(-)qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk x  
  x x                  ()    Local mirror for source packages                                                  x x  
  x x                  [*]   Automatic rebuild of packages                                                     x x  
  x x                  [ ]   Automatic removal of build directories                                            x x  
  x x                  ()    Build suffix to append to the target BUILD_DIR variable                           x x  
  x x                  ()    Override the default TARGET_ROOTFS_DIR variable                                   x x  
  x x                  [ ]   Use ccache                                                                        x x  
  x x                  (your_kernel_path) Use external kernel tree                            x x  
  x x                  ()    Enter git repository to clone                                                     x x  
  x x                  [ ]   Enable log files during build process                                             x x  
  x x                  [ ]   Enable package source tree override                                               x x  
  x x                  (-fno-caller-saves -fno-plt) Additional compiler options                                x x  
  x x                  [ ]   Target Options  ----                                                              x x  
  x x                  [ ]   Use external toolchain  ----                                                      x x  
  x x                  [ ]   Toolchain Options  ----                                                           x x  
  x mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj x  
  tqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu  
  x                           <Select>    < Exit >    < Help >    < Save >    < Load >                           x  
  mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj  
                                                                                                                    

        use external kernel tree 按 enter 进入填写路径。

        外部源码配置只需要填下这个路径就行了,没有其他配置项。

编译:

        这里我建议,先去外部源码里面预编译一下,然后回来openwrt 走openwrt 的编译流程,因为这里面会涉及到很多问题。

        问题1:配置加载失败

                

		scripts/kconfig/conf  --silentoldconfig Kconfig
		*
		* Restart config...
		*
		*
		* Xilinx Specific Options
		*
Cache Prefetch (XILINX_PREFETCH) [Y/n/?] (NEW) 

            出现类似这样的错误,也就是内核的配置有问题,而先把内核源码编译一下就不会出现。

        问题2:make menuconfig 功能和 make kernel_menuconfig CONFIG_TARGET=target 功能的冲突

                这个问题对我来说是这样的:我需要kernel 中的某项功能支持,是cfg80211 模块的子功能,在make kernel_menuconfig CONFIG_TARGET=target(说明:这个是openwrt 下编译内核功能的命令)中已经勾选,但是make 时却提示:

                Package kmod-XXX is missing dependencies for the following libraries:

                cfg80211.ko

                mac80211.ko

                所以openwrt 未识别你在kernel 中勾选的功能,因此先编译下内核源码会让你的编译工作更顺畅。

        内核编译

                cd YOUR_KERNEL_PATH        #在你的外部源码路径下操作 

                准备好.config 配置文件

                export ARCH=arm

                export CROSS_COMPILE=arm-linux-gnueabihf-        #没有的话就下载:sudo apt-get install gcc-arm-linux-gnueabihf

                make menuconfig        #确认下你需要的一些功能有没有选上

                make prepare && make modules_prepare

                make -j12 uImage UIMAGE_LOADADDR=0x8000        #这个地方可能有个报错,直接百度下,安装那个工具就好了,具体我忘记了

                make modules

                到此为止,内核预编译工作完成,内核功能已经编好,接下来去openwrt 继续完成openwrt 的编译就好了。

        openwrt 编译

               cd OPENWRT_PATH

               make menuconfig        #确认下配置

               make kernel_menuconfig CONFIG_TARGET=target        #加载下内核配置,并进行确认。这里有个地方需要注意,openwrt 会分一个squashfs 分区,所以你的kernel 配置需要支持这个,进行勾选即可。

 .config - Linux/arm 4.14.0 Kernel Configuration
 > File systems > Miscellaneous filesystems qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
  lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq Miscellaneous filesystems qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk
  x  Arrow keys navigate the menu.  <Enter> selects submenus ---> (or empty submenus ----).  Highlighted letters x  
  x  are hotkeys.  Pressing <Y> includes, <N> excludes, <M> modularizes features.  Press <Esc><Esc> to exit, <?> x  
  x  for Help, </> for Search.  Legend: [*] built-in  [ ] excluded  <M> module  < > module capable               x  
  x                                                                                                              x  
  x lqqqqqqqqqqqqqqqqqq^(-)qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk x  
  x x                  [ ]     Advanced compression options for JFFS2                                          x x  
  x x                  < >   Compressed ROM file system support (cramfs) (OBSOLETE)                            x x  
  x x                  <*>   SquashFS 4.0 - Squashed file system support                                       x x  
  x x                          File decompression options (Decompress files directly into the page cache)  --->x x  
  x x                          Decompressor parallelisation options (Use percpu multiple decompressors for parax x  
  x x                  [ ]     Squashfs XATTR support                                                          x x  
  x x                  [ ]     Include support for ZLIB compressed file systems                                x x  
  x x                  [ ]     Include support for LZ4 compressed file systems                                 x x  
  x x                  [ ]     Include support for LZO compressed file systems                                 x x  
  x x                  [*]     Include support for XZ compressed file systems                                  x x  
  x x                  [ ]     Include support for ZSTD compressed file systems                                x x  
  x x                  [ ]     Use 4K device block size?                                                       x x  
  x x                  [*]     Additional option for memory-constrained systems                                x x  
  x x                  (3)       Number of fragments cached                                                    x x  
  x mqqqqqqqqqqqqqqqqqqv(+)qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj x  
  tqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu  
  x                           <Select>    < Exit >    < Help >    < Save >    < Load >                           x  
  mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj  
                                                                                                                    

                然后就是编译:

                make download V=s        #这个项我建议无论是第一次还是之后的每一次编译,都先进行这一步。这个命令会下载好你需要编译的软件,提高编译成功率。

                make V=s        #第一次编译时间会稍长一点。

                等待结束即可。

                然后就能烧入 img 镜像,看看能不能跑起来了。

                sz bin/targets/zynq/generic/openwrt-zynq-YourTargerProfile-squashfs-sdcard.img.gz        #具体名称不一样,根据你的平台,但是这个路径。

4、第三方库和软件编译

        以上工作可以满足大部分的需求:内核是根据你的需求定制的;应用也是根据你的需求勾选编译的。img会很小,烧录非常快。

        但是有些情况我们需要的库和软件,openwrt 确实没有提供,怎么办呢?那我们就按照openwrt 的规范,将源码放在要求的位置,稍微改下配置文件,让openwrt 帮助我们完成编译。

        由于我的工作限制,我只编译过库、驱动和应用这些类型,所以后面根据这个分类详细贴出配置文件。

第三方编译规范

        需要编译第三方的软件(驱动、库等),只需要按照openwrt 的规则,安排好源码的路径结构,稍微改下 makefile 等文件,使openwrt 能够识别你的软件,就可以了,其实还是很简单的。

        一般是这样:

                在package/libs(对于库)、package/kernel/(对于驱动)、package/utils(应用软件) 路径下,创建一个文件夹,里面放你的代码,结构如下:

                Makefile 和 src/ 

                Makefile 我在后面会根据软件类型不同贴出来。

                src里面就放你的源码。

                src 有些会自带Makefile ,没关系,不需要对它进行修改。比如src/Makefile 里面指定了编译器:CC=gcc,没事啊,外面的Makefile 也会指定openwrt 的编译器,并覆盖内层Makefile 中的配置。

        这些填好后,去make menuconfig 里面勾选上这个软件,就可以开始编译了。

分类型软件规范和编译

        库

               注意下外层Makefile 就行了:

                示例(需要改的地方我已经作了标注):

include $(TOPDIR)/rules.mk

PKG_NAME:=lib+库名称
PKG_VERSION:=0.10.0
PKG_RELEASE:=1

PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
PKG_INSTALL:=1
include $(INCLUDE_DIR)/package.mk

define Package/lib库名
	SECTION:=libs
	CATEGORY:=Libraries
	SUBMENU:=子目录,名字随便取,方便查找
	TITLE:=这个是对库的简介
	DEPENDS:=+libcurl +libssh  +libxml2 +libxslt(这个就看你的库依赖什么了,+加上就行) 
endef

define Package/lib库名/description
 库的介绍 
endef

define Build/InstallDev
	$(INSTALL_DIR) $(1)/usr/lib
	$(CP) $(PKG_INSTALL_DIR)/usr/lib/lib库名.{a,so*,la} $(1)/usr/lib/
endef

define Package/lib库名/install
	$(INSTALL_DIR) $(1)/usr/lib
	$(CP) $(PKG_INSTALL_DIR)/usr/lib/lib库名.so.* $(1)/usr/lib/
endef
$(eval $(call BuildPackage,${PKG_NAME}))

        驱动

                驱动的话多一步,在src/ 里面添加一个Kconfig 文件:

config 驱动名
    tristate "驱动介绍"
    help
    This is a example, for debugging kernel model.
    If unsure, say N.

                外层Makefile 示例(需要改的地方我已经作了标注):

#
# Copyright (C) 2014-2016 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk

PKG_NAME:=驱动名

include $(INCLUDE_DIR)/package.mk

define KernelPackage/驱动名
	SUBMENU:=目录
	TITLE:=驱动title
	FILES:=$(PKG_BUILD_DIR)/驱动名.ko
endef

define Build/Compile
	$(KERNEL_MAKE) M="$(PKG_BUILD_DIR)" \
		CFLAGS="$(TARGET_CFLAGS)   -g -pipe " \
		modules
endef

$(eval $(call KernelPackage,$(PKG_NAME)))

        软件、上层应用

                外层Makefile(需要修改的地方我已经做了批注):

include $(TOPDIR)/rules.mk

PKG_NAME:=应用名
PKG_VERSION:=1.0
PKG_RELEASE:=1

PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)

include $(INCLUDE_DIR)/package.mk

define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
	$(CP) ./src/* $(PKG_BUILD_DIR)
endef

define Package/应用名
	SECTION:=utils
	SUBMENU:=目录
	CATEGORY:=Utilities
	TITLE:=应用title
	DEPENDS:=+libpcap(根据需要添加依赖) 
endef

define Package/应用名/description
 介绍
endef

define Build/Compile
        $(MAKE) -C $(PKG_BUILD_DIR) \
                CC="$(TARGET_CC)" \
                CFLAGS="$(TARGET_CFLAGS) -Wall -Werror -g -pipe  -lpcap "
endef

define Package/应用名/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/$(PKG_NAME) $(1)/usr/bin/
endef

$(eval $(call BuildPackage,$(PKG_NAME)))

        编译

                不需要make 全部重新编译,想看看软件编译是否有问题,只需要如此:

                make package/libs/库文件夹名/compile

                make package/kernel/驱动文件夹名/compile

                make package/utils/应用文件夹名/compile

                即可单独编译应用。

5、感谢与参考文章

        这部分其实才是我写这篇文章的原因,在最开始一点都没有接触openwrt 的时候,是这些文章给我指明了方向,现在我已经初步入门,也踩了不少的坑,所以我要像这些文章的作者一样,学习他们的精神,不吝自己的时间与笔墨,将自己的经历和经验写下来,帮助更多的人,少走弯路,更快的编译出自己的固件,享受这份喜悦。

        等到做完这一切回头看的时候,可能觉得也没什么,并不是多难,但是在当初做的时候,那种迷茫与困惑的焦躁心情依然难忘,好多网上的文章都是复制的,根本没用。所以我才自己写文章,试图让一个初学者从头能够学习。

        参考文章:

        https://blog.csdn.net/qq_40614144/article/details/105538483

        https://blog.csdn.net/wind0419/article/details/78036522?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-2.control

        https://www.right.com.cn/forum/thread-324501-1-1.html (恩山论坛有许多openwrt 的帖子,很有帮助)

        最后,我只是做了我的一点工作,写了我对这个事情的理解,肯定有我的理解上的局限,有什么错误请大家指正,欢迎批评和交流。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值