[CMake] Setting a value in a sub-project

Petr Kmoch petr.kmoch at gmail.com
Fri May 20 05:37:18 EDT 2016


Hi Doug,

your syntax for `set()` in Foo is incorrect; you're actually setting a
non-cache variable OPT1 do the value "ON;FORCE".

You want to set the *cache* variable OPT1, which would be done like this:

  set(OPT1 ON CACHE BOOL "Set to OFF|ON (default is OFF) to control build
of Bar library" FORCE)

Petr

On 20 May 2016 at 11:24, Doug Cuthbertson <doug.cuthbertson at gmail.com>
wrote:

>
> 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:
>
>     cmake_minimum_required(VERSION 2.8.12)
>
>     option(OPT1
>       "Set to OFF|ON (default is OFF) to control build of Bar library" OFF)
>
>     if(OPT1)
>       message("Bar: OPT1 is on")
>     else(OPT1)
>       message("Bar: OPT1 is off")
>     endif(OPT1)
>
> I then create CMakeLists.txt for Foo that sets OPT1 to ON and includes Bar:
>
>     cmake_minimum_required(VERSION 2.8.12)
>
>     set(OPT1 ON FORCE)
>     if(OPT1)
>       message("Foo: OPT1 is on")
>     else(OPT1)
>       message("Foo: OPT1 is off")
>     endif(OPT1)
>     add_subdirectory(Bar)
>
> The first time I run cmake the message output is:
>
>     Foo: OPT1 is on
>     Bar: OPT1 is off
>
> If I run cmake again, I get:
>
>     Foo: OPT1 is on
>     Bar: OPT1 is on
>
> 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.
>
> Thanks,
> Doug
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20160520/4614e44b/attachment-0001.html>


More information about the CMake mailing list