2011-05-24 2 views
2

私のcmakeの設定では、いくつかの変数が環境変数に依存して正しく設定されます。この環境変数は変更可能であり、cmakeのキャッシュを再構築する必要があることを意味します。環境変数が変更されたときにmakefileベースのcmakeプロジェクトにmake rebuild_cacheを自動的に実行させる

私の設定では、この再構成の必要性を検出し、別の "cmake"呼び出しが呼び出されたとき、または "make rebuild_cache"が呼び出されたときに、適切なキャッシュエントリを更新することができます。

しかし、実行するたびに変更が自動的にチェックされ、必要に応じてrebuild_cacheターゲットが実行されるようにしたいと思います。

これは可能ですか?

答えて

1

メイクにメモリがありません。 makeは、makeが最後に実行されたときに与えられた環境変数が設定されたことを「記憶」する方法はありません。

を設定しないと、ファイルに環境変数を書き込むことができません。

私はCMakeを一度も使用していないので、私はそれをどのように実装するのが最善かわかりません。しかし、レベルを作る「生」の上に、一般的なアイデアはに次のようになりますないのでcoincidentially、という名前(ファイルに環境変数を書き込みルール(たとえば、envir_cache)、envir_cacheを書く

1))、場合そのファイルがまだ存在しない場合、またはファイルが存在しますが、その内容は環境変数の値とは異なります。 (if [ -f envir_cache ]read cached_var < envir_cacheif [ "${myvar}" != "${cached_var}" ]の線に沿って何か。)

2)envir_cache依存rebuild_cacheターゲットを作ります。

このようにして、最初の実行時に、実行中に変数が変更されたときは常にrebuild_cacheルールが実行されます。

+0

ありがとうございます。それは実行可能なようだ、私はそれを試してみる時間がなかった。すぐに私は答えを受け入れたものとしてマークします。 –

0

これはCMakeで簡単にはできませんが、CMakeインクルードモジュールとしてこれを行う方法の例を以下に示します。この解決策は、問題の変数をファイルに出力するカスタムターゲットの使用に依存し、cmakeのcompare_filesオプションを呼び出して前のファイルと検証ファイルを比較し、一致しない場合にcmakeを呼び出して再構築します。

解決策は、環境変数によってキャッシュされた値が変更されていないことを再帰的に呼び出す慎重に作成されたCMakeインクルードモジュールを使用します。存在する場合は、以下のように適切な引数を指定してcmakeを呼び出すことで、キャッシュの再構築ステップを実行します。

# Capture the full path to this CMake module file 
if(NOT _option_cmake_file) 
    set(_option_cmake_file ${CMAKE_CURRENT_LIST_FILE}) 
endif() 

# When this CMake module is called as a script include the option file 
if(_option_verify) 
    include(${_option_file}) 
endif() 

# add_option macro for adding cached values you want to be able to 
# override with an environment variable of the same name 
# _name - variable name to use for the cached value 
# _type - type of cached variable 
# _description - description of cached variable for CMake GUI 
# _default - default value if no variable with same name is defined 
macro(add_option _name _type _description _default) 
    # Define _option_file to be created if not in verify mode 
    if(NOT _option_verify) 
    set(_option_file ${CMAKE_BINARY_DIR}/${_name}.cmake) 
    endif() 

    # Determine the source for the alue of the cached variable 
    set(_option_output "set(_name ${_name})") 
    list(APPEND _option_output "\nset(_type ${_type})") 
    list(APPEND _option_output "\nset(_description \"${_description}\")") 
    if(DEFINED ENV{${_name}}) 
    set(${_name} $ENV{${_name}} CACHE ${_type} "${_description}" FORCE) 
    list(APPEND _option_output "\nset(${_name} $ENV{${_name}})") 
    elseif(${_name}) 
    set(${_name} ${${_name}} CACHE ${_type} "${_description}" FORCE) 
    set(ENV{${_name}} ${${_name}}) # needed to pass from verify back to rebuild_cache 
    list(APPEND _option_output "\nset(${_name} ${${_name}})") 
    else() 
    set(${_name} ${_default} CACHE ${_type} "${_description}" FORCE) 
    list(APPEND _option_output "\nset(${_name} ${_default})") 
    endif() 

    # Create the _option_file (or verify file) containing the values 
    # defined above 
    execute_process(
    COMMAND ${CMAKE_COMMAND} -E echo ${_option_output} 
    OUTPUT_FILE ${_option_output}${_option_verify}) 

    # If not in verify mode create check target to verify value 
    if(NOT _option_verify) 
    # Only create parent check-variables target once 
    if(NOT TARGET check-variables) 
     add_custom_target(check-variables ALL) 
    endif() 
    # Use this file as custom CMake target to verify variable value 
    add_custom_target(check-${_name} 
     COMMAND ${CMAKE_COMMAND} 
     -D_option_verify:String=-verify 
     -D_option_file:Filepath=${_option_file} 
     -D_option_sdir:Path=${CMAKE_SOURCE_DIR} 
     -D_option_bdir:Path=${CMAKE_BINARY_DIR} 
     -P ${_option_cmake_file} 
     COMMENT "Checking variable '${_name}' for changes" 
     VERBATIM) 
    # Add custom target as dependency for parent check-variables target 
    add_dependencies(check-variables check-${_name}) 
    else() 
    # Use cmake to compare options file and verify file created above 
    execute_process(
     COMMAND ${CMAKE_COMMAND} -E compare_files 
     ${_option_file} ${_option_file}${_option_verify} 
     OUTPUT_VARIABLE COMPARE_OUTPUT 
     ERROR_VARIABLE COMPARE_ERROR 
     RESULT_VARIABLE COMPARE_RESULT) 
    # Remove verify file 
    file(REMOVE ${_option_file}${_option_verify}) 
    # If compare failed, then call CMAKE to rebuild_cache 
    if(NOT COMPARE_RESULT EQUAL 0) 
     # Perform the rebuild_cache step 
     execute_process(
     COMMAND ${CMAKE_COMMAND} -H${_option_sdir} -B${_option_bdir}) 
    endif() 
    endif() 
