<div dir="ltr"><div>You don't have to create a cache variable for this. Put yourself in the position of CMake;</div><div>* While scanning the CMakeLists.txt of Foo CMake encounters the set(OPT1 ON FORCE) command, which tells CMake to create a *variable* called OPT1 with value ON</div><div>* Then CMake is asked to include Bar</div><div>* While scanning Bar, CMake encounters the option() command, so it will create an option called OPT1</div><div>* end then.... nothing, CMake finished scanning</div><div><br></div><div>Only the second time around, when CMake encounters the set(OPT1..) command, it will have gained knowledge of the presence of the OPT1 option, and hence it will realize it needs to change that option instead of creating a variable with that name.</div><div><br></div><div>So to me, your output is exactly as expected. I suspect if you include Bar before setting the option, you will get the behaviour you expected. This, to me, makes perfect sense, as Bar is a dependency of Foo, and hence needs to be setup before you start setting up Foo. You wouldn't compile Foo before compiling Bar, so why would that be different for the configuration step.</div><div><br></div><div>Sincerely,</div><div>Jakob</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, May 20, 2016 at 11:24 AM, Doug Cuthbertson <span dir="ltr"><<a href="mailto:doug.cuthbertson@gmail.com" target="_blank">doug.cuthbertson@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br style="font-size:12.8px"><div dir="ltr" style="font-size:12.8px">CMake (version 3.5.2) surprised me with how it's passing values to sub-projects, so I was wondering if this is expected behavior. Here's an example of what I mean. Let's say I have a project Foo in a directory of the same name. It contains a third-party library called Bar which has a CMakeLists.txt file that looks like:<div><br></div><div>    cmake_minimum_required(VERSION 2.8.12)</div><div>    </div><div><div>    option(OPT1</div><div>      "Set to OFF|ON (default is OFF) to control build of Bar library" OFF)</div><div>    </div><div>    if(OPT1)</div><div>      message("Bar: OPT1 is on")</div><div>    else(OPT1)</div><div>      message("Bar: OPT1 is off")</div><div>    endif(OPT1)</div><div><br></div><div>I then create CMakeLists.txt for Foo that sets OPT1 to ON and includes Bar:</div><div><br></div><div><div>    cmake_minimum_required(VERSION 2.8.12)</div><div>    </div><div></div><div>    set(OPT1 ON FORCE)</div><div>    if(OPT1)</div><div>      message("Foo: OPT1 is on")</div><div>    else(OPT1)</div><div>      message("Foo: OPT1 is off")</div><div>    endif(OPT1)</div><div>    add_subdirectory(Bar)</div></div><div><br></div><div>The first time I run cmake the message output is:</div><div><br></div><div>    Foo: OPT1 is on</div><div>    Bar: OPT1 is off</div><div><br></div><div>If I run cmake again, I get:</div><div><br></div><div>    Foo: OPT1 is on</div><div>    Bar: OPT1 is on</div><div><br></div><div>If this is expected behavior, is there any way I can ensure Bar receives the value of OPT1 the first time? It makes a huge difference when the option controls, for example, whether a static or dynamic library will be built.</div><div><br></div><div>Thanks,</div><div>Doug</div></div></div></div>
<br>--<br>
<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" target="_blank">http://www.cmake.org/Wiki/CMake_FAQ</a><br>
<br>
Kitware offers various services to support the CMake community. For more information on each offering, please visit:<br>
<br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/support.html</a><br>
CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/consulting.html</a><br>
CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/training.html</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/cmake" rel="noreferrer" target="_blank">http://public.kitware.com/mailman/listinfo/cmake</a><br></blockquote></div><br></div>