Home > Software engineering >  How can I set PATH with space in cmake?
How can I set PATH with space in cmake?

Time:04-26

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?

CodePudding user response:

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

CodePudding user response:

You can just escape the spaces.

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