# How to retrieve variable definition but not from cache?

**URL:** https://discourse.cmake.org/t/how-to-retrieve-variable-definition-but-not-from-cache/303
**Category:** Development
**Created:** [December 2, 2019, 11:49am UTC](https://discourse.cmake.org/t/how-to-retrieve-variable-definition-but-not-from-cache/303 "2019-12-02T11:49:18Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![dbahadir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dbahadir/32/197_2.png) [@dbahadir](https://discourse.cmake.org/u/dbahadir)
#### Post date: [December 2, 2019, 11:49am UTC](https://discourse.cmake.org/t/how-to-retrieve-variable-definition-but-not-from-cache/303/1 "2019-12-02T11:49:18Z")

</div>

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?

Thanks for any information.  
Deniz

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [December 2, 2019, 12:40pm UTC](https://discourse.cmake.org/t/how-to-retrieve-variable-definition-but-not-from-cache/303/2 "2019-12-02T12:40:51Z")

</div>

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:

> [@dbahadir](#):
>
> I want to distinguish if its value comes from a CMake cache variable or from a local CMake variable.

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…

---

<div class="post-metadata">

### Author: ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)
#### Post date: [December 2, 2019, 2:22pm UTC](https://discourse.cmake.org/t/how-to-retrieve-variable-definition-but-not-from-cache/303/3 "2019-12-02T14:22:05Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![dbahadir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dbahadir/32/197_2.png) [@dbahadir](https://discourse.cmake.org/u/dbahadir)
#### Post date: [December 2, 2019, 2:52pm UTC](https://discourse.cmake.org/t/how-to-retrieve-variable-definition-but-not-from-cache/303/4 "2019-12-02T14:52:56Z")

</div>

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.)

---

<div class="post-metadata">

### Author: ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)
#### Post date: [December 12, 2019, 2:11pm UTC](https://discourse.cmake.org/t/how-to-retrieve-variable-definition-but-not-from-cache/303/5 "2019-12-12T14:11:27Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [December 12, 2019, 2:18pm UTC](https://discourse.cmake.org/t/how-to-retrieve-variable-definition-but-not-from-cache/303/6 "2019-12-12T14:18:13Z")

</div>

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.
