From 277a5a969fd7a35c8e5d112861ba6a815c0af0ec Mon Sep 17 00:00:00 2001 From: Scott Rifenbark Date: Mon, 8 Aug 2016 08:54:41 -0700 Subject: [PATCH] bitbake: bitbake-user-manual: Added more detail to anonymous Python functions. Fixes [YOCTO #10093] Provided much more detail on how these functions work. (Bitbake rev: dbe25523d899850f85acb6986eca98bf1b0ef52a) Signed-off-by: Scott Rifenbark Signed-off-by: Richard Purdie --- .../bitbake-user-manual-metadata.xml | 93 +++++++++++++++---- 1 file changed, 73 insertions(+), 20 deletions(-) diff --git a/bitbake/doc/bitbake-user-manual/bitbake-user-manual-metadata.xml b/bitbake/doc/bitbake-user-manual/bitbake-user-manual-metadata.xml index 4beb5a1d89..4fa51c446d 100644 --- a/bitbake/doc/bitbake-user-manual/bitbake-user-manual-metadata.xml +++ b/bitbake/doc/bitbake-user-manual/bitbake-user-manual-metadata.xml @@ -428,6 +428,10 @@ FOO := "${@foo()}" + For a different way to set variables with Python code during + parsing, see the + "Anonymous Python Functions" + section. @@ -1063,32 +1067,81 @@ Anonymous Python Functions - Sometimes it is useful to run some code during - parsing to set variables or to perform other operations - programmatically. - To do this, you can define an anonymous Python function. - Here is an example that conditionally sets a - variable based on the value of another variable: - - python __anonymous () { - if d.getVar('SOMEVAR', True) == 'value': - d.setVar('ANOTHERVAR', 'value2') - } - - The "__anonymous" function name is optional, so the - following example is functionally equivalent to the above: + Sometimes it is useful to set variables or perform + other operations programmatically during parsing. + To do this, you can define special Python functions, + called anonymous Python functions, that run at the + end of parsing. + For example, the following conditionally sets a variable + based on the value of another variable: python () { if d.getVar('SOMEVAR', True) == 'value': d.setVar('ANOTHERVAR', 'value2') } - Because unlike other Python functions anonymous - Python functions are executed during parsing, the - "d" variable within an anonymous Python function represents - the datastore for the entire recipe. - Consequently, you can set variable values here and - those values can be picked up by other functions. + An equivalent way to mark a function as an anonymous + function is to give it the name "__anonymous", rather + than no name. + + + + Anonymous Python functions always run at the end + of parsing, regardless of where they are defined. + If a recipe contains many anonymous functions, they + run in the same order as they are defined within the + recipe. + As an example, consider the following snippet: + + python () { + d.setVar('FOO', 'foo 2') + } + + FOO = "foo 1" + + python () { + d.appendVar('BAR', ' bar 2') + } + + BAR = "bar 1" + + The previous example is conceptually equivalent to the + following snippet: + + FOO = "foo 1" + BAR = "bar 1" + FOO = "foo 2" + BAR += "bar 2" + + FOO ends up with the value "foo 2", + and BAR with the value "bar 1 bar 2". + Just as in the second snippet, the values set for the + variables within the anonymous functions become available + to tasks, which always run after parsing. + + + + Overrides and override-style operators such as + "_append" are applied before + anonymous functions run. + In the following example, FOO ends + up with the value "foo from anonymous": + + FOO = "foo" + FOO_append = " from outside" + + python () { + d.setVar("FOO", "foo from anonymous") + } + + For methods you can use with anonymous Python functions, + see the + "Accessing Datastore Variables Using Python" + section. + For a different method to run Python code during parsing, + see the + "Inline Python Variable Expansion" + section.