1
  1. 首先,我定义COMMIT_ID变量:
    execute_process(COMMAND git rev-parse HEAD
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        OUTPUT_VARIABLE COMMIT_ID )
  1. 如果您指定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)
  1. 但是,如果您指定一个静态字符串,则项目的构建不会出错:
    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)
4

1 回答 1

0

使用该${COMMIT_ID}变量的问题是它可能包含在创建时的尾随空格或换行符execute_process。您可以添加参数以在变量中使用它之前OUTPUT_STRIP_TRAILING_WHITESPACE清理输出:execute_process

execute_process(COMMAND git rev-parse HEAD
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    OUTPUT_VARIABLE COMMIT_ID
    OUTPUT_STRIP_TRAILING_WHITESPACE
)
于 2020-06-23T19:55:00.477 回答