Skip to content

Instantly share code, notes, and snippets.

@thomasfaingnaert
Created April 6, 2022 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasfaingnaert/e60159b58002ccf586d5b2a49120ef5c to your computer and use it in GitHub Desktop.
Save thomasfaingnaert/e60159b58002ccf586d5b2a49120ef5c to your computer and use it in GitHub Desktop.
Using git version in cmake

Using git version in cmake

include/version.h.in

#define VERSION_STRING "@VERSION_STRING@"

cmake/generate_version.cmake

execute_process(COMMAND git describe --always --dirty=+
    OUTPUT_VARIABLE VERSION_STRING
    OUTPUT_STRIP_TRAILING_WHITESPACE)

configure_file(${VERSION_INPUT} ${VERSION_OUTPUT})

CMakeLists.txt

add_custom_target(generate-version
    COMMAND ${CMAKE_COMMAND}
            -D VERSION_INPUT=${CMAKE_SOURCE_DIR}/include/version.h.in
            -D VERSION_OUTPUT=${CMAKE_BINARY_DIR}/include/version.h
            -P ${CMAKE_SOURCE_DIR}/cmake/generate_version.cmake)

add_dependencies(<main target> generate-version)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment