Skip to content

Commit 4af003e

Browse files
IronsDuclaude
andauthored
feat: 添加 CMakePresets.json 支持现代化构建配置 (#31)
* feat: 添加 CMakePresets.json 支持现代化构建配置 - 添加 6 个配置预设:debug, release, relwithdebinfo, coverage, clang-debug, clang-release - 添加对应的构建预设和测试预设 - 使用隐藏预设 vcpkg-base 统一 vcpkg 工具链配置 - 将 CMakeUserPresets.json 添加到 .gitignore Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: CI 使用 CMake 预设替代传统命令 - build-gcc: 使用 release 预设 - coverage: 使用 coverage 预设 - build-clang: 使用 clang-release 预设 - 移除不再需要的 BUILD_TYPE 环境变量 - 更新构建产物路径 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * docs: 更新 ROADMAP.md 标记 CMakePresets.json 已完成 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 06e878e commit 4af003e

4 files changed

Lines changed: 165 additions & 50 deletions

File tree

.github/workflows/ci.yml

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ on:
66
pull_request:
77
branches: [ main, develop ]
88

9-
env:
10-
BUILD_TYPE: Release
11-
129
jobs:
1310
# Ubuntu with GCC
1411
build-gcc:
@@ -64,26 +61,16 @@ jobs:
6461
${{ github.workspace }}/vcpkg/vcpkg version
6562
6663
- name: Configure CMake
67-
run: |
68-
mkdir build
69-
cd build
70-
cmake .. \
71-
-DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} \
72-
-DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake \
73-
-DBUILD_SHARED_LIBS=ON
64+
run: cmake --preset=release
7465

7566
- name: Build
76-
run: |
77-
cd build
78-
make -j$(nproc)
67+
run: cmake --build --preset=release
7968

8069
- name: Run tests
81-
working-directory: build
82-
run: |
83-
ctest --output-on-failure --verbose
70+
run: ctest --preset=release --verbose
8471

8572
- name: Check library linking
86-
working-directory: build
73+
working-directory: build/release
8774
run: |
8875
ldd profiler_example
8976
ldd libprofiler_lib.so
@@ -132,28 +119,17 @@ jobs:
132119
runVcpkgFormatString: ['install', '--recurse', '--clean-after-build', '--x-install-root', '$[env.VCPKG_INSTALLED_DIR]', '--triplet', 'x64-linux']
133120

134121
- name: Configure CMake with Coverage
135-
run: |
136-
mkdir build
137-
cd build
138-
cmake .. \
139-
-DCMAKE_BUILD_TYPE=Debug \
140-
-DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake \
141-
-DBUILD_SHARED_LIBS=ON \
142-
-DENABLE_COVERAGE=ON
122+
run: cmake --preset=coverage
143123

144124
- name: Build
145-
run: |
146-
cd build
147-
make -j$(nproc)
125+
run: cmake --build --preset=coverage
148126

149127
- name: Run tests
150-
working-directory: build
151-
run: |
152-
ctest --output-on-failure
128+
run: ctest --preset=coverage
153129

154130
- name: Generate coverage report
131+
working-directory: build/coverage
155132
run: |
156-
cd build
157133
gcovr --xml-pretty --exclude-unreachable-branches --print-summary \
158134
--output coverage.xml \
159135
--root ${{ github.workspace }} \
@@ -164,7 +140,7 @@ jobs:
164140
uses: codecov/codecov-action@v5
165141
with:
166142
token: ${{ secrets.CODECOV_TOKEN }}
167-
files: build/coverage.xml
143+
files: build/coverage/coverage.xml
168144
flags: unittests
169145
name: cpp-remote-profiler-coverage
170146
fail_ci_if_error: false
@@ -225,26 +201,16 @@ jobs:
225201
${{ github.workspace }}/vcpkg/vcpkg version
226202
227203
- name: Configure CMake with Clang
228-
run: |
229-
mkdir build
230-
cd build
231-
CC=clang CXX=clang++ cmake .. \
232-
-DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} \
233-
-DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake \
234-
-DBUILD_SHARED_LIBS=ON
204+
run: cmake --preset=clang-release
235205

