Diving into BitBake Metadata
在本书的这一部分,我们已经知道了如何生成图像和软件包,以及如何使用软件包源--基本上,这就是我们简单使用 Poky 所必须知道的一切。接下来,我们将学习如何控制 Poky 的行为,以实现我们的目标,并从整个 Yocto 项目中获取最大利益。
At this point in this book, we know how to generate images and packages and how to use package feeds – basically, everything we must know for the simple usage of Poky. Hereafter, we will learn how to control the behavior of Poky to accomplish our goals and achieve maximum benefit from the Yocto Project as a whole.
本章将帮助我们加深对 BitBake 元数据语法的理解。我们将学习使用 BitBake 运算符来更改变量内容、变量扩展等。这些都是我们可以用来制作配方和进行定制的关键概念,我们将在接下来的章节中学习这些内容。
This chapter will help enhance our understanding of the BitBake metadata syntax. We will learn to use the BitBake operators to alter the content of variables, variable expansions, and so on. These are the key concepts we can use to make our recipes and the customization that we will learn about in the following chapters.
1, Understanding BitBake’s metadata
BitBake 使用的元数据量非常大。因此,要想从 Poky 中获得最大收益,我们必须掌握它。正如我们在第 4 章 “认识 BitBake 工具 ”中所了解到的,元数据包括三个主要方面:
The amount of metadata used by BitBake is enormous. Therefore, to get the maximum benefit from Poky, we must master it. As we learned in Chapter 4, Meeting the BitBake Tool, metadata covers three major areas:
* 配置(.conf 文件): 配置文件定义了配置类和配方工作方式的全局内容。
* 类(.bbclass 文件): 类可以继承,以便于维护,促进代码重用,避免代码重复。
* 配方(.bb 或 .bbappend 文件): 配方描述要运行的任务,并提供所需的信息,以便 BitBake 生成所需的任务链。它们是最常用的元数据,因为它们为配方定义了变量和任务。最常见的配方类型是生成软件包和图像。
* Configuration (the .conf files): The configuration files define the global content that configures how the classes and recipes will work.
* Classes (the .bbclass files): Classes can be inherited for easier maintenance and to promote code reuse and avoid code duplication.
* Recipes (the .bb or .bbappend files): The recipes describe the tasks to be run and provide the required information to allow BitBake to generate the required task chain. They are the most commonly used metadata, as they define the variables and tasks for the recipes. The most common types of recipes generate packages and images.
这些类和配方混合使用了 Python 和 Shell Script 代码,BitBake 在解析这些代码时会生成大量任务和本地状态,这些任务和状态在解析后仍需执行。
The classes and recipes use a mix of Python and Shell Script code, which is parsed by BitBake, generating a massive number of tasks and local states that must still be executed after being parsed.
我们还将学习建立配方所需的运算符和基本概念。
We will also learn about the operators and essential concepts we need to build our recipes.
Working with metadata
BitBake 元数据使用的语法可能会引起误解,有时甚至难以追踪。不过,我们可以使用 bitbake 选项(-e 或 --environment)检查 BitBake 生成的预处理配方数据中每个变量的值,如下所示:
The syntax used by BitBake metadata can be misleading and sometimes hard to trace. However, we can check the value of each variable in BitBake-generated, pre-processed recipe data by using the
bitbake option (
-e or
--environment), as follows:

[ Figure 8.1 – How to display the BitBake environment ]
要了解 BitBake 如何工作,请参阅《BitBake 用户手册》 (
BitBake User Manual — Bitbake dev documentation )。下文将介绍配方中常用的大部分语法。
To understand how BitBake works, please refer to BitBake User Manual (
BitBake User Manual — Bitbake dev documentation ). The following sections will show most of the syntax commonly used in recipes.
The basic variable assignment
变量的赋值过程如下所示:
The assignment of a variable can be done as shown here:

[ Figure 8.2 – An example of a variable assignment ]
在上例中,FOO 变量的值被分配给了 bar。变量赋值是 BitBake 元数据语法的核心,因为大多数示例都使用了变量。
In the preceding example, the value of the
FOO variable is assigned to
bar. Variable assignment is core to the BitBake metadata syntax, as most examples use variables.
The variable expansion
BitBake 支持变量引用。其语法与 Shell 脚本十分相似,例如以下语法:
BitBake supports variable referencing. The syntax closely resembles Shell Script, such as the following:

[ Figure 8.3 – An example of variable expansion ]
上例的结果是 A 包含 aValue,B 包含 before-aValue-after。需要注意的是,变量只有在使用时才会展开,如图所示&