[CMake] Variable shadowing between scope, cache and command line argument

Ghyslain Leclerc ghleclerc at gmail.com
Wed Jul 30 14:59:46 EDT 2014


Hello,

First post here.  Sorry if its too long.  Simply trying to be as clear as I can be.

I am trying to make sense of the various ways to set a variable and how one can shadow the other.  Long story short, I am trying to get the following behaviour for a variable in a script:

  1 - check if MYVAR exists
          1.1 - If it does, test its validity and set a second variable named MYVAR_VALID to TRUE or FALSE

  2 - if MYVAR does not exist or if MYVAR_VALID is FALSE, inspect system for information and set MYVAR (with FORCE in the cache) and MYVAR_VALID (not in cache, but does not seem to make a difference).

Hope this is written clearly enough to be understood.  Sorry, english is not my first language.  Anyhow, I get the following behaviour easily except in one case, which is the reason for my question.  These are the cases I have tested :

- Start with empty cache and call ccmake.  Then, MYVAR does not exist.  My script inspects the system, sets the value, sets MYVAR_VALID to TRUE and stops.  On successive runs, the variable is defined and valid, so the system is not inspected again.  Everything is fine.

- Start with empty cache.  Run it once, but can’t find a valid entry (or find a wrong one somehow, but that’s practically impossible since the code in my script to inspect the system and to test the variable are basically the same.  I digress, sorry).  Set MYVAR_VALID to FALSE.  User can set the value to a valid one and on next run, the script will set MYVAR_VALID to TRUE and then, we are back to variable defined and valid.  Everything is fine.

- Start with "non-empty cache" because ccmake (or cmake) is called with -DMYVAR:PATH="/Users/“, for instance.  If the value set on command line is fine, then MYVAR_VALID will be set to TRUE on the first run and no system inspection is necessary.  The value is now set and valid.  Everything is fine.

Now, for my problem :
- Start with "non-empty cache" because ccmake (or cmake) is called with -DMYVAR:PATH="/Users/“, for instance.  But this time, the value is not a valid one.  Then, the variable is defined but not valid.  So on the first run, the script will inspect the system.  If it can find a valid value, I would like my script to override the variable with the valid one. Then, set to valid and so on and so forth...

I have not been able to do this.  I can find the correct value, I can set the new value, but it is not used.  I mean by that that I have inspected the CMakeCache.txt file and when I call ccmake, the cache contains the value set on the command line.  Then, I launch my cmake script and output the values of MYVAR and MYVAR_VALID and they are respectively the one of the command line and FALSE.  Then, I find the correct value for MYVAR and try and set it.  When I inspect the cache, it seems the value has effectively been overwritten.  But when I try to output the new variables, it seems to remain stuck at the value provided on the command line.  I have tried using unset( MYVAR ) in scope, in the parent scope and in the CACHE before setting the new value.  Still, the old value is outputted.  The only way to get the new value is to close ccmake and launch it again.

Now if you’re still following (I am sorry, a bit complicated to follow I’m afraid, trying to be thourough), I guess my question is:
Is this normal behaviour ?   Is there something I am missing or is it simply how CMake works and I should simply tell my users to either initially set the right value or restart their ccmake.

I haven’t tested this on Windows with CMake-GUI yet.  I might have time to do it a little later.

Anyhow, if anybody has the courage to read through my mail and try and help me, thanks a million.

Ghyslain


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20140730/11853965/attachment.html>


More information about the CMake mailing list