[cmake-developers] Alternate if() without implicit variable expansion

Brad King brad.king at kitware.com
Thu Sep 4 10:40:56 EDT 2014


On 09/04/2014 10:25 AM, Nils Gladitz wrote:
> This keeps coming up and there probably have been many discussions on 
> how this might be "fixed" as well.

As I have explained every other time it has come up there is exactly
one way to fix it: a policy that makes expansion happen only for
unquoted arguments.  Someone just has to do it.

> I am wondering if we could provide an alias for the "if" command (e.g. 
> "safe_if") that would inherit cmIfCommand but would reimplement 
> GetVariableOrString() without the variable lookup.
> 
> This would leave if() behavior intact for compatibility but would 
> provide an alternative with less surprising semantics.

I think a new command would ("if_noexpand") would make the "correct"
code less readable than the "incorrect" code.  Also the implicit-bool
conversion of variables is more readable IMO; consider

 if(${FOO_FOUND})

versus

 if(FOO_FOUND)

On 09/04/2014 10:32 AM, David Cole via cmake-developers wrote:
> Another approach might be to add STRING_EQUAL and STRING_MATCHES (or
> similarly unambiguous names) operators that do not do the variable
> lookup.

This is a reasonable proposal but the difference to STREQUAL
and MATCHES is quite subtle.  Also adding more operators this
late in the game is more likely to collide with variable names
people already use.

-Brad




More information about the cmake-developers mailing list