yocto的hello world

为了辅助理解yocto编译的架构和编译过程,写一遍helloworld会好很多。

首先下载编译poky

$ git clone git://git.yoctoproject.org/poky
$ cd poky
$ source oe-init-build-env
$ bitbake core-image-sato

创建自己的layer:meta-mylayer

$ bitbake-layers create-layer meta-mylayer
NOTE: Starting bitbake server...
Add your new layer with 'bitbake-layers add-layer meta-mylayer'

在poky目录下通过bitbake-layers增加meta-layer,提示找不到bblayers.conf,原来是要在build目录下添加才对:

hui@hui:~/disk4t/codes/poky
$ bitbake-layers add-layer meta-mylayer
NOTE: Starting bitbake server...
Unable to find bblayers.conf

在build目录下add-layer, 没有指定meta-mylayer路径报错

hui@hui:~/disk4t/codes/poky/build
$ bitbake-layers add-layer meta-mylayer
NOTE: Starting bitbake server...
Specified layer directory /home/hui/disk4t/codes/poky/build/meta-mylayer doesn't exist

指定路径后可以了:

$ bitbake-layers add-layer ../meta-mylayer
NOTE: Starting bitbake server...

cat bblayer.conf可以看到meta-mylayer加到了最后一行

$ cat conf/bblayers.conf 
# POKY_BBLAYERS_CONF_VERSION is increased each time build/conf/bblayers.conf
# changes incompatibly
POKY_BBLAYERS_CONF_VERSION = "2"

BBPATH = "${TOPDIR}"
BBFILES ?= ""

BBLAYERS ?= " \
  /home/hui/disk4t/codes/poky/meta \
  /home/hui/disk4t/codes/poky/meta-poky \
  /home/hui/disk4t/codes/poky/meta-yocto-bsp \
  /home/hui/disk4t/codes/poky/meta-mylayer \
  "

查看meta-mylayer目录

$ tree .
.
├── conf
│   └── layer.conf
├── COPYING.MIT
├── README
└── recipes-example
    └── example
        └── example_0.1.bb

新建files目录,在files目录下添加hello.c文件

$ tree meta-mylayer/
meta-mylayer/
├── conf
│   └── layer.conf
├── COPYING.MIT
├── README
└── recipes-example
    └── example
        ├── example_0.1.bb
        └── files
            └── hello.c

修改example_0.1.bb,定义编译规则

SUMMARY = "bitbake-layers recipe"
LICENSE = "MIT"

python do_display_banner() {
    bb.plain("***********************************************");
    bb.plain("*                                             *");
    bb.plain("*  Example recipe created by bitbake-layers   *");
    bb.plain("*                                             *");
    bb.plain("***********************************************");
}

DESCRIPTION = "Simple helloworld application"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"

SRC_URI = "file://hello.c"

S = "${WORKDIR}"

do_compile() {
        ${CC} hello.c ${LDFLAGS} -o helloworld
}

do_install() {
        install -d ${D}${bindir}
        install -m 0755 helloworld ${D}${bindir}
}

编译

$ bitbake example
Loading cache: 100% |########################################################################################################################################################################| Time: 0:00:00
Loaded 1470 entries from dependency cache.
NOTE: Resolving any missing task queue dependencies

Build Configuration:
BB_VERSION           = "1.51.1"
BUILD_SYS            = "x86_64-linux"
NATIVELSBSTRING      = "universal"
TARGET_SYS           = "x86_64-poky-linux"
MACHINE              = "qemux86-64"
DISTRO               = "poky"
DISTRO_VERSION       = "3.3+snapshot-bf47addb34debc049b59e5e466228f00045c2061"
TUNE_FEATURES        = "m64 core2"
TARGET_FPU           = ""
meta                 
meta-poky            
meta-yocto-bsp       
meta-mylayer         = "master:bf47addb34debc049b59e5e466228f00045c2061"

Initialising tasks: 100% |###################################################################################################################################################################| Time: 0:00:00
Sstate summary: Wanted 0 Local 0 Network 0 Missed 0 Current 153 (0% match, 100% complete)
NOTE: Executing Tasks
NOTE: Tasks Summary: Attempted 623 tasks of which 623 didn't need to be rerun and all succeeded.

然后在tmp目录下find生成的Helloworld

$ find . -name "helloworld"
./work-shared/gcc-11.2.0-r0/gcc-11.2.0/libgo/misc/cgo/fortran/helloworld
./work/core2-64-poky-linux/gstreamer1.0/1.18.4-r0/gstreamer-1.18.4/tests/examples/helloworld
./work/core2-64-poky-linux/example/0.1-r0/packages-split/example/usr/bin/helloworld
./work/core2-64-poky-linux/example/0.1-r0/packages-split/example-dbg/usr/bin/.debug/helloworld
./work/core2-64-poky-linux/example/0.1-r0/package/usr/bin/.debug/helloworld
./work/core2-64-poky-linux/example/0.1-r0/package/usr/bin/helloworld
./work/core2-64-poky-linux/example/0.1-r0/image/usr/bin/helloworld
./work/core2-64-poky-linux/example/0.1-r0/helloworld

在conf/local.conf文件中增加IMAGE_INSTALL

IMAGE_INSTALL = "example"

IMAGE_INSTALL_append = "example",用IMAGE_INSTALL_append如下会报错,没有研究应该怎么用

$ bitbake core-image-sato
ERROR: Variable IMAGE_INSTALL_append contains an operation using the old override syntax. Please convert this layer/metadata before attempting to use with a newer bitbake.

然后build core-image-sato

$ bitbake core-image-sato

编译完成后再次运行,这次可能因为加了helloworld的缘故,没有进入到图形界面,但是在QEMU的shell环境中就可以运行helloworld程序,输出’Hello World!’。

$ runqemu tmp/deploy/images/qemux86-64/bzImage-qemux86-64.bin tmp/deploy/images/qemux86-64/core-image-sato-qemux86-64.ext4

参考: