[Yocto] HelloWorld in the image of BitBake getting started

explain

Previous article [Yocto] Introduction to BitBake HelloWorld In, you print Hello World through bitmake, and this article puts the printing of Hello World into the image.

get ready

The environment used is Ubuntu 18.04:

jw@X1C:~/code/poky$ uname -a
Linux X1C 5.4.0-84-generic #94~18.04.1-Ubuntu SMP Thu Aug 26 23:17:46 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Download the necessary tools:

sudo apt install gawk wget git-core diffstat unzip texinfo gcc-multilib build-essential chrpath socat cpio python python3 python3-pip python3-pexpect xz-utils debianutils iputils-ping

Prepare a basic Yocto code, which can be used directly in https://gitee.com/jiangwei0512/poky.git Download. The downloaded code is as follows:

jw@X1C:~/code/poky$ ls
bitbake               LICENSE.MIT  meta-selftest      README.OE-Core
contrib               Makefile     meta-skeleton      README.poky
documentation         MEMORIAM     meta-yocto-bsp     README.qemu
LICENSE               meta         oe-init-build-env  scripts
LICENSE.GPL-2.0-only  meta-poky    README.hardware

In addition, there are certain requirements for network and storage space. It is necessary to ensure that the network can usually download various installation packages, and that the compilation environment has enough space (greater than 50GB).

Custom Layer

In order not to affect the original code framework, we add our own Layer to store code, recipes and various configurations, which is also the way recommended by Yocto.

Before creating a recipe, you need to execute Yocto's initial script OE init build Env, which is located in the code root directory. The execution process is as follows:

jw@X1C:~/code/poky$ source oe-init-build-env 
You had no conf/local.conf file. This configuration file has therefore been
created for you with some default values. You may wish to edit it to, for
example, select a different MACHINE (target hardware). See conf/local.conf
for more information as common configuration options are commented.

You had no conf/bblayers.conf file. This configuration file has therefore been
created for you with some default values. To add additional metadata layers
into your configuration please add entries to conf/bblayers.conf.

The Yocto Project has extensive documentation about OE including a reference
manual which can be found at:
    http://yoctoproject.org/documentation

For more information about OpenEmbedded see their website:
    http://www.openembedded.org/


### Shell environment set up for builds. ###

You can now run 'bitbake <target>'

Common targets are:
    core-image-minimal
    core-image-full-cmdline
    core-image-sato
    core-image-weston
    meta-toolchain
    meta-ide-support

You can also run generated qemu images with a command like 'runqemu qemux86'

Other commonly useful commands are:
 - 'devtool' and 'recipetool' handle common recipe tasks
 - 'bitbake-layers' handles common layer tasks
 - 'oe-pkgdata-util' handles common target package tasks

After the script is executed, it is equivalent to creating a project. The official project of Yocto is used here. The name is Yocto. It supports different images. The name has been printed during the script execution:

Common targets are:
    core-image-minimal
    core-image-full-cmdline
    core-image-sato
    core-image-weston
    meta-toolchain
    meta-ide-support

The image can be generated by bitmake followed by the above target, but it will not be used here for the time being.

Next, you need to use the bitmake layers tool to create layers. The commands are as follows:

jw@X1C:~/code/poky/build$ bitbake-layers create-layer ../meta-mylayer
NOTE: Starting bitbake server...
Add your new layer with 'bitbake-layers add-layer ../meta-mylayer'
jw@X1C:~/code/poky/build$ bitbake-layers add-layer ../meta-mylayer
NOTE: Starting bitbake server...

There are two steps here. The first step is to create a Layer and the second step is to add a Layer to the current project. It should be noted that after executing the OE init build env script through source, you will enter the new build directory. Therefore, when creating a Layer, you should pay special attention to using.. / meta mylayer to create the Layer to the root directory, otherwise the created custom Layer will be in the temporary build directory, which is meaningless.

After the meta mylayer is created, you can view its contents:

