Skip to content

Commit fd50e79

Browse files
authored
docs: add mcpp.toml project file guide (#22)
New `docs/05-mcpp-toml.md` covering: - Minimal examples (bin + lib, 3-5 lines each) - Full field reference ([package], [targets], [build], [lib], [dependencies], [dev-dependencies], [toolchain]) - 6 practical examples (hello world, modular lib + gtest, multi-dep app, pure C lib, mixed C/C++23, cross-compile static release) - Convention & defaults cheat sheet Also updates README.md doc section and docs/README.md index.
1 parent 8889f3c commit fd50e79

3 files changed

Lines changed: 301 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ mcpp run
7272
- [发布打包](docs/02-pack-and-release.md)
7373
- [工具链管理](docs/03-toolchains.md)
7474
- [从源码构建 & 参与贡献](docs/04-build-from-source.md)
75+
- [mcpp.toml 工程文件指南](docs/05-mcpp-toml.md)
7576

7677
任意命令的完整选项可通过 `mcpp <cmd> --help` 查阅。
7778

docs/05-mcpp-toml.md

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
# mcpp.toml 工程文件指南
2+
3+
`mcpp.toml` 是 mcpp 构建工具的项目配置文件,类似 Cargo 的 `Cargo.toml` 或 Node 的 `package.json`。放在项目根目录下,`mcpp build` 会自动发现并读取它。
4+
5+
## 1. 最小化示例
6+
7+
mcpp 的设计原则是 **约定优于配置** —— 大多数字段都有合理默认值,最简单的 `mcpp.toml` 只需几行:
8+
9+
### 1.1 可执行程序(最简)
10+
11+
```toml
12+
[package]
13+
name = "hello"
14+
version = "0.1.0"
15+
```
16+
17+
mcpp 自动推断:
18+
- 源文件: `src/**/*.{cppm,cpp,cc,c}`
19+
- 入口: `src/main.cpp` → 生成 `hello` 二进制
20+
- 标准: C++23
21+
- 模块: 扫描 `export module ...` 声明自动建立依赖图
22+
23+
### 1.2 库项目(最简)
24+
25+
```toml
26+
[package]
27+
name = "mylib"
28+
version = "0.1.0"
29+
30+
[targets.mylib]
31+
kind = "lib"
32+
```
33+
34+
lib-root 约定:主模块接口默认在 `src/mylib.cppm`(包名的最后一段)。
35+
36+
## 2. 完整字段参考
37+
38+
### 2.1 `[package]` — 包元数据
39+
40+
```toml
41+
[package]
42+
name = "myapp" # 包名(必填)
43+
version = "0.1.0" # 语义化版本(必填)
44+
description = "My awesome app" # 简介(可选)
45+
license = "MIT" # 许可证(可选)
46+
authors = ["Alice", "Bob"] # 作者列表(可选)
47+
repo = "https://github.com/user/myapp" # 仓库地址(可选)
48+
```
49+
50+
### 2.2 `[targets.<name>]` — 构建目标
51+
52+
```toml
53+
# 可执行程序(默认,有 src/main.cpp 时自动推断)
54+
[targets.myapp]
55+
kind = "bin"
56+
main = "src/main.cpp" # 可选,默认 src/main.cpp
57+
58+
# 静态库
59+
[targets.mylib]
60+
kind = "lib"
61+
62+
# 共享库
63+
[targets.mylib]
64+
kind = "shared"
65+
```
66+
67+
### 2.3 `[build]` — 构建配置
68+
69+
```toml
70+
[build]
71+
sources = ["src/**/*.cppm", "src/**/*.cpp"] # 源文件 glob(默认: src/**/*.{cppm,cpp,cc,c})
72+
include_dirs = ["include", "third_party/include"] # 头文件搜索路径
73+
c_standard = "c11" # C 源文件的标准(默认 c11)
74+
cflags = ["-DFOO=1"] # 额外 C 编译参数
75+
cxxflags = ["-DBAR=2"] # 额外 C++ 编译参数
76+
static_stdlib = true # 静态链接 libstdc++(默认 true)
77+
```
78+
79+
**glob 排除**(`!` 前缀,mcpp 0.0.4+):
80+
81+
```toml
82+
[build]
83+
sources = [
84+
"src/**/*.cpp",
85+
"!src/**/*_test.cpp", # 排除测试文件
86+
"!src/**/*_fuzzer.cpp", # 排除 fuzzer
87+
]
88+
```
89+
90+
### 2.4 `[lib]` — 库根模块约定
91+
92+
```toml
93+
[lib]
94+
path = "src/capi/lua.cppm" # 覆盖默认的 lib-root 位置
95+
```
96+
97+
默认约定:`src/<包名最后一段>.cppm`(如包名 `mcpplibs.cmdline``src/cmdline.cppm`)。
98+
99+
### 2.5 `[dependencies]` — 运行时依赖
100+
101+
```toml
102+
# 默认命名空间(mcpp)下的包
103+
[dependencies]
104+
gtest = "1.15.2" # 精确版本
105+
mbedtls = "3.6.1"
106+
ftxui = "6.1.9"
107+
108+
# 命名空间子表写法
109+
[dependencies.mcpplibs]
110+
cmdline = "0.0.2"
111+
tinyhttps = "0.2.2"
112+
llmapi = "0.2.5"
113+
114+
# 路径依赖(本地开发)
115+
[dependencies]
116+
mylib = { path = "../mylib" }
117+
118+
# Git 依赖
119+
[dependencies]
120+
mylib = { git = "https://github.com/user/mylib.git", tag = "v1.0.0" }
121+
```
122+
123+
**SemVer 约束**:
124+
125+
```toml
126+
[dependencies]
127+
foo = "^1.2.3" # >= 1.2.3, < 2.0.0 (caret,默认)
128+
bar = "~1.2.3" # >= 1.2.3, < 1.3.0 (tilde)
129+
baz = "=1.2.3" # 精确匹配
130+
qux = ">=1.0, <2.0" # 范围组合
131+
```
132+
133+
### 2.6 `[dev-dependencies]` — 测试依赖
134+
135+
```toml
136+
[dev-dependencies]
137+
gtest = "1.15.2"
138+
```
139+
140+
`mcpp build` 忽略这些;`mcpp test` 解析并使用。`mcpp test` 会自动发现 `tests/**/*.cpp` 并编译为测试二进制。
141+
142+
### 2.7 `[toolchain]` — 工具链配置
143+
144+
```toml
145+
[toolchain]
146+
default = "gcc@16.1.0"
147+
148+
# 跨编译目标覆盖
149+
[target.x86_64-linux-musl]
150+
toolchain = "gcc@15.1.0-musl"
151+
linkage = "static"
152+
```
153+
154+
## 3. 实战示例
155+
156+
### 3.1 简单 Hello World
157+
158+
```toml
159+
[package]
160+
name = "hello"
161+
version = "0.1.0"
162+
```
163+
164+
```cpp
165+
// src/main.cpp
166+
import std;
167+
int main() { std::println("Hello, mcpp!"); }
168+
```
169+
170+
```bash
171+
mcpp build && mcpp run
172+
```
173+
174+
### 3.2 模块化库 + 测试
175+
176+
```toml
177+
[package]
178+
name = "mymath"
179+
version = "1.0.0"
180+
181+
[targets.mymath]
182+
kind = "lib"
183+
184+
[dev-dependencies]
185+
gtest = "1.15.2"
186+
```
187+
188+
```cpp
189+
// src/mymath.cppm
190+
export module mymath;
191+
export int add(int a, int b) { return a + b; }
192+
```
193+
194+
```cpp
195+
// tests/test_add.cpp
196+
#include <gtest/gtest.h>
197+
import mymath;
198+
TEST(Math, Add) { EXPECT_EQ(add(1, 2), 3); }
199+
```
200+
201+
```bash
202+
mcpp build # 编译库
203+
mcpp test # 编译 + 跑测试
204+
```
205+
206+
### 3.3 依赖其他包的应用
207+
208+
```toml
209+
[package]
210+
name = "myapp"
211+
version = "0.1.0"
212+
213+
[dependencies]
214+
ftxui = "6.1.9"
215+
216+
[dependencies.mcpplibs]
217+
cmdline = "0.0.2"
218+
llmapi = "0.2.5"
219+
```
220+
221+
mcpp 自动:
222+
1. 从 mcpp-index 下载源码 tarball
223+
2.`[build].include_dirs` 传播头文件路径
224+
3. 传递依赖自动入图(llmapi → tinyhttps → mbedtls 全自动)
225+
226+
### 3.4 纯 C 库
227+
228+
```toml
229+
[package]
230+
name = "myc"
231+
version = "0.1.0"
232+
233+
[build]
234+
c_standard = "c99"
235+
include_dirs = ["include"]
236+
sources = ["src/**/*.c"]
237+
238+
[targets.myc]
239+
kind = "lib"
240+
```
241+
242+
### 3.5 混合 C / C++23 模块项目
243+
244+
```toml
245+
[package]
246+
name = "hybrid"
247+
version = "0.1.0"
248+
249+
[build]
250+
include_dirs = ["include"]
251+
c_standard = "c11"
252+
253+
[dependencies]
254+
lua = "5.4.7" # 纯 C 库,mcpp 自动用 C 编译器编译 .c 文件
255+
256+
[targets.hybrid]
257+
kind = "bin"
258+
```
259+
260+
### 3.6 跨编译静态发布
261+
262+
```toml
263+
[package]
264+
name = "mytool"
265+
version = "1.0.0"
266+
267+
[toolchain]
268+
default = "gcc@16.1.0"
269+
270+
[target.x86_64-linux-musl]
271+
toolchain = "gcc@15.1.0-musl"
272+
linkage = "static"
273+
```
274+
275+
```bash
276+
mcpp build --target x86_64-linux-musl
277+
# → 产出完全静态链接的二进制,可直接 scp 到任意 Linux x86_64 机器运行
278+
```
279+
280+
## 4. 约定与默认值速查
281+
282+
| 项目 | 默认值 | 说明 |
283+
|---|---|---|
284+
| 源文件 | `src/**/*.{cppm,cpp,cc,c}` | 自动递归扫描 |
285+
| 入口 | `src/main.cpp` | 有这个文件就推断为 `bin` 目标 |
286+
| 库根 | `src/<pkg-tail>.cppm` | 可用 `[lib].path` 覆盖 |
287+
| 标准 | `c++23` | C++23 模块是一等公民 |
288+
| C 标准 | `c11` | `.c` 文件自动走 C 编译器 |
289+
| 静态 stdlib | `true` | 便携二进制 |
290+
| 头文件 | `include/`(如果存在) | 自动加到 `-I` |
291+
| 测试 | `tests/**/*.cpp` | `mcpp test` 自动发现 |
292+
| 依赖命名空间 | `mcpp`(默认) | 平铺写法走默认 ns |

docs/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 用户文档目录
2+
3+
- [00 - 快速开始](00-getting-started.md)
4+
- [01 - 示例项目](01-examples.md)
5+
- [02 - 发布打包](02-pack-and-release.md)
6+
- [03 - 工具链管理](03-toolchains.md)
7+
- [04 - 从源码构建 & 参与贡献](04-build-from-source.md)
8+
- [05 - mcpp.toml 工程文件指南](05-mcpp-toml.md)

0 commit comments

Comments
 (0)