I am currently playing around with the C++ source-code of CMake, trying to implement a new feature.
For that I need to retrieve some defined variable, but I want to distinguish if its value comes from a CMake cache variable or from a local CMake variable.
In Source/cmMakefile.[h|cxx]I found the following methods which retrieve/access the values of variables:
GetDefinitions,
GetDefinition,
GetDef,
GetSafeDefinition,
GetRequiredDefinition and
IsDefinitionSet.
Sadly, none of these allows to retrieve just the locally defined variable. They always fall back to returning the Cache variable if no local one was found.
Is there really no other way than to modify the signature of these functions and provide an optional (defaulted) parameter which determines if the Cache should be accessed as fallback or not?
I am not familiar with the C++ source code of CMake, so I can’t answer your question, but I’m curious about the following:
Why do you want to distinguish based on the origin of the variable? I don’t know of any CMake feature that behaves like this (but I’m maybe ignorant, feel free to enlighten me), so this sounds quite intriguing…
Add me to the list of people that are curious on why you want to distinguish between cache and a local variable.
But what you want to do is possible. An example is found at cmOptionCommand.cxx:32, where we explicitly want to obey local variables instead of constructing a cache entry.
I am actually exploring how difficult it would be to implement some push/pop commands for CMake variables.
And to make it simpler for me I thought about these commands taking an option determining what kind of variable to push/pop (e.g. something like push(VARIABLE <VAR> ...) and push(CACHE_VARIABLE <VAR> ...)).
Currently, I am at the point where I want to retrieve the old value to save it (the push). How and where this actually should be stored, I haven’t thought through yet. (I thought about storing these variables and their value under some other name but there might even be a better mechanism, by opening another scope or so. I currently do not know what CMake’s C++ code already provides there.)
@robert.maynard Do you think these commands would be something useful for CMake or would you prefer to provide such functionality as a CMake-Module? (I actually think it would be even easier for me to implement this as a CMake-Module, as I do not know the CMake C++ code too well.)
I personally would see value in being able to push / pop variable state. In general I have never needed to know if the variables I am pushing/popping is a cache or local variable. I think as long as the work doesn’t change the type ( local/cache ) it would be really nice.
A push/pop pair for normal variable scopes wouldn’t be hard to implement (except for bikeshedding the names). The cache exists at a separate layer and has no such scoping though.