vVersion.cmake文件内容如下:
#vversion.cmake
#vDateTime
string(TIMESTAMP vDateTime "%Y%m%d-%H%M%S")
#vProjectDir
execute_process(
COMMAND basename ${CMAKE_CURRENT_SOURCE_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE vProjectDir
OUTPUT_STRIP_TRAILING_WHITESPACE
)
#vGitBranch
execute_process(
COMMAND git rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE vGitBranch
OUTPUT_STRIP_TRAILING_WHITESPACE
)
#vGitCommit
execute_process(
COMMAND git log -1 --pretty=format:%h
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE vGitCommit
OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(vFileName "${CMAKE_CURRENT_SOURCE_DIR}/vversion.h")
file(WRITE ${vFileName} "
#ifndef VVERSION_H
#define VVERSION_H
" )
file(APPEND ${vFileName} "const char* vversion="[vov]:${vDateTime}:${vProjectDir}:${vGitBranch}:${vGitCommit}";")
file(APPEND ${vFileName} "
#endif")
使用方法:在CMakeList.txt中添加:
include(vVersion.cmake)