[CMake] Setting a value in a sub-project

Doug Cuthbertson doug.cuthbertson at gmail.com
Fri May 20 05:24:58 EDT 2016


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20160520/944adca8/attachment.html>


More information about the CMake mailing list