I'm slowly realizing the gravity of this behavior:<br><br>if(build_system STREQUAL "windows")<br>...<br>endif()<br><br>If someone creates a variable named "windows", then this code will not work as intended.<br>
<br>I'm starting to convert our scripts to use this hopefully foolproof alternative:<br><br>string(COMPARE EQUAL "${build_system}" windows _cmp)<br>if (_cmp)<br>...<br>endif()<br><br>It will make it ugly to create a nested if else block, but it'll work.<br>
<br><div class="gmail_quote">On Thu, Feb 14, 2013 at 4:20 PM, Matthew Woehlke <span dir="ltr"><<a href="mailto:matthew.woehlke@kitware.com" target="_blank">matthew.woehlke@kitware.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5"><br>
<br>
On 2013-02-14 16:44, Shaun Williams wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I learned something very valuable today about cmake after getting an<br>
unexpected result with STREQUAL:<br>
<br>
set(foo bar)<br>
...<br>
set(baz foo)<br>
...<br>
if("${baz}" STREQUAL "bar") # This evaluates to true.<br>
...<br>
<br>
I expected it to be false, because I was trying to get baz's value ("foo")<br>
to compare with "bar". I thought that parameters explicitly quoted were<br>
treated as strings.<br>
<br>
Then I learned that the interpreter is the only one that sees quotes around<br>
parameters, for the sole purpose of string interpolation and preventing<br>
whitespace from splitting a parameter. Cmake commands do not see these<br>
quotes. Therefore, STREQUAL can never know the difference between "baz" and<br>
baz.<br>
<br>
So, STREQUAL treats a parameter as a variable name if it is defined, but as<br>
a string value if it is not. (I verified this in cmIfCommand.cxx)<br>
<br>
Is this quote behavior well-known among cmake users? For others like me,<br>
I'd like this behavior to be emphasized in the cmake docs for STREQUAL.<br>
</blockquote>
<br></div></div>
Yes, it is :-/.<br>
<br>
This is why I take a page from autotools and try to always write:<br>
if("x_${baz}" STREQUAL "x_bar")<br>
<br>
(Or you can write 'if(baz STREQUAL'... er... well if your RHS for sure cannot be a variable name - e.g. is empty, contains spaces, etc. - then you can write your constructs assuming that auto-expansion will occur. Personally, I'm going to stick with explicit expansion - guaranteeing an empty value in case the variable is not set - and adding something to make it unlikely that either side expands to a variable name to prevent auto-expansion...)<span class="HOEnZb"><font color="#888888"><br>
<br>
-- <br>
Matthew<br>
<br>
</font></span></blockquote></div><br>