# Yocto 添加自訂 Layer 與編譯 HelloWorld 測試
## 1. 初始化環境
```sh
$ source poky/oe-init-build-env helloqemu_build
```
## 2. 建立自訂 Layer
```sh
$ bitbake-layers create-layer hello
$ mv hello ../poky/meta-hello
```
## 3. 添加 Layer
```sh
$ bitbake-layers add-layer ../poky/meta-hello
```
## 4. 確認 Layer 是否成功添加
```sh
$ bitbake-layers show-layers
```
### 預期輸出:
```
NOTE: Starting bitbake server...
layer path priority
========================================================================================================
core /home/macd/test_qemu/poky/meta 5
yocto /home/macd/test_qemu/poky/meta-poky 5
yoctobsp /home/macd/test_qemu/poky/meta-yocto-bsp 5
hello /home/macd/test_qemu/poky/meta-hello 6
```
:::info
目的:
1.檢視當前環境的 Layers:列出目前 Yocto 构建环境中可用的所有層(layers),包含其路徑與優先級。
2.除錯與驗證:幫助開發者確認 layers 是否正確加入 bblayers.conf,確保 Yocto 環境設置無誤。
3.管理與維護 Layers:在開發過程中,隨著不同的 meta-layer(如 meta-openembedded、meta-custom)加入或移除,該指令可以用來檢查當前活躍的 layers。
:::
---
## 5. 設置目錄結構
```sh
$ cd ~/test_qemu/poky/meta-hello/recipes-local
$ tree ./
```
### 預期目錄結構:
```
./
└── helloworld
├── files
│ └── helloworld.c
└── helloworld_0.1.bb
```
---
## 6. 參考範例 `.bb` 配方檔
```sh
DESCRIPTION = "A friendly program that prints Hello World!"
PRIORITY = "optional"
SECTION = "examples"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
SRC_URI = "file://helloworld.c"
S = "${WORKDIR}"
do_compile() {
${CC} ${CFLAGS} ${LDFLAGS} helloworld.c -o helloworld
}
do_install() {
install -d ${D}${bindir}
install -m 0755 helloworld ${D}${bindir}
}
```
::: danger
> LICENSE = "MIT"
> LIC_FILES_CHKSUM = > "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
這兩行可以參考其他 .bb 檔案的寫法,直接複製過來使用即可。
:::
---
## 7. `local.conf`完整版
```sh
MACHINE ?= "qemuarm"
#
DISTRO ?= "poky"
EXTRA_IMAGE_FEATURES ?= "debug-tweaks"
#
USER_CLASSES ?= "buildstats"
#
# By default disable interactive patch resolution (tasks will just fail instead):
PATCHRESOLVE = "noop"
BB_DISKMON_DIRS ??= "\
STOPTASKS,${TMPDIR},1G,100K \
STOPTASKS,${DL_DIR},1G,100K \
STOPTASKS,${SSTATE_DIR},1G,100K \
STOPTASKS,/tmp,100M,100K \
HALT,${TMPDIR},100M,1K \
HALT,${DL_DIR},100M,1K \
HALT,${SSTATE_DIR},100M,1K \
HALT,/tmp,10M,1K"
PACKAGECONFIG:append:pn-qemu-system-native = " sdl"
CONF_VERSION = "2"
IMAGE_INSTALL:append = " helloworld "
LICENSE_FLAGS_ACCEPTED = "commercial"
```
:::success
IMAGE_INSTALL:append = " helloworld "
MACHINE ?= "qemuarm"
1️⃣ `IMAGE_INSTALL:append = " helloworld "` → 將 `helloworld` 軟體包加入最終映像檔。
2️⃣ `MACHINE ?= "qemuarm"` → 指定目標硬體為 QEMU 模擬的 ARM 架構。
:::
---
---
## 9. 編譯 Yocto 映像檔
```sh
$ bitbake core-image-minimal
```
---
## 10. 啟動 QEMU
```sh
$ runqemu qemuarm nographic
```
---
:::success
登入 : root
:::
## 11. 測試 HelloWorld
在 QEMU 內部:
```sh
$ cd /usr/bin
$ ls | grep he
```
### 預期輸出:
```
head
helloworld
hexdump
```
執行 `helloworld`:
```sh
$ helloworld
```
---
這樣就能成功在 Yocto 內建測試 `helloworld` 了 🚀
:::warning
> **如何退出qemu**
`ps aux | grep qemu`
`kill -9 <PID>`
:::
# .bb檔內的參數
:::info
使用情境 .bb檔案內有一堆參數,卻不知道參數代表甚麼值
:::
假設我希望知道helloword 內部的D參數,有三個做法**推薦第一個**
1. `bitbake-getvar -r helloworld D`
```shell=
macd@macsever:~/test_qemu/helloqemu_build$ bitbake-getvar -r helloworld D
#
# $D [2 operations]
# set /home/macd/test_qemu/poky/meta/conf/bitbake.conf:409
# "${WORKDIR}/image"
# set /home/macd/test_qemu/poky/meta/conf/documentation.conf:131
# [doc] "The destination directory."
# pre-expansion value:
# "${WORKDIR}/image"
D="/home/macd/test_qemu/helloqemu_build/tmp/work/cortexa15t2hf-neon-poky-linux-gnueabi/helloworld/0.1/image"
```
:::success
bitbake-getvar 命令是用來查詢 BitBake 變數的工具,它可以幫助你找到變數在哪些檔案中被定義或修改,並顯示變數的具體值。當你使用這個命令時,除了顯示變數的值,還會告訴你該變數的設定來自於哪個檔案或路徑,這對於調試和追蹤變數的來源非常有用。
:::
2. 透過命令行
```shell!
bitbake helloworld -e | grep ^D=
```
```shell!
D="/home/macd/test_qemu/helloqemu_build/tmp/work/cortexa15t2hf-neon-poky-linux-gnueabi/helloworld/0.1/image"
```
3.查看官網(先看官網)
[Variables Glossary](https://docs.yoctoproject.org/ref-manual/variables.html#term-PKGDATA_DIR)
bitbake -c clean helloworld
會將`<構建目錄>/tmp/work/cortexa15t2hf-neon-poky-linux-gnueabi/helloworld`裡的資料夾全部清除