[CMake] If execute_process generates embedded newlines in a variable, they are not properly escaped when used in add_custom_target()

Cody P Schafer dev at codyps.com
Thu Sep 4 13:36:35 EDT 2014


Example:

    # cat CMakeLists.txt
    execute_process(COMMAND echo master
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        OUTPUT_VARIABLE BRANCH
    )

    add_custom_target(do-it
        COMMAND echo "${BRANCH}"
        COMMAND echo "${BRANCH}"
    )
    # cmake . -GNinja >/dev/null
    # echo $?
    0
    # ninja
    ninja: error:
    #

Examining the build.ninja file shows:

build CMakeFiles/do-it: CUSTOM_COMMAND
  COMMAND = cd /home/cody/foo && echo master
 && echo master

(That is a newline before the '&&')

Make output is similarly flawed, but recovers due to more lenient syntax:

CMakeFiles/do-it:
        echo master

        echo master


More information about the CMake mailing list