无法在生成的CMake脚本中设置变量
我试图从调用无法在生成的CMake脚本中设置变量,cmake,ctest,Cmake,Ctest,我试图从调用CMake中的可执行文件中获取输出,作为字符串,以便在构建系统中进行处理。这是一个测试套件列表,我将使用add\u test添加到CTest工具中 在CMakeLists.txt中 ...(After adding the mlpack_test target)... configure_file(generate_test_names.cmake.in generate_test_names.cmake) add_custom_command(TARGET mlpack_test
CMake
中的可执行文件中获取输出,作为字符串,以便在构建系统中进行处理。这是一个测试套件列表,我将使用add\u test
添加到CTest工具中
在CMakeLists.txt中
...(After adding the mlpack_test target)...
configure_file(generate_test_names.cmake.in generate_test_names.cmake)
add_custom_command(TARGET mlpack_test
POST_BUILD
COMMAND ${CMAKE_COMMAND} -P generate_test_names.cmake
)
在generate\u test\u names.cmake.In
function(get_names)
message("Adding tests to the test suite")
execute_process(COMMAND ${CMAKE_BINARY_DIR}/bin/mlpack_test --list_content
OUTPUT_VARIABLE FOO)
message(STATUS "FOO='${FOO}'")
endfunction()
get_names()
脚本被执行,我可以在构建的stdout
中看到mlpack\u测试--list\u content
的输出。但是FOO
仍然是一个空字符串
输出:
Adding tests to the test suite
ActivationFunctionsTest*
TanhFunctionTest*
LogisticFunctionTest*
SoftsignFunctionTest*
IdentityFunctionTest*
RectifierFunctionTest*
LeakyReLUFunctionTest*
HardTanHFunctionTest*
ELUFunctionTest*
SoftplusFunctionTest*
PReLUFunctionTest*
-- FOO=''
为什么
OUTPUT\u变量的参数
没有使用执行的进程的stdout
初始化?当使用configure\u file
生成CMake脚本时,最好对该命令使用@ONLY选项:
configure_file(generate_test_names.cmake.in generate_test_names.cmake @ONLY)
在这种情况下,只有@var@
引用将替换为变量的值,但${var}
引用保持不变:
可能,您在构建输出中看到的是已执行流程的stderr。您可以为
execute\u process
传递附加选项ERROR\u VARIABLE FOO
,这样它的整个输出将重定向到FOO VARIABLE中。我尝试了您的建议,但FOO
仍然为空。我还注意到,我无法在脚本中定义任何变量,即即使在set(myvar 1)
myvar
为空之后。命令configure\u file
将替换所有出现的${var}
。。。您可能希望将@ONLY
参数传递给该命令,因此它将仅替换@var@
实例。顺便说一句,您可以检查生成文件的内容generate\u test\u names.cmake
@Tsyvarev非常感谢!添加@只起作用。应该更仔细地检查configure_file
的文档。@Tsyvarev您介意添加它作为问题的答案吗?:)
function(get_names)
message("Adding tests to the test suite")
# CMAKE_BINARY_DIR will be replaced with the actual value of the variable
execute_process(COMMAND @CMAKE_BINARY_DIR@/bin/mlpack_test --list_content
OUTPUT_VARIABLE FOO)
# But FOO will not be replaced by 'configure_file'.
message(STATUS "FOO='${FOO}'")
endfunction()
get_names()