如何将列表变量传递给另一个CMake调用?

如何将列表变量传递给另一个CMake调用?

问题描述:

在我的CMake脚本中,我使用 exec_program($ {CMAKE_COMMAND} ...)运行其他CMake实例。我想将父环境的 $ {CMAKE_MODULES_PATH} 设置为子环境可用。因此,我尝试将变量作为参数传递:

In my CMake scripts, I run other CMake instances using exec_program(${CMAKE_COMMAND} ...). I want to make the ${CMAKE_MODULES_PATH} of the parent environment available to the child environments. Therefore, I tried to pass the variable as argument:

exec_program(${CMAKE_COMMAND} ... ARGS ...
    -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH} ...)

这弄乱了其他参数,我得到 CMake错误:源目录< first-module-path>似乎不包含CMakeLists.txt。。因此,我尝试对变量进行转义:

This messes up the other parameters and I get a CMake Error: The source directory <first-module-path> does not appear to contain CMakeLists.txt.. Therefore, I tried escaping the variable:

exec_program(${CMAKE_COMMAND} ... ARGS ...
    -DCMAKE_MODULE_PATH="${CMAKE_MODULE_PATH}" ...)

当我打印 $ {CMAKE_MODULE_PATH} 形式,将打印所有路径,并以空格分隔。但是,CMake在这些路径中找不到脚本。我想这与列表以字符串而不是半彩色分隔的列表传递有关。

When I print the ${CMAKE_MODULE_PATH} form within the child environment, all the paths get printed, separated by a space each. However, CMake doesn't find scripts inside those paths. I guess it has something to do with the list being passed as string rather than a semi-color separated list.

如何传递包含字符串列表的CMake变量

How can I pass a CMake variable holding a list of strings to another CMake command?


在我的CMake脚本中,我使用exec_program运行其他CMake实例( $ {CMAKE_COMMAND} ...)

In my CMake scripts, I run other CMake instances using exec_program(${CMAKE_COMMAND} ...)

根据文档不推荐使用此命令:

According to documentation this command is deprecated:

Deprecated. Use the execute_process() command instead.

带有列表变量CMakeLists.txt的示例:

Example with the list variable, CMakeLists.txt:

execute_process(
    COMMAND "${CMAKE_COMMAND}" "-DVAR=A;B;C" -P script.cmake
    OUTPUT_VARIABLE output
)

script.cmake:

script.cmake:

message("VAR: ${VAR}")

foreach(x ${VAR})
  message("x: ${x}")
endforeach()

输出:

VAR: A;B;C
x: A
x: B
x: C