變量
cmake中變量不需要聲明,有兩種形式${variable}
和variable
,前者是值引用,是最常見的用法,后者是直接引用,一般只用在賦值和條件判斷中才會使用。
- 賦值:
set(variable arg1 arg2 arg3)
命令調用中的參數
1. Bracket Argument
- 以
[=*[
和]=*]
封閉參數內容,其中前后的=
數量相同; - 我們可以使用任意數量的
=
,只要防止參數中出現和括號中相同數量的=就行; - 其中的
${variable}
不會被替換.
message([=[
This is the first line in a bracket argument with bracket length 1.
No \-escape sequences or ${variable} references are evaluated.
This is always one argument even though it contains a ; character.
The text does not end on a closing bracket of length 0 like ]].
It does end in a closing bracket of length 1.
]=])
2. Quoted Argument
- 使用雙引號
""
封閉參數內容; - 參數中不能包含
"
和\
,可以使用\
進行轉義; - 參數中的
${variable}
會被替換成相應的值; - 如果一行寫不下,可以使用\換行。
message("This is a quoted argument containing multiple lines.
This is always one argument even though it contains a ; character.
Both \\-escape sequences and ${variable} references are evaluated.
The text does not end on an escaped double-quote like \".
It does end in an unescaped double quote.
")
3. Unquoted Argument
- 不使用任何符號封閉參數內容;
- 參數中不可以包含空格,
(
,)
,#
,"
, 或者\
, 可以使用\
轉義; ;
會將參數內容分割成多個參數,可以使用\
轉義;
foreach(arg
NoSpace
Escaped\ Space
This;Divides;Into;Five;Arguments
Escaped\;Semicolon
)
message("${arg}")
endforeach()
注釋
- 單行注釋:以
#
為行首; - 多行注釋:
#[[comment]]
, 還可以可以放在代碼中間;
#[[This is a bracket comment.
It runs until the close bracket.]]
message("First Argument\n" #[[Bracket Comment]] "Second Argument")
流程控制
1. 條件控制
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)
- if(<constant>)
- 為true的constant:
1
,YES
,ON
,TRUE
,Y
, 或者非0數字; - 為false的constant:
0
,NO
,OFF
,FALSE
,N
,IGNORE
,NOTFOUND
, 空字符串, 或者以-NOTFOUND
為后綴的變量; - 以上常量大小寫不限;
- 為true的constant:
- if(variable | string)
- variable被定義為非false的常量時為true,否則為false;
- 邏輯運算:
NOT
,AND
,OR
; - if(COMMAND command-name)
- 判斷給定的名稱是一個可以被調用的命令、宏或者函數;
- if(TARGET target-name)
- 判斷是否是一個已經(在子目錄中)調用
add_library
,add_executable
,add_custom_target
生成的目標;
- 判斷是否是一個已經(在子目錄中)調用
- ...
2. 循環
foreach(loop_var arg1 arg2 ...)
COMMAND1(ARGS ...)
COMMAND2(ARGS ...)
...
endforeach(loop_var)
- 對每個參數進行迭代
foreach(loop_var RANGE total)
foreach(loop_var RANGE start stop [step])
- 迭代生成的數字范圍
例:
set(src_list a.c b.c d.c)
foreach(src ${src_list})
message(${src})
endforeach()
#生成從0到100的數字
foreach(num RANGE 100)
message(${num})
endforeach()
#生成從2到100的數字,間隔為2
foreach(num RANGE 2 100 2)
message(${num})
endforeach()
命令定義
1. 宏
macro(<name> [arg1 [arg2 [arg3 ...]]])
COMMAND1(ARGS ...)
COMMAND2(ARGS ...)
...
endmacro(<name>)
例:
# 求和
macro(sum out)
set(ret 0)
foreach(var ${ARGN})
math(EXPR ret "${ret}+${var}")
endforeach()
set(out ${ret})
endmacro()
sum(outRet 1 2 3 4 5)
message(${outRet})
- 使用
${ARGC}
獲取傳入的參數的數量; - 使用
${ARGV}
獲取傳入的所有參數列表; - 使用
${ARGN}
獲取期望的參數列表(除去在定義宏時指定的參數列表) - 使用
${ARGV#}
獲取指定的參數,其中#
為從0到${ARGC}的值,超出范圍,行為未定義;
2. 函數
function(<name> [arg1 [arg2 [arg3 ...]]])
COMMAND1(ARGS ...)
COMMAND2(ARGS ...)
...
endfunction(<name>)
宏內部的變量在外部是可以訪問的,而函數內部的變量則不行