236206
- name: Build
237-
run: |
238-
cd build
239-
make -j$(nproc)
207+
run: cmake --build --preset=clang-release
240208

241209
- name: Run tests
242-
working-directory: build
243-
run: |
244-
ctest --output-on-failure --verbose
210+
run: ctest --preset=clang-release --verbose
245211

246212
- name: Check library linking
247-
working-directory: build
213+
working-directory: build/clang-release
248214
run: |
249215
ldd profiler_example
250216
ldd libprofiler_lib.so

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ CMakeCache.txt
4141
CMakeFiles/
4242
cmake_install.cmake
4343
Makefile
44+
CMakeUserPresets.json
4445

4546
# Unused directories
4647
third_party/

CMakePresets.json

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
{
2+
"version": 3,
3+
"cmakeMinimumRequired": {
4+
"major": 3,
5+
"minor": 20,
6+
"patch": 0
7+
},
8+
"configurePresets": [
9+
{
10+
"name": "vcpkg-base",
11+
"hidden": true,
12+
"cacheVariables": {
13+
"CMAKE_TOOLCHAIN_FILE": {
14+
"type": "FILEPATH",
15+
"value": "${sourceDir}/vcpkg/scripts/buildsystems/vcpkg.cmake"
16+
},
17+
"BUILD_SHARED_LIBS": "ON",
18+
"REMOTE_PROFILER_BUILD_EXAMPLES": "ON",
19+
"REMOTE_PROFILER_BUILD_TESTS": "ON"
20+
}
21+
},
22+
{
23+
"name": "debug",
24+
"displayName": "Debug",
25+
"description": "Debug build for development",
26+
"inherits": "vcpkg-base",
27+
"binaryDir": "${sourceDir}/build/${presetName}",
28+
"cacheVariables": {
29+
"CMAKE_BUILD_TYPE": "Debug"
30+
}
31+
},
32+
{
33+
"name": "release",
34+
"displayName": "Release",
35+
"description": "Release build for production",
36+
"inherits": "vcpkg-base",
37+
"binaryDir": "${sourceDir}/build/${presetName}",
38+
"cacheVariables": {
39+
"CMAKE_BUILD_TYPE": "Release"
40+
}
41+
},
42+
{
43+
"name": "relwithdebinfo",
44+
"displayName": "RelWithDebInfo",
45+
"description": "Release with debug info (project default)",
46+
"inherits": "vcpkg-base",
47+
"binaryDir": "${sourceDir}/build/${presetName}",
48+
"cacheVariables": {
49+
"CMAKE_BUILD_TYPE": "RelWithDebInfo"
50+
}
51+
},
52+
{
53+
"name": "coverage",
54+
"displayName": "Coverage",
55+
"description": "Debug build with code coverage enabled",
56+
"inherits": "vcpkg-base",
57+
"binaryDir": "${sourceDir}/build/${presetName}",
58+
"cacheVariables": {
59+
"CMAKE_BUILD_TYPE": "Debug",
60+
"ENABLE_COVERAGE": "ON"
61+
}
62+
},
63+
{
64+
"name": "clang-debug",
65+
"displayName": "Clang Debug",
66+
"description": "Debug build with Clang compiler",
67+
"inherits": "debug",
68+
"environment": {
69+
"CC": "clang",
70+
"CXX": "clang++"
71+
}
72+
},
73+
{
74+
"name": "clang-release",
75+
"displayName": "Clang Release",
76+
"description": "Release build with Clang compiler",
77+
"inherits": "release",
78+
"environment": {
79+
"CC": "clang",
80+
"CXX": "clang++"
81+
}
82+
}
83+
],
84+
"buildPresets": [
85+
{
86+
"name": "debug",
87+
"configurePreset": "debug"
88+
},
89+
{
90+
"name": "release",
91+
"configurePreset": "release"
92+
},
93+
{
94+
"name": "relwithdebinfo",
95+
"configurePreset": "relwithdebinfo"
96+
},
97+
{
98+
"name": "coverage",
99+
"configurePreset": "coverage"
100+
},
101+
{
102+
"name": "clang-debug",
103+
"configurePreset": "clang-debug"
104+
},
105+
{
106+
"name": "clang-release",
107+
"configurePreset": "clang-release"
108+
}
109+
],
110+
"testPresets": [
111+
{
112+
"name": "debug",
113+
"configurePreset": "debug",
114+
"output": {
115+
"outputOnFailure": true
116+
}
117+
},
118+
{
119+
"name": "release",
120+
"configurePreset": "release",
121+
"output": {
122+
"outputOnFailure": true
123+
}
124+
},
125+
{
126+
"name": "coverage",
127+
"configurePreset": "coverage",
128+
"output": {
129+
"outputOnFailure": true
130+
}
131+
},
132+
{
133+
"name": "clang-debug",
134+
"configurePreset": "clang-debug",
135+
"output": {
136+
"outputOnFailure": true
137+
}
138+
},
139+
{
140+
"name": "clang-release",
141+
"configurePreset": "clang-release",
142+
"output": {
143+
"outputOnFailure": true
144+
}
145+
}
146+
]
147+
}

