2016-08-08 61 views
2

我想將git describe的輸出作爲字符串寫入文件,所以我可以將信息嵌入到我的二進制文件(C++)中。這必須跨平臺工作。在custom_target中執行git describe

我還沒有想出是最好的:

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" 
) 

這大致在Windows上運行(在末尾缺少;):

czstring GIT_VERSION = STRINGIFY(
tag-343434 
) 

有沒有更好/更跨平臺的方式做到這一點?

回答

1

創建「版本」文件的常用方法是使用configure_file命令。這種方式的文件將在配置創建階段:

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") 

如果你想創建版本文件build階段,將cmake命令移到某個文件上,然後執行該文件CMake的腳本模式

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 
) 
+0

就像一個魅力,謝謝。 – abergmeier