如何编写Yocto Recipe构建非默认目标架构的主机调试工具
Yocto同步编译主机侧工具的实现方案
不需要修改MACHINE变量,也不需要为了主机工具新建专门的MACHINE配置,Yocto原生提供了主机侧工具的构建能力,根据你的使用场景可以选择以下两种方案:
方案1:扩展原有recipe,构建目标固件时自动同步编译host_tool
适合每次构建固件都需要生成对应host_tool的场景:
- 在原有recipe开头添加配置,让bitbake自动生成目标架构、主机架构两个构建变体:
BBCLASSEXTEND = "native" - 修改
do_compile函数,区分构建场景执行不同编译目标:do_compile() { # 原有逻辑:编译ARM架构目标程序 oe_runmake # 仅在主机架构构建场景下编译host_tool if [ "${BUILD_ARCH}" = "${HOST_ARCH}" ]; then oe_runmake <host_tool> fi } - 修改
do_install函数,把host_tool安装到对应目录:do_install() { # 原有目标程序安装逻辑保留 oe_runmake install DESTDIR=${D} if [ "${BUILD_ARCH}" = "${HOST_ARCH}" ]; then install -d ${D}${bindir} install -m 0755 <host_tool> ${D}${bindir}/ fi } - 在image recipe或local.conf中添加依赖,让构建固件时自动编译host_tool:
DEPENDS += "你的recipe名称-native"
方案2:创建独立的native recipe作为单独构建目标
适合仅在需要时才手动编译host_tool的场景,不需要和固件构建绑定:
- 不需要新建单独的层,直接在现有自定义层的
recipes-devtools目录(或原有recipe所在的同级目录)下新建recipe,命名格式为你的recipe名称-native_<版本号>.bb - recipe内容示例:
# 继承native类,自动使用主机工具链构建 inherit native # 复用原有recipe的源码、补丁等配置 require 原有recipe的相对路径/你的recipe名称_${PV}.bb # 覆盖编译逻辑,仅编译host_tool do_compile() { oe_runmake <host_tool> } # 覆盖安装逻辑,仅安装host_tool do_install() { install -d ${D}${bindir} install -m 0755 <host_tool> ${D}${bindir}/ } - 需要构建时执行命令
bitbake 你的recipe名称-native即可,编译完成的工具默认会放在构建目录的tmp/work/<主机架构>/你的recipe名称-native/<版本号>/image路径下。
注意事项
- 禁止在recipe中覆盖MACHINE变量:MACHINE是用于定义目标嵌入式设备硬件配置的参数,和主机侧工具构建完全无关,私自修改会破坏原有固件的构建逻辑。
- 如果需要把host_tool打包到Yocto生成的SDK中供外部开发使用,在local.conf或SDK配置中添加
SDK_DEPENDS += "你的recipe名称-native"即可。 - 不要硬编码主机工具链参数:使用Yocto原生的native类会自动处理环境变量、工具链适配,避免出现兼容性问题。
内容的提问来源于stack exchange,提问作者nass
相关产品推荐
相关产品推荐

