Last active
August 20, 2018 13:29
-
-
Save m-ou-se/c4ac8a8fb8c9cefb12cc27dbae210581 to your computer and use it in GitHub Desktop.
Automatically include git version in software.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include_directories(${CMAKE_CURRENT_BINARY_DIR}/generated) | |
#... | |
add_custom_target(version | |
COMMAND ${CMAKE_COMMAND} | |
ARGS | |
-DOUTPUT=${CMAKE_CURRENT_BINARY_DIR}/generated/version.h | |
-P ${CMAKE_CURRENT_SOURCE_DIR}/gen-version.cmake | |
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} | |
BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/generated/version.h | |
) | |
add_dependencies(some-target version) | |
add_dependencies(maybe-some-other-target version) | |
# ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
execute_process(COMMAND | |
git describe --always --dirty=-modified | |
OUTPUT_VARIABLE ver | |
OUTPUT_STRIP_TRAILING_WHITESPACE | |
) | |
get_filename_component(DIR ${OUTPUT} DIRECTORY) | |
file(MAKE_DIRECTORY ${DIR}) | |
file(WRITE ${OUTPUT}.new "#ifndef VERSION\n#define VERSION \"${ver}\"\n#endif\n") | |
execute_process( | |
COMMAND | |
${CMAKE_COMMAND} -E copy_if_different | |
${OUTPUT}.new ${OUTPUT} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment