写在前面

这本书的“译者序”里面有这么一句话:

市面上不缺乏适用于各种开发语言的开发、编译和打包工具,但在Yocto项目出现前,这些零散的工作需要嵌入式开发者自己串起来以交付最终的嵌入式系统。

我之前MCU做的多,Linux做的少。
最近研究Linux裁剪移植,很多芯片厂商都是适用Yocto。这个工具需要会。
但是从上面的这句话我理解到:

  • 对Yocto的使用是一个层面
  • Linux 系统的整体发布是另一个层面
  • Yocto是一个工具,如果不知道这个工具要解决的问题的话,学习起来会很迷茫

这本书有英文原版,可以在网上找一下。中文版翻译的一般情况,墙裂推荐英文原版。

关于构建和Yocto工作流

红色的 Metadata/Input 是 Yocto 工程重点关注项,控制配置及工作流程。
金色的 Process Steps 是 Yocto 的固定的工作流。
草绿色?的Local Storage 是工程的源代码等资源。

两个步骤

  1. 构建环境配置
    一般通过 shell 脚本完成该任务。构建环境相关的配置(主要是若干环境变量)可以在此时(此处)实例化。
  2. 执行构建命令
$ bitbake [options] [recipename/target …]

这是 Yocto 的追求:一条命令完成构建。

bitbake 命令

Bitbake:一个任务执行引擎,用来解析并执行Metadata;类似于一个脚本解释器
需要完成构建环境配置后,才能在命令行里使用该命令。

$ bitbake —help
Usage: bitbake [options] [recipename/target …] Executes the specified task (default is ‘build’) for a given set of
target recipes (.bb files). It is assumed there is a conf/bblayers.conf
available in cwd or in BBPATH which will provide the layer, BBFILES and
other configuration information. Options:...

Options 可以被分为几类:

  • 显示程序版本和帮助
  • 执行带依赖处理的构建(重要)
    Calling BitBake with a target, the base name of the recipe file without the .bb extension, runs the default task as defined by the variable BB_DEFAULT_TASK, typically build:
$ bitbake core-image-minimal

会处理相关的依赖,特点是将 recipe 的后缀 . bb 取掉
如果加上 -k 的话,即使出错,也会继续构建

  • 执行不带依赖处理的构建
    加上 -b 并指定 recipe file

  • 执行特定任务(重要,可以单独clean、配置、编译等)
    所以说一条 bitbake 命令可以跟两个变量:recipe 和 task
    加上 -c <task>

$ bitbake editor -c compile

listtasks 是一个有用的 task,可以列出一个定制套餐的待执行任务:

$ bitbake core-image-minimal -c listtasks

<task> 是可以自主定制的。

  • 强制执行
    通过 -C (大写)或者 -f 来控制,类似于重新编译

If, on subsequent runs of a task, the task’s time stamp is current or later then the time stamps of all the tasks that task depends on BitBake does not run the task.

  • 展示元数据(重要,调试相关)
    经过一系列的脚本解释,Yocto 执行了一个类似于编译和链接的操作。会形成一个整体的输出。
    通过 -e,可以将这个输出展示出来。

  • 创建 Dependency Graphs

  • 提供和覆盖配置数据

Yocto工程的文件组织

Poky

Poky is the Yocto Project’s reference distribution. It includes the
OpenEmbedded build system. It provides all the necessary tools,
recipes, and configuration data required to build a Linux OS stack. As
we saw in the previous chapter, Poky is a mostly self- contained
system bundled as a simple archive.

Poky 既是 Yocto 的一个参考发行版,又包含了构建一个 Yocto 工程的必要工具。
所以,所有的 Yocto 项目都会包含一个 Poky 文件夹;并且基本上只需要一个 Poky 文件夹就可以做 Yocto 工程了,还是很简洁的。

Build System Structure

构建系统(这是个偏正短语,不是动宾短语),一般指的是 Poky 这个文件夹。

