You ran into a rather annoying "it's not a bug, it's a feature" behavior of CMake. As explained in the documentation of the if command:
The if command was written very early in CMake's history, predating the ${}
variable evaluation syntax, and for convenience evaluates variables named
by its arguments as shown in the above signatures.
Well, the convenience turned out to be an inconvenience. In your example the the string "d"
is treated as a variable named d
by the if
command. If the variable d
happens to be defined to the empty string, the message statement will print "oops...", e.g.:
set (d "")
if("d" STREQUAL "")
# this branch will be taken
message("oops...")
else()
message("fine")
endif()
This can give surprising results for statements like
if("${A}" STREQUAL "some string")
because there can be an unintended double expansion of the first argument if the variable A
happens to be defined to a string which is also the name of a CMake variable, e.g.:
set (A "d")
set (d "some string")
if("${A}" STREQUAL "some string")
# this branch will be taken
message("oops...")
else()
message("fine")
endif()
Possible work-arounds:
You can add a suffix character to the string after the ${}
expansion which prevents the if statement from doing the automatic evaluation:
set (A "d")
set (d "some string")
if("${A} " STREQUAL "some string ")
message("oops...")
else()
# this branch will be taken
message("fine")
endif()
Do not use ${}
expansion:
set (A "d")
set (d "some string")
if(A STREQUAL "some string")
message("oops...")
else()
# this branch will be taken
message("fine")
endif()
To prevent unintended evaluation on the right side of STREQUAL
use MATCHES
with a CMake regular expression instead:
if(A MATCHES "^value$")
...
endif()
Addendum: CMake 3.1 no longer does double expansions for quoted arguments. See the new policy.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…