https://docs.yoctoproject.org/bitbake/bitbake-user-manual/bitbake-user-manual-metadata.html
使用等于号(=)给变量赋值:
VARIABLE = "value" //立即生效
VARIABLE = " value" //空格会被保留,赋给变量
VARIABLE = "value " //空格会被保留,赋给变量
VARIABLE = "" //双引号中没有空格。VRAIABLE为值为空
VARIABLE = " " //双引号中有一个空格。VARIABLE的为一个空格。
VARIABLE = 'I have a " in my value' //可以用单引号给变量赋值。单引号中的值,可以包括双引号
修改已有变量的值
在基于Yocto的实际开发和使用过程中,我们可能经常需要修改已有变量的值。下面是Yocto官方列出的一些典型场景:
-
Customize a recipe that uses the variable. //当你想通过新建/定制自己的recipe 的方式添加自己的应用程序时
-
Change a variable’s default value used in a
*.bbclass
file. //修改bbclass中的变量默认值 -
Change the variable in a
*.bbappend
file to override the variable in the original recipe.//修改或者新建bbappend文件,来修改当前bb文件中的变量 -
Change the variable in a configuration file so that the value overrides an existing configuration.//修改conf文件的变量,以覆盖已存值
在Yocto中修改变量的值,经常依赖于该变量被如何定义的,以及你想通过什么方法修改这个变量。比如,当你想附加一个值 到 已经有默认数值的变量中,最终该变量的数值可能不是你想要的结果。可能该变量的默认值已经被你替换成了新的值,而不是在默认值的基础上附加。可以用下面的方法查看变量的值:
$bitbake -e
这个命令会显示包括conf文件(比如. local.conf
, bblayers.conf
, bitbake.conf等
),以及很多变量在conf/inc/bbclass等文件中操作。
.conf //各种conf文件列表
.inc //各种inc文件列表
.bbclass //各种bbclass文件列表
$AARCH322_INC_PATH
# set ....
通过多行语句给变量赋值
下面两个语句最终结果都是FOO = “barbaz”
FOO = "barbaz"
FOO = "bar\
baz"
变量扩展
A = "aval"
B = "pre${A}post"
A的值是aval; B的值是preavalpost;
“=”运算符不会立即在右侧扩展变量引用。 而是将扩展推迟到实际使用分配给它的变量之前。 结果取决于引用变量的当前值。 示例:
A = "${B} baz"
B = "${C} bar"
C = "foo"
*At this point, ${A} equals "foo bar baz"*
C = "qux"
*At this point, ${A} equals "qux bar baz"*
B = "norf"
*At this point, ${A} equals "norf baz"\*
“=”的这一特点与“:=”运算符不同,可以与下文的“:=”运算符作比较。
另外,如果对不存在的变量使用变量扩展语法,则字符串将保持原样。 例如,给定以下分配,只要FOO不存在,BAR就会扩展为字符串“$ {FOO}”。
BAR = "${FOO}"
通过"?="给变量设置默认值
你可以使用“?=”运算符来实现变量的“软”分配。 这种类型的分配可以在分析语句时定义 未定义的变量,但是如果变量已经有值,则保留原有值值。 例子:
A ?= "aval"
If A
is set at the time this statement is parsed, the variable retains its value. However, if A
is not set, the variable is set to “aval”.
Note:This assignment is immediate. Consequently, if multiple “?=” assignments to a single variable exist, the first of those ends up getting used.
通过"??="给变量设置“弱”默认值
除了 "this assignment is made at the end of the parsing process rather than immediately", 其他与“?=”相同。
Consequently, when multiple “??=” assignments exist, the last one is used. Also, any “=” or “?=” assignment will override the value set with “??=”. 例子:
A ??= "somevalue"
A ??= "someothervalue"
If A
is set before the above statements are parsed, the variable retains its value. If A
is not set, the variable is set to “someothervalue”.
如果在上面的语句被分析之前A就被赋值了,那么A保留原值。如果A没有被赋值,那么A会被赋值为someothervalue.
Again, this assignment is a “lazy” or “weak” assignment because it does not occur until the end of the parsing process.
变量立即扩展 “:=”
“:=” 操作符会立即给变量赋值, 而不是在变量使用时才赋值。
T = "123"
A := "test ${T}"
T = "456"
B := "${T} ${C}"
C = "cval"
C := "${C}append"
以上语句的结果:A的值为“test 123”,虽然T最终的值为456。B的值为“456 cvalappend”, C的值为“cvalappend”
Appending/追加(+=) and prepending/前置 (=+) With Spaces
These operators insert a space between the current value and prepended or appended value. These operators take immediate effect during parsing.
+= 和 =+ 会在原值与附加值之间添加空格。例子:
B = "bval"
B += "additionaldata"
C = "cval"
C =+ "test"
结果:B为bval additionaldata, C为test cval
Appending/追加 (.=) and Prepending/前置 (=.) Without Spaces
与+=和=+不一样的地方在于:在原值与附加值之间无空格
B = "bval"
B .= "additionaldata"
C = "cval"
C =. "test"
结果:B为bvaladditionaldata, C为testcval
Appending/Prepending (Override Style Syntax)
该语法追加和前置,不会添加空格。所以使用时也需要注意空格的有无。
与运算符“=”,“.=”,“=.”,“+=”和“=+”运算符的不同之处在于,它们的效果是在可变扩展时间应用的,而不是立即应用的。 例子:
B = "bval"
B_append = " additional data"
C = "cval"
C_prepend = "additional data "
D = "dval"
D_append = "additional data"
结果:B的值为bval additional data,C的值为additional data cval, D的值为dvaladditional data
使用Override Style Syntax的好处
使用“_append”, “_prepend”, and “_remove” 相对于 “+=” and “=+” 的好处就是,前者能提供可保证达到你的操作目的。例子:
在class foo.bbclass
中添加 “val” 到 FOO变量
, 而在一个recipe 使用了 foo.bbclass
:
inherit foo
FOO = "initial"
如果像下面的语句一样在foo.bbclass
使用 “+=”,那么FOO的最终值为initial,并没有添加val。也就是说,并没有达到你的目的。
FOO += "val"
但是,如果在foo.bbclass中使用如下语句,那么FOO的最终值为initial val。(注意val前面有个空格)
FOO_append = " val"
Note: It is never necessary to use “+=” together with “_append”. The following sequence of assignments appends “barbaz” to FOO:
FOO_append = "bar"
FOO_append = "baz"
The only effect of changing the second assignment in the previous example to use “+=” would be to add a space before “baz” in the appended value (due to how the “+=” operator works).
接下来更新该章节的其余内容,并结合例子。
如何添加自己的应用程序;
注意事项:为什么IMAGE_INSTALL_append 不能遗漏空格;为什么IMAGE_INSTALL不能添加自己的应用程序到rootfs;除了IMAGE_INSTALL方式,其他的方法是什么