xxx@xxx:poky$ tree -L 1
.
├── bitbake
├── contrib
├── documentation
├── LICENSE
├── LICENSE.GPL-2.0-only
├── LICENSE.MIT
├── Makefile
├── MEMORIAM
├── meta
├── meta-poky
├── meta-selftest
├── meta-skeleton
├── meta-yocto-bsp
├── oe-init-build-env
├── README.hardware -> meta-yocto-bsp/README.hardware
├── README.OE-Core
├── README.poky -> meta-poky/README.poky
├── README.qemu
└── scripts

这个文件夹里的内容,原则上是不应该去修改的,并且在开发过程中应该锁定某个版本,而不是跟随社区进行更新。
文件夹的内容基本上是和上图对应的。

bitbake.conf

这是 BitBake 的主配置文件,比较重要的几个 .conf 之一。
位置:poky/meta/conf/bitbake.conf

BitBake’s master or main configuration file is named bitbake.conf.
BitBake expects this file to be present in all of the directories
listed in its metadata search path. This file contains all the default
configuration settings. Other configuration files and recipes commonly
override some of the variable settings in this file according to their
specific requirements.

oe-init-build-env

这是是一个非常重要的脚本,它是生成构建环境的核心。
不应该去修改它,但是可以通过:

  1. 传参
  2. 修改其生成文件
    的方式完成对“构建环境”的差异化和定制化。

meta目录

poky/meta/classes下面有大量的 .bbclass 文件,提供了丰富的操作函数,可以直接被 .bb 文件引用。

scripts 目录

Yocto 一个的脚本集,大量的脚本会在实际的 Yocto 工程中被引用。

Build Environment Structure

构建环境。
这个文件夹是被生成的。

原生的构建环境

仅执行 oe-init-build-env 脚本,便可得到一个原生的构建环境。

xxx@xxx:imx-yocto-bsp$ tree -L 2 ./build
./build
└── conf├── bblayers.conf├── bblayers.conf.org├── local.conf├── local.conf.org├── local.conf.sample└── templateconf.cfg

原生的构建环境,只有两个有效文件:

  1. bblayers.conf,构建环境层配置文件

A build environment needs to tell BitBake what layers it requires for its build process. The file bblayers.conf provides BitBake with information on what layers to include with the build process and the filesystem paths where they are found. Each build environment has its own bblayers.conf file, which can be found in the conf subdirectory of the build environment.

  1. local.conf,构建环境本地配置文件

Local configuration of a build environment is provided through a configuration file named local.conf. The local.conf file contains settings that apply to the particular build environment, such as paths to download locations, build outputs, and other files; configuration settings for the target system such as the target machine, package management system, and distribution policy; and many other settings.

上面的这两个文件,只会在构建环境的目录中存在。

对构建环境进行定制

可以通过一系列的脚本对上文提到的两个 .conf 文件进行修改和定制。

machine、distro、和 image

执行整体构建的时候,bitbake 后面接的参数是按 image 来的。
image 的主要作用是说明依赖的模块。每个 <module> 由 <module.bb> 来代表。

暂时还没有搞明白 distro 的作用。
总感觉一个 image 就决定了 distro 了。应该是还有一个领域暂时不清楚。
也许比如说,一个 Ubuntu1604 是一个distro,但是这个发型版里预装的软件可以有差别。

machine 是比较好理解的,同样的上层有可能会跑着不同的底层上。
在 NXP 的脚本里,构建环境是按 machine 来命名区分的。

元数据层结构

元数据层的文件命名是比较结构化的。下表中,斜体代表目录。

meta-<layername>
├── classes
│   ├── class<1>.bbclass
│   ├── class<2>.bbclass
│   ├── ...
│   └── class<l>.bbclass
├── conf
│   ├── layer.conf
│   ├── machine
│   │   ├── <machine 1>.conf
│   │   ├── <machine 2>.conf
│   │   ├── ...
│   │   └── <machine m>.conf
│   └── distro
│       ├── <distro 1>.conf
│       ├── <distro 2>.conf
│       ├── ...
│       └── <distro r>.conf
├── recipes-<category 1>
│   ├── <package a>
│   │   ├── <package a>_<version 1>.bb
│   │   └── <package a>_<version 2>.bb
│   ├── <package b>
│   │   ├── <package b>_<version 1>.bb
│   │   └── <package b>_<version 2>.bb
│   ├── ...
│   └── <package z>
├── recipes-<category 2>
│   └── ...
└── recipes-<category n>└── ...