jw@X1C:~/code/poky/build$ tree ../meta-mylayer/
../meta-mylayer/
├── conf
│   └── layer.conf
├── COPYING.MIT
├── README
└── recipes-example
    └── example
        └── example_0.1.bb

3 directories, 4 files

Subsequent customized recipes need to be added here. In addition, all layers contained in the current project can be listed through bitmake layers:

jw@X1C:~/code/poky/meta-mylayer$ bitbake-layers show-layers
NOTE: Starting bitbake server...
layer                 path                                      priority
==========================================================================
meta                  /home/jw/code/poky/meta                   5
meta-poky             /home/jw/code/poky/meta-poky              5
meta-yocto-bsp        /home/jw/code/poky/meta-yocto-bsp         5
meta-mylayer          /home/jw/code/poky/meta-mylayer           6

The new meta mylayer is also included.

Custom recipe

Add a new directory recipes common in meta mylayer to store general recipes, and add helloworld recipes and their source code, as follows:

jw@X1C:~/code/poky/meta-mylayer/recipes-common$ tree
.
└── helloworld
    ├── files
    │   └── helloworld.c
    └── helloworld.bb

The first is the recipe file helloworld.bb:

DESCRIPTION = "Prints Hello World"

SRC_URI = "file://helloworld.c"

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

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

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

FILES_${PN} = "${bindir}/helloworld"

Here is a brief description of the formula:

  • DESCRIPTION: DESCRIPTION of the formula;
  • SRC_URI: the source code used by the recipe;
  • LICENSE: open source LICENSE for code use;
  • LIC_FILES_CHKSUM: the Checksum of the open source license file. The general MIT license, common, is used here_ LICENSE_ Dir refers to meta / files / common licenses, which contains all open source licenses;
  • do_compile(): the method of compiling source code;
  • do_install(): the installation method of the generated binary;
  • FILES_${PN}: specify the list of files to be placed in the Package (helloworld in the current example). The value of PN in the current example is helloworld. The value of the bindir variable is / usr/bin.

Then the source code helloworld.c:

#include <stdio.h>

int main()
{
  printf("hello world\n");
  return 0;
}

After putting the recipe into the custom Layer, you can view all the recipes contained in the project through bitmake layers, where you can see the helloworld recipe:

jw@X1C:~/code/poky/meta-mylayer$ bitbake-layers show-recipes
NOTE: Starting bitbake server...
Loading cache: 100% |                                           | ETA:  --:--:--
Loaded 0 entries from dependency cache.
Parsing recipes: 100% |##########################################| Time: 0:00:30
Parsing of 810 .bb files complete (0 cached, 810 parsed). 1424 targets, 42 skipped, 0 masked, 0 errors.
=== Available recipes: ===
helloworld:
  meta-mylayer         1.0

Note that many recipes are displayed here, but only the new helloworld recipes are listed. With the helloworld recipe, you can execute the following bitmake helloworld command. The following is the execution process:

jw@X1C:~/code/poky$ bitbake helloworld
Loading cache: 100% |############################################| Time: 0:00:00
Loaded 1424 entries from dependency cache.
NOTE: Resolving any missing task queue dependencies

Build Configuration:
BB_VERSION           = "1.49.0"
BUILD_SYS            = "x86_64-linux"
NATIVELSBSTRING      = "ubuntu-18.04"
TARGET_SYS           = "x86_64-poky-linux"
MACHINE              = "qemux86-64"
DISTRO               = "poky"
DISTRO_VERSION       = "3.2+snapshot-12c409c0dbf3c924ec7f76e51ca25a37481bfb5b"
TUNE_FEATURES        = "m64 core2"
TARGET_FPU           = ""
meta                 
meta-poky            
meta-yocto-bsp       
meta-mylayer         = "master:12c409c0dbf3c924ec7f76e51ca25a37481bfb5b"

Initialising tasks: 100% |#######################################| Time: 0:00:00
Sstate summary: Wanted 131 Found 0 Missed 131 Current 0 (0% match, 0% complete)
NOTE: Executing Tasks
Currently  3 running tasks (466 of 524)  88% |#############################    |
0: gcc-cross-x86_64-10.2.0-r0 do_compile - 55s (pid 20429)
1: perl-native-5.32.0-r0 do_install - 28s (pid 9495)
2: python3-native-3.9.1-r0 do_compile - 26s (pid 10984)

