Overwriting Yocto Classes Through Meta-Layer

Overwriting Yocto Classes through meta-layer

Generally in Yocto there is no way to override .bbclass files like with .bb files (using .bbappend), to archive that it is needed to copy whole class file and move to another layer, I was able to manage that with this configuration:

layer structure:

$ tree ../meta-test/
../meta-test/
├── classes
│   └── image-live.bbclass
├── conf
│   └── layer.conf
├── COPYING.MIT
├── README
└── recipes-example
└── example.bb

3 directories, 5 files

content of example.bb recipe:

$ cat ../meta-test/recipes-example/example/example.bb 
LICENSE = "CLOSED"
inherit image-live

and finally really important thing*, the configuration file conf/bblayers.conf needs to be configured with this order meta-test/ above meta/ layer:

$ tail -n6 conf/bblayers.conf 
BBLAYERS ?= " \
/home/user/poky/meta-test \
/home/user/poky/meta \
/home/user/poky/meta-poky \
/home/user/poky/meta-yocto-bsp \
"

$ bitbake -e example -D | grep ^DEBUG:\\sInheriting\\s.*image-live.bbclass\\s\(from
DEBUG: Inheriting /home/user/poky/meta-test/classes/image-live.bbclass (from /home/user/poky/meta-test/recipes-example/example/example.bb:3)

*I don't know why bitbake layer priority doesn't work here, only modifying layers order in conf/bblayers.conf allows me to achieve the main goal:

$ bitbake-layers show-layers
NOTE: Starting bitbake server...
layer path priority
==========================================================================
meta /home/user/poky/meta 5
meta-test /home/user/poky/meta-test 10
meta-poky /home/user/poky/meta-poky 5
meta-yocto-bsp /home/user/poky/meta-yocto-bsp 5

layer meta-test/ below meta/ in conf/bblayers.conf:

$ tail -n6 conf/bblayers.conf 
BBLAYERS ?= " \
/home/user/poky/meta \
/home/user/poky/meta-test \
/home/user/poky/meta-poky \
/home/user/poky/meta-yocto-bsp \
"

$ bitbake -e example -D | grep ^DEBUG:\\sInheriting\\s.*image-live.bbclass\\s\(from
DEBUG: Inheriting /home/user/poky/meta/classes/image-live.bbclass (from /home/user/poky/meta-test/recipes-example/example/example.bb:3)

How to extend BitBake class

Create classes folder in your meta layer and create a new class, e.g. myclass.bbclass. Inherit original class with inherit original-bitbake-CLASS and add whatever functionality you need.

Then use the new bbclass instead of the original.

Upgrading (overriding) an entire recipe in a custom layer

The way you proceeded is the right way, you should add new recipes/bbappend files on your own layer(s), you shouldn't modify Yocto's base layers nor third-party layers by default. Nevertheless, since you want to add a newer version of an existing recipe, you should keep in mind that:

  • You have to check the priority of your own layer and the priority of the layer that contains the original recipe. Yocto will pick the recipe of the higher priority layer, no matter if it is a newer version or not (ignoring PV). For further information, search for BBFILE_PRIORITY in the Yocto Project Reference Manual. (You can also see a list of all configured layers along with their priorities with the command bitbake-layers show-layers)
  • In the case of both layers having the same priority, Yocto will build the recipe with the highest PV (you can check/set this value inside your recipe or in its filename recipename_pv.bb). Alternatively, if you wish to select another version rather than the one that is being currently built, you can just set the variable PREFERRED_VERSION_recipename = desiredPV in your distro.conf or local.conf file.

Excluding yocto meta-layer depending on a variable value

Yes, you can pass your environment variable into the build environment and then use it to conditionally add the extra layer(s).

You'll need to modify your bblayers.conf to store a default value for NIGHTCORE_ENABLED and to add the extra layer(s) to BBLAYERS if it is set to 1:

NIGHTCORE_ENABLED ?= "0"  # overridden by env if specified in BB_ENV_EXTRAWHITE
NIGHTCORE_LAYERS ?= "/path/to/poky/meta-nightcore"

BBLAYERS ?= " \
/path/to/poky/meta \
/path/to/poky/meta-poky \
/path/to/poky/meta-yocto-bsp \
${@bb.utils.contains('NIGHTCORE_ENABLED', '1', '${NIGHTCORE_LAYERS}', '', d)} \
"

Then, you need to tell Bitbake to allow your environment variable to be captured into the Bitbake datastore by adding it to BB_ENV_EXTRAWHITE:

export NIGHTCORE_ENABLED=1
export BB_ENV_EXTRAWHITE="${BB_ENV_EXTRAWHITE} NIGHTCORE_ENABLED"

You can then run bitbake <image_name>.

Because bblayers.conf is usually generated when source oe-init-build-env is run for the first time, you may wish to use TEMPLATECONF to create a bblayers.conf.sample file that already includes this extra logic.

There's some related answers here too:

Is it possible to pass in command line variables to a bitbake build?

Yocto - Why can't I override the build task?

The base bbclass file (meta/classes/base.bbclass) sets:

do_build[noexec] = "1

which means the content of the function is not executed and it is just a placeholder task for the dependency graph. This is why you never see output from the build task.

As mentioned in other answers, there are default dependencies which are why other recipes execute when you try and run "standard" tasks like do_build.

Override compatibility of recipe

While writing this question I figured that the latter option seems most suitable. Given that COMPATIBLE_MACHINE needs to be a regular expression appending my own machine definition simply does not work (e.g. using using COMPATIBLE_MACHINE_append). Thus, I had to override the compatible machine in my ownxradio.bbappend`:

COMPATIBLE_MACHINE = "opi-zero-fix"

This works for now.

Use a yocto layer's recipe or class without inheriting all bbappends

You could mask all directories from meta-qt5 later except the classes dir. Add to you local.conf:

BBMASK += "meta-qt5/recipes-*/"

More on BBMASK here:
https://www.yoctoproject.org/docs/latest/ref-manual/ref-manual.html



Related Topics



Leave a reply



Submit