Mikhail Mikhail - 1 year ago 203
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:

And I have the following:


get_git_head_revision(GIT_REFSPEC GITHASH)

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

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


#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 Source

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

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download