Yocto Bitbake variable syntax

One of the main things that one must do when working with Yocto is to became comfortable with Bitbake variable syntax. Here we will go through all most commonly used syntax concepts.

Assigning variable

Basic Variable Setting

VARIABLE = "value"
VARIABLE = 'I have a " in my value'

This assignment occurs immediately as the statement is parsed. It is a “hard” assignment.

Default value ?=

VARNAME ?= "value"

If we have same default variable in layer-1 and layer-2, and these layers are included in that order, only the default value from layer-1 is going to be used.

Weak default value ??=

VARNAME ??= "value"

Difference between default and weak default variable is that for weak default variable last found value is going to be used, while for normal default value first value will be used.

VARNAME ?= "a"
VARNAME ?= "b"
// VARNAME is "a"

WEAK ??= "a"
WEAK ??= "b"
// WEAK is "b"

Immediate variable expansion :=

T = "123"
A := "test ${T}"
T = "456"
B := "${T} ${C}"
C = "cval"
C := "${C}append"

#Result
A = "test 123"
B = "456 cvalappend"
C = "cvalappend"

Appending (+=) and prepending (=+) with spaces

B = "bval"
B += "additionaldata"
C = "cval"
C =+ "test"

# Result
B = "bval additionaldata"
C = "test cval"

These operators take immediate effect during parsing

Appending (.=) and Prepending (=.) without Spaces

B = "bval"
B .= "additionaldata"
C = "cval"
C =. "test"

#Result
B = "bvaladditionaldata"
C = "testcval"

Appending and Prepending (Override Style Syntax)

These operators differ from the “:=”, “.=”, “=.”, “+=”, and “=+” operators in that their effects are applied at variable expansion time rather than being immediately applied

B = "bval"
B:append = " additional data"
C = "cval"
C:prepend = "additional data "
D = "dval"
D:append = "additional data"

#Result
B = "bval additional data"
C = "additional data cval"

Removal syntax

FOO = "123 456 789 123456 123 456 123 456"
FOO:remove = "123"
FOO:remove = "456"
FOO2 = " abc def ghi abcdef abc def abc def def"
FOO2:remove = "\
    def \
    abc \
    ghi \
    "
#Result
FOO  = " 789 123456" 
FOO2 = " abcdef"

Variable Flag syntax

FOO[a] = "abc"
FOO[b] = "123"
FOO[a] += "456"

# One extremely common application is to attach some brief documentation to a Bitbake variable as follows:
CACHE[doc] = "some documentation"

Variable flags are Bitbake’s implementation of variable properties or attributres. It is a way of tagging extra information onto a variable. You can define, append and prepend values to variable flags. All the standard operations previously mention work for the flags except for override syntax (i.e. “:prepend”, “:append” and “:remove”).

Inline Python Variable Expansion

DATE = "${@time.strftime('%Y%m%d',time.gmtime())}"
PN = "${@bb.parse.vars_from_file(d.getVar('FILE', False),d)[0] or 'defaultpkgname'}"

Unsetting variables

unset DATE
unset do_fetch[noexec]

Providing Pathnames

When specifying pathnames for use with Bitbake, do not use the tilde (“~”)

Exporting Variables to the Environment

export ENV_VARIABLE
ENV_VARIABLE = "value from the environment"

do_foo() {
    bbplain "$ENV_VARIABLE"
}

It does not matter whether export ENV_VARIABLE appears before or after assignments to ENV_VARIABLE.

Conditional Metadata

OVERRIDES = "architecture:os:machine"
TEST = "default"
TEST:os = "osspecific"
TEST:nooverride = "othercondvalue"

# Reslt
TEST == "osspecific"

------------------------------------------------------------
OVERRIDES = "machine:local"
DEPENDS = "libat"
DEPENDS:append:machine = " libmad"

# Result
DEPENDS == "libat libmad"



You can use OVERRIDES to conditionally select a specific version of a variable and to conditionally append or prepend the value of a variable.

Setting a Variable for a Single Task

FOO:task-configure = "val 1"
FOO:task-compile = "val 2"

# Result
# During do_configure, FOO == "val 1"
# During do_compile,   FOO == "val 2"

------------------------------------------------------------
EXTRA_OEMAKE:prepend:task_compile = "${PARALLEL_MAKE} "
# Result
# During do_compile will prepend "${PARALLEL_MAKE}" variable to variable EXTRA_OEMAKE 

Key Expansion

A${B} = "X"
B = "2"
A2 = "Y"

# Result
# In this case, after all the parsing is complete, Bitbake expands ${B} into “2”. This expansion causes A2, which was set to “Y” before the expansion, to become “X”.
A2 == "X"

Key expansion happens when the Bitbake datastore is finalized.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *