Looking for a way to store versions in a binary compiled from git repo

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.

  • Using git for distribution with autotools
  • Cause of change from `-arch ppc` to `-arch ppc7400`?
  • Compiling workflow with version control
  • How to handle subprojects with autotools?
  • Recommended way to use Autoconf Archive macros and other 3rd party macros
  • Which files generated by Autotools should I keep in version control repository?
  • 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

  • Merging: Hg/Git vs. SVN
  • How to keep track of origin/master in my dev git branch
  • SVN error: “Can't write to connection: An existing connection was forcibly closed by the remote host.”
  • “fatal: ref HEAD is not a symbolic ref” during interactive git rebase
  • Reading the first line of a file in Ruby
  • Git merge two local branches
  • 2 Solutions collect form web for “Looking for a way to store versions in a binary compiled from git repo”

    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.

    The VERSION_FILE (see other answer) needs to be marked as BUILT_SOURCES in Makefile.am too for it to be successfully used with myprog_SOURCES, in case you use automake.

    Git Baby is a git and github fan, let's start git clone.