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 <srifenbark@gmail.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
parent
3a1ae38966
commit
277a5a969f
|
@ -428,6 +428,10 @@
|
||||||
FOO := "${@foo()}"
|
FOO := "${@foo()}"
|
||||||
</literallayout>
|
</literallayout>
|
||||||
</note>
|
</note>
|
||||||
|
For a different way to set variables with Python code during
|
||||||
|
parsing, see the
|
||||||
|
"<link linkend='anonymous-python-functions'>Anonymous Python Functions</link>"
|
||||||
|
section.
|
||||||
</para>
|
</para>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
@ -1063,32 +1067,81 @@
|
||||||
<title>Anonymous Python Functions</title>
|
<title>Anonymous Python Functions</title>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
Sometimes it is useful to run some code during
|
Sometimes it is useful to set variables or perform
|
||||||
parsing to set variables or to perform other operations
|
other operations programmatically during parsing.
|
||||||
programmatically.
|
To do this, you can define special Python functions,
|
||||||
To do this, you can define an anonymous Python function.
|
called anonymous Python functions, that run at the
|
||||||
Here is an example that conditionally sets a
|
end of parsing.
|
||||||
variable based on the value of another variable:
|
For example, the following conditionally sets a variable
|
||||||
<literallayout class='monospaced'>
|
based on the value of another variable:
|
||||||
python __anonymous () {
|
|
||||||
if d.getVar('SOMEVAR', True) == 'value':
|
|
||||||
d.setVar('ANOTHERVAR', 'value2')
|
|
||||||
}
|
|
||||||
</literallayout>
|
|
||||||
The "__anonymous" function name is optional, so the
|
|
||||||
following example is functionally equivalent to the above:
|
|
||||||
<literallayout class='monospaced'>
|
<literallayout class='monospaced'>
|
||||||
python () {
|
python () {
|
||||||
if d.getVar('SOMEVAR', True) == 'value':
|
if d.getVar('SOMEVAR', True) == 'value':
|
||||||
d.setVar('ANOTHERVAR', 'value2')
|
d.setVar('ANOTHERVAR', 'value2')
|
||||||
}
|
}
|
||||||
</literallayout>
|
</literallayout>
|
||||||
Because unlike other Python functions anonymous
|
An equivalent way to mark a function as an anonymous
|
||||||
Python functions are executed during parsing, the
|
function is to give it the name "__anonymous", rather
|
||||||
"d" variable within an anonymous Python function represents
|
than no name.
|
||||||
the datastore for the entire recipe.
|
</para>
|
||||||
Consequently, you can set variable values here and
|
|
||||||
those values can be picked up by other functions.
|
<para>
|
||||||
|
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:
|
||||||
|
<literallayout class='monospaced'>
|
||||||
|
python () {
|
||||||
|
d.setVar('FOO', 'foo 2')
|
||||||
|
}
|
||||||
|
|
||||||
|
FOO = "foo 1"
|
||||||
|
|
||||||
|
python () {
|
||||||
|
d.appendVar('BAR', ' bar 2')
|
||||||
|
}
|
||||||
|
|
||||||
|
BAR = "bar 1"
|
||||||
|
</literallayout>
|
||||||
|
The previous example is conceptually equivalent to the
|
||||||
|
following snippet:
|
||||||
|
<literallayout class='monospaced'>
|
||||||
|
FOO = "foo 1"
|
||||||
|
BAR = "bar 1"
|
||||||
|
FOO = "foo 2"
|
||||||
|
BAR += "bar 2"
|
||||||
|
</literallayout>
|
||||||
|
<filename>FOO</filename> ends up with the value "foo 2",
|
||||||
|
and <filename>BAR</filename> 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.
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<para>
|
||||||
|
Overrides and override-style operators such as
|
||||||
|
"<filename>_append</filename>" are applied before
|
||||||
|
anonymous functions run.
|
||||||
|
In the following example, <filename>FOO</filename> ends
|
||||||
|
up with the value "foo from anonymous":
|
||||||
|
<literallayout class='monospaced'>
|
||||||
|
FOO = "foo"
|
||||||
|
FOO_append = " from outside"
|
||||||
|
|
||||||
|
python () {
|
||||||
|
d.setVar("FOO", "foo from anonymous")
|
||||||
|
}
|
||||||
|
</literallayout>
|
||||||
|
For methods you can use with anonymous Python functions,
|
||||||
|
see the
|
||||||
|
"<link linkend='accessing-datastore-variables-using-python'>Accessing Datastore Variables Using Python</link>"
|
||||||
|
section.
|
||||||
|
For a different method to run Python code during parsing,
|
||||||
|
see the
|
||||||
|
"<link linkend='inline-python-variable-expansion'>Inline Python Variable Expansion</link>"
|
||||||
|
section.
|
||||||
</para>
|
</para>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue