<div dir="ltr"><div dir="ltr"><div>Similar question with some suggestions in response:<br></div><div><br></div><div><a href="https://cmake.org/pipermail/cmake/2018-July/067887.html">https://cmake.org/pipermail/cmake/2018-July/067887.html</a></div><div><br></div><div>Best,</div><div>Isaiah<br></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Oct 10, 2018 at 5:05 PM Matt Schulte <<a href="mailto:schultetwin1@gmail.com">schultetwin1@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br>
<br>
I'd like to set a CMake variable to the current git commit short hash.<br>
This variable will be used as part of the version string for my<br>
project (ex: "1.0.1+git.${SHORT_HASH}"). I can get at this short hash<br>
by using execute_process and setting the resulting output to a<br>
variable.<br>
<br>
```cmake<br>
execute_process(<br>
COMMAND<br>
git rev-parse --short HEAD<br>
RESULT_VARIABLE<br>
SHORT_HASH_RESULT<br>
OUTPUT_VARIABLE<br>
SHORT_HASH)<br>
```<br>
<br>
My issue is that cmake will only run execute_process once, during the<br>
configure step. I need cmake to run this execute_process on every<br>
build and, if the output has changed, reconfigure to make sure<br>
SHORT_HASH is up to date.<br>
<br>
I came up with one solution to this issue: During the configure step,<br>
I can write the current short hash to a file named short_hash.txt. On<br>
every build, I'll re-compute the short hash and verify that the<br>
computed short hash is the same as what is in short_hash.txt. If its<br>
not, I'll write the new short hash to short_hash.txt. I then make<br>
short_hash.txt an input to configure_file. This will cause cmake to<br>
validate SHORT_HASH is properly set, and re-configure if its not.<br>
<br>
```cmake<br>
execute_process(<br>
COMMAND<br>
git rev-parse --short HEAD<br>
RESULT_VARIABLE<br>
SHORT_HASH_RESULT<br>
OUTPUT_VARIABLE<br>
SHORT_HASH)<br>
<br>
# If running in script mode (this runs on every build)<br>
if (CMAKE_SCRIPT_MODE_FILE)<br>
if (EXISTS "${SHORT_HASH_FILE}")<br>
file(READ ${SHORT_HASH_FILE} READ_IN_SHORT_HASH)<br>
else()<br>
set(READ_IN_SHORT_HASH "")<br>
endif()<br>
<br>
if (NOT ("${READ_IN_SHORT_HASH}" STREQUAL "${SHORT_HASH}"))<br>
message(STATUS "Short hash is out of date")<br>
# This will update short_hash.txt, causing cmake to reconfigure<br>
file(WRITE ${SHORT_HASH_FILE} ${SHORT_HASH})<br>
endif()<br>
<br>
# Else running as part of cmake configure<br>
else()<br>
set(SHORT_HASH_FILE ${CMAKE_CURRENT_BINARY_DIR}/short_hash.txt)<br>
file(WRITE ${SHORT_HASH_FILE} ${SHORT_HASH})<br>
<br>
# The trick here is to make sure short_hash.txt is listed as a byproduct<br>
add_custom_target(<br>
git_short_hash<br>
BYPRODUCTS<br>
${SHORT_HASH_FILE}<br>
COMMAND<br>
${CMAKE_COMMAND}<br>
"-DSHORT_HASH_FILE=${SHORT_HASH_FILE}"<br>
"-P" "${CMAKE_CURRENT_LIST_FILE}"<br>
COMMENT<br>
"Re-checking short hash..."<br>
VERBATIM<br>
USES_TERMINAL)<br>
<br>
# This configure_file makes cmake reconfigure dependent on short_hash.txt<br>
configure_file(${SHORT_HASH_FILE} ${SHORT_HASH_FILE}.junk COPYONLY)<br>
<br>
message(STATUS "Short Hash: ${SHORT_HASH}")<br>
endif()<br>
```<br>
<br>
This works great with cmake 3.12 and ninja 1.8.2! (I was really happy<br>
with how well it worked. I tip my hat to the cmake developers for<br>
this). However, it doesn't work with Makefiles, and causes ninja 1.7.2<br>
to get stuck in an infinite loop. On CMake 3.10 this will cause ninja<br>
1.8.2 to generate a warning about a loop.<br>
<br>
Has anyone run into this issue before and have a better solution? Or<br>
is trying to execute a command before cmake checks if it should<br>
reconfigure a hack that should never be done?<br>
<br>
Thanks for the help!<br>
Matt<br>
-- <br>
<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" target="_blank">http://www.cmake.org/Wiki/CMake_FAQ</a><br>
<br>
Kitware offers various services to support the CMake community. For more information on each offering, please visit:<br>
<br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/support.html</a><br>
CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/consulting.html</a><br>
CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/training.html</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="https://cmake.org/mailman/listinfo/cmake" rel="noreferrer" target="_blank">https://cmake.org/mailman/listinfo/cmake</a><br>
</blockquote></div>