The CMake documentation states:
The following syntax applies to the condition argument of the if,
elseif and while() clauses.Compound conditions are evaluated in the following order of
precedence: Innermost parentheses are evaluated first. Next come unary
tests such as EXISTS, COMMAND, and DEFINED. Then binary tests such as
EQUAL, LESS, LESS_EQUAL, GREATER, GREATER_EQUAL, STREQUAL, STRLESS,
STRLESS_EQUAL, STRGREATER, STRGREATER_EQUAL, VERSION_EQUAL,
VERSION_LESS, VERSION_LESS_EQUAL, VERSION_GREATER,
VERSION_GREATER_EQUAL, and MATCHES. Then the boolean operators in the
order NOT, AND, and finally OR.
But the following prints ‘FALSE’:
cmake_minimum_required(VERSION 3.22)
project(Test)
if(YES OR NO AND NO)
message("TRUE")
else()
message("FALSE")
endif()
I’d expect the expression to evaluate as YES OR (NO AND NO). What’s going on?
>Solution :
This is clearly a bug… putting the true/false values in variables works as expected:
# test.cmake
cmake_minimum_required(VERSION 3.22)
set(X "YES")
set(Y "NO")
if(X OR Y AND Y)
message("TRUE")
else()
message("FALSE")
endif()
$ cmake -P test.cmake
TRUE
Quoting to avoid variable expansion doesn’t help either:
# test.cmake
cmake_minimum_required(VERSION 3.22)
if("YES" OR "NO" AND "NO")
message("TRUE")
else()
message("FALSE")
endif()
$ cmake -P test.cmake
FALSE