I found a case where the CMake NOT operator doesn’t behave as I expected. I expect that if I negate the condition by prefixing NOT to it then the other branch of the if-statement is taken, but that doesn’t always happen. My CMake code is as follows:
include(CheckSymbolExists)
# cmath contains sin but not nosin
check_symbol_exists(sin "cmath" HAVE_SIN)
check_symbol_exists(nosin "cmath" HAVE_NOSIN)
message("HAVE_SIN: ${HAVE_SIN}")
message("HAVE_NOSIN: ${HAVE_NOSIN}")
if (${HAVE_SIN})
message("sin true")
else()
message("sin false")
endif()
if (NOT ${HAVE_SIN})
message("not sin true")
else()
message("not sin false")
endif()
if (${HAVE_NOSIN})
message("nosin true")
else()
message("nosin false")
endif()
if (NOT ${HAVE_NOSIN})
message("not nosin true")
else()
message("not nosin false")
endif()
My expectation was that I’d get “sin true”, “not sin false”, “nosin false”, and “not nosin true”. I got (with CMake 3.30.2 on Microsoft Windows 11)
-- Looking for sin
-- Looking for sin - found
-- Looking for nosin
-- Looking for nosin - not found
HAVE_SIN: 1
HAVE_NOSIN:
sin true
not sin false
nosin false
not nosin false
So for “nosin” the same branch of the if statement is taken regardless of the use of NOT. Is this as designed? Or is it a bug?
I need some action taken if the sought symbol is not yet available. I thought I could do that using “if (NOT ${HAVE_NOSIN}) do_action endif()” but that doesn’t work. The workaround is to use “if (${HAVE_NOSIN}) else() do_action endif()”.
Best regards,
Louis Strous