Since the current configuration depends on other tools and libraries, you can see that a lot of additional content (524 in total) needs to be generated, and this process needs to last for some time. After completion, you can see the generated content:

jw@X1C:~/code/poky/build/tmp/work/core2-64-poky-linux$ ll
 Total consumption 32
drwxrwxr-x 8 jw jw 4096 9 June 20-17:18 .
drwxrwxr-x 4 jw jw 4096 9 June 20-17:17 ..
drwxrwxr-x 3 jw jw 4096 9 June 20-17:18 gcc-runtime
drwxrwxr-x 3 jw jw 4096 9 June 20-17:17 glibc
drwxrwxr-x 3 jw jw 4096 9 June 20-17:18 helloworld
drwxrwxr-x 3 jw jw 4096 9 June 20-17:18 libgcc
drwxrwxr-x 3 jw jw 4096 9 June 20-17:17 libgcc-initial
drwxrwxr-x 3 jw jw 4096 9 June 20-17:17 linux-libc-headers

Here is the helloworld directory, which contains all the data in the project. However, the currently generated content has no practical effect, and it needs to be put into the Poky project image before it can be executed.

Generate image

Before generating an image, you need to add helloworld to the image. According to Yocto's manual, you can add image by adding_ INSTALL_ The append variable is added to build/conf/local.conf. The details are as follows:

IMAGE_INSTALL_append = "helloworld"

After that, the process of generating an image is nothing special. You only need to execute the previously mentioned bitmake plus the image name. Here, use core image minimal, which is the simplest one. The whole build process takes longer than building helloworld. After completion, it is shown as follows:

jw@X1C:~/code/poky$ bitbake core-image-minimal
Loading cache: 100% |                                           | ETA:  --:--:--
Loaded 0 entries from dependency cache.
Parsing recipes: 100% |##########################################| Time: 0:00:23
Parsing of 810 .bb files complete (0 cached, 810 parsed). 1424 targets, 42 skipped, 0 masked, 0 errors.
NOTE: Resolving any missing task queue dependencies

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

Initialising tasks: 100% |#######################################| Time: 0:00:01
Sstate summary: Wanted 7 Found 0 Missed 7 Current 1086 (0% match, 99% complete)
NOTE: Executing Tasks
NOTE: Tasks Summary: Attempted 3099 tasks of which 3082 didn't need to be rerun and all succeeded.

Here, the image is built, as follows:

jw@X1C:~/code/poky/build/tmp/deploy/images/qemux86-64$ ls
bzImage
bzImage--5.10.5+git0+47c7a3148a_f08df324cc-r0-qemux86-64-20210920095309.bin
bzImage-qemux86-64.bin
core-image-minimal-qemux86-64-20210920130948.qemuboot.conf
core-image-minimal-qemux86-64-20210920130948.rootfs.ext4
core-image-minimal-qemux86-64-20210920130948.rootfs.manifest
core-image-minimal-qemux86-64-20210920130948.rootfs.tar.bz2
core-image-minimal-qemux86-64-20210920130948.testdata.json
core-image-minimal-qemux86-64.ext4
core-image-minimal-qemux86-64.manifest
core-image-minimal-qemux86-64.qemuboot.conf
core-image-minimal-qemux86-64.tar.bz2
core-image-minimal-qemux86-64.testdata.json
modules--5.10.5+git0+47c7a3148a_f08df324cc-r0-qemux86-64-20210920095309.tgz
modules-qemux86-64.tgz

Execute helloworld

After the image is built, you can start it by executing runqemu:

As can be seen from the above figure, helloworld is executed.

Here, the task of including custom tools to the Poky project image is completed.

Tags: Linux yocto

Posted on Tue, 21 Sep 2021 00:05:48 -0400 by GamingWarrior