CMake也是有语法的,这里总结一些。CMake系列学习个人笔记:
分支 if/elseif/else/endif
分支的应用十分广泛,可以用于判断值、比较大小、判断文件是否存在、版本比较、新旧比较等等。语法格式如下:
if(<condition>)
<commands>
elseif(<condition>)
<commands>
else()
<commands>
endif()
例子:
if(NOT <condition>)
if(<cond1> AND <cond2>)
if(<cond1> OR <cond2>)
if((condition) AND (condition OR (condition)))
if(TARGET target-name)
if(DEFINED <name>|CACHE{<name>}|ENV{<name>})
if(<variable|string> IN_LIST <variable>)
if(EXISTS path-to-file-or-directory)
if(file1 IS_NEWER_THAN file2)
if(IS_DIRECTORY path-to-directory)
if(<variable|string> LESS|GREATER|EQUAL|LESS_EQUAL|GREATER_EQUAL| \
STRLESS|STRGREATER|STREQUAL|STRLESS_EQUAL|STRGREATER_EQUAL <variable|string>)
列表 list
很像python的语法,支持插入、查找、删除、读取、排序、反转。
Reading
list(LENGTH <list> <out-var>)
list(GET <list> <element index> [<index> ...] <out-var>)
list(JOIN <list> <glue> <out-var>)
list(SUBLIST <list> <begin> <length> <out-var>)
Search
list(FIND <list> <value> <out-var>)
Modification
list(APPEND <list> [<element>...])
list(FILTER <list> {INCLUDE | EXCLUDE} REGEX <regex>)
list(INSERT <list> <index> [<element>...])
list(POP_BACK <list> [<out-var>...])
list(POP_FRONT <list> [<out-var>...])
list(PREPEND <list> [<element>...])
list(REMOVE_ITEM <list> <value>...)
list(REMOVE_AT <list> <index>...)
list(REMOVE_DUPLICATES <list>)
list(TRANSFORM <list> <ACTION> [...])
Ordering
list(REVERSE <list>)
list(SORT <list> [...])
例子:
set(test_list 0 2 3 1 4 5 6)
list(LENGTH test_list output_length)
message("The length of test_list is ${output_length}")
list(GET test_list 5 5th_val)
message("5th_val is ${5th_val}")
list(SORT test_list ORDER DESCENDING)
message("\nlist sort result:")
foreach(x IN LISTS test_list)
message(STATUS "x = ${x}")
endforeach()
结果:
The length of test_list is 7
5th_val is 5
list sort result:
-- x = 6
-- x = 5
-- x = 4
-- x = 3
-- x = 2
-- x = 1
-- x = 0
循环 foreach/while/break/continue
foreach即可以用来循环计数又可以遍历列表:
foreach(<loop_var> <items>)
<commands>
endforeach()
foreach(<loop_var> RANGE <stop>)
foreach(<loop_var> RANGE <start> <stop> [<step>])
foreach(<loop_var> IN [LISTS [<lists>]] [ITEMS [<items>]])
while(<condition>)
<commands>
endwhile()
例子:
message("\nforeach RABGE test result:")
foreach(i RANGE 0 2)
message(STATUS "i = ${i}")
endforeach()
set(file_groups output.txt output.log output.error)
foreach(file_name IN LISTS file_groups)
if(EXISTS "${CMAKE_SOURCE_DIR}/${file_name}")
message("output file exists, file is in ${CMAKE_SOURCE_DIR}/${file_name}")
endif()
endforeach()
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()
set(condition OFF)
while(NOT condition)
message("This is a while loop")
set(condition ON)
endwhile()
结果:
foreach RABGE test result:
-- i = 0
-- i = 1
-- i = 2
output file exists, file is in /home/sjl/learn/cmake-examples/grammer/output.log
-- num_0=one, num_1=satu
-- num_0=two, num_1=dua
-- num_0=three, num_1=tiga
-- num_0=four, num_1=
-- en=one, ba=satu
-- en=two, ba=dua
-- en=three, ba=tiga
-- en=four, ba=
This is a while loop
函数
函数声明必须在使用之前。格式如下:
function(<name> [<arg1> ...])
<commands>
endfunction()
例子,判断条件并设定值,如果依赖条件成立设置为ON,如果不成立设置为OFF(FORCE设置),这里的函数功能和官方cmake_dependent_option()函数用法一致,先看一下这个函数的用法:
cmake_dependent_option(<option> "<help_text>" <value> <depends> <force>)
cmake_dependent_option(USE_FOO "Use Foo" ON
"USE_BAR;NOT USE_ZOT" OFF)
这里如果depends支持,则设置值为value(CACHE设置),如果不支持则设置为force,这是强制设置。我们来实现一下这个函数:
function(sync_test target_config depend_config str)
if(${depend_config})
set(${target_config} OFF CACHE BOOL ${str})
endif()
endfunction()
|