Yocto - 使用Yocto开发嵌入式Linux系统_12 开发定制层
Exploring External Layers
Poky 最迷人的功能之一是灵活使用外部层。在本章中,我们将探讨为什么这是一项重要功能,以及如何利用它。我们还将了解不同类型的图层及其目录树布局。最后,在本章结尾,我们将学习在项目中加入一个新图层。
One of the most charming features of Poky is the flexibility of using external layers. In this chapter, we will examine why this is a vital capability and how we can take advantage of it. We will also look at the different types of layers and their directory trees layout. Finally, at the end of this chapter, we will learn to include a new layer in our project.
1, Powering flexibility with layers
Poky 包含的元数据分布在机器和发行版文件、类和配方等配置定义文件中,涵盖了从简单应用到完整图形堆栈和框架的所有内容。BitBake 可以从多个地方加载元数据集合,这些地方被称为元数据层。
Poky contains metadata spread over configuration definition files such as machine and distro files, classes, and recipes, covering everything from simple applications to full graphical stacks and frameworks. There are multiple places that BitBake can load metadata collection from, which are known as metadata layers.
使用图层的最大优势是可以将元数据分割成逻辑单元,这样用户就可以只选择项目所需的元数据集合。
The biggest strength of using layers is the ability to split metadata into logical units, which enables users to pick only the metadata collection needed for a project.
使用元数据层可以让我们做到以下几点:
* 提高代码重用率
* 在不同团队、社区和供应商之间共享和扩展工作
* 提高 Yocto 项目社区的代码质量,因为多个开发人员和用户会共同关注他们感兴趣的特定元数据层
Using metadata layers enables us to do the following:
* Improve code reuse
* Share and scale work across different teams, communities, and vendors
* Increase the Yocto Project community’s code quality, as multiple developers and users focus together on a particular metadata layer that is of interest to them
我们可以出于不同的原因配置系统,例如需要启用/禁用某个功能,或更改构建标志以启用特定架构的优化。这些都是可以使用层进行定制的例子。
We can configure the system for different reasons, such as the need to enable/disable a feature or change build flags to enable architecture-specific optimizations. These are examples of customizations that can be done using layers.
In addition, we should organize metadata in different layers instead of creating our custom project environment, changing recipes, and modifying files in the Poky layer. The more separated an organization is, the easier it is to reuse the layers in future projects, as the Poky source code is split into different layers as well. It contains three layers by default, as we can see in the output of the following command line:
此外,我们应该在不同的层中组织元数据,而不是在 Poky 层中创建自定义项目环境、更改配方和修改文件。由于 Poky 源代码也被分割成不同的层,因此组织得越分离,就越容易在未来的项目中重复使用这些层。默认情况下,它包含三个层,我们可以从以下命令行的输出中看到:
[ Figure 11.1 – The result of bitbake-layers show-layers ]
命令行输出会显示任何图层的以下三个基本属性:
* 名称: 通常以元字符串开头。
* 路径: 当我们想在项目中添加一个层,同时附加到 BBPATH 变量时,这一点非常重要。
* 优先级: 这是 BitBake 用来决定使用哪种配方以及 .bbappend 文件连接顺序的值。这意味着,如果两个层包含相同的配方文件(.bb),则使用优先级最高的那个。对于 .bbappend 文件,每个 .bbappend 文件都包含在原始配方中。图层优先级决定了包含的顺序,因此优先级最高的图层中的 .bbappend 文件会首先被附加,然后才是其他文件。
The command-line output shows the following three essential properties of any layer:
* Name: This usually starts with the meta string.
* Path: This is important when we want to add a layer in our project that is appended to the BBPATH variable.
* Priority: This is the value used by BitBake to decide which recipe to use and the order in which the .bbappend files should be concatenated. It means that if two layers include the same recipe file ( .bb), the one with the highest priority is used. In the case of .bbappend, every .bbappend file is included in the original recipe. The layer priority determines the order of inclusion, so the .bbappend files within the highest priority layers are appended first, followed by the others.
以 Poky 为例,它有三个中心独立层。meta-yocto-bsp 层是 Poky参考的板支持包(BSP)层。它包含机器配置文件和为机器配置软件包的配方。由于它是一个参考 BSP 层,因此可以作为示例使用。
Taking Poky as an example, it has three central individual layers. The meta-yocto-bsp layer is the Poky reference Board Support Package (BSP) layer. It contains machine configuration files and recipes to configure packages for the machines. As it is a reference BSP layer, it can be used as an example.
meta-poky 层是 Poky 参考分发层。它包含 Yocto Project 默认使用的发行版配置。该默认发行版在 poky.conf 文件中有所描述,被广泛用于测试产品。在设计自己的发行版时,可以将其作为起点。
The meta-poky layer is the Poky reference distribution layer. It contains a distribution configuration used in Yocto Project by default. This default distribution is described in the poky.conf file, and it is widely used for testing products. It can be used as a starting point when designing your own distribution.
另一种层是软件层,只包括应用程序或应用程序的配置文件,可用于任何架构。软件层种类繁多。仅举几例,我们就有元 Java、元 qt5 和元浏览器。meta-java 层提供 Java 运行时和 SDK 支持,meta-qt5 层包括 Qt5 支持,meta-browser 支持 Firefox 和 Chrome 等网络浏览器。
Another kind of layer is the software layer, which includes only applications or configuration files for applications and can be used on any architecture. There is a massive list of software layers. To name only a few, we have meta-java, meta-qt5, and meta-browser. The meta-java layer provides Java runtime and SDK support, the meta-qt5 layer includes Qt5 support, and meta-browser supports web browsers such as Firefox and Chrome.
meta层是 OpenEmbedded Core 元数据,其中包含配方、类和 QEMU 机器配置文件。它可以被视为一种混合层类型,因为它包含软件集合、BSP 定义和作为基线的 Yocto 项目使用的发行版。
The meta layer is the OpenEmbedded Core metadata, which contains the recipes, classes, and the QEMU machine configuration files. It can be considered a mixed layer type, as it has software collection, BSP definition, and the distribution used by Yocto Project as the baseline.
有时,您的产品可能有特殊要求,需要根据需要更改 build/conf/local.conf 文件。最适当和可维护的解决方案是创建一个分发层来放置分发定义文件。
Sometimes, your product may have special requirements, and changes in the build/conf/local.conf file will need to be made as required. The most adequate and maintainable solution is to create a distribution layer to place the distribution definition file.
Tip
build/conf/local.conf 文件是一个不稳定的文件,不应该被 Git 跟踪。
The build/conf/local.conf file is a volatile file that is not supposed to be tracked by Git.
我们不应依赖它来设置软件包版本、提供程序和产品的系统功能,而应将其作为开发过程中测试的快捷方式。
We should not rely on it to set package versions, providers, and the system features for products but use it instead just as a shortcut for testing purposes during development.
避免在 build/conf/local.conf 文件中添加自定义设置,有助于确保构建后的可重现性。
Avoiding adding custom settings in build/conf/local.conf helps to make our builds reproducible afterward.
2, Detailing a layer’s source code
通常,一个图层有一个目录树,如下图所示:
Usually, a layer has a directory tree, as shown in the following screenshot:
[ Figure 11.2 – The standard layer layout ]
该目录下有两个文件:<layer>/COPYING 和 <layer>/README,分别是许可证和给用户的信息。在 <layer>/README 中,我们必须指定该层用户需要了解的任何其他依赖关系和信息。层的meta-前缀并不是要求,而是常用的命名约定。
Inside this directory are two files, <layer>/COPYING and <layer>/README, a license and a message to a user respectively. In <layer>/README, we must specify any other dependency and information that the layer’s users need to know. The meta- prefix for the layer is not a requirement but a commonly used naming convention.
classes 文件夹应包含该层的特定类(.bbclass 文件)。这是一个可选目录。
The classes folder should hold the classes specific to that layer (the .bbclass files). It is an optional directory.
<layer>/conf 文件夹是必须的,它应提供配置文件(.conf 文件)。层配置文件 <layer>/conf/layer.conf 将在下一章详细介绍,它是包含层定义的文件。
The <layer>/conf folder is mandatory and should provide the configuration files (the .conf files). The layer configuration file, <layer>/conf/layer.conf, which will be covered in detail in the next chapter, is the file with the layer definition.
<layer>/conf 文件夹的目录布局示例如图 11.2 所示,其中 (a) 显示的是 BSP 层的结构,(b) 显示的是分发层的结构:
An example of the directory layout of the <layer>/conf folder is shown in Figure 11.2, where (a) shows the structure for a BSP layer and (b) shows the structure for a distribution layer:
[ Figure 11.3 – The <layer>/conf layout for BSP and distribution layers ]
recipes-* 文件夹是一组按类别分隔的配方,例如,recipes-core、recipes-bsp、recipes-graphic、recipes-multimedia 和 recipes-kernel。在以 recipes- 为前缀的每个文件夹中,都有一个包含菜谱名称或菜谱组的目录。在该目录中,放置了名称以 .bb 或 .bbappend 结尾的配方文件。例如,我们可以从meta层中找到以下截图:
The recipe-* folder is a cluster of recipes separated by category – for example, recipes-core, recipes-bsp, recipes-graphic, recipes-multimedia, and recipes-kernel. Inside each folder, starting with the recipes- prefix, there is a directory with the recipe name or a group of recipes. Inside it, the recipe files are placed, whose names end with .bb or .bbappend. For example, we can find the following screenshot from meta layer:
3, Adding meta layers
我们可以在 http://layers.openembedded.org 找到大部分可用的元图层。来自 Yocto 项目、OpenEmbedded、社区和公司的数百个元图层可以手动克隆到项目源代码目录中。
We can find the most of available meta layers at http://layers.openembedded.org . There are hundreds of meta layers from the Yocto Project, OpenEmbedded, communities, and companies that can be manually cloned inside the project source directory.
例如,要在我们的项目中加入 meta-oe(meta-openembedded 资源库中的多个元层之一),我们可以更改配置文件的内容或使用 BitBake 命令行。不过,我们首先需要获取该层的源代码。在 Poky 源代码目录下运行以下命令:
To include, for example, meta-oe (one of the several meta layers inside the meta-openembedded repository) in our project, we can change the content of the configuration files or use BitBake command lines. However, we first need to fetch the layer’s source code. Run the following command from your Poky source directory:
[ Figure 11.5 – Cloning the meta-openembedded layer ]
我们需要修改 build/conf/bblayer.conf 文件,使用绝对路径添加图层位置。请参见图 11.6 中的第 12 行,如下所示:
We need to modify the build/conf/bblayer.conf file to add the layer location, using its absolute path. See line 12 in Figure 11.6 as follows:
或者,我们也可以使用 bitbake-layers 工具来执行包含。可以在构建目录中使用以下命令来完成:
Alternatively, we can use the bitbake-layers tool to perform the inclusion for us. This can be done using the following command from the build directory:
4,The Yocto Project layer ecosystem
创建图层非常方便。为了使所有可用图层更易于访问,OpenEmbedded 社区开发了一个索引,可在 http://layers.openembedded.org 上找到大部分图层。图层选项卡的示例如下:
It is convenient to create a layer. To make all the available layers easier to access, the OpenEmbedded community has developed an index, available at http://layers.openembedded.org, where most of them can be found. An example of its Layers tab is shown as follows:
[ Figure 11.8 – The OpenEmbedded Layer Index for Kirkstone ]
OpenEmbedded Layer Index 网站的另一个方便使用情况是搜索特定的软件类型或配方。通过 OpenEmbedded Layer Index,我们可以搜索以下内容:
Another convenient use case for the OpenEmbedded Layer Index website is to search for a specific software type or recipe. The OpenEmbedded Layer Index can save the day by allowing us to search for the following:
* Machines
* Distributions
* Layers
* Recipes
* Classes
bitbake-layers 工具还支持使用 OpenEmbedded Layer Index。例如,要添加 meta-oe 图层,我们可以使用以下命令:
The bitbake-layers tool also supports the use of the OpenEmbedded Layer Index. For example, to add the meta-oe layer, we can use the following command:
5, Summary
在本章中,我们介绍了分层的概念。我们详细了解了目录结构和每种图层类型的内容。此外,我们还了解了如何手动或使用 BitBake 命令行向项目中添加外部图层,以及如何使用 OpenEmbedded 图层索引轻松查找所需的可用图层。
In this chapter, we introduced the concept of layering. We learned about the directory structure in detail and the content in each layer type. In addition, we saw how to add an external layer to our project manually or by using the BitBake command line, as well as how to use the OpenEmbedded Layer index to find the available layers we need easily.
在下一章中,我们将进一步了解为什么需要创建新图层以及其中包含的常见元数据(如机器定义文件、配方和图像)。最后,我们将以分发自定义为例进行总结。
In the next chapter, we will learn more about why we need to create new layers and what the common metadata included in them is (such as machine definition files, recipes, and images). We will wrap it all up with an example of distribution customization.