1. if 条件
if 条件: if
, elseif
, else
, endif
,支持语法如下,
# if
if(<condition>)
# elseif
elseif(<condition>)
# else
else([<condition>])
# endif
endif([<condition>])
PS: endif() 命令中
<condition>
仅用于向后兼容。如果使用它,必须于 if() 命令参数相同。
概要
if(<condition>)
<commands>
elseif(<condition>) # optional block, can be repeated
<commands>
else() # optional block
<commands>
endif()
2. foreach 循环
for 循环:: foreach
, endforeach
,支持语法如下,
# foreach
foreach(<loop_var> <items>)
# endforeach
endforeach([<loop_var>])
概要
foreach(<loop_var> <items>)
<commands>
endforeach()
变体一:
foreach(<loop_var> <items>)
其中 <items>
是由分号或空格分隔的项目列表。每次迭代开始变量 <loop_var>
被设置为当前项的值。
变体二:
foreach(<loop_var> IN [LISTS [<lists>]] [ITEMS [<items>]])
在这个变体中,<lists>
是列表变量,其值由分号或空格分隔。
跟在 ITEMS
关键字后面的 <items>
想 foreach 命令的第一个变体中一样处理。LISTS A
和 ITEMS ${A}
是等价形式。
案例: 如何处理列表
# 列表定义
set(A 0;1)
set(B 2 3)
set(C "4 5")
set(D 6;7 8)
set(E "")
# 变体一
foreach(X ${A} ${B} ${C} ${D} ${E})
message(STATUS "X=${X}")
endforeach()
# 变体二,等价于 foreach(X IN LISTS A B C ITEMS ${D} ${E})
foreach(X IN LISTS A B C D E)
message(STATUS "X=${X}")
endforeach()
变体三:
foreach(<loop_var> RANGE <stop>)
这个变体中,遍历数字 0, 1, … 直到(并包括) 整数 <stop>
。
变体四:
foreach(<loop_var> RANGE <start> <stop> [<step>])
这个变体,有起始值 <start>
和 步长 <step>
案例: 如何处理 1-10
# 变体三
foreach(X RANGE 10)
message(STATUS "X=${X}")
endforeach()
# 变体四
foreach(X RANGE 0 10 2)
message(STATUS "X=${X}")
endforeach()
变体五:
foreach(<loop_var>... IN ZIP_LISTS <lists>)
在这个变体中,<lists>
是列表变量,其值由分号或空格分隔。
这个变量主要用于打包多个列表变量的值。
foreach 同时迭代所有的列表,
- 如果给定一个变量
<loop_var>
,那么它给一系列的 <loop_var_N> 变量设置对应列表的当前项。 - 如果给定多个变量,那么列表数目与变量数目匹配。
- 如果列表的长度不一致,较短的列表对应的变量项为空。
案例: 打包两个长度不一致的列表的项
list(APPEND English one two three four)
list(APPEND Bahasa satu dua tiga)
foreach(num IN ZIP_LISTS English Bahasa)
message(STATUS "num_0=${num_0}, num_1=${num_1}")
endforeach()
foreach(en ba IN ZIP_LISTS English Bahasa)
message(STATUS "en=${en}, ba=${ba}")
endforeach()
3. while 循环
while 循环: while
, endwhile
,支持语法如下,
# while
while(<condition>)
# endwhile
endwhile([<condition>])
如果 while
命令,条件为真,则一直执行。
概要
while(<condition>)
<commands>
endwhile()
4. 跳出循环
break
和 continue
支持 foreach 循环
和 while 循环
。
我们使用 break()
命令提前终止循环,而使用 continue()
命令可用于立即开始下一次迭代。
5. condition
条件语法
condition
条件语法支持 if(<condition>)
, elseif(<condition>)
and while(<condition>)
。
布尔常量
True 常量为,1, ON, YES, TRUE, Y, or 非零数字.
False 常量为,0, OFF, NO, FALSE, N, IGNORE, NOTFOUND, 空字符串, 后缀为 -NOTFOUND。
布尔常量命名不区分大小写。
if(<constant>)
如果参数不是这些特定常量,则被视为变量或字符串。
案例: 如果常量为 True,则输出,如果常量为 False,则不输出。
if(ON)
message("ON")
endif()
if(OFF)
message("OFF")
endif()
if(<variable|string>)
给定的变量被定义为一个非 False 常量,则被视为 True。表示变量被使用。
案例: 判断变量是否被定义为一个非 False 常量,两种形式都可以。
if(DEBUG)
message("DEBUG: ${DEBUG}")
endif()
if(${DEBUG})
message("DEBUG: ${DEBUG}")
endif()
运行 cmake .. -DDEBUG=Y -L
,会打印信息。
PS: if() 命令由一个特殊的条件语法,允许以短形式
<variable>
,而不是${<variable>}
。
变量
if(DEFINED <name>|CACHE{<name>}|ENV{<name>})
如果给定的名称为被定义的变量、缓存变量或环境变量,则为真。变量值无关紧要。
与 if(<variable|string>)
相比,if(<variable|string>)
是用来判断变量的值,而 if(DEFINED ...)
用来判断变量是否被定义。
案例: 判断变量是否被定义
if(DEFINED DEBUG)
message("DEBUG: ${DEBUG}")
endif()
运行 cmake .. -DDEBUG=N -L
,也会打印信息。
逻辑运算
# 否
if(NOT <condition>)
# 与
if(<cond1> AND <cond2>)
# 或
if(<cond1> OR <cond2>)
# 逻辑组合
if((condition) AND (condition OR (condition)))
判断命令是否存在
if(COMMAND command-name)
如果给定的名称为可调用的命令、宏、函数名,则为真。
判断策略是否存在
if(POLICY policy-id)
如果给定的名称为现有的策略(形式为 CMP<NNNN>
),则为真。
判断目标是否存在
if(TARGET target-name)
如果给定的名称为现有的目标,用 add_executable(), add_library(), add_custom_target() 命令创建,则为真。
判断测试是否存在
if(TEST test-name)
如果给定的名称为现有的测试,用 add_test() 命令创建,则为真。
路径
if(EXISTS path-to-file-or-directory)
如果给定的文件或目录存在,则为真。仅适用于完整路径。支持符号链接。
if(file1 IS_NEWER_THAN file2)
如果 file1 比 file2 新 或者 两个文件之一不存在,则为真。仅适用于完整路径。
if(IS_DIRECTORY path-to-directory)
如果给定名称是目录,则为真。仅适用于完整路径。
if(IS_SYMLINK file-name)
如果给定名称是符号链接,则为真。仅适用于完整路径。
if(IS_ABSOLUTE path)
如果给定名称是绝对路径,则为真。仅适用于完整路径。
字符串匹配
if(<variable|string> MATCHES regex)
数字的比较
比较数字的大小
# 小于
if(<variable|string> LESS <variable|string>)
# 大于
if(<variable|string> GREATER <variable|string>)
# 等于
if(<variable|string> EQUAL <variable|string>)
# 小于或等于
if(<variable|string> LESS_EQUAL <variable|string>)
# 大于或等于
if(<variable|string> GREATER_EQUAL <variable|string>)
字符串的比较
按字典的顺序,比较字符串的大小。
if(<variable|string> STRLESS <variable|string>)
if(<variable|string> STRGREATER <variable|string>)
if(<variable|string> STREQUAL <variable|string>)
if(<variable|string> STRLESS_EQUAL <variable|string>)
if(<variable|string> STRGREATER_EQUAL <variable|string>)
版本的比较
比较版本的大小。
版本格式为 major[.minor[.patch[.tweak]]]
,省略的组件被视为零
if(<variable|string> VERSION_LESS <variable|string>)
if(<variable|string> VERSION_GREATER <variable|string>)
if(<variable|string> VERSION_EQUAL <variable|string>)
if(<variable|string> VERSION_LESS_EQUAL <variable|string>)
if(<variable|string> VERSION_GREATER_EQUAL <variable|string>)
元素是否在列表中
if(<variable|string> IN_LIST <variable>)
如果给定元素在列表中,则为真。