Discussion:
[CMake] Problem with option() when in subdirectory
David Demelier
2011-09-18 12:15:54 UTC
Permalink
Hello,

I noticed that option() values are not checked when the option() command
is under a add_subdirectory() target.

Example:


-- a's CMakeLists.txt --
project(a C)

set(BUILD_DEMOS OFF)
add_subdirectory(extern/b)

-- extern/b's CMakeLists.txt --
project(b C)

option(BUILD_DEMOS "Enable the demos build" ON)

if (BUILD_DEMOS)
...
endif ()

Then when building the project a it will also enter the if BUILD_DEMOS
conditional.

What can I do now? :)

Cheers,
--
David Demelier
Alexander Neundorf
2011-09-18 12:32:33 UTC
Permalink
Post by David Demelier
Hello,
I noticed that option() values are not checked when the option() command
is under a add_subdirectory() target.
-- a's CMakeLists.txt --
project(a C)
set(BUILD_DEMOS OFF)
add_subdirectory(extern/b)
-- extern/b's CMakeLists.txt --
project(b C)
option(BUILD_DEMOS "Enable the demos build" ON)
if (BUILD_DEMOS)
...
endif ()
Then when building the project a it will also enter the if BUILD_DEMOS
conditional.
What can I do now? :)
The values of options are stored in the cache.
In your toplevel file, OFF is put in the cache.
Then, in the subdir, the variable of BUILD_DEMOS is already in the cache, so
it is not overridden with ON, but keeps the value which is in the cache.

Does that help ?

Alex
Michael Wild
2011-09-18 12:42:49 UTC
Permalink
Post by Alexander Neundorf
Post by David Demelier
Hello,
I noticed that option() values are not checked when the option() command
is under a add_subdirectory() target.
-- a's CMakeLists.txt --
project(a C)
set(BUILD_DEMOS OFF)
add_subdirectory(extern/b)
-- extern/b's CMakeLists.txt --
project(b C)
option(BUILD_DEMOS "Enable the demos build" ON)
if (BUILD_DEMOS)
...
endif ()
Then when building the project a it will also enter the if BUILD_DEMOS
conditional.
What can I do now? :)
The values of options are stored in the cache.
In your toplevel file, OFF is put in the cache.
Then, in the subdir, the variable of BUILD_DEMOS is already in the cache, so
it is not overridden with ON, but keeps the value which is in the cache.
Does that help ?
Alex
Alex, you mis-read his code. In the top-level CMakeLists.txt file
BUILD_DEMOS is *not* put in the cache, which is why it is overridden by
the option() call *the first time round*, but not afterwards which makes
for very confusing behaviour.

In general, it is safe to override a cached variable with a uncached
variable, but the other way round is asking for trouble...

It would be better to do in a/CMakeLists.txt:

set(BUILD_DEMOS OFF CACHE INTERNAL "Don't build demos")

this way
Alexander Neundorf
2011-09-18 19:54:51 UTC
Permalink
Post by Michael Wild
Post by Alexander Neundorf
Post by David Demelier
Hello,
I noticed that option() values are not checked when the option() command
is under a add_subdirectory() target.
-- a's CMakeLists.txt --
project(a C)
set(BUILD_DEMOS OFF)
add_subdirectory(extern/b)
-- extern/b's CMakeLists.txt --
project(b C)
option(BUILD_DEMOS "Enable the demos build" ON)
if (BUILD_DEMOS)
...
endif ()
Then when building the project a it will also enter the if BUILD_DEMOS
conditional.
What can I do now? :)
The values of options are stored in the cache.
In your toplevel file, OFF is put in the cache.
Then, in the subdir, the variable of BUILD_DEMOS is already in the cache,
so it is not overridden with ON, but keeps the value which is in the
cache.
Does that help ?
Alex
Alex, you mis-read his code. In the top-level CMakeLists.txt file
BUILD_DEMOS is *not* put in the cache, which is why it is overridden by
the option() call *the first time round*, but not afterwards which makes
for very confusing behaviour.
In general, it is safe to override a cached variable with a uncached
variable, but the other way round is asking for trouble...
set(BUILD_DEMOS OFF CACHE INTERNAL "Don't build demos")
Yes, you are completely right.

Alex
David Demelier
2011-09-21 19:50:43 UTC
Permalink
Post by Alexander Neundorf
Post by Michael Wild
Post by Alexander Neundorf
Post by David Demelier
Hello,
I noticed that option() values are not checked when the option() command
is under a add_subdirectory() target.
-- a's CMakeLists.txt --
project(a C)
set(BUILD_DEMOS OFF)
add_subdirectory(extern/b)
-- extern/b's CMakeLists.txt --
project(b C)
option(BUILD_DEMOS "Enable the demos build" ON)
if (BUILD_DEMOS)
...
endif ()
Then when building the project a it will also enter the if BUILD_DEMOS
conditional.
What can I do now? :)
The values of options are stored in the cache.
In your toplevel file, OFF is put in the cache.
Then, in the subdir, the variable of BUILD_DEMOS is already in the cache,
so it is not overridden with ON, but keeps the value which is in the
cache.
Does that help ?
Alex
Alex, you mis-read his code. In the top-level CMakeLists.txt file
BUILD_DEMOS is *not* put in the cache, which is why it is overridden by
the option() call *the first time round*, but not afterwards which makes
for very confusing behaviour.
In general, it is safe to override a cached variable with a uncached
variable, but the other way round is asking for trouble...
set(BUILD_DEMOS OFF CACHE INTERNAL "Don't build demos")
Yes, you are completely right.
Alex
_______________________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
http://www.cmake.org/mailman/listinfo/cmake
Thanks a lot! This solved my problem :)

Cheers,
--
David Demelier
Loading...