blob: af4b135867acdee159c74570c8624fb6df5ea668 [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
198It is possible to use a "weaker" assignment than in the previous section
199by using the "??=" operator. This assignment behaves identical to "?="
200except that the assignment is made at the end of the parsing process
201rather than immediately. Consequently, when multiple "??=" assignments
202exist, the last one is used. Also, any "=" or "?=" assignment will
Andrew Geisslerc926e172021-05-07 16:11:35 -0500203override the value set with "??=". Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500204
205 A ??= "somevalue"
206 A ??= "someothervalue"
207
208If ``A`` is set before the above statements are
209parsed, the variable retains its value. If ``A`` is not set, the
210variable is set to "someothervalue".
211
212Again, this assignment is a "lazy" or "weak" assignment because it does
213not occur until the end of the parsing process.
214
215Immediate variable expansion (:=)
216---------------------------------
217
218The ":=" operator results in a variable's contents being expanded
Andrew Geisslerc926e172021-05-07 16:11:35 -0500219immediately, rather than when the variable is actually used::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500220
221 T = "123"
222 A := "test ${T}"
223 T = "456"
224 B := "${T} ${C}"
225 C = "cval"
226 C := "${C}append"
227
228In this example, ``A`` contains "test 123", even though the final value
Patrick Williams213cb262021-08-07 19:21:33 -0500229of :term:`T` is "456". The variable :term:`B` will end up containing "456
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500230cvalappend". This is because references to undefined variables are
231preserved as is during (immediate)expansion. This is in contrast to GNU
232Make, where undefined variables expand to nothing. The variable ``C``
233contains "cvalappend" since ``${C}`` immediately expands to "cval".
234
235.. _appending-and-prepending:
236
237Appending (+=) and prepending (=+) With Spaces
238----------------------------------------------
239
240Appending and prepending values is common and can be accomplished using
241the "+=" and "=+" operators. These operators insert a space between the
242current value and prepended or appended value.
243
244These operators take immediate effect during parsing. Here are some
Andrew Geisslerc926e172021-05-07 16:11:35 -0500245examples::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500246
247 B = "bval"
248 B += "additionaldata"
249 C = "cval"
250 C =+ "test"
251
Patrick Williams213cb262021-08-07 19:21:33 -0500252The variable :term:`B` contains "bval additionaldata" and ``C`` contains "test
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500253cval".
254
255.. _appending-and-prepending-without-spaces:
256
257Appending (.=) and Prepending (=.) Without Spaces
258-------------------------------------------------
259
260If you want to append or prepend values without an inserted space, use
261the ".=" and "=." operators.
262
263These operators take immediate effect during parsing. Here are some
Andrew Geisslerc926e172021-05-07 16:11:35 -0500264examples::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500265
266 B = "bval"
267 B .= "additionaldata"
268 C = "cval"
269 C =. "test"
270
Patrick Williams213cb262021-08-07 19:21:33 -0500271The variable :term:`B` contains "bvaladditionaldata" and ``C`` contains
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500272"testcval".
273
274Appending and Prepending (Override Style Syntax)
275------------------------------------------------
276
277You can also append and prepend a variable's value using an override
278style syntax. When you use this syntax, no spaces are inserted.
279
280These operators differ from the ":=", ".=", "=.", "+=", and "=+"
281operators in that their effects are applied at variable expansion time
Andrew Geisslerc926e172021-05-07 16:11:35 -0500282rather than being immediately applied. Here are some examples::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500283
284 B = "bval"
Patrick Williams213cb262021-08-07 19:21:33 -0500285 B:append = " additional data"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500286 C = "cval"
Patrick Williams213cb262021-08-07 19:21:33 -0500287 C:prepend = "additional data "
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500288 D = "dval"
Patrick Williams213cb262021-08-07 19:21:33 -0500289 D:append = "additional data"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500290
Patrick Williams213cb262021-08-07 19:21:33 -0500291The variable :term:`B`
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500292becomes "bval additional data" and ``C`` becomes "additional data cval".
293The variable ``D`` becomes "dvaladditional data".
294
295.. note::
296
297 You must control all spacing when you use the override syntax.
298
299It is also possible to append and prepend to shell functions and
300BitBake-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`"
301sections for examples.
302
303.. _removing-override-style-syntax:
304
305Removal (Override Style Syntax)
306-------------------------------
307
308You can remove values from lists using the removal override style
309syntax. Specifying a value for removal causes all occurrences of that
310value to be removed from the variable.
311
312When you use this syntax, BitBake expects one or more strings.
Andrew Geisslerc926e172021-05-07 16:11:35 -0500313Surrounding spaces and spacing are preserved. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500314
315 FOO = "123 456 789 123456 123 456 123 456"
Patrick Williams213cb262021-08-07 19:21:33 -0500316 FOO:remove = "123"
317 FOO:remove = "456"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500318 FOO2 = " abc def ghi abcdef abc def abc def def"
Patrick Williams213cb262021-08-07 19:21:33 -0500319 FOO2:remove = "\
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500320 def \
321 abc \
322 ghi \
323 "
324
325The variable ``FOO`` becomes
326" 789 123456 " and ``FOO2`` becomes " abcdef ".
327
Patrick Williams213cb262021-08-07 19:21:33 -0500328Like ":append" and ":prepend", ":remove" is applied at variable
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500329expansion time.
330
331Override Style Operation Advantages
332-----------------------------------
333
Patrick Williams213cb262021-08-07 19:21:33 -0500334An advantage of the override style operations ":append", ":prepend", and
335":remove" as compared to the "+=" and "=+" operators is that the
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500336override style operators provide guaranteed operations. For example,
337consider a class ``foo.bbclass`` that needs to add the value "val" to
Andrew Geisslerc926e172021-05-07 16:11:35 -0500338the variable ``FOO``, and a recipe that uses ``foo.bbclass`` as follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500339
340 inherit foo
341 FOO = "initial"
342
343If ``foo.bbclass`` uses the "+=" operator,
344as follows, then the final value of ``FOO`` will be "initial", which is
Andrew Geisslerc926e172021-05-07 16:11:35 -0500345not what is desired::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500346
347 FOO += "val"
348
349If, on the other hand, ``foo.bbclass``
Patrick Williams213cb262021-08-07 19:21:33 -0500350uses the ":append" operator, then the final value of ``FOO`` will be
Andrew Geisslerc926e172021-05-07 16:11:35 -0500351"initial val", as intended::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500352
Patrick Williams213cb262021-08-07 19:21:33 -0500353 FOO:append = " val"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500354
355.. note::
356
Patrick Williams213cb262021-08-07 19:21:33 -0500357 It is never necessary to use "+=" together with ":append". The following
Andrew Geisslerc926e172021-05-07 16:11:35 -0500358 sequence of assignments appends "barbaz" to FOO::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500359
Patrick Williams213cb262021-08-07 19:21:33 -0500360 FOO:append = "bar"
361 FOO:append = "baz"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500362
363
364 The only effect of changing the second assignment in the previous
365 example to use "+=" would be to add a space before "baz" in the
366 appended value (due to how the "+=" operator works).
367
368Another advantage of the override style operations is that you can
369combine them with other overrides as described in the
370":ref:`bitbake-user-manual/bitbake-user-manual-metadata:conditional syntax (overrides)`" section.
371
372Variable Flag Syntax
373--------------------
374
375Variable flags are BitBake's implementation of variable properties or
376attributes. It is a way of tagging extra information onto a variable.
377You can find more out about variable flags in general in the
378":ref:`bitbake-user-manual/bitbake-user-manual-metadata:variable flags`" section.
379
380You can define, append, and prepend values to variable flags. All the
381standard syntax operations previously mentioned work for variable flags
Patrick Williams213cb262021-08-07 19:21:33 -0500382except for override style syntax (i.e. ":prepend", ":append", and
383":remove").
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500384
Andrew Geisslerc926e172021-05-07 16:11:35 -0500385Here are some examples showing how to set variable flags::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500386
387 FOO[a] = "abc"
388 FOO[b] = "123"
389 FOO[a] += "456"
390
391The variable ``FOO`` has two flags:
392``[a]`` and ``[b]``. The flags are immediately set to "abc" and "123",
393respectively. The ``[a]`` flag becomes "abc 456".
394
395No need exists to pre-define variable flags. You can simply start using
396them. One extremely common application is to attach some brief
Andrew Geisslerc926e172021-05-07 16:11:35 -0500397documentation to a BitBake variable as follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500398
399 CACHE[doc] = "The directory holding the cache of the metadata."
400
401Inline Python Variable Expansion
402--------------------------------
403
404You can use inline Python variable expansion to set variables. Here is
Andrew Geisslerc926e172021-05-07 16:11:35 -0500405an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500406
407 DATE = "${@time.strftime('%Y%m%d',time.gmtime())}"
408
409This example results in the ``DATE`` variable being set to the current date.
410
411Probably the most common use of this feature is to extract the value of
412variables from BitBake's internal data dictionary, ``d``. The following
413lines select the values of a package name and its version number,
Andrew Geisslerc926e172021-05-07 16:11:35 -0500414respectively::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500415
Andrew Geissler5199d832021-09-24 16:47:35 -0500416 PN = "${@bb.parse.vars_from_file(d.getVar('FILE', False),d)[0] or 'defaultpkgname'}"
417 PV = "${@bb.parse.vars_from_file(d.getVar('FILE', False),d)[1] or '1.0'}"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500418
419.. note::
420
421 Inline Python expressions work just like variable expansions insofar as the
422 "=" and ":=" operators are concerned. Given the following assignment, foo()
Andrew Geisslerc926e172021-05-07 16:11:35 -0500423 is called each time FOO is expanded::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500424
425 FOO = "${@foo()}"
426
427 Contrast this with the following immediate assignment, where foo() is only
Andrew Geisslerc926e172021-05-07 16:11:35 -0500428 called once, while the assignment is parsed::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500429
430 FOO := "${@foo()}"
431
432For a different way to set variables with Python code during parsing,
433see the
434":ref:`bitbake-user-manual/bitbake-user-manual-metadata:anonymous python functions`" section.
435
436Unsetting variables
437-------------------
438
439It is possible to completely remove a variable or a variable flag from
440BitBake's internal data dictionary by using the "unset" keyword. Here is
Andrew Geisslerc926e172021-05-07 16:11:35 -0500441an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500442
443 unset DATE
444 unset do_fetch[noexec]
445
446These two statements remove the ``DATE`` and the ``do_fetch[noexec]`` flag.
447
448Providing Pathnames
449-------------------
450
451When specifying pathnames for use with BitBake, do not use the tilde
452("~") character as a shortcut for your home directory. Doing so might
453cause BitBake to not recognize the path since BitBake does not expand
454this character in the same way a shell would.
455
Andrew Geisslerc926e172021-05-07 16:11:35 -0500456Instead, provide a fuller path as the following example illustrates::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500457
458 BBLAYERS ?= " \
459 /home/scott-lenovo/LayerA \
460 "
461
462Exporting Variables to the Environment
463======================================
464
465You can export variables to the environment of running tasks by using
466the ``export`` keyword. For example, in the following example, the
Andrew Geisslerc926e172021-05-07 16:11:35 -0500467``do_foo`` task prints "value from the environment" when run::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500468
469 export ENV_VARIABLE
470 ENV_VARIABLE = "value from the environment"
471
472 do_foo() {
473 bbplain "$ENV_VARIABLE"
474 }
475
476.. note::
477
478 BitBake does not expand ``$ENV_VARIABLE`` in this case because it lacks the
479 obligatory ``{}`` . Rather, ``$ENV_VARIABLE`` is expanded by the shell.
480
481It does not matter whether ``export ENV_VARIABLE`` appears before or
482after assignments to ``ENV_VARIABLE``.
483
484It is also possible to combine ``export`` with setting a value for the
Andrew Geisslerc926e172021-05-07 16:11:35 -0500485variable. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500486
487 export ENV_VARIABLE = "variable-value"
488
489In the output of ``bitbake -e``, variables that are exported to the
490environment are preceded by "export".
491
492Among the variables commonly exported to the environment are ``CC`` and
493``CFLAGS``, which are picked up by many build systems.
494
495Conditional Syntax (Overrides)
496==============================
497
498BitBake uses :term:`OVERRIDES` to control what
499variables are overridden after BitBake parses recipes and configuration
Patrick Williams213cb262021-08-07 19:21:33 -0500500files. This section describes how you can use :term:`OVERRIDES` as
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500501conditional metadata, talks about key expansion in relationship to
Patrick Williams213cb262021-08-07 19:21:33 -0500502:term:`OVERRIDES`, and provides some examples to help with understanding.
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500503
504Conditional Metadata
505--------------------
506
Patrick Williams213cb262021-08-07 19:21:33 -0500507You can use :term:`OVERRIDES` to conditionally select a specific version of
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500508a variable and to conditionally append or prepend the value of a
509variable.
510
511.. note::
512
Andrew Geissler9aee5002022-03-30 16:27:02 +0000513 Overrides can only use lower-case characters, digits and dashes.
514 In particular, colons are not permitted in override names as they are used to
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500515 separate overrides from each other and from the variable name.
516
Patrick Williams213cb262021-08-07 19:21:33 -0500517- *Selecting a Variable:* The :term:`OVERRIDES` variable is a
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500518 colon-character-separated list that contains items for which you want
519 to satisfy conditions. Thus, if you have a variable that is
Patrick Williams213cb262021-08-07 19:21:33 -0500520 conditional on "arm", and "arm" is in :term:`OVERRIDES`, then the
Andrew Geisslerf0343792020-11-18 10:42:21 -0600521 "arm"-specific version of the variable is used rather than the
Andrew Geisslerc926e172021-05-07 16:11:35 -0500522 non-conditional version. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500523
524 OVERRIDES = "architecture:os:machine"
525 TEST = "default"
Andrew Geissler9aee5002022-03-30 16:27:02 +0000526 TEST:os = "osspecific"
527 TEST:nooverride = "othercondvalue"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500528
Patrick Williams213cb262021-08-07 19:21:33 -0500529 In this example, the :term:`OVERRIDES`
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500530 variable lists three overrides: "architecture", "os", and "machine".
531 The variable ``TEST`` by itself has a default value of "default". You
532 select the os-specific version of the ``TEST`` variable by appending
Andrew Geissler9aee5002022-03-30 16:27:02 +0000533 the "os" override to the variable (i.e. ``TEST:os``).
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500534
535 To better understand this, consider a practical example that assumes
536 an OpenEmbedded metadata-based Linux kernel recipe file. The
537 following lines from the recipe file first set the kernel branch
538 variable ``KBRANCH`` to a default value, then conditionally override
Andrew Geisslerc926e172021-05-07 16:11:35 -0500539 that value based on the architecture of the build::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500540
541 KBRANCH = "standard/base"
Patrick Williams213cb262021-08-07 19:21:33 -0500542 KBRANCH:qemuarm = "standard/arm-versatile-926ejs"
543 KBRANCH:qemumips = "standard/mti-malta32"
544 KBRANCH:qemuppc = "standard/qemuppc"
545 KBRANCH:qemux86 = "standard/common-pc/base"
546 KBRANCH:qemux86-64 = "standard/common-pc-64/base"
547 KBRANCH:qemumips64 = "standard/mti-malta64"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500548
549- *Appending and Prepending:* BitBake also supports append and prepend
550 operations to variable values based on whether a specific item is
Patrick Williams213cb262021-08-07 19:21:33 -0500551 listed in :term:`OVERRIDES`. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500552
553 DEPENDS = "glibc ncurses"
554 OVERRIDES = "machine:local"
Patrick Williams213cb262021-08-07 19:21:33 -0500555 DEPENDS:append:machine = "libmad"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500556
Patrick Williams213cb262021-08-07 19:21:33 -0500557 In this example, :term:`DEPENDS` becomes "glibc ncurses libmad".
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500558
559 Again, using an OpenEmbedded metadata-based kernel recipe file as an
560 example, the following lines will conditionally append to the
Andrew Geisslerc926e172021-05-07 16:11:35 -0500561 ``KERNEL_FEATURES`` variable based on the architecture::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500562
Patrick Williams213cb262021-08-07 19:21:33 -0500563 KERNEL_FEATURES:append = " ${KERNEL_EXTRA_FEATURES}"
564 KERNEL_FEATURES:append:qemux86=" cfg/sound.scc cfg/paravirt_kvm.scc"
565 KERNEL_FEATURES:append:qemux86-64=" cfg/sound.scc cfg/paravirt_kvm.scc"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500566
567- *Setting a Variable for a Single Task:* BitBake supports setting a
Andrew Geisslerc926e172021-05-07 16:11:35 -0500568 variable just for the duration of a single task. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500569
Andrew Geissler9aee5002022-03-30 16:27:02 +0000570 FOO:task-configure = "val 1"
Patrick Williams213cb262021-08-07 19:21:33 -0500571 FOO:task-compile = "val 2"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500572
573 In the
574 previous example, ``FOO`` has the value "val 1" while the
575 ``do_configure`` task is executed, and the value "val 2" while the
576 ``do_compile`` task is executed.
577
578 Internally, this is implemented by prepending the task (e.g.
579 "task-compile:") to the value of
580 :term:`OVERRIDES` for the local datastore of the
581 ``do_compile`` task.
582
583 You can also use this syntax with other combinations (e.g.
Patrick Williams213cb262021-08-07 19:21:33 -0500584 "``:prepend``") as shown in the following example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500585
Patrick Williams213cb262021-08-07 19:21:33 -0500586 EXTRA_OEMAKE:prepend:task-compile = "${PARALLEL_MAKE} "
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500587
Andrew Geissler9aee5002022-03-30 16:27:02 +0000588.. note::
589
590 Before BitBake 1.52 (Honister 3.4), the syntax for :term:`OVERRIDES`
591 used ``_`` instead of ``:``, so you will still find a lot of documentation
592 using ``_append``, ``_prepend``, and ``_remove``, for example.
593
594 For details, see the
595 :yocto_docs:`Overrides Syntax Changes </migration-guides/migration-3.4.html#override-syntax-changes>`
596 section in the Yocto Project manual migration notes.
597
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500598Key Expansion
599-------------
600
601Key expansion happens when the BitBake datastore is finalized. To better
Andrew Geisslerc926e172021-05-07 16:11:35 -0500602understand this, consider the following example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500603
604 A${B} = "X"
605 B = "2"
606 A2 = "Y"
607
608In this case, after all the parsing is complete, BitBake expands
609``${B}`` into "2". This expansion causes ``A2``, which was set to "Y"
610before the expansion, to become "X".
611
612.. _variable-interaction-worked-examples:
613
614Examples
615--------
616
617Despite the previous explanations that show the different forms of
618variable definitions, it can be hard to work out exactly what happens
619when variable operators, conditional overrides, and unconditional
620overrides are combined. This section presents some common scenarios
621along with explanations for variable interactions that typically confuse
622users.
623
624There is often confusion concerning the order in which overrides and
625various "append" operators take effect. Recall that an append or prepend
Patrick Williams213cb262021-08-07 19:21:33 -0500626operation using ":append" and ":prepend" does not result in an immediate
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500627assignment as would "+=", ".=", "=+", or "=.". Consider the following
Andrew Geisslerc926e172021-05-07 16:11:35 -0500628example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500629
630 OVERRIDES = "foo"
631 A = "Z"
Patrick Williams213cb262021-08-07 19:21:33 -0500632 A:foo:append = "X"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500633
634For this case,
635``A`` is unconditionally set to "Z" and "X" is unconditionally and
Patrick Williams213cb262021-08-07 19:21:33 -0500636immediately appended to the variable ``A:foo``. Because overrides have
637not been applied yet, ``A:foo`` is set to "X" due to the append and
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500638``A`` simply equals "Z".
639
640Applying overrides, however, changes things. Since "foo" is listed in
Patrick Williams213cb262021-08-07 19:21:33 -0500641:term:`OVERRIDES`, the conditional variable ``A`` is replaced with the "foo"
642version, which is equal to "X". So effectively, ``A:foo`` replaces
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500643``A``.
644
Andrew Geisslerc926e172021-05-07 16:11:35 -0500645This next example changes the order of the override and the append::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500646
647 OVERRIDES = "foo"
648 A = "Z"
Patrick Williams213cb262021-08-07 19:21:33 -0500649 A:append:foo = "X"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500650
651For this case, before
Patrick Williams213cb262021-08-07 19:21:33 -0500652overrides are handled, ``A`` is set to "Z" and ``A:append:foo`` is set
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500653to "X". Once the override for "foo" is applied, however, ``A`` gets
654appended with "X". Consequently, ``A`` becomes "ZX". Notice that spaces
655are not appended.
656
657This next example has the order of the appends and overrides reversed
Andrew Geisslerc926e172021-05-07 16:11:35 -0500658back as in the first example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500659
660 OVERRIDES = "foo"
661 A = "Y"
Patrick Williams213cb262021-08-07 19:21:33 -0500662 A:foo:append = "Z"
663 A:foo:append = "X"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500664
665For this case, before any overrides are resolved,
666``A`` is set to "Y" using an immediate assignment. After this immediate
Patrick Williams213cb262021-08-07 19:21:33 -0500667assignment, ``A:foo`` is set to "Z", and then further appended with "X"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500668leaving the variable set to "ZX". Finally, applying the override for
669"foo" results in the conditional variable ``A`` becoming "ZX" (i.e.
Patrick Williams213cb262021-08-07 19:21:33 -0500670``A`` is replaced with ``A:foo``).
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500671
Andrew Geisslerc926e172021-05-07 16:11:35 -0500672This final example mixes in some varying operators::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500673
674 A = "1"
Patrick Williams213cb262021-08-07 19:21:33 -0500675 A:append = "2"
676 A:append = "3"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500677 A += "4"
678 A .= "5"
679
680For this case, the type of append
681operators are affecting the order of assignments as BitBake passes
682through the code multiple times. Initially, ``A`` is set to "1 45"
683because of the three statements that use immediate operators. After
Patrick Williams213cb262021-08-07 19:21:33 -0500684these assignments are made, BitBake applies the ":append" operations.
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500685Those operations result in ``A`` becoming "1 4523".
686
687Sharing Functionality
688=====================
689
690BitBake allows for metadata sharing through include files (``.inc``) and
691class files (``.bbclass``). For example, suppose you have a piece of
692common functionality such as a task definition that you want to share
693between more than one recipe. In this case, creating a ``.bbclass`` file
694that contains the common functionality and then using the ``inherit``
695directive in your recipes to inherit the class would be a common way to
696share the task.
697
698This section presents the mechanisms BitBake provides to allow you to
699share functionality between recipes. Specifically, the mechanisms
Patrick Williams213cb262021-08-07 19:21:33 -0500700include ``include``, ``inherit``, :term:`INHERIT`, and ``require``
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500701directives.
702
703Locating Include and Class Files
704--------------------------------
705
706BitBake uses the :term:`BBPATH` variable to locate
707needed include and class files. Additionally, BitBake searches the
708current directory for ``include`` and ``require`` directives.
709
710.. note::
711
712 The BBPATH variable is analogous to the environment variable PATH .
713
714In order for include and class files to be found by BitBake, they need
715to be located in a "classes" subdirectory that can be found in
Patrick Williams213cb262021-08-07 19:21:33 -0500716:term:`BBPATH`.
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500717
718``inherit`` Directive
719---------------------
720
721When writing a recipe or class file, you can use the ``inherit``
722directive to inherit the functionality of a class (``.bbclass``).
723BitBake only supports this directive when used within recipe and class
724files (i.e. ``.bb`` and ``.bbclass``).
725
726The ``inherit`` directive is a rudimentary means of specifying
727functionality contained in class files that your recipes require. For
728example, you can easily abstract out the tasks involved in building a
729package that uses Autoconf and Automake and put those tasks into a class
730file and then have your recipe inherit that class file.
731
732As an example, your recipes could use the following directive to inherit
733an ``autotools.bbclass`` file. The class file would contain common
Andrew Geisslerc926e172021-05-07 16:11:35 -0500734functionality for using Autotools that could be shared across recipes::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500735
736 inherit autotools
737
738In this case, BitBake would search for the directory
Patrick Williams213cb262021-08-07 19:21:33 -0500739``classes/autotools.bbclass`` in :term:`BBPATH`.
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500740
741.. note::
742
743 You can override any values and functions of the inherited class
744 within your recipe by doing so after the "inherit" statement.
745
746If you want to use the directive to inherit multiple classes, separate
747them with spaces. The following example shows how to inherit both the
Andrew Geisslerc926e172021-05-07 16:11:35 -0500748``buildhistory`` and ``rm_work`` classes::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500749
750 inherit buildhistory rm_work
751
752An advantage with the inherit directive as compared to both the
753:ref:`include <bitbake-user-manual/bitbake-user-manual-metadata:\`\`include\`\` directive>` and :ref:`require <bitbake-user-manual/bitbake-user-manual-metadata:\`\`require\`\` directive>`
754directives is that you can inherit class files conditionally. You can
755accomplish this by using a variable expression after the ``inherit``
Andrew Geisslerc926e172021-05-07 16:11:35 -0500756statement. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500757
758 inherit ${VARNAME}
759
760If ``VARNAME`` is
761going to be set, it needs to be set before the ``inherit`` statement is
762parsed. One way to achieve a conditional inherit in this case is to use
Andrew Geisslerc926e172021-05-07 16:11:35 -0500763overrides::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500764
765 VARIABLE = ""
Patrick Williams213cb262021-08-07 19:21:33 -0500766 VARIABLE:someoverride = "myclass"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500767
Andrew Geisslerc926e172021-05-07 16:11:35 -0500768Another method is by using anonymous Python. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500769
770 python () {
771 if condition == value:
772 d.setVar('VARIABLE', 'myclass')
773 else:
774 d.setVar('VARIABLE', '')
775 }
776
777Alternatively, you could use an in-line Python expression in the
Andrew Geisslerc926e172021-05-07 16:11:35 -0500778following form::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500779
780 inherit ${@'classname' if condition else ''}
781 inherit ${@functionname(params)}
782
783In all cases, if the expression evaluates to an
784empty string, the statement does not trigger a syntax error because it
785becomes a no-op.
786
787``include`` Directive
788---------------------
789
790BitBake understands the ``include`` directive. This directive causes
791BitBake to parse whatever file you specify, and to insert that file at
792that location. The directive is much like its equivalent in Make except
793that if the path specified on the include line is a relative path,
Patrick Williams213cb262021-08-07 19:21:33 -0500794BitBake locates the first file it can find within :term:`BBPATH`.
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500795
796The include directive is a more generic method of including
797functionality as compared to the :ref:`inherit <bitbake-user-manual/bitbake-user-manual-metadata:\`\`inherit\`\` directive>`
798directive, which is restricted to class (i.e. ``.bbclass``) files. The
799include directive is applicable for any other kind of shared or
800encapsulated functionality or configuration that does not suit a
801``.bbclass`` file.
802
803As an example, suppose you needed a recipe to include some self-test
Andrew Geisslerc926e172021-05-07 16:11:35 -0500804definitions::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500805
806 include test_defs.inc
807
808.. note::
809
810 The include directive does not produce an error when the file cannot be
811 found. Consequently, it is recommended that if the file you are including is
812 expected to exist, you should use :ref:`require <require-inclusion>` instead
813 of include . Doing so makes sure that an error is produced if the file cannot
814 be found.
815
816.. _require-inclusion:
817
818``require`` Directive
819---------------------
820
821BitBake understands the ``require`` directive. This directive behaves
822just like the ``include`` directive with the exception that BitBake
823raises a parsing error if the file to be included cannot be found. Thus,
824any file you require is inserted into the file that is being parsed at
825the location of the directive.
826
827The require directive, like the include directive previously described,
828is a more generic method of including functionality as compared to the
829:ref:`inherit <bitbake-user-manual/bitbake-user-manual-metadata:\`\`inherit\`\` directive>` directive, which is restricted to class
830(i.e. ``.bbclass``) files. The require directive is applicable for any
831other kind of shared or encapsulated functionality or configuration that
832does not suit a ``.bbclass`` file.
833
834Similar to how BitBake handles :ref:`include <bitbake-user-manual/bitbake-user-manual-metadata:\`\`include\`\` directive>`, if
835the path specified on the require line is a relative path, BitBake
Patrick Williams213cb262021-08-07 19:21:33 -0500836locates the first file it can find within :term:`BBPATH`.
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500837
838As an example, suppose you have two versions of a recipe (e.g.
839``foo_1.2.2.bb`` and ``foo_2.0.0.bb``) where each version contains some
840identical functionality that could be shared. You could create an
841include file named ``foo.inc`` that contains the common definitions
842needed to build "foo". You need to be sure ``foo.inc`` is located in the
843same directory as your two recipe files as well. Once these conditions
844are set up, you can share the functionality using a ``require``
Andrew Geisslerc926e172021-05-07 16:11:35 -0500845directive from within each recipe::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500846
847 require foo.inc
848
849``INHERIT`` Configuration Directive
850-----------------------------------
851
852When creating a configuration file (``.conf``), you can use the
853:term:`INHERIT` configuration directive to inherit a
854class. BitBake only supports this directive when used within a
855configuration file.
856
857As an example, suppose you needed to inherit a class file called
Andrew Geisslerc926e172021-05-07 16:11:35 -0500858``abc.bbclass`` from a configuration file as follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500859
860 INHERIT += "abc"
861
862This configuration directive causes the named class to be inherited at
863the point of the directive during parsing. As with the ``inherit``
864directive, the ``.bbclass`` file must be located in a "classes"
Patrick Williams213cb262021-08-07 19:21:33 -0500865subdirectory in one of the directories specified in :term:`BBPATH`.
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500866
867.. note::
868
869 Because .conf files are parsed first during BitBake's execution, using
870 INHERIT to inherit a class effectively inherits the class globally (i.e. for
871 all recipes).
872
873If you want to use the directive to inherit multiple classes, you can
874provide them on the same line in the ``local.conf`` file. Use spaces to
875separate the classes. The following example shows how to inherit both
Andrew Geisslerc926e172021-05-07 16:11:35 -0500876the ``autotools`` and ``pkgconfig`` classes::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500877
878 INHERIT += "autotools pkgconfig"
879
880Functions
881=========
882
883As with most languages, functions are the building blocks that are used
884to build up operations into tasks. BitBake supports these types of
885functions:
886
887- *Shell Functions:* Functions written in shell script and executed
888 either directly as functions, tasks, or both. They can also be called
889 by other shell functions.
890
891- *BitBake-Style Python Functions:* Functions written in Python and
892 executed by BitBake or other Python functions using
893 ``bb.build.exec_func()``.
894
895- *Python Functions:* Functions written in Python and executed by
896 Python.
897
898- *Anonymous Python Functions:* Python functions executed automatically
899 during parsing.
900
901Regardless of the type of function, you can only define them in class
902(``.bbclass``) and recipe (``.bb`` or ``.inc``) files.
903
904Shell Functions
905---------------
906
Andrew Geissler595f6302022-01-24 19:11:47 +0000907Functions written in shell script are executed either directly as
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500908functions, tasks, or both. They can also be called by other shell
Andrew Geisslerc926e172021-05-07 16:11:35 -0500909functions. Here is an example shell function definition::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500910
911 some_function () {
912 echo "Hello World"
913 }
914
915When you create these types of functions in
916your recipe or class files, you need to follow the shell programming
917rules. The scripts are executed by ``/bin/sh``, which may not be a bash
918shell but might be something such as ``dash``. You should not use
919Bash-specific script (bashisms).
920
Patrick Williams213cb262021-08-07 19:21:33 -0500921Overrides and override-style operators like ``:append`` and ``:prepend``
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500922can also be applied to shell functions. Most commonly, this application
923would be used in a ``.bbappend`` file to modify functions in the main
924recipe. It can also be used to modify functions inherited from classes.
925
Andrew Geisslerc926e172021-05-07 16:11:35 -0500926As an example, consider the following::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500927
928 do_foo() {
929 bbplain first
930 fn
931 }
932
Patrick Williams213cb262021-08-07 19:21:33 -0500933 fn:prepend() {
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500934 bbplain second
935 }
936
937 fn() {
938 bbplain third
939 }
940
Patrick Williams213cb262021-08-07 19:21:33 -0500941 do_foo:append() {
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500942 bbplain fourth
943 }
944
Andrew Geisslerc926e172021-05-07 16:11:35 -0500945Running ``do_foo`` prints the following::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500946
947 recipename do_foo: first
948 recipename do_foo: second
949 recipename do_foo: third
950 recipename do_foo: fourth
951
952.. note::
953
954 Overrides and override-style operators can be applied to any shell
955 function, not just :ref:`tasks <bitbake-user-manual/bitbake-user-manual-metadata:tasks>`.
956
Andrew Geissler595f6302022-01-24 19:11:47 +0000957You can use the ``bitbake -e recipename`` command to view the final
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500958assembled function after all overrides have been applied.
959
960BitBake-Style Python Functions
961------------------------------
962
963These functions are written in Python and executed by BitBake or other
964Python functions using ``bb.build.exec_func()``.
965
Andrew Geisslerc926e172021-05-07 16:11:35 -0500966An example BitBake function is::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500967
968 python some_python_function () {
969 d.setVar("TEXT", "Hello World")
970 print d.getVar("TEXT")
971 }
972
973Because the
974Python "bb" and "os" modules are already imported, you do not need to
975import these modules. Also in these types of functions, the datastore
976("d") is a global variable and is always automatically available.
977
978.. note::
979
980 Variable expressions (e.g. ``${X}`` ) are no longer expanded within Python
981 functions. This behavior is intentional in order to allow you to freely set
982 variable values to expandable expressions without having them expanded
983 prematurely. If you do wish to expand a variable within a Python function,
984 use ``d.getVar("X")`` . Or, for more complicated expressions, use ``d.expand()``.
985
986Similar to shell functions, you can also apply overrides and
987override-style operators to BitBake-style Python functions.
988
Andrew Geisslerc926e172021-05-07 16:11:35 -0500989As an example, consider the following::
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500990
Patrick Williams213cb262021-08-07 19:21:33 -0500991 python do_foo:prepend() {
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500992 bb.plain("first")
993 }
994
995 python do_foo() {
996 bb.plain("second")
997 }
998
Patrick Williams213cb262021-08-07 19:21:33 -0500999 python do_foo:append() {
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001000 bb.plain("third")
1001 }
1002
Andrew Geisslerc926e172021-05-07 16:11:35 -05001003Running ``do_foo`` prints the following::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001004
1005 recipename do_foo: first
1006 recipename do_foo: second
1007 recipename do_foo: third
1008
Andrew Geissler595f6302022-01-24 19:11:47 +00001009You can use the ``bitbake -e recipename`` command to view
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001010the final assembled function after all overrides have been applied.
1011
1012Python Functions
1013----------------
1014
1015These functions are written in Python and are executed by other Python
1016code. Examples of Python functions are utility functions that you intend
1017to call from in-line Python or from within other Python functions. Here
Andrew Geisslerc926e172021-05-07 16:11:35 -05001018is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001019
1020 def get_depends(d):
1021 if d.getVar('SOMECONDITION'):
1022 return "dependencywithcond"
1023 else:
1024 return "dependency"
1025
1026 SOMECONDITION = "1"
1027 DEPENDS = "${@get_depends(d)}"
1028
Patrick Williams213cb262021-08-07 19:21:33 -05001029This would result in :term:`DEPENDS` containing ``dependencywithcond``.
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001030
1031Here are some things to know about Python functions:
1032
1033- Python functions can take parameters.
1034
1035- The BitBake datastore is not automatically available. Consequently,
1036 you must pass it in as a parameter to the function.
1037
1038- The "bb" and "os" Python modules are automatically available. You do
1039 not need to import them.
1040
1041BitBake-Style Python Functions Versus Python Functions
1042------------------------------------------------------
1043
1044Following are some important differences between BitBake-style Python
1045functions and regular Python functions defined with "def":
1046
1047- Only BitBake-style Python functions can be :ref:`tasks <bitbake-user-manual/bitbake-user-manual-metadata:tasks>`.
1048
1049- Overrides and override-style operators can only be applied to
1050 BitBake-style Python functions.
1051
1052- Only regular Python functions can take arguments and return values.
1053
1054- :ref:`Variable flags <bitbake-user-manual/bitbake-user-manual-metadata:variable flags>` such as
1055 ``[dirs]``, ``[cleandirs]``, and ``[lockfiles]`` can be used on BitBake-style
1056 Python functions, but not on regular Python functions.
1057
1058- BitBake-style Python functions generate a separate
1059 ``${``\ :term:`T`\ ``}/run.``\ function-name\ ``.``\ pid
1060 script that is executed to run the function, and also generate a log
1061 file in ``${T}/log.``\ function-name\ ``.``\ pid if they are executed
1062 as tasks.
1063
1064 Regular Python functions execute "inline" and do not generate any
1065 files in ``${T}``.
1066
1067- Regular Python functions are called with the usual Python syntax.
1068 BitBake-style Python functions are usually tasks and are called
1069 directly by BitBake, but can also be called manually from Python code
Andrew Geisslerc926e172021-05-07 16:11:35 -05001070 by using the ``bb.build.exec_func()`` function. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001071
1072 bb.build.exec_func("my_bitbake_style_function", d)
1073
1074 .. note::
1075
1076 ``bb.build.exec_func()`` can also be used to run shell functions from Python
1077 code. If you want to run a shell function before a Python function within
1078 the same task, then you can use a parent helper Python function that
1079 starts by running the shell function with ``bb.build.exec_func()`` and then
1080 runs the Python code.
1081
1082 To detect errors from functions executed with
1083 ``bb.build.exec_func()``, you can catch the ``bb.build.FuncFailed``
1084 exception.
1085
1086 .. note::
1087
1088 Functions in metadata (recipes and classes) should not themselves raise
1089 ``bb.build.FuncFailed``. Rather, ``bb.build.FuncFailed`` should be viewed as a
1090 general indicator that the called function failed by raising an
1091 exception. For example, an exception raised by ``bb.fatal()`` will be caught
1092 inside ``bb.build.exec_func()``, and a ``bb.build.FuncFailed`` will be raised in
1093 response.
1094
1095Due to their simplicity, you should prefer regular Python functions over
1096BitBake-style Python functions unless you need a feature specific to
1097BitBake-style Python functions. Regular Python functions in metadata are
1098a more recent invention than BitBake-style Python functions, and older
1099code tends to use ``bb.build.exec_func()`` more often.
1100
1101Anonymous Python Functions
1102--------------------------
1103
1104Sometimes it is useful to set variables or perform other operations
1105programmatically during parsing. To do this, you can define special
1106Python functions, called anonymous Python functions, that run at the end
1107of parsing. For example, the following conditionally sets a variable
Andrew Geisslerc926e172021-05-07 16:11:35 -05001108based on the value of another variable::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001109
1110 python () {
1111 if d.getVar('SOMEVAR') == 'value':
1112 d.setVar('ANOTHERVAR', 'value2')
1113 }
1114
1115An equivalent way to mark a function as an anonymous function is to give it
1116the name "__anonymous", rather than no name.
1117
1118Anonymous Python functions always run at the end of parsing, regardless
1119of where they are defined. If a recipe contains many anonymous
1120functions, they run in the same order as they are defined within the
Andrew Geisslerc926e172021-05-07 16:11:35 -05001121recipe. As an example, consider the following snippet::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001122
1123 python () {
1124 d.setVar('FOO', 'foo 2')
1125 }
1126
1127 FOO = "foo 1"
1128
1129 python () {
1130 d.appendVar('BAR',' bar 2')
1131 }
1132
1133 BAR = "bar 1"
1134
1135The previous example is conceptually
Andrew Geisslerc926e172021-05-07 16:11:35 -05001136equivalent to the following snippet::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001137
1138 FOO = "foo 1"
1139 BAR = "bar 1"
1140 FOO = "foo 2"
1141 BAR += "bar 2"
1142
1143``FOO`` ends up with the value "foo 2", and
1144``BAR`` with the value "bar 1 bar 2". Just as in the second snippet, the
1145values set for the variables within the anonymous functions become
1146available to tasks, which always run after parsing.
1147
Patrick Williams213cb262021-08-07 19:21:33 -05001148Overrides and override-style operators such as "``:append``" are applied
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001149before anonymous functions run. In the following example, ``FOO`` ends
Andrew Geisslerc926e172021-05-07 16:11:35 -05001150up with the value "foo from anonymous"::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001151
1152 FOO = "foo"
Patrick Williams213cb262021-08-07 19:21:33 -05001153 FOO:append = " from outside"
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001154
1155 python () {
1156 d.setVar("FOO", "foo from anonymous")
1157 }
1158
1159For methods
1160you can use with anonymous Python functions, see the
1161":ref:`bitbake-user-manual/bitbake-user-manual-metadata:functions you can call from within python`"
1162section. For a different method to run Python code during parsing, see
1163the ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:inline python variable expansion`" section.
1164
1165Flexible Inheritance for Class Functions
1166----------------------------------------
1167
1168Through coding techniques and the use of ``EXPORT_FUNCTIONS``, BitBake
1169supports exporting a function from a class such that the class function
1170appears as the default implementation of the function, but can still be
1171called if a recipe inheriting the class needs to define its own version
1172of the function.
1173
1174To understand the benefits of this feature, consider the basic scenario
1175where a class defines a task function and your recipe inherits the
1176class. In this basic scenario, your recipe inherits the task function as
1177defined in the class. If desired, your recipe can add to the start and
Patrick Williams213cb262021-08-07 19:21:33 -05001178end of the function by using the ":prepend" or ":append" operations
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001179respectively, or it can redefine the function completely. However, if it
1180redefines the function, there is no means for it to call the class
1181version of the function. ``EXPORT_FUNCTIONS`` provides a mechanism that
1182enables the recipe's version of the function to call the original
1183version of the function.
1184
1185To make use of this technique, you need the following things in place:
1186
Andrew Geisslerc926e172021-05-07 16:11:35 -05001187- The class needs to define the function as follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001188
1189 classname_functionname
1190
1191 For example, if you have a class file
1192 ``bar.bbclass`` and a function named ``do_foo``, the class must
Andrew Geisslerc926e172021-05-07 16:11:35 -05001193 define the function as follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001194
1195 bar_do_foo
1196
1197- The class needs to contain the ``EXPORT_FUNCTIONS`` statement as
Andrew Geisslerc926e172021-05-07 16:11:35 -05001198 follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001199
1200 EXPORT_FUNCTIONS functionname
1201
1202 For example, continuing with
1203 the same example, the statement in the ``bar.bbclass`` would be as
Andrew Geisslerc926e172021-05-07 16:11:35 -05001204 follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001205
1206 EXPORT_FUNCTIONS do_foo
1207
1208- You need to call the function appropriately from within your recipe.
1209 Continuing with the same example, if your recipe needs to call the
1210 class version of the function, it should call ``bar_do_foo``.
1211 Assuming ``do_foo`` was a shell function and ``EXPORT_FUNCTIONS`` was
1212 used as above, the recipe's function could conditionally call the
Andrew Geisslerc926e172021-05-07 16:11:35 -05001213 class version of the function as follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001214
1215 do_foo() {
1216 if [ somecondition ] ; then
1217 bar_do_foo
1218 else
1219 # Do something else
1220 fi
1221 }
1222
1223 To call your modified version of the function as defined in your recipe,
1224 call it as ``do_foo``.
1225
1226With these conditions met, your single recipe can freely choose between
1227the original function as defined in the class file and the modified
1228function in your recipe. If you do not set up these conditions, you are
1229limited to using one function or the other.
1230
1231Tasks
1232=====
1233
1234Tasks are BitBake execution units that make up the steps that BitBake
1235can run for a given recipe. Tasks are only supported in recipes and
1236classes (i.e. in ``.bb`` files and files included or inherited from
1237``.bb`` files). By convention, tasks have names that start with "do\_".
1238
1239Promoting a Function to a Task
1240------------------------------
1241
1242Tasks are either :ref:`shell functions <bitbake-user-manual/bitbake-user-manual-metadata:shell functions>` or
1243:ref:`BitBake-style Python functions <bitbake-user-manual/bitbake-user-manual-metadata:bitbake-style python functions>`
1244that have been promoted to tasks by using the ``addtask`` command. The
1245``addtask`` command can also optionally describe dependencies between
1246the task and other tasks. Here is an example that shows how to define a
Andrew Geisslerc926e172021-05-07 16:11:35 -05001247task and declare some dependencies::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001248
1249 python do_printdate () {
1250 import time
1251 print time.strftime('%Y%m%d', time.gmtime())
1252 }
1253 addtask printdate after do_fetch before do_build
1254
1255The first argument to ``addtask`` is the name
1256of the function to promote to a task. If the name does not start with
1257"do\_", "do\_" is implicitly added, which enforces the convention that all
1258task names start with "do\_".
1259
1260In the previous example, the ``do_printdate`` task becomes a dependency
1261of the ``do_build`` task, which is the default task (i.e. the task run
1262by the ``bitbake`` command unless another task is specified explicitly).
1263Additionally, the ``do_printdate`` task becomes dependent upon the
1264``do_fetch`` task. Running the ``do_build`` task results in the
1265``do_printdate`` task running first.
1266
1267.. note::
1268
1269 If you try out the previous example, you might see that the
1270 ``do_printdate``
1271 task is only run the first time you build the recipe with the
1272 ``bitbake``
1273 command. This is because BitBake considers the task "up-to-date"
1274 after that initial run. If you want to force the task to always be
1275 rerun for experimentation purposes, you can make BitBake always
1276 consider the task "out-of-date" by using the
1277 :ref:`[nostamp] <bitbake-user-manual/bitbake-user-manual-metadata:Variable Flags>`
Andrew Geisslerc926e172021-05-07 16:11:35 -05001278 variable flag, as follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001279
1280 do_printdate[nostamp] = "1"
1281
1282 You can also explicitly run the task and provide the
Andrew Geisslerc926e172021-05-07 16:11:35 -05001283 -f option as follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001284
1285 $ bitbake recipe -c printdate -f
1286
1287 When manually selecting a task to run with the bitbake ``recipe
1288 -c task`` command, you can omit the "do\_" prefix as part of the task
1289 name.
1290
1291You might wonder about the practical effects of using ``addtask``
Andrew Geisslerc926e172021-05-07 16:11:35 -05001292without specifying any dependencies as is done in the following example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001293
1294 addtask printdate
1295
1296In this example, assuming dependencies have not been
1297added through some other means, the only way to run the task is by
1298explicitly selecting it with ``bitbake`` recipe ``-c printdate``. You
1299can use the ``do_listtasks`` task to list all tasks defined in a recipe
Andrew Geisslerc926e172021-05-07 16:11:35 -05001300as shown in the following example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001301
1302 $ bitbake recipe -c listtasks
1303
1304For more information on task dependencies, see the
1305":ref:`bitbake-user-manual/bitbake-user-manual-execution:dependencies`" section.
1306
1307See the ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:variable flags`" section for information
1308on variable flags you can use with tasks.
1309
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001310.. note::
1311
1312 While it's infrequent, it's possible to define multiple tasks as
1313 dependencies when calling ``addtask``. For example, here's a snippet
1314 from the OpenEmbedded class file ``package_tar.bbclass``::
1315
1316 addtask package_write_tar before do_build after do_packagedata do_package
1317
1318 Note how the ``package_write_tar`` task has to wait until both of
1319 ``do_packagedata`` and ``do_package`` complete.
1320
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001321Deleting a Task
1322---------------
1323
1324As well as being able to add tasks, you can delete them. Simply use the
1325``deltask`` command to delete a task. For example, to delete the example
Andrew Geisslerc926e172021-05-07 16:11:35 -05001326task used in the previous sections, you would use::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001327
1328 deltask printdate
1329
1330If you delete a task using the ``deltask`` command and the task has
1331dependencies, the dependencies are not reconnected. For example, suppose
1332you have three tasks named ``do_a``, ``do_b``, and ``do_c``.
1333Furthermore, ``do_c`` is dependent on ``do_b``, which in turn is
1334dependent on ``do_a``. Given this scenario, if you use ``deltask`` to
1335delete ``do_b``, the implicit dependency relationship between ``do_c``
1336and ``do_a`` through ``do_b`` no longer exists, and ``do_c``
1337dependencies are not updated to include ``do_a``. Thus, ``do_c`` is free
1338to run before ``do_a``.
1339
1340If you want dependencies such as these to remain intact, use the
1341``[noexec]`` varflag to disable the task instead of using the
Andrew Geisslerc926e172021-05-07 16:11:35 -05001342``deltask`` command to delete it::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001343
1344 do_b[noexec] = "1"
1345
1346Passing Information Into the Build Task Environment
1347---------------------------------------------------
1348
1349When running a task, BitBake tightly controls the shell execution
1350environment of the build tasks to make sure unwanted contamination from
1351the build machine cannot influence the build.
1352
1353.. note::
1354
1355 By default, BitBake cleans the environment to include only those
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001356 things exported or listed in its passthrough list to ensure that the
1357 build environment is reproducible and consistent. You can prevent this
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001358 "cleaning" by setting the :term:`BB_PRESERVE_ENV` variable.
1359
1360Consequently, if you do want something to get passed into the build task
1361environment, you must take these two steps:
1362
1363#. Tell BitBake to load what you want from the environment into the
1364 datastore. You can do so through the
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001365 :term:`BB_ENV_PASSTHROUGH` and
1366 :term:`BB_ENV_PASSTHROUGH_ADDITIONS` variables. For
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001367 example, assume you want to prevent the build system from accessing
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001368 your ``$HOME/.ccache`` directory. The following command adds the
1369 the environment variable ``CCACHE_DIR`` to BitBake's passthrough
1370 list to allow that variable into the datastore::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001371
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001372 export BB_ENV_PASSTHROUGH_ADDITIONS="$BB_ENV_PASSTHROUGH_ADDITIONS CCACHE_DIR"
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001373
1374#. Tell BitBake to export what you have loaded into the datastore to the
1375 task environment of every running task. Loading something from the
1376 environment into the datastore (previous step) only makes it
1377 available in the datastore. To export it to the task environment of
1378 every running task, use a command similar to the following in your
1379 local configuration file ``local.conf`` or your distribution
Andrew Geisslerc926e172021-05-07 16:11:35 -05001380 configuration file::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001381
1382 export CCACHE_DIR
1383
1384 .. note::
1385
1386 A side effect of the previous steps is that BitBake records the
1387 variable as a dependency of the build process in things like the
1388 setscene checksums. If doing so results in unnecessary rebuilds of
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001389 tasks, you can also flag the variable so that the setscene code
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001390 ignores the dependency when it creates checksums.
1391
1392Sometimes, it is useful to be able to obtain information from the
1393original execution environment. BitBake saves a copy of the original
1394environment into a special variable named :term:`BB_ORIGENV`.
1395
Patrick Williams213cb262021-08-07 19:21:33 -05001396The :term:`BB_ORIGENV` variable returns a datastore object that can be
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001397queried using the standard datastore operators such as
1398``getVar(, False)``. The datastore object is useful, for example, to
Andrew Geisslerc926e172021-05-07 16:11:35 -05001399find the original ``DISPLAY`` variable. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001400
1401 origenv = d.getVar("BB_ORIGENV", False)
1402 bar = origenv.getVar("BAR", False)
1403
1404The previous example returns ``BAR`` from the original execution
1405environment.
1406
1407Variable Flags
1408==============
1409
1410Variable flags (varflags) help control a task's functionality and
1411dependencies. BitBake reads and writes varflags to the datastore using
Andrew Geisslerc926e172021-05-07 16:11:35 -05001412the following command forms::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001413
1414 variable = d.getVarFlags("variable")
1415 self.d.setVarFlags("FOO", {"func": True})
1416
1417When working with varflags, the same syntax, with the exception of
1418overrides, applies. In other words, you can set, append, and prepend
1419varflags just like variables. See the
1420":ref:`bitbake-user-manual/bitbake-user-manual-metadata:variable flag syntax`" section for details.
1421
1422BitBake has a defined set of varflags available for recipes and classes.
1423Tasks support a number of these flags which control various
1424functionality of the task:
1425
1426- ``[cleandirs]``: Empty directories that should be created before
1427 the task runs. Directories that already exist are removed and
1428 recreated to empty them.
1429
1430- ``[depends]``: Controls inter-task dependencies. See the
1431 :term:`DEPENDS` variable and the
1432 ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:inter-task
1433 dependencies`" section for more information.
1434
1435- ``[deptask]``: Controls task build-time dependencies. See the
1436 :term:`DEPENDS` variable and the ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:build dependencies`" section for more information.
1437
1438- ``[dirs]``: Directories that should be created before the task
1439 runs. Directories that already exist are left as is. The last
1440 directory listed is used as the current working directory for the
1441 task.
1442
1443- ``[lockfiles]``: Specifies one or more lockfiles to lock while the
1444 task executes. Only one task may hold a lockfile, and any task that
1445 attempts to lock an already locked file will block until the lock is
1446 released. You can use this variable flag to accomplish mutual
1447 exclusion.
1448
1449- ``[noexec]``: When set to "1", marks the task as being empty, with
1450 no execution required. You can use the ``[noexec]`` flag to set up
1451 tasks as dependency placeholders, or to disable tasks defined
1452 elsewhere that are not needed in a particular recipe.
1453
1454- ``[nostamp]``: When set to "1", tells BitBake to not generate a
1455 stamp file for a task, which implies the task should always be
1456 executed.
1457
1458 .. caution::
1459
1460 Any task that depends (possibly indirectly) on a ``[nostamp]`` task will
1461 always be executed as well. This can cause unnecessary rebuilding if you
1462 are not careful.
1463
1464- ``[number_threads]``: Limits tasks to a specific number of
1465 simultaneous threads during execution. This varflag is useful when
1466 your build host has a large number of cores but certain tasks need to
1467 be rate-limited due to various kinds of resource constraints (e.g. to
1468 avoid network throttling). ``number_threads`` works similarly to the
1469 :term:`BB_NUMBER_THREADS` variable but is task-specific.
1470
1471 Set the value globally. For example, the following makes sure the
1472 ``do_fetch`` task uses no more than two simultaneous execution
1473 threads: do_fetch[number_threads] = "2"
1474
1475 .. warning::
1476
1477 - Setting the varflag in individual recipes rather than globally
1478 can result in unpredictable behavior.
1479
1480 - Setting the varflag to a value greater than the value used in
Patrick Williams213cb262021-08-07 19:21:33 -05001481 the :term:`BB_NUMBER_THREADS` variable causes ``number_threads`` to
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001482 have no effect.
1483
1484- ``[postfuncs]``: List of functions to call after the completion of
1485 the task.
1486
1487- ``[prefuncs]``: List of functions to call before the task executes.
1488
1489- ``[rdepends]``: Controls inter-task runtime dependencies. See the
1490 :term:`RDEPENDS` variable, the
1491 :term:`RRECOMMENDS` variable, and the
1492 ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:inter-task dependencies`" section for
1493 more information.
1494
1495- ``[rdeptask]``: Controls task runtime dependencies. See the
1496 :term:`RDEPENDS` variable, the
1497 :term:`RRECOMMENDS` variable, and the
1498 ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:runtime dependencies`" section for more
1499 information.
1500
1501- ``[recideptask]``: When set in conjunction with ``recrdeptask``,
1502 specifies a task that should be inspected for additional
1503 dependencies.
1504
1505- ``[recrdeptask]``: Controls task recursive runtime dependencies.
1506 See the :term:`RDEPENDS` variable, the
1507 :term:`RRECOMMENDS` variable, and the
1508 ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:recursive dependencies`" section for
1509 more information.
1510
1511- ``[stamp-extra-info]``: Extra stamp information to append to the
1512 task's stamp. As an example, OpenEmbedded uses this flag to allow
1513 machine-specific tasks.
1514
1515- ``[umask]``: The umask to run the task under.
1516
1517Several varflags are useful for controlling how signatures are
1518calculated for variables. For more information on this process, see the
1519":ref:`bitbake-user-manual/bitbake-user-manual-execution:checksums (signatures)`" section.
1520
1521- ``[vardeps]``: Specifies a space-separated list of additional
1522 variables to add to a variable's dependencies for the purposes of
1523 calculating its signature. Adding variables to this list is useful,
1524 for example, when a function refers to a variable in a manner that
1525 does not allow BitBake to automatically determine that the variable
1526 is referred to.
1527
1528- ``[vardepsexclude]``: Specifies a space-separated list of variables
1529 that should be excluded from a variable's dependencies for the
1530 purposes of calculating its signature.
1531
1532- ``[vardepvalue]``: If set, instructs BitBake to ignore the actual
1533 value of the variable and instead use the specified value when
1534 calculating the variable's signature.
1535
1536- ``[vardepvalueexclude]``: Specifies a pipe-separated list of
1537 strings to exclude from the variable's value when calculating the
1538 variable's signature.
1539
1540Events
1541======
1542
1543BitBake allows installation of event handlers within recipe and class
1544files. Events are triggered at certain points during operation, such as
1545the beginning of operation against a given recipe (i.e. ``*.bb``), the
1546start of a given task, a task failure, a task success, and so forth. The
1547intent is to make it easy to do things like email notification on build
1548failures.
1549
1550Following is an example event handler that prints the name of the event
Patrick Williams213cb262021-08-07 19:21:33 -05001551and the content of the :term:`FILE` variable::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001552
1553 addhandler myclass_eventhandler
1554 python myclass_eventhandler() {
1555 from bb.event import getName
1556 print("The name of the Event is %s" % getName(e))
1557 print("The file we run for is %s" % d.getVar('FILE'))
1558 }
1559 myclass_eventhandler[eventmask] = "bb.event.BuildStarted
1560 bb.event.BuildCompleted"
1561
1562In the previous example, an eventmask has been
1563set so that the handler only sees the "BuildStarted" and
1564"BuildCompleted" events. This event handler gets called every time an
1565event matching the eventmask is triggered. A global variable "e" is
1566defined, which represents the current event. With the ``getName(e)``
1567method, you can get the name of the triggered event. The global
1568datastore is available as "d". In legacy code, you might see "e.data"
1569used to get the datastore. However, realize that "e.data" is deprecated
1570and you should use "d" going forward.
1571
1572The context of the datastore is appropriate to the event in question.
1573For example, "BuildStarted" and "BuildCompleted" events run before any
1574tasks are executed so would be in the global configuration datastore
1575namespace. No recipe-specific metadata exists in that namespace. The
1576"BuildStarted" and "BuildCompleted" events also run in the main
1577cooker/server process rather than any worker context. Thus, any changes
1578made to the datastore would be seen by other cooker/server events within
1579the current build but not seen outside of that build or in any worker
1580context. Task events run in the actual tasks in question consequently
1581have recipe-specific and task-specific contents. These events run in the
1582worker context and are discarded at the end of task execution.
1583
1584During a standard build, the following common events might occur. The
1585following events are the most common kinds of events that most metadata
1586might have an interest in viewing:
1587
1588- ``bb.event.ConfigParsed()``: Fired when the base configuration; which
1589 consists of ``bitbake.conf``, ``base.bbclass`` and any global
Patrick Williams213cb262021-08-07 19:21:33 -05001590 :term:`INHERIT` statements; has been parsed. You can see multiple such
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001591 events when each of the workers parse the base configuration or if
1592 the server changes configuration and reparses. Any given datastore
1593 only has one such event executed against it, however. If
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001594 :term:`BB_INVALIDCONF` is set in the datastore by the event
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001595 handler, the configuration is reparsed and a new event triggered,
1596 allowing the metadata to update configuration.
1597
1598- ``bb.event.HeartbeatEvent()``: Fires at regular time intervals of one
1599 second. You can configure the interval time using the
1600 ``BB_HEARTBEAT_EVENT`` variable. The event's "time" attribute is the
1601 ``time.time()`` value when the event is triggered. This event is
1602 useful for activities such as system state monitoring.
1603
1604- ``bb.event.ParseStarted()``: Fired when BitBake is about to start
1605 parsing recipes. This event's "total" attribute represents the number
1606 of recipes BitBake plans to parse.
1607
1608- ``bb.event.ParseProgress()``: Fired as parsing progresses. This
1609 event's "current" attribute is the number of recipes parsed as well
1610 as the "total" attribute.
1611
1612- ``bb.event.ParseCompleted()``: Fired when parsing is complete. This
1613 event's "cached", "parsed", "skipped", "virtuals", "masked", and
1614 "errors" attributes provide statistics for the parsing results.
1615
1616- ``bb.event.BuildStarted()``: Fired when a new build starts. BitBake
1617 fires multiple "BuildStarted" events (one per configuration) when
1618 multiple configuration (multiconfig) is enabled.
1619
1620- ``bb.build.TaskStarted()``: Fired when a task starts. This event's
1621 "taskfile" attribute points to the recipe from which the task
1622 originates. The "taskname" attribute, which is the task's name,
1623 includes the ``do_`` prefix, and the "logfile" attribute point to
1624 where the task's output is stored. Finally, the "time" attribute is
1625 the task's execution start time.
1626
1627- ``bb.build.TaskInvalid()``: Fired if BitBake tries to execute a task
1628 that does not exist.
1629
1630- ``bb.build.TaskFailedSilent()``: Fired for setscene tasks that fail
1631 and should not be presented to the user verbosely.
1632
1633- ``bb.build.TaskFailed()``: Fired for normal tasks that fail.
1634
1635- ``bb.build.TaskSucceeded()``: Fired when a task successfully
1636 completes.
1637
1638- ``bb.event.BuildCompleted()``: Fired when a build finishes.
1639
1640- ``bb.cooker.CookerExit()``: Fired when the BitBake server/cooker
1641 shuts down. This event is usually only seen by the UIs as a sign they
1642 should also shutdown.
1643
1644This next list of example events occur based on specific requests to the
1645server. These events are often used to communicate larger pieces of
1646information from the BitBake server to other parts of BitBake such as
1647user interfaces:
1648
1649- ``bb.event.TreeDataPreparationStarted()``
1650- ``bb.event.TreeDataPreparationProgress()``
1651- ``bb.event.TreeDataPreparationCompleted()``
1652- ``bb.event.DepTreeGenerated()``
1653- ``bb.event.CoreBaseFilesFound()``
1654- ``bb.event.ConfigFilePathFound()``
1655- ``bb.event.FilesMatchingFound()``
1656- ``bb.event.ConfigFilesFound()``
1657- ``bb.event.TargetsTreeGenerated()``
1658
1659.. _variants-class-extension-mechanism:
1660
Andrew Geissler9aee5002022-03-30 16:27:02 +00001661Variants --- Class Extension Mechanism
1662======================================
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001663
Andrew Geissler09036742021-06-25 14:25:14 -05001664BitBake supports multiple incarnations of a recipe file via the
1665:term:`BBCLASSEXTEND` variable.
1666
1667The :term:`BBCLASSEXTEND` variable is a space separated list of classes used
1668to "extend" the recipe for each variant. Here is an example that results in a
1669second incarnation of the current recipe being available. This second
1670incarnation will have the "native" class inherited. ::
1671
1672 BBCLASSEXTEND = "native"
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001673
1674.. note::
1675
1676 The mechanism for this class extension is extremely specific to the
1677 implementation. Usually, the recipe's :term:`PROVIDES` , :term:`PN` , and
1678 :term:`DEPENDS` variables would need to be modified by the extension
1679 class. For specific examples, see the OE-Core native , nativesdk , and
1680 multilib classes.
1681
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001682Dependencies
1683============
1684
1685To allow for efficient parallel processing, BitBake handles dependencies
1686at the task level. Dependencies can exist both between tasks within a
1687single recipe and between tasks in different recipes. Following are
1688examples of each:
1689
1690- For tasks within a single recipe, a recipe's ``do_configure`` task
1691 might need to complete before its ``do_compile`` task can run.
1692
1693- For tasks in different recipes, one recipe's ``do_configure`` task
1694 might require another recipe's ``do_populate_sysroot`` task to finish
1695 first such that the libraries and headers provided by the other
1696 recipe are available.
1697
1698This section describes several ways to declare dependencies. Remember,
1699even though dependencies are declared in different ways, they are all
1700simply dependencies between tasks.
1701
1702.. _dependencies-internal-to-the-bb-file:
1703
1704Dependencies Internal to the ``.bb`` File
1705-----------------------------------------
1706
1707BitBake uses the ``addtask`` directive to manage dependencies that are
1708internal to a given recipe file. You can use the ``addtask`` directive
1709to indicate when a task is dependent on other tasks or when other tasks
Andrew Geisslerc926e172021-05-07 16:11:35 -05001710depend on that recipe. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001711
1712 addtask printdate after do_fetch before do_build
1713
1714In this example, the ``do_printdate`` task
1715depends on the completion of the ``do_fetch`` task, and the ``do_build``
1716task depends on the completion of the ``do_printdate`` task.
1717
1718.. note::
1719
1720 For a task to run, it must be a direct or indirect dependency of some
1721 other task that is scheduled to run.
1722
1723 For illustration, here are some examples:
1724
1725 - The directive ``addtask mytask before do_configure`` causes
1726 ``do_mytask`` to run before ``do_configure`` runs. Be aware that
1727 ``do_mytask`` still only runs if its :ref:`input
1728 checksum <bitbake-user-manual/bitbake-user-manual-execution:checksums (signatures)>` has changed since the last time it was
1729 run. Changes to the input checksum of ``do_mytask`` also
1730 indirectly cause ``do_configure`` to run.
1731
1732 - The directive ``addtask mytask after do_configure`` by itself
1733 never causes ``do_mytask`` to run. ``do_mytask`` can still be run
Andrew Geisslerc926e172021-05-07 16:11:35 -05001734 manually as follows::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001735
1736 $ bitbake recipe -c mytask
1737
1738 Declaring ``do_mytask`` as a dependency of some other task that is
1739 scheduled to run also causes it to run. Regardless, the task runs after
1740 ``do_configure``.
1741
1742Build Dependencies
1743------------------
1744
1745BitBake uses the :term:`DEPENDS` variable to manage
1746build time dependencies. The ``[deptask]`` varflag for tasks signifies
Patrick Williams213cb262021-08-07 19:21:33 -05001747the task of each item listed in :term:`DEPENDS` that must complete before
Andrew Geisslerc926e172021-05-07 16:11:35 -05001748that task can be executed. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001749
1750 do_configure[deptask] = "do_populate_sysroot"
1751
1752In this example, the ``do_populate_sysroot`` task
Patrick Williams213cb262021-08-07 19:21:33 -05001753of each item in :term:`DEPENDS` must complete before ``do_configure`` can
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001754execute.
1755
1756Runtime Dependencies
1757--------------------
1758
1759BitBake uses the :term:`PACKAGES`, :term:`RDEPENDS`, and :term:`RRECOMMENDS`
1760variables to manage runtime dependencies.
1761
Patrick Williams213cb262021-08-07 19:21:33 -05001762The :term:`PACKAGES` variable lists runtime packages. Each of those packages
1763can have :term:`RDEPENDS` and :term:`RRECOMMENDS` runtime dependencies. The
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001764``[rdeptask]`` flag for tasks is used to signify the task of each item
1765runtime dependency which must have completed before that task can be
1766executed. ::
1767
1768 do_package_qa[rdeptask] = "do_packagedata"
1769
1770In the previous
Patrick Williams213cb262021-08-07 19:21:33 -05001771example, the ``do_packagedata`` task of each item in :term:`RDEPENDS` must
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001772have completed before ``do_package_qa`` can execute.
Patrick Williams213cb262021-08-07 19:21:33 -05001773Although :term:`RDEPENDS` contains entries from the
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001774runtime dependency namespace, BitBake knows how to map them back
1775to the build-time dependency namespace, in which the tasks are defined.
1776
1777Recursive Dependencies
1778----------------------
1779
1780BitBake uses the ``[recrdeptask]`` flag to manage recursive task
1781dependencies. BitBake looks through the build-time and runtime
1782dependencies of the current recipe, looks through the task's inter-task
1783dependencies, and then adds dependencies for the listed task. Once
1784BitBake has accomplished this, it recursively works through the
1785dependencies of those tasks. Iterative passes continue until all
1786dependencies are discovered and added.
1787
1788The ``[recrdeptask]`` flag is most commonly used in high-level recipes
1789that need to wait for some task to finish "globally". For example,
Andrew Geisslerc926e172021-05-07 16:11:35 -05001790``image.bbclass`` has the following::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001791
1792 do_rootfs[recrdeptask] += "do_packagedata"
1793
1794This statement says that the ``do_packagedata`` task of
1795the current recipe and all recipes reachable (by way of dependencies)
1796from the image recipe must run before the ``do_rootfs`` task can run.
1797
1798BitBake allows a task to recursively depend on itself by
Andrew Geisslerc926e172021-05-07 16:11:35 -05001799referencing itself in the task list::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001800
1801 do_a[recrdeptask] = "do_a do_b"
1802
1803In the same way as before, this means that the ``do_a``
1804and ``do_b`` tasks of the current recipe and all
1805recipes reachable (by way of dependencies) from the recipe
1806must run before the ``do_a`` task can run. In this
1807case BitBake will ignore the current recipe's ``do_a``
1808task circular dependency on itself.
1809
1810Inter-Task Dependencies
1811-----------------------
1812
1813BitBake uses the ``[depends]`` flag in a more generic form to manage
1814inter-task dependencies. This more generic form allows for
1815inter-dependency checks for specific tasks rather than checks for the
Patrick Williams213cb262021-08-07 19:21:33 -05001816data in :term:`DEPENDS`. Here is an example::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001817
1818 do_patch[depends] = "quilt-native:do_populate_sysroot"
1819
1820In this example, the ``do_populate_sysroot`` task of the target ``quilt-native``
1821must have completed before the ``do_patch`` task can execute.
1822
1823The ``[rdepends]`` flag works in a similar way but takes targets in the
1824runtime namespace instead of the build-time dependency namespace.
1825
1826Functions You Can Call From Within Python
1827=========================================
1828
1829BitBake provides many functions you can call from within Python
1830functions. This section lists the most commonly used functions, and
1831mentions where to find others.
1832
1833Functions for Accessing Datastore Variables
1834-------------------------------------------
1835
1836It is often necessary to access variables in the BitBake datastore using
1837Python functions. The BitBake datastore has an API that allows you this
1838access. Here is a list of available operations:
1839
1840.. list-table::
1841 :widths: auto
1842 :header-rows: 1
1843
1844 * - *Operation*
1845 - *Description*
1846 * - ``d.getVar("X", expand)``
1847 - Returns the value of variable "X". Using "expand=True" expands the
1848 value. Returns "None" if the variable "X" does not exist.
1849 * - ``d.setVar("X", "value")``
1850 - Sets the variable "X" to "value"
1851 * - ``d.appendVar("X", "value")``
1852 - Adds "value" to the end of the variable "X". Acts like ``d.setVar("X",
1853 "value")`` if the variable "X" does not exist.
1854 * - ``d.prependVar("X", "value")``
1855 - Adds "value" to the start of the variable "X". Acts like
1856 ``d.setVar("X","value")`` if the variable "X" does not exist.
1857 * - ``d.delVar("X")``
1858 - Deletes the variable "X" from the datastore. Does nothing if the variable
1859 "X" does not exist.
1860 * - ``d.renameVar("X", "Y")``
1861 - Renames the variable "X" to "Y". Does nothing if the variable "X" does
1862 not exist.
1863 * - ``d.getVarFlag("X", flag, expand)``
1864 - Returns the value of variable "X". Using "expand=True" expands the
1865 value. Returns "None" if either the variable "X" or the named flag does
1866 not exist.
1867 * - ``d.setVarFlag("X", flag, "value")``
1868 - Sets the named flag for variable "X" to "value".
1869 * - ``d.appendVarFlag("X", flag, "value")``
1870 - Appends "value" to the named flag on the variable "X". Acts like
1871 ``d.setVarFlag("X", flag, "value")`` if the named flag does not exist.
1872 * - ``d.prependVarFlag("X", flag, "value")``
1873 - Prepends "value" to the named flag on the variable "X". Acts like
1874 ``d.setVarFlag("X", flag, "value")`` if the named flag does not exist.
1875 * - ``d.delVarFlag("X", flag)``
1876 - Deletes the named flag on the variable "X" from the datastore.
1877 * - ``d.setVarFlags("X", flagsdict)``
1878 - Sets the flags specified in the ``flagsdict()``
1879 parameter. ``setVarFlags`` does not clear previous flags. Think of this
1880 operation as ``addVarFlags``.
1881 * - ``d.getVarFlags("X")``
1882 - Returns a ``flagsdict`` of the flags for the variable "X". Returns "None"
1883 if the variable "X" does not exist.
1884 * - ``d.delVarFlags("X")``
1885 - Deletes all the flags for the variable "X". Does nothing if the variable
1886 "X" does not exist.
1887 * - ``d.expand(expression)``
1888 - Expands variable references in the specified string
1889 expression. References to variables that do not exist are left as is. For
1890 example, ``d.expand("foo ${X}")`` expands to the literal string "foo
1891 ${X}" if the variable "X" does not exist.
1892
1893Other Functions
1894---------------
1895
1896You can find many other functions that can be called from Python by
1897looking at the source code of the ``bb`` module, which is in
1898``bitbake/lib/bb``. For example, ``bitbake/lib/bb/utils.py`` includes
1899the commonly used functions ``bb.utils.contains()`` and
1900``bb.utils.mkdirhier()``, which come with docstrings.
1901
1902Task Checksums and Setscene
1903===========================
1904
1905BitBake uses checksums (or signatures) along with the setscene to
1906determine if a task needs to be run. This section describes the process.
1907To help understand how BitBake does this, the section assumes an
1908OpenEmbedded metadata-based example.
1909
1910These checksums are stored in :term:`STAMP`. You can
Andrew Geisslerc926e172021-05-07 16:11:35 -05001911examine the checksums using the following BitBake command::
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001912
1913 $ bitbake-dumpsigs
1914
1915This command returns the signature data in a readable
1916format that allows you to examine the inputs used when the OpenEmbedded
1917build system generates signatures. For example, using
1918``bitbake-dumpsigs`` allows you to examine the ``do_compile`` task's
Andrew Geisslerf0343792020-11-18 10:42:21 -06001919"sigdata" for a C application (e.g. ``bash``). Running the command also
1920reveals that the "CC" variable is part of the inputs that are hashed.
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001921Any changes to this variable would invalidate the stamp and cause the
1922``do_compile`` task to run.
1923
1924The following list describes related variables:
1925
1926- :term:`BB_HASHCHECK_FUNCTION`:
1927 Specifies the name of the function to call during the "setscene" part
1928 of the task's execution in order to validate the list of task hashes.
1929
1930- :term:`BB_SETSCENE_DEPVALID`:
1931 Specifies a function BitBake calls that determines whether BitBake
1932 requires a setscene dependency to be met.
1933
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001934- :term:`BB_TASKHASH`: Within an executing task,
1935 this variable holds the hash of the task as returned by the currently
1936 enabled signature generator.
1937
1938- :term:`STAMP`: The base path to create stamp files.
1939
1940- :term:`STAMPCLEAN`: Again, the base path to
1941 create stamp files but can use wildcards for matching a range of
1942 files for clean operations.
1943
1944Wildcard Support in Variables
1945=============================
1946
1947Support for wildcard use in variables varies depending on the context in
Andrew Geissler5199d832021-09-24 16:47:35 -05001948which it is used. For example, some variables and filenames allow
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001949limited use of wildcards through the "``%``" and "``*``" characters.
1950Other variables or names support Python's
1951`glob <https://docs.python.org/3/library/glob.html>`_ syntax,
1952`fnmatch <https://docs.python.org/3/library/fnmatch.html#module-fnmatch>`_
1953syntax, or
1954`Regular Expression (re) <https://docs.python.org/3/library/re.html>`_
1955syntax.
1956
1957For variables that have wildcard suport, the documentation describes
1958which form of wildcard, its use, and its limitations.