<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 04-Sep-14 09:58, Clark Wang wrote:<br>
</div>
<blockquote
cite="mid:CADv8-ohpUOrt1yVbxqr-vpfXztYy=28SQA8eCn5eZeyP8NzZKg@mail.gmail.com"
type="cite">
<div dir="ltr">
<div class="gmail_extra">
<div class="gmail_quote">On Thu, Sep 4, 2014 at 1:44 PM, Clark
Wang <span dir="ltr"><<a moz-do-not-send="true"
href="mailto:dearvoid@gmail.com" target="_blank">dearvoid@gmail.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left:1px solid
rgb(204,204,204);padding-left:1ex">
<div dir="ltr">
<div class="gmail_extra">
<div class="gmail_quote">
<div class="">On Thu, Sep 4, 2014 at 1:36 PM, Chuck
Atkins <span dir="ltr"><<a
moz-do-not-send="true"
href="mailto:chuck.atkins@kitware.com"
target="_blank">chuck.atkins@kitware.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0px
0px 0px 0.8ex;border-left:1px solid
rgb(204,204,204);padding-left:1ex">
<div dir="ltr">Hi Clark<br>
<div class="gmail_extra"><br>
The expression inside the if statement has
it's variables dereferenced before
evaluating and the non-variables are treated
as constant expressions. In this case, a
resolves to "b", b resolves to "c", and c is
not a variable so it's treated as the
constant expression "c". Thus
<div>
<br>
<br>
<span style="font-family:courier
new,monospace">if(a STREQUAL b OR a
STREQUAL c)<br>
</span><br>
</div>
</div>
<div class="gmail_extra">gets evaluated as<br>
<br>
<span style="font-family:courier
new,monospace">if( ("b" STREQUAL "c") OR
("b" STREQUAL "c") )<br>
</span></div>
</div>
</blockquote>
<div><br>
</div>
</div>
<div>Thanks a lot. It's clear now. But is there a
way to check if the value of the variable a equals
to "b" or "c"?<span class=""><font color="#888888"><br>
</font></span></div>
</div>
</div>
</div>
</blockquote>
<div><br>
Found <a moz-do-not-send="true"
href="http://stackoverflow.com/questions/19982340/cmake-compare-to-empty-string-with-strequal-failed">http://stackoverflow.com/questions/19982340/cmake-compare-to-empty-string-with-strequal-failed</a>
which suggests to use if(a MATCHES "^b$").<br>
</div>
</div>
</div>
</div>
</blockquote>
The core of the problem is that command "if(... STREQUAL ...)" has
ambiguous syntax `<variable|string>`. So the result depends on
what the left/right side is (if variable -> use variable value,
if not, use string as-is).<br>
Command `if(a MATCHES ...)` has the same flaw:
`if(<variable|string> MATCHES regex)`:<br>
<br>
# MYSTRING undefined<br>
if(MYSTRING MATCHES "^MYSTRING$")<br>
# go here even MYSTRING is not defined (string "MYSTRING" used)<br>
endif()<br>
<br>
set(MYSTRING "B")<br>
set(A "MYSTRING")<br>
if("${A}" MATCHES "^MYSTRING$")<br>
# do *not* go here even A is MYSTRING (use *variable* MYSTRING)<br>
endif()<br>
<br>
One of the solution that doesn't suffer from ambiguity is
`string(COMPARE EQUAL <string1> <string2> <output
variable>)`:<br>
string(COMPARE EQUAL "${MYSTRING}" "MYSTRING" is_equal)<br>
if(is_equal)<br>
# only if MYSTRING defined and is "MYSTRING"<br>
endif()<br>
<br>
string(COMPARE EQUAL "${A}" "MYSTRING" is_equal)<br>
if(is_equal)<br>
# only if A defined and is a string "MYSTRING" (works even if
MYSTRING variable defined)<br>
endif()<br>
</body>
</html>