Skip to content

Commit a5ed75f

Browse files
committed
docs: [test] discover, MCPP_RUNTIME_FILES, format-named runners, --toolchain on run/test/pack, and the runner emission contract (#634)
docs/04 section 2.17 and the defaults table; docs/08 where tests are, the --toolchain axis and the files a test program carries to a device; docs/09 a toolchain for one invocation; docs/30 one argv per runner name within a build program; docs/41 what a runner receives and how a distributable is run. The Chinese mirrors carry the same sections.
1 parent e8a957f commit a5ed75f

10 files changed

Lines changed: 227 additions & 2 deletions

‎docs/04-mcpp-toml.md‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,24 @@ do`.
14341434

14351435
Moved to [09 — Commands by Scenario](09-commands-by-scenario.md).
14361436

1437+
### 2.17 `[test]` — Where Test Programs Are
1438+
1439+
```toml
1440+
[test]
1441+
discover = ["tests/**/*.cpp"] # the default
1442+
```
1443+
1444+
| Key | Type | Meaning |
1445+
|---|---|---|
1446+
| `discover` | array of globs | every file a glob matches is one test program; a glob beginning with `!` removes the files it matches; `[]` discovers none |
1447+
1448+
The globs use the vocabulary of `[build] sources`. A test's name is its path
1449+
relative to the fixed directory of the first glob that matched it, without the
1450+
extension. Two files with one name are refused, naming both. A value that is not
1451+
an array of non-empty strings is an error; any other key in `[test]` is a
1452+
warning, and an error under `--strict`. [08 — Testing](08-testing.md) describes
1453+
the test model.
1454+
14371455

14381456
## 3. Worked Examples
14391457

@@ -1495,7 +1513,7 @@ kind = "bin"
14951513
| C standard | `c11` | `.c` files go through the C compiler automatically |
14961514
| Static stdlib | `true` | Portable binary |
14971515
| Headers | `include/` (if present) | Added to `-I` automatically |
1498-
| Tests | `tests/**/*.cpp` | Discovered automatically by `mcpp test` |
1516+
| Tests | `tests/**/*.cpp` | Discovered automatically by `mcpp test`; `[test] discover` replaces the set |
14991517
| Dependency namespace | `mcpplibs` (default) | A bare selector means only this exact namespace |
15001518

15011519
### 4.1 Legacy `[language]` Compatibility Layer

‎docs/08-testing.md‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,27 @@ also why a test written for another framework needs no adapter.
3636
`mcpp new` scaffolds `tests/test_smoke.cpp` so a project starts with the
3737
directory in place.
3838

39+
### Where tests are
40+
41+
`tests/**/*.cpp` is the default of one key:
42+
43+
```toml
44+
[test]
45+
discover = ["checks/**/*.cpp", "!checks/fixtures/**"]
46+
```
47+
48+
`discover` takes globs in the vocabulary of `[build] sources`: every file a
49+
glob matches is one test program, and a glob beginning with `!` removes the
50+
files it matches from the set, whichever glob found them. A test's name is its
51+
path relative to the fixed directory of the first glob that matched it, without
52+
the extension, so the default names `tests/unit/test_span.cpp` `unit/test_span`.
53+
`discover = []` discovers no test. Two files whose names coincide are refused,
54+
naming both.
55+
56+
A suite compiled from several sources is a package of its own: a workspace
57+
member whose `[build] sources` carries the suite and whose one test program
58+
drives it, selected with `mcpp test -p <member>`.
59+
3960
## Running them
4061

4162
```bash
@@ -55,6 +76,7 @@ configuration it is meant to check rather than against the default one:
5576
| `--target <triple>` | a target other than the host |
5677
| `--accel <spec>` / `--no-accel` | the device backends the build targets |
5778
| `--cap <list>` | pin a capability provider |
79+
| `--toolchain <spec>` | the toolchain for this invocation, e.g. `[email protected]` |
5880

5981
`--timeout <secs>` kills a test still running (default 300; `0` disables it) and
6082
`--build-timeout <secs>` bounds the compile. A test that hangs is reported as a
@@ -92,6 +114,13 @@ or a QEMU exit code is what a bare-metal runner is chosen to produce.
92114
`--no-runner` exists for a host that can execute the binaries natively and
93115
should not pay for an emulator.
94116

117+
A test program carries the files it reads beside it: the runner receives
118+
`MCPP_RUNTIME_FILES`, the list of its deployed files and the shared libraries it
119+
loads, and a runner that moves the program to a device copies them with it. A
120+
test locates such a file relative to its own directory. On the Android rows a
121+
test program links the C++ runtime statically unless `cxx_runtime` says
122+
otherwise, so it needs no `libc++_shared.so` on the device.
123+
95124
The runner itself, named runners, and what a board declares are
96125
[41 — Reaching a Device](41-devices.md).
97126

@@ -119,6 +148,8 @@ here is only that the flag exists and that the human format is the default.
119148
- A test is one `.cpp` producing one program. mcpp does not discover cases
120149
inside a file, so a framework's per-case selection happens inside the program,
121150
through arguments after `--`.
151+
- `mcpp test --list` over a manifest that does not load lists
152+
`tests/**/*.cpp`, not the `[test] discover` set it cannot read.
122153
- `--build-timeout` is POSIX-only.
123154
- `--workspace-timeout` bounds a `--workspace` fan-out and reports what did run;
124155
it does not attribute the timeout to a member.

‎docs/09-commands-by-scenario.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,22 @@ This scan runs only after a lookup has already failed, and its result reaches
107107
error text and search output only. A bare name never resolves across namespaces
108108
on the strength of it.
109109

110+
## Another toolchain for one invocation
111+
112+
`mcpp build`, `mcpp run`, `mcpp test` and `mcpp pack` take `--toolchain <spec>`,
113+
which selects the compiler for that invocation and writes nothing:
114+
115+
```bash
116+
mcpp test --toolchain [email protected]
117+
mcpp run --toolchain [email protected]
118+
mcpp pack --toolchain [email protected] --format dir
119+
```
120+
121+
For that invocation the option takes the place of `[toolchain] default` in
122+
`mcpp.toml`, at the rank [20 — Toolchain Management](20-toolchains.md) gives
123+
`MCPP_TOOLCHAIN`. Each toolchain builds into its own output directory, and a
124+
recorded build is replayed only for the toolchain request that recorded it.
125+
110126
## Explaining a resolution
111127

112128
`mcpp why` reports what a build would resolve, and builds nothing:

‎docs/30-build-mcpp.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,14 @@ both claiming to know how to run the artifact is a configuration error, and
243243
mcpp reports it naming both rather than merging them into an argv that is
244244
neither one's.
245245

246+
**Within one build program, the tokens are one argv per name.** Every
247+
`mcpp::runner(tok)` call appends to the default runner, and every
248+
`mcpp::runner(name, tok)` call appends to the runner `name`, in emission order,
249+
whichever host module compiled into the program makes the call. A program that
250+
chooses between two runners chooses before it emits either. A runner named
251+
after a pack format reaches that format's distributable under `mcpp run
252+
--format <name>` ([41 — Reaching a Device](41-devices.md)).
253+
246254
### Asking instead of declaring: `toolchain_dir` / `sysroot_dir` (2026.8.19.4+)
247255

248256
```cpp

‎docs/41-devices.md‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,54 @@ a dependency supplied, and the override is reported rather than applied in
9494
silence. Exactly one dependency may supply a given name; a second is an error
9595
naming both packages.
9696

97+
## What a runner receives
98+
99+
A runner receives an argv — its own tokens, with the artifact's path appended
100+
or substituted for `{}` — and one environment variable, `MCPP_RUNTIME_FILES`.
101+
The variable is set for every runner that `mcpp run` and `mcpp test` start,
102+
the default and the named ones alike. `mcpp run --no-runner` starts no runner
103+
and sets nothing.
104+
105+
`MCPP_RUNTIME_FILES` names a file that lists what the artifact reads or loads
106+
from its own directory: the `[runtime] deploy` and `deploy_files` entries, and
107+
the shared libraries the build links. One line per file:
108+
109+
```
110+
data/data.txt<TAB>/abs/project/target/<triple>/<fp>/bin/data/data.txt
111+
libfw.so<TAB>/abs/project/target/<triple>/<fp>/bin/libfw.so
112+
```
113+
114+
The first field is the destination relative to the artifact's directory, with
115+
`/` separators; it begins with `../` for a test program discovered in a
116+
subdirectory. The second is the absolute path of the file in the output tree. A
117+
TAB separates them. The file exists for every runner invocation and is empty
118+
when there is nothing to carry; a distributable run with `--format` receives
119+
an empty file, because the distributable holds its own files.
120+
121+
A runner that executes the artifact where it lies ignores the variable. A
122+
runner that moves the artifact — to a device, a container, a remote host —
123+
copies each listed file to the destination beside the moved artifact.
124+
125+
## Running a distributable
126+
127+
`mcpp run --format <f>` packs the distributable `<f>` and runs it. Without
128+
`--runner`, it is reached by the runner named `<f>` when the graph or the
129+
manifest supplies one, and by the default runner otherwise:
130+
131+
```cpp
132+
mcpp::runner("app", "<tool>"); // a package names a runner after its format
133+
```
134+
135+
```bash
136+
mcpp run --format app # through the runner named `app`
137+
mcpp run --format app --runner other # a typed --runner still wins
138+
```
139+
140+
A distributable that is a directory, such as an application bundle, is not
141+
executed directly. When no runner reaches it, `mcpp run --format <f>` refuses
142+
before starting anything, with exit status `126`, and names the runner `<f>`
143+
and the manifest table that would declare it.
144+
97145
## Termination is declared, not inferred
98146

99147
| | Meaning |

‎docs/zh/04-mcpp-toml.md‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,22 @@ o.arg("./mkblob.sh").arg("blob.bin").arg("${mcpp.out_dir}/blob.o")
12361236

12371237
已移入 [09 —— 按场景选命令](09-commands-by-scenario.md)。
12381238

1239+
### 2.17 `[test]` —— 测试程序的位置
1240+
1241+
```toml
1242+
[test]
1243+
discover = ["tests/**/*.cpp"] # 默认值
1244+
```
1245+
1246+
| 键 | 类型 | 含义 |
1247+
|---|---|---|
1248+
| `discover` | glob 数组 | glob 匹配到的每个文件都是一个测试程序;以 `!` 开头的 glob 去掉它匹配到的文件;`[]` 不发现任何测试 |
1249+
1250+
glob 使用 `[build] sources` 的词汇。测试的名字是它相对于第一个匹配它的 glob 的固定
1251+
目录的路径,去掉扩展名。同名的两个文件会被拒绝,并点名两者。值不是非空字符串数组时
1252+
报错;`[test]` 中的其他键给出警告,在 `--strict` 下为错误。测试模型见
1253+
[08 —— 测试](08-testing.md)。
1254+
12391255

12401256
## 3. 实战示例
12411257

@@ -1296,7 +1312,7 @@ kind = "bin"
12961312
| C 标准 | `c11` | `.c` 文件自动走 C 编译器 |
12971313
| 静态 stdlib | `true` | 便携二进制 |
12981314
| 头文件 | `include/`(如果存在) | 自动加到 `-I` |
1299-
| 测试 | `tests/**/*.cpp` | `mcpp test` 自动发现 |
1315+
| 测试 | `tests/**/*.cpp` | `mcpp test` 自动发现;`[test] discover` 替换这个集合 |
13001316
| 依赖命名空间 | `mcpplibs`(默认) | 裸 selector 只表示该精确 ns |
13011317

13021318
### 4.1 旧 `[language]` 兼容层

‎docs/zh/08-testing.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ myproject/
3333

3434
`mcpp new` 会生成 `tests/test_smoke.cpp`,让工程一开始就有这个目录。
3535

36+
### 测试的位置
37+
38+
`tests/**/*.cpp` 是一个键的默认值:
39+
40+
```toml
41+
[test]
42+
discover = ["checks/**/*.cpp", "!checks/fixtures/**"]
43+
```
44+
45+
`discover` 接受与 `[build] sources` 同一套词汇的 glob:glob 匹配到的每个文件都是一个
46+
测试程序,以 `!` 开头的 glob 把它匹配到的文件从集合中去掉,无论是哪个 glob 找到的。
47+
测试的名字是它相对于第一个匹配它的 glob 的固定目录的路径,去掉扩展名,因此默认值
48+
把 `tests/unit/test_span.cpp` 命名为 `unit/test_span`。`discover = []` 不发现任何测试。
49+
名字相同的两个文件会被拒绝,并点名两者。
50+
51+
由多个源文件编译成的测试套件是一个独立的包:一个工作区成员,它的 `[build] sources`
52+
承载套件,它唯一的测试程序驱动套件,用 `mcpp test -p <member>` 选中。
53+
3654
## 运行它们
3755

3856
```bash
@@ -52,6 +70,7 @@ mcpp test -- --verbose # `--` 之后的一切传给每个测试程序
5270
| `--target <triple>` | 宿主以外的目标 |
5371
| `--accel <spec>` / `--no-accel` | 本次构建面向的设备后端 |
5472
| `--cap <list>` | 钉住某个能力的 provider |
73+
| `--toolchain <spec>` | 本次调用使用的工具链,例如 `[email protected]` |
5574

5675
`--timeout <secs>` 杀掉仍在运行的测试(默认 300;`0` 关闭),`--build-timeout <secs>`
5776
限制编译。一个挂住的测试被报为**以它自己的名字失败**,而不是一个停下来的任务。
@@ -84,6 +103,11 @@ mcpp test --no-runner # 忽略 runner,直接执行
84103

85104
`--no-runner` 是给「本机就能原生执行这些二进制、不该为模拟器付代价」的宿主准备的。
86105

106+
测试程序把它读取的文件带在身边:runner 收到 `MCPP_RUNTIME_FILES`,即它部署的文件与
107+
它加载的共享库的清单,把程序移到设备上的 runner 连同这些文件一起复制。测试按相对于
108+
自身所在目录的路径定位这类文件。在 Android 行上,除非 `cxx_runtime` 另有声明,测试
109+
程序静态链接 C++ 运行时,因此设备上不需要 `libc++_shared.so`。
110+
87111
runner 本身、具名 runner,以及一块板子声明什么,见
88112
[41 —— 抵达一台设备](41-devices.md)。
89113

@@ -110,6 +134,8 @@ mcpp test --message-format json
110134

111135
- 一个测试是一个 `.cpp` 产出一个程序。mcpp 不发现文件内部的用例,因此框架的
112136
逐用例选择发生在程序内部,经由 `--` 之后的参数。
137+
- 清单无法加载时,`mcpp test --list` 列出的是 `tests/**/*.cpp`,而不是它读不到的
138+
`[test] discover` 集合。
113139
- `--build-timeout` 只在 POSIX 上有效。
114140
- `--workspace-timeout` 限制 `--workspace` 的扇出并报告跑到了哪些;它不把超时
115141
归因到某个成员。

‎docs/zh/09-commands-by-scenario.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ $ mcpp search imgui
9090
这次扫描只在查找已经失败之后进行,结果只进入错误文本与 search 输出。裸名不会因此跨
9191
命名空间解析。
9292

93+
## 为一次调用换一个工具链
94+
95+
`mcpp build`、`mcpp run`、`mcpp test` 与 `mcpp pack` 接受 `--toolchain <spec>`,
96+
它为这一次调用选择编译器,不写入任何东西:
97+
98+
```bash
99+
mcpp test --toolchain [email protected]
100+
mcpp run --toolchain [email protected]
101+
mcpp pack --toolchain [email protected] --format dir
102+
```
103+
104+
对这一次调用,这个选项取代 `mcpp.toml` 中的 `[toolchain] default`,其优先级即
105+
[20 —— 工具链管理](20-toolchains.md) 给 `MCPP_TOOLCHAIN` 的那一级。每个工具链构建到
106+
它自己的输出目录,一次记录下来的构建只为记录它的那个工具链请求重放。
107+
93108
## 解释一次解析
94109

95110
`mcpp why` 报告一次构建会解析出什么,并且不构建任何东西:

‎docs/zh/30-build-mcpp.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ for (auto a : {"-machine","virt","-nographic","-no-reboot","-kernel"})
213213
**只能有一个依赖提供 runner。** 两个板级支持包都声称知道怎么跑这个产物是配置
214214
错误;mcpp 会**同时点名两个**并报错,而不是把它们并成一个谁也不是的 argv。
215215

216+
**在一个构建程序之内,每个名字的 token 组成一个 argv。** 每次 `mcpp::runner(tok)`
217+
调用追加到默认 runner,每次 `mcpp::runner(name, tok)` 调用追加到名为 `name` 的
218+
runner,按发出顺序,无论调用来自编译进这个程序的哪个 host module。要在两个 runner
219+
之间选择的程序,在发出任何一个之前完成选择。以打包格式命名的 runner 在
220+
`mcpp run --format <name>` 下抵达该格式的可分发物([41 —— 抵达一台设备](41-devices.md))。
221+
216222
### 问,而不是声明:`toolchain_dir` / `sysroot_dir`(2026.8.19.4+)
217223

218224
```cpp

‎docs/zh/41-devices.md‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,47 @@ monitor = ["probe-rs", "attach", "--chip", "STM32L475VG"]
7777
优先级是通常那个:工程作者写的胜过依赖提供的,且覆盖会被报告。一个名字只允许一个
7878
依赖提供,第二个是点名两个包的错误。
7979

80+
## runner 收到的内容
81+
82+
runner 收到一段 argv —— 它自己的 token,产物路径被追加或替换 `{}` —— 以及一个环境
83+
变量 `MCPP_RUNTIME_FILES`。`mcpp run` 与 `mcpp test` 启动的每一个 runner 都会收到
84+
这个变量,默认 runner 与具名 runner 相同。`mcpp run --no-runner` 不启动 runner,
85+
也不设置它。
86+
87+
`MCPP_RUNTIME_FILES` 指向一个文件,列出产物从自己所在目录读取或加载的东西:
88+
`[runtime] deploy` 与 `deploy_files` 的条目,以及构建链接的共享库。每个文件一行:
89+
90+
```
91+
data/data.txt<TAB>/abs/project/target/<triple>/<fp>/bin/data/data.txt
92+
libfw.so<TAB>/abs/project/target/<triple>/<fp>/bin/libfw.so
93+
```
94+
95+
第一个字段是相对于产物所在目录的目的路径,分隔符为 `/`;在子目录中发现的测试程序,
96+
其目的路径以 `../` 开头。第二个字段是该文件在输出树中的绝对路径。两者以 TAB 分隔。
97+
每次启动 runner 时这个文件都存在,没有需要携带的东西时为空;以 `--format` 运行的
98+
可分发物收到空文件,因为可分发物自带其文件。
99+
100+
就地执行产物的 runner 忽略这个变量。把产物移走的 runner —— 移到设备、容器或远程
101+
主机 —— 把列出的每个文件复制到被移走的产物旁边对应的目的路径。
102+
103+
## 运行一个可分发物
104+
105+
`mcpp run --format <f>` 打包可分发物 `<f>` 并运行它。不带 `--runner` 时,若图或
106+
清单提供了名为 `<f>` 的 runner,就由它抵达,否则由默认 runner 抵达:
107+
108+
```cpp
109+
mcpp::runner("app", "<tool>"); // 包以格式名命名 runner
110+
```
111+
112+
```bash
113+
mcpp run --format app # 经名为 `app` 的 runner
114+
mcpp run --format app --runner other # 显式的 --runner 仍然优先
115+
```
116+
117+
目录形式的可分发物(例如应用包)不被直接执行。没有 runner 抵达它时,
118+
`mcpp run --format <f>` 在启动任何东西之前拒绝,退出状态为 `126`,并点名能抵达它的
119+
runner `<f>` 以及声明它的清单表。
120+
80121
## 终止由声明决定,不由推断
81122

82123
| | 含义 |

0 commit comments

Comments
 (0)