Skip to content

Commit 49c160e

Browse files
committed
docs: workspace guide + bump to 0.0.11
- Add docs/06-workspace.md: workspace concepts, configuration, commands, version inheritance, toolchain inheritance, directory layout, and relationship with C++ modules - README.md: add workspace doc link to documentation section - Bump version to 0.0.11
1 parent 3c326cd commit 49c160e

4 files changed

Lines changed: 220 additions & 25 deletions

File tree

README.md

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,6 @@ mcpp run
6565

6666
注: 初次构建需要初始化环境并获取工具链, 可能需要一些时间
6767

68-
## 工作空间 (Workspace)
69-
70-
mcpp 支持多包工作空间,在一个仓库中管理多个相关的库和应用:
71-
72-
```toml
73-
# 根 mcpp.toml
74-
[workspace]
75-
members = ["libs/core", "libs/http", "apps/server"]
76-
77-
[workspace.dependencies.compat]
78-
mbedtls = "3.6.1"
79-
```
80-
81-
```bash
82-
mcpp build # 构建所有 member
83-
mcpp build -p server # 只构建 server 及其依赖
84-
```
85-
86-
- member 之间用 `path = "..."` 声明依赖
87-
- `.workspace = true` 继承 workspace 统一管理的依赖版本
88-
- 工具链配置可继承和覆盖
89-
- 详见 [examples/04-workspace](examples/04-workspace/)
90-
9168
## 文档
9269

9370
- [快速开始](docs/00-getting-started.md)
@@ -96,6 +73,7 @@ mcpp build -p server # 只构建 server 及其依赖
9673
- [工具链管理](docs/03-toolchains.md)
9774
- [从源码构建 & 参与贡献](docs/04-build-from-source.md)
9875
- [mcpp.toml 工程文件指南](docs/05-mcpp-toml.md)
76+
- [工作空间 (Workspace)](docs/06-workspace.md)
9977

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

