'How can I set PATH with space in cmake?

I am using a CMake custom target to prepend a PATH and execute a command. The current CMakeLists.txt is like this (partial):

add_custom_target(${target}
    COMMAND PATH=${SOME_PATH}/bin:$ENV{PATH} ${SOME_PROGRAM})

The problem is that when the $ENV{PATH} contains a space, the produced Makefile is like:

"PATH=/some path/bin:/bin" some_program

But actually it should be like this otherwise bash will not work:

PATH="/some path/bin:/bin" some program

How should I write the CMakeLists.txt to achieve this?



Solution 1:[1]

The COMMAND option does not understand shell's var=value command .... You can write like this:

add_custom_target(${target}
    COMMAND env PATH=${SOME_PATH}/bin:$ENV{PATH} ${SOME_PROGRAM})
#           ^^^

Or

add_custom_target(${target}
    COMMAND export PATH=${SOME_PATH}/bin:$ENV{PATH} \; ${SOME_PROGRAM})
#           ^^^^^^                                  ^^

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1