寻找一种方法来存储从git repo编译的二进制文件中的版本 [英] Looking for a way to store versions in a binary compiled from git repo

查看:231
本文介绍了寻找一种方法来存储从git repo编译的二进制文件中的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些提示来实现 binary --version ,它可以提供关于其编译版本的良好信息。



该项目使用 autotools 构建系统,并存储在充当SVN前端的git仓库中。



我想在二进制文件里面有:
$ b $ ul
编译时间

  • 作为基础的SVN提交
  • 最后提交git提交ID和时间

  • 如果可能,影响此特定二进制文件的最后提交
  • >

    解决方案

    您可能需要编写源代码以使用#defined常量版本字符串。然后,您可以通过 -DMY_VERSION = ... 选项将其传入。这可以让你在代码中嵌入一个默认值,包装在 #ifndef 中,以防万一!

      #ifndef MY_VERSION 
    #define MY_VERSION 0.0.1-alpha
    #endif

    print_version(){
    printf (我的产品:%s \\\
    ,MY_VERSION);

    在构建过程中处理这个问题的一个好方法是进行中间构建产品,它只是一个makefile片段,如 MY_VERSION =...。这再次增加了冗余性,让您可以使用已创建的版本文件分发项目,以便构建不必依赖于SCM的存在。



    您然后可以创建版本字符串,例如:

      echo -n'MY_VERSION =''> VERSION_FILE 
    git describe>> VERSION_FILE
    echoCompiled on $(date)>> VERSION_FILE
    ...
    echo''>>然后在您的主makefile中包含该代码段,然后添加 -DMY_VERSION ='$(MY_VERSION)'添加到相应对象的构建标记。



    稍微改变一下:版本字符串,然后将该值放入makefile中的相应变量中。



    如果您需要特定git命令获得所需输出的帮助,请随时发表评论。 git describe 是一个很好的例子,但它的意思正是这种事情。默认输出是当前提交最接近的标记祖先,连字符,自标记以来提交的次数,连字符和缩写提交哈希。


    I'm looking for some tips to implement binary --version that would provide good information about the version it was compiled from.

    The project is using autotools build system, and is stored in a git repo that acts as a SVN frontend.

    What I would like to have inside the binary is:

    • compilation time
    • SVN commit that acts as base
    • last git commit ID and time
    • if possible the last commit that affects this specific binary

    解决方案

    You'll probably want to write your source code to use a #defined constant version string. You can then pass that in through your build with a -DMY_VERSION=... option. That'll let you embed a default value in the code, too, wrapped in an #ifndef, just in case!

    #ifndef MY_VERSION
    #define MY_VERSION 0.0.1-alpha
    #endif
    
    print_version() {
        printf("my product: %s\n", MY_VERSION);
    }
    

    A nice way to handle this on the build process side to make an intermediate build product which is simply a makefile snippet like MY_VERSION = "...". This again adds redundancy by letting you distribute the project with the version file already created, so that the build doesn't have to depend on the presence of the SCM.

    You can then create the version string however you like, for example:

    echo -n 'MY_VERSION = "' > VERSION_FILE
    git describe >> VERSION_FILE
    echo "Compiled on $(date)" >> VERSION_FILE
    ...
    echo '"' >> VERSION_FILE
    

    Then in your primary makefile, include that snippet, and add -DMY_VERSION='"$(MY_VERSION)"' to the build flags for the appropriate object.

    A slight variation: make your generated file purely the version string, then pull that value into the appropriate variable in the makefile.

    If you need help with specific git commands to get the desired output, feel free to comment. git describe is a great one, though, meant for exactly this kind of thing. The default output is the closest tag ancestor of the current commit, hyphen, number of commits since the tag, hyphen, and abbreviated commit hash.

    这篇关于寻找一种方法来存储从git repo编译的二进制文件中的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