endmacro() 

# In verify mode? then call add_option macro to initiate the process 
if(_option_verify) 
    # The values below come from the include(_option_file) above 
    add_option(${_name} ${_type} "${_description}" ${${_name}}) 
endif() 

CMakeのモジュール上で指名された場合は、使用することができadd_option.cmake:あなたが(以下の例を参照)、環境変数を使用して上書きできるようにしたいすべての変数のadd_optionマクロを呼び出すことが期待されますそれは次のよう:上記CMakeLists.txtファイルで

cmake_minimum_required(VERSION 2.8) 
project(Example) 
include(${PROJECT_SOURCE_DIR}/add_option.cmake) 
add_option(MYVAR 
    BOOL 
    "A boolean cached value that can be overridden by Environment variable" 
    ON) 
add_option(MYSTR 
    STRING 
    "A string cached value that can be overridden by Environment variable" 
    "some string") 
message(STATUS "MYVAR=${MYVAR}") 
message(STATUS "MYSTR=${MYSTR}") 

は(UnixののMakefileを使用して)次の操作を行います

mkdir build 
cd build 

次の例は、initを示していますUnix Makefilesの作成この場合、変数はデフォルト値を使用することに注意してください。

cmake .. -G "Unix Makefiles" 
-- The C compiler identification is GNU 4.8.4 
-- The CXX compiler identification is GNU 4.8.4 
-- Check for working C compiler: /usr/bin/cc 
-- Check for working C compiler: /usr/bin/cc -- works 
-- Detecting C compiler ABI info 
-- Detecting C compiler ABI info - done 
-- Check for working CXX compiler: /usr/bin/c++ 
-- Check for working CXX compiler: /usr/bin/c++ -- works 
-- Detecting CXX compiler ABI info 
-- Detecting CXX compiler ABI info - done 
-- MYVAR=ON 
-- MYSTR=some string 
-- Configuring done 
-- Generating done 
-- Build files have been written to: /home/user/example/build 

次の例では、makeはチェックインの変数は、ターゲット起動と上記add_option.cmakeモジュールによって作成されたその依存ターゲットする方法を示しています。再構築キャッシュが発生しないことに注意してください。

make 
Scanning dependencies of target check-MYVAR 
[ 50%] Checking variable 'MYVAR' for changes 
[ 50%] Built target check-MYVAR 
Scanning dependencies of target check-MYSTR 
[100%] Checking variable 'MYSTR' for changes 
[100%] Built target check-MYSTR 
Scanning dependencies of target check-variables 
[100%] Built target check-variables 

次の例では、環境変数がチェック変数の一つが失敗するステップ原因となり、再構築したキャッシュイベントをトリガーする方法を示しています。キャッシュ再構築プロセス中のMYVAR値の変更に注意してください。

make MYVAR=off 
[ 50%] Checking variable 'MYVAR' for changes 
-- MYVAR=off 
-- MYSTR=some string 
-- Configuring done 
-- Generating done 
-- Build files have been written to: /home/user/example/build 
[ 50%] Built target check-MYVAR 
[100%] Checking variable 'MYSTR' for changes 
[100%] Built target check-MYSTR 
[100%] Built target check-variables 

次の例では、一時的に上記変更変数がデフォルトに戻って戻され、異なる変数がその値を変更するためにトリガーされる方法を示しています。 MYVAR変数が新しい値を取得する間、MYVAR変数はデフォルト値に戻ります。

make MYSTR="hi mom" 
[ 50%] Checking variable 'MYSTR' for changes 
-- MYVAR=ON 
-- MYSTR=hi mom 
-- Configuring done 
-- Generating done 
-- Build files have been written to: /home/user/example/build 
[ 50%] Built target check-MYSTR 
[100%] Checking variable 'MYVAR' for changes 
[100%] Built target check-MYVAR 
[100%] Built target check-variables 
関連する問題