如何在 cmake 中使用 space 设置 PATH?

How can I set PATH with space in cmake?

我正在使用 CMake 自定义目标来添加 PATH 并执行命令。目前的CMakeLists.txt是这样的(部分):

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

问题是当 $ENV{PATH} 包含 space 时,生成的 Makefile 如下:

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

但实际上应该是这样的,不然bash不行:

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

我应该如何编写 CMakeLists.txt 来实现这个?

COMMAND 选项不理解 shell 的 var=value command ...。你可以这样写:

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

或者

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