在一个层中,conf/layer.conf 是必选的(相应的 conf 目录也是必选的)。BitBake requires this file to set up paths and search patterns for metadata files.
conf/machine/和conf/distro/是可选的,可以通过这两个文件夹里的配置文件,对不同的板卡和发布版本做出差异化设置。
recipes-<category 1>原则上也是可选的。但是没有 recipes 的 layer 是没有意义的。
可以使用yocto-layer脚本来自动化的创建一个自己的层(但是测试没有成功)。

调试

日志系统

Yocto 的日志系统也是比较全(复)面(杂)的。具体细节这里不说了。
每次任务运行,bitbake 都会创建一个任务脚本文件:run.do_<taskname>.<pid>
bitbake 的每个任务都会有其对应的日志文件:log.do_<taskname>.<pid>,如果任务被运行多次,会有多个日志被生成。

bitbake-layers

进入到构建环境中,可以执行 bitbake-layers 命令。具体细节参考附件一的第5.6节。
$bitbake-layers show-layers

Yocto 的一些语法策略

散点

  • Append file:An append file extends an existing recipe. BitBake verbatim appends the contents of an append file to the corresponding recipe, creating a single file before parsing it. Variables in an append file can override the same veriables defined in the corresponding recipe. Append files use the bbappend extension.
    就是说,除了包含和继承之外,还有另外一种方式扩展菜谱,就是新写一个 .bbappend 的文件做目标菜谱的扩展。

  • Recipe:A recipe is a metadata file containing directives for BitBake on how to build a particular software package. Through its directives, a recipe describes from where to obtain the source code, what patches to apply and how to apply them, how to build the binaries and associated files, how to install the build results on a target system, how to create the packaged software bundle, and much more. Recipes also describe dependencies during build and runtime on other software packages, hence creating a logical hierarchy of the pieces required for the build process. Recipes use the bb file extension.
    所以,菜谱做的事情,大概就是,获取资源、配置、编译、安装、打包,以及各模块间的依赖性。

  • Configuration Files:A variable can be set in one configuration file and overwritten in another. Recipes can also set and overwrite variables, but the assignments made in recipes remain local to the recipe.
    .conf 文件是用来配置的,里面没有函数和任务,.bbclass 里面主要是放函数,.bb 是主干,里面有任务的编排。
    The configuration file bitbake.conf has the lowest priority, and the local configuration file of the build environment local.conf has the highest.

  • bblayers.conf:Each build environment has its own bblayers.conf file, which can be found in the conf subdirectory of the build environment.
    这为不同的 image 构建提供了支持,每个 image 构建都有它自己的 build environment,然后在它自己的 build environment 里指定不同的 layers.

  • Priority:Each layer is assigned a priority by setting the variable BBFILE_PRIORITY. Layer priorities range from 1 to 10 with 1 being the lowest and 10 being the highest priority. If two layers use the same priority, then their order in the BBLAYERS veriable of the file bblayers.conf file determines the priority.
    The priority also determines in what order BitBake appends append files to the recipes. An append file from a layer with higher priority is added after an append file from a layer with lower priority.

  • BitBake automatically sets the variable LAYERDIR to the path to the top-level directory of a layer when it begins parsing the files in that layer.

  • BBFILE_COLLECTIONS:Contains a list of the names of configured layers. This list is used by BitBake to find other BBFILE_* variables in its data directory. Each layer typically adds its own name to the list.

  • .bbclass:Recipes can include class files by simply referencing them by their name using the inherit directive. Classes are global, meaning that recipes located in a layer can inherit classes from any other layer the build environment includes.

  • A metadata file can include any other metadata file; however, files containing executable metadata may be included only by recipes, append files, and classes.

