2017-07-13 7 views
0

プログラムの出力をプラットフォームでチェックすることは可能ですか?CMakeのプログラム出力を確認する

#include <library.h> 
#include <iostream> 
int main() { 
    std::cout << LIBRARY_MAGIC_VALUE << std::endl; 
    return 0; 
} 

をしてLIBRARY_MAGIC_VALUEの値を抽出するために(CMakeの中にステップを設定します)、それを実行します。

のは、私はこのプログラムをコンパイルしたいとしましょう。

How to write platform checksガイドでは、このユースケースは考慮されていないか、または特定のもの(型のサイズのチェックなど)にのみ特化されているようです。

どうすればこのようなチェックを実装できますか?

+0

CMake 3.0は "execute_process"を持っていますが、非推奨バージョンは "exec_program"と思います。 coutを実行する代わりに、LIBRARY_MAGIC_VALUEを返し、 "execute_process"に戻り変数を設定し、その値を使ってチェックを行うことができます。 –

+0

execute_processはプログラムの作成をカバーしていませんか?プログラムをターゲットとして追加すると、configureステップでそのプログラムが必要であることをどのように指定するのですか? – fferri

+0

正解です。後で実行する値を決定するためにセカンダリプログラムを実行するようなものです。依存関係として追加することができます。そのため、まずVersionCheckerを起動してからAppUsingVersionCheckerを起動し、それぞれに独自のCMakeList.txtを付けます。 –

答えて

2

使用このようなチェックのためのtry_runコマンド:

TRY_RUN(
    # Name of variable to store the run result (process exit status; number) in: 
    test_run_result 

    # Name of variable to store the compile result (TRUE or FALSE) in: 
    test_compile_result 

    # Binary directory: 
    ${CMAKE_CURRENT_BINARY_DIR}/ 

    # Source file to be compiled: 
    ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp 

    # Where to store the output produced during compilation: 
    COMPILE_OUTPUT_VARIABLE test_compile_output 

    # Where to store the output produced by running the compiled executable: 
    RUN_OUTPUT_VARIABLE test_run_output) 

これはtest.cppに与えられたチェックをコンパイルして実行しようとします。以下のように

try_run(LIBRARY_MAGIC_VAL_RUN_RESULT 
    LIBRARY_MAGIC_VAL_COMPILE_RESULT 
    ${CMAKE_CURRENT_BINARY_DIR}/library_magic_val 
    "test_library_magic_val.c" 
    RUN_OUTPUT_VARIABLE LIBRARY_MAGIC_VAL_OUTPUT) 

# Do not forget to check 'LIBRARY_MAGIC_VAL_RUN_RESULT' and 
# 'LIBRARY_MAGIC_VAL_COMPILE_RESULT' for successfullness. 

# Variable LIBRARY_MAGIC_VAL_OUTPUT now contains output of your test program. 
+0

ありがとう、これはそれを解決しました。インクルードパスを指定するには、 'try_run(RUN_RESULT COMPILE_RESULT binary source.c CMAKE_FLAGS -DINCLUDE_DIRECTORIES =/path/to/foo RUN_OUTPUT_VARIABLE OUTVAR)'のような 'CMAKE_FLAGS'キーワードを使用しなければならないことに注意してください。 – fferri

1

あなたは組み込みのtry_runコマンドCMakeのを使用することができます。コンパイルしてチェックを実行し、出力を適切に処理するには、まだtry_runが成功したかどうかを確認する必要があります。例えば、あなたは次のようなことをすることができます:

# Did compilation succeed and process return 0 (success)? 
IF("${test_compile_result}" AND ("${test_run_result}" EQUAL 0)) 
    # Strip whitespace (such as the trailing newline from std::endl) 
    # from the produced output: 
    STRING(STRIP "${test_run_output}" test_run_output) 
ELSE() 
    # Error on failure and print error message: 
    MESSAGE(FATAL_ERROR "Failed check!") 
ENDIF() 
+0

ありがとう、try_runは – fferri