2017-06-04 77 views
0

我試圖從CMake中調用一個可執行文件的輸出作爲在構建系統中處理的字符串。這是一個測試套件列表,我將使用add_test添加到CTest工具。無法在生成的CMake腳本中設置變量

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_test_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() 

的腳本執行後,我可以看到在構建的stdoutmlpack_test --list_content輸出。但FOO仍然是一個空字符串。

輸出:

Adding tests to the test suite 
ActivationFunctionsTest* 
    TanhFunctionTest* 
    LogisticFunctionTest* 
    SoftsignFunctionTest* 
    IdentityFunctionTest* 
    RectifierFunctionTest* 
    LeakyReLUFunctionTest* 
    HardTanHFunctionTest* 
    ELUFunctionTest* 
    SoftplusFunctionTest* 
    PReLUFunctionTest* 
-- FOO='' 

爲什麼參數OUTPUT_VARIABLE不與過程的stdout初始化執行?

+0

也許,您在構建輸出中看到的是執行過程的** stderr **。你可以爲'execute_process'傳遞'ERROR_VARIABLE FOO'的附加選項,所以它的**整個輸出**將被重定向到* FOO *變量。 – Tsyvarev

+0

我試過你的建議,但'FOO'仍然是空的。我還注意到,我無法在腳本中定義任何變量,即。即使在set(myvar 1)之後,myvar也是空的。 – bluefog

+1

命令'configure_file'替換** $'var}'的所有出現** ...您可能希望將'@ ONLY'參數傳遞給該命令,因此它只會替換'@ var @'實例。順便說一句,你可以檢查生成的文件'generate_test_names.cmake'的內容。 – Tsyvarev

回答

1

configure_file生成CMake的腳本,它是更好地使用@ONLY選項用於命令:

configure_file(generate_test_names.cmake.in generate_test_names.cmake @ONLY) 

在這種情況下,只有@[email protected]引用將變量的值來代替,但是${var}引用仍然不變

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 @[email protected]/bin/mlpack_test --list_content 
    OUTPUT_VARIABLE FOO) 
    # But FOO will not be replaced by 'configure_file'. 
    message(STATUS "FOO='${FOO}'") 
endfunction() 

get_names()