0


buildroot快速入门

本文约定:以字符‘$’符号开头的为命令,其后以字符‘#’开始的部分为命令说明。

、Buildroot什么?****

简述

buildroot是一个嵌入式系统构建工具,可以帮助我们快速构建系统映像文件,如rootfs.tar、uImage、uboot.imx以及设备树,同时也可以帮助我们构建对应的应用开发环境(交叉编译工具链以及第三方库,如Qt)。buildroot工程组织方式跟内核类似,即Config.in文件中描述执行make menuconfig时显示的配置菜单,Makefile根据Conifg.in和配置文件*.mk中定义的变量和规则编译整个工程。buildroot构建过程可以概括为下载、解压、配置、编译、安装。同时,buildroot也提供了多种源码下载方式,以提供灵活的定制方式,例如,支持从官方网站下载,从git库下载,从svn库下载或者简单的从本地拷贝。对第三方库包的种类支持也比较广泛,如普通包(Makefile组织)、cmake包(由Cmake组织)、python包(python脚本)等。为了支持更灵活的定制,buildroot提供了参考目录架构(所谓的br2-external tree),即定制部分放在buildroot工程目录的外面,通过变量设置存放位置的方式来连接到buildroot工程。

buildroot工程目录结构

board: 一些典型板子的配置文件或说明文档

output:输出目录,其中有目标映像uboot.imx、uImage、rootfs.tar.gz、xxx.dtb; 编译时源码主目录build; 主机环境host

dl:下载的源码存放目录

support:支持脚本

configs:配置文件,make xxx_defconfig

arch: 架构配置文件,arm、mips、powerpc、x86 etc. 配置选项Target options --->

system:构建rootfs基本目录结构

toolchain:编译器配置目录,支持buildroot自带的,第三方的,自定义的。

fs:各种文件系统,squashfs、yaffs2、ext2 etc

package:rootfs中用到的各种库或命令工具包,如qt、ssh、mplayer、busybox、python etc.

linux: 内核配置目录

buildroot可以帮解决哪些问题?****

  • 轻松构建嵌入式系统(rootfs)
  • 把系统各库之间繁杂的依赖关系交给工具处理,解放自己
  • 自动部署第三方库,减少体力劳动
  • 优化程序启动,根据不同启动等级启动程序,方便程序启动、终止以及重启。(需要添加debian扩展工具包)
  • 解决root权限问题,无法上外网和非root权限下服务器上构建rootfs
  • 规范rootfs目录和权限管控,自动管理用户及各目录权限

如何使用buildroot构建嵌入式系统****

  1. 获取buildroot工程****

一般是从官网Index of /downloads (buildroot.org) 或GitHub下载。但我们的服务器不能上网,所以将buildroot工程及第三方库部署到了自己用的服务器192.168.x.x上,此处简述从192.168.x.x服务器上获取buildroot工程及相关代码的步骤。

  1. 将自己账户的密钥拷贝到远程服务器192.168.x.x上(若已操作的话可省去此步骤)

$ ssh-copy-id git@192.168.x.x

  1. 获取引导脚本repo(若没有bin目录,创建之)

$ scp git@192.168.x.x:/home/git/tools/repo ~/bin/

$ chmod a+x ~/bin/repo

  1. 获取repo脚本及表单文件

$ repo init -u git@192.168.x.x:/home/git/tools/manifests --no-repo-verify

  1. 获取buildroot工程及相关文件

$ repo sync

注:下载完成后,当前目录下会有一个.repo隐藏文件夹和project文件夹,project/buildroot即为buildroot工程目录。

  1. 配置和编译buildroot工程****

  2. 配置工程

  3. 进入buildroot工程目录

$ cd project/buildroot

  1. 配置并编译buildroot工程

$ make xxx_defconfig BR2_EXTERNAL=../external

  1. 编译工程,构建rootfs

执行make进行编译,编译期间出现的问题及处理方法如下。

  1. 一开始提示如下类似的信息,均键入y回车即可。

unzip (BR2_PACKAGE_UNZIP) [N/y/?] (NEW) y

