語法說明
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("\n\thello 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
- ^ 匹配一行或一字符串開頭
- $匹配一行或一字符串結尾
- .匹配單一字符或一個新行
- [ ]匹配括號中的任一字符
- [^ ] 匹配不在括號內的任一字符
- [-] 匹配指定范圍內的字符
- * 匹配0次或多次
- + 匹配一次或多次
- ? 匹配0次或一次
- ()保存匹配的表達式並用隨后的替換它