前言

在準備完開發環境之後,接下來就要開始加上我們的設定、服務或是應用了。

主要內容

在進行下列操作前,我們需要先初始化 Yocto 環境,如果還不知道要如何進行的同學,可以參考這邊

建立我們的 “層”

建議是將我們的 Layer 放在建置資料夾( first-build )的外面。所以我們將在它的上一層目錄建立我們的 Layer meta-first-layer

1cd ..
2ls # first-build poky 
3bitbake-layers create-layer meta-first-layer
4ls # first-build meta-first-layer poky 

加入我們的 “層”

在建置資料夾( first-build ) 中執行下列指令,它會幫我們更新 conf/bblayer.bb 的內容。

1cd first-build
2bitbake-layers add-layer ../meta-first-layer

Recipes-example

我們在建立 meta-first-layer 時,bitbake-layers 會順便幫我們建立 recipes-example。 我們可以透過下列指令,看到它的輸出結果。

1bitbake example

執行結果:

執行結果

執行結果

建立我們的 Recipes-Hello

Yocto 專案提供了許多便捷的工具,其中 recipetool 與 devtool 便是與 Recipes 較為相關。
接下來我們將會使用 recipetool 來建立我們的 Recipes。

建立 recipes-hello 資料夾,並在 files 中放入我們的源始碼。

1mkdir -p recipes-hello/hello/files
2cd recipes-hello/hello

main.c:

1#include <stdio.h>
2
3int main(int argc, char *argv[]) {
4  printf("Hello\n");
5  return 0;
6}

Makefile:

 1SRCS := main.c
 2OBJS := $(SRCS:.c=.o)
 3bindir ?= /usr/bin
 4
 5TARGET := hello
 6
 7all: $(TARGET)
 8
 9%.o : %.c
10	$(CC) -c $< -o $@ ${LDFLAGS}
11
12$(TARGET) : $(OBJS)
13	$(CC) $^ -o $@ ${LDFLAGS}
14
15install:
16	install -d $(DESTDIR)$(bindir)
17	install -m 755 $(TARGET) $(DESTDIR)$(bindir)/

使用 recipetool 建立 recipe

1recipetool create -o hello_0.1.bb files

修改產生出的 hello_0.1.bb

 1LICENSE = "CLOSED"
 2LIC_FILES_CHKSUM = ""
 3
 4SRC_URI = " \
 5           file://main.c \
 6           file://Makefile \
 7           "
 8
 9S = "${WORKDIR}"
10
11do_configure () {
12}
13
14do_compile () {
15	oe_runmake
16}
17
18do_install () {
19    oe_runmake install 'DESTDIR=${D}'
20}

回到 first-build 進行測試

1bitbake hello

可以正常的進行編譯,但在最後出現了問題。

ERROR: hello-0.1-r0 do_package_qa: QA Issue: File /usr/bin/hello in package hello doesn’t have GNU_HASH (didn’t pass LDFLAGS?) [ldflags]
ERROR: hello-0.1-r0 do_package_qa: QA run found fatal errors. Please consider fixing them.
ERROR: Logfile of failure stored in: /home/yuan/first-build/tmp/work/cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi/hello/0.1-r0/temp/log.do_package_qa.2726283
ERROR: Task (/home/yuan/meta-first-layer/recipes-hello/hello/hello_0.1.bb:do_package_qa) failed with exit code ‘1’

參考這篇文章修改 hello_0.1.bb

1do_install () {
2    oe_runmake install 'DESTDIR=${D}'
3}
4+
5+ INSANE_SKIP_${PN} = "ldflags"
6+ INSANE_SKIP_${PN}-dev = "ldflags"

建立我們的 image

在 recipes-hello 中建立 images 資料夾。

1mkdir -p recipes-hello/images
2cd recipes-hello/images
3cp ../../../poky/meta/recipes-core/images/core-image-minimal.bb hello-image.bb

hello-image.bb:

 1SUMMARY = "A hello image that just for testing our layer"
 2
 3LICENSE = "MIT"
 4
 5IMAGE_INSTALL = "packagegroup-core-boot ${CORE_IMAGE_EXTRA_INSTALL} hello"
 6
 7inherit core-image
 8
 9IMAGE_ROOTFS_SIZE ?= "8192"
10IMAGE_ROOTFS_EXTRA_SPACE_append = "${@bb.utils.contains("DISTRO_FEATURES", "systemd", " + 4096", "" ,d)}"

回到 first-build 進行測試

1bitbake hello-image
2runqemu hello-image nographic slirp

另一個建立 Recipes-hello 的方法

在工作目錄 ( first-build ) 使用 devtool 加入我們寫好的 hello 至 工作目錄中。

1devtool add hello ../hello

這個動作會幫我們在 conf/bblayer.bb 中加入 workspace 層,並建立 workspace/recipes/hello 資料夾。

使用下列指令編譯 hello

1devtool build hello
2ls 

可以發現它確實多了 Workspace Layer

使用 devtool 建立臨時測試用的 image,並使用 qemu 進行驗證。

1devtool build-image core-image-minimal
2runqemu core-image-minimal nographic slirp

在測試沒有問題之後,再使用 devtool 將 hello 加入我們的 Layer

1devtool finish hello ../meta-first-layer

小結

我們已經建立了自己的 Layer、Recipe 以及 Image。 接下來我們就可以試著建立屬於我們的 Distribution 和 Machine 了。

參考連結