0

I have a directory with CMakeLists.txt. And it invokes a CMake module module1.cmake file located elsewhere.

module1.cmake will reference a variable named XXX, which may come from:

  1. the environment variable %XXX%.
  2. the command line through cmake -D XXX=
  3. XXX defined in the invoking directory's CMakeLists.txt through set().
  4. and the CACHE entry in the previously configured build folder

Then module1.cmake will store the variable XXX into the CACHE.

In the module1.cmake file, I need to tell where XXX is from because I need to treat them differently.

For 1, I can check the value of $ENV{XXX}.

For 4, I was thinking about using get_property(cached_XXX CACHE XXX PROPERTY VALUE) and check the value of cached_XXX. But it turns out the -D XXX= option always update the CACHE entry. So I cannot tell if the cached_XXX value is from an old CACHE or from the current -D option.

So after all, how can I tell between 2, 3 and 4?

smwikipedia
  • 61,609
  • 92
  • 309
  • 482
  • For 4, you can use [$CACHE{VAR}](https://cmake.org/cmake/help/latest/variable/CACHE.html?highlight=read%20cache) available since CMake 3.13. – havogt Jul 15 '19 at 07:24
  • Sorry, you will run into the same problem as with your solution via `get_property`... – havogt Jul 15 '19 at 07:25

0 Answers0