yocto-第25篇-BitBake全过程(3)

By: fu linux
E-mail: fulinux@sina.com
Blog: https://blog.csdn.net/fulinus
喜欢的盆友欢迎点赞和订阅!
你的喜欢就是我写作的动力!

前面讲到构建过程中先是获取源文件、展开,再到打补丁(有补丁的话)这几个任务,接着开始将recipe依赖的一些文件放到sysroot中,之后是项目的configure和编译源码的阶段了,参考下图:
在这里插入图片描述

BitBake全过程

do_prepare_recipe_sysroot任务

此任务在${WORKDIR}目录中创建两个sysroot(即recipe-sysroot和recipe-sysroot-native)子目录,
以便在打包阶段,将recipe所依赖的其他recipe在do_populate_sysroot任务时放到sysroot目录中,两个sysroot子目录里面分别放着目标machine和本地机器(编译机,host,比如ubuntu)使用到的执行程序和库等二进制文件,好像还有头文件。

build]$ ls tmp/work/core2-64-poky-linux/learnyocto/1.0+gitAUTOINC+cac0e57e2f-r0/recipe-sysroot/    
lib  sysroot-providers  usr
  • 1
  • 2

在这里插入图片描述

build]$ ls tmp/work/core2-64-poky-linux/learnyocto/1.0+gitAUTOINC+cac0e57e2f-r0/recipe-sysroot-native/usr/
bin/     include/ lib/     libexec/ sbin/    share/
  • 1
  • 2

在这里插入图片描述

do_configure任务

比如当你的项目是autotools项目(就是目录里面有哦configure.ac 和 Makefile.am文件的项目)时,do_configure任务就是做类似我们手动输入命令./configure --prefiex=xxx一样,

EXTRA_OECONF变量

一般如果是autotools项目,configuration选项可以通过使用EXTRA_OECONF或者PACKAGECONFIG_CONFARGS变量传达。随便举个例子:

EXTRA_OECONF = "--with-platform=${GRUBPLATFORM} \
                --disable-grub-mkfont \
                --program-prefix="" \
                --enable-liblzma=no \
                --enable-libzfs=no \
                --enable-largefile \
"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

而如果我们的项目是cmake管理的项目时,就如同我们手动执行:

learnyocto ]$ mkdir build/ && cd build/
build]$ cmake ..
  • 1
  • 2

EXTRA_OECMAKE变量

一般如果是cmake项目, configuration选项可以通过使用EXTRA_OECMAKE变量传达,随便举个例子:

EXTRA_OECMAKE = "-DBUILD_SHARED_LIBS=ON \
                 -DCMAKE_DISABLE_FIND_PACKAGE_Boost=TRUE \
                 -DHAVE_BOOST_BYTESWAP=FALSE \
                 -DCMAKE_CXX_STANDARD=11 \
                 -DCMAKE_CXX_STANDARD_REQUIRED=OFF \
                 -DLIB_SUFFIX=${@d.getVar('baselib').replace('lib', '')} \
"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

有很多很多类似的,不知如何使用?找一些bb文件,看看官方都是怎么添加的。

EXTRA_OEMAKE变量

如果是Makefile项目,configuration选项可以通过使用EXTRA_OEMAKE变量传达,随便举个例子:

EXTRA_OEMAKE = "MKDIR='mkdir' CP='cp -f' RM='rm' LDFLAGS='-lpthread -ldl' OE_BUILD=1"
  • 1

CFLAGS或CXXFLAGS变量

gcc编译选项CFLAGS、g++编译选项CXXFLAGS,举栗子:

CFLAGS += "-I${S}/include"
CFLAGS_append = " -fPIC"
CXXFLAGS_append = " -fPIC"
  • 1
  • 2
  • 3
干货演示

有些小白盆友可能不知道如何使用这些变量,这里我们结合learnyocto项目,来演示下如何使用CFLAGS变量。
比如我们要在learnyocto项目中添加一个宏来控制程序逻辑,过程如下:

build]$ devtool modify learnyocto
build]$ cd workspace/sources/learnyocto/
  • 1
  • 2

编辑main.c,main.c文件内容如下:

#include <stdio.h>

int main (int argc, char **argv)
{
    printf ("[master branch]Hello Yocto!\n");//change develop to master
    printf ("Learn devtool upgrade command\n");
#ifdef LEARN_CFLAGS_VAR
    printf ("Learn CFLAGS variable when define\n");
#else
    printf ("Learn the CFLAGS variable when not define\n");
#endif

    return 0;
} /* ----- End of main() ----- */
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

修改保存退出

