On Tue, Oct 25, 2011 at 6:31 AM, Rolf Eike Beer <span dir="ltr"><<a href="mailto:eike@sf-mail.de">eike@sf-mail.de</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">> hi there,<br>
><br>
> how do I test if a string is empty ("") or not ?<br>
> IF( ${test} STREQUAL "" )<br>
> gives the wrong number of arguments if ${test} is ""<br>
<br>
</div>if ("${test}" STREQUAL "")<br>
if (NOT test)<br>
<br>
What does not work (in 2.8.5) but should work according to the documentation:<br>
<br>
if (test STREQUAL "")<br></blockquote><div><br></div><div>Ah, but this does work, and it works precisely according to the documentation. Unfortunately, it is complicated, and confusing to human beings, though...</div>
<div><br></div><div>The construct:</div><div><br></div><div> if (test STREQUAL "")</div><div><br></div><div>is evaluated differently according to context. The context is whether or not there is a variable defined that is named "test".</div>
<div><br></div><div>If there is a variable named test, then we do variable-value comparison:</div><div><div> if (test STREQUAL "")</div></div><div>is equivalent to:</div><div><div><div> if ("${test}" STREQUAL "")</div>
</div></div><div>(It is this way for backwards compatibility with IF commands that were written before we even had ${test} variable expansion in the CMake language. Years ago at this point...)</div><div><br></div><div>If there is no variable named test, then we do string literal comparison:</div>
<div><div><div> if (test STREQUAL "")</div></div><div>is equivalent to:</div><div><div><div> if ("test" STREQUAL "")</div></div></div></div><div><br></div><div>If you are intending string value comparison with variables, I always recommend always using double quotes and always using the form:</div>
<div><div><div><div> if ("${test}" STREQUAL "")</div></div></div></div><div><br></div><div>because it is unambiguous, and the meaning is clearer from reading it.</div><div><br></div><div><br></div><div>
This is all illustrated by the following script:</div><div>(run with cmake -P to see the output does NOT go to the FATAL_ERROR segments.....)</div><div><br></div><div><div># test is not a defined variable at this point, so the following test is a</div>
<div># simple string compare between the left and right arguments, which are</div><div># "test" and ""</div><div>#</div><div>if (test STREQUAL "")</div><div> message(FATAL_ERROR "unexpected: the string literal 'test' is the empty string")</div>
<div>else()</div><div> message("the string 'test' is not the empty string")</div><div>endif()</div><div><br></div><div><br></div><div>set(test "test contents")</div><div><br></div><div># test is defined as a variable at this point, so the following test is a</div>
<div># comparison of the string value of the variable test with the string literal</div><div># "", the empty string</div><div>#</div><div>if (test STREQUAL "")</div><div> message(FATAL_ERROR "unexpected: the variable test is the empty string")</div>
<div>else()</div><div> message("the variable test's value is NOT the empty string")</div><div>endif()</div><div><br></div><div><br></div><div># Same variable-based if command now, but the value *is* now the empty string:</div>
<div>#</div><div>set(test "")</div><div><br></div><div>if (test STREQUAL "")</div><div> message("the variable test's value is the empty string")</div><div>else()</div><div> message(FATAL_ERROR "unexpected: the variable test is NOT the empty string")</div>
<div>endif()</div></div><div><br></div><div><br></div><div>HTH,</div><div>David</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<font color="#888888"><br>
Eike<br>
</font><div><div></div><div class="h5">--<br>
<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" target="_blank">http://www.cmake.org/Wiki/CMake_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://www.cmake.org/mailman/listinfo/cmake" target="_blank">http://www.cmake.org/mailman/listinfo/cmake</a><br>
</div></div></blockquote></div><br>