I’m working on a simulation system written in C++ that is managed by git. I use GNU make as build tool. To make simulation results reproducible git is very useful as you can go back to the exact version of the simulation program the results have been created with.
Currently the status and SHA1 of the git repository are determined programmaticaly at run time and written to a file together with the results. However, if the sources have been changed since compilation of the program, the status in my log file will not reflect the actual version of the program. Thus I’m looking for a way to determine the git status at compile time. Is there any chance to accomplish this?
One solution is to have your build system extract the value and have it generate some C++ header (or source file) with this value inside it.
For example, if using CMake, you can use the FindGit module to do something like:
Then, add the following
version.h.infile:CMake will replace the
@GIT_HEAD_HASH@string with the value it extracted usingget_tree_info().Then, from your regular code:
This is a simplified and absolutely untested example. If you take a look at the sources of the
FindGitCMake module, you’ll see that it just runs anexecute_process()command at build time to extract the information. You can modify it to extract anything based on invocation of the Git command-line interface.