- 首先,我定义
COMMIT_ID
变量:
execute_process(COMMAND git rev-parse HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE COMMIT_ID )
- 如果您指定
COMMIT_ID
变量,则不会构建项目:
add_custom_command(TARGET ${APP_NAME} POST_BUILD
WORKING_DIRECTORY
$<TARGET_FILE_DIR:${APP_NAME}>
DEPENDS
${COMMIT_ID}
COMMAND
${CMAKE_COMMAND} -E echo ${COMMIT_ID} > ./version.md
COMMENT
"Generating file version.md"
VERBATIM)
- 但是,如果您指定一个静态字符串,则项目的构建不会出错:
add_custom_command(TARGET ${APP_NAME} POST_BUILD
WORKING_DIRECTORY
$<TARGET_FILE_DIR:${APP_NAME}>
DEPENDS
${COMMIT_ID}
COMMAND
${CMAKE_COMMAND} -E echo "COMMIT_ID" > ./version.md
COMMENT
"Generating file version.md"
VERBATIM)