Module 4 · Lesson 107 of 156

Layers and creating one with bitbake-layers

Create a Yocto layer, register it with a build, and verify that BitBake discovers its recipes.

  • Beginner to advanced
  • 7 min read
  • 15 min lab
What you will need

Ubuntu 24.04 in WSL2 or a VM, with the Poky checkout and initialized build directory from the first Yocto build lesson. No board required.

Your place in the module
By the end you can 4 objectives
  • Identify the build directory file that selects active layers.
  • Explain the roles of BBPATH, BBFILES, and LAYERSERIES_COMPAT in layer.conf.
  • Create and register a layer using bitbake-layers.
  • Verify layer registration and recipe discovery with separate commands.
Video coming soonLayers and creating one with bitbake-layers
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from Recipe tasks and classes
  • The upstream build system determines which class should handle configuration, compilation and installation.
  • EXTRA_OECONF provides extra arguments to Autotools configure, and EXTRA_OECMAKE provides extra arguments to CMake.
  • do_install stages files under ${D}, and FILES:${PN} selects staged paths for the main package.
01

How a build selects its layers

2 min read

The last session wrote recipes in isolation; layers are where recipes and configuration live together. This lesson answers layer.conf, BBPATH and BBFILES, and bitbake-layers create-layer.

Open the conf/bblayers.conf file from your first Poky build. It shows a practical problem every Yocto project has to solve. OpenEmbedded Core supplies common recipes, Poky supplies reference distribution metadata, and your own project needs a place to maintain its recipes across upstream updates. These sources can live in different directories and be maintained by different teams. BBLAYERS is the list that tells this build which directories to use together.

A can contain recipes, classes, and configuration. Keeping product metadata in a layer you own gives it a clear location in version control. When you update the upstream checkout, you can review the product metadata and the upstream changes independently. Several build directories can use the same layer directory, each with its own list of active layers. For example, one build can include a layer for internal diagnostics while another leaves that layer out.

The two configuration files live in different places. The build keeps its selection in conf/bblayers.conf. Each selected layer keeps its discovery rules in conf/layer.conf, relative to that layer's root. A directory can exist on your host and contain valid recipes while remaining outside the build's selection. BitBake reads the configured layer roots and loads the conf/layer.conf in each of them. The root is the directory you pass to bitbake-layers add-layer, so a path ending in meta-embeddex/conf is one level too deep.

How BitBake reads enabled layersbblayers.confin thebuilddirectoryBBLAYERSpaths toenabledlayer rootsconf/layer.confread fromeach rootMetadatadiscoveryuses theloadedlayer configurationbblayers.confin thebuilddirectoryBBLAYERSpaths toenabledlayer rootsconf/layer.confread fromeach rootMetadatadiscoveryuses theloadedlayer configuration
The build lists layer roots, then BitBake reads each selected layer's configuration to discover metadata

There is a separate decision about what goes into an image. Adding a layer makes its metadata available to the build. An ordinary recipe in that layer can appear in a recipe listing before any image chooses to include its output. If you can see a recipe in bitbake-layers show-recipes, you have evidence that BitBake discovered it. Image contents are a separate question.

02

What layer.conf tells BitBake

3 min read

Inside a layer's configuration, ${LAYERDIR} refers to the current layer's root. The generated file uses it to describe paths relative to the layer itself, so the discovery rules can be used from different checkout locations. These are two lines you will see in the layer created below.

text
BBPATH .= ":${LAYERDIR}"
BBFILES += "${LAYERDIR}/recipes-*/*/*.bb \
            ${LAYERDIR}/recipes-*/*/*.bbappend"

BBPATH is a colon-separated search path BitBake uses when looking up metadata such as configuration and class files. The .= operator adds text without inserting a separator, so the line includes the colon explicitly. Adding the layer root gives BitBake a location under which to look for the matching configuration or class path. For classes, the relevant file is a .bbclass file. This is a search path for BitBake metadata, distinct from the shell's PATH for executables.

BBFILES is a space-separated list of file patterns. The += operator adds the layer's patterns with a separating space. BitBake expands these patterns to discover .bb recipes and .bbappend files. A .bbappend extends a matching recipe. The pattern in the example uses pathname wildcards. As with the pathname patterns described in glob(7), the * in a directory component matches names at that level. BitBake expands BBFILES when it reads the metadata.

Read the recipe pattern one slash at a time. It expects a directory starting with recipes- under the layer root, then a subdirectory, then a file ending in .bb. The path recipes-example/example/example_0.1.bb fits. A file placed directly in recipes-example/ misses the subdirectory level. If show-layers lists the layer but show-recipes omits a new recipe, compare the recipe's relative path with the patterns in BBFILES.

Three settings in layer.confBBPATHConfigurationlookupClass lookupBBFILESRecipepatternsAppendpatternsLAYERSERIES_COMPATSupported core series
BBPATH supplies search locations, while BBFILES supplies file patterns and LAYERSERIES_COMPAT declares release support