变量

  • 变量作用域: .conf 中的变量范围是全局的,.bb 中的变量是局部的;如果有同名,在局部范围内,全局范围的值会被覆盖。
  • 默认赋值 (?=):多个默认赋值中,较早的那个有效;直接赋值无条件覆盖默认赋值。
  • Weak Default Value Assignment (??=):直到解析使用时,赋值才生效,所以较晚的赋值语句有效;直接赋值和默认赋值无条件覆盖弱默认赋值。
  • Immediate Variable Expansion (:=):普通的是到用的时候才展开,这个是赋值的时候就展开,遇到了要好好注意。
  • Python Variable Expansion:The @ operator tells BitBake to treat the expression following it as Python code.

变量追加和删减

  • Appending (+=) and Prepending (=+) with Space:前者把字符加到后面,后者把字符加到前面。这两个操作会相应的增加空格。
  • Appending (.=) and Prepending (=.) without Space:这个和前面类似,不过会拼接起来,中间不加空格。
  • Appending and Prepending Using the _append and _prepend Operators:这个和 (.=) 与 (=.) 一样,拼接不加空格。

复用

include 和 requried

后面可以跟 .inc 和 .bb 的文件。
include 是 optional inclusion;required 是 required inclusion.
The include and required directives can be used with relative and absolute path.
when relative paths are used, BitBake tries to locate the file using the list of file paths specified by the BBPATH variable.
BitBake uses the first file it finds that has the correct path segment and filename. 想想也对,使用遇到的第一个,可减少搜索的时间。
include 文件里的设置会覆盖 including file 里的设置,这个要注意。

Inheriance 和 class

后面只能跟 .bbclass 的文件。
.inc 文件中无法使用 inheriance.

  • BitBake identifies classes by their class name and not by their filename and path, which means class names must be unique across all metadata layers included by a build environment.

Executable Metadata

BitBake treats executable metadata exactly the same as variables: the function name is stored in the data dictionary together with the function code that represents the assigned value. Consequently, functions can be appended and prepended like regular variables and may also have metadata attributes.
The scope of metadata functions defined in recipes and append files is local to the particular file, whereas functions defined in classes are global.

也就是说,可以把可执行函数写在 .bbclass 里,也可以写在 .bb 里。但是在 .bb 里的是局部的,在 .bbclass 里的是全局的。函数可以像变量那边被补充,可以补充追加到函数的前面,也可以补充追加到函数的后面。
函数可以使用 shell 语法,也可以使用 python 语法。python 语法:

python printdate () {import timeprint time strftime(%Y%m%d’, time.gettime())
}

前面需要加关键字 python.


一些事情

<build_env>/tmp/log/…/ 下面,会有一个总的 log.
这个 log 会列出构建所输入的一些信息,还有
主体:都运行了哪些任务,还有任务执行时的一些消息打印。

任务

task 都是挂到 recipes 下面的;换句话说,每个配方下面会有若干个任务。
任务的执行函数是可以通过文件的继承来获得的。
不同的 recipes 下命名空间是独立的。

基于 NXP 的开发板进行构建测试的命令:
DISTRO=fsl-imx-wayland MACHINE=imx6ull14x14evk source imx-setup-release.sh -b build_6w
bitbake fsl-image-machine-test

一些其他的事情

关于名字

Yocto,发音 幺[科托]
与国际单位制的词头(也就是大小)中最小的那一个同源。
1um = 10^-6m;微米
1nm = 10^-9m;纳米
1pm = 10^-12m;皮米
1ym = 10^-24m;幺米

构建发行版所需要做的事情

  • 创建交叉编译的工具链

    • uClibc VS glibc、X.org、GStreamer
  • 引导加载程序
  • 内核镜像
  • 设备驱动
  • 根文件系统
  • 应用软件和库

Buildroot / OpenEmbedded / Yocto / Poky

Poky 是 Yocto 项目的参考发行版,Poky 包括了 OpenEmbedded 构建系统和一整套元数据。

Linux & 大量的开源软件包

搞清楚不同包之间的依赖、不兼容性和冲突是一个艰巨的任务。

