Skip to content

Commit 48c6325

Browse files
committed
new:添加构建脚本和部分开发说明文档
1 parent f3ab714 commit 48c6325

12 files changed

+465
-8
lines changed

CMakePresets.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,17 @@
1313
"binaryDir": "${sourceDir}/build/default",
1414
"cacheVariables": {
1515
"CMAKE_EXPORT_COMPILE_COMMANDS": "YES",
16-
"CMAKE_BUILD_TYPE": "Debug",
17-
"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
16+
"CMAKE_BUILD_TYPE": "Debug"
1817
}
1918
},
2019
{
2120
"name": "debug",
22-
"displayName": "Default Config",
21+
"displayName": "Debug Config",
2322
"generator": "Visual Studio 17 2022",
2423
"binaryDir": "${sourceDir}/build/debug",
2524
"cacheVariables": {
2625
"CMAKE_EXPORT_COMPILE_COMMANDS": "YES",
27-
"CMAKE_BUILD_TYPE": "Debug",
28-
"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
26+
"CMAKE_BUILD_TYPE": "Debug"
2927
}
3028
},
3129
{
@@ -36,9 +34,7 @@
3634
"cacheVariables": {
3735
"CMAKE_EXPORT_COMPILE_COMMANDS": "YES",
3836
"CMAKE_BUILD_TYPE": "Release",
39-
"CMAKE_TOOLSET": "v142",
40-
"VCPKG_TARGET_TRIPLET": "x64-windows",
41-
"CMAKE_TOOLCHAIN_FILE": "${sourceDir}/tools/vcpkg/scripts/buildsystems/vcpkg.cmake"
37+
"CMAKE_VS_PLATFORM_TOOLSET": "v142"
4238
}
4339
}
4440
],

build_all.bat

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@echo off
2+
3+
:: 获取所有预设配置名称
4+
for /f "skip=2 tokens=1" %%p in ('cmake --list-presets') do (
5+
set PRESET=%%p
6+
echo Building preset: %%p
7+
cmake --preset=%%p
8+
if errorlevel 1 (
9+
echo Build failed for preset: %%p
10+
exit /b 1
11+
)
12+
cmake --build --preset=%%p-build
13+
if errorlevel 1 (
14+
echo Build failed for preset: %%p-build
15+
exit /b 1
16+
)
17+
echo Build completed for preset: %%p
18+
)
19+
20+
echo All presets built successfully!

build_all.ps1

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 获取所有预设配置名称
2+
#$PRESETS = cmake --list-presets | Select-Object -Skip 2 | ForEach-Object { $_.Split()[0] }
3+
$PRESETS = cmake --list-presets | Select-Object -Skip 1 | ForEach-Object {
4+
if ($_ -match '^\s*"([^"]+)"') {
5+
$matches[1]
6+
}
7+
}
8+
9+
# 打印 cmake --list-presets 的输出
10+
Write-Host "Output of cmake --list-presets:"
11+
cmake --list-presets
12+
13+
# 打印解析后的预设名称
14+
Write-Host "Parsed presets:"
15+
$PRESETS
16+
17+
# 检查是否有预设配置
18+
if (-Not $PRESETS) {
19+
Write-Host "No presets found in CMakePresets.json." -ForegroundColor Red
20+
exit 1
21+
}
22+
23+
# 遍历每个预设配置并构建
24+
foreach ($PRESET in $PRESETS) {
25+
Write-Host "Building preset: $PRESET" -ForegroundColor Cyan
26+
cmake --preset=$PRESET
27+
if (-Not $?) {
28+
Write-Host "Build failed for preset: $PRESET" -ForegroundColor Red
29+
exit 1
30+
}
31+
cmake --build --preset=${PRESET}-build
32+
if (-Not $?) {
33+
Write-Host "Build failed for preset: $PRESET" -ForegroundColor Red
34+
exit 1
35+
}
36+
Write-Host "Build completed for preset: $PRESET" -ForegroundColor Green
37+
}
38+
39+
Write-Host "All presets built successfully!" -ForegroundColor Green

