[cmake-developers] set(CACHE) and the local scope

Ben Boeckel ben.boeckel at kitware.com
Thu Dec 10 15:44:42 EST 2015

On Thu, Dec 10, 2015 at 21:20:21 +0100, Alexander Neundorf wrote:
> On Wednesday, December 09, 2015 17:35:28 Ben Boeckel wrote:
> > So some behavior I was unaware of until today came up:
> > 
> >     set(var ON)
> >     option(var "description" OFF)
> >     message("var: ${var}")
> Assuming I wouldn't know about the subtle characteristics of normal vs. 
> cache variables, I think I would expect that var has the value of the option 
> afterwards.
> I.e. on the first run it would be OFF (since that's the default value of the 
> option), and all later runs it would have the value which is in the cache.

The problem with this behavior is that if I do -Dinternal_var:BOOL=OFF,
it will override any variable of that name inside the project and it
cannot override it without knowing it is in the cache to unset it so
that the local variable is used again.

This behavior also breaks cmake_dependent_option as-is since it sets a
local variable to the fallback value if its requirements are not met
(which is how it remembers the user decision when it becomes a viable
option again).


More information about the cmake-developers mailing list