zoukankan      html  css  js  c++  java
  • cmake 语法

    语法说明

    CMakeLists.txt 文件遵循一种简单的语法包括 注释,命令和空格字符。注释使用#符号,从符号开始之后的一行都表示注释。命令包括命令名,左括号,分隔参数的空白字符和右括号。命令既可以是一个内置命令如 add_library,也可以是自定义的宏和函数。输入CMake的源目录是CMakeList.txt文件。这个文件也可以使用include和add_subdirectory命令添加外部输入文件。

    所有的空白符号(空格,换行,制表符)除了分隔参数外被忽略。对大多数语言来说,任何加了双引号的符号都被当成一个参数。反斜杠可以用来转义。

    每一个命令按在CMakefile文件中出现的顺序评估。命令有如下形式

     command (args...)

    其中command是命令,宏或者函数的名称,args是一个用空白符分隔的参数表。(嵌入空白符的参数必须使用双引号) CMake 大小写不敏感。所以可以用COMMAND和Command互相代替。

    列表和字符串

    在CMake中基础的数据形式是字符串。CMake也支持字符串列表。

    列表通过分号分隔。譬如两个声明给变量VAR设同样的值:

    set(VAR a;b;c)     set(VAR a b c)

    字符串列表可以通过foreach命令迭代或直接操控列表命令。

    变量

    CMake 支持简单的变量可以是字符串也可以是字符串列表。变量参考使用${VAR}语法。多参数可以使用set命令组合到一个列表中。所有其他的命令

    通过空白分隔符传递命令来扩展列表,例如

    set(Foo a b c)

    将 变量 Foo 设为 a b c, 并且如果Foo 传递给另一个命令

    command(${Foo})

    等同于

    command(a b c)

    如果要把参数列表传递给一个命令,且它是一个简单的参数只要加一个双引号就可以。例如

    command("${Foo}")

    将执行只传递一个参数的命令等价于

    command("a b c")

    控制流

    不用方法写CMakeLists文件就像用一种简单语言写程序。像大多数语言一样,Cmake 提供了控制流结构。Cmake提供了三中控制流:

    1: 条件控制流 if

    # some_command will be called if the variable's value is not: 
    # empty, 0, N, NO, OFF, FALSE, NOTFOUND, or -NOTFOUND. 
    if(var) 
       some_command(...) 
    endif(var) 

    2:  循环结构: foreach 和 while

    set(VAR a b c) 
      # loop over a, b,c with the variable f 
    foreach(f ${VAR}) 
        message(${f}) 
    endforeach(f) 

    3: 过程定义 宏和函数(函数在2.6及更高的版本中有效)。函数对变量局部有效,宏是全局有效。

    # define a macro hello 
    macro(hello MESSAGE)
        message(${MESSAGE})
    endmacro(hello) 
    # call the macro with the string "hello world" 
    hello("hello world") 
    # define a function hello 
    function(hello MESSAGE)
        message(${MESSAGE}) 
    endfunction(hello)

    更多控制流信息参见命令 if,while,foreach,macro,function文档。

    引号,字符串和转义

    在CMake中原义字符串用双引号括起来。字符串可以是多行字符串,并在其中嵌入新的行。例如

    set(MY_STRING "this is a string with a
    
     newline in
    
    it")

    也可以在一个字符串中转义字符和使用变量

    set(VAR "
    
      hello
    
      world
    
    ")
    
    message("${VAR}= ${VAR}")
    
    # prints out
    
    ${VAR}=
    
    hello
    
    world

    同样支持标准C中的转义

    message("
    	hello world")
    
    #prints out
    
    hello world

    如果字符在引号之前是空格则原义字符串只是原义字符串。例如

    message(hell"o") -> prints hell"o"
    
    message(hell"o") -> prints hell"o"
    
    message(hell"o") -> prints hello"o"

    然而引号必须成对,一下就是一个错误

    message(hell"o) -> produces this error:
    Parse error.Function missing ending ")".
    Instead found unterminated string with text "o)
    
    ".
    
    message(hell"o) -> prints hell"o

    正则表达式

    一些CMake命令,比如if 和 string,使用正则表达式或者用一个正则表达式来代替一个参数。最简单的形式,一个正则表达式就是字符序列中精确查询匹配字符。

    然而,很多次这个精确序列式找不到的,或者只匹配了字符串中期望的头尾部分。由于对特定的正则表达式有许多不同的约定,CMake的标准描述如下。描述是基于Texas Instruments开源的正则表达式类。

    正则表达式可以使用标准文字数值字符和如下正则表达式元字符的结合

    • ^ Matches at beginning of a line or string
    • $ Matches at end of a line or string
    • . Matches any single character other than a newline
    • [ ] Matches any character(s) inside the brackets
    • [^ ] Matches any character(s) not inside the brackets
    • [-] Matches any character in range on either side of a dash
    • * Matches preceding pattern zero or more times
    • + Matches preceding pattern one or more times
    • ? Matches preceding pattern zero or once only
    • () Saves a matched expression and uses it in a later replacement
  • 相关阅读:
    案例详解:MTU不一致导致主机和RAC不断重启
    近千人观看live,晚8点继续安排,2个CPU过高案例+1个文件数据删除案例->Oracle故障分析的方法论+DBA能力提升要领...
    一个模版让报表自动生成,领导:这才是数据分析人该干的事
    如何构造一个 SYN_SENT 状态的连接
    TCP 3次握手原理
    SpringCloud Alibaba微服务番外一
    socket bind 随机端口
    Yii项目Security加密解密类提取
    linux中iptables配置文件及命令详解详解
    linux中iptables配置文件及命令详解详解
  • 原文地址:https://www.cnblogs.com/is-smiling/p/3269059.html
Copyright © 2011-2022 走看看