libuuid (BR2_PACKAGE_LIBUUID) [N/y/?] (NEW) y

  1. 出现如下提示:

You have PERL_MM_OPT defined because Perl local::lib

is installed on your system. Please unset this variable

before starting Buildroot, otherwise the compilation of

Perl related packages will fail

解决方法:

$ unset PERL_MM_OPT

  1. 选择Qt库版本提示和协议提示

Type 'c' if you want to use the Commercial Edition.

Type 'o' if you want to use the Open Source Edition.

处理方法:

键入o,然后回车即可,出现协议

Do you accept the terms of the license? y

键入y,回车即可。

  1. 生成文件说明****

生成的文件主要有目标映像文件output/images/*和主机开发环境目录../host。

注意事项:

  1. buildroot工程中不要使用make –j选项进行多线程编译,若要加快编译速度,可以进行如下配置

$ make menuconfig

选择Build options ---> (8) Number of jobs to run simultaneously (0 for auto),此处的8表示8线程编译,同-j8

  1. 在buildroot工程中使用make clean会清楚生成目录output和外部编译器所在目录../host,若使用了,只需要切换到.repo所在目录并执行repo sync即可。

  2. 若下载中出现问题,请先删除.repo目录,再重新操作。

  3. 如何重新编译安装

buildroot完成每个流程后都会在对应的工作目录下建立标志文件,如uboot:

编译安装完成后在output/build/uboot-custom/目录下会有以下标志文件:

.stamp_rsynced

.stamp_configured

.stamp_built

.stamp_target_installed

.stamp_images_installed

因此,若要重新执行有两种方法:

  1. 对于make show-targets列表中存在的包,可以执行make <pkg>-reconfigure或make <pkg>-rebuild进行重新配置或编译

  2. 对于对于make show-targets列表中不存在的包,可以手动删除output/build/<pkg>目录中的对应标志文件,然后执行make,buildroot会重新启动操作,并建立标志文件。

  3. 如何编译安装单个

buildroot本身不支持单个包安装,但有时候编译和安装单个包又为必须工作,此处提供一种方式。

  1. 编译整个buildroot工程。
  2. 进入output/build/<pkg>目录中
  3. 执行命令make DESTDIR=<distdir> install

distdir为将要安装包<pkg>的目录。

如何自定义自己的buildroot工程****

  1. 推荐的目录结构****

为了适用于自己的项目,buildroot工程支持高度自定义,但为了结构清晰,buildroot推荐将自定义的部分单独放到buildroot工程外部,而不是与buildroot工程本身混杂起来,即buildroot中所谓的br2-external架构目录,以下是推荐的目录结构。

本项目中的目录结构如下:

├── buildroot # buildroot主目录

│ ├── configs

│ ├── downloads

│ ├── output

│ ├── package

│ └── ...

├── external # br2-external架构目录

│ ├── Config.in

│ ├── external.desc

│ ├── external.mk

│ ├── board

│ │ └── xxx_imx6qp

│ │ ├── local.mk # package override file定义目录

│ │ ├── rootfs_overlay

│ │ ├── scripts

│ │ └── users_table

│ ├── configs

│ │ └── xxx_imx6qp_defconfig

│ └── package

│ ├── Config.in

│ ├── libuuid

│ └── ...

├── host # 选项Build options ---> (${TOPDIR}/../host)Host dir

│ └── opt # 外部编译器所在目录

│ └── gcc-linaro-arm-linux-gnueabihf

└── source # local.mk中所定义的本地包目录

├── kernel

└── uboot
  1. 自定义外部编译器****

buildroot自己带有编译器,也支持外部编译器(本地编译器)

当配置为buildroot内部编译器时,构建过程中会从相关网站上下载编译器,并先编译编译器,再利用编译器构建系统;当配置为外部编译器时,会下载其他编译器或拷贝本地编译器到buildroot工程。

Buildroot编译器的配置选项如下:

Toolchain ---> Toolchain type (External toolchain) --->

Toolchain ---> Toolchain (Custom toolchain) --->

Toolchain ---> (${TOPDIR}/../host/opt/gcc-linaro-arm-linux-gnueabihf)Toolchain path

Toolchain ---> ($(ARCH)-linux-gnueabihf)Toolchain prefix

此处配置为本地编译器,并使用buildroot工程上层目录中host目录中的编译器。

  1. 自定义uboot和内核****

uboot和内核的定义使用buildroot的<pkg>_OVERRIDE_SRCDIR机制来实现。uboot和内核源码使用git管控,在配置文件local.mk中指定变量uboot_OVERRIDE_SRCDIR的值为uboot的实际路径,linux_OVERRIDE_SRCDIR变量的值为linux路径,并在buildroot的配置选项中指定local.mk的路径和uboot和内核的获取方式即可,具体配置自定义步骤如下:

  1. 新建文件$(BR2_EXTERNAL)/ board/xxx_imx6qp/local.mk

其内容为:

UBOOT_SITE_METHOD := local

UBOOT_OVERRIDE_SRCDIR = $(BR2_EXTERNAL)/../source/uboot

LINUX_SITE_METHOD := local

LINUX_OVERRIDE_SRCDIR = $(BR2_EXTERNAL)/../source/kernel

  1. 配置buildroot选项

Build options -->($(BR2_EXTERNAL)/board/xxx_imx6qp/local.mk) location of a package override file

Kernel ---> Kernel version (Local directory) --->

($(TOPDIR)/../source) Path to the local directory

Bootloaders ---> U-Boot Version (Local directory) --->

($(TOPDIR)/../source) Path to the local directory

  1. 自定义rootfs****

rootfs自定义方式比较灵活,大概有以下几种:

  1. 通过基本目录架构定制(不推荐)

buildroot会从一个基础的系统目录架构开始构建rootfs,这个基础目录架构默认为system/skeleton目录,可以通过修改该目录达到定制的目的。相关配置选项如下:

System configuration ---> Root FS skeletoon (custom target skeleton) --->

System configuration ---> (your rootfs path)custom target skeleton path

  1. 通过rootfs覆盖目录机制

buildroot提供在最终打包rootfs之前添加文件到rootfs中的机制,可以通过该机制添加文件到rootfs中。相关配置选项如下:

System configuration ---> ($(BR2_EXTERNAL)/board/xxx_mx6qp/overlay-rootfs) Root filesystem overlay directories

这样,在最终打包之前,buildroot会先拷贝$(BR2_EXTERNAL)/board/xxx_mx6qp/overlay-rootfs文件夹中的内容到rootfs,然后再打包。

  1. 通过自定义脚本

buildroot提供在rootfs打包前和打包后执行脚本的机制,只需要通过以下配置方式指定要执行的脚本即可:

System configuration ---> ($(BR2_EXTERNAL)/scripts/before-create-rootfs-tar.sh) Custom scripts to run before creating filesystem

System configuration ---> ($(BR2_EXTERNAL)/scripts/after-create-rootfs-tar.sh) Custom scripts to run after creating filesystem

此种方式中,自定义脚本中可能需要使用buildroot本身的一些变量(如文件路径),可以通过以下配置传递变量到自定义脚本中:

System configuration ---> (TARGET_DIR HOST_DIR BASE_DIR) Extra post-{build,image} arguments

  1. 添加自定义外部包****

buildroot包含了绝大多数包(第三方库或工具),尽管如此,你也可以添加自己的包到buildroot工程中,步骤如下:

  1. 添加包目录external/package/<pkg>
  2. 拷贝buildroot/package目录下某个包中的Config.in和*.mk到目录external/package/<pkg>下,并按规则修改。

注:根据包的类型和所属系统(包将在target中使用,还是在host中使用,或者两者都有)选择对应的模板文件

包的种类有:

$(eval $(generic-package)) # make

$(eval $(autotools-package))

$(eval $(cmake-package))

$(eval $(python-package))

buildroot会根据包的类型选择编译工具

所属系统有:

$(eval $(generic-package))

$(eval $(host-generic-package))

            添加hash文件到目录external/package/<pkg>下

注:内核模块也可以添加为外部包,参考[buildroot文档](The Buildroot user manual)

附录

  1. 参考文档****

[1] The Buildroot user manual

[2] buildroot-slides.pdf

[3] buildroot-xxx/docs/*

[4] Buildroot用户手册-将软件包添加到Buildroot_buildroot添加软件包

  1. buildroot命令****

配置:

$ make xxx_defconfig # 拷贝一个已有的配置

$ make menuconfig # 配置buildroot

$ make uboot-menuconfig # 配置uboot,自己添加

$ make linux-menuconfig # 配置内核

$ make busybox-menuconfig # 配置busybox

编译:

$ make show-targets # 显示可单独编译的包

$ make uboot # 单独编译uboot

$ make linux # 单独编译linux

$ make rootfs-tar # 构建rootfs-tar

$ make <pkg>-rebuild # 重新编译包pkg

$ make <package>-reconfigure # 重新配置包package

$ make source # 只下载包源码

执行流:

$ make source # 获取源码

$ make extract # 解包到output/build目录

$ make pactch # 应用补丁,如果有的话

$ make configure # 运行配置命令

$ make build # 运行编译命令

$ make install-target # 运行target目录的安装包

依赖图:

$ make show-depends # 显示依赖关系, 需要先编译

$ make graph-depends #生成已经编译的全系统依赖图, 存储在output/graph目录

$ make <pkg>-graph-depends #生成pkg包的依赖图

$ BR2_GRAPH_OUT=svg/png make pulseaudio-graph-depends #生成svg格式或png格式的依赖图

这两个命令要求在系统中已经安装了dot 工具

  1. 注意事项****

1、buildroot-2018版本中external/Config.in中

变量引用为source "$BR2_EXTERNAL_xxx_PATH/package/Config.in"

变量引用没有括号,也不能直接写成相对路径"package/Config.in"

原因:buildroot主目录下Config.in中的相对路径是相对于主目录而言,即便是在package目录下;

2、external中的Config.in中的相对路径也是相对于buildroot主目录而言的,因此即便是在external目录下的Config.in文件中包含本目录下的Config.in也必须加$BR2_EXTERNAL_xxx_PATH指定时external目录

3、make clean会清空output下的所有内容,谨慎使用

  1. 一个buildroot配置例子****

$ make menuconfig

Build options

-->Host dir-->$(TOPDIR)/../host

-->(8)Numbers of jobs...

Toolchain

-->Toolchain type (External toolchain) -->

Toolchain (custom toolchain) -->

($(TOPDIR)/../host/opt/gcc-linaro-arm-linux-gnueabihf)Toolchain path

($(ARCH)-linux-gnueabihf) Toolchain prefix

External toolchain gcc version (4.8.x) -->

External toolchain kernel headers series (3.1.x) -->

External toolchain C library (glibc/eglibc) -->

[*] Toolchain has SSP support?

[*] Toolchain has RPC support?

[*] Toolchain has C++ support? #QT依赖此选项

($(TOPDIR)/../host/opt/gcc-linaro-arm-linux-gnueabihf/arm-linux-gnueabihf/libc/lib) \

Extra toolchain libraries to be copied to target

[*] Copy gdb server to the Target

System configuration

(root) System hostname

(Welcome to root) System banner

Kernel

(3.14.52) Kernel version

(0x10008000)load address (for 3.7+ multi-platform image)

(imx6q-sabresd imx6q-sabrelite) In-tree Device Tree Source file names

[*] Install kernel image to /boot in target

Bootloaders

(mx6qsabresd) U-Boot board name

(2015.04) U-Boot version

U-Boot binary format -->[*]u-boot.imx

[] Install U-Boot SPL binary image

Filesystem images-->

Compression method (gzip)-->

标签: linux git

本文转载自: https://blog.csdn.net/Austin_2020/article/details/128163229
版权归原作者 Austin_2020 所有, 如有侵权,请联系我们删除。

“buildroot快速入门”的评论:

还没有评论