Linux 标准基

Linux Standard Base,LSB
为 Linux 发行版确立的一套通用标准——应用开发者在 Linux 发行版上开发的代码将可以不加额外修改地运行在其他 Linux 发行版上。
并且也会解决 Linux 发行版的连续性问题:约定哪些(包括未来的)版本和特定的 Linux 标准基的版本保持兼容。
LSB 会有一整套规格说明、文档和工具,来检测兼容性。

LSB 可以作为自己的 OS 平台的产品战略。
Linux 标准基官网

Poky 实践

Yocto Project Quick Build
按照链接的指引去操作,当执行完:

$ bitbake core-image-sato

以后,就能生成 core-image-sato,然后运行:

$ runqemu qemux86-64

就能跑了。
但是,这并没有什么用。
因为现在还不知道 bitbake 是怎么接收并差异化 core-image-sato 这个参数的。
执行 bitbake 的时候到底发生了什么,也不知道。
链接后面还有很多内容,但是没有发现太多又有信息,唯一练习了的,就是打字和英文阅读。
不过,这是分析 Yocto 工程的一个比较合适的例程。

需要了解“构建系统结构”和“构建环境结构”

需要了解“元数据层结构”

.bb 文件 / .bbappend 文件 / .inc 文件是相辅相成的

元数据的组织

变量 & 可执行元数据

  • .conf,变量,底层会覆盖顶层
  • .bb,描述特定软件包以及如何构建该软件包:下载、解压、打补丁、编译、打包、安装指令
  • .bbclass,可以理解为函数集,里面的函数被 .bb 调用,通过 inherit 指令

core-image —— Linux 发行版蓝图

通过这个命令,可以找到当前的构建系统所支持的发布镜像
find ./meta*/recipes*/images -name “*.bb”

所有核心镜像菜谱都继承自 core-image 类;core-image 类继承自 image 类。


~
最后附上 Yocto 的主页:Yocto 官网
以下内容都不重要:


参考文档及若干概念

Yocto的介绍

Poky:Poky有两个含义。第一个含义是用来构建Linux的构建系统,值得注意的该Poky仅仅是一个概念,而非一个实体:它包含了 BitBake工具、编译工具链、BSP、诸多程序包或层,可以认为Poky即是Yocto的本质;此外Poky还有另外一层意思,使用Poky系统得到的默认参考 Linux 发行版也叫Poky(当然,我们可以对此发行版随意命名)。

Metadata 相关概念:

Recipes:.bb/.bbappend文件,配方文件,描述了从哪获取软件源码,如何配置,如何编译。
Class:.bbclass文件,包含在配方文件之间共享的有用信息。
Configuration:.conf文件,即配置文件,我们可以用它来改变构建方式。conf/bblayers.conf中可以添加用到的layer层路径,从而在编译时将他们添加进去。

Bitbake:一个任务执行引擎,用来解析并执行Metadata

install 命令

讲到构建,install 的命令一定是少不了的。
Linux 中的 Install命令

编译完了,需要把需要的东西打包。

