Skip to content

[fetch-content.*]

拉取外部工程,对应 CMake FetchContent_Declare。支持 git / svn / url / 本地目录 四种来源。

git

toml
[fetch-content.gitcontent]
condition = "mycondition"
git = "https://github.com/myuser/gitcontent"
tag = "v0.1"
shallow = false
system = false
subdir = ""

svn

toml
[fetch-content.svncontent]
svn = "https://svn-host.com/url"
rev = "svn_rev"

url(压缩包)

toml
[fetch-content.urlcontent]
url = "https://content-host.com/urlcontent.zip"
# 这些算法等价(选一个):
# md5, sha1, sha224, sha256, sha384, sha512, sha3_224, sha3_256, sha3_384, sha3_512
hash = "SHA1 502a4e25b8b209889c99c7fa0732102682c2e4ff"
sha1 = "502a4e25b8b209889c99c7fa0732102682c2e4ff"

本地目录(dir)

不拉取,直接指向本地工程目录:

toml
[fetch-content.mylib]
dir = "E:/path/to/mylib"

键名透传

[fetch-content.xxx] 里匹配 CMake 变量名([A-Z_]+)的键会原样传给 FetchContent_Declare。除 git/svn/url/hash/dir 等特殊键外,可按 CMake 参数自由扩展。

状态

[fetch-content] 官方标记未完成,未来版本可能变化。

字段速查

字段来源说明
gitgit仓库地址
taggit拉取 tag
shallowgit浅克隆
systemgit优先用系统包
subdirgit仓库内子目录
svnsvnSVN 地址
revsvnSVN 版本
urlurl压缩包地址
hash / sha1url完整性校验(选一个算法)
dir本地本地目录路径
condition所有条件前缀

实例

toml
[project]
name = "myapp"

[fetch-content.nlohmann_json]
git = "https://github.com/nlohmann/json"
tag = "v3.11.3"

[target.app]
type = "executable"
sources = ["src/main.cpp"]
link-libraries = ["nlohmann_json::nlohmann_json"]

注意事项

  • fetch 引入的 cmkr 工程,其 [options]"root" 特值变 false
  • dir 用绝对路径或相对 cmake.toml 的路径均可
  • 想固定拉取版本,用 tag + hash 双重锁定

下一步