cmake 语言初识
文件组织
CMake源文件用cmake语言编写,共三种组织形式
Directories (
CMakeLists.txt
) 目录Scripts (
<script>.cmake
) 脚本Modules (
<module>.cmake
) 模块
Directories 目录
用cmake 去编译项目时候,入口是项目顶级目录的CMakeLists.txt
文件,CMakeLists.txt
描述了当前项目构建规则。当遇到add _ subdirectory ()
命令,cmake会去命令指定的子目录中搜索CMakeLists.txt
,将其添加到构建过程,这是一个递归解析的过程。子目录中的CMakeLists.txt
描述了子项目的构建规则。
Scripts 脚本
一个单独的<script>.cmake
文件可以被当做脚本来处理,可以理解为shell脚本,通过cmake来解析和执行脚本定义的操作。脚本中不允许出现定义构建目标和构建行为的命令,
cmake 脚本不参与构建过程。
1 |
|
Process the given cmake file as a script written in the CMake language. No configure or generate step is performed and the cache is not modified. If variables are defined using
-D
, this must be done before the-P
argument.
Any options after
--
are not parsed by CMake, but they are still included in the set ofCMAKE_ARGV<n>
variables passed to the script (including the--
itself).
Modules 模块
就是一个cmake的源文件,形式<module>.cmake
。可以在当前CMakeLists.txt
或cmake源文件中,通过include()
命令,引入其他cmake文件中定义的内容。类似于c语言的
#include<>
。
cmake 内置了很多的模块,可以通过cmake-modules查看。项目可以定义自己cmake modules,通过CMAKE_MODULE_PATH来指定他们的路径。
cmake 变量
cmake 变量的值是字符串类型
Set ()和 unset ()命令显式地设置或取消设置变量
变量名是区分大小写的,建议只使用由字母数字字符加上
_
和-
组成的名称根据作用于范围, 变量的类型有normal, cache, or environment variable
变量作用范围
参考这篇文章cmake cache变量_反复研究好几遍,我才发现关于 CMake 变量还可以这样理解!_weixin_39732534的博客-CSDN博客 可以更好的理解cmake 变量及其作用域范围。
函数作用域
cmake 通过 function()
命令来定义函数。在函数作用域通过set
定义的变量,只在函数作用域内有效,函数返回后,该变量失效。
但是通过set
命令指定 PARENT_SCOPE
,可以设置函数上级作用域内的变量。
CMake 文件中
每个CMakeLists.txt 和 xx.cmake
文件中都定义一个作用域。上级CMakeLists.txt文件中定义的变量,会传递给下级的CMakeLists.txt文件中。
不在函数用通过set
和unset
命令设置的变量,作用范围在当前的文件中。
修改当前CMakeLists.txt文件中定义的变量,在上级CMakeLists.txt文件中不生效,但是通过set
命令指定 PARENT_SCOPE
,可以修改上级作用域内的变量。
cache 中
cache 变量存被保存在CMakeCache.txt
文件中。全局变量,多个构建构建中持续生效。
只能通过set命令,指定CACHE
选项来显示修改。
环境变量
类似于普通变量,但是作用范围是全局,不会被缓存。
变量引用的格式为 $ENV { < Variable > }
CMake 环境变量的初始值是调用进程的初始值。可以使用 set ()和 unset ()命令更改值。这些命令只影响正在运行的 CMake 进程,而不影响整个系统环境。更改后的值不会写回调用进程,后续的构建或测试进程也不会看到它们。
变量求值的顺序:
当前函数内查找
当前文件中查找
缓存中查找
最后没找到,计为空字符串。
可以通过
$CACHE{VAR}
跳过前面的过程,直接从缓存中查找。
set 给变量赋值
normal 变量
1 |
|
如果指定了PARENT_SCOPE,修改会在上级作用域内生效。
Set Cache Entry
1 |
|
不加FORCE,修改不会覆盖之前cache中的值。 FORCE 选项覆盖现有条目。
type的类型必须指定为以下一种:
BOOL
FILEPATH
PATH
STRING
INTERNAL
也可以通过cmake命令,通过
-D<var>=<value>
添加缓存条目。
环境变量
1 |
|
设置给定值的环境变量,$ENV{<variable>}
的后续调用将返回这个新值。
此命令只影响当前的 CMake 进程,而不影响调用 CMake 的进程,也不影响整个系统环境,也不影响后续构建或测试流程的环境。
如果在 ENV { < variable > }
之后没有给出参数,或者如果 < value >
是一个空字符串,那么这个命令将清除任何已存在的环境变量
list 变量
Note A list in cmake is a
;
separated group of strings. To create a list the set command can be used. For example,set(var a b c d e)
creates a list witha;b;c;d;e
, andset(var "a b c d e")
creates a string or a list with one item in it. (Note macro arguments are not variables, and therefore cannot be used in LIST commands.)
列表不应该用于复杂的数据处理任务,常常用来保存源文件列表。
大多数构造列表的命令不会转义; 列表元素中的字符,因此将嵌套列表展开:
1 |
|
list操作,就是一些增删查改
1 |
|
file 命令
1 |
|
Filesystem
1 |
|
Generate a list of files that match the
<globbing-expressions>
and store it into the<variable>
. Globbing expressions are similar to regular expressions, but much simpler. IfRELATIVE
flag is specified, the results will be returned as relative paths to the given path.On Windows and macOS, globbing is case-insensitive even if the underlying filesystem is case-sensitive (both filenames and globbing expressions are converted to lowercase before matching). On other platforms, globbing is case-sensitive
By default
GLOB
lists directories - directories are omitted in result ifLIST_DIRECTORIES
is set to false.
Examples of globbing expressions include:
1 |
|
The
GLOB_RECURSE
mode will traverse all the subdirectories of the matched directory and match the files.
cmake 执行shell命令
通过 execute_process 可以执行调用shell 命令
1 |
|