build_all.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
# 获取所有预设配置名称
4+
PRESETS=$(cmake --list-presets | awk 'NR>2 {print $1}')
5+
6+
# 检查是否有预设配置
7+
if [ -z "$PRESETS" ]; then
8+
echo "No presets found in CMakePresets.json."
9+
exit 1
10+
fi
11+
12+
# 遍历每个预设配置并构建
13+
for PRESET in $PRESETS; do
14+
echo "Building preset: $PRESET"
15+
cmake --preset=$PRESET && cmake --build --preset=${PRESET}-build
16+
if [ $? -ne 0 ]; then
17+
echo "Build failed for preset: $PRESET"
18+
exit 1
19+
fi
20+
echo "Build completed for preset: $PRESET"
21+
done
22+
23+
echo "All presets built successfully!"
File renamed without changes.
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
以下是针对 CMake 项目通过 `CMakePresets.json` 构建的脚本,支持一次性构建所有预设配置。脚本分别用 `bash``PowerShell``bat` 编写。
2+
3+
---
4+
5+
### 1. **Bash 脚本 (`build_all.sh`)**
6+
适用于 Linux/macOS 或 Windows 上的 Git Bash/WSL。
7+
8+
```bash
9+
#!/bin/bash
10+
11+
# 获取所有预设配置名称
12+
PRESETS=$(cmake --list-presets | awk 'NR>2 {print $1}')
13+
14+
# 检查是否有预设配置
15+
if [ -z "$PRESETS" ]; then
16+
echo "No presets found in CMakePresets.json."
17+
exit 1
18+
fi
19+
20+
# 遍历每个预设配置并构建
21+
for PRESET in $PRESETS; do
22+
echo "Building preset: $PRESET"
23+
cmake --preset=$PRESET && cmake --build --preset=$PRESET
24+
if [ $? -ne 0 ]; then
25+
echo "Build failed for preset: $PRESET"
26+
exit 1
27+
fi
28+
echo "Build completed for preset: $PRESET"
29+
done
30+
31+
echo "All presets built successfully!"
32+
```
33+
34+
#### 使用方法:
35+
1. 将脚本保存为 `build_all.sh`
36+
2. 赋予执行权限:`chmod +x build_all.sh`
37+
3. 运行脚本:`./build_all.sh`
38+
39+
---
40+
41+
### 2. **PowerShell 脚本 (`build_all.ps1`)**
42+
适用于 Windows 上的 PowerShell。
43+
44+
```powershell
45+
# 获取所有预设配置名称
46+
$PRESETS = cmake --list-presets | Select-Object -Skip 2 | ForEach-Object { $_.Split()[0] }
47+
48+
# 检查是否有预设配置
49+
if (-Not $PRESETS) {
50+
Write-Host "No presets found in CMakePresets.json." -ForegroundColor Red
51+
exit 1
52+
}
53+
54+
# 遍历每个预设配置并构建
55+
foreach ($PRESET in $PRESETS) {
56+
Write-Host "Building preset: $PRESET" -ForegroundColor Cyan
57+
cmake --preset=$PRESET
58+
if (-Not $?) {
59+
Write-Host "Build failed for preset: $PRESET" -ForegroundColor Red
60+
exit 1
61+
}
62+
cmake --build --preset=$PRESET
63+
if (-Not $?) {
64+
Write-Host "Build failed for preset: $PRESET" -ForegroundColor Red
65+
exit 1
66+
}
67+
Write-Host "Build completed for preset: $PRESET" -ForegroundColor Green
68+
}
69+
70+
Write-Host "All presets built successfully!" -ForegroundColor Green
71+
```
72+
73+
#### 使用方法:
74+
1. 将脚本保存为 `build_all.ps1`
75+
2. 在 PowerShell 中运行脚本:`.\build_all.ps1`
76+
- 如果遇到权限问题,请先运行:`Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass`
77+
78+
---
79+
80+
### 3. **Batch 脚本 (`build_all.bat`)**
81+
适用于 Windows 上的命令提示符(CMD)。
82+
83+
```bat
84+
@echo off
85+
86+
:: 获取所有预设配置名称
87+
for /f "skip=2 tokens=1" %%p in ('cmake --list-presets') do (
88+
set PRESET=%%p
89+
echo Building preset: %%p
90+
cmake --preset=%%p
91+
if errorlevel 1 (
92+
echo Build failed for preset: %%p
93+
exit /b 1
94+
)
95+
cmake --build --preset=%%p
96+
if errorlevel 1 (
97+
echo Build failed for preset: %%p
98+
exit /b 1
99+
)
100+
echo Build completed for preset: %%p
101+
)
102+
103+
echo All presets built successfully!
104+
```
105+
106+
#### 使用方法:
107+
1. 将脚本保存为 `build_all.bat`
108+
2. 双击运行或在命令提示符中运行:`build_all.bat`
109+
110+
---
111+
112+
### 功能说明:
113+
1. **获取预设配置**
114+
- 使用 `cmake --list-presets` 获取 `CMakePresets.json` 中定义的所有预设配置名称。
115+
2. **遍历并构建**
116+
- 遍历每个预设配置,依次运行 `cmake --preset``cmake --build --preset`
117+
3. **错误处理**
118+
- 如果某个预设配置构建失败,脚本会输出错误信息并退出。
119+
120+
---
121+
122+
### 注意事项:
123+
1. **CMake 版本**
124+
- 确保 CMake 版本支持 `--preset``--list-presets` 参数(CMake 3.20 及以上版本)。
125+
2. **预设配置**
126+
- 确保 `CMakePresets.json` 文件已正确配置。
127+
3. **构建目录**
128+
- 每个预设配置的构建目录由 `CMakePresets.json` 中的 `binaryDir` 指定。
129+
130+
---
131+
132+
### 示例 `CMakePresets.json`
133+
```json
134+
{
135+
"version": 3,
136+
"configurePresets": [
137+
{
138+
"name": "debug",
139+
"displayName": "Debug",
140+
"binaryDir": "build/debug",
141+
"generator": "Ninja",
142+
"cacheVariables": {
143+
"CMAKE_BUILD_TYPE": "Debug"
144+
}
145+
},
146+
{
147+
"name": "release",
148+
"displayName": "Release",
149+
"binaryDir": "build/release",
150+
"generator": "Ninja",
151+
"cacheVariables": {
152+
"CMAKE_BUILD_TYPE": "Release"
153+
}
154+
}
155+
],
156+
"buildPresets": [
157+
{
158+
"name": "debug",
159+
"configurePreset": "debug"
160+
},
161+
{
162+
"name": "release",
163+
"configurePreset": "release"
164+
}
165+
]
166+
}
167+
```
168+
169+
---
170+
171+
希望这些脚本能帮助你一次性构建所有 CMake 预设配置!

0 commit comments

Comments
 (0)