JoeAC
JoeAC

Reputation: 922

Include Git commit hash and/or branch name in C/C++ source

I would like to know how you can get a Git commit hash and/or other information into the contents of a C++ variable in the compiled binary without having it be part of the source that's tracked by Git.

I need to keep track of firmware release information in a compiled executable running on an embedded processor. Means to identify the particular release of the firmware binary such as meaningful filenames, MD5 checksums or even date/time stamps are not available in this closed environment (i.e., there is no filesystem).

One approach is to have the device's console output produce identifying text, such as 'Release 1.2.3', 'commit hash 1bc123...', or similar. The firmware release information is only of interest to maintenance personnel, so a trained operator could examine the console output. To implement this it could potentially involve manual editing of a version string, which is then compiled into the code and output to the console at program startup.

This is acceptable for major releases where a signoff workflow is used to double-check that the version information is correct. However, this being a manual process it is inherently unreliable. For example, what if the developer forgets to update the version information? - There is now a disconnect between the compiled code and its reported version string.

A workflow where the code is freshly compiled and downloaded each time the user wants to test the hardware is not practical in the situation in question, ie., it is quite onerous to update the firmware.

An automatic way of identifying the version of the code is thus required. In the situation in question, Git is used, and the developers regularly commit their work to feature branches. Displaying the Git commit hash, and perhaps also whether or not there are unstaged changes, would be a way of identifying the status of the source code used to compile the firmware.

The requirement is that I would like my application to have information available to it so that it is able to display: "Git commit:[01abcdef...etc], branch: experimentalStuffDoNotRelease"

Thus, I would like to automatically include Git information, such as commit hash and branch, in the compiled C and/or C++ code.

The development environment has developers using both Windows and Linux, and uses Eclipse CDT with a relatively unsophisticated workflow of: check out; compile; download to the hardware.

Upvotes: 13

Views: 15386

Answers (6)

Richard
Richard

Reputation: 61289

I use a makefile, like so:

GIT_HASH=$(git rev-parse HEAD)
COMPILE_TIME=$(date -u +'%Y-%m-%d %H:%M:%S UTC')
GIT_BRANCH=$(git branch --show-current)
export VERSION_FLAGS=-DGIT_HASH="\"$(GIT_HASH)\"" -DCOMPILE_TIME="\"$(COMPILE_TIME)\"" -DGIT_BRANCH="\"$(GIT_BRANCH)\""

all:
    g++ main.cpp $(VERSION_FLAGS)

When the makefile is run, the git hash and the time of compilation are both loaded into macros accessible within the source, like so:

#include <iostream>

int main(){
  std::cerr<<"hash="<<GIT_HASH<<", time="<<COMPILE_TIME<<", branch="<<GIT_BRANCH<<std::endl; 
}

Which gives output like:

hash=35f531bf1c959626e1b95f2d3e1a7d1e4c58e5ec, time=2017-05-18 04:17:25 UTC, branch=master

Upvotes: 22

rightaway717
rightaway717

Reputation: 2831

Based on @Richard's answer and my fixes for qmake/Qt, here is the solution I use: With Qt 5.14 the following lines in .pro file work for me:

GIT_HASH="\\\"$$system(git -C \""$$_PRO_FILE_PWD_"\" rev-parse --short HEAD)\\\""
GIT_BRANCH="\\\"$$system(git -C \""$$_PRO_FILE_PWD_"\" rev-parse --abbrev-ref HEAD)\\\""
BUILD_TIMESTAMP="\\\"$$system(date -u +\""%Y-%m-%dT%H:%M:%SUTC\"")\\\""
DEFINES += GIT_HASH=$$GIT_HASH GIT_BRANCH=$$GIT_BRANCH BUILD_TIMESTAMP=$$BUILD_TIMESTAMP

In your code you can check the revision like this:

int main(int argc, char *argv[])
{
    QStringList args;
    for (int i = 0; i < argc; i++)
        args << QString(argv[i]);

    if (args.contains("-v") || args.contains("--version")) {
        std::cout << QString("branch: %1, version: %2, built_at: %3").arg(GIT_BRANCH).arg(GIT_HASH).arg(BUILD_TIMESTAMP).toUtf8().constData() << std::endl;
        return 0;
    }
    // ...
}

