1
  1. First, I define the COMMIT_ID variable:
    execute_process(COMMAND git rev-parse HEAD
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        OUTPUT_VARIABLE COMMIT_ID )
  1. If you specify the COMMIT_ID variable the project is not built:
    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. But, if you specify a static string the project is built without errors:
    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)
Kevin
  • 16,549
  • 8
  • 60
  • 74
  • One note, I think the `DEPENDS` argument works when it is a *target name*, or file name, or path. I don't think it'll work as intended when you provide a **variable** to `DEPENDS`. – Kevin Jun 23 '20 at 19:39

1 Answers1

0

The issue with using the ${COMMIT_ID} variable is that it may contain trailing whitespace or newlines from its creation in execute_process. You can add the OUTPUT_STRIP_TRAILING_WHITESPACE argument to cleanup the output of execute_process before it is used in the variable:

execute_process(COMMAND git rev-parse HEAD
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    OUTPUT_VARIABLE COMMIT_ID
    OUTPUT_STRIP_TRAILING_WHITESPACE
)
Kevin
  • 16,549
  • 8
  • 60
  • 74