《嵌入式Linux系统开发:基于 Yocto Project》笔记相关推荐

  1. 《信贷的逻辑与常识》笔记

    序 银行信贷风险管理的反思 现状与趋势 银行贷款的质量变化与经济周期.宏观调控政策等存在很高的相关性 现在银行不良贷款的增加主要是前几年经济快速增长时企业过度投资.银行过度放贷所带来的结果. 从历史情 ...

  2. AI公开课:19.02.27周逵(投资人)《AI时代的投资逻辑》课堂笔记以及个人感悟

    AI公开课:19.02.27周逵(投资人)<AI时代的投资逻辑>课堂笔记以及个人感悟 目录 课堂PPT图片 精彩语录 个人感悟 课堂PPT图片 精彩语录 更新中-- 文件图片已经丢失-- ...

  3. 人工智能入门算法逻辑回归学习笔记

    逻辑回归是一个非常经典的算法,其中也包含了非常多的细节,曾看到一句话:如果面试官问你熟悉哪个机器学习模型,可以说 SVM,但千万别说 LR,因为细节真的太多了. 秉持着精益求精的工匠精神不断对笔记进行 ...

  4. 【逻辑回归学习笔记】

    算法描述 1.逻辑回归要做的事就是寻找分界面实现二分类. 2.问题假设:对一堆三角形和正方形分类. 3.数据输入:已知正方形和三角形的坐标和标签. 4.算法过程: 知识储备 1.分类和回归 ①分类的目 ...

  5. 逻辑回归函数学习笔记

    继续逻辑回归学习,今日笔记记录. 1.逻辑回归和线性回归的关系:对逻辑回归的概率比取自然对数,则得到的是一个线性函数,推导过程如下. 首先,看逻辑回归的定义 其次,计算两个极端y/(1-y),其值为( ...

  6. 2.2 逻辑回归-机器学习笔记-斯坦福吴恩达教授

    逻辑回归 上一节我们知道,使用线性回归来处理 0/1 分类问题总是困难重重的,因此,人们定义了逻辑回归来完成 0/1 分类问题,逻辑一词也代表了是(1) 和 非(0). Sigmoid预测函数 在逻辑 ...

  7. LVM逻辑卷分区笔记

    磁盘的静态分区有其缺点:分区大小难评估,估计不准确,当分区空间不够用的时候,系统管理员可能需要先备份整个系统,清除磁盘空间,然后重新对磁盘进行分区,然后恢复磁盘数据到新分区,且需要停机一段时间进行恢复 ...

  8. 适合理工直男的钟平老师逻辑英语学习笔记

    一切的一切都只是套路!             --鲁迅 核心公式: En: (状语1) 主(定语1) 谓(状语2) (宾)(定语2) (状语1) Ch: (状语1) (定语1)主 (状语2)谓 (定 ...

  9. 【数字逻辑】学习笔记 第四章 Part2 常用组合逻辑电路与竞争、险象

    文章目录 一.常用组合逻辑电路 1. 译码器 (1) 二进制译码器 74LS138(3/8译码器) a. 一般符号和图形符号 b. 74LS138功能表 c. 两片 `74LS138` 构成 `4-1 ...

  10. 线性回归、逻辑回归学习笔记

    学习源代码 import numpy as np import matplotlib.pyplot as plt def true_fun(X): # 这是我们设定的真实函数,即ground trut ...

最新文章

  1. editplus 配置 golang 开发调试环境
  2. hadoop 1.2.1 安装步骤 伪分布式
  3. Linux 查看并删除.svn目录
  4. Python编程基础:第十节 while循环While Loops
  5. NP-Hard问题及组合最优化问题
  6. 直击「神策 2021 数据驱动大会」五大论坛,精彩不断
  7. linux下c语言编程gedit,Ubuntu Linux下实现Gedit支持NesC语法高亮
  8. Django与jQuery通信;Django前后端传值
  9. Bootstrap 折叠插件Collapse 选项
  10. linux rabbitmq 远程登录
  11. 华为云Volcano:让企业AI算力像火山一样爆发
  12. FreeMarker MyEclipse IDE
  13. linux mysql 2003_如何解决linux mysql2003错误
  14. 进阶版启动jupyterlab教程
  15. Axure Rp8激活码
  16. 基于AP6212实现 Airkiss NDK编程
  17. 车企销量“期中考”结束之后,新能源们下半年会持续高光吗?
  18. HTML 5入门基础
  19. 织梦dedecms 内容管理系统模板标签代码参考
  20. AndroidAPI

热门文章

  1. 爬取双色球的中奖号码
  2. 谷歌浏览器那些有趣的隐藏功能
  3. iOS App 安装包瘦身指南
  4. Linux上vim编辑器使用教程
  5. 云场景实践研究第50期:咕咚
  6. HTML5 代码规范
  7. Bootstrap下拉菜单
  8. Web项目实现前端锁屏功能
  9. PHP和Python该如何抉择?现在PHP还有前景吗?
  10. 常用网址收藏 (zz)