Module 4 · Lesson 103 of 156

BitBake variables and overrides

Predict how BitBake assignments combine, select values using active overrides, and verify the effective value for a recipe.

  • Beginner to advanced
  • 8 min read
  • 13 min lab
What you will need

Ubuntu 24.04 in WSL2 or a VM with the course Yocto build directory and its BitBake environment initialized

Your place in the module
By the end you can 4 objectives
  • Predict the result of direct, default, and concatenating assignments.
  • Translate a legacy conditional assignment into modern override syntax.
  • Identify machine, distribution, and recipe tokens in a recipe's OVERRIDES list.
  • Explain which of two active single-token overrides supplies a value and verify it with bitbake -e.
Video coming soonBitBake variables and overrides
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from Flashing, runqemu and BitBake basics
  • Write a board-specific Wic image to the whole destination device so its partition table is written.
  • bmaptool can use a matching block map to skip unmapped ranges, while dd copies the uncompressed image byte for byte.
  • runqemu uses QEMU build artifacts for the selected emulated machine.
01

Assign values and combine strings

4 min read

The first build ran recipes you did not write; now you learn the language they are written in. This lesson answers the assignment operators, the OVERRIDES list and the _ syntax, and how precedence resolves conflicts.

The BitBake User Manual documents a practical problem with build metadata. Two lines that both appear to add text can produce different results when the variable has a weak default. A change to build flags or a list of options can therefore discard a default you expected to keep. Start by identifying what each line does to the variable, then consider what BitBake sees when it expands the value.

BitBake reads assignments in metadata files such as recipes (.bb) and configuration files (.conf). An assignment has a variable name on the left and a quoted value on the right. The examples use DEMO_ names so you can examine the value without changing a real build setting. A word list such as "serial logging" is a string with a space in it. The metadata that consumes the string decides how to interpret it.

bitbake
DEMO_MODE = "serial"
DEMO_LABEL = "mode-${DEMO_MODE}"
DEMO_MODE = "network"

The final DEMO_MODE is network. The expanded DEMO_LABEL is mode-network because = stores the reference ${DEMO_MODE} for expansion when the value is used. When you read a reference in an assignment, keep tracing the referenced variable through the metadata. The value visible directly above the reference may differ from the one used during expansion.

?= sets an ordinary value immediately if the variable has no ordinary value at that point. Given DEMO_PORT ?= "serial" followed by DEMO_PORT ?= "network", the value is serial. A later DEMO_PORT = "network" sets it to network. An explicit empty assignment, DEMO_PORT = "", also counts as a value. A subsequent ?= leaves it empty.

??= sets a . Each ??= updates that fallback. Two successive weak defaults of "serial" and "network" supply network if nothing sets an ordinary value. A ?= "local" can set the ordinary value even after both weak defaults have been read. If an ordinary value already exists, a subsequent ??= updates the fallback and the ordinary value still applies.

BitBake assignment operatorsOperatorEffectDirect=Set valueDefault?=Set if unsetWeak default??=Set fallbackWith space+= and =+Append orprependWithout space.= and =.Append orprepend
What each assignment operator changes as BitBake parses metadata

The four concatenating operators act on the value at the point BitBake parses the line. += appends and =+ prepends, inserting a space at the join. .= appends and =. prepends, adding exactly the characters you supply. From a starting value of "base", the four lines below produce start base end and pre-base.cfg respectively.

bitbake
DEMO_WORDS = "base"
DEMO_WORDS += "end"
DEMO_WORDS =+ "start"

DEMO_NAME = "base"
DEMO_NAME .= ".cfg"
DEMO_NAME =. "pre-"

A later = for the same variable can replace the string assembled by those parse-time operations. A weak default also matters here. With DEMO_LIST ??= "base" followed by DEMO_LIST += "end", the value is " end" with a leading space. += creates an ordinary value from the empty starting string and the appended text. The weak default stays unused. With DEMO_LIST:append = " end" in place of the += line, the expanded value is "base end". The override-style :append applies after the weak default is available and uses the leading space you wrote inside the quotes.

02

Select a value for the current build context

2 min read

An lets you give a variable a general value and a different value for a selected build context. Imagine that your project uses a standard set of diagnostic options with an extra setting for the qemuarm64 machine. One set of metadata can express both cases.

bitbake
DEMO_DIAGNOSTICS = "standard"
DEMO_DIAGNOSTICS:qemuarm64 = "verbose"

In modern BitBake, read DEMO_DIAGNOSTICS:qemuarm64 as the qemuarm64 variant of DEMO_DIAGNOSTICS. When qemuarm64 is active, the conditional assignment supplies verbose. In a context without that token, this example supplies standard. In older Yocto metadata the corresponding spelling was DEMO_DIAGNOSTICS_qemuarm64 = "verbose". That is the legacy _<override> syntax. For current metadata use :<override>. Keep underscores that belong to the variable name itself, as in DEMO_DIAGNOSTICS.

OVERRIDES contains the active tokens separated by colons. Check the entire token between separators. A token called qemuarm64 is a different condition from one called qemuarm. OpenEmbedded builds this list from information that includes the selected machine, the distribution, and the recipe being evaluated.

  • MACHINE identifies the build's machine. The machine tokens come through MACHINEOVERRIDES, which can include both the machine name and shared tokens for related machines.
  • DISTRO identifies the distribution configuration. DISTROOVERRIDES normally includes the distribution name and can contain additional distribution tokens.
  • PN is the recipe name in the recipe context. OpenEmbedded adds pn-${PN} to OVERRIDES, so pn-busybox is active when evaluating the recipe whose PN is busybox.
