This module allows you to obtain the current branch, sha1, short sha1 and dirty
flag directly within C++. It creates a static library called GitHash behind
the scenes which contains these values as symbols you can use.
It additionally creates a cache of the last generated hash (and whether it was dirty) so it avoids recompilation as long as the current hash is equal to the last compiled one.
To use GitHash in your project, you only need to follow these steps:
- Copy the file
GitHash.cmakein your project'scmake/Modulesfolder. - Copy the file
GitHash.hppto your project's include directory. - Add the following two lines to your main
CMakeLists.txtfile:include(${PROJECT_SOURCE_DIR}/cmake/Modules/GitHash.cmake) SetupGitHash() - Use these variables in your C++ files:
GitHash::branch; // C-string GitHash::sha1; // C-string GitHash::shortSha1; // C-string GitHash::dirty; // boolean - In CMake, use
target_link_librariesto link your project to the GitHash library (using the${GITHASH_LIBRARIES}variable).
Note that currently the GitHash library is outputted in a subfolder of your
PROJECT_BINARY_DIR. If this is not desired you will have to manually modify
the script to specify your ideal output directory.
It is possible to add additional fields to read (for example, to read tags). For each new field, you need to:
- Modify the
GitHash.hppheader file to expose the new field you want. - Add the new field, and the appropriate
gitcommand to obtain its value, in theGitHash.cmakescript. All modifications can be done briefly at the top of the CMake script:- Add a new CMake variable to
variablesToRead - Add a new
CMD_variable containing the appropriate command to run - Add a new
externfield (both declaration and definition) inside the string in thegetCppContentsfunction
- Add a new CMake variable to
The GitHash mechanism can be used more generally than git, since you could use
arbitrary commands to generate and expose arbitrary values. If you do so, you
may also want to change what is put in the cache so you can avoid recompilation
when your commands return the same values. To do so, you just have to change the
format of the cache file as returned by the genCache function.
This library was developed with the help of this blog post.