Understanding the difference between ??=, ?= and = for bitbake variables

Understanding the difference between ??=, ?= and = for bitbake variables

When looking up something in the Yocto project reference manual, I sometimes encounter ??= or ?= operators. Normally, a simple = is used in recipes instead.

What's the difference between ??= and a normal = assignment?

The operator seems to be used with config options like PACKAGECONFIG, but I couldn't find any explanation on what it does. I found related questions, where the authors are confused by it as well.

For an example have a look at ICECC_DISABLED:

Disables or enables the icecc (Icecream) function. [...]
Setting this variable to “1” in your local.conf disables the function:

ICECC_DISABLED ??= "1"

Answer

The ??= and ?= are features of bitbake, so you have to look inside the bitbake reference manual. In simple terms, they allow to set "soft defaults". They are typically used in machine layers and other places that expect later configuration of a variable.

  • Using var ??= "x", it will be overridden by ?=, = and += assignments
  • Using var ?= "x", it will be overridden by = and += assignments
  • Using var = "x", it won't be overridden. += appends.
# Typically, those would be spread among different layers:
var ??= "weak default"
var ?= "default"
var = "actually used"
# Result: var = "actually used"

If you'd simply var = "x" and var = "y" in two different places, the first one would win. If that's not done correctly, things get complicated. Thus, bitbake has weak defaults, when a recipe requires a variable to be set, but does want to enable the user to set a different value.

There are some caveats:

  • Assignment is immediate. Consequently, if multiple ?= assignments to the same variable exist, the first is used.
  • += acts as a =. User var:append = " y" instead:
W ??= "x"
W += "y"
# Result: "y"

var ??= "x"
var:append = " y"
# Result: "x y"

PACKAGECONFIG is a popular example, because the recipe needs some config in any case. But if the user has a .bbappend for the recipe, the user config shall be used instead.

Enjoyed this question?

Check out more content on our blog or follow us on social media.

Browse more questions