Sources of override tokensOVERRIDESMACHINEOVERRIDESMachine and shared machine tokensDISTROOVERRIDESDistribution tokenspn-${PN}Current recipe tokenOVERRIDESMACHINEOVERRIDESMachine and shared machinetokensDISTROOVERRIDESDistribution tokenspn-${PN}Current recipe token
Machine, distribution, and recipe information contribute tokens to OVERRIDES

The pn- prefix lets you target a single recipe from shared configuration. For instance, DEMO_DIAGNOSTICS:pn-busybox = "verbose" defines a value for the BusyBox recipe. The name to match is its PN. For diagnosis, inspect the recipe whose value matters, because recipe-specific tokens depend on that choice.

A conditional operation can also preserve the selected value and add text. DEMO_DIAGNOSTICS:append:qemuarm64 = " trace" appends trace when qemuarm64 is active. If both machine lines are present, the selected verbose becomes verbose trace. On a machine without the token, the base value stays standard in this example. The leading space comes from the quoted string. This form is useful for adding an option to an existing list whose full contents are defined elsewhere.

03

Resolve and inspect the effective value

3 min read

A recipe can match a machine condition and a recipe condition at the same time. Consider these three definitions from shared metadata.

bitbake
DEMO_DIAGNOSTICS = "standard"
DEMO_DIAGNOSTICS:pn-busybox = "recipe"
DEMO_DIAGNOSTICS:qemuarm64 = "machine"

If neither condition is active, the value is standard. If only one is active, that variant supplies the value. When both are active, look at their order in the recipe's effective OVERRIDES. For two competing assignments qualified by distinct single override tokens, the token farther to the right wins. With the relevant tokens ordered as qemuarm64:pn-busybox, the result is recipe. With those tokens ordered as pn-busybox:qemuarm64, the result is machine. Both results were confirmed by parsing these lines with the BitBake library of the scarthgap branch.

Reversing the two conditional assignment lines in the example leaves the result the same for a given list order. Those lines define different variants. If you write two ordinary = assignments for the same variant, such as two assignments to DEMO_DIAGNOSTICS:qemuarm64, the later parsed assignment sets that variant's value. To diagnose a conflict, first establish the value assigned to each variant, then use OVERRIDES to decide which variant BitBake selects.

Two active override assignmentsqemuarm64Earlieractivetokenpn-busyboxLateractivetokenRecipevalueSelectedfrompn-busyboxqemuarm64Earlieractivetokenpn-busyboxLateractivetokenRecipevalueSelectedfrompn-busybox
For two active single-token assignments, the one later in OVERRIDES supplies the value

In the initialized build environment, ask BitBake for the environment of the recipe you are investigating. The following command uses BusyBox as the target. It parses the metadata and reports the effective values without running a build.

bash
bitbake -e busybox | grep -E '^(MACHINE|DISTRO|PN|OVERRIDES|DEMO_DIAGNOSTICS)='

The pipe sends the environment report to grep. Bash single quotes preserve the expression for grep -E, which uses ^ to match the beginning of a line and | inside the parentheses to select any of the named variables. The trailing = limits matches to assignment lines. DEMO_DIAGNOSTICS appears if you defined it in metadata visible to that recipe. Your selected machine, distribution and layers determine the values on your system.

A real run of this command on the Poky scarthgap branch (default MACHINE, DEMO_DIAGNOSTICS = "standard" and DEMO_DIAGNOSTICS:pn-busybox = "verbose" added to local.conf) printed these five lines. Yours will differ if your machine or layers differ:

text
DEMO_DIAGNOSTICS="verbose"
DISTRO="poky"
MACHINE="qemux86-64"
OVERRIDES="linux:x86-64:pn-busybox:layer-core:qemuall:qemux86-64:poky:class-target:libc-glibc:forcevariable"
PN="busybox"

Read the order in that list. pn-busybox stands to the left of the machine tokens qemuall and qemux86-64, and forcevariable is last. The list also holds architecture tokens (linux, x86-64), a layer token, class-target and libc-glibc. So in a normal build a variant named after the machine beats a variant named after the recipe when both set the same variable. The paper example above, with qemuarm64:pn-busybox, is an order chosen for practice and not the order you will see in this output.

For a fuller explanation, run bitbake -e busybox without the filter and find DEMO_DIAGNOSTICS in the report. The variable history above the final assignment can show which files and operations contributed. Check the active list and compare the exact token positions. Then inspect the final value for added text and spaces. If you are comparing two recipes, read the report for each recipe separately so that each value is paired with its own active list.

Before moving on, explain how the same configuration file can yield two different values for two recipes. Name the token that changes and the command that lets you check it. The next lesson, Tasks, PROVIDES and PREFERRED_PROVIDER, uses effective metadata values to answer what BitBake runs and how it chooses a recipe when more than one can supply what the build needs.

Key takeaways

  • ?= sets an ordinary value immediately if the variable has no ordinary value, while ??= supplies a replaceable weak default.
  • += and =+ insert a space, while .= and =. concatenate without inserting a separator.
  • Modern BitBake uses colon syntax for conditional assignments, and recipes can have different active override tokens.
  • When two distinct single-token overrides both supply a value, the token later in OVERRIDES wins.
Reflect
  1. Explain to a colleague, in two sentences: predict the result of direct, default, and concatenating assignments.
  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

Official documentation first. Links checked when the lesson was published.

Find your next step

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