1.. 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 29trailing spaces as part of an assignment, the spaces are retained:: 30 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 43double quote character:: 44 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 80- For configuration changes, use the following:: 81 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 94- To find changes to a given variable in a specific recipe, use the 95 following:: 96 97 $ bitbake recipename -e | grep VARIABLENAME=\" 98 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 107a backslash character ("\\") with the following line before parsing 108statements. The most common use for the "\\" character is to split 109variable assignments over multiple lines, as in the following example:: 110 111 FOO = "bar \ 112 baz \ 113 qaz" 114 115Both the "\\" character and the newline 116character 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 120"barbaz" to ``FOO``:: 121 122 FOO = "barbaz" 123 FOO = "bar\ 124 baz" 125 126.. note:: 127 128 BitBake does not interpret escape sequences like "\\n" in variable 129 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 153behavior:: 154 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" 162 *At this point, ${A} equals "norf baz"* 163 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 181if the variable has a value. Here is an example:: 182 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 198The weak default value of a variable is the value which that variable 199will expand to if no value has been assigned to it via any of the other 200assignment operators. The "??=" operator takes effect immediately, replacing 201any previously defined weak default value. Here is an example:: 202 203 W ??= "x" 204 A := "${W}" # Immediate variable expansion 205 W ??= "y" 206 B := "${W}" # Immediate variable expansion 207 W ??= "z" 208 C = "${W}" 209 W ?= "i" 210 211After parsing we will have:: 212 213 A = "x" 214 B = "y" 215 C = "i" 216 W = "i" 217 218Appending and prepending non-override style will not substitute the weak 219default value, which means that after parsing:: 220 221 W ??= "x" 222 W += "y" 223 224we will have:: 225 226 W = " y" 227 228On the other hand, override-style appends/prepends/removes are applied after 229any active weak default value has been substituted:: 230 231 W ??= "x" 232 W:append = "y" 233 234After parsing we will have:: 235 236 W = "xy" 237 238Immediate variable expansion (:=) 239--------------------------------- 240 241The ":=" operator results in a variable's contents being expanded 242immediately, rather than when the variable is actually used:: 243 244 T = "123" 245 A := "test ${T}" 246 T = "456" 247 B := "${T} ${C}" 248 C = "cval" 249 C := "${C}append" 250 251In this example, ``A`` contains "test 123", even though the final value 252of :term:`T` is "456". The variable :term:`B` will end up containing "456 253cvalappend". This is because references to undefined variables are 254preserved as is during (immediate)expansion. This is in contrast to GNU 255Make, where undefined variables expand to nothing. The variable ``C`` 256contains "cvalappend" since ``${C}`` immediately expands to "cval". 257 258.. _appending-and-prepending: 259 260Appending (+=) and prepending (=+) With Spaces 261---------------------------------------------- 262 263Appending and prepending values is common and can be accomplished using 264the "+=" and "=+" operators. These operators insert a space between the 265current value and prepended or appended value. 266 267These operators take immediate effect during parsing. Here are some 268examples:: 269 270 B = "bval" 271 B += "additionaldata" 272 C = "cval" 273 C =+ "test" 274 275The variable :term:`B` contains "bval additionaldata" and ``C`` contains "test 276cval". 277 278.. _appending-and-prepending-without-spaces: 279 280Appending (.=) and Prepending (=.) Without Spaces 281------------------------------------------------- 282 283If you want to append or prepend values without an inserted space, use 284the ".=" and "=." operators. 285 286These operators take immediate effect during parsing. Here are some 287examples:: 288 289 B = "bval" 290 B .= "additionaldata" 291 C = "cval" 292 C =. "test" 293 294The variable :term:`B` contains "bvaladditionaldata" and ``C`` contains 295"testcval". 296 297Appending and Prepending (Override Style Syntax) 298------------------------------------------------ 299 300You can also append and prepend a variable's value using an override 301style syntax. When you use this syntax, no spaces are inserted. 302 303These operators differ from the ":=", ".=", "=.", "+=", and "=+" 304operators in that their effects are applied at variable expansion time 305rather than being immediately applied. Here are some examples:: 306 307 B = "bval" 308 B:append = " additional data" 309 C = "cval" 310 C:prepend = "additional data " 311 D = "dval" 312 D:append = "additional data" 313 314The variable :term:`B` 315becomes "bval additional data" and ``C`` becomes "additional data cval". 316The variable ``D`` becomes "dvaladditional data". 317 318.. note:: 319 320 You must control all spacing when you use the override syntax. 321 322.. note:: 323 324 The overrides are applied in this order, ":append", ":prepend", ":remove". 325 326It is also possible to append and prepend to shell functions and 327BitBake-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`" 328sections for examples. 329 330.. _removing-override-style-syntax: 331 332Removal (Override Style Syntax) 333------------------------------- 334 335You can remove values from lists using the removal override style 336syntax. Specifying a value for removal causes all occurrences of that 337value to be removed from the variable. Unlike ":append" and ":prepend", 338there is no need to add a leading or trailing space to the value. 339 340When you use this syntax, BitBake expects one or more strings. 341Surrounding spaces and spacing are preserved. Here is an example:: 342 343 FOO = "123 456 789 123456 123 456 123 456" 344 FOO:remove = "123" 345 FOO:remove = "456" 346 FOO2 = " abc def ghi abcdef abc def abc def def" 347 FOO2:remove = "\ 348 def \ 349 abc \ 350 ghi \ 351 " 352 353The variable ``FOO`` becomes 354" 789 123456 " and ``FOO2`` becomes " abcdef ". 355 356Like ":append" and ":prepend", ":remove" is applied at variable 357expansion time. 358 359.. note:: 360 361 The overrides are applied in this order, ":append", ":prepend", ":remove". 362 This implies it is not possible to re-append previously removed strings. 363 However, one can undo a ":remove" by using an intermediate variable whose 364 content is passed to the ":remove" so that modifying the intermediate 365 variable equals to keeping the string in:: 366 367 FOOREMOVE = "123 456 789" 368 FOO:remove = "${FOOREMOVE}" 369 ... 370 FOOREMOVE = "123 789" 371 372 This expands to ``FOO:remove = "123 789"``. 373 374.. note:: 375 376 Override application order may not match variable parse history, i.e. 377 the output of ``bitbake -e`` may contain ":remove" before ":append", 378 but the result will be removed string, because ":remove" is handled 379 last. 380 381Override Style Operation Advantages 382----------------------------------- 383 384An advantage of the override style operations ":append", ":prepend", and 385":remove" as compared to the "+=" and "=+" operators is that the 386override style operators provide guaranteed operations. For example, 387consider a class ``foo.bbclass`` that needs to add the value "val" to 388the variable ``FOO``, and a recipe that uses ``foo.bbclass`` as follows:: 389 390 inherit foo 391 FOO = "initial" 392 393If ``foo.bbclass`` uses the "+=" operator, 394as follows, then the final value of ``FOO`` will be "initial", which is 395not what is desired:: 396 397 FOO += "val" 398 399If, on the other hand, ``foo.bbclass`` 400uses the ":append" operator, then the final value of ``FOO`` will be 401"initial val", as intended:: 402 403 FOO:append = " val" 404 405.. note:: 406 407 It is never necessary to use "+=" together with ":append". The following 408 sequence of assignments appends "barbaz" to FOO:: 409 410 FOO:append = "bar" 411 FOO:append = "baz" 412 413 414 The only effect of changing the second assignment in the previous 415 example to use "+=" would be to add a space before "baz" in the 416 appended value (due to how the "+=" operator works). 417 418Another advantage of the override style operations is that you can 419combine them with other overrides as described in the 420":ref:`bitbake-user-manual/bitbake-user-manual-metadata:conditional syntax (overrides)`" section. 421 422Variable Flag Syntax 423-------------------- 424 425Variable flags are BitBake's implementation of variable properties or 426attributes. It is a way of tagging extra information onto a variable. 427You can find more out about variable flags in general in the 428":ref:`bitbake-user-manual/bitbake-user-manual-metadata:variable flags`" section. 429 430You can define, append, and prepend values to variable flags. All the 431standard syntax operations previously mentioned work for variable flags 432except for override style syntax (i.e. ":prepend", ":append", and 433":remove"). 434 435Here are some examples showing how to set variable flags:: 436 437 FOO[a] = "abc" 438 FOO[b] = "123" 439 FOO[a] += "456" 440 441The variable ``FOO`` has two flags: 442``[a]`` and ``[b]``. The flags are immediately set to "abc" and "123", 443respectively. The ``[a]`` flag becomes "abc 456". 444 445No need exists to pre-define variable flags. You can simply start using 446them. One extremely common application is to attach some brief 447documentation to a BitBake variable as follows:: 448 449 CACHE[doc] = "The directory holding the cache of the metadata." 450 451.. note:: 452 453 Variable flag names starting with an underscore (``_``) character 454 are allowed but are ignored by ``d.getVarFlags("VAR")`` 455 in Python code. Such flag names are used internally by BitBake. 456 457Inline Python Variable Expansion 458-------------------------------- 459 460You can use inline Python variable expansion to set variables. Here is 461an example:: 462 463 DATE = "${@time.strftime('%Y%m%d',time.gmtime())}" 464 465This example results in the ``DATE`` variable being set to the current date. 466 467Probably the most common use of this feature is to extract the value of 468variables from BitBake's internal data dictionary, ``d``. The following 469lines select the values of a package name and its version number, 470respectively:: 471 472 PN = "${@bb.parse.vars_from_file(d.getVar('FILE', False),d)[0] or 'defaultpkgname'}" 473 PV = "${@bb.parse.vars_from_file(d.getVar('FILE', False),d)[1] or '1.0'}" 474 475.. note:: 476 477 Inline Python expressions work just like variable expansions insofar as the 478 "=" and ":=" operators are concerned. Given the following assignment, foo() 479 is called each time FOO is expanded:: 480 481 FOO = "${@foo()}" 482 483 Contrast this with the following immediate assignment, where foo() is only 484 called once, while the assignment is parsed:: 485 486 FOO := "${@foo()}" 487 488For a different way to set variables with Python code during parsing, 489see the 490":ref:`bitbake-user-manual/bitbake-user-manual-metadata:anonymous python functions`" section. 491 492Unsetting variables 493------------------- 494 495It is possible to completely remove a variable or a variable flag from 496BitBake's internal data dictionary by using the "unset" keyword. Here is 497an example:: 498 499 unset DATE 500 unset do_fetch[noexec] 501 502These two statements remove the ``DATE`` and the ``do_fetch[noexec]`` flag. 503 504Providing Pathnames 505------------------- 506 507When specifying pathnames for use with BitBake, do not use the tilde 508("~") character as a shortcut for your home directory. Doing so might 509cause BitBake to not recognize the path since BitBake does not expand 510this character in the same way a shell would. 511 512Instead, provide a fuller path as the following example illustrates:: 513 514 BBLAYERS ?= " \ 515 /home/scott-lenovo/LayerA \ 516 " 517 518Exporting Variables to the Environment 519====================================== 520 521You can export variables to the environment of running tasks by using 522the ``export`` keyword. For example, in the following example, the 523``do_foo`` task prints "value from the environment" when run:: 524 525 export ENV_VARIABLE 526 ENV_VARIABLE = "value from the environment" 527 528 do_foo() { 529 bbplain "$ENV_VARIABLE" 530 } 531 532.. note:: 533 534 BitBake does not expand ``$ENV_VARIABLE`` in this case because it lacks the 535 obligatory ``{}`` . Rather, ``$ENV_VARIABLE`` is expanded by the shell. 536 537It does not matter whether ``export ENV_VARIABLE`` appears before or 538after assignments to ``ENV_VARIABLE``. 539 540It is also possible to combine ``export`` with setting a value for the 541variable. Here is an example:: 542 543 export ENV_VARIABLE = "variable-value" 544 545In the output of ``bitbake -e``, variables that are exported to the 546environment are preceded by "export". 547 548Among the variables commonly exported to the environment are ``CC`` and 549``CFLAGS``, which are picked up by many build systems. 550 551Conditional Syntax (Overrides) 552============================== 553 554BitBake uses :term:`OVERRIDES` to control what 555variables are overridden after BitBake parses recipes and configuration 556files. This section describes how you can use :term:`OVERRIDES` as 557conditional metadata, talks about key expansion in relationship to 558:term:`OVERRIDES`, and provides some examples to help with understanding. 559 560Conditional Metadata 561-------------------- 562 563You can use :term:`OVERRIDES` to conditionally select a specific version of 564a variable and to conditionally append or prepend the value of a 565variable. 566 567.. note:: 568 569 Overrides can only use lower-case characters, digits and dashes. 570 In particular, colons are not permitted in override names as they are used to 571 separate overrides from each other and from the variable name. 572 573- *Selecting a Variable:* The :term:`OVERRIDES` variable is a 574 colon-character-separated list that contains items for which you want 575 to satisfy conditions. Thus, if you have a variable that is 576 conditional on "arm", and "arm" is in :term:`OVERRIDES`, then the 577 "arm"-specific version of the variable is used rather than the 578 non-conditional version. Here is an example:: 579 580 OVERRIDES = "architecture:os:machine" 581 TEST = "default" 582 TEST:os = "osspecific" 583 TEST:nooverride = "othercondvalue" 584 585 In this example, the :term:`OVERRIDES` 586 variable lists three overrides: "architecture", "os", and "machine". 587 The variable ``TEST`` by itself has a default value of "default". You 588 select the os-specific version of the ``TEST`` variable by appending 589 the "os" override to the variable (i.e. ``TEST:os``). 590 591 To better understand this, consider a practical example that assumes 592 an OpenEmbedded metadata-based Linux kernel recipe file. The 593 following lines from the recipe file first set the kernel branch 594 variable ``KBRANCH`` to a default value, then conditionally override 595 that value based on the architecture of the build:: 596 597 KBRANCH = "standard/base" 598 KBRANCH:qemuarm = "standard/arm-versatile-926ejs" 599 KBRANCH:qemumips = "standard/mti-malta32" 600 KBRANCH:qemuppc = "standard/qemuppc" 601 KBRANCH:qemux86 = "standard/common-pc/base" 602 KBRANCH:qemux86-64 = "standard/common-pc-64/base" 603 KBRANCH:qemumips64 = "standard/mti-malta64" 604 605- *Appending and Prepending:* BitBake also supports append and prepend 606 operations to variable values based on whether a specific item is 607 listed in :term:`OVERRIDES`. Here is an example:: 608 609 DEPENDS = "glibc ncurses" 610 OVERRIDES = "machine:local" 611 DEPENDS:append:machine = "libmad" 612 613 In this example, :term:`DEPENDS` becomes "glibc ncurses libmad". 614 615 Again, using an OpenEmbedded metadata-based kernel recipe file as an 616 example, the following lines will conditionally append to the 617 ``KERNEL_FEATURES`` variable based on the architecture:: 618 619 KERNEL_FEATURES:append = " ${KERNEL_EXTRA_FEATURES}" 620 KERNEL_FEATURES:append:qemux86=" cfg/sound.scc cfg/paravirt_kvm.scc" 621 KERNEL_FEATURES:append:qemux86-64=" cfg/sound.scc cfg/paravirt_kvm.scc" 622 623- *Setting a Variable for a Single Task:* BitBake supports setting a 624 variable just for the duration of a single task. Here is an example:: 625 626 FOO:task-configure = "val 1" 627 FOO:task-compile = "val 2" 628 629 In the 630 previous example, ``FOO`` has the value "val 1" while the 631 ``do_configure`` task is executed, and the value "val 2" while the 632 ``do_compile`` task is executed. 633 634 Internally, this is implemented by prepending the task (e.g. 635 "task-compile:") to the value of 636 :term:`OVERRIDES` for the local datastore of the 637 ``do_compile`` task. 638 639 You can also use this syntax with other combinations (e.g. 640 "``:prepend``") as shown in the following example:: 641 642 EXTRA_OEMAKE:prepend:task-compile = "${PARALLEL_MAKE} " 643 644.. note:: 645 646 Before BitBake 1.52 (Honister 3.4), the syntax for :term:`OVERRIDES` 647 used ``_`` instead of ``:``, so you will still find a lot of documentation 648 using ``_append``, ``_prepend``, and ``_remove``, for example. 649 650 For details, see the 651 :yocto_docs:`Overrides Syntax Changes </migration-guides/migration-3.4.html#override-syntax-changes>` 652 section in the Yocto Project manual migration notes. 653 654Key Expansion 655------------- 656 657Key expansion happens when the BitBake datastore is finalized. To better 658understand this, consider the following example:: 659 660 A${B} = "X" 661 B = "2" 662 A2 = "Y" 663 664In this case, after all the parsing is complete, BitBake expands 665``${B}`` into "2". This expansion causes ``A2``, which was set to "Y" 666before the expansion, to become "X". 667 668.. _variable-interaction-worked-examples: 669 670Examples 671-------- 672 673Despite the previous explanations that show the different forms of 674variable definitions, it can be hard to work out exactly what happens 675when variable operators, conditional overrides, and unconditional 676overrides are combined. This section presents some common scenarios 677along with explanations for variable interactions that typically confuse 678users. 679 680There is often confusion concerning the order in which overrides and 681various "append" operators take effect. Recall that an append or prepend 682operation using ":append" and ":prepend" does not result in an immediate 683assignment as would "+=", ".=", "=+", or "=.". Consider the following 684example:: 685 686 OVERRIDES = "foo" 687 A = "Z" 688 A:foo:append = "X" 689 690For this case, 691``A`` is unconditionally set to "Z" and "X" is unconditionally and 692immediately appended to the variable ``A:foo``. Because overrides have 693not been applied yet, ``A:foo`` is set to "X" due to the append and 694``A`` simply equals "Z". 695 696Applying overrides, however, changes things. Since "foo" is listed in 697:term:`OVERRIDES`, the conditional variable ``A`` is replaced with the "foo" 698version, which is equal to "X". So effectively, ``A:foo`` replaces 699``A``. 700 701This next example changes the order of the override and the append:: 702 703 OVERRIDES = "foo" 704 A = "Z" 705 A:append:foo = "X" 706 707For this case, before 708overrides are handled, ``A`` is set to "Z" and ``A:append:foo`` is set 709to "X". Once the override for "foo" is applied, however, ``A`` gets 710appended with "X". Consequently, ``A`` becomes "ZX". Notice that spaces 711are not appended. 712 713This next example has the order of the appends and overrides reversed 714back as in the first example:: 715 716 OVERRIDES = "foo" 717 A = "Y" 718 A:foo:append = "Z" 719 A:foo:append = "X" 720 721For this case, before any overrides are resolved, 722``A`` is set to "Y" using an immediate assignment. After this immediate 723assignment, ``A:foo`` is set to "Z", and then further appended with "X" 724leaving the variable set to "ZX". Finally, applying the override for 725"foo" results in the conditional variable ``A`` becoming "ZX" (i.e. 726``A`` is replaced with ``A:foo``). 727 728This final example mixes in some varying operators:: 729 730 A = "1" 731 A:append = "2" 732 A:append = "3" 733 A += "4" 734 A .= "5" 735 736For this case, the type of append 737operators are affecting the order of assignments as BitBake passes 738through the code multiple times. Initially, ``A`` is set to "1 45" 739because of the three statements that use immediate operators. After 740these assignments are made, BitBake applies the ":append" operations. 741Those operations result in ``A`` becoming "1 4523". 742 743Sharing Functionality 744===================== 745 746BitBake allows for metadata sharing through include files (``.inc``) and 747class files (``.bbclass``). For example, suppose you have a piece of 748common functionality such as a task definition that you want to share 749between more than one recipe. In this case, creating a ``.bbclass`` file 750that contains the common functionality and then using the ``inherit`` 751directive in your recipes to inherit the class would be a common way to 752share the task. 753 754This section presents the mechanisms BitBake provides to allow you to 755share functionality between recipes. Specifically, the mechanisms 756include ``include``, ``inherit``, :term:`INHERIT`, and ``require`` 757directives. There is also a higher-level abstraction called 758``configuration fragments`` that is enabled with ``addfragments`` 759directive. 760 761Locating Include and Class Files 762-------------------------------- 763 764BitBake uses the :term:`BBPATH` variable to locate 765needed include and class files. Additionally, BitBake searches the 766current directory for ``include`` and ``require`` directives. 767 768.. note:: 769 770 The BBPATH variable is analogous to the environment variable PATH . 771 772In order for include and class files to be found by BitBake, they need 773to be located in a "classes" subdirectory that can be found in 774:term:`BBPATH`. 775 776.. _ref-bitbake-user-manual-metadata-inherit: 777 778``inherit`` Directive 779--------------------- 780 781When writing a recipe or class file, you can use the ``inherit`` 782directive to inherit the functionality of a class (``.bbclass``). 783BitBake only supports this directive when used within recipe and class 784files (i.e. ``.bb`` and ``.bbclass``). 785 786The ``inherit`` directive is a rudimentary means of specifying 787functionality contained in class files that your recipes require. For 788example, you can easily abstract out the tasks involved in building a 789package that uses Autoconf and Automake and put those tasks into a class 790file and then have your recipe inherit that class file. 791 792As an example, your recipes could use the following directive to inherit 793an ``autotools.bbclass`` file. The class file would contain common 794functionality for using Autotools that could be shared across recipes:: 795 796 inherit autotools 797 798In this case, BitBake would search for the directory 799``classes/autotools.bbclass`` in :term:`BBPATH`. 800 801.. note:: 802 803 You can override any values and functions of the inherited class 804 within your recipe by doing so after the "inherit" statement. 805 806If you want to use the directive to inherit multiple classes, separate 807them with spaces. The following example shows how to inherit both the 808``buildhistory`` and ``rm_work`` classes:: 809 810 inherit buildhistory rm_work 811 812An advantage with the inherit directive as compared to both the 813:ref:`include <bitbake-user-manual/bitbake-user-manual-metadata:\`\`include\`\` directive>` and :ref:`require <bitbake-user-manual/bitbake-user-manual-metadata:\`\`require\`\` directive>` 814directives is that you can inherit class files conditionally. You can 815accomplish this by using a variable expression after the ``inherit`` 816statement. 817 818For inheriting classes conditionally, using the :ref:`inherit_defer 819<ref-bitbake-user-manual-metadata-inherit-defer>` directive is advised as 820:ref:`inherit_defer <ref-bitbake-user-manual-metadata-inherit-defer>` is 821evaluated at the end of parsing. 822 823.. _ref-bitbake-user-manual-metadata-inherit-defer: 824 825``inherit_defer`` Directive 826~~~~~~~~~~~~~~~~~~~~~~~~~~~ 827 828The :ref:`inherit_defer <ref-bitbake-user-manual-metadata-inherit-defer>` 829directive works like the :ref:`inherit 830<ref-bitbake-user-manual-metadata-inherit>` directive, except that it is only 831evaluated at the end of parsing. Its usage is recommended when a conditional 832expression is used. 833 834This allows conditional expressions to be evaluated "late", meaning changes to 835the variable after the line is parsed will take effect. With the :ref:`inherit 836<ref-bitbake-user-manual-metadata-inherit>` directive this is not the case. 837 838Here is an example:: 839 840 inherit_defer ${VARNAME} 841 842If ``VARNAME`` is 843going to be set, it needs to be set before the ``inherit_defer`` statement is 844parsed. One way to achieve a conditional inherit in this case is to use 845overrides:: 846 847 VARIABLE = "" 848 VARIABLE:someoverride = "myclass" 849 850Another method is by using :ref:`anonymous Python 851<bitbake-user-manual/bitbake-user-manual-metadata:Anonymous Python Functions>`. 852Here is an example:: 853 854 python () { 855 if condition == value: 856 d.setVar('VARIABLE', 'myclass') 857 else: 858 d.setVar('VARIABLE', '') 859 } 860 861Alternatively, you could use an inline Python expression in the 862following form:: 863 864 inherit_defer ${@'classname' if condition else ''} 865 866Or:: 867 868 inherit_defer ${@bb.utils.contains('VARIABLE', 'something', 'classname', '', d)} 869 870In all cases, if the expression evaluates to an 871empty string, the statement does not trigger a syntax error because it 872becomes a no-op. 873 874``include`` Directive 875--------------------- 876 877BitBake understands the ``include`` directive. This directive causes 878BitBake to parse whatever file you specify, and to insert that file at 879that location. The directive is much like its equivalent in Make except 880that if the path specified on the include line is a relative path, 881BitBake locates the first file it can find within :term:`BBPATH`. 882 883The include directive is a more generic method of including 884functionality as compared to the :ref:`inherit <bitbake-user-manual/bitbake-user-manual-metadata:\`\`inherit\`\` directive>` 885directive, which is restricted to class (i.e. ``.bbclass``) files. The 886include directive is applicable for any other kind of shared or 887encapsulated functionality or configuration that does not suit a 888``.bbclass`` file. 889 890As an example, suppose you needed a recipe to include some self-test 891definitions:: 892 893 include test_defs.inc 894 895.. note:: 896 897 The include directive does not produce an error when the file cannot be 898 found. Consequently, it is recommended that if the file you are including is 899 expected to exist, you should use :ref:`require <require-inclusion>` instead 900 of include . Doing so makes sure that an error is produced if the file cannot 901 be found. 902 903``include_all`` Directive 904------------------------- 905 906The ``include_all`` directive works like the :ref:`include 907<bitbake-user-manual/bitbake-user-manual-metadata:\`\`include\`\` directive>` 908directive but will include all of the files that match the specified path in 909the enabled layers (layers part of :term:`BBLAYERS`). 910 911For example, let's say a ``maintainers.inc`` file is present in different layers 912and is conventionally placed in the ``conf/distro/include`` directory of each 913layer. In that case the ``include_all`` directive can be used to include 914the ``maintainers.inc`` file for all of these layers:: 915 916 include_all conf/distro/include/maintainers.inc 917 918In other words, the ``maintainers.inc`` file for each layer is included through 919the :ref:`include <bitbake-user-manual/bitbake-user-manual-metadata:\`\`include\`\` directive>` 920directive. 921 922BitBake will iterate through the colon-separated :term:`BBPATH` list to look for 923matching files to include, from left to right. As a consequence, matching files 924are included in that order. 925 926As the ``include_all`` directive uses the :ref:`include 927<bitbake-user-manual/bitbake-user-manual-metadata:\`\`include\`\` directive>` 928directive in the background, no error is produced if no files are matched. 929 930.. _require-inclusion: 931 932``require`` Directive 933--------------------- 934 935BitBake understands the ``require`` directive. This directive behaves 936just like the ``include`` directive with the exception that BitBake 937raises a parsing error if the file to be included cannot be found. Thus, 938any file you require is inserted into the file that is being parsed at 939the location of the directive. 940 941The require directive, like the include directive previously described, 942is a more generic method of including functionality as compared to the 943:ref:`inherit <bitbake-user-manual/bitbake-user-manual-metadata:\`\`inherit\`\` directive>` directive, which is restricted to class 944(i.e. ``.bbclass``) files. The require directive is applicable for any 945other kind of shared or encapsulated functionality or configuration that 946does not suit a ``.bbclass`` file. 947 948Similar to how BitBake handles :ref:`include <bitbake-user-manual/bitbake-user-manual-metadata:\`\`include\`\` directive>`, if 949the path specified on the require line is a relative path, BitBake 950locates the first file it can find within :term:`BBPATH`. 951 952As an example, suppose you have two versions of a recipe (e.g. 953``foo_1.2.2.bb`` and ``foo_2.0.0.bb``) where each version contains some 954identical functionality that could be shared. You could create an 955include file named ``foo.inc`` that contains the common definitions 956needed to build "foo". You need to be sure ``foo.inc`` is located in the 957same directory as your two recipe files as well. Once these conditions 958are set up, you can share the functionality using a ``require`` 959directive from within each recipe:: 960 961 require foo.inc 962 963``INHERIT`` Configuration Directive 964----------------------------------- 965 966When creating a configuration file (``.conf``), you can use the 967:term:`INHERIT` configuration directive to inherit a 968class. BitBake only supports this directive when used within a 969configuration file. 970 971As an example, suppose you needed to inherit a class file called 972``abc.bbclass`` from a configuration file as follows:: 973 974 INHERIT += "abc" 975 976This configuration directive causes the named class to be inherited at 977the point of the directive during parsing. As with the ``inherit`` 978directive, the ``.bbclass`` file must be located in a "classes" 979subdirectory in one of the directories specified in :term:`BBPATH`. 980 981.. note:: 982 983 Because .conf files are parsed first during BitBake's execution, using 984 INHERIT to inherit a class effectively inherits the class globally (i.e. for 985 all recipes). 986 987If you want to use the directive to inherit multiple classes, you can 988provide them on the same line in the ``local.conf`` file. Use spaces to 989separate the classes. The following example shows how to inherit both 990the ``autotools`` and ``pkgconfig`` classes:: 991 992 INHERIT += "autotools pkgconfig" 993 994``addfragments`` Directive 995-------------------------- 996 997This directive allows fine-tuning local configurations with configuration 998snippets contained in layers in a structured, controlled way. Typically it would 999go into ``bitbake.conf``, for example:: 1000 1001 addfragments conf/fragments OE_FRAGMENTS OE_FRAGMENTS_METADATA_VARS 1002 1003``addfragments`` takes three parameters: 1004 1005- path prefix for fragment files inside the layer file tree that bitbake 1006 uses to construct full paths to the fragment files 1007 1008- name of variable that holds the list of enabled fragments in an 1009 active build 1010 1011- name of variable that contains a list of variable names containing 1012 fragment-specific metadata (such as descriptions) 1013 1014This allows listing enabled configuration fragments in ``OE_FRAGMENTS`` 1015variable like this:: 1016 1017 OE_FRAGMENTS = "core/domain/somefragment core/someotherfragment anotherlayer/anotherdomain/anotherfragment" 1018 1019Fragment names listed in this variable must be prefixed by the layer name 1020where a fragment file is located, defined by :term:`BBFILE_COLLECTIONS` in ``layer.conf``. 1021 1022The implementation then expands this list into 1023:ref:`require <bitbake-user-manual/bitbake-user-manual-metadata:\`\`require\`\` directive>` 1024directives with full paths to respective layers:: 1025 1026 require /path/to/core-layer/conf/fragments/domain/somefragment.conf 1027 require /path/to/core-layer/conf/fragments/someotherfragment.conf 1028 require /path/to/another-layer/conf/fragments/anotherdomain/anotherfragment.conf 1029 1030The variable containing a list of fragment metadata variables could look like this:: 1031 1032 OE_FRAGMENTS_METADATA_VARS = "BB_CONF_FRAGMENT_SUMMARY BB_CONF_FRAGMENT_DESCRIPTION" 1033 1034The implementation will add a flag containing the fragment name to each of those variables 1035when parsing fragments, so that the variables are namespaced by fragment name, and do not override 1036each other when several fragments are enabled. 1037 1038Functions 1039========= 1040 1041As with most languages, functions are the building blocks that are used 1042to build up operations into tasks. BitBake supports these types of 1043functions: 1044 1045- *Shell Functions:* Functions written in shell script and executed 1046 either directly as functions, tasks, or both. They can also be called 1047 by other shell functions. 1048 1049- *BitBake-Style Python Functions:* Functions written in Python and 1050 executed by BitBake or other Python functions using 1051 ``bb.build.exec_func()``. 1052 1053- *Python Functions:* Functions written in Python and executed by 1054 Python. 1055 1056- *Anonymous Python Functions:* Python functions executed automatically 1057 during parsing. 1058 1059Regardless of the type of function, you can only define them in class 1060(``.bbclass``) and recipe (``.bb`` or ``.inc``) files. 1061 1062Shell Functions 1063--------------- 1064 1065Functions written in shell script are executed either directly as 1066functions, tasks, or both. They can also be called by other shell 1067functions. Here is an example shell function definition:: 1068 1069 some_function () { 1070 echo "Hello World" 1071 } 1072 1073When you create these types of functions in 1074your recipe or class files, you need to follow the shell programming 1075rules. The scripts are executed by ``/bin/sh``, which may not be a bash 1076shell but might be something such as ``dash``. You should not use 1077Bash-specific script (bashisms). 1078 1079Overrides and override-style operators like ``:append`` and ``:prepend`` 1080can also be applied to shell functions. Most commonly, this application 1081would be used in a ``.bbappend`` file to modify functions in the main 1082recipe. It can also be used to modify functions inherited from classes. 1083 1084As an example, consider the following:: 1085 1086 do_foo() { 1087 bbplain first 1088 fn 1089 } 1090 1091 fn:prepend() { 1092 bbplain second 1093 } 1094 1095 fn() { 1096 bbplain third 1097 } 1098 1099 do_foo:append() { 1100 bbplain fourth 1101 } 1102 1103Running ``do_foo`` prints the following:: 1104 1105 recipename do_foo: first 1106 recipename do_foo: second 1107 recipename do_foo: third 1108 recipename do_foo: fourth 1109 1110.. note:: 1111 1112 Overrides and override-style operators can be applied to any shell 1113 function, not just :ref:`tasks <bitbake-user-manual/bitbake-user-manual-metadata:tasks>`. 1114 1115You can use the ``bitbake -e recipename`` command to view the final 1116assembled function after all overrides have been applied. 1117 1118BitBake-Style Python Functions 1119------------------------------ 1120 1121These functions are written in Python and executed by BitBake or other 1122Python functions using ``bb.build.exec_func()``. 1123 1124An example BitBake function is:: 1125 1126 python some_python_function () { 1127 d.setVar("TEXT", "Hello World") 1128 print d.getVar("TEXT") 1129 } 1130 1131Because the 1132Python "bb" and "os" modules are already imported, you do not need to 1133import these modules. Also in these types of functions, the datastore 1134("d") is a global variable and is always automatically available. 1135 1136.. note:: 1137 1138 Variable expressions (e.g. ``${X}`` ) are no longer expanded within Python 1139 functions. This behavior is intentional in order to allow you to freely set 1140 variable values to expandable expressions without having them expanded 1141 prematurely. If you do wish to expand a variable within a Python function, 1142 use ``d.getVar("X")`` . Or, for more complicated expressions, use ``d.expand()``. 1143 1144Similar to shell functions, you can also apply overrides and 1145override-style operators to BitBake-style Python functions. 1146 1147As an example, consider the following:: 1148 1149 python do_foo:prepend() { 1150 bb.plain("first") 1151 } 1152 1153 python do_foo() { 1154 bb.plain("second") 1155 } 1156 1157 python do_foo:append() { 1158 bb.plain("third") 1159 } 1160 1161Running ``do_foo`` prints the following:: 1162 1163 recipename do_foo: first 1164 recipename do_foo: second 1165 recipename do_foo: third 1166 1167You can use the ``bitbake -e recipename`` command to view 1168the final assembled function after all overrides have been applied. 1169 1170Python Functions 1171---------------- 1172 1173These functions are written in Python and are executed by other Python 1174code. Examples of Python functions are utility functions that you intend 1175to call from in-line Python or from within other Python functions. Here 1176is an example:: 1177 1178 def get_depends(d): 1179 if d.getVar('SOMECONDITION'): 1180 return "dependencywithcond" 1181 else: 1182 return "dependency" 1183 1184 SOMECONDITION = "1" 1185 DEPENDS = "${@get_depends(d)}" 1186 1187This would result in :term:`DEPENDS` containing ``dependencywithcond``. 1188 1189Here are some things to know about Python functions: 1190 1191- Python functions can take parameters. 1192 1193- The BitBake datastore is not automatically available. Consequently, 1194 you must pass it in as a parameter to the function. 1195 1196- The "bb" and "os" Python modules are automatically available. You do 1197 not need to import them. 1198 1199BitBake-Style Python Functions Versus Python Functions 1200------------------------------------------------------ 1201 1202Following are some important differences between BitBake-style Python 1203functions and regular Python functions defined with "def": 1204 1205- Only BitBake-style Python functions can be :ref:`tasks <bitbake-user-manual/bitbake-user-manual-metadata:tasks>`. 1206 1207- Overrides and override-style operators can only be applied to 1208 BitBake-style Python functions. 1209 1210- Only regular Python functions can take arguments and return values. 1211 1212- :ref:`Variable flags <bitbake-user-manual/bitbake-user-manual-metadata:variable flags>` such as 1213 ``[dirs]``, ``[cleandirs]``, and ``[lockfiles]`` can be used on BitBake-style 1214 Python functions, but not on regular Python functions. 1215 1216- BitBake-style Python functions generate a separate 1217 ``${``\ :term:`T`\ ``}/run.``\ function-name\ ``.``\ pid 1218 script that is executed to run the function, and also generate a log 1219 file in ``${T}/log.``\ function-name\ ``.``\ pid if they are executed 1220 as tasks. 1221 1222 Regular Python functions execute "inline" and do not generate any 1223 files in ``${T}``. 1224 1225- Regular Python functions are called with the usual Python syntax. 1226 BitBake-style Python functions are usually tasks and are called 1227 directly by BitBake, but can also be called manually from Python code 1228 by using the ``bb.build.exec_func()`` function. Here is an example:: 1229 1230 bb.build.exec_func("my_bitbake_style_function", d) 1231 1232 .. note:: 1233 1234 ``bb.build.exec_func()`` can also be used to run shell functions from Python 1235 code. If you want to run a shell function before a Python function within 1236 the same task, then you can use a parent helper Python function that 1237 starts by running the shell function with ``bb.build.exec_func()`` and then 1238 runs the Python code. 1239 1240 To detect errors from functions executed with 1241 ``bb.build.exec_func()``, you can catch the ``bb.build.FuncFailed`` 1242 exception. 1243 1244 .. note:: 1245 1246 Functions in metadata (recipes and classes) should not themselves raise 1247 ``bb.build.FuncFailed``. Rather, ``bb.build.FuncFailed`` should be viewed as a 1248 general indicator that the called function failed by raising an 1249 exception. For example, an exception raised by ``bb.fatal()`` will be caught 1250 inside ``bb.build.exec_func()``, and a ``bb.build.FuncFailed`` will be raised in 1251 response. 1252 1253Due to their simplicity, you should prefer regular Python functions over 1254BitBake-style Python functions unless you need a feature specific to 1255BitBake-style Python functions. Regular Python functions in metadata are 1256a more recent invention than BitBake-style Python functions, and older 1257code tends to use ``bb.build.exec_func()`` more often. 1258 1259Anonymous Python Functions 1260-------------------------- 1261 1262Sometimes it is useful to set variables or perform other operations 1263programmatically during parsing. To do this, you can define special 1264Python functions, called anonymous Python functions, that run at the end 1265of parsing. For example, the following conditionally sets a variable 1266based on the value of another variable:: 1267 1268 python () { 1269 if d.getVar('SOMEVAR') == 'value': 1270 d.setVar('ANOTHERVAR', 'value2') 1271 } 1272 1273An equivalent way to mark a function as an anonymous function is to give it 1274the name "__anonymous", rather than no name. 1275 1276Anonymous Python functions always run at the end of parsing, regardless 1277of where they are defined. If a recipe contains many anonymous 1278functions, they run in the same order as they are defined within the 1279recipe. As an example, consider the following snippet:: 1280 1281 python () { 1282 d.setVar('FOO', 'foo 2') 1283 } 1284 1285 FOO = "foo 1" 1286 1287 python () { 1288 d.appendVar('BAR',' bar 2') 1289 } 1290 1291 BAR = "bar 1" 1292 1293The previous example is conceptually 1294equivalent to the following snippet:: 1295 1296 FOO = "foo 1" 1297 BAR = "bar 1" 1298 FOO = "foo 2" 1299 BAR += "bar 2" 1300 1301``FOO`` ends up with the value "foo 2", and 1302``BAR`` with the value "bar 1 bar 2". Just as in the second snippet, the 1303values set for the variables within the anonymous functions become 1304available to tasks, which always run after parsing. 1305 1306Overrides and override-style operators such as "``:append``" are applied 1307before anonymous functions run. In the following example, ``FOO`` ends 1308up with the value "foo from anonymous":: 1309 1310 FOO = "foo" 1311 FOO:append = " from outside" 1312 1313 python () { 1314 d.setVar("FOO", "foo from anonymous") 1315 } 1316 1317For methods 1318you can use with anonymous Python functions, see the 1319":ref:`bitbake-user-manual/bitbake-user-manual-metadata:functions you can call from within python`" 1320section. For a different method to run Python code during parsing, see 1321the ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:inline python variable expansion`" section. 1322 1323Flexible Inheritance for Class Functions 1324---------------------------------------- 1325 1326Through coding techniques and the use of ``EXPORT_FUNCTIONS``, BitBake 1327supports exporting a function from a class such that the class function 1328appears as the default implementation of the function, but can still be 1329called if a recipe inheriting the class needs to define its own version 1330of the function. 1331 1332To understand the benefits of this feature, consider the basic scenario 1333where a class defines a task function and your recipe inherits the 1334class. In this basic scenario, your recipe inherits the task function as 1335defined in the class. If desired, your recipe can add to the start and 1336end of the function by using the ":prepend" or ":append" operations 1337respectively, or it can redefine the function completely. However, if it 1338redefines the function, there is no means for it to call the class 1339version of the function. ``EXPORT_FUNCTIONS`` provides a mechanism that 1340enables the recipe's version of the function to call the original 1341version of the function. 1342 1343To make use of this technique, you need the following things in place: 1344 1345- The class needs to define the function as follows:: 1346 1347 classname_functionname 1348 1349 For example, if you have a class file 1350 ``bar.bbclass`` and a function named ``do_foo``, the class must 1351 define the function as follows:: 1352 1353 bar_do_foo 1354 1355- The class needs to contain the ``EXPORT_FUNCTIONS`` statement as 1356 follows:: 1357 1358 EXPORT_FUNCTIONS functionname 1359 1360 For example, continuing with 1361 the same example, the statement in the ``bar.bbclass`` would be as 1362 follows:: 1363 1364 EXPORT_FUNCTIONS do_foo 1365 1366- You need to call the function appropriately from within your recipe. 1367 Continuing with the same example, if your recipe needs to call the 1368 class version of the function, it should call ``bar_do_foo``. 1369 Assuming ``do_foo`` was a shell function and ``EXPORT_FUNCTIONS`` was 1370 used as above, the recipe's function could conditionally call the 1371 class version of the function as follows:: 1372 1373 do_foo() { 1374 if [ somecondition ] ; then 1375 bar_do_foo 1376 else 1377 # Do something else 1378 fi 1379 } 1380 1381 To call your modified version of the function as defined in your recipe, 1382 call it as ``do_foo``. 1383 1384With these conditions met, your single recipe can freely choose between 1385the original function as defined in the class file and the modified 1386function in your recipe. If you do not set up these conditions, you are 1387limited to using one function or the other. 1388 1389Tasks 1390===== 1391 1392Tasks are BitBake execution units that make up the steps that BitBake 1393can run for a given recipe. Tasks are only supported in recipes and 1394classes (i.e. in ``.bb`` files and files included or inherited from 1395``.bb`` files). By convention, tasks have names that start with "do\_". 1396 1397Promoting a Function to a Task 1398------------------------------ 1399 1400Tasks are either :ref:`shell functions <bitbake-user-manual/bitbake-user-manual-metadata:shell functions>` or 1401:ref:`BitBake-style Python functions <bitbake-user-manual/bitbake-user-manual-metadata:bitbake-style python functions>` 1402that have been promoted to tasks by using the ``addtask`` command. The 1403``addtask`` command can also optionally describe dependencies between 1404the task and other tasks. Here is an example that shows how to define a 1405task and declare some dependencies:: 1406 1407 python do_printdate () { 1408 import datetime 1409 bb.plain('Date: %s' % (datetime.date.today())) 1410 } 1411 addtask printdate after do_fetch before do_build 1412 1413The first argument to ``addtask`` is the name 1414of the function to promote to a task. If the name does not start with 1415"do\_", "do\_" is implicitly added, which enforces the convention that all 1416task names start with "do\_". 1417 1418In the previous example, the ``do_printdate`` task becomes a dependency 1419of the ``do_build`` task, which is the default task (i.e. the task run 1420by the ``bitbake`` command unless another task is specified explicitly). 1421Additionally, the ``do_printdate`` task becomes dependent upon the 1422``do_fetch`` task. Running the ``do_build`` task results in the 1423``do_printdate`` task running first. 1424 1425.. note:: 1426 1427 If you try out the previous example, you might see that the 1428 ``do_printdate`` 1429 task is only run the first time you build the recipe with the 1430 ``bitbake`` 1431 command. This is because BitBake considers the task "up-to-date" 1432 after that initial run. If you want to force the task to always be 1433 rerun for experimentation purposes, you can make BitBake always 1434 consider the task "out-of-date" by using the 1435 :ref:`[nostamp] <bitbake-user-manual/bitbake-user-manual-metadata:Variable Flags>` 1436 variable flag, as follows:: 1437 1438 do_printdate[nostamp] = "1" 1439 1440 You can also explicitly run the task and provide the 1441 -f option as follows:: 1442 1443 $ bitbake recipe -c printdate -f 1444 1445 When manually selecting a task to run with the bitbake ``recipe 1446 -c task`` command, you can omit the "do\_" prefix as part of the task 1447 name. 1448 1449You might wonder about the practical effects of using ``addtask`` 1450without specifying any dependencies as is done in the following example:: 1451 1452 addtask printdate 1453 1454In this example, assuming dependencies have not been 1455added through some other means, the only way to run the task is by 1456explicitly selecting it with ``bitbake`` recipe ``-c printdate``. You 1457can use the ``do_listtasks`` task to list all tasks defined in a recipe 1458as shown in the following example:: 1459 1460 $ bitbake recipe -c listtasks 1461 1462For more information on task dependencies, see the 1463":ref:`bitbake-user-manual/bitbake-user-manual-execution:dependencies`" section. 1464 1465See the ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:variable flags`" section for information 1466on variable flags you can use with tasks. 1467 1468.. note:: 1469 1470 While it's infrequent, it's possible to define multiple tasks as 1471 dependencies when calling ``addtask``. For example, here's a snippet 1472 from the OpenEmbedded class file ``package_tar.bbclass``:: 1473 1474 addtask package_write_tar before do_build after do_packagedata do_package 1475 1476 Note how the ``package_write_tar`` task has to wait until both of 1477 ``do_packagedata`` and ``do_package`` complete. 1478 1479Deleting a Task 1480--------------- 1481 1482As well as being able to add tasks, you can delete them. Simply use the 1483``deltask`` command to delete a task. For example, to delete the example 1484task used in the previous sections, you would use:: 1485 1486 deltask printdate 1487 1488If you delete a task using the ``deltask`` command and the task has 1489dependencies, the dependencies are not reconnected. For example, suppose 1490you have three tasks named ``do_a``, ``do_b``, and ``do_c``. 1491Furthermore, ``do_c`` is dependent on ``do_b``, which in turn is 1492dependent on ``do_a``. Given this scenario, if you use ``deltask`` to 1493delete ``do_b``, the implicit dependency relationship between ``do_c`` 1494and ``do_a`` through ``do_b`` no longer exists, and ``do_c`` 1495dependencies are not updated to include ``do_a``. Thus, ``do_c`` is free 1496to run before ``do_a``. 1497 1498If you want dependencies such as these to remain intact, use the 1499``[noexec]`` varflag to disable the task instead of using the 1500``deltask`` command to delete it:: 1501 1502 do_b[noexec] = "1" 1503 1504Passing Information Into the Build Task Environment 1505--------------------------------------------------- 1506 1507When running a task, BitBake tightly controls the shell execution 1508environment of the build tasks to make sure unwanted contamination from 1509the build machine cannot influence the build. 1510 1511.. note:: 1512 1513 By default, BitBake cleans the environment to include only those 1514 things exported or listed in its passthrough list to ensure that the 1515 build environment is reproducible and consistent. You can prevent this 1516 "cleaning" by setting the :term:`BB_PRESERVE_ENV` variable. 1517 1518Consequently, if you do want something to get passed into the build task 1519environment, you must take these two steps: 1520 1521#. Tell BitBake to load what you want from the environment into the 1522 datastore. You can do so through the 1523 :term:`BB_ENV_PASSTHROUGH` and 1524 :term:`BB_ENV_PASSTHROUGH_ADDITIONS` variables. For 1525 example, assume you want to prevent the build system from accessing 1526 your ``$HOME/.ccache`` directory. The following command adds the 1527 the environment variable ``CCACHE_DIR`` to BitBake's passthrough 1528 list to allow that variable into the datastore:: 1529 1530 export BB_ENV_PASSTHROUGH_ADDITIONS="$BB_ENV_PASSTHROUGH_ADDITIONS CCACHE_DIR" 1531 1532#. Tell BitBake to export what you have loaded into the datastore to the 1533 task environment of every running task. Loading something from the 1534 environment into the datastore (previous step) only makes it 1535 available in the datastore. To export it to the task environment of 1536 every running task, use a command similar to the following in your 1537 local configuration file ``local.conf`` or your distribution 1538 configuration file:: 1539 1540 export CCACHE_DIR 1541 1542 .. note:: 1543 1544 A side effect of the previous steps is that BitBake records the 1545 variable as a dependency of the build process in things like the 1546 setscene checksums. If doing so results in unnecessary rebuilds of 1547 tasks, you can also flag the variable so that the setscene code 1548 ignores the dependency when it creates checksums. 1549 1550Sometimes, it is useful to be able to obtain information from the 1551original execution environment. BitBake saves a copy of the original 1552environment into a special variable named :term:`BB_ORIGENV`. 1553 1554The :term:`BB_ORIGENV` variable returns a datastore object that can be 1555queried using the standard datastore operators such as 1556``getVar(, False)``. The datastore object is useful, for example, to 1557find the original ``DISPLAY`` variable. Here is an example:: 1558 1559 origenv = d.getVar("BB_ORIGENV", False) 1560 bar = origenv.getVar("BAR", False) 1561 1562The previous example returns ``BAR`` from the original execution 1563environment. 1564 1565Variable Flags 1566============== 1567 1568Variable flags (varflags) help control a task's functionality and 1569dependencies. BitBake reads and writes varflags to the datastore using 1570the following command forms:: 1571 1572 variable = d.getVarFlags("variable") 1573 self.d.setVarFlags("FOO", {"func": True}) 1574 1575When working with varflags, the same syntax, with the exception of 1576overrides, applies. In other words, you can set, append, and prepend 1577varflags just like variables. See the 1578":ref:`bitbake-user-manual/bitbake-user-manual-metadata:variable flag syntax`" section for details. 1579 1580BitBake has a defined set of varflags available for recipes and classes. 1581Tasks support a number of these flags which control various 1582functionality of the task: 1583 1584- ``[cleandirs]``: Empty directories that should be created before 1585 the task runs. Directories that already exist are removed and 1586 recreated to empty them. 1587 1588- ``[depends]``: Controls inter-task dependencies. See the 1589 :term:`DEPENDS` variable and the 1590 ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:inter-task 1591 dependencies`" section for more information. 1592 1593- ``[deptask]``: Controls task build-time dependencies. See the 1594 :term:`DEPENDS` variable and the ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:build dependencies`" section for more information. 1595 1596- ``[dirs]``: Directories that should be created before the task 1597 runs. Directories that already exist are left as is. The last 1598 directory listed is used as the current working directory for the 1599 task. 1600 1601- ``[file-checksums]``: Controls the file dependencies for a task. The 1602 baseline file list is the set of files associated with 1603 :term:`SRC_URI`. May be used to set additional dependencies on 1604 files not associated with :term:`SRC_URI`. 1605 1606 The value set to the list is a file-boolean pair where the first 1607 value is the file name and the second is whether or not it 1608 physically exists on the filesystem. :: 1609 1610 do_configure[file-checksums] += "${MY_DIRPATH}/my-file.txt:True" 1611 1612 It is important to record any paths which the task looked at and 1613 which didn't exist. This means that if these do exist at a later 1614 time, the task can be rerun with the new additional files. The 1615 "exists" True or False value after the path allows this to be 1616 handled. 1617 1618- ``[lockfiles]``: Specifies one or more lockfiles to lock while the 1619 task executes. Only one task may hold a lockfile, and any task that 1620 attempts to lock an already locked file will block until the lock is 1621 released. You can use this variable flag to accomplish mutual 1622 exclusion. 1623 1624- ``[network]``: When set to "1", allows a task to access the network. By 1625 default, only the ``do_fetch`` task is granted network access. Recipes 1626 shouldn't access the network outside of ``do_fetch`` as it usually 1627 undermines fetcher source mirroring, image and licence manifests, software 1628 auditing and supply chain security. 1629 1630- ``[noexec]``: When set to "1", marks the task as being empty, with 1631 no execution required. You can use the ``[noexec]`` flag to set up 1632 tasks as dependency placeholders, or to disable tasks defined 1633 elsewhere that are not needed in a particular recipe. 1634 1635- ``[nostamp]``: When set to "1", tells BitBake to not generate a 1636 stamp file for a task, which implies the task should always be 1637 executed. 1638 1639 .. caution:: 1640 1641 Any task that depends (possibly indirectly) on a ``[nostamp]`` task will 1642 always be executed as well. This can cause unnecessary rebuilding if you 1643 are not careful. 1644 1645- ``[number_threads]``: Limits tasks to a specific number of 1646 simultaneous threads during execution. This varflag is useful when 1647 your build host has a large number of cores but certain tasks need to 1648 be rate-limited due to various kinds of resource constraints (e.g. to 1649 avoid network throttling). ``number_threads`` works similarly to the 1650 :term:`BB_NUMBER_THREADS` variable but is task-specific. 1651 1652 Set the value globally. For example, the following makes sure the 1653 ``do_fetch`` task uses no more than two simultaneous execution 1654 threads: do_fetch[number_threads] = "2" 1655 1656 .. warning:: 1657 1658 - Setting the varflag in individual recipes rather than globally 1659 can result in unpredictable behavior. 1660 1661 - Setting the varflag to a value greater than the value used in 1662 the :term:`BB_NUMBER_THREADS` variable causes ``number_threads`` to 1663 have no effect. 1664 1665- ``[postfuncs]``: List of functions to call after the completion of 1666 the task. 1667 1668- ``[prefuncs]``: List of functions to call before the task executes. 1669 1670- ``[rdepends]``: Controls inter-task runtime dependencies. See the 1671 :term:`RDEPENDS` variable, the 1672 :term:`RRECOMMENDS` variable, and the 1673 ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:inter-task dependencies`" section for 1674 more information. 1675 1676- ``[rdeptask]``: Controls task runtime dependencies. See the 1677 :term:`RDEPENDS` variable, the 1678 :term:`RRECOMMENDS` variable, and the 1679 ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:runtime dependencies`" section for more 1680 information. 1681 1682- ``[recideptask]``: When set in conjunction with ``recrdeptask``, 1683 specifies a task that should be inspected for additional 1684 dependencies. 1685 1686- ``[recrdeptask]``: Controls task recursive runtime dependencies. 1687 See the :term:`RDEPENDS` variable, the 1688 :term:`RRECOMMENDS` variable, and the 1689 ":ref:`bitbake-user-manual/bitbake-user-manual-metadata:recursive dependencies`" section for 1690 more information. 1691 1692- ``[stamp-extra-info]``: Extra stamp information to append to the 1693 task's stamp. As an example, OpenEmbedded uses this flag to allow 1694 machine-specific tasks. 1695 1696- ``[umask]``: The umask to run the task under. 1697 1698Several varflags are useful for controlling how signatures are 1699calculated for variables. For more information on this process, see the 1700":ref:`bitbake-user-manual/bitbake-user-manual-execution:checksums (signatures)`" section. 1701 1702- ``[vardeps]``: Specifies a space-separated list of additional 1703 variables to add to a variable's dependencies for the purposes of 1704 calculating its signature. Adding variables to this list is useful, 1705 for example, when a function refers to a variable in a manner that 1706 does not allow BitBake to automatically determine that the variable 1707 is referred to. 1708 1709- ``[vardepsexclude]``: Specifies a space-separated list of variables 1710 that should be excluded from a variable's dependencies for the 1711 purposes of calculating its signature. 1712 1713- ``[vardepvalue]``: If set, instructs BitBake to ignore the actual 1714 value of the variable and instead use the specified value when 1715 calculating the variable's signature. 1716 1717- ``[vardepvalueexclude]``: Specifies a pipe-separated list of 1718 strings to exclude from the variable's value when calculating the 1719 variable's signature. 1720 1721Events 1722====== 1723 1724BitBake allows installation of event handlers within recipe and class 1725files. Events are triggered at certain points during operation, such as 1726the beginning of operation against a given recipe (i.e. ``*.bb``), the 1727start of a given task, a task failure, a task success, and so forth. The 1728intent is to make it easy to do things like email notification on build 1729failures. 1730 1731Following is an example event handler that prints the name of the event 1732and the content of the :term:`FILE` variable:: 1733 1734 addhandler myclass_eventhandler 1735 python myclass_eventhandler() { 1736 from bb.event import getName 1737 print("The name of the Event is %s" % getName(e)) 1738 print("The file we run for is %s" % d.getVar('FILE')) 1739 } 1740 myclass_eventhandler[eventmask] = "bb.event.BuildStarted 1741 bb.event.BuildCompleted" 1742 1743In the previous example, an eventmask has been 1744set so that the handler only sees the "BuildStarted" and 1745"BuildCompleted" events. This event handler gets called every time an 1746event matching the eventmask is triggered. A global variable "e" is 1747defined, which represents the current event. With the ``getName(e)`` 1748method, you can get the name of the triggered event. The global 1749datastore is available as "d". In legacy code, you might see "e.data" 1750used to get the datastore. However, realize that "e.data" is deprecated 1751and you should use "d" going forward. 1752 1753The context of the datastore is appropriate to the event in question. 1754For example, "BuildStarted" and "BuildCompleted" events run before any 1755tasks are executed so would be in the global configuration datastore 1756namespace. No recipe-specific metadata exists in that namespace. The 1757"BuildStarted" and "BuildCompleted" events also run in the main 1758cooker/server process rather than any worker context. Thus, any changes 1759made to the datastore would be seen by other cooker/server events within 1760the current build but not seen outside of that build or in any worker 1761context. Task events run in the actual tasks in question consequently 1762have recipe-specific and task-specific contents. These events run in the 1763worker context and are discarded at the end of task execution. 1764 1765During a standard build, the following common events might occur. The 1766following events are the most common kinds of events that most metadata 1767might have an interest in viewing: 1768 1769- ``bb.event.ConfigParsed()``: Fired when the base configuration; which 1770 consists of ``bitbake.conf``, ``base.bbclass`` and any global 1771 :term:`INHERIT` statements; has been parsed. You can see multiple such 1772 events when each of the workers parse the base configuration or if 1773 the server changes configuration and reparses. Any given datastore 1774 only has one such event executed against it, however. If 1775 :term:`BB_INVALIDCONF` is set in the datastore by the event 1776 handler, the configuration is reparsed and a new event triggered, 1777 allowing the metadata to update configuration. 1778 1779- ``bb.event.HeartbeatEvent()``: Fires at regular time intervals of one 1780 second. You can configure the interval time using the 1781 ``BB_HEARTBEAT_EVENT`` variable. The event's "time" attribute is the 1782 ``time.time()`` value when the event is triggered. This event is 1783 useful for activities such as system state monitoring. 1784 1785- ``bb.event.ParseStarted()``: Fired when BitBake is about to start 1786 parsing recipes. This event's "total" attribute represents the number 1787 of recipes BitBake plans to parse. 1788 1789- ``bb.event.ParseProgress()``: Fired as parsing progresses. This 1790 event's "current" attribute is the number of recipes parsed as well 1791 as the "total" attribute. 1792 1793- ``bb.event.ParseCompleted()``: Fired when parsing is complete. This 1794 event's "cached", "parsed", "skipped", "virtuals", "masked", and 1795 "errors" attributes provide statistics for the parsing results. 1796 1797- ``bb.event.BuildStarted()``: Fired when a new build starts. BitBake 1798 fires multiple "BuildStarted" events (one per configuration) when 1799 multiple configuration (multiconfig) is enabled. 1800 1801- ``bb.build.TaskStarted()``: Fired when a task starts. This event's 1802 "taskfile" attribute points to the recipe from which the task 1803 originates. The "taskname" attribute, which is the task's name, 1804 includes the ``do_`` prefix, and the "logfile" attribute point to 1805 where the task's output is stored. Finally, the "time" attribute is 1806 the task's execution start time. 1807 1808- ``bb.build.TaskInvalid()``: Fired if BitBake tries to execute a task 1809 that does not exist. 1810 1811- ``bb.build.TaskFailedSilent()``: Fired for setscene tasks that fail 1812 and should not be presented to the user verbosely. 1813 1814- ``bb.build.TaskFailed()``: Fired for normal tasks that fail. 1815 1816- ``bb.build.TaskSucceeded()``: Fired when a task successfully 1817 completes. 1818 1819- ``bb.event.BuildCompleted()``: Fired when a build finishes. 1820 1821- ``bb.cooker.CookerExit()``: Fired when the BitBake server/cooker 1822 shuts down. This event is usually only seen by the UIs as a sign they 1823 should also shutdown. 1824 1825This next list of example events occur based on specific requests to the 1826server. These events are often used to communicate larger pieces of 1827information from the BitBake server to other parts of BitBake such as 1828user interfaces: 1829 1830- ``bb.event.TreeDataPreparationStarted()`` 1831- ``bb.event.TreeDataPreparationProgress()`` 1832- ``bb.event.TreeDataPreparationCompleted()`` 1833- ``bb.event.DepTreeGenerated()`` 1834- ``bb.event.CoreBaseFilesFound()`` 1835- ``bb.event.ConfigFilePathFound()`` 1836- ``bb.event.FilesMatchingFound()`` 1837- ``bb.event.ConfigFilesFound()`` 1838- ``bb.event.TargetsTreeGenerated()`` 1839 1840.. _variants-class-extension-mechanism: 1841 1842Variants --- Class Extension Mechanism 1843====================================== 1844 1845BitBake supports multiple incarnations of a recipe file via the 1846:term:`BBCLASSEXTEND` variable. 1847 1848The :term:`BBCLASSEXTEND` variable is a space separated list of classes used 1849to "extend" the recipe for each variant. Here is an example that results in a 1850second incarnation of the current recipe being available. This second 1851incarnation will have the "native" class inherited. :: 1852 1853 BBCLASSEXTEND = "native" 1854 1855.. note:: 1856 1857 The mechanism for this class extension is extremely specific to the 1858 implementation. Usually, the recipe's :term:`PROVIDES` , :term:`PN` , and 1859 :term:`DEPENDS` variables would need to be modified by the extension 1860 class. For specific examples, see the OE-Core native , nativesdk , and 1861 multilib classes. 1862 1863Dependencies 1864============ 1865 1866To allow for efficient parallel processing, BitBake handles dependencies 1867at the task level. Dependencies can exist both between tasks within a 1868single recipe and between tasks in different recipes. Following are 1869examples of each: 1870 1871- For tasks within a single recipe, a recipe's ``do_configure`` task 1872 might need to complete before its ``do_compile`` task can run. 1873 1874- For tasks in different recipes, one recipe's ``do_configure`` task 1875 might require another recipe's ``do_populate_sysroot`` task to finish 1876 first such that the libraries and headers provided by the other 1877 recipe are available. 1878 1879This section describes several ways to declare dependencies. Remember, 1880even though dependencies are declared in different ways, they are all 1881simply dependencies between tasks. 1882 1883.. _dependencies-internal-to-the-bb-file: 1884 1885Dependencies Internal to the ``.bb`` File 1886----------------------------------------- 1887 1888BitBake uses the ``addtask`` directive to manage dependencies that are 1889internal to a given recipe file. You can use the ``addtask`` directive 1890to indicate when a task is dependent on other tasks or when other tasks 1891depend on that recipe. Here is an example:: 1892 1893 addtask printdate after do_fetch before do_build 1894 1895In this example, the ``do_printdate`` task 1896depends on the completion of the ``do_fetch`` task, and the ``do_build`` 1897task depends on the completion of the ``do_printdate`` task. 1898 1899.. note:: 1900 1901 For a task to run, it must be a direct or indirect dependency of some 1902 other task that is scheduled to run. 1903 1904 For illustration, here are some examples: 1905 1906 - The directive ``addtask mytask before do_configure`` causes 1907 ``do_mytask`` to run before ``do_configure`` runs. Be aware that 1908 ``do_mytask`` still only runs if its :ref:`input 1909 checksum <bitbake-user-manual/bitbake-user-manual-execution:checksums (signatures)>` has changed since the last time it was 1910 run. Changes to the input checksum of ``do_mytask`` also 1911 indirectly cause ``do_configure`` to run. 1912 1913 - The directive ``addtask mytask after do_configure`` by itself 1914 never causes ``do_mytask`` to run. ``do_mytask`` can still be run 1915 manually as follows:: 1916 1917 $ bitbake recipe -c mytask 1918 1919 Declaring ``do_mytask`` as a dependency of some other task that is 1920 scheduled to run also causes it to run. Regardless, the task runs after 1921 ``do_configure``. 1922 1923Build Dependencies 1924------------------ 1925 1926BitBake uses the :term:`DEPENDS` variable to manage 1927build time dependencies. The ``[deptask]`` varflag for tasks signifies 1928the task of each item listed in :term:`DEPENDS` that must complete before 1929that task can be executed. Here is an example:: 1930 1931 do_configure[deptask] = "do_populate_sysroot" 1932 1933In this example, the ``do_populate_sysroot`` task 1934of each item in :term:`DEPENDS` must complete before ``do_configure`` can 1935execute. 1936 1937Runtime Dependencies 1938-------------------- 1939 1940BitBake uses the :term:`PACKAGES`, :term:`RDEPENDS`, and :term:`RRECOMMENDS` 1941variables to manage runtime dependencies. 1942 1943The :term:`PACKAGES` variable lists runtime packages. Each of those packages 1944can have :term:`RDEPENDS` and :term:`RRECOMMENDS` runtime dependencies. The 1945``[rdeptask]`` flag for tasks is used to signify the task of each item 1946runtime dependency which must have completed before that task can be 1947executed. :: 1948 1949 do_package_qa[rdeptask] = "do_packagedata" 1950 1951In the previous 1952example, the ``do_packagedata`` task of each item in :term:`RDEPENDS` must 1953have completed before ``do_package_qa`` can execute. 1954Although :term:`RDEPENDS` contains entries from the 1955runtime dependency namespace, BitBake knows how to map them back 1956to the build-time dependency namespace, in which the tasks are defined. 1957 1958Recursive Dependencies 1959---------------------- 1960 1961BitBake uses the ``[recrdeptask]`` flag to manage recursive task 1962dependencies. BitBake looks through the build-time and runtime 1963dependencies of the current recipe, looks through the task's inter-task 1964dependencies, and then adds dependencies for the listed task. Once 1965BitBake has accomplished this, it recursively works through the 1966dependencies of those tasks. Iterative passes continue until all 1967dependencies are discovered and added. 1968 1969The ``[recrdeptask]`` flag is most commonly used in high-level recipes 1970that need to wait for some task to finish "globally". For example, 1971``image.bbclass`` has the following:: 1972 1973 do_rootfs[recrdeptask] += "do_packagedata" 1974 1975This statement says that the ``do_packagedata`` task of 1976the current recipe and all recipes reachable (by way of dependencies) 1977from the image recipe must run before the ``do_rootfs`` task can run. 1978 1979BitBake allows a task to recursively depend on itself by 1980referencing itself in the task list:: 1981 1982 do_a[recrdeptask] = "do_a do_b" 1983 1984In the same way as before, this means that the ``do_a`` 1985and ``do_b`` tasks of the current recipe and all 1986recipes reachable (by way of dependencies) from the recipe 1987must run before the ``do_a`` task can run. In this 1988case BitBake will ignore the current recipe's ``do_a`` 1989task circular dependency on itself. 1990 1991Inter-Task Dependencies 1992----------------------- 1993 1994BitBake uses the ``[depends]`` flag in a more generic form to manage 1995inter-task dependencies. This more generic form allows for 1996inter-dependency checks for specific tasks rather than checks for the 1997data in :term:`DEPENDS`. Here is an example:: 1998 1999 do_patch[depends] = "quilt-native:do_populate_sysroot" 2000 2001In this example, the ``do_populate_sysroot`` task of the target ``quilt-native`` 2002must have completed before the ``do_patch`` task can execute. 2003 2004The ``[rdepends]`` flag works in a similar way but takes targets in the 2005runtime namespace instead of the build-time dependency namespace. 2006 2007Functions You Can Call From Within Python 2008========================================= 2009 2010BitBake provides many functions you can call from within Python 2011functions. This section lists the most commonly used functions, and 2012mentions where to find others. 2013 2014Functions for Accessing Datastore Variables 2015------------------------------------------- 2016 2017It is often necessary to access variables in the BitBake datastore using 2018Python functions. The BitBake datastore has an API that allows you this 2019access. Here is a list of available operations: 2020 2021.. list-table:: 2022 :widths: auto 2023 :header-rows: 1 2024 2025 * - *Operation* 2026 - *Description* 2027 * - ``d.getVar("X", expand)`` 2028 - Returns the value of variable "X". Using "expand=True" expands the 2029 value. Returns "None" if the variable "X" does not exist. 2030 * - ``d.setVar("X", "value")`` 2031 - Sets the variable "X" to "value" 2032 * - ``d.appendVar("X", "value")`` 2033 - Adds "value" to the end of the variable "X". Acts like ``d.setVar("X", 2034 "value")`` if the variable "X" does not exist. 2035 * - ``d.prependVar("X", "value")`` 2036 - Adds "value" to the start of the variable "X". Acts like 2037 ``d.setVar("X","value")`` if the variable "X" does not exist. 2038 * - ``d.delVar("X")`` 2039 - Deletes the variable "X" from the datastore. Does nothing if the variable 2040 "X" does not exist. 2041 * - ``d.renameVar("X", "Y")`` 2042 - Renames the variable "X" to "Y". Does nothing if the variable "X" does 2043 not exist. 2044 * - ``d.getVarFlag("X", flag, expand)`` 2045 - Returns the value of variable "X". Using "expand=True" expands the 2046 value. Returns "None" if either the variable "X" or the named flag does 2047 not exist. 2048 * - ``d.setVarFlag("X", flag, "value")`` 2049 - Sets the named flag for variable "X" to "value". 2050 * - ``d.appendVarFlag("X", flag, "value")`` 2051 - Appends "value" to the named flag on the variable "X". Acts like 2052 ``d.setVarFlag("X", flag, "value")`` if the named flag does not exist. 2053 * - ``d.prependVarFlag("X", flag, "value")`` 2054 - Prepends "value" to the named flag on the variable "X". Acts like 2055 ``d.setVarFlag("X", flag, "value")`` if the named flag does not exist. 2056 * - ``d.delVarFlag("X", flag)`` 2057 - Deletes the named flag on the variable "X" from the datastore. 2058 * - ``d.setVarFlags("X", flagsdict)`` 2059 - Sets the flags specified in the ``flagsdict()`` 2060 parameter. ``setVarFlags`` does not clear previous flags. Think of this 2061 operation as ``addVarFlags``. 2062 * - ``d.getVarFlags("X")`` 2063 - Returns a ``flagsdict`` of the flags for the variable "X". Returns "None" 2064 if the variable "X" does not exist. 2065 * - ``d.delVarFlags("X")`` 2066 - Deletes all the flags for the variable "X". Does nothing if the variable 2067 "X" does not exist. 2068 * - ``d.expand(expression)`` 2069 - Expands variable references in the specified string 2070 expression. References to variables that do not exist are left as is. For 2071 example, ``d.expand("foo ${X}")`` expands to the literal string "foo 2072 ${X}" if the variable "X" does not exist. 2073 2074Other Functions 2075--------------- 2076 2077Other functions are documented in the 2078:doc:`/bitbake-user-manual/bitbake-user-manual-library-functions` document. 2079 2080Extending Python Library Code 2081----------------------------- 2082 2083If you wish to add your own Python library code (e.g. to provide 2084functions/classes you can use from Python functions in the metadata) 2085you can do so from any layer using the ``addpylib`` directive. 2086This directive is typically added to your layer configuration ( 2087``conf/layer.conf``) although it will be handled in any ``.conf`` file. 2088 2089Usage is of the form:: 2090 2091 addpylib <directory> <namespace> 2092 2093Where <directory> specifies the directory to add to the library path. 2094The specified <namespace> is imported automatically, and if the imported 2095module specifies an attribute named ``BBIMPORTS``, that list of 2096sub-modules is iterated and imported too. 2097 2098Testing and Debugging BitBake Python code 2099----------------------------------------- 2100 2101The OpenEmbedded build system implements a convenient ``pydevshell`` target which 2102you can use to access the BitBake datastore and experiment with your own Python 2103code. See :yocto_docs:`Using a Python Development Shell 2104</dev-manual/python-development-shell.html#using-a-python-development-shell>` in the Yocto 2105Project manual for details. 2106 2107Task Checksums and Setscene 2108=========================== 2109 2110BitBake uses checksums (or signatures) along with the setscene to 2111determine if a task needs to be run. This section describes the process. 2112To help understand how BitBake does this, the section assumes an 2113OpenEmbedded metadata-based example. 2114 2115These checksums are stored in :term:`STAMP`. You can 2116examine the checksums using the following BitBake command:: 2117 2118 $ bitbake-dumpsigs 2119 2120This command returns the signature data in a readable 2121format that allows you to examine the inputs used when the OpenEmbedded 2122build system generates signatures. For example, using 2123``bitbake-dumpsigs`` allows you to examine the ``do_compile`` task's 2124"sigdata" for a C application (e.g. ``bash``). Running the command also 2125reveals that the "CC" variable is part of the inputs that are hashed. 2126Any changes to this variable would invalidate the stamp and cause the 2127``do_compile`` task to run. 2128 2129The following list describes related variables: 2130 2131- :term:`BB_HASHCHECK_FUNCTION`: 2132 Specifies the name of the function to call during the "setscene" part 2133 of the task's execution in order to validate the list of task hashes. 2134 2135- :term:`BB_SETSCENE_DEPVALID`: 2136 Specifies a function BitBake calls that determines whether BitBake 2137 requires a setscene dependency to be met. 2138 2139- :term:`BB_TASKHASH`: Within an executing task, 2140 this variable holds the hash of the task as returned by the currently 2141 enabled signature generator. 2142 2143- :term:`STAMP`: The base path to create stamp files. 2144 2145- :term:`STAMPCLEAN`: Again, the base path to 2146 create stamp files but can use wildcards for matching a range of 2147 files for clean operations. 2148 2149Wildcard Support in Variables 2150============================= 2151 2152Support for wildcard use in variables varies depending on the context in 2153which it is used. For example, some variables and filenames allow 2154limited use of wildcards through the "``%``" and "``*``" characters. 2155Other variables or names support Python's 2156`glob <https://docs.python.org/3/library/glob.html>`_ syntax, 2157`fnmatch <https://docs.python.org/3/library/fnmatch.html#module-fnmatch>`_ 2158syntax, or 2159`Regular Expression (re) <https://docs.python.org/3/library/re.html>`_ 2160syntax. 2161 2162For variables that have wildcard suport, the documentation describes 2163which form of wildcard, its use, and its limitations. 2164