如何在BitBake中拉取不同目录的多个Git仓库?
背景信息
原文件结构:
main/ ├── recipes/ ├── folder1/ ├── folder2/ | └── files/ | ├── file1.json | └── file2.json └── recipe.bb
原配方recipe.bb内容:
SRC_URI = "<git-repo1>;protocol=ssh;branch=master; \ file://folder2/files " SRCREV = "<commit-hash>" PV = "0.1.2+git${SRCPV}"
原配置可正常拉取git-repo1,使用其中的CMakeLists.txt和本地files目录下的json文件。
现在需要将folder2/files迁移到新仓库git-repo2,通过BitBake拉取,尝试两种方法均失败。
尝试的方法及错误
方法1:修改原配方拉取两个仓库
修改后的文件结构:
main/ ├── recipes/ ├── folder1/ ├── folder2/ └── recipe.bb
修改后的配方:
SRC_URI = "<git-repo1>;protocol=ssh;branch=master;name=repo1 \ <git-repo2>;protocol=ssh;branch=main;name=repo2;subdir=folder2/ \ " SRCREV_FORMAT = "repo1_repo2" SRCREV_repo1 = "<commit-hash>" SRCREV_repo2 = "<commit-hash>" PV = "0.1.2+git${SRCPV}"
错误信息:
ERROR: project-0.1.2<git-repo1hash_repo2hash>-r1 do_configure: Execution of '/workdir/../project/0.1.2<git-repo1hash_repo2hash>-r1/temp/run.do_configure.835' failed with exit code 1:
CMake Error: The source directory "/workdir/.../project/0.1.2<git-repo1hash_repo2hash>-r1/git" does not appear to contain CMakeLists.txt.
方法2:新增子配方拉取新仓库
修改后的文件结构:
main/ ├── recipes/ ├── folder1/ ├── folder2/ | └── newrecipe.bb └── recipe.bb
新配方newrecipe.bb内容:
SRC_URI = "<git-repo2>;protocol=ssh;branch=main;" SRCREV = "<commit-hash>" PV = "1"
错误信息:
ERROR: project-1-r1 do_configure: Execution of '/workdir/.../project/1-r1/temp/run.do_configure.835' failed with exit code 1:
CMake Error: The source directory "/workdir/.../project/1-r1/git" does not appear to contain CMakeLists.txt.
问题原因
两种方法的核心问题都是源码目录结构不符合CMake的预期:
- 方法1中,
subdir=folder2/会将git-repo2拉到${WORKDIR}/folder2,但git-repo1的默认拉取目录${WORKDIR}/git未被正确保留,导致CMake找不到该目录下的CMakeLists.txt。 - 方法2中,新增配方是独立包,未与原配方建立依赖关系,编译新配方时自然找不到原仓库的CMakeLists.txt。
正确实现方法
方案1:在原配方中正确拉取两个仓库(推荐)
调整subdir参数,确保git-repo1拉到主源码目录(CMakeLists.txt所在位置),git-repo2拉到主目录下的folder2/files子目录:
SRC_URI = "<git-repo1>;protocol=ssh;branch=master;name=repo1 \ <git-repo2>;protocol=ssh;branch=main;name=repo2;subdir=git/folder2/files \ " SRCREV_FORMAT = "repo1_repo2" SRCREV_repo1 = "<commit-hash>" SRCREV_repo2 = "<commit-hash>" PV = "0.1.2+git${SRCPV}" # 明确指定源码目录为git-repo1的拉取目录,确保CMake定位到正确路径 S = "${WORKDIR}/git"
说明:
subdir=git/folder2/files表示将git-repo2的内容直接拉到${WORKDIR}/git/folder2/files,与原结构完全匹配。S = "${WORKDIR}/git"强制指定CMake的源码目录为git-repo1的拉取路径,避免路径混乱。
方案2:将新仓库作为独立配方,原配方依赖它
如果需要拆分配方,可通过依赖+文件复制的方式实现:
1. 新配方newrecipe.bb修改:
SRC_URI = "<git-repo2>;protocol=ssh;branch=main;" SRCREV = "<commit-hash>" PV = "1" # 指定文件安装路径 FILES_${PN} += "${datadir}/project/folder2/files/*" do_install() { install -d ${D}${datadir}/project/folder2/files cp -r ${S}/* ${D}${datadir}/project/folder2/files/ }
2. 原配方recipe.bb修改:
DEPENDS += "newrecipe" SRC_URI = "<git-repo1>;protocol=ssh;branch=master;" SRCREV = "<commit-hash>" PV = "0.1.2+git${SRCPV}" do_configure_prepend() { # 在配置前将新仓库的文件复制到原源码目录的对应位置 install -d ${S}/folder2/files cp -r ${STAGING_DATADIR}/project/folder2/files/* ${S}/folder2/files/ }
说明:
- 新配方将git-repo2的文件安装到系统staging目录,原配方通过
DEPENDS依赖新配方,确保文件已准备就绪。 do_configure_prepend在CMake配置前完成文件复制,保证原项目能找到所需的json文件。
内容的提问来源于stack exchange,提问作者Elneto Cibrian

