Wednesday, April 27, 2022

[SOLVED] How can I set PATH with space in cmake?

Issue

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

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})
#           ^^^^^^                                  ^^


Answered By - pynexj
Answer Checked By - Katrina (WPSolving Volunteer)