CMake 在复合 'if' 条件下以什么顺序评估 OR 和 AND?
In what order does CMake evaluate OR and AND in compound 'if' condition?
CMake 文档指出:
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.
但是下面的打印 'FALSE':
cmake_minimum_required(VERSION 3.22)
project(Test)
if(YES OR NO AND NO)
message("TRUE")
else()
message("FALSE")
endif()
我希望表达式的计算结果为 YES OR (NO AND NO)
。怎么回事?
不幸的是,这不是实现中的错误,而是文档中的错误。 CMake 被(错误)设计为以相同的优先级从左到右评估 AND
和 OR
。
查看将在此处更新文档的 MR:https://gitlab.kitware.com/cmake/cmake/-/merge_requests/6970
CMake 文档指出:
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.
但是下面的打印 'FALSE':
cmake_minimum_required(VERSION 3.22)
project(Test)
if(YES OR NO AND NO)
message("TRUE")
else()
message("FALSE")
endif()
我希望表达式的计算结果为 YES OR (NO AND NO)
。怎么回事?
不幸的是,这不是实现中的错误,而是文档中的错误。 CMake 被(错误)设计为以相同的优先级从左到右评估 AND
和 OR
。
查看将在此处更新文档的 MR:https://gitlab.kitware.com/cmake/cmake/-/merge_requests/6970