Upvotes: 0

JoeAC
JoeAC

Reputation: 922

In Eclipse CDT, use a pre-build step to generate an include file containing the relevant information, and a conditional inclusion to check that the file was created:

  1. Right-click the project

  2. Select Properties

  3. Expand the C/C++ Build

  4. Select Settings In the Build Steps tab

  5. Enter the following in the Command text box:

      git log --pretty=format:'#define GIT_INFO_PRESENT%n static const char* GIT_INFO = "Version Information=[%H,%d]\r\n";' -n 1 > ../src/gitcommit.h
    

    This will, upon build, create a file gitcommit.h that will be included in the source code. To customise it, adjust the string to your needs. (See https://git-scm.com/docs/pretty-formats)

As an example, I produce a debug output at the beginning of the main routine to inform the user of the commit and branch (not strictly needed knowing the commit, but certainly helpful):

  1. Put this in the file, probably at the top

     #if __has_include("gitcommit.h")
     #include "gitcommit.h"
     #else
     static const char* GIT_INFO = "Git version information not present.\r\n";
     #endif
    
  2. To display the information somewhere in your code, do similar to this:

     printf(GIT_INFO);
    

Note that I haven't, in this case, made the pre-build step a shell script or Windows/DOS .bat file, as I work often in Linux or Windows.

Note that this isn't tested in Windows.

In both cases, 'git' must be executable from the standard command line.

There is a dependency on provision of __has_include. This was intended to provide simplicity so that a default include file need not be provided.

Note that the gitcommit.h file's path should be discoverable by the compiler.

Upvotes: 8

A C file or a C++ file can be generated by some program (for example, some shell script on Linux, or some GNU awk script, or your C program running system or popen(3) running some git command).

You just configure your build automation tool (e.g. your Makefile if you use GNU make, or your build.ninja file if you use ninja) appropriately.

Both Bismon and RefPerSys are doing so and keep their git id inside the obtained executable (displaying it when invoked with --version). Note: both are projects I created.

PS a different question is how to configure your IDE to run a particular build automation tool. This is specific to your IDE. But Eclipse CDT FAQ offers an insight.

Upvotes: -2

Andrey Starodubtsev
Andrey Starodubtsev

Reputation: 5292

Here's simple solution if you need git hash and local modifications flag only:

$ cat update-version-info.sh

#!/bin/sh
version=$(git describe --always --dirty --tags)
echo "#define GIT_VERSION \"$version\"" > git-version.h

$ cat 1.c

#include <stdlib.h>
#include <stdio.h>

#include "git-version.h"

int main() {
#ifdef GIT_VERSION
    printf("%s\n", GIT_VERSION);
#endif
    return 0;
}

$ ./1

ee4f307-dirty

Upvotes: 1

Pavel P
Pavel P

Reputation: 16843

Usually as part of your build you run some command that generates something like that. For example, git describe gives you something that you could use:

echo // auto generated version: > version.h
git describe > echo // auto generated version: > version.h
echo -e "#define VERSION " >> version.h
git describe >> version.h

For example x264 use this simple script to generate it:

if [ -d .git ] && command -v git >/dev/null 2>&1 ; then
    localver="$(($(git rev-list HEAD | wc -l)))"
    if [ "$localver" -gt 1 ] ; then
        ver_diff="$(($(git rev-list origin/master..HEAD | wc -l)))"
        ver="$((localver-ver_diff))"
        echo "#define X264_REV $ver"
        echo "#define X264_REV_DIFF $ver_diff"
        if [ "$ver_diff" -ne 0 ] ; then
            ver="$ver+$ver_diff"
        fi
        if git status | grep -q "modified:" ; then
            ver="${ver}M"
        fi
        ver="$ver $(git rev-list -n 1 HEAD | cut -c 1-7)"
        version=" r$ver"
    fi
fi

echo "#define X264_VERSION \"$version\""
echo "#define X264_POINTVER \"0.$ver\""

This will generate something like:

#define X264_VERSION  " r2708 86b7198"
#define X264_POINTVER "0.148.2708 86b7198"

Upvotes: 3

Related Questions