按照代码的逻辑将,如果定义了宏LEARN_CFLAGS_VAR的话就会打印如下字符串:

Learn CFLAGS variable when define
  • 1

如果没有定义该宏,打印的就是下面这一句:

Learn the CFLAGS variable when not define
  • 1

编译项目

learnyocto]$ cd -
build]$ devtool build learnyocto
  • 1
  • 2

在线部署演示

build]$ runqemu qemux86-64
  • 1

在另外一个终端中在线部署项目,将执行程序放到用户根目录下:

~]$ cd poky/
poky]$ source oe-init-build-env 
build]$ devtool deploy-target learnyocto root@192.168.7.2:~/
  • 1
  • 2
  • 3

然后登录qemux86-64虚拟机:

$ ssh root@192.168.7.2
root@qemux86-64:~# 
root@qemux86-64:~# ./usr/bin/learnyocto 
[master branch]Hello Yocto!
Learn devtool upgrade command
Learn the CFLAGS variable when not define
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

如我们前面所述,因为我们没有定义LEARN_CFLAGS_VAR宏,所以打印的是第二行字符串。

修改bb文件
下面我们在bb文件中修改,添加该宏

build]$ devtool edit-recipe learnyocto
  • 1

在最后一行,加上:

CFLAGS += "-DLEARN_CFLAGS_VAR=ON"
  • 1

保存退出。

重新编译验证:

build]$ devtool build learnyocto  
build]$ devtool deploy-target learnyocto root@192.168.7.2:~/
root@qemux86-64:~# ./usr/bin/learnyocto 
[master branch]Hello Yocto!
Learn devtool upgrade command
Learn CFLAGS variable when define
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

符合我们的预期。

git 提交代码

learnyocto]$ git add -u
learnyocto]$ git commit -m "learn the CFLAGS variable in recipe"
  • 1
  • 2

结束recipe修改

build]$ devtool finish learnyocto meta-mylayer
build]$ rm -rf workspace/sources/learnyocto
  • 1
  • 2

生成了补丁文件,同时bb文件也有更新:

build]$ ls ../meta-mylayer/recipes-learnyocto/learnyocto/learnyocto
0001-learn-the-CFLAGS-variable-in-recipe.patch
build]$ cat ../meta-mylayer/recipes-learnyocto/learnyocto/learnyocto_git.bb 
...
SRC_URI = "git://gitee.com/fulinux/learnyocto.git;protocol=https;branch=master \
           file://0001-learn-the-CFLAGS-variable-in-recipe.patch \  #这里引用了补丁文件
           "
...
CFLAGS += "-DLEARN_CFLAGS_VAR=ON"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

do_compile任务

配置任务完成后,BitBake使用do_compile任务编译源代码。编译发生在B变量指向的目录中。请注意,缺省情况下,B指向的目录与S指向的目录相同。如下:

build]$ bitbake -e learnyocto | grep ^B=
B="/home/peeta/poky/build/tmp/work/core2-64-poky-linux/learnyocto/1.0+gitAUTOINC+cac0e57e2f-r0/build"
  • 1
  • 2

知识点: bitbake --help
-e, --environment Show the global or per-recipe environment complete
with information about where variables were
set/changed.

任务的空函数形式

如果是现成的执行程序或者库文件,configure和编译源码任务,可以在bb文件中写成空函数,例如:

do_configure_prepend () {
}
do_configure () {
}

do_compile () {
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

do_install任务

编译完成后,BitBake执行do_install任务。这个任务从B指向的目录中复制文件到D变量所指向的目录中。稍后使用此目录中的文件进行打包。D变量如下:

build]$ bitbake -e learnyocto | grep ^D=
D="/home/peeta/poky/build/tmp/work/core2-64-poky-linux/learnyocto/1.0+gitAUTOINC+cac0e57e2f-r0/image"
  • 1
  • 2

清空learnyocto编译内容:

build]$ bitbake learnyocto -c cleanall
  • 1

不清空,构建该项目时有错误提示:

ERROR: learnyocto-1.0+gitAUTOINC+cac0e57e2f-r0 do_packagedata: QA Issue: Package version for package learnyocto-src went backwards which would break package feeds (from 0:1.0+git999-r0 to 0:1.0+git0+cac0e57e2f-r0) [version-going-backwards]
ERROR: learnyocto-1.0+gitAUTOINC+cac0e57e2f-r0 do_packagedata: QA Issue: Package version for package learnyocto-dbg went backwards which would break package feeds (from 0:1.0+git999-r0 to 0:1.0+git0+cac0e57e2f-r0) [version-going-backwards]
  • 1
  • 2

END