blob: b7c3d8091fd37ed9b85805f453d831fd4674167a [file] [log] [blame]
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001.. SPDX-License-Identifier: CC-BY-2.5
2
3====================
4Syntax and Operators
5====================
6
7|
8
9BitBake files have their own syntax. The syntax has similarities to
10several other languages but also has some unique features. This section
11describes the available syntax and operators as well as provides
12examples.
13
14Basic Syntax
15============
16
17This section provides some basic syntax examples.
18
19Basic Variable Setting
20----------------------
21
22The following example sets ``VARIABLE`` to "value". This assignment
23occurs immediately as the statement is parsed. It is a "hard"
24assignment. ::
25
26 VARIABLE = "value"
27
28As expected, if you include leading or
Andrew Geisslerc926e172021-05-07 16:11:35 -050029trailing spaces as part of an assignment, the spaces are retained::
Andrew Geisslerc9f78652020-09-18 14:11:35 -050030
31 VARIABLE = " value"
32 VARIABLE = "value "
33
34Setting ``VARIABLE`` to "" sets
35it to an empty string, while setting the variable to " " sets it to a
36blank space (i.e. these are not the same values). ::
37
38 VARIABLE = ""
39 VARIABLE = " "
40
41You can use single quotes instead of double quotes when setting a
42variable's value. Doing so allows you to use values that contain the
Andrew Geisslerc926e172021-05-07 16:11:35 -050043double quote character::
Andrew Geisslerc9f78652020-09-18 14:11:35 -050044
45 VARIABLE = 'I have a " in my value'
46
47.. note::
48
49 Unlike in Bourne shells, single quotes work identically to double
50 quotes in all other ways. They do not suppress variable expansions.
51
52Modifying Existing Variables
53----------------------------
54
55Sometimes you need to modify existing variables. Following are some
56cases where you might find you want to modify an existing variable:
57
58- Customize a recipe that uses the variable.
59
60- Change a variable's default value used in a ``*.bbclass`` file.
61
62- Change the variable in a ``*.bbappend`` file to override the variable
63 in the original recipe.
64
65- Change the variable in a configuration file so that the value
66 overrides an existing configuration.
67
68Changing a variable value can sometimes depend on how the value was
69originally assigned and also on the desired intent of the change. In
70particular, when you append a value to a variable that has a default
71value, the resulting value might not be what you expect. In this case,
72the value you provide might replace the value rather than append to the
73default value.
74
75If after you have changed a variable's value and something unexplained
76occurs, you can use BitBake to check the actual value of the suspect
77variable. You can make these checks for both configuration and recipe
78level changes:
79
Andrew Geisslerc926e172021-05-07 16:11:35 -050080- For configuration changes, use the following::
Andrew Geisslerc9f78652020-09-18 14:11:35 -050081
82 $ bitbake -e
83
84 This
85 command displays variable values after the configuration files (i.e.
86 ``local.conf``, ``bblayers.conf``, ``bitbake.conf`` and so forth)
87 have been parsed.
88
89 .. note::
90
91 Variables that are exported to the environment are preceded by the
92 string "export" in the command's output.
93
Patrick Williams213cb262021-08-07 19:21:33 -050094- To find changes to a given variable in a specific recipe, use the
95 following::
Andrew Geisslerc9f78652020-09-18 14:11:35 -050096
Patrick Williams213cb262021-08-07 19:21:33 -050097 $ bitbake recipename -e | grep VARIABLENAME=\"
Andrew Geisslerc9f78652020-09-18 14:11:35 -050098
99 This command checks to see if the variable actually makes
100 it into a specific recipe.
101
102Line Joining
103------------
104
105Outside of :ref:`functions <bitbake-user-manual/bitbake-user-manual-metadata:functions>`,
106BitBake joins any line ending in
Andrew Geissler595f6302022-01-24 19:11:47 +0000107a backslash character ("\\") with the following line before parsing
108statements. The most common use for the "\\" character is to split
Andrew Geisslerc926e172021-05-07 16:11:35 -0500109variable assignments over multiple lines, as in the following example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500110
111 FOO = "bar \
112 baz \
113 qaz"
114
Andrew Geissler595f6302022-01-24 19:11:47 +0000115Both the "\\" character and the newline
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500116character that follow it are removed when joining lines. Thus, no
117newline characters end up in the value of ``FOO``.
118
119Consider this additional example where the two assignments both assign
Andrew Geisslerc926e172021-05-07 16:11:35 -0500120"barbaz" to ``FOO``::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500121
122 FOO = "barbaz"
123 FOO = "bar\
124 baz"
125
126.. note::
127
Andrew Geissler595f6302022-01-24 19:11:47 +0000128 BitBake does not interpret escape sequences like "\\n" in variable
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500129 values. For these to have an effect, the value must be passed to some
130 utility that interprets escape sequences, such as
131 ``printf`` or ``echo -n``.
132
133Variable Expansion
134------------------
135
136Variables can reference the contents of other variables using a syntax
137that is similar to variable expansion in Bourne shells. The following
138assignments result in A containing "aval" and B evaluating to
139"preavalpost". ::
140
141 A = "aval"
142 B = "pre${A}post"
143
144.. note::
145
146 Unlike in Bourne shells, the curly braces are mandatory: Only ``${FOO}`` and not
147 ``$FOO`` is recognized as an expansion of ``FOO``.
148
149The "=" operator does not immediately expand variable references in the
150right-hand side. Instead, expansion is deferred until the variable
151assigned to is actually used. The result depends on the current values
152of the referenced variables. The following example should clarify this
Andrew Geisslerc926e172021-05-07 16:11:35 -0500153behavior::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500154
155 A = "${B} baz"
156 B = "${C} bar"
157 C = "foo"
158 *At this point, ${A} equals "foo bar baz"*
159 C = "qux"
160 *At this point, ${A} equals "qux bar baz"*
161 B = "norf"
Andrew Geissler595f6302022-01-24 19:11:47 +0000162 *At this point, ${A} equals "norf baz"*
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500163
164Contrast this behavior with the
165:ref:`bitbake-user-manual/bitbake-user-manual-metadata:immediate variable
166expansion (:=)` operator.
167
168If the variable expansion syntax is used on a variable that does not
169exist, the string is kept as is. For example, given the following
170assignment, ``BAR`` expands to the literal string "${FOO}" as long as
171``FOO`` does not exist. ::
172
173 BAR = "${FOO}"
174
175Setting a default value (?=)
176----------------------------
177
178You can use the "?=" operator to achieve a "softer" assignment for a
179variable. This type of assignment allows you to define a variable if it
180is undefined when the statement is parsed, but to leave the value alone
Andrew Geisslerc926e172021-05-07 16:11:35 -0500181if the variable has a value. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500182
183 A ?= "aval"
184
185If ``A`` is
186set at the time this statement is parsed, the variable retains its
187value. However, if ``A`` is not set, the variable is set to "aval".
188
189.. note::
190
191 This assignment is immediate. Consequently, if multiple "?="
192 assignments to a single variable exist, the first of those ends up
193 getting used.
194
195Setting a weak default value (??=)
196----------------------------------
197
Andrew Geissler615f2f12022-07-15 14:00:58 -0500198The weak default value of a variable is the value which that variable
199will expand to if no value has been assigned to it via any of the other
200assignment operators. The "??=" operator takes effect immediately, replacing
201any previously defined weak default value. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500202
Andrew Geissler615f2f12022-07-15 14:00:58 -0500203 W ??= "x"
204 A := "${W}" # Immediate variable expansion
205 W ??= "y"
206 B := "${W}" # Immediate variable expansion
207 W ??= "z"
208 C = "${W}"
209 W ?= "i"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500210
Andrew Geissler615f2f12022-07-15 14:00:58 -0500211After parsing we will have::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500212
Andrew Geissler615f2f12022-07-15 14:00:58 -0500213 A = "x"
214 B = "y"
215 C = "i"
216 W = "i"
217
218Appending and prepending non-override style will not substitute the weak
219default value, which means that after parsing::
220
221 W ??= "x"
222 W += "y"
223
224we will have::
225
226 W = " y"
227
228On the other hand, override-style appends/prepends/removes are applied after
229any active weak default value has been substituted::
230
231 W ??= "x"
232 W:append = "y"
233
234After parsing we will have::
235
236 W = "xy"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500237
238Immediate variable expansion (:=)
239---------------------------------
240
241The ":=" operator results in a variable's contents being expanded
Andrew Geisslerc926e172021-05-07 16:11:35 -0500242immediately, rather than when the variable is actually used::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500243
244 T = "123"
245 A := "test ${T}"
246 T = "456"
247 B := "${T} ${C}"
248 C = "cval"
249 C := "${C}append"
250
251In this example, ``A`` contains "test 123", even though the final value
Patrick Williams213cb262021-08-07 19:21:33 -0500252of :term:`T` is "456". The variable :term:`B` will end up containing "456
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500253cvalappend". This is because references to undefined variables are
254preserved as is during (immediate)expansion. This is in contrast to GNU
255Make, where undefined variables expand to nothing. The variable ``C``
256contains "cvalappend" since ``${C}`` immediately expands to "cval".
257
258.. _appending-and-prepending:
259
260Appending (+=) and prepending (=+) With Spaces
261----------------------------------------------
262
263Appending and prepending values is common and can be accomplished using
264the "+=" and "=+" operators. These operators insert a space between the
265current value and prepended or appended value.
266
267These operators take immediate effect during parsing. Here are some
Andrew Geisslerc926e172021-05-07 16:11:35 -0500268examples::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500269
270 B = "bval"
271 B += "additionaldata"
272 C = "cval"
273 C =+ "test"
274
Patrick Williams213cb262021-08-07 19:21:33 -0500275The variable :term:`B` contains "bval additionaldata" and ``C`` contains "test
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500276cval".
277
278.. _appending-and-prepending-without-spaces:
279
280Appending (.=) and Prepending (=.) Without Spaces
281-------------------------------------------------
282
283If you want to append or prepend values without an inserted space, use
284the ".=" and "=." operators.
285
286These operators take immediate effect during parsing. Here are some
Andrew Geisslerc926e172021-05-07 16:11:35 -0500287examples::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500288
289 B = "bval"
290 B .= "additionaldata"
291 C = "cval"
292 C =. "test"
293
Patrick Williams213cb262021-08-07 19:21:33 -0500294The variable :term:`B` contains "bvaladditionaldata" and ``C`` contains
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500295"testcval".
296
297Appending and Prepending (Override Style Syntax)
298------------------------------------------------
299
300You can also append and prepend a variable's value using an override
301style syntax. When you use this syntax, no spaces are inserted.
302
303These operators differ from the ":=", ".=", "=.", "+=", and "=+"
304operators in that their effects are applied at variable expansion time
Andrew Geisslerc926e172021-05-07 16:11:35 -0500305rather than being immediately applied. Here are some examples::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500306
307 B = "bval"
Patrick Williams213cb262021-08-07 19:21:33 -0500308 B:append = " additional data"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500309 C = "cval"
Patrick Williams213cb262021-08-07 19:21:33 -0500310 C:prepend = "additional data "
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500311 D = "dval"
Patrick Williams213cb262021-08-07 19:21:33 -0500312 D:append = "additional data"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500313
Patrick Williams213cb262021-08-07 19:21:33 -0500314The variable :term:`B`
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500315becomes "bval additional data" and ``C`` becomes "additional data cval".
316The variable ``D`` becomes "dvaladditional data".
317
318.. note::
319
320 You must control all spacing when you use the override syntax.
321
322It is also possible to append and prepend to shell functions and
323BitBake-style Python functions. See the ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:shell functions`" and ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:bitbake-style python functions`"
324sections for examples.
325
326.. _removing-override-style-syntax:
327
328Removal (Override Style Syntax)
329-------------------------------
330
331You can remove values from lists using the removal override style
332syntax. Specifying a value for removal causes all occurrences of that
Patrick Williams2390b1b2022-11-03 13:47:49 -0500333value to be removed from the variable. Unlike ":append" and ":prepend",
334there is no need to add a leading or trailing space to the value.
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500335
336When you use this syntax, BitBake expects one or more strings.
Andrew Geisslerc926e172021-05-07 16:11:35 -0500337Surrounding spaces and spacing are preserved. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500338
339 FOO = "123 456 789 123456 123 456 123 456"
Patrick Williams213cb262021-08-07 19:21:33 -0500340 FOO:remove = "123"
341 FOO:remove = "456"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500342 FOO2 = " abc def ghi abcdef abc def abc def def"
Patrick Williams213cb262021-08-07 19:21:33 -0500343 FOO2:remove = "\
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500344 def \
345 abc \
346 ghi \
347 "
348
349The variable ``FOO`` becomes
350" 789 123456 " and ``FOO2`` becomes " abcdef ".
351
Patrick Williams213cb262021-08-07 19:21:33 -0500352Like ":append" and ":prepend", ":remove" is applied at variable
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500353expansion time.
354
355Override Style Operation Advantages
356-----------------------------------
357
Patrick Williams213cb262021-08-07 19:21:33 -0500358An advantage of the override style operations ":append", ":prepend", and
359":remove" as compared to the "+=" and "=+" operators is that the
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500360override style operators provide guaranteed operations. For example,
361consider a class ``foo.bbclass`` that needs to add the value "val" to
Andrew Geisslerc926e172021-05-07 16:11:35 -0500362the variable ``FOO``, and a recipe that uses ``foo.bbclass`` as follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500363
364 inherit foo
365 FOO = "initial"
366
367If ``foo.bbclass`` uses the "+=" operator,
368as follows, then the final value of ``FOO`` will be "initial", which is
Andrew Geisslerc926e172021-05-07 16:11:35 -0500369not what is desired::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500370
371 FOO += "val"
372
373If, on the other hand, ``foo.bbclass``
Patrick Williams213cb262021-08-07 19:21:33 -0500374uses the ":append" operator, then the final value of ``FOO`` will be
Andrew Geisslerc926e172021-05-07 16:11:35 -0500375"initial val", as intended::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500376
Patrick Williams213cb262021-08-07 19:21:33 -0500377 FOO:append = " val"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500378
379.. note::
380
Patrick Williams213cb262021-08-07 19:21:33 -0500381 It is never necessary to use "+=" together with ":append". The following
Andrew Geisslerc926e172021-05-07 16:11:35 -0500382 sequence of assignments appends "barbaz" to FOO::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500383
Patrick Williams213cb262021-08-07 19:21:33 -0500384 FOO:append = "bar"
385 FOO:append = "baz"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500386
387
388 The only effect of changing the second assignment in the previous
389 example to use "+=" would be to add a space before "baz" in the
390 appended value (due to how the "+=" operator works).
391
392Another advantage of the override style operations is that you can
393combine them with other overrides as described in the
394":ref:`bitbake-user-manual/bitbake-user-manual-metadata:conditional syntax (overrides)`" section.
395
396Variable Flag Syntax
397--------------------
398
399Variable flags are BitBake's implementation of variable properties or
400attributes. It is a way of tagging extra information onto a variable.
401You can find more out about variable flags in general in the
402":ref:`bitbake-user-manual/bitbake-user-manual-metadata:variable flags`" section.
403
404You can define, append, and prepend values to variable flags. All the
405standard syntax operations previously mentioned work for variable flags
Patrick Williams213cb262021-08-07 19:21:33 -0500406except for override style syntax (i.e. ":prepend", ":append", and
407":remove").
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500408
Andrew Geisslerc926e172021-05-07 16:11:35 -0500409Here are some examples showing how to set variable flags::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500410
411 FOO[a] = "abc"
412 FOO[b] = "123"
413 FOO[a] += "456"
414
415The variable ``FOO`` has two flags:
416``[a]`` and ``[b]``. The flags are immediately set to "abc" and "123",
417respectively. The ``[a]`` flag becomes "abc 456".
418
419No need exists to pre-define variable flags. You can simply start using
420them. One extremely common application is to attach some brief
Andrew Geisslerc926e172021-05-07 16:11:35 -0500421documentation to a BitBake variable as follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500422
423 CACHE[doc] = "The directory holding the cache of the metadata."
424
Patrick Williams2390b1b2022-11-03 13:47:49 -0500425.. note::
426
427 Variable flag names starting with an underscore (``_``) character
428 are allowed but are ignored by ``d.getVarFlags("VAR")``
429 in Python code. Such flag names are used internally by BitBake.
430
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500431Inline Python Variable Expansion
432--------------------------------
433
434You can use inline Python variable expansion to set variables. Here is
Andrew Geisslerc926e172021-05-07 16:11:35 -0500435an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500436
437 DATE = "${@time.strftime('%Y%m%d',time.gmtime())}"
438
439This example results in the ``DATE`` variable being set to the current date.
440
441Probably the most common use of this feature is to extract the value of
442variables from BitBake's internal data dictionary, ``d``. The following
443lines select the values of a package name and its version number,
Andrew Geisslerc926e172021-05-07 16:11:35 -0500444respectively::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500445
Andrew Geissler5199d832021-09-24 16:47:35 -0500446 PN = "${@bb.parse.vars_from_file(d.getVar('FILE', False),d)[0] or 'defaultpkgname'}"
447 PV = "${@bb.parse.vars_from_file(d.getVar('FILE', False),d)[1] or '1.0'}"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500448
449.. note::
450
451 Inline Python expressions work just like variable expansions insofar as the
452 "=" and ":=" operators are concerned. Given the following assignment, foo()
Andrew Geisslerc926e172021-05-07 16:11:35 -0500453 is called each time FOO is expanded::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500454
455 FOO = "${@foo()}"
456
457 Contrast this with the following immediate assignment, where foo() is only
Andrew Geisslerc926e172021-05-07 16:11:35 -0500458 called once, while the assignment is parsed::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500459
460 FOO := "${@foo()}"
461
462For a different way to set variables with Python code during parsing,
463see the
464":ref:`bitbake-user-manual/bitbake-user-manual-metadata:anonymous python functions`" section.
465
466Unsetting variables
467-------------------
468
469It is possible to completely remove a variable or a variable flag from
470BitBake's internal data dictionary by using the "unset" keyword. Here is
Andrew Geisslerc926e172021-05-07 16:11:35 -0500471an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500472
473 unset DATE
474 unset do_fetch[noexec]
475
476These two statements remove the ``DATE`` and the ``do_fetch[noexec]`` flag.
477
478Providing Pathnames
479-------------------
480
481When specifying pathnames for use with BitBake, do not use the tilde
482("~") character as a shortcut for your home directory. Doing so might
483cause BitBake to not recognize the path since BitBake does not expand
484this character in the same way a shell would.
485
Andrew Geisslerc926e172021-05-07 16:11:35 -0500486Instead, provide a fuller path as the following example illustrates::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500487
488 BBLAYERS ?= " \
489 /home/scott-lenovo/LayerA \
490 "
491
492Exporting Variables to the Environment
493======================================
494
495You can export variables to the environment of running tasks by using
496the ``export`` keyword. For example, in the following example, the
Andrew Geisslerc926e172021-05-07 16:11:35 -0500497``do_foo`` task prints "value from the environment" when run::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500498
499 export ENV_VARIABLE
500 ENV_VARIABLE = "value from the environment"
501
502 do_foo() {
503 bbplain "$ENV_VARIABLE"
504 }
505
506.. note::
507
508 BitBake does not expand ``$ENV_VARIABLE`` in this case because it lacks the
509 obligatory ``{}`` . Rather, ``$ENV_VARIABLE`` is expanded by the shell.
510
511It does not matter whether ``export ENV_VARIABLE`` appears before or
512after assignments to ``ENV_VARIABLE``.
513
514It is also possible to combine ``export`` with setting a value for the
Andrew Geisslerc926e172021-05-07 16:11:35 -0500515variable. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500516
517 export ENV_VARIABLE = "variable-value"
518
519In the output of ``bitbake -e``, variables that are exported to the
520environment are preceded by "export".
521
522Among the variables commonly exported to the environment are ``CC`` and
523``CFLAGS``, which are picked up by many build systems.
524
525Conditional Syntax (Overrides)
526==============================
527
528BitBake uses :term:`OVERRIDES` to control what
529variables are overridden after BitBake parses recipes and configuration
Patrick Williams213cb262021-08-07 19:21:33 -0500530files. This section describes how you can use :term:`OVERRIDES` as
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500531conditional metadata, talks about key expansion in relationship to
Patrick Williams213cb262021-08-07 19:21:33 -0500532:term:`OVERRIDES`, and provides some examples to help with understanding.
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500533
534Conditional Metadata
535--------------------
536
Patrick Williams213cb262021-08-07 19:21:33 -0500537You can use :term:`OVERRIDES` to conditionally select a specific version of
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500538a variable and to conditionally append or prepend the value of a
539variable.
540
541.. note::
542
Andrew Geissler9aee5002022-03-30 16:27:02 +0000543 Overrides can only use lower-case characters, digits and dashes.
544 In particular, colons are not permitted in override names as they are used to
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500545 separate overrides from each other and from the variable name.
546
Patrick Williams213cb262021-08-07 19:21:33 -0500547- *Selecting a Variable:* The :term:`OVERRIDES` variable is a
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500548 colon-character-separated list that contains items for which you want
549 to satisfy conditions. Thus, if you have a variable that is
Patrick Williams213cb262021-08-07 19:21:33 -0500550 conditional on "arm", and "arm" is in :term:`OVERRIDES`, then the
Andrew Geisslerf0343792020-11-18 10:42:21 -0600551 "arm"-specific version of the variable is used rather than the
Andrew Geisslerc926e172021-05-07 16:11:35 -0500552 non-conditional version. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500553
554 OVERRIDES = "architecture:os:machine"
555 TEST = "default"
Andrew Geissler9aee5002022-03-30 16:27:02 +0000556 TEST:os = "osspecific"
557 TEST:nooverride = "othercondvalue"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500558
Patrick Williams213cb262021-08-07 19:21:33 -0500559 In this example, the :term:`OVERRIDES`
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500560 variable lists three overrides: "architecture", "os", and "machine".
561 The variable ``TEST`` by itself has a default value of "default". You
562 select the os-specific version of the ``TEST`` variable by appending
Andrew Geissler9aee5002022-03-30 16:27:02 +0000563 the "os" override to the variable (i.e. ``TEST:os``).
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500564
565 To better understand this, consider a practical example that assumes
566 an OpenEmbedded metadata-based Linux kernel recipe file. The
567 following lines from the recipe file first set the kernel branch
568 variable ``KBRANCH`` to a default value, then conditionally override
Andrew Geisslerc926e172021-05-07 16:11:35 -0500569 that value based on the architecture of the build::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500570
571 KBRANCH = "standard/base"
Patrick Williams213cb262021-08-07 19:21:33 -0500572 KBRANCH:qemuarm = "standard/arm-versatile-926ejs"
573 KBRANCH:qemumips = "standard/mti-malta32"
574 KBRANCH:qemuppc = "standard/qemuppc"
575 KBRANCH:qemux86 = "standard/common-pc/base"
576 KBRANCH:qemux86-64 = "standard/common-pc-64/base"
577 KBRANCH:qemumips64 = "standard/mti-malta64"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500578
579- *Appending and Prepending:* BitBake also supports append and prepend
580 operations to variable values based on whether a specific item is
Patrick Williams213cb262021-08-07 19:21:33 -0500581 listed in :term:`OVERRIDES`. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500582
583 DEPENDS = "glibc ncurses"
584 OVERRIDES = "machine:local"
Patrick Williams213cb262021-08-07 19:21:33 -0500585 DEPENDS:append:machine = "libmad"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500586
Patrick Williams213cb262021-08-07 19:21:33 -0500587 In this example, :term:`DEPENDS` becomes "glibc ncurses libmad".
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500588
589 Again, using an OpenEmbedded metadata-based kernel recipe file as an
590 example, the following lines will conditionally append to the
Andrew Geisslerc926e172021-05-07 16:11:35 -0500591 ``KERNEL_FEATURES`` variable based on the architecture::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500592
Patrick Williams213cb262021-08-07 19:21:33 -0500593 KERNEL_FEATURES:append = " ${KERNEL_EXTRA_FEATURES}"
594 KERNEL_FEATURES:append:qemux86=" cfg/sound.scc cfg/paravirt_kvm.scc"
595 KERNEL_FEATURES:append:qemux86-64=" cfg/sound.scc cfg/paravirt_kvm.scc"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500596
597- *Setting a Variable for a Single Task:* BitBake supports setting a
Andrew Geisslerc926e172021-05-07 16:11:35 -0500598 variable just for the duration of a single task. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500599
Andrew Geissler9aee5002022-03-30 16:27:02 +0000600 FOO:task-configure = "val 1"
Patrick Williams213cb262021-08-07 19:21:33 -0500601 FOO:task-compile = "val 2"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500602
603 In the
604 previous example, ``FOO`` has the value "val 1" while the
605 ``do_configure`` task is executed, and the value "val 2" while the
606 ``do_compile`` task is executed.
607
608 Internally, this is implemented by prepending the task (e.g.
609 "task-compile:") to the value of
610 :term:`OVERRIDES` for the local datastore of the
611 ``do_compile`` task.
612
613 You can also use this syntax with other combinations (e.g.
Patrick Williams213cb262021-08-07 19:21:33 -0500614 "``:prepend``") as shown in the following example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500615
Patrick Williams213cb262021-08-07 19:21:33 -0500616 EXTRA_OEMAKE:prepend:task-compile = "${PARALLEL_MAKE} "
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500617
Andrew Geissler9aee5002022-03-30 16:27:02 +0000618.. note::
619
620 Before BitBake 1.52 (Honister 3.4), the syntax for :term:`OVERRIDES`
621 used ``_`` instead of ``:``, so you will still find a lot of documentation
622 using ``_append``, ``_prepend``, and ``_remove``, for example.
623
624 For details, see the
625 :yocto_docs:`Overrides Syntax Changes </migration-guides/migration-3.4.html#override-syntax-changes>`
626 section in the Yocto Project manual migration notes.
627
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500628Key Expansion
629-------------
630
631Key expansion happens when the BitBake datastore is finalized. To better
Andrew Geisslerc926e172021-05-07 16:11:35 -0500632understand this, consider the following example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500633
634 A${B} = "X"
635 B = "2"
636 A2 = "Y"
637
638In this case, after all the parsing is complete, BitBake expands
639``${B}`` into "2". This expansion causes ``A2``, which was set to "Y"
640before the expansion, to become "X".
641
642.. _variable-interaction-worked-examples:
643
644Examples
645--------
646
647Despite the previous explanations that show the different forms of
648variable definitions, it can be hard to work out exactly what happens
649when variable operators, conditional overrides, and unconditional
650overrides are combined. This section presents some common scenarios
651along with explanations for variable interactions that typically confuse
652users.
653
654There is often confusion concerning the order in which overrides and
655various "append" operators take effect. Recall that an append or prepend
Patrick Williams213cb262021-08-07 19:21:33 -0500656operation using ":append" and ":prepend" does not result in an immediate
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500657assignment as would "+=", ".=", "=+", or "=.". Consider the following
Andrew Geisslerc926e172021-05-07 16:11:35 -0500658example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500659
660 OVERRIDES = "foo"
661 A = "Z"
Patrick Williams213cb262021-08-07 19:21:33 -0500662 A:foo:append = "X"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500663
664For this case,
665``A`` is unconditionally set to "Z" and "X" is unconditionally and
Patrick Williams213cb262021-08-07 19:21:33 -0500666immediately appended to the variable ``A:foo``. Because overrides have
667not been applied yet, ``A:foo`` is set to "X" due to the append and
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500668``A`` simply equals "Z".
669
670Applying overrides, however, changes things. Since "foo" is listed in
Patrick Williams213cb262021-08-07 19:21:33 -0500671:term:`OVERRIDES`, the conditional variable ``A`` is replaced with the "foo"
672version, which is equal to "X". So effectively, ``A:foo`` replaces
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500673``A``.
674
Andrew Geisslerc926e172021-05-07 16:11:35 -0500675This next example changes the order of the override and the append::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500676
677 OVERRIDES = "foo"
678 A = "Z"
Patrick Williams213cb262021-08-07 19:21:33 -0500679 A:append:foo = "X"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500680
681For this case, before
Patrick Williams213cb262021-08-07 19:21:33 -0500682overrides are handled, ``A`` is set to "Z" and ``A:append:foo`` is set
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500683to "X". Once the override for "foo" is applied, however, ``A`` gets
684appended with "X". Consequently, ``A`` becomes "ZX". Notice that spaces
685are not appended.
686
687This next example has the order of the appends and overrides reversed
Andrew Geisslerc926e172021-05-07 16:11:35 -0500688back as in the first example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500689
690 OVERRIDES = "foo"
691 A = "Y"
Patrick Williams213cb262021-08-07 19:21:33 -0500692 A:foo:append = "Z"
693 A:foo:append = "X"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500694
695For this case, before any overrides are resolved,
696``A`` is set to "Y" using an immediate assignment. After this immediate
Patrick Williams213cb262021-08-07 19:21:33 -0500697assignment, ``A:foo`` is set to "Z", and then further appended with "X"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500698leaving the variable set to "ZX". Finally, applying the override for
699"foo" results in the conditional variable ``A`` becoming "ZX" (i.e.
Patrick Williams213cb262021-08-07 19:21:33 -0500700``A`` is replaced with ``A:foo``).
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500701
Andrew Geisslerc926e172021-05-07 16:11:35 -0500702This final example mixes in some varying operators::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500703
704 A = "1"
Patrick Williams213cb262021-08-07 19:21:33 -0500705 A:append = "2"
706 A:append = "3"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500707 A += "4"
708 A .= "5"
709
710For this case, the type of append
711operators are affecting the order of assignments as BitBake passes
712through the code multiple times. Initially, ``A`` is set to "1 45"
713because of the three statements that use immediate operators. After
Patrick Williams213cb262021-08-07 19:21:33 -0500714these assignments are made, BitBake applies the ":append" operations.
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500715Those operations result in ``A`` becoming "1 4523".
716
717Sharing Functionality
718=====================
719
720BitBake allows for metadata sharing through include files (``.inc``) and
721class files (``.bbclass``). For example, suppose you have a piece of
722common functionality such as a task definition that you want to share
723between more than one recipe. In this case, creating a ``.bbclass`` file
724that contains the common functionality and then using the ``inherit``
725directive in your recipes to inherit the class would be a common way to
726share the task.
727
728This section presents the mechanisms BitBake provides to allow you to
729share functionality between recipes. Specifically, the mechanisms
Patrick Williams213cb262021-08-07 19:21:33 -0500730include ``include``, ``inherit``, :term:`INHERIT`, and ``require``
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500731directives.
732
733Locating Include and Class Files
734--------------------------------
735
736BitBake uses the :term:`BBPATH` variable to locate
737needed include and class files. Additionally, BitBake searches the
738current directory for ``include`` and ``require`` directives.
739
740.. note::
741
742 The BBPATH variable is analogous to the environment variable PATH .
743
744In order for include and class files to be found by BitBake, they need
745to be located in a "classes" subdirectory that can be found in
Patrick Williams213cb262021-08-07 19:21:33 -0500746:term:`BBPATH`.
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500747
748``inherit`` Directive
749---------------------
750
751When writing a recipe or class file, you can use the ``inherit``
752directive to inherit the functionality of a class (``.bbclass``).
753BitBake only supports this directive when used within recipe and class
754files (i.e. ``.bb`` and ``.bbclass``).
755
756The ``inherit`` directive is a rudimentary means of specifying
757functionality contained in class files that your recipes require. For
758example, you can easily abstract out the tasks involved in building a
759package that uses Autoconf and Automake and put those tasks into a class
760file and then have your recipe inherit that class file.
761
762As an example, your recipes could use the following directive to inherit
763an ``autotools.bbclass`` file. The class file would contain common
Andrew Geisslerc926e172021-05-07 16:11:35 -0500764functionality for using Autotools that could be shared across recipes::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500765
766 inherit autotools
767
768In this case, BitBake would search for the directory
Patrick Williams213cb262021-08-07 19:21:33 -0500769``classes/autotools.bbclass`` in :term:`BBPATH`.
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500770
771.. note::
772
773 You can override any values and functions of the inherited class
774 within your recipe by doing so after the "inherit" statement.
775
776If you want to use the directive to inherit multiple classes, separate
777them with spaces. The following example shows how to inherit both the
Andrew Geisslerc926e172021-05-07 16:11:35 -0500778``buildhistory`` and ``rm_work`` classes::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500779
780 inherit buildhistory rm_work
781
782An advantage with the inherit directive as compared to both the
783:ref:`include <bitbake-user-manual/bitbake-user-manual-metadata:\`\`include\`\` directive>` and :ref:`require <bitbake-user-manual/bitbake-user-manual-metadata:\`\`require\`\` directive>`
784directives is that you can inherit class files conditionally. You can
785accomplish this by using a variable expression after the ``inherit``
Andrew Geisslerc926e172021-05-07 16:11:35 -0500786statement. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500787
788 inherit ${VARNAME}
789
790If ``VARNAME`` is
791going to be set, it needs to be set before the ``inherit`` statement is
792parsed. One way to achieve a conditional inherit in this case is to use
Andrew Geisslerc926e172021-05-07 16:11:35 -0500793overrides::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500794
795 VARIABLE = ""
Patrick Williams213cb262021-08-07 19:21:33 -0500796 VARIABLE:someoverride = "myclass"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500797
Andrew Geisslerc926e172021-05-07 16:11:35 -0500798Another method is by using anonymous Python. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500799
800 python () {
801 if condition == value:
802 d.setVar('VARIABLE', 'myclass')
803 else:
804 d.setVar('VARIABLE', '')
805 }
806
807Alternatively, you could use an in-line Python expression in the
Andrew Geisslerc926e172021-05-07 16:11:35 -0500808following form::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500809
810 inherit ${@'classname' if condition else ''}
811 inherit ${@functionname(params)}
812
813In all cases, if the expression evaluates to an
814empty string, the statement does not trigger a syntax error because it
815becomes a no-op.
816
817``include`` Directive
818---------------------
819
820BitBake understands the ``include`` directive. This directive causes
821BitBake to parse whatever file you specify, and to insert that file at
822that location. The directive is much like its equivalent in Make except
823that if the path specified on the include line is a relative path,
Patrick Williams213cb262021-08-07 19:21:33 -0500824BitBake locates the first file it can find within :term:`BBPATH`.
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500825
826The include directive is a more generic method of including
827functionality as compared to the :ref:`inherit <bitbake-user-manual/bitbake-user-manual-metadata:\`\`inherit\`\` directive>`
828directive, which is restricted to class (i.e. ``.bbclass``) files. The
829include directive is applicable for any other kind of shared or
830encapsulated functionality or configuration that does not suit a
831``.bbclass`` file.
832
833As an example, suppose you needed a recipe to include some self-test
Andrew Geisslerc926e172021-05-07 16:11:35 -0500834definitions::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500835
836 include test_defs.inc
837
838.. note::
839
840 The include directive does not produce an error when the file cannot be
841 found. Consequently, it is recommended that if the file you are including is
842 expected to exist, you should use :ref:`require <require-inclusion>` instead
843 of include . Doing so makes sure that an error is produced if the file cannot
844 be found.
845
846.. _require-inclusion:
847
848``require`` Directive
849---------------------
850
851BitBake understands the ``require`` directive. This directive behaves
852just like the ``include`` directive with the exception that BitBake
853raises a parsing error if the file to be included cannot be found. Thus,
854any file you require is inserted into the file that is being parsed at
855the location of the directive.
856
857The require directive, like the include directive previously described,
858is a more generic method of including functionality as compared to the
859:ref:`inherit <bitbake-user-manual/bitbake-user-manual-metadata:\`\`inherit\`\` directive>` directive, which is restricted to class
860(i.e. ``.bbclass``) files. The require directive is applicable for any
861other kind of shared or encapsulated functionality or configuration that
862does not suit a ``.bbclass`` file.
863
864Similar to how BitBake handles :ref:`include <bitbake-user-manual/bitbake-user-manual-metadata:\`\`include\`\` directive>`, if
865the path specified on the require line is a relative path, BitBake
Patrick Williams213cb262021-08-07 19:21:33 -0500866locates the first file it can find within :term:`BBPATH`.
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500867
868As an example, suppose you have two versions of a recipe (e.g.
869``foo_1.2.2.bb`` and ``foo_2.0.0.bb``) where each version contains some
870identical functionality that could be shared. You could create an
871include file named ``foo.inc`` that contains the common definitions
872needed to build "foo". You need to be sure ``foo.inc`` is located in the
873same directory as your two recipe files as well. Once these conditions
874are set up, you can share the functionality using a ``require``
Andrew Geisslerc926e172021-05-07 16:11:35 -0500875directive from within each recipe::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500876
877 require foo.inc
878
879``INHERIT`` Configuration Directive
880-----------------------------------
881
882When creating a configuration file (``.conf``), you can use the
883:term:`INHERIT` configuration directive to inherit a
884class. BitBake only supports this directive when used within a
885configuration file.
886
887As an example, suppose you needed to inherit a class file called
Andrew Geisslerc926e172021-05-07 16:11:35 -0500888``abc.bbclass`` from a configuration file as follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500889
890 INHERIT += "abc"
891
892This configuration directive causes the named class to be inherited at
893the point of the directive during parsing. As with the ``inherit``
894directive, the ``.bbclass`` file must be located in a "classes"
Patrick Williams213cb262021-08-07 19:21:33 -0500895subdirectory in one of the directories specified in :term:`BBPATH`.
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500896
897.. note::
898
899 Because .conf files are parsed first during BitBake's execution, using
900 INHERIT to inherit a class effectively inherits the class globally (i.e. for
901 all recipes).
902
903If you want to use the directive to inherit multiple classes, you can
904provide them on the same line in the ``local.conf`` file. Use spaces to
905separate the classes. The following example shows how to inherit both
Andrew Geisslerc926e172021-05-07 16:11:35 -0500906the ``autotools`` and ``pkgconfig`` classes::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500907
908 INHERIT += "autotools pkgconfig"
909
910Functions
911=========
912
913As with most languages, functions are the building blocks that are used
914to build up operations into tasks. BitBake supports these types of
915functions:
916
917- *Shell Functions:* Functions written in shell script and executed
918 either directly as functions, tasks, or both. They can also be called
919 by other shell functions.
920
921- *BitBake-Style Python Functions:* Functions written in Python and
922 executed by BitBake or other Python functions using
923 ``bb.build.exec_func()``.
924
925- *Python Functions:* Functions written in Python and executed by
926 Python.
927
928- *Anonymous Python Functions:* Python functions executed automatically
929 during parsing.
930
931Regardless of the type of function, you can only define them in class
932(``.bbclass``) and recipe (``.bb`` or ``.inc``) files.
933
934Shell Functions
935---------------
936
Andrew Geissler595f6302022-01-24 19:11:47 +0000937Functions written in shell script are executed either directly as
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500938functions, tasks, or both. They can also be called by other shell
Andrew Geisslerc926e172021-05-07 16:11:35 -0500939functions. Here is an example shell function definition::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500940
941 some_function () {
942 echo "Hello World"
943 }
944
945When you create these types of functions in
946your recipe or class files, you need to follow the shell programming
947rules. The scripts are executed by ``/bin/sh``, which may not be a bash
948shell but might be something such as ``dash``. You should not use
949Bash-specific script (bashisms).
950
Patrick Williams213cb262021-08-07 19:21:33 -0500951Overrides and override-style operators like ``:append`` and ``:prepend``
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500952can also be applied to shell functions. Most commonly, this application
953would be used in a ``.bbappend`` file to modify functions in the main
954recipe. It can also be used to modify functions inherited from classes.
955
Andrew Geisslerc926e172021-05-07 16:11:35 -0500956As an example, consider the following::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500957
958 do_foo() {
959 bbplain first
960 fn
961 }
962
Patrick Williams213cb262021-08-07 19:21:33 -0500963 fn:prepend() {
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500964 bbplain second
965 }
966
967 fn() {
968 bbplain third
969 }
970
Patrick Williams213cb262021-08-07 19:21:33 -0500971 do_foo:append() {
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500972 bbplain fourth
973 }
974
Andrew Geisslerc926e172021-05-07 16:11:35 -0500975Running ``do_foo`` prints the following::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500976
977 recipename do_foo: first
978 recipename do_foo: second
979 recipename do_foo: third
980 recipename do_foo: fourth
981
982.. note::
983
984 Overrides and override-style operators can be applied to any shell
985 function, not just :ref:`tasks <bitbake-user-manual/bitbake-user-manual-metadata:tasks>`.
986
Andrew Geissler595f6302022-01-24 19:11:47 +0000987You can use the ``bitbake -e recipename`` command to view the final
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500988assembled function after all overrides have been applied.
989
990BitBake-Style Python Functions
991------------------------------
992
993These functions are written in Python and executed by BitBake or other
994Python functions using ``bb.build.exec_func()``.
995
Andrew Geisslerc926e172021-05-07 16:11:35 -0500996An example BitBake function is::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500997
998 python some_python_function () {
999 d.setVar("TEXT", "Hello World")
1000 print d.getVar("TEXT")
1001 }
1002
1003Because the
1004Python "bb" and "os" modules are already imported, you do not need to
1005import these modules. Also in these types of functions, the datastore
1006("d") is a global variable and is always automatically available.
1007
1008.. note::
1009
1010 Variable expressions (e.g. ``${X}`` ) are no longer expanded within Python
1011 functions. This behavior is intentional in order to allow you to freely set
1012 variable values to expandable expressions without having them expanded
1013 prematurely. If you do wish to expand a variable within a Python function,
1014 use ``d.getVar("X")`` . Or, for more complicated expressions, use ``d.expand()``.
1015
1016Similar to shell functions, you can also apply overrides and
1017override-style operators to BitBake-style Python functions.
1018
Andrew Geisslerc926e172021-05-07 16:11:35 -05001019As an example, consider the following::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001020
Patrick Williams213cb262021-08-07 19:21:33 -05001021 python do_foo:prepend() {
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001022 bb.plain("first")
1023 }
1024
1025 python do_foo() {
1026 bb.plain("second")
1027 }
1028
Patrick Williams213cb262021-08-07 19:21:33 -05001029 python do_foo:append() {
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001030 bb.plain("third")
1031 }
1032
Andrew Geisslerc926e172021-05-07 16:11:35 -05001033Running ``do_foo`` prints the following::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001034
1035 recipename do_foo: first
1036 recipename do_foo: second
1037 recipename do_foo: third
1038
Andrew Geissler595f6302022-01-24 19:11:47 +00001039You can use the ``bitbake -e recipename`` command to view
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001040the final assembled function after all overrides have been applied.
1041
1042Python Functions
1043----------------
1044
1045These functions are written in Python and are executed by other Python
1046code. Examples of Python functions are utility functions that you intend
1047to call from in-line Python or from within other Python functions. Here
Andrew Geisslerc926e172021-05-07 16:11:35 -05001048is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001049
1050 def get_depends(d):
1051 if d.getVar('SOMECONDITION'):
1052 return "dependencywithcond"
1053 else:
1054 return "dependency"
1055
1056 SOMECONDITION = "1"
1057 DEPENDS = "${@get_depends(d)}"
1058
Patrick Williams213cb262021-08-07 19:21:33 -05001059This would result in :term:`DEPENDS` containing ``dependencywithcond``.
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001060
1061Here are some things to know about Python functions:
1062
1063- Python functions can take parameters.
1064
1065- The BitBake datastore is not automatically available. Consequently,
1066 you must pass it in as a parameter to the function.
1067
1068- The "bb" and "os" Python modules are automatically available. You do
1069 not need to import them.
1070
1071BitBake-Style Python Functions Versus Python Functions
1072------------------------------------------------------
1073
1074Following are some important differences between BitBake-style Python
1075functions and regular Python functions defined with "def":
1076
1077- Only BitBake-style Python functions can be :ref:`tasks <bitbake-user-manual/bitbake-user-manual-metadata:tasks>`.
1078
1079- Overrides and override-style operators can only be applied to
1080 BitBake-style Python functions.
1081
1082- Only regular Python functions can take arguments and return values.
1083
1084- :ref:`Variable flags <bitbake-user-manual/bitbake-user-manual-metadata:variable flags>` such as
1085 ``[dirs]``, ``[cleandirs]``, and ``[lockfiles]`` can be used on BitBake-style
1086 Python functions, but not on regular Python functions.
1087
1088- BitBake-style Python functions generate a separate
1089 ``${``\ :term:`T`\ ``}/run.``\ function-name\ ``.``\ pid
1090 script that is executed to run the function, and also generate a log
1091 file in ``${T}/log.``\ function-name\ ``.``\ pid if they are executed
1092 as tasks.
1093
1094 Regular Python functions execute "inline" and do not generate any
1095 files in ``${T}``.
1096
1097- Regular Python functions are called with the usual Python syntax.
1098 BitBake-style Python functions are usually tasks and are called
1099 directly by BitBake, but can also be called manually from Python code
Andrew Geisslerc926e172021-05-07 16:11:35 -05001100 by using the ``bb.build.exec_func()`` function. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001101
1102 bb.build.exec_func("my_bitbake_style_function", d)
1103
1104 .. note::
1105
1106 ``bb.build.exec_func()`` can also be used to run shell functions from Python
1107 code. If you want to run a shell function before a Python function within
1108 the same task, then you can use a parent helper Python function that
1109 starts by running the shell function with ``bb.build.exec_func()`` and then
1110 runs the Python code.
1111
1112 To detect errors from functions executed with
1113 ``bb.build.exec_func()``, you can catch the ``bb.build.FuncFailed``
1114 exception.
1115
1116 .. note::
1117
1118 Functions in metadata (recipes and classes) should not themselves raise
1119 ``bb.build.FuncFailed``. Rather, ``bb.build.FuncFailed`` should be viewed as a
1120 general indicator that the called function failed by raising an
1121 exception. For example, an exception raised by ``bb.fatal()`` will be caught
1122 inside ``bb.build.exec_func()``, and a ``bb.build.FuncFailed`` will be raised in
1123 response.
1124
1125Due to their simplicity, you should prefer regular Python functions over
1126BitBake-style Python functions unless you need a feature specific to
1127BitBake-style Python functions. Regular Python functions in metadata are
1128a more recent invention than BitBake-style Python functions, and older
1129code tends to use ``bb.build.exec_func()`` more often.
1130
1131Anonymous Python Functions
1132--------------------------
1133
1134Sometimes it is useful to set variables or perform other operations
1135programmatically during parsing. To do this, you can define special
1136Python functions, called anonymous Python functions, that run at the end
1137of parsing. For example, the following conditionally sets a variable
Andrew Geisslerc926e172021-05-07 16:11:35 -05001138based on the value of another variable::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001139
1140 python () {
1141 if d.getVar('SOMEVAR') == 'value':
1142 d.setVar('ANOTHERVAR', 'value2')
1143 }
1144
1145An equivalent way to mark a function as an anonymous function is to give it
1146the name "__anonymous", rather than no name.
1147
1148Anonymous Python functions always run at the end of parsing, regardless
1149of where they are defined. If a recipe contains many anonymous
1150functions, they run in the same order as they are defined within the
Andrew Geisslerc926e172021-05-07 16:11:35 -05001151recipe. As an example, consider the following snippet::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001152
1153 python () {
1154 d.setVar('FOO', 'foo 2')
1155 }
1156
1157 FOO = "foo 1"
1158
1159 python () {
1160 d.appendVar('BAR',' bar 2')
1161 }
1162
1163 BAR = "bar 1"
1164
1165The previous example is conceptually
Andrew Geisslerc926e172021-05-07 16:11:35 -05001166equivalent to the following snippet::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001167
1168 FOO = "foo 1"
1169 BAR = "bar 1"
1170 FOO = "foo 2"
1171 BAR += "bar 2"
1172
1173``FOO`` ends up with the value "foo 2", and
1174``BAR`` with the value "bar 1 bar 2". Just as in the second snippet, the
1175values set for the variables within the anonymous functions become
1176available to tasks, which always run after parsing.
1177
Patrick Williams213cb262021-08-07 19:21:33 -05001178Overrides and override-style operators such as "``:append``" are applied
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001179before anonymous functions run. In the following example, ``FOO`` ends
Andrew Geisslerc926e172021-05-07 16:11:35 -05001180up with the value "foo from anonymous"::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001181
1182 FOO = "foo"
Patrick Williams213cb262021-08-07 19:21:33 -05001183 FOO:append = " from outside"
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001184
1185 python () {
1186 d.setVar("FOO", "foo from anonymous")
1187 }
1188
1189For methods
1190you can use with anonymous Python functions, see the
1191":ref:`bitbake-user-manual/bitbake-user-manual-metadata:functions you can call from within python`"
1192section. For a different method to run Python code during parsing, see
1193the ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:inline python variable expansion`" section.
1194
1195Flexible Inheritance for Class Functions
1196----------------------------------------
1197
1198Through coding techniques and the use of ``EXPORT_FUNCTIONS``, BitBake
1199supports exporting a function from a class such that the class function
1200appears as the default implementation of the function, but can still be
1201called if a recipe inheriting the class needs to define its own version
1202of the function.
1203
1204To understand the benefits of this feature, consider the basic scenario
1205where a class defines a task function and your recipe inherits the
1206class. In this basic scenario, your recipe inherits the task function as
1207defined in the class. If desired, your recipe can add to the start and
Patrick Williams213cb262021-08-07 19:21:33 -05001208end of the function by using the ":prepend" or ":append" operations
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001209respectively, or it can redefine the function completely. However, if it
1210redefines the function, there is no means for it to call the class
1211version of the function. ``EXPORT_FUNCTIONS`` provides a mechanism that
1212enables the recipe's version of the function to call the original
1213version of the function.
1214
1215To make use of this technique, you need the following things in place:
1216
Andrew Geisslerc926e172021-05-07 16:11:35 -05001217- The class needs to define the function as follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001218
1219 classname_functionname
1220
1221 For example, if you have a class file
1222 ``bar.bbclass`` and a function named ``do_foo``, the class must
Andrew Geisslerc926e172021-05-07 16:11:35 -05001223 define the function as follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001224
1225 bar_do_foo
1226
1227- The class needs to contain the ``EXPORT_FUNCTIONS`` statement as
Andrew Geisslerc926e172021-05-07 16:11:35 -05001228 follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001229
1230 EXPORT_FUNCTIONS functionname
1231
1232 For example, continuing with
1233 the same example, the statement in the ``bar.bbclass`` would be as
Andrew Geisslerc926e172021-05-07 16:11:35 -05001234 follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001235
1236 EXPORT_FUNCTIONS do_foo
1237
1238- You need to call the function appropriately from within your recipe.
1239 Continuing with the same example, if your recipe needs to call the
1240 class version of the function, it should call ``bar_do_foo``.
1241 Assuming ``do_foo`` was a shell function and ``EXPORT_FUNCTIONS`` was
1242 used as above, the recipe's function could conditionally call the
Andrew Geisslerc926e172021-05-07 16:11:35 -05001243 class version of the function as follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001244
1245 do_foo() {
1246 if [ somecondition ] ; then
1247 bar_do_foo
1248 else
1249 # Do something else
1250 fi
1251 }
1252
1253 To call your modified version of the function as defined in your recipe,
1254 call it as ``do_foo``.
1255
1256With these conditions met, your single recipe can freely choose between
1257the original function as defined in the class file and the modified
1258function in your recipe. If you do not set up these conditions, you are
1259limited to using one function or the other.
1260
1261Tasks
1262=====
1263
1264Tasks are BitBake execution units that make up the steps that BitBake
1265can run for a given recipe. Tasks are only supported in recipes and
1266classes (i.e. in ``.bb`` files and files included or inherited from
1267``.bb`` files). By convention, tasks have names that start with "do\_".
1268
1269Promoting a Function to a Task
1270------------------------------
1271
1272Tasks are either :ref:`shell functions <bitbake-user-manual/bitbake-user-manual-metadata:shell functions>` or
1273:ref:`BitBake-style Python functions <bitbake-user-manual/bitbake-user-manual-metadata:bitbake-style python functions>`
1274that have been promoted to tasks by using the ``addtask`` command. The
1275``addtask`` command can also optionally describe dependencies between
1276the task and other tasks. Here is an example that shows how to define a
Andrew Geisslerc926e172021-05-07 16:11:35 -05001277task and declare some dependencies::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001278
1279 python do_printdate () {
1280 import time
1281 print time.strftime('%Y%m%d', time.gmtime())
1282 }
1283 addtask printdate after do_fetch before do_build
1284
1285The first argument to ``addtask`` is the name
1286of the function to promote to a task. If the name does not start with
1287"do\_", "do\_" is implicitly added, which enforces the convention that all
1288task names start with "do\_".
1289
1290In the previous example, the ``do_printdate`` task becomes a dependency
1291of the ``do_build`` task, which is the default task (i.e. the task run
1292by the ``bitbake`` command unless another task is specified explicitly).
1293Additionally, the ``do_printdate`` task becomes dependent upon the
1294``do_fetch`` task. Running the ``do_build`` task results in the
1295``do_printdate`` task running first.
1296
1297.. note::
1298
1299 If you try out the previous example, you might see that the
1300 ``do_printdate``
1301 task is only run the first time you build the recipe with the
1302 ``bitbake``
1303 command. This is because BitBake considers the task "up-to-date"
1304 after that initial run. If you want to force the task to always be
1305 rerun for experimentation purposes, you can make BitBake always
1306 consider the task "out-of-date" by using the
1307 :ref:`[nostamp] <bitbake-user-manual/bitbake-user-manual-metadata:Variable Flags>`
Andrew Geisslerc926e172021-05-07 16:11:35 -05001308 variable flag, as follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001309
1310 do_printdate[nostamp] = "1"
1311
1312 You can also explicitly run the task and provide the
Andrew Geisslerc926e172021-05-07 16:11:35 -05001313 -f option as follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001314
1315 $ bitbake recipe -c printdate -f
1316
1317 When manually selecting a task to run with the bitbake ``recipe
1318 -c task`` command, you can omit the "do\_" prefix as part of the task
1319 name.
1320
1321You might wonder about the practical effects of using ``addtask``
Andrew Geisslerc926e172021-05-07 16:11:35 -05001322without specifying any dependencies as is done in the following example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001323
1324 addtask printdate
1325
1326In this example, assuming dependencies have not been
1327added through some other means, the only way to run the task is by
1328explicitly selecting it with ``bitbake`` recipe ``-c printdate``. You
1329can use the ``do_listtasks`` task to list all tasks defined in a recipe
Andrew Geisslerc926e172021-05-07 16:11:35 -05001330as shown in the following example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001331
1332 $ bitbake recipe -c listtasks
1333
1334For more information on task dependencies, see the
1335":ref:`bitbake-user-manual/bitbake-user-manual-execution:dependencies`" section.
1336
1337See the ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:variable flags`" section for information
1338on variable flags you can use with tasks.
1339
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001340.. note::
1341
1342 While it's infrequent, it's possible to define multiple tasks as
1343 dependencies when calling ``addtask``. For example, here's a snippet
1344 from the OpenEmbedded class file ``package_tar.bbclass``::
1345
1346 addtask package_write_tar before do_build after do_packagedata do_package
1347
1348 Note how the ``package_write_tar`` task has to wait until both of
1349 ``do_packagedata`` and ``do_package`` complete.
1350
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001351Deleting a Task
1352---------------
1353
1354As well as being able to add tasks, you can delete them. Simply use the
1355``deltask`` command to delete a task. For example, to delete the example
Andrew Geisslerc926e172021-05-07 16:11:35 -05001356task used in the previous sections, you would use::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001357
1358 deltask printdate
1359
1360If you delete a task using the ``deltask`` command and the task has
1361dependencies, the dependencies are not reconnected. For example, suppose
1362you have three tasks named ``do_a``, ``do_b``, and ``do_c``.
1363Furthermore, ``do_c`` is dependent on ``do_b``, which in turn is
1364dependent on ``do_a``. Given this scenario, if you use ``deltask`` to
1365delete ``do_b``, the implicit dependency relationship between ``do_c``
1366and ``do_a`` through ``do_b`` no longer exists, and ``do_c``
1367dependencies are not updated to include ``do_a``. Thus, ``do_c`` is free
1368to run before ``do_a``.
1369
1370If you want dependencies such as these to remain intact, use the
1371``[noexec]`` varflag to disable the task instead of using the
Andrew Geisslerc926e172021-05-07 16:11:35 -05001372``deltask`` command to delete it::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001373
1374 do_b[noexec] = "1"
1375
1376Passing Information Into the Build Task Environment
1377---------------------------------------------------
1378
1379When running a task, BitBake tightly controls the shell execution
1380environment of the build tasks to make sure unwanted contamination from
1381the build machine cannot influence the build.
1382
1383.. note::
1384
1385 By default, BitBake cleans the environment to include only those
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001386 things exported or listed in its passthrough list to ensure that the
1387 build environment is reproducible and consistent. You can prevent this
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001388 "cleaning" by setting the :term:`BB_PRESERVE_ENV` variable.
1389
1390Consequently, if you do want something to get passed into the build task
1391environment, you must take these two steps:
1392
1393#. Tell BitBake to load what you want from the environment into the
1394 datastore. You can do so through the
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001395 :term:`BB_ENV_PASSTHROUGH` and
1396 :term:`BB_ENV_PASSTHROUGH_ADDITIONS` variables. For
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001397 example, assume you want to prevent the build system from accessing
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001398 your ``$HOME/.ccache`` directory. The following command adds the
1399 the environment variable ``CCACHE_DIR`` to BitBake's passthrough
1400 list to allow that variable into the datastore::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001401
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001402 export BB_ENV_PASSTHROUGH_ADDITIONS="$BB_ENV_PASSTHROUGH_ADDITIONS CCACHE_DIR"
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001403
1404#. Tell BitBake to export what you have loaded into the datastore to the
1405 task environment of every running task. Loading something from the
1406 environment into the datastore (previous step) only makes it
1407 available in the datastore. To export it to the task environment of
1408 every running task, use a command similar to the following in your
1409 local configuration file ``local.conf`` or your distribution
Andrew Geisslerc926e172021-05-07 16:11:35 -05001410 configuration file::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001411
1412 export CCACHE_DIR
1413
1414 .. note::
1415
1416 A side effect of the previous steps is that BitBake records the
1417 variable as a dependency of the build process in things like the
1418 setscene checksums. If doing so results in unnecessary rebuilds of
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001419 tasks, you can also flag the variable so that the setscene code
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001420 ignores the dependency when it creates checksums.
1421
1422Sometimes, it is useful to be able to obtain information from the
1423original execution environment. BitBake saves a copy of the original
1424environment into a special variable named :term:`BB_ORIGENV`.
1425
Patrick Williams213cb262021-08-07 19:21:33 -05001426The :term:`BB_ORIGENV` variable returns a datastore object that can be
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001427queried using the standard datastore operators such as
1428``getVar(, False)``. The datastore object is useful, for example, to
Andrew Geisslerc926e172021-05-07 16:11:35 -05001429find the original ``DISPLAY`` variable. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001430
1431 origenv = d.getVar("BB_ORIGENV", False)
1432 bar = origenv.getVar("BAR", False)
1433
1434The previous example returns ``BAR`` from the original execution
1435environment.
1436
1437Variable Flags
1438==============
1439
1440Variable flags (varflags) help control a task's functionality and
1441dependencies. BitBake reads and writes varflags to the datastore using
Andrew Geisslerc926e172021-05-07 16:11:35 -05001442the following command forms::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001443
1444 variable = d.getVarFlags("variable")
1445 self.d.setVarFlags("FOO", {"func": True})
1446
1447When working with varflags, the same syntax, with the exception of
1448overrides, applies. In other words, you can set, append, and prepend
1449varflags just like variables. See the
1450":ref:`bitbake-user-manual/bitbake-user-manual-metadata:variable flag syntax`" section for details.
1451
1452BitBake has a defined set of varflags available for recipes and classes.
1453Tasks support a number of these flags which control various
1454functionality of the task:
1455
1456- ``[cleandirs]``: Empty directories that should be created before
1457 the task runs. Directories that already exist are removed and
1458 recreated to empty them.
1459
1460- ``[depends]``: Controls inter-task dependencies. See the
1461 :term:`DEPENDS` variable and the
1462 ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:inter-task
1463 dependencies`" section for more information.
1464
1465- ``[deptask]``: Controls task build-time dependencies. See the
1466 :term:`DEPENDS` variable and the ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:build dependencies`" section for more information.
1467
1468- ``[dirs]``: Directories that should be created before the task
1469 runs. Directories that already exist are left as is. The last
1470 directory listed is used as the current working directory for the
1471 task.
1472
1473- ``[lockfiles]``: Specifies one or more lockfiles to lock while the
1474 task executes. Only one task may hold a lockfile, and any task that
1475 attempts to lock an already locked file will block until the lock is
1476 released. You can use this variable flag to accomplish mutual
1477 exclusion.
1478
1479- ``[noexec]``: When set to "1", marks the task as being empty, with
1480 no execution required. You can use the ``[noexec]`` flag to set up
1481 tasks as dependency placeholders, or to disable tasks defined
1482 elsewhere that are not needed in a particular recipe.
1483
1484- ``[nostamp]``: When set to "1", tells BitBake to not generate a
1485 stamp file for a task, which implies the task should always be
1486 executed.
1487
1488 .. caution::
1489
1490 Any task that depends (possibly indirectly) on a ``[nostamp]`` task will
1491 always be executed as well. This can cause unnecessary rebuilding if you
1492 are not careful.
1493
1494- ``[number_threads]``: Limits tasks to a specific number of
1495 simultaneous threads during execution. This varflag is useful when
1496 your build host has a large number of cores but certain tasks need to
1497 be rate-limited due to various kinds of resource constraints (e.g. to
1498 avoid network throttling). ``number_threads`` works similarly to the
1499 :term:`BB_NUMBER_THREADS` variable but is task-specific.
1500
1501 Set the value globally. For example, the following makes sure the
1502 ``do_fetch`` task uses no more than two simultaneous execution
1503 threads: do_fetch[number_threads] = "2"
1504
1505 .. warning::
1506
1507 - Setting the varflag in individual recipes rather than globally
1508 can result in unpredictable behavior.
1509
1510 - Setting the varflag to a value greater than the value used in
Patrick Williams213cb262021-08-07 19:21:33 -05001511 the :term:`BB_NUMBER_THREADS` variable causes ``number_threads`` to
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001512 have no effect.
1513
1514- ``[postfuncs]``: List of functions to call after the completion of
1515 the task.
1516
1517- ``[prefuncs]``: List of functions to call before the task executes.
1518
1519- ``[rdepends]``: Controls inter-task runtime dependencies. See the
1520 :term:`RDEPENDS` variable, the
1521 :term:`RRECOMMENDS` variable, and the
1522 ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:inter-task dependencies`" section for
1523 more information.
1524
1525- ``[rdeptask]``: Controls task runtime dependencies. See the
1526 :term:`RDEPENDS` variable, the
1527 :term:`RRECOMMENDS` variable, and the
1528 ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:runtime dependencies`" section for more
1529 information.
1530
1531- ``[recideptask]``: When set in conjunction with ``recrdeptask``,
1532 specifies a task that should be inspected for additional
1533 dependencies.
1534
1535- ``[recrdeptask]``: Controls task recursive runtime dependencies.
1536 See the :term:`RDEPENDS` variable, the
1537 :term:`RRECOMMENDS` variable, and the
1538 ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:recursive dependencies`" section for
1539 more information.
1540
1541- ``[stamp-extra-info]``: Extra stamp information to append to the
1542 task's stamp. As an example, OpenEmbedded uses this flag to allow
1543 machine-specific tasks.
1544
1545- ``[umask]``: The umask to run the task under.
1546
1547Several varflags are useful for controlling how signatures are
1548calculated for variables. For more information on this process, see the
1549":ref:`bitbake-user-manual/bitbake-user-manual-execution:checksums (signatures)`" section.
1550
1551- ``[vardeps]``: Specifies a space-separated list of additional
1552 variables to add to a variable's dependencies for the purposes of
1553 calculating its signature. Adding variables to this list is useful,
1554 for example, when a function refers to a variable in a manner that
1555 does not allow BitBake to automatically determine that the variable
1556 is referred to.
1557
1558- ``[vardepsexclude]``: Specifies a space-separated list of variables
1559 that should be excluded from a variable's dependencies for the
1560 purposes of calculating its signature.
1561
1562- ``[vardepvalue]``: If set, instructs BitBake to ignore the actual
1563 value of the variable and instead use the specified value when
1564 calculating the variable's signature.
1565
1566- ``[vardepvalueexclude]``: Specifies a pipe-separated list of
1567 strings to exclude from the variable's value when calculating the
1568 variable's signature.
1569
1570Events
1571======
1572
1573BitBake allows installation of event handlers within recipe and class
1574files. Events are triggered at certain points during operation, such as
1575the beginning of operation against a given recipe (i.e. ``*.bb``), the
1576start of a given task, a task failure, a task success, and so forth. The
1577intent is to make it easy to do things like email notification on build
1578failures.
1579
1580Following is an example event handler that prints the name of the event
Patrick Williams213cb262021-08-07 19:21:33 -05001581and the content of the :term:`FILE` variable::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001582
1583 addhandler myclass_eventhandler
1584 python myclass_eventhandler() {
1585 from bb.event import getName
1586 print("The name of the Event is %s" % getName(e))
1587 print("The file we run for is %s" % d.getVar('FILE'))
1588 }
1589 myclass_eventhandler[eventmask] = "bb.event.BuildStarted
1590 bb.event.BuildCompleted"
1591
1592In the previous example, an eventmask has been
1593set so that the handler only sees the "BuildStarted" and
1594"BuildCompleted" events. This event handler gets called every time an
1595event matching the eventmask is triggered. A global variable "e" is
1596defined, which represents the current event. With the ``getName(e)``
1597method, you can get the name of the triggered event. The global
1598datastore is available as "d". In legacy code, you might see "e.data"
1599used to get the datastore. However, realize that "e.data" is deprecated
1600and you should use "d" going forward.
1601
1602The context of the datastore is appropriate to the event in question.
1603For example, "BuildStarted" and "BuildCompleted" events run before any
1604tasks are executed so would be in the global configuration datastore
1605namespace. No recipe-specific metadata exists in that namespace. The
1606"BuildStarted" and "BuildCompleted" events also run in the main
1607cooker/server process rather than any worker context. Thus, any changes
1608made to the datastore would be seen by other cooker/server events within
1609the current build but not seen outside of that build or in any worker
1610context. Task events run in the actual tasks in question consequently
1611have recipe-specific and task-specific contents. These events run in the
1612worker context and are discarded at the end of task execution.
1613
1614During a standard build, the following common events might occur. The
1615following events are the most common kinds of events that most metadata
1616might have an interest in viewing:
1617
1618- ``bb.event.ConfigParsed()``: Fired when the base configuration; which
1619 consists of ``bitbake.conf``, ``base.bbclass`` and any global
Patrick Williams213cb262021-08-07 19:21:33 -05001620 :term:`INHERIT` statements; has been parsed. You can see multiple such
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001621 events when each of the workers parse the base configuration or if
1622 the server changes configuration and reparses. Any given datastore
1623 only has one such event executed against it, however. If
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001624 :term:`BB_INVALIDCONF` is set in the datastore by the event
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001625 handler, the configuration is reparsed and a new event triggered,
1626 allowing the metadata to update configuration.
1627
1628- ``bb.event.HeartbeatEvent()``: Fires at regular time intervals of one
1629 second. You can configure the interval time using the
1630 ``BB_HEARTBEAT_EVENT`` variable. The event's "time" attribute is the
1631 ``time.time()`` value when the event is triggered. This event is
1632 useful for activities such as system state monitoring.
1633
1634- ``bb.event.ParseStarted()``: Fired when BitBake is about to start
1635 parsing recipes. This event's "total" attribute represents the number
1636 of recipes BitBake plans to parse.
1637
1638- ``bb.event.ParseProgress()``: Fired as parsing progresses. This
1639 event's "current" attribute is the number of recipes parsed as well
1640 as the "total" attribute.
1641
1642- ``bb.event.ParseCompleted()``: Fired when parsing is complete. This
1643 event's "cached", "parsed", "skipped", "virtuals", "masked", and
1644 "errors" attributes provide statistics for the parsing results.
1645
1646- ``bb.event.BuildStarted()``: Fired when a new build starts. BitBake
1647 fires multiple "BuildStarted" events (one per configuration) when
1648 multiple configuration (multiconfig) is enabled.
1649
1650- ``bb.build.TaskStarted()``: Fired when a task starts. This event's
1651 "taskfile" attribute points to the recipe from which the task
1652 originates. The "taskname" attribute, which is the task's name,
1653 includes the ``do_`` prefix, and the "logfile" attribute point to
1654 where the task's output is stored. Finally, the "time" attribute is
1655 the task's execution start time.
1656
1657- ``bb.build.TaskInvalid()``: Fired if BitBake tries to execute a task
1658 that does not exist.
1659
1660- ``bb.build.TaskFailedSilent()``: Fired for setscene tasks that fail
1661 and should not be presented to the user verbosely.
1662
1663- ``bb.build.TaskFailed()``: Fired for normal tasks that fail.
1664
1665- ``bb.build.TaskSucceeded()``: Fired when a task successfully
1666 completes.
1667
1668- ``bb.event.BuildCompleted()``: Fired when a build finishes.
1669
1670- ``bb.cooker.CookerExit()``: Fired when the BitBake server/cooker
1671 shuts down. This event is usually only seen by the UIs as a sign they
1672 should also shutdown.
1673
1674This next list of example events occur based on specific requests to the
1675server. These events are often used to communicate larger pieces of
1676information from the BitBake server to other parts of BitBake such as
1677user interfaces:
1678
1679- ``bb.event.TreeDataPreparationStarted()``
1680- ``bb.event.TreeDataPreparationProgress()``
1681- ``bb.event.TreeDataPreparationCompleted()``
1682- ``bb.event.DepTreeGenerated()``
1683- ``bb.event.CoreBaseFilesFound()``
1684- ``bb.event.ConfigFilePathFound()``
1685- ``bb.event.FilesMatchingFound()``
1686- ``bb.event.ConfigFilesFound()``
1687- ``bb.event.TargetsTreeGenerated()``
1688
1689.. _variants-class-extension-mechanism:
1690
Andrew Geissler9aee5002022-03-30 16:27:02 +00001691Variants --- Class Extension Mechanism
1692======================================
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001693
Andrew Geissler09036742021-06-25 14:25:14 -05001694BitBake supports multiple incarnations of a recipe file via the
1695:term:`BBCLASSEXTEND` variable.
1696
1697The :term:`BBCLASSEXTEND` variable is a space separated list of classes used
1698to "extend" the recipe for each variant. Here is an example that results in a
1699second incarnation of the current recipe being available. This second
1700incarnation will have the "native" class inherited. ::
1701
1702 BBCLASSEXTEND = "native"
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001703
1704.. note::
1705
1706 The mechanism for this class extension is extremely specific to the
1707 implementation. Usually, the recipe's :term:`PROVIDES` , :term:`PN` , and
1708 :term:`DEPENDS` variables would need to be modified by the extension
1709 class. For specific examples, see the OE-Core native , nativesdk , and
1710 multilib classes.
1711
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001712Dependencies
1713============
1714
1715To allow for efficient parallel processing, BitBake handles dependencies
1716at the task level. Dependencies can exist both between tasks within a
1717single recipe and between tasks in different recipes. Following are
1718examples of each:
1719
1720- For tasks within a single recipe, a recipe's ``do_configure`` task
1721 might need to complete before its ``do_compile`` task can run.
1722
1723- For tasks in different recipes, one recipe's ``do_configure`` task
1724 might require another recipe's ``do_populate_sysroot`` task to finish
1725 first such that the libraries and headers provided by the other
1726 recipe are available.
1727
1728This section describes several ways to declare dependencies. Remember,
1729even though dependencies are declared in different ways, they are all
1730simply dependencies between tasks.
1731
1732.. _dependencies-internal-to-the-bb-file:
1733
1734Dependencies Internal to the ``.bb`` File
1735-----------------------------------------
1736
1737BitBake uses the ``addtask`` directive to manage dependencies that are
1738internal to a given recipe file. You can use the ``addtask`` directive
1739to indicate when a task is dependent on other tasks or when other tasks
Andrew Geisslerc926e172021-05-07 16:11:35 -05001740depend on that recipe. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001741
1742 addtask printdate after do_fetch before do_build
1743
1744In this example, the ``do_printdate`` task
1745depends on the completion of the ``do_fetch`` task, and the ``do_build``
1746task depends on the completion of the ``do_printdate`` task.
1747
1748.. note::
1749
1750 For a task to run, it must be a direct or indirect dependency of some
1751 other task that is scheduled to run.
1752
1753 For illustration, here are some examples:
1754
1755 - The directive ``addtask mytask before do_configure`` causes
1756 ``do_mytask`` to run before ``do_configure`` runs. Be aware that
1757 ``do_mytask`` still only runs if its :ref:`input
1758 checksum <bitbake-user-manual/bitbake-user-manual-execution:checksums (signatures)>` has changed since the last time it was
1759 run. Changes to the input checksum of ``do_mytask`` also
1760 indirectly cause ``do_configure`` to run.
1761
1762 - The directive ``addtask mytask after do_configure`` by itself
1763 never causes ``do_mytask`` to run. ``do_mytask`` can still be run
Andrew Geisslerc926e172021-05-07 16:11:35 -05001764 manually as follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001765
1766 $ bitbake recipe -c mytask
1767
1768 Declaring ``do_mytask`` as a dependency of some other task that is
1769 scheduled to run also causes it to run. Regardless, the task runs after
1770 ``do_configure``.
1771
1772Build Dependencies
1773------------------
1774
1775BitBake uses the :term:`DEPENDS` variable to manage
1776build time dependencies. The ``[deptask]`` varflag for tasks signifies
Patrick Williams213cb262021-08-07 19:21:33 -05001777the task of each item listed in :term:`DEPENDS` that must complete before
Andrew Geisslerc926e172021-05-07 16:11:35 -05001778that task can be executed. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001779
1780 do_configure[deptask] = "do_populate_sysroot"
1781
1782In this example, the ``do_populate_sysroot`` task
Patrick Williams213cb262021-08-07 19:21:33 -05001783of each item in :term:`DEPENDS` must complete before ``do_configure`` can
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001784execute.
1785
1786Runtime Dependencies
1787--------------------
1788
1789BitBake uses the :term:`PACKAGES`, :term:`RDEPENDS`, and :term:`RRECOMMENDS`
1790variables to manage runtime dependencies.
1791
Patrick Williams213cb262021-08-07 19:21:33 -05001792The :term:`PACKAGES` variable lists runtime packages. Each of those packages
1793can have :term:`RDEPENDS` and :term:`RRECOMMENDS` runtime dependencies. The
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001794``[rdeptask]`` flag for tasks is used to signify the task of each item
1795runtime dependency which must have completed before that task can be
1796executed. ::
1797
1798 do_package_qa[rdeptask] = "do_packagedata"
1799
1800In the previous
Patrick Williams213cb262021-08-07 19:21:33 -05001801example, the ``do_packagedata`` task of each item in :term:`RDEPENDS` must
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001802have completed before ``do_package_qa`` can execute.
Patrick Williams213cb262021-08-07 19:21:33 -05001803Although :term:`RDEPENDS` contains entries from the
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001804runtime dependency namespace, BitBake knows how to map them back
1805to the build-time dependency namespace, in which the tasks are defined.
1806
1807Recursive Dependencies
1808----------------------
1809
1810BitBake uses the ``[recrdeptask]`` flag to manage recursive task
1811dependencies. BitBake looks through the build-time and runtime
1812dependencies of the current recipe, looks through the task's inter-task
1813dependencies, and then adds dependencies for the listed task. Once
1814BitBake has accomplished this, it recursively works through the
1815dependencies of those tasks. Iterative passes continue until all
1816dependencies are discovered and added.
1817
1818The ``[recrdeptask]`` flag is most commonly used in high-level recipes
1819that need to wait for some task to finish "globally". For example,
Andrew Geisslerc926e172021-05-07 16:11:35 -05001820``image.bbclass`` has the following::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001821
1822 do_rootfs[recrdeptask] += "do_packagedata"
1823
1824This statement says that the ``do_packagedata`` task of
1825the current recipe and all recipes reachable (by way of dependencies)
1826from the image recipe must run before the ``do_rootfs`` task can run.
1827
1828BitBake allows a task to recursively depend on itself by
Andrew Geisslerc926e172021-05-07 16:11:35 -05001829referencing itself in the task list::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001830
1831 do_a[recrdeptask] = "do_a do_b"
1832
1833In the same way as before, this means that the ``do_a``
1834and ``do_b`` tasks of the current recipe and all
1835recipes reachable (by way of dependencies) from the recipe
1836must run before the ``do_a`` task can run. In this
1837case BitBake will ignore the current recipe's ``do_a``
1838task circular dependency on itself.
1839
1840Inter-Task Dependencies
1841-----------------------
1842
1843BitBake uses the ``[depends]`` flag in a more generic form to manage
1844inter-task dependencies. This more generic form allows for
1845inter-dependency checks for specific tasks rather than checks for the
Patrick Williams213cb262021-08-07 19:21:33 -05001846data in :term:`DEPENDS`. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001847
1848 do_patch[depends] = "quilt-native:do_populate_sysroot"
1849
1850In this example, the ``do_populate_sysroot`` task of the target ``quilt-native``
1851must have completed before the ``do_patch`` task can execute.
1852
1853The ``[rdepends]`` flag works in a similar way but takes targets in the
1854runtime namespace instead of the build-time dependency namespace.
1855
1856Functions You Can Call From Within Python
1857=========================================
1858
1859BitBake provides many functions you can call from within Python
1860functions. This section lists the most commonly used functions, and
1861mentions where to find others.
1862
1863Functions for Accessing Datastore Variables
1864-------------------------------------------
1865
1866It is often necessary to access variables in the BitBake datastore using
1867Python functions. The BitBake datastore has an API that allows you this
1868access. Here is a list of available operations:
1869
1870.. list-table::
1871 :widths: auto
1872 :header-rows: 1
1873
1874 * - *Operation*
1875 - *Description*
1876 * - ``d.getVar("X", expand)``
1877 - Returns the value of variable "X". Using "expand=True" expands the
1878 value. Returns "None" if the variable "X" does not exist.
1879 * - ``d.setVar("X", "value")``
1880 - Sets the variable "X" to "value"
1881 * - ``d.appendVar("X", "value")``
1882 - Adds "value" to the end of the variable "X". Acts like ``d.setVar("X",
1883 "value")`` if the variable "X" does not exist.
1884 * - ``d.prependVar("X", "value")``
1885 - Adds "value" to the start of the variable "X". Acts like
1886 ``d.setVar("X","value")`` if the variable "X" does not exist.
1887 * - ``d.delVar("X")``
1888 - Deletes the variable "X" from the datastore. Does nothing if the variable
1889 "X" does not exist.
1890 * - ``d.renameVar("X", "Y")``
1891 - Renames the variable "X" to "Y". Does nothing if the variable "X" does
1892 not exist.
1893 * - ``d.getVarFlag("X", flag, expand)``
1894 - Returns the value of variable "X". Using "expand=True" expands the
1895 value. Returns "None" if either the variable "X" or the named flag does
1896 not exist.
1897 * - ``d.setVarFlag("X", flag, "value")``
1898 - Sets the named flag for variable "X" to "value".
1899 * - ``d.appendVarFlag("X", flag, "value")``
1900 - Appends "value" to the named flag on the variable "X". Acts like
1901 ``d.setVarFlag("X", flag, "value")`` if the named flag does not exist.
1902 * - ``d.prependVarFlag("X", flag, "value")``
1903 - Prepends "value" to the named flag on the variable "X". Acts like
1904 ``d.setVarFlag("X", flag, "value")`` if the named flag does not exist.
1905 * - ``d.delVarFlag("X", flag)``
1906 - Deletes the named flag on the variable "X" from the datastore.
1907 * - ``d.setVarFlags("X", flagsdict)``
1908 - Sets the flags specified in the ``flagsdict()``
1909 parameter. ``setVarFlags`` does not clear previous flags. Think of this
1910 operation as ``addVarFlags``.
1911 * - ``d.getVarFlags("X")``
1912 - Returns a ``flagsdict`` of the flags for the variable "X". Returns "None"
1913 if the variable "X" does not exist.
1914 * - ``d.delVarFlags("X")``
1915 - Deletes all the flags for the variable "X". Does nothing if the variable
1916 "X" does not exist.
1917 * - ``d.expand(expression)``
1918 - Expands variable references in the specified string
1919 expression. References to variables that do not exist are left as is. For
1920 example, ``d.expand("foo ${X}")`` expands to the literal string "foo
1921 ${X}" if the variable "X" does not exist.
1922
1923Other Functions
1924---------------
1925
1926You can find many other functions that can be called from Python by
1927looking at the source code of the ``bb`` module, which is in
1928``bitbake/lib/bb``. For example, ``bitbake/lib/bb/utils.py`` includes
1929the commonly used functions ``bb.utils.contains()`` and
1930``bb.utils.mkdirhier()``, which come with docstrings.
1931
Andrew Geissler87f5cff2022-09-30 13:13:31 -05001932Testing and Debugging BitBake Python code
1933-----------------------------------------
1934
1935The OpenEmbedded build system implements a convenient ``pydevshell`` target which
1936you can use to access the BitBake datastore and experiment with your own Python
1937code. See :yocto_docs:`Using a Python Development Shell
1938</dev-manual/common-tasks.html#using-a-python-development-shell>` in the Yocto
1939Project manual for details.
1940
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001941Task Checksums and Setscene
1942===========================
1943
1944BitBake uses checksums (or signatures) along with the setscene to
1945determine if a task needs to be run. This section describes the process.
1946To help understand how BitBake does this, the section assumes an
1947OpenEmbedded metadata-based example.
1948
1949These checksums are stored in :term:`STAMP`. You can
Andrew Geisslerc926e172021-05-07 16:11:35 -05001950examine the checksums using the following BitBake command::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001951
1952 $ bitbake-dumpsigs
1953
1954This command returns the signature data in a readable
1955format that allows you to examine the inputs used when the OpenEmbedded
1956build system generates signatures. For example, using
1957``bitbake-dumpsigs`` allows you to examine the ``do_compile`` task's
Andrew Geisslerf0343792020-11-18 10:42:21 -06001958"sigdata" for a C application (e.g. ``bash``). Running the command also
1959reveals that the "CC" variable is part of the inputs that are hashed.
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001960Any changes to this variable would invalidate the stamp and cause the
1961``do_compile`` task to run.
1962
1963The following list describes related variables:
1964
1965- :term:`BB_HASHCHECK_FUNCTION`:
1966 Specifies the name of the function to call during the "setscene" part
1967 of the task's execution in order to validate the list of task hashes.
1968
1969- :term:`BB_SETSCENE_DEPVALID`:
1970 Specifies a function BitBake calls that determines whether BitBake
1971 requires a setscene dependency to be met.
1972
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001973- :term:`BB_TASKHASH`: Within an executing task,
1974 this variable holds the hash of the task as returned by the currently
1975 enabled signature generator.
1976
1977- :term:`STAMP`: The base path to create stamp files.
1978
1979- :term:`STAMPCLEAN`: Again, the base path to
1980 create stamp files but can use wildcards for matching a range of
1981 files for clean operations.
1982
1983Wildcard Support in Variables
1984=============================
1985
1986Support for wildcard use in variables varies depending on the context in
Andrew Geissler5199d832021-09-24 16:47:35 -05001987which it is used. For example, some variables and filenames allow
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001988limited use of wildcards through the "``%``" and "``*``" characters.
1989Other variables or names support Python's
1990`glob <https://docs.python.org/3/library/glob.html>`_ syntax,
1991`fnmatch <https://docs.python.org/3/library/fnmatch.html#module-fnmatch>`_
1992syntax, or
1993`Regular Expression (re) <https://docs.python.org/3/library/re.html>`_
1994syntax.
1995
1996For variables that have wildcard suport, the documentation describes
1997which form of wildcard, its use, and its limitations.