Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute git describe in custom_target

Tags:

cmake

I would like to write output of git describe as a string to a file, so I can embed the information in my binary (C++). This has to work across platforms.

The best I can yet come up with was:

add_custom_target( SubmarineGitVersion
    COMMAND cmd /c "${CMAKE_EXECUTABLE}" echo czstring GIT_VERSION = STRINGIFY\( > "${CMAKE_CURRENT_BINARY_DIR}/GitVersion.hpp"
    COMMAND cmd /c "${GIT_EXECUTABLE}" describe --tags --always >> "${CMAKE_CURRENT_BINARY_DIR}/GitVersion.hpp"
    COMMAND cmd /c "${CMAKE_EXECUTABLE}" echo \) >> "${CMAKE_CURRENT_BINARY_DIR}/GitVersion.hpp"
)

This roughly works on Windows (is missing a ; at the end):

czstring GIT_VERSION = STRINGIFY(
tag-343434
)

Is there any better/more cross-platform way of doing this?

like image 465
abergmeier Avatar asked Aug 08 '16 07:08

abergmeier


People also ask

How do you describe what git is?

Git is a DevOps tool used for source code management. It is a free and open-source version control system used to handle small to very large projects efficiently. Git is used to tracking changes in the source code, enabling multiple developers to work together on non-linear development.

How do you describe a commit?

4 : to pledge to do some particular thing When asked if he would volunteer, he wouldn't commit himself.

Which command will have a human readable name to go with commit?

The result is a "human-readable" object name which can also be used to identify the commit to other git commands. By default (without --all or --tags) git describe only shows annotated tags. For more information about creating annotated tags see the -a and -s options to git-tag[1].


1 Answers

Common way for create "version" files is using configure_file command. Such way file will be created at configure stage:

GitVersion.hpp.in:

czstring GIT_VERSION = STRINGIFY(
${GIT_REPO_VERSION}
)

CMakeLists.txt:

# Store version into variable
execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --always
    OUTPUT_VARIABLE GIT_REPO_VERSION)
# The variable will be used when file is configured
configure_file("GitVersion.hpp.in" "GitVersion.hpp")

If you want to create version file on build stage, move above cmake commands into some file, and execute this file in CMake script mode:

generate_version.cmake:

# Git executable is extracted from parameters.
execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --always
    OUTPUT_VARIABLE GIT_REPO_VERSION)
# Input and output files are extracted from parameters.
configure_file(${INPUT_FILE} ${OUTPUT_FILE})

CMakeLists.txt:

add_custom_target( SubmarineGitVersion
    COMMAND ${CMAKE_COMMAND}
        -D GIT_EXECUTABLE=${GIT_EXECUTABLE}
        -D INPUT_FILE=${CMAKE_CURRENT_SOURCE_DIR}/GitVersion.hpp.in
        -D OUTPUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/GitVersion.hpp
        -P ${CMAKE_CURRENT_SOURCE_DIR}/generate_version.cmake
)
like image 190
Tsyvarev Avatar answered Oct 09 '22 21:10

Tsyvarev