Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CMake evaluation of compound condition

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
.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading