zaufi
(Alex Turbov)
June 28, 2021, 9:07am
1
Hi,
I’ve faced w/ contr-intuitive behavior when trying to check for empty string:
cmake_policy(VERSION 3.20)
message(STATUS "ENABLE_LIBCXX=`${ENABLE_LIBCXX}`")
if(NOT ENABLE_LIBCXX STREQUAL "")
list(APPEND _extra_args "-DENABLE_LIBCXX=${ENABLE_LIBCXX}")
endif()
message(STATUS "_extra_args=`${_extra_args}`")
which results in the following output:
$ cmake -P check-string.cmake
-- ENABLE_LIBCXX=``
-- _extra_args=`-DENABLE_LIBCXX=`
I can’t understand how if
condition could be passed with undefined/empty value of ENABLE_LIBCXX
??
$ cmake --version | head -n1
cmake version 3.20.5
zaufi
(Alex Turbov)
June 28, 2021, 9:09am
2
Looks like ENABLE_LIBCXX
interpreted as string , not as a variable name (even being unquoted)… ???
fenrir
(Jakub Zakrzewski)
June 28, 2021, 9:20am
3
See this: https://cmake.org/cmake/help/latest/command/if.html#variable-expansion
What it means is that the unquoted value is first checked for being a DEFINED VARIABLE, and if yes, then expanded. Your ENABLE_LIBCXX
is not a defined variable, so it’s treated as a string.
1 Like
ben.boeckel
(Ben Boeckel (Kitware))
June 28, 2021, 12:20pm
4
Yes, this is a long-standing pothole. Your check should be if (DEFINED ENABLE_LIBCXX AND NOT ENABLE_LIBCXX STREQUAL "")
2 Likes
McMartin
(Alain Martin)
June 28, 2021, 1:58pm
5
Using
if(NOT "${ENABLE_LIBCXX}" STREQUAL "")
should also work as expected.
2 Likes