Skip to content

语法总览

cmake.toml 是标准的 TOML 文件。理解三种语法后即可上手:顶层表条件键可见性前缀

文件结构

顶层只有两类表:单例配置表[project][cmake] 等)和具名表[target.xxx][conditions] 等)。

一个完整最小文件:

toml
[project]
name = "myproject"
version = "1.0.0"

[cmake]
version = "3.15"

[target.app]
type = "executable"
sources = ["src/main.cpp"]

值类型

类型写法示例
字符串双引号name = "myproject"
布尔true / falseshallow = false
数组方括号sources = ["a.cpp", "b.cpp"]
多行字符串三引号cmake-before = """..."""
[key][target.app]

多行字符串用于内嵌原生 CMake 代码:

toml
[project]
cmake-after = """
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
"""

TOML 合法子集

所有键值都是合法 TOML。数组元素可跨行,字符串可含转义;多行字符串 """ 里可以自由写 CMake 的 " 和换行,不必转义。

条件键:condition.key

大多数键可加 条件前缀,编译期按条件展开成 if() 块:

toml
[target.app]
sources = ["src/main.cpp"]
windows.sources = ["src/win_main.cpp"]      # 仅 Windows 额外编译
x64.definitions = ["IS_64BIT"]              # 仅 64 位定义宏

展开结果:

cmake
add_executable(app src/main.cpp)
if(WIN32)
  target_sources(app PRIVATE src/win_main.cpp)
endif()
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  target_compile_definitions(app PRIVATE IS_64BIT)
endif()

condition. 可加在任意键前:windows.compile-optionslinux.link-librariesroot.cmake-after……条件名来自预定义条件或自定义 [conditions],也可以是带引号的 CMake 表达式。详见 条件系统

可见性前缀:private-

对应 target_xxx 命令的属性键默认可见性取决于 target 类型(见 target)。需要私有时加 private- 前缀:

toml
[target.lib]
type = "static"
include-directories = ["include"]          # PUBLIC:传播给消费者
private-compile-options = ["-Wall"]        # PRIVATE:仅自己

顶层表清单(按使用频率排序)

顶层表用途详见
[project]项目名 / 版本 / 语言 / CMake 注入project
[target.*]构建目标(含 post-build / win32-executable)target
[cmake]CMake 版本 / 引导脚本 / utf-8cmake
[conditions]自定义条件conditions
[options]CMake 缓存选项 → 条件options
[variables]发射 set() 的变量variables
[fetch-content.*]拉取外部依赖fetch-content
[vcpkg]vcpkg 包管理vcpkg
[find-package.*]find_package()find-package
[subdir.*]子目录subdir
[template.*]自定义 target 类型template
[[target.*.custom-command]]add_custom_command / add_custom_targetcustom-command
[[test]]CTest 测试(未完成)test
[[install]]安装规则(未完成)install

键 → CMake 构造映射

多数键名对应同名 CMake 命令(去掉短横线):

cmake.toml 键CMake 构造
sourcestarget_sources
compile-definitionstarget_compile_definitions
compile-optionstarget_compile_options
include-directoriestarget_include_directories
link-librariestarget_link_libraries
link-optionstarget_link_options
precompile-headerstarget_precompile_headers
[target.x.properties]set_target_properties

下一步