[CMake] if(MATCHES) broken??

Matthew Woehlke mw_triad at users.sourceforge.net
Thu Nov 20 14:44:01 EST 2008


Bill Hoffman wrote:
> The problem is that if(something MATCHES something) works for either 
> strings or variable names.  The way that works is by checking to see if 
> there is a variable defined by the string given to if.  Even if that 
> string is in quotes, it can still be treated like a variable.
> 
> So, in this case cmake looks up "CFLAGS" and finds that there is a 
> variable of that name, and uses that value for the MATCHES statement. 
> Basically ending up with if("happyjoy" MATCHES "CFLAGS").  You can 
> verify this by commenting out set(CFLAGS happyjoy) so that CFLAGS does 
> not have a definition, then your code works.

Yes, I figured that out. Hmm... at least it's documented, if annoying.

After poking at this further, there seems to be a bug here... we've 
established that 'if(CFLAGS MATCHES CFLAGS)' expands the variable on the 
LHS. That being the case, then it seems - from my reading of the doc - 
that 'if(CFLAGS STREQUAL CFLAGS)' should be doing likewise, i.e. both 
should be false. Further, it seems that 'if(bar MATCHES "CFLAGS")' does 
*not* expand bar... why?

> I agree this is confusing...  Perhaps there should be some new if
> arguments that are always string based, and never look up variables.

What about simply inhibiting "expansion" when the LHS is quoted? (I 
guess the concern would be compatibility? But given that the current 
behavior seems buggy - i.e. inconsistent - already...) Since variable 
names can't contain spaces (ah, they can't, right? :-) ), it seems 
reasonable that if you are trying to dereference a variable that is 
supposed to contain the name of another variable, whose value you want 
to use, that this seems to make sense.

OTOH, given that '${${bar}}' is a legal construct that does exactly what 
you'd expect (namely, expand the variable whose name is the expansion of 
bar), I'm inclined to question using implicit expansion at all. But... 
it can be convenient, so maybe that would be too much. Inhibiting 
"expansion" in quoted-strings seems reasonable, though. (After all, 
who's putting things that are supposed to be variable names in quotes?)

-- 
Matthew
Please do not quote my e-mail address unobfuscated in message bodies.
-- 
C++ is for people who want to be able to not just shoot themselves in 
the foot, but do it with a rocket launcher. -- Igor Peshansky



More information about the CMake mailing list