【yocto】自作bbclassの作り方

# はじめに
- bbclassは共通処理をまとめたもの
    - bbclass：共通設定(どのイメージをbitbakeをしても実行されるような処理)
        　↕
    - bb/bbappend：個別設定(レシピ毎に処理)

# 方法
- classesディレクトリの下にxxx.bbclassを配置する
- local.confに`INHERIT += "xxx"`を追記する

# 例. hello!!を表示するbbclass
- hello.bbclassをmeta/classesの下に配置する
    - hello.bbclassはhelloと表示するdo_helloタスクだけを実行する
    - do_hello[doc]には説明文を記載する
    - add_taskでdo_helloをdo_rootfsとdo_image間で実行するように設定する
~~~
takeshi@X250:~/yocto/poky$ cat meta/classes/hello.bbclass 
python do_hello () {
    bb.plain("hello!!");
}

do_hello[doc]="echo hello"
addtask do_hello after do_rootfs before do_image
~~~
- oe-init-build-envを実施して、bitbakeを実施できるようにする
~~~
takeshi@X250:~/yocto/poky$ source oe-init-build-env

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

You can now run 'bitbake <target>'

Common targets are:
    core-image-minimal
    core-image-sato
    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
~~~
- local.confでhello.bbclassを使用するように記載する
~~~
takeshi@X250:~/yocto/poky/build$ cat conf/local.conf | tail -n 1
INHERIT += "hello"
~~~
- listtasksでdo_helloとdo_helloの説明文が反映されていることを確認
~~~
takeshi@X250:~/yocto/poky/build$ bitbake -c listtasks core-image-minimal | grep "do_hello"
do_hello                       echo hello
~~~
- bitbakeを実行すると、hello!!と表示される
~~~
takeshi@X250:~/yocto/poky/build$ bitbake core-image-minimal
WARNING: Host distribution "zorin-15" has not been validated with this version of the build system; you may possibly experience unexpected failures. It is recommended that you use a tested distribution.
Loading cache: 100% |####################################################################################################################################| Time: 0:00:00
Loaded 1298 entries from dependency cache.
NOTE: Resolving any missing task queue dependencies

Build Configuration:
BB_VERSION           = "1.44.0"
BUILD_SYS            = "x86_64-linux"
NATIVELSBSTRING      = "universal"
TARGET_SYS           = "x86_64-poky-linux"
MACHINE              = "qemux86-64"
DISTRO               = "poky"
DISTRO_VERSION       = "3.0"
TUNE_FEATURES        = "m64 core2"
TARGET_FPU           = ""
meta                 
meta-poky            
meta-yocto-bsp       = "my-yocto-3.0:94f6b31befda5c496f65e863a6f8152b42d7ebf0"

Initialising tasks: 100% |###############################################################################################################################| Time: 0:00:02
Sstate summary: Wanted 818 Found 815 Missed 3 Current 0 (99% match, 0% complete)
NOTE: Executing Tasks
NOTE: Setscene tasks completed
hello!!
NOTE: Tasks Summary: Attempted 2655 tasks of which 2424 didn't need to be rerun and all succeeded.

Summary: There was 1 WARNING message shown.
~~~
- do_helloがdo_rootfsとdo_imageの間で実行されていることも確認
~~~
takeshi@X250:~/yocto/poky/build$ cat tmp/work/qemux86_64-poky-linux/core-image-minimal/1.0-r0/temp/log.task_order 
do_listtasks (10433): log.do_listtasks.10433
do_populate_lic_setscene (11459): log.do_populate_lic_setscene.11459
do_prepare_recipe_sysroot (20078): log.do_prepare_recipe_sysroot.20078
do_rootfs (20107): log.do_rootfs.20107
do_image_qa (26249): log.do_image_qa.26249
do_write_qemuboot_conf (26250): log.do_write_qemuboot_conf.26250
do_hello (26253): log.do_hello.26253
do_image (26261): log.do_image.26261
do_image_ext4 (26279): log.do_image_ext4.26279
do_image_tar (26287): log.do_image_tar.26287
do_image_complete (26326): log.do_image_complete.26326
do_populate_lic_deploy (26345): log.do_populate_lic_deploy.26345
~~~