[CMake entry and advanced (8)] CMakeLists.txt grammar rules (with code)

The role of double quotes

        In CMake, we can introduce the role of double quotes from two aspects, command parameters and reference variables.

        command parameters

        When calling a command, parameters can use double quotes, for example:

project("HELLO")

        You can also not use double quotes, for example:

project(HELLO)

        So what's the difference between them? In this example, there is no difference. Spaces are used to separate multiple parameters in the command, and cmake will treat the content enclosed in double quotes as a whole as a parameter. If there are spaces in your parameters (space is part of the parameter), then you can use double quotes like this:

message(Hello World)
message("Hello World")

        In this example, the first message command passes in two parameters, while the second message command only passes in one parameter; in the first message command, when printing the message, two separate strings Hello and World are printed out, and World will be followed by Hello, as follows:

HelloWorld

        The second message command has only one parameter, so the printed information is as follows:

Hello World

        This is a function of double quotes in parameters.

        reference variable

        Let's look at an example first, as follows:

# CMakeLists.txt
set(MY_LIST Hello World China)
message(${MY_LIST})

        The print information of this example is as follows:

Hello;World;China

        Because ${MY_LIST} is a list at this time, when we use the form "${MY_LIST}", it means that we want CMake to treat all elements of this array as a whole, rather than scattered individuals. Therefore, in order to maintain the meaning of the array and provide an overall expression, CMake will use a semicolon ";" to connect multiple elements of the array.

        And if you do not add double quotes, CMake will not treat the array as a whole, but will extract each element in the array and print it out.

conditional judgment

        Conditional judgment can be used in cmake, and the form of conditional judgment is as follows:

if(expression)
 # then section.
 command1(args ...)
 command2(args ...)
 ...
elseif(expression2)
 # elseif section.
 command1(args ...)
 command2(args ...)
 ...
else(expression)
 # else section.
 command1(args ...)
 command2(args ...)
 ...
endif(expression)

        The else and endif brackets can be written or not. If written, it must be consistent with the if.

        expression is an expression for judgment, and the expression comparison table is as follows:

expression true false illustrate
<constant> True if constant is 1, ON, YES, TRUE, Y or a non-zero number False if constant is 0, OFF, NO, FALSE, N, IGNORE, NOTFOUND, an empty string, or ends with the suffix NOTFOUND. Booleans are case-insensitive; if none of these constants match, it is treated as a variable or string
<variable|string> Variables that are already defined and not false Variables that are undefined or false variables are strings
NOT<expression> expression is false expression is true
<expr1>AND<expr2> expr1 and expr2 are both true At least one of expr1 and expr2 is false
<expr1>OR<expr2> At least one of expr1 and expr2 is true Both expr1 and expr2 are false
COMMAND name name is an already defined command, macro or function name is undefined
TARGET name name is a target defined by add_executable(), add_library() or add_custom_target() name is undefined
TEST name name is the name of an existing test created by the add_test() command name not created
EXISTS path The file or directory specified by path exists The file or directory specified by path does not exist Only works with full paths
IS_DIRECTORY path The path specified by path is a directory The path specified by path is not a directory Only works with full paths
IS_SYMLINK path path is a symbolic link path is not a symbolic link Only works with full paths
IS_ABSOLUTE path path is an absolute path path is not an absolute path
<variable|string>MATCHES regex variable matches the regular expression regex successfully variable failed to match the regular expression regex
<variable|string>IN_LIST<variable> The list on the right contains the elements on the left The list on the right does not contain the elements on the left
DEFINED<varible> True if the given variable is defined. If the given variable is undefined As long as the variable has been set, it doesn't matter if it's true or false. (Note that macros are not variables.)
<variable|string>LESS<variable|string> True if the value of the given string or variable is a valid number and less than the number to the right. The number on the left is greater than or equal to the number on the right
<variable|string>GREATER<variable|string> True if the value of the given string or variable is a valid number and greater than the number to the right. The number on the left is less than or equal to the number on the right
<variable|string>EQUAL<variable|string> True if the value of the given string or variable is a valid number and equal to the value on the right The number on the left is not equal to the number on the right

        The above table only lists some of the expressions, and some other expressions are not listed here. You can check it through this link address if — CMake 3.5.2 Documentation . Now we explain the expressions in the above table in detail .

  • <constant>

        In the if (constant) condition judgment, if the constant is 1, ON, YES, TRUE, Y or a non-zero number, then the if condition is true; if the constant is 0, OFF, NO, FALSE, N, IGNORE, NOTFOUND, If it is an empty string or ends with the suffix -NOTFOUND, then the result of this conditional judgment is false.

        在 cmake 中,可以把 1、ON、YES、TRUE、Y 或非零数字以及 0、OFF、NO、FALSE、N、IGNORE、 NOTFOUND、空字符串或以后缀-NOTFOUND 结尾这些理解为常量,类似于布尔值,而且它们不区分大小写;如果参数不是这些特定常量之一,则将其视为变量或字符串,并使用除之外的表达式。   

if(ON)
    message(true)
else()
    message(false)
endif()

        输出为:true

if(YES)
    message(true)
else()
    message(false)
endif()

        输出为:true

if(true)
    message(true)
else()
    message(false)
endif()

        输出为:true

if(100)
    message(true)
else()
    message(false)
endif()

        输出为:true

if(0)
    message(true)
else()
    message(false)
endif()

        输出为:false

if(N)
    message(true)
else()
    message(false)
endif()

        输出为:false

if(NO)
    message(true)
else()
    message(false)
endif()

        输出为:false

  • <variable/string>

        在 if(<variable/string>)条件判断中,如果变量已经定义,并且它的值是一个非假常量,则条件为真;否则为假,注意宏参数不是变量(在 cmake 中也可以使用宏,这个后面再给大家介绍)。

set(GG Hello)
if(GG)
    message(true)
else()
    message(false)
endif()

        输出为:true

set(GG NO)
if(GG)
    message(true)
else()
    message(false)
endif()

        输出为:false

if(GG)
    message(true)
else()
    message(false)
endif()

        输出为:false

  • NOT<expression>

        NOT 其实就类似于 C 语言中的取反,在 if(NOT)条件判断中,如果表达式 expression 为真, 则条件判断为假;如果表达式 expression 为假,则条件判断为真。

if(NOT GG)
    message(true)
else()
    message(false)
endif()

        输出为:true

        因为 GG 变量没有定义,所以 GG 表达式为假,但因为前面有 NOT 关键字,进行取反操作,整个 if 条件判断为真。

if(NOT YES)
    message(true)
else()
    message(false)
endif()

        输出为:false

if(NOT 0)
    message(true)
else()
    message(false)
endif()

        输出为:true

  • <expr1> AND <expr2>

        这个就类似于 C 语言中的逻辑与(&&),只有 expr1 和 expr2 同时为真时,条件判断才为真;否则条件判断为假。

if(yes AND on)
    message(true)
else()
    message(false)
endif()

        输出为:true

if(yes AND no)
    message(true)
else()
    message(false)
endif()

        输出为:false

if(false AND no)
    message(true)
else()
    message(false)
endif()

        输出为:false

  • <expr1>OR<expr2>

       类似于 C 语言中的逻辑或(||),当 expr1 或 expr2 至少有一个为真时,条件判断为真;否则为假。

if(false OR no)
    message(true)
else()
    message(false)
endif()

        输出为:false

if(yes OR no)
    message(true)
else()
    message(false)
endif()

        输出为:true

if(ON OR yes)
    message(true)
else()
    message(false)
endif()

        输出为:true


未完待续......

Guess you like

Origin blog.csdn.net/cj_lsk/article/details/131212424