Yocto开发流程参考
主流嵌入式开发yocto搭建参考
·
- 环境搭建
1.1 系统要求
- 操作系统:Ubuntu 20.04/22.04 LTS(推荐)
- 内存:至少8GB RAM,推荐16GB以上
- 硬盘空间:至少100GB可用空间
- 网络:稳定的互联网连接
1.2 安装依赖包
sudo apt update
sudo apt upgrade
sudo apt install -y gawk wget git-core diffstat unzip texinfo gcc-multilib \
build-essential chrpath socat cpio python3 python3-pip python3-pexpect \
xz-utils debianutils iputils-ping python3-git python3-jinja2 libegl1-mesa \
libsdl1.2-dev pylint3 xterm python3-subunit mesa-common-dev \
zstd liblz4-tool
1.3 配置Git
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
1.4 下载Yocto项目
# 创建工作目录
mkdir -p ~/yocto/workspace
cd ~/yocto/workspace
# 克隆poky(Yocto核心)
git clone git://git.yoctoproject.org/poky -b kirkstone
# 克隆meta-openembedded(额外的层)
git clone git://git.openembedded.org/meta-openembedded -b kirkstone
# 克隆meta-qt6(如果需要Qt支持)
git clone https://github.com/meta-qt6/meta-qt6.git -b kirkstone
1.5 初始化构建环境
cd ~/yocto/workspace/poky
# 初始化环境
source oe-init-build-env
1.6 配置构建环境
编辑 conf/local.conf 文件:
# 设置目标机器
MACHINE ?= "qemux86-64"
# 设置并行编译
BB_NUMBER_THREADS = "8"
PARALLEL_MAKE = "-j 8"
# 启用镜像压缩
IMAGE_FSTYPES += "tar.bz2"
# 设置包管理器
PACKAGE_CLASSES ?= "package_rpm"
# 启用本地镜像缓存
DL_DIR ?= "${TOPDIR}/downloads"
SSTATE_DIR ?= "${TOPDIR}/sstate-cache"
2. 系统镜像编译
2.1 选择镜像类型
Yocto提供多种预定义镜像类型:
| 镜像类型 | 描述 | 大小 |
|---|---|---|
| core-image-minimal | 最小系统镜像 | ~50MB |
| core-image-base | 基础系统镜像 | ~100MB |
| core-image-sato | 带GUI的镜像 | ~500MB |
| core-image-full-cmdline | 完整命令行镜像 | ~200MB |
2.2 编译镜像
# 编译最小镜像
bitbake core-image-minimal
# 编译带GUI的镜像
bitbake core-image-sato
# 编译完整命令行镜像
bitbake core-image-full-cmdline
2.3 编译过程监控
# 查看编译进度
bitbake -e | grep ^PARALLEL_MAKE
# 查看任务状态
bitbake -g core-image-minimal && cat pn-buildlist | grep -v "native"
# 查看错误日志
bitbake -v core-image-minimal 2>&1 | tee build.log
2.4 验证编译结果
# 查看生成的镜像
ls -la tmp/deploy/images/qemux86-64/
# 运行QEMU模拟器
runqemu qemux86-64 core-image-minimal
3. 添加Recipe
3.1 Recipe基本结构
# myapp_1.0.bb
SUMMARY = "My Application"
DESCRIPTION = "A simple example application"
HOMEPAGE = "https://example.com"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://LICENSE;md5=xxxxxxx"
SRC_URI = "git://github.com/user/myapp.git;branch=main;protocol=https"
SRCREV = "v1.0"
S = "${WORKDIR}/git"
inherit cmake
DEPENDS = "boost openssl"
do_install() {
install -d ${D}${bindir}
install -m 0755 ${B}/myapp ${D}${bindir}/
}
3.2 创建自定义层
# 创建层结构
mkdir -p meta-mylayer/recipes-myapp/myapp
cd meta-mylayer
# 创建层配置文件
cat > conf/layer.conf << EOF
# We have a conf and classes directory, add to BBPATH
BBPATH .= ":${LAYERDIR}"
# We have recipes-* directories, add to BBFILES
BBFILES += "${LAYERDIR}/recipes-*/*/*.bb \
${LAYERDIR}/recipes-*/*/*.bbappend"
BBFILE_COLLECTIONS += "mylayer"
BBFILE_PATTERN_mylayer = "^${LAYERDIR}/"
BBFILE_PRIORITY_mylayer = "10"
LAYERDEPENDS_mylayer = "core"
EOF
3.3 添加层到构建配置
# 在build/conf/bblayers.conf中添加
BBLAYERS ?= " \
/path/to/poky/meta \
/path/to/poky/meta-poky \
/path/to/meta-openembedded/meta-oe \
/path/to/meta-mylayer \
"
3.4 常见Recipe类型
3.4.1 CMake项目
inherit cmake
EXTRA_OECMAKE = "-DCMAKE_BUILD_TYPE=Release"
3.4.2 Autotools项目
inherit autotools
3.4.3 Python项目
inherit setuptools3
3.4.4 内核模块
inherit module
4.5 调试Recipe
# 清理recipe
bitbake -c clean myapp
# 重新下载源码
bitbake -c fetch -f myapp
# 解压源码
bitbake -c unpack myapp
# 打补丁
bitbake -c patch myapp
# 配置
bitbake -c configure myapp
# 编译
bitbake -c compile myapp
# 安装
bitbake -c install myapp
4. 常见问题与解决方案
4.1 编译错误
问题:依赖包缺失
# 解决方案
bitbake -c cleanall <recipe-name>
bitbake <recipe-name>
问题:网络连接问题
# 解决方案:设置代理
export http_proxy=http://proxy.example.com:8080
export https_proxy=http://proxy.example.com:8080
# 或在local.conf中添加
PREMIRRORS ??= "\
bzr://.*/.* http://downloads.yoctoproject.org/mirror/sources/ \n \
cvs://.*/.* http://downloads.yoctoproject.org/mirror/sources/ \n \
git://.*/.* http://downloads.yoctoproject.org/mirror/sources/ \n \
gitsm://.*/.* http://downloads.yoctoproject.org/mirror/sources/ \n \
hg://.*/.* http://downloads.yoctoproject.org/mirror/sources/ \n \
osc://.*/.* http://downloads.yoctoproject.org/mirror/sources/ \n \
p4://.*/.* http://downloads.yoctoproject.org/mirror/sources/ \n \
svk://.*/.* http://downloads.yoctoproject.org/mirror/sources/ \n \
svn://.*/.* http://downloads.yoctoproject.org/mirror/sources/ \n"
4.2 性能优化
# 在local.conf中添加
# 启用ccache
INHERIT += "ccache"
CCACHE_DIR = "${TMPDIR}/ccache"
# 设置磁盘缓存
BB_DISKMON_DIRS ??= "\
STOPTASKS,${TMPDIR},1G,100K \
STOPTASKS,${DL_DIR},1G,100K \
STOPTASKS,${SSTATE_DIR},1G,100K \
STOPTASKS,/tmp,100M,100K \
ABORT,${TMPDIR},100M,1K \
ABORT,${DL_DIR},100M,1K \
ABORT,${SSTATE_DIR},100M,1K \
ABORT,/tmp,10M,1K"
4.3 镜像定制
# 添加自定义包到镜像
IMAGE_INSTALL_append = " myapp myutils"
# 移除默认包
IMAGE_FEATURES_remove = "x11-base"
# 设置root密码
INHERIT += "extrausers"
EXTRA_USERS_PARAMS = "usermod -P root root;"
4.4 调试技巧
# 查看recipe变量
bitbake -e <recipe-name> | grep VAR_NAME
# 查看依赖关系
bitbake -g <recipe-name>
cat pn-depends.dot | grep -v "native"
# 查看文件路径
bitbake -c listtasks <recipe-name>
总结
Yocto Project是一个强大的嵌入式Linux构建系统,通过本文档的介绍,您应该能够:
- 成功搭建Yocto开发环境
- 编译系统镜像
- 使用开发工具提高效率
- 创建和管理自定义Recipe
随着经验的积累,您可以进一步探索Yocto的高级特性,如:
- 多配置构建
- SDK生成
- 自动化测试
- 持续集成
更多资源:
更多推荐
所有评论(0)