The generated file also defines a collection name through BBFILE_COLLECTIONS. You will see that identifier again in variable names such as BBFILE_PATTERN_<collection> and LAYERSERIES_COMPAT_<collection>. The generator uses the name of the layer directory, so a layer created as meta-embeddex gets LAYERSERIES_COMPAT_meta-embeddex; in a real run with a layer directory named meta-course, the generated conf/layer.conf had BBFILE_COLLECTIONS += "meta-course", BBFILE_PATTERN_meta-course = "^${LAYERDIR}/", BBFILE_PRIORITY_meta-course = "6", LAYERDEPENDS_meta-course = "core" and LAYERSERIES_COMPAT_meta-course = "scarthgap". Layers written by other people can use a collection name that differs from the directory name (Poky's meta-poky uses yocto), so read the identifier from the file.

LAYERSERIES_COMPAT declares the OpenEmbedded Core release series that a collection supports. Its value contains one or more release codenames separated by spaces. The create command uses the active build's core series (the LAYERSERIES_CORENAMES of the core layer) when generating the layer configuration. The codename comes from the Yocto metadata you checked out. Two people using the same Ubuntu release can have different Yocto series because they checked out different branches. If BitBake reports that an existing layer is incompatible, look for the layer revision that supports your core series. Editing the declaration alone cannot make the metadata compatible.

Going deeperExplore the mechanism

BBFILE_PRIORITY is set per collection as BBFILE_PRIORITY_<collection>. Among competing recipes for the same recipe name, a recipe from a higher-priority layer takes precedence over one from a lower-priority layer even when the lower-priority recipe has a higher version. bitbake-layers show-layers lists the priorities alongside the paths: in a real run it showed core, yocto and yoctobsp at priority 5 and the new meta-course layer at 6, so its recipes win over same-named recipes in the Poky layers.

When multiple .bbappend files match a recipe, BitBake applies all matching appends. The Yocto Development Manual (section "Prioritizing Your Layer") says that priority values also affect the order in which multiple .bbappend files for the same recipe are applied. In increasing priority, an append from a higher-priority layer is parsed later (this order was taken from the documentation and not tested here). That gives a later plain assignment the chance to replace an earlier plain assignment to the same variable. The normal BitBake assignment and override rules still determine the final value, and contributions from several appends can accumulate. Look at the matching files with bitbake-layers show-appends before attributing the result to one layer.

03

Create, add, and verify a layer

3 min read

Use the shell where you initialized the Poky build environment and entered the build directory. The example assumes your build directory is directly inside the Poky checkout, as with the default build directory. The relative path ../meta-embeddex then places the layer beside build. If you initialized a build elsewhere, use an absolute path to the intended new layer directory in both commands. Check where you are with pwd before choosing a relative path.

bash
bitbake-layers create-layer ../meta-embeddex

Run this command for a new directory. It creates a scaffold on disk, including conf/layer.conf, a README, an MIT license file, and an example recipe. A real run produced exactly COPYING.MIT, README, conf/layer.conf and recipes-example/example/example_0.1.bb. The example recipe is a file you can use to verify discovery immediately. Open ../meta-embeddex/conf/layer.conf and locate the file patterns and compatibility declaration. Then trace the recipe's path from the layer root through recipes-example and example to example_0.1.bb. Those directory levels explain why the file matches the generated pattern.

Files created for meta-embeddexmeta-embeddexREADMECOPYING.MITconflayer.confrecipes-exampleexampleexample_0.1.bbmeta-embeddexREADMECOPYING.MITconflayer.confrecipes-exampleexampleexample_0.1.bb
The generated layer contains configuration, documentation, a licence file, and an example recipe

Now register the layer with the active build and check discovery.

bash
bitbake-layers add-layer ../meta-embeddex
bitbake-layers show-layers
bitbake-layers show-recipes example

add-layer updates BBLAYERS in the current build directory's conf/bblayers.conf. show-layers lets you verify the registered directory. Compare its path with the one you intended to add, since another checkout can contain a layer with the same name. show-recipes example lets you verify that BitBake found the generated recipe. Check that the entry is associated with the layer you created. These checks inspect build metadata without building an image. In the real run, add-layer appended the new root to BBLAYERS as an absolute path, and bitbake-layers show-recipes example printed === Matching recipes: ===, example: and meta-course 0.1.

If a command fails, work from the directory toward the recipe. Start by checking that the layer root exists and contains conf/layer.conf. Then look at the current build's conf/bblayers.conf to see whether the intended root was registered. Read any compatibility or dependency error before investigating recipe paths. Once the layer is listed successfully, inspect BBFILES and the file layout if the recipe is missing. This order helps you locate the first point at which the build and the filesystem disagree.

The next lesson, Appending to the kernel recipe, answers how a file in your own layer can extend the kernel recipe supplied by another layer. The registered layer gives BitBake a place to discover that file.

Key takeaways

  • Each build selects its active layers through BBLAYERS in conf/bblayers.conf.
  • BBPATH provides a search path for configuration and class files, while BBFILES supplies patterns for recipe and append files.
  • LAYERSERIES_COMPAT declares the OpenEmbedded Core release series a layer supports.
  • Creating a layer writes files to disk, and adding it registers its path with the current build.
Reflect
  1. Explain to a colleague, in two sentences: identify the build directory file that selects active layers.
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Find your next step

↑ ↓ to move · Enter to open · Esc to close