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

Alexander Neundorf a.neundorf-work at gmx.net
Wed Aug 20 16:28:02 EDT 2014


On Wednesday, July 30, 2014 14:59:46 Ghyslain Leclerc wrote:
> 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. 

You are using set(... FORCE), right ?

> 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, 

How do you mean that ?
IIRC -DVAR=value sets the cache variable. Not sure, it seems -D maybe also 
sets the non-cache variable ?
Then uset() should have worked.

Can you post a small example to reproduce it ?

Alex



More information about the CMake mailing list