<div dir="ltr">I am working on a CMake project that uses ExternalProject_Add to add an external dependency. The issue is, I need all of my command line arguments from my project to be propagated to the external project so it will build using the same build configuration and with the correct compiler settings.<div><br></div><div>Is there a simple way to extract all of the command line arguments into a variable so I can simply pass them to ExternalProject_Add with the "CMAKE_ARGS" parameter?</div><div><br></div><div>The only solution I have found so far is a function that parses the CMake Cache and extracts all of the variables with a help message that says "No help, variable specified on the command line." While this does work, it feels like a hacky workaround at best and seems like it may break in a future update of CMake if they decide to change the default cache help messages.</div><div><br></div><div>Here is the code to make this hack work:</div><div><br></div><div><pre style="margin-top:0px;margin-bottom:0px"><span style="font-style:italic;color:rgb(69,198,214)"> get_cmake_property</span>(CACHE_VARS<span style="color:rgb(190,192,194)"> </span>CACHE_VARIABLES)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-style:italic;color:rgb(69,198,214)"> foreach</span>(CACHE_VAR<span style="color:rgb(190,192,194)"> </span><span style="color:rgb(138,96,44)">${CACHE_VARS}</span>)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(190,192,194)"> </span><span style="font-style:italic;color:rgb(69,198,214)">get_property</span>(CACHE_VAR_HELPSTRING<span style="color:rgb(190,192,194)"> </span>CACHE<span style="color:rgb(190,192,194)"> </span><span style="color:rgb(138,96,44)">${CACHE_VAR}</span><span style="color:rgb(190,192,194)"> </span>PROPERTY<span style="color:rgb(190,192,194)"> </span>HELPSTRING)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(190,192,194)"> </span><span style="font-style:italic;color:rgb(69,198,214)">if</span>(CACHE_VAR_HELPSTRING<span style="color:rgb(190,192,194)"> </span>STREQUAL<span style="color:rgb(190,192,194)"> </span><span style="color:rgb(214,149,69)">"No</span><span style="color:rgb(190,192,194)"> </span><span style="color:rgb(214,149,69)">help,</span><span style="color:rgb(190,192,194)"> </span><span style="color:rgb(214,149,69)">variable</span><span style="color:rgb(190,192,194)"> </span><span style="color:rgb(214,149,69)">specified</span><span style="color:rgb(190,192,194)"> </span><span style="color:rgb(214,149,69)">on</span><span style="color:rgb(190,192,194)"> </span><span style="color:rgb(214,149,69)">the</span><span style="color:rgb(190,192,194)"> </span><span style="color:rgb(214,149,69)">command</span><span style="color:rgb(190,192,194)"> </span><span style="color:rgb(214,149,69)">line."</span>)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(190,192,194)"> </span><span style="font-style:italic;color:rgb(69,198,214)">get_property</span>(CACHE_VAR_TYPE<span style="color:rgb(190,192,194)"> </span>CACHE<span style="color:rgb(190,192,194)"> </span><span style="color:rgb(138,96,44)">${CACHE_VAR}</span><span style="color:rgb(190,192,194)"> </span>PROPERTY<span style="color:rgb(190,192,194)"> </span>TYPE)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(190,192,194)"> </span><span style="font-style:italic;color:rgb(69,198,214)">if</span>(CACHE_VAR_TYPE<span style="color:rgb(190,192,194)"> </span>STREQUAL<span style="color:rgb(190,192,194)"> </span><span style="color:rgb(214,149,69)">"UNINITIALIZED"</span>)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(190,192,194)"> </span><span style="font-style:italic;color:rgb(69,198,214)">set</span>(CACHE_VAR_TYPE)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(190,192,194)"> </span><span style="font-style:italic;color:rgb(69,198,214)">else</span>()</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(190,192,194)"> </span><span style="font-style:italic;color:rgb(69,198,214)">set</span>(CACHE_VAR_TYPE<span style="color:rgb(190,192,194)"> </span>:<span style="color:rgb(138,96,44)">${CACHE_VAR_TYPE}</span>)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(190,192,194)"> </span><span style="font-style:italic;color:rgb(69,198,214)">endif</span>()</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(190,192,194)"> </span><span style="font-style:italic;color:rgb(69,198,214)">list</span>(APPEND<span style="color:rgb(190,192,194)"> </span>CMAKE_ARGS<span style="color:rgb(190,192,194)"> </span><span style="color:rgb(214,149,69)">"-D</span><span style="color:rgb(138,96,44)">${CACHE_VAR}${CACHE_VAR_TYPE}</span><span style="color:rgb(214,149,69)">=</span><span style="color:rgb(138,96,44)">${${CACHE_VAR}}</span><span style="color:rgb(214,149,69)">"</span>)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(190,192,194)"> </span><span style="font-style:italic;color:rgb(69,198,214)">endif</span>()</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-style:italic;color:rgb(69,198,214)"> endforeach</span>()</pre></div><div><br></div><div>Is there any easy way to just get a list of all of the command line arguments without parsing the cache and filtering by help messages? This solution just really feels wrong... there must be a better way.</div><div><br></div><div>Thanks,</div><div>Tim</div></div>