Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>Can anyone explain me why I can not use variables in Script file? </code></pre> <p><code>install(SCRIPT ...)</code> command works like <a href="http://www.cmake.org/cmake/help/v2.8.12/cmake.html#opt%3a-Pfile">cmake -P</a>. So there is no variables forwarded from parent script to child (until you explicitly define one):</p> <pre><code>&gt; cat run.cmake if(A) message("A: ${A}") else() message("A is empty") endif() &gt; cmake -P run.cmake A is empty &gt; cmake -DA=15 -P run.cmake A: 15 </code></pre> <p>Using <code>CMakeLists.txt</code>:</p> <pre><code>&gt; cat CMakeLists.txt cmake_minimum_required(VERSION 2.8) set(A 43) execute_process(COMMAND ${CMAKE_COMMAND} -P run.cmake) &gt; cmake -H. -B_builds A is empty </code></pre> <p>Forward to child process:</p> <pre><code>&gt; cat CMakeLists.txt cmake_minimum_required(VERSION 2.8) set(A 43) execute_process(COMMAND ${CMAKE_COMMAND} -DA=${A} -P run.cmake) &gt; cmake -H. -B_builds A: 43 </code></pre> <h3>Solution #1 (forwarding)</h3> <p>Using <code>install(CODE ...)</code> command you can define variable for <code>run.cmake</code> script:</p> <pre><code>&gt; cat CMakeLists.txt install( CODE "execute_process( COMMAND ${CMAKE_COMMAND} -DA=${A} -P ${CMAKE_CURRENT_LIST_DIR}/run.cmake )" ) &gt; cmake -H. -B_builds -DA=554 &gt; cmake --build _builds --target install Install the project... -- Install configuration: "" A: 554 </code></pre> <h3>Solution #2 (configuring)</h3> <p>You can configure install script using <a href="http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command%3aconfigure_file">configure_file</a> command:</p> <pre><code>&gt; cat run.cmake.in set(A @A@) if(A) message("A: ${A}") else() message("A is empty") endif() &gt; cat CMakeLists.txt set(custom_script ${PROJECT_BINARY_DIR}/custom_install_scripts/run.cmake) configure_file(run.cmake.in ${custom_script} @ONLY) install(SCRIPT ${custom_script}) &gt; cmake -H. -B_builds -DA=42 &gt; cmake --build _builds --target install Install the project... -- Install configuration: "" A: 42 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload