我想在execute_process
的子目录中执行CMake命令,并将一些缓存变量作为-D
选项传递。
如果变量的类型为string,则可以正常工作。但是,如果变量是列表,则在命令行中传递列表的typical method似乎不起作用。
我尝试了该答案中列出的所有组合。我甚至试图加入mylist
与"\\;"
或"\\\\;"
。然而,execute_process
似乎总是将'-DVal2=a\\;b\\;c\\;'
或'-DVal2=a;b;c'
解压缩到-Dval2=a b c
。
我怎么能阻止这个?只有-DVal2=a\\;b\\;c
工作,但它非常烦人。
set(
mylist
a
b
c
)
set(
cmake_args
"-DVal1=abc"
"'-DVal2=${mylist}'" #does not work, the execute_process will unpack it into seperated args
)
execute_process(
COMMAND ${CMAKE_COMMAND} ${cmake_args} ${CMAKE_SOURCE_DIR}/subproject
OUTPUT_FILE ${CMAKE_BINARY_DIR}/config.log
ERROR_FILE ${CMAKE_BINARY_DIR}/config.log
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/subproject
RESULT_VARIABLE config_result
)
我认为你需要逃避;
字符,它是CMake中列表的默认分隔符,但不清楚你是如何做到这一点的,因此它不适合你。
所以,尝试这样的事情
set(mylist_str "")
foreach(item ${mylist})
string(APPEND mylist_str ${item} "\;")
endforeach()
# this is for debugging
message(STATUS "List as string: ${mylist_str}")
set(cmake_args
"-DVal1=abc"
"-DVal2=${mylist_str}"
"-DVal3=\"${mylist_str}\"" # this has quotes around it
)
# this is for debugging
foreach(item ${cmake_args})
message(STATUS "A list item: ${item}")
endforeach()