docs/06-workspace.md

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
# 工作空间 (Workspace)
2+
3+
工作空间允许在同一个仓库中组织和管理多个相关的 mcpp 包(库或应用程序)。各成员包共享统一的依赖版本配置和工具链设置,同时保持独立的 `mcpp.toml` 工程文件。
4+
5+
## 1. 概述
6+
7+
工作空间解决以下问题:
8+
9+
- **依赖版本统一管理** — 多个子包使用相同版本的第三方依赖,避免重复声明和版本不一致
10+
- **工具链配置共享** — 在工作空间根目录统一声明工具链,各成员继承或覆盖
11+
- **多包协同开发** — 库与应用在同一仓库中开发,通过 `path` 依赖相互引用
12+
13+
工作空间不改变依赖声明方式。成员之间通过已有的 `path = "..."` 机制声明依赖关系,与非工作空间项目的用法完全一致。
14+
15+
## 2. 工程文件结构
16+
17+
### 2.1 工作空间根
18+
19+
在仓库根目录的 `mcpp.toml` 中声明 `[workspace]`
20+
21+
```toml
22+
[workspace]
23+
members = [
24+
"libs/core",
25+
"libs/http",
26+
"apps/server",
27+
]
28+
```
29+
30+
`members` 列出各成员包的相对路径,每个路径下须包含独立的 `mcpp.toml`
31+
32+
可选 `exclude` 字段排除特定路径:
33+
34+
```toml
35+
[workspace]
36+
members = ["libs/*"]
37+
exclude = ["libs/experimental"]
38+
```
39+
40+
### 2.2 虚拟工作空间与根包工作空间
41+
42+
**虚拟工作空间**:根 `mcpp.toml` 仅包含 `[workspace]`,不包含 `[package]`。根目录不产出构建产物,仅作为管理节点。
43+
44+
```toml
45+
# 虚拟工作空间 — 只有 [workspace]
46+
[workspace]
47+
members = ["libs/core", "apps/server"]
48+
```
49+
50+
**根包工作空间**:根 `mcpp.toml` 同时包含 `[package]``[workspace]`。根目录本身也是一个可构建的包。
51+
52+
```toml
53+
[workspace]
54+
members = ["libs/core"]
55+
56+
[package]
57+
name = "myapp"
58+
version = "0.1.0"
59+
60+
[dependencies]
61+
core = { path = "libs/core" }
62+
```
63+
64+
### 2.3 成员工程文件
65+
66+
各成员维护独立的 `mcpp.toml`,结构与普通项目一致:
67+
68+
```toml
69+
# libs/core/mcpp.toml
70+
[package]
71+
namespace = "myproject"
72+
name = "core"
73+
version = "0.1.0"
74+
75+
[targets.core]
76+
kind = "lib"
77+
```
78+
79+
成员之间通过 `path` 依赖引用:
80+
81+
```toml
82+
# libs/http/mcpp.toml
83+
[package]
84+
namespace = "myproject"
85+
name = "http"
86+
version = "0.1.0"
87+
88+
[dependencies]
89+
core = { path = "../core" }
90+
91+
[dependencies.compat]
92+
mbedtls.workspace = true
93+
```
94+
95+
## 3. 依赖版本继承
96+
97+
`[workspace.dependencies]` 中集中声明依赖版本,成员通过 `.workspace = true` 继承:
98+
99+
```toml
100+
# 根 mcpp.toml
101+
[workspace.dependencies]
102+
cmdline = "0.0.2"
103+
104+
[workspace.dependencies.compat]
105+
mbedtls = "3.6.1"
106+
gtest = "1.15.2"
107+
```
108+
109+
```toml
110+
# 成员 mcpp.toml
111+
[dependencies.compat]
112+
mbedtls.workspace = true # 继承版本 → "3.6.1"
113+
114+
[dev-dependencies.compat]
115+
gtest.workspace = true # 继承版本 → "1.15.2"
116+
```
117+
118+
成员可以覆盖继承的版本:
119+
120+
```toml
121+
[dependencies.compat]
122+
mbedtls = "4.0.0" # 覆盖,不使用 workspace 版本
123+
```
124+
125+
## 4. 工具链与构建配置继承
126+
127+
工作空间根的 `[toolchain]``[target.<triple>]` 配置自动继承到所有成员。成员可在自身的工程文件中覆盖。
128+
129+
配置优先级(从高到低):
130+
131+
1. 命令行参数(`--target``--static`
132+
2. 成员 `mcpp.toml` 中的声明
133+
3. 工作空间根 `mcpp.toml` 中的声明
134+
4. 全局配置(`~/.mcpp/config.toml`
135+
5. 内置默认值
136+
137+
```toml
138+
# 工作空间根
139+
[toolchain]
140+
default = "gcc@16.1.0"
141+
142+
[target.x86_64-linux-musl]
143+
toolchain = "gcc@15.1.0-musl"
144+
linkage = "static"
145+
```
146+
147+
```toml
148+
# 某成员覆盖工具链
149+
[toolchain]
150+
default = "clang@19.0"
151+
```
152+
153+
## 5. 构建命令
154+
155+
### 5.1 从工作空间根目录构建
156+
157+
```bash
158+
mcpp build # 构建默认目标(自动选择含二进制目标的成员)
159+
mcpp build -p server # 构建指定成员及其依赖
160+
mcpp build -p core # 构建指定库成员
161+
```
162+
163+
### 5.2 从成员子目录构建
164+
165+
```bash
166+
cd libs/http
167+
mcpp build # 自动检测工作空间,构建当前成员
168+
```
169+
170+
mcpp 从当前目录向上搜索,若发现包含 `[workspace]``mcpp.toml` 且当前目录在 `members` 列表中,则自动进入工作空间模式,继承工作空间配置。
171+
172+
### 5.3 `-p, --package` 选项
173+
174+
`-p` 可用于 `build``test``run` 等命令,指定构建的目标成员。参数值为成员路径的最后一段目录名或完整相对路径:
175+
176+
```bash
177+
mcpp build -p server # 匹配 apps/server
178+
mcpp test -p core # 匹配 libs/core
179+
mcpp run -p server -- --port 8080
180+
```
181+
182+
## 6. 目录布局
183+
184+
工作空间推荐的目录布局:
185+
186+
```
187+
myproject/
188+
├── mcpp.toml # [workspace] 声明
189+
├── libs/
190+
│ ├── core/
191+
│ │ ├── mcpp.toml # [package] namespace="myproject" name="core"
192+
│ │ └── src/
193+
│ │ └── core.cppm # export module myproject.core;
194+
│ └── http/
195+
│ ├── mcpp.toml
196+
│ └── src/
197+
│ └── http.cppm # export module myproject.http;
198+
└── apps/
199+
└── server/
200+
├── mcpp.toml
201+
└── src/
202+
└── main.cpp # import myproject.http;
203+
```
204+
205+
各成员的构建产物位于各自的 `target/` 子目录下。
206+
207+
## 7. 与 C++ 模块的关系
208+
209+
工作空间与 C++23 模块机制协同工作:
210+
211+
- **接口可见性由语言控制**`export module``import` 语句决定模块的公开接口,工作空间不做额外的可见性限制
212+
- **模块名由库作者决定** — 工作空间不强制模块名与包名或命名空间一致
213+
- **partition 用于内部组织**`import :internal;`(不带 `export`)的 partition 对消费者不可见,无需构建工具介入
214+
215+
## 8. 完整示例
216+
217+
参见 [`examples/04-workspace/`](../examples/04-workspace/),包含一个三成员工作空间的完整可运行示例。

mcpp.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mcpp"
3-
version = "0.0.10"
3+
version = "0.0.11"
44
description = "Modern C++ build & package management tool"
55
license = "Apache-2.0"
66
authors = ["mcpp-community"]

src/toolchain/fingerprint.cppm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import mcpp.toolchain.detect;
1818

1919
export namespace mcpp::toolchain {
2020

21-
inline constexpr std::string_view MCPP_VERSION = "0.0.10";
21+
inline constexpr std::string_view MCPP_VERSION = "0.0.11";
2222

2323
struct FingerprintInputs {
2424
Toolchain toolchain;

0 commit comments

Comments
 (0)