如何使用 git 信息动态创建文件并将其包含在图像中并将其保存在构建系统中

How to dynamical create a file with git info and include it in the the image and save it on build system

我们有几个开发人员在处理一个项目。我们关注的领域(我们经常修改)是内核、我们的自定义代码和 yocto space 本身。

我们想在过程中的某个时刻(do_fetch 或 do_install?)创建一个文件,其中包含有关正在构建的内容的信息。例如上面每个回购的 git 分支名称和哈希。然后我们将该文件(或多个文件,如果需要)安装到图像上,并将其存档在中央服务器上。

我知道其中一些信息在构建历史中可用,但我不确定在我们要安装和打包时它是否存在。

获取分支和散列应该很容易通过配方函数中的 shell 命令获得。

在我开始破解之前,我想问一下是否有标准的方法来做类似的事情。

谢谢!

如果您需要包含自定义信息。一个很好的方法是创建一个自定义层 bbclass,定义如下:

DEPENDS += "git-native"

do_rootfs_save_versions() {
    #Do custom tasks here like getting layer names and linked SHA numbers
    #Store these information in a file and deploy it in ${DEPLOY_DIR_IMAGE}
}

ROOTFS_POSTPROCESS_COMMAND += "do_rootfs_save_versions;"

然后,在图像文件中包含 bbclass

IMAGE_CLASSES += "<bbclass_name>"

当你想确定目标层 version/image 名称/.. 运行 时非常有用。

好的,这就是我所做的。

添加了附加到 do_install 我想跟踪的函数并将它们放在构建目录的顶部:

do_install_append () {
 echo ${SRCPV} > ${TOPDIR}/kernel_manifest.txt
 git rev-parse --abbrev-ref HEAD >> ${TOPDIR}/kernel_manifest.txt
}

在我们的元目录中添加了一个新的 bbclass:

DEPENDS += "git-native"

do_rootfs_save_manifests[nostamp] = "1"
do_rootfs_save_manifests() {

    date  > ${TOPDIR}/buildinfo.txt
    hostname >> ${TOPDIR}/buildinfo.txt
    git config user.name >> ${TOPDIR}/buildinfo.txt
    cp ${TOPDIR}/buildinfo.txt ${IMAGE_ROOTFS}/usr/custom_space/

    if [ ! -f ${TOPDIR}/kernel_manifest.txt ]; then
       echo "kernel_manifest empty:  Rebuild or run cleanall on it's recipe" >    ${TOPDIR}/error_kernel_manifest.txt
       cp  ${TOPDIR}/error_kernel_manifest.txt ${IMAGE_ROOTFS}/usr/custom_space/
    else
       cp  ${TOPDIR}/kernel_manifest.txt ${IMAGE_ROOTFS}/usr/custom_space/
       if [ -f ${TOPDIR}/error_kernel_manifest.txt ]; then
           rm ${TOPDIR}/error_kernel_manifest.txt
       fi

    fi

    if [ ! -f ${TOPDIR}/buildhistory/metadata-revs ]; then
       echo " metadata_revs empty:  Make sure INHERIT += \"buildhistory\" and" > ${TOPDIR}/error_yocto_manifest.txt
       echo " BUILDHISTORY_COMMIT = "1" are in your local.conf " >> ${TOPDIR}/error_yocto_manifest.txt
       cp  ${TOPDIR}/error_yocto_manifest.txt ${IMAGE_ROOTFS}/usr/custom_space/

    else
       if [ -f ${TOPDIR}/error_yocto_manifest.txt ]; then
           rm ${TOPDIR}/error_yocto_manifest.txt
       fi
       cp  ${TOPDIR}/buildhistory/metadata-revs ${TOPDIR}/yocto_manifest.txt
       cp  ${TOPDIR}/buildhistory/metadata-revs ${IMAGE_ROOTFS}/usr/custom_space/yocto_manifest.txt
    fi

}

ROOTFS_POSTPROCESS_COMMAND += "do_rootfs_save_manifests;"

将以下行添加到我们想要使用该过程的图像配方中:

IMAGE_CLASSES += "manifest"
inherit ${IMAGE_CLASSES}

感谢您的帮助!