Mikhail Mikhail - 2 months ago 13
Git Question

How to force re-run of cmake, or add functions to makefile through CMakeLists.txt

I'd like to keep very close track of what version of my code I'm using for documentation purposes. Therefore I'd like to display the version of code I have compiled every time I run my program..

I've been using this previous question as reference.

How can I pass git SHA1 to compiler as definition using cmake?

I am using the two files that Ryan Pavlik suggested from his github repo, seen below:
https://github.com/rpavlik/cmake-modules/blob/master/GetGitRevisionDescription.cmake.in#L13
https://github.com/rpavlik/cmake-modules/blob/master/GetGitRevisionDescription.cmake

And I have the following:

CMakeLists.txt

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/")
include(GetGitRevisionDescription)
get_git_head_revision(GIT_REFSPEC GITHASH)

set(test_GITHASH "\"${GITHASH}\"")

include_directories("${PROJECT_BINARY_DIR}")
configure_file ("${PROJECT_SOURCE_DIR}/testgithash.h.in"
"${PROJECT_BINARY_DIR}/testgithash.h" )


testgithash.h.in

#define test_GITHASH @test_GITHASH@


So far I have my final executable displaying the hash from my testgithash.h file that is generated, but that header file is only refreshed when I run cmake. So really my question is, does anyone know of way that I can force cmake to re-run when I run make if my hash has changed? Or is there a way I can put the git hash functionality I'm looking for directly into my generated makefile rather than in my CMakeLists.txt?

Thanks in advance for any help!

Answer

As far as I've been able to figure out, the only way to force CMake to be rerun on some external condition is to have it include a file whose timestamp has been modified. Something like this:

# Somewhere in your CMakeLists.txt
include(external.stamp)

Then CMake will be rerun when external.stamp is updated.

In your case, maybe you could have a post-commit hook in git touch an (empty) file when a commit is made, then include that in your CMakeLists.txt.