ROADMAP.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
| 现状 | 建议 | 状态 |
1010
|------|------|------|
1111
| CMake 3.15 | 升级到 **CMake 3.20+**,支持更多现代特性 | ⬜ 待做 |
12-
| 无 CMake Presets | 添加 **`CMakePresets.json`** | ⬜ 待做 |
12+
| 无 CMake Presets | 添加 **`CMakePresets.json`** | ✅ 完成 |
1313
| 无预编译头 | 添加 **PCH (Precompiled Headers)** | ⬜ 待做 |
1414
| 无 Unity Build | 添加 **Unity Build** 选项 | ⬜ 待做 |
1515
| 无 C++20 模块 | 考虑实验性支持 **C++20 Modules** | ⬜ 待做 |
@@ -117,7 +117,7 @@
117117
- [ ] 添加 `SECURITY.md` - 明确漏洞报告流程
118118
- [ ] 添加 `.gitattributes` - 统一换行符处理
119119
- [ ] 添加 `.editorconfig` - 统一编辑器配置
120-
- [ ] 添加 `CMakeUserPresets.json` 到 .gitignore
120+
- [x] 添加 `CMakeUserPresets.json` 到 .gitignore
121121
- [ ] vcpkg baseline pinning - 确保可重复构建
122122
- [ ] 添加 Conan lockfile
123123

@@ -131,7 +131,7 @@
131131
| 🔴 高 | 代码覆盖率 | ✅ 完成 |
132132
| 🔴 高 | CONTRIBUTING.md | ⬜ 待做 |
133133
| 🔴 高 | Issue/PR 模板 | ⬜ 待做 |
134-
| 🟡 中 | CMakePresets.json | ⬜ 待做 |
134+
| 🟡 中 | CMakePresets.json | ✅ 完成 |
135135
| 🟡 中 | Doxygen API 文档 | ⬜ 待做 |
136136
| 🟡 中 | CHANGELOG.md | ✅ 完成 |
137137
| 🟡 中 | ccache | ⬜ 待做 |
@@ -147,6 +147,7 @@
147147

148148
| 日期 | 变更 |
149149
|------|------|
150+
| 2026-03-06 | 添加 CMakePresets.json,CI 使用预设构建 |
150151
| 2026-03-04 | 添加代码覆盖率支持(Codecov)|
151152
| 2026-03-04 | 更新 CHANGELOG.md |
152153
| 2026-03-04 | 创建路线图文件 |

0 commit comments

Comments
 (0)