@chengzhao-dev/dsh-cpp-workflow 0.7.6 → 0.7.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +8 -8
- package/README.md +10 -10
- package/lib/client.js +7 -3
- package/lib/comment-policy.js +7 -3
- package/lib/index.js +2 -1
- package/lib/workspace.js +17 -3
- package/package.json +1 -1
- package/skills/building-cpp/SKILL.md +6 -6
- package/skills/building-cpp/assets/config/.clang-format +8 -8
- package/skills/building-cpp/assets/config/.clang-tidy +0 -4
- package/skills/building-cpp/assets/config/.vscode/settings.json +11 -0
- package/skills/building-cpp/assets/templates/projects/common/running-build.sh +50 -43
- package/skills/building-cpp/assets/templates/projects/multi/CMakeLists.txt +6 -10
- package/skills/building-cpp/assets/templates/projects/multi/include/greeting.h +1 -4
- package/skills/building-cpp/assets/templates/projects/multi/src/greeting.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/multi/src/main.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/shared-library/CMakeLists.txt +6 -10
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/CMakeLists.txt +3 -10
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/include/greeting.h +1 -4
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/src/greeting.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/shared-library/src/main.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/single/CMakeLists.txt +4 -8
- package/skills/building-cpp/assets/templates/projects/single/src/main.cpp +4 -4
- package/skills/building-cpp/assets/templates/projects/static-library/CMakeLists.txt +4 -8
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/CMakeLists.txt +3 -10
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/include/greeting.h +1 -4
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/src/greeting.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/static-library/src/main.cpp +2 -2
- package/skills/building-cpp/references/01-build-script.md +8 -23
- package/skills/building-cpp/references/02-environment.md +9 -8
- package/skills/building-cpp/scripts/checking-environment.mjs +45 -13
- package/skills/building-cpp/steps/01-build.md +5 -4
- package/skills/building-cpp/steps/02-tooling.md +6 -6
- package/skills/building-cpp/steps/03-check.md +8 -6
- package/skills/building-cpp/steps/04-vscode.md +4 -4
- package/skills/developing-cpp/SKILL.md +4 -5
- package/skills/developing-cpp/references/comments/01-comment-rules.md +6 -6
- package/skills/developing-cpp/references/comments/02-style.md +20 -7
- package/skills/developing-cpp/references/comments/examples/01-declaration-class.md +2 -2
- package/skills/developing-cpp/references/comments/examples/04-declaration-file.md +5 -3
- package/skills/developing-cpp/references/comments/examples/05-declaration-function.md +1 -1
- package/skills/developing-cpp/references/comments/glossary/01-comment-terms.md +5 -5
- package/skills/developing-cpp/references/design/01-project-design.md +1 -1
- package/skills/developing-cpp/references/design/session-layer.md +10 -10
- package/skills/developing-cpp/references/exceptions/01-exception-rules.md +1 -1
- package/skills/developing-cpp/references/index.md +12 -11
- package/skills/developing-cpp/references/language/glossary/01-language-terms.md +2 -2
- package/skills/developing-cpp/references/language/language-basics/02-initialization.md +2 -2
- package/skills/developing-cpp/references/language/language-basics/05-expressions.md +2 -2
- package/skills/developing-cpp/references/language/language-basics/06-functions.md +4 -3
- package/skills/developing-cpp/references/language/language-basics/07-classes.md +1 -1
- package/skills/developing-cpp/references/language/performance/02-complexity.md +2 -3
- package/skills/developing-cpp/references/language/performance/03-memory.md +1 -1
- package/skills/developing-cpp/references/language/performance/05-pitfalls.md +1 -1
- package/skills/developing-cpp/references/language/standard-library/04-algorithms.md +1 -1
- package/skills/developing-cpp/references/language/standard-library/06-format.md +1 -31
- package/skills/developing-cpp/references/language/standard-library/11-format-ecosystem.md +32 -0
- package/skills/developing-cpp/references/language/value-types/02-signed.md +2 -2
- package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +3 -2
- package/skills/developing-cpp/references/language/value-types/04-enum.md +4 -4
- package/skills/developing-cpp/references/layout/01-source-layout.md +1 -1
- package/skills/developing-cpp/references/layout/02-file-template.md +30 -43
- package/skills/developing-cpp/references/layout/03-declaration-order.md +8 -10
- package/skills/developing-cpp/references/naming/01-identifier-names.md +9 -3
- package/skills/developing-cpp/references/naming/glossary/04-square.md +1 -1
- package/skills/developing-cpp/references/style/01-google-cpp-style.md +10 -10
- package/skills/developing-cpp/scripts/checking-comments.mjs +33 -10
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +77 -10
- package/skills/developing-cpp/steps/03-declaration.md +5 -5
- package/skills/developing-cpp/steps/04-implementation.md +3 -3
- package/skills/engineering/SKILL.md +7 -6
- package/skills/engineering/references/cmake/assets/CompileSettings.cmake +100 -100
- package/skills/engineering/references/cmake/assets/LinkSettings.cmake +25 -25
- package/skills/engineering/references/cmake/assets/OutputSettings.cmake +38 -0
- package/skills/engineering/references/cmake/linking/01-libraries.md +1 -1
- package/skills/engineering/references/cmake/modules/01-functions.md +11 -10
- package/skills/engineering/references/cmake/standards/c++11/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++14/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++17/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++20/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++23/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/index.md +6 -6
- package/skills/engineering/references/cmake/standards/versions.md +1 -1
- package/skills/engineering/references/cmake/targets/01-target-rules.md +8 -18
- package/skills/engineering/references/formatting/01-encoding.md +6 -3
- package/skills/engineering/references/formatting/02-clang-format.md +3 -3
- package/skills/engineering/references/formatting/03-clang-tidy.md +2 -2
- package/skills/engineering/references/formatting/04-clangd.md +4 -4
- package/skills/engineering/references/formatting/examples/01-clang-format.md +2 -2
- package/skills/engineering/references/formatting/examples/04-config-comments.md +43 -0
- package/skills/engineering/references/index.md +4 -3
- package/skills/engineering/scripts/checking-encoding.mjs +2 -1
- package/skills/engineering/steps/01-targets.md +3 -3
- package/skills/engineering/steps/02-modules.md +3 -9
- package/skills/engineering/steps/03-check.md +1 -1
- package/skills/shared/operations.md +1 -1
- package/skills/shared/rule-priority.md +1 -1
- package/templates/cpp-workflow-project/SKILL.md +5 -3
|
@@ -5,23 +5,19 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
|
5
5
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
|
6
6
|
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
|
|
7
7
|
|
|
8
|
-
enable_testing()
|
|
9
|
-
|
|
10
8
|
add_subdirectory(greeting)
|
|
11
9
|
|
|
12
10
|
add_executable(app src/main.cpp)
|
|
13
11
|
target_compile_features(app PRIVATE cxx_std_23)
|
|
14
12
|
set_target_properties(app PROPERTIES
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
CXX_STANDARD 23
|
|
14
|
+
CXX_STANDARD_REQUIRED ON
|
|
15
|
+
CXX_EXTENSIONS OFF
|
|
18
16
|
)
|
|
19
|
-
|
|
17
|
+
# 警告与 -Werror 由工程按需要打开(set_target_warnings),模板不默认加在 target 上。
|
|
20
18
|
target_link_libraries(app PRIVATE greeting)
|
|
21
19
|
if(APPLE)
|
|
22
|
-
|
|
20
|
+
set_target_properties(app PROPERTIES BUILD_RPATH "@loader_path/../lib")
|
|
23
21
|
else()
|
|
24
|
-
|
|
22
|
+
set_target_properties(app PROPERTIES BUILD_RPATH "$ORIGIN/../lib")
|
|
25
23
|
endif()
|
|
26
|
-
|
|
27
|
-
add_test(NAME app_smoke COMMAND app)
|
package/skills/building-cpp/assets/templates/projects/shared-library/greeting/CMakeLists.txt
CHANGED
|
@@ -2,14 +2,7 @@ add_library(greeting SHARED src/greeting.cpp)
|
|
|
2
2
|
target_include_directories(greeting PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
|
3
3
|
target_compile_features(greeting PUBLIC cxx_std_23)
|
|
4
4
|
set_target_properties(greeting PROPERTIES
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
)
|
|
9
|
-
target_compile_options(greeting PRIVATE
|
|
10
|
-
-Wall
|
|
11
|
-
-Wextra
|
|
12
|
-
-Werror
|
|
13
|
-
-Wold-style-cast
|
|
14
|
-
-Wshadow
|
|
5
|
+
CXX_STANDARD 23
|
|
6
|
+
CXX_STANDARD_REQUIRED ON
|
|
7
|
+
CXX_EXTENSIONS OFF
|
|
15
8
|
)
|
package/skills/building-cpp/assets/templates/projects/shared-library/greeting/include/greeting.h
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
#
|
|
2
|
-
#define CPP_WORKFLOW_TEMPLATE_GREETING_INCLUDE_GREETING_H_
|
|
1
|
+
#pragma once
|
|
3
2
|
|
|
4
3
|
#include <string>
|
|
5
4
|
|
|
@@ -8,5 +7,3 @@ namespace greeting {
|
|
|
8
7
|
std::string MakeGreeting(const std::string& name);
|
|
9
8
|
|
|
10
9
|
} // namespace greeting
|
|
11
|
-
|
|
12
|
-
#endif // CPP_WORKFLOW_TEMPLATE_GREETING_INCLUDE_GREETING_H_
|
package/skills/building-cpp/assets/templates/projects/shared-library/greeting/src/greeting.cpp
CHANGED
|
@@ -9,9 +9,9 @@ namespace greeting {
|
|
|
9
9
|
|
|
10
10
|
std::string MakeGreeting(const std::string& name) {
|
|
11
11
|
#if __cplusplus >= 202002L
|
|
12
|
-
|
|
12
|
+
return std::format("Hello, {}!", name);
|
|
13
13
|
#else
|
|
14
|
-
|
|
14
|
+
return "Hello, " + name + "!";
|
|
15
15
|
#endif
|
|
16
16
|
}
|
|
17
17
|
|
|
@@ -4,15 +4,11 @@ project({{PROJECT_NAME}} VERSION 0.1.0 LANGUAGES CXX)
|
|
|
4
4
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
5
5
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
|
6
6
|
|
|
7
|
-
enable_testing()
|
|
8
|
-
|
|
9
7
|
add_executable(app src/main.cpp)
|
|
10
8
|
target_compile_features(app PRIVATE cxx_std_23)
|
|
11
9
|
set_target_properties(app PROPERTIES
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
CXX_STANDARD 23
|
|
11
|
+
CXX_STANDARD_REQUIRED ON
|
|
12
|
+
CXX_EXTENSIONS OFF
|
|
15
13
|
)
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
add_test(NAME app_smoke COMMAND app)
|
|
14
|
+
# 警告与 -Werror 由工程按需要打开(set_target_warnings),模板不默认加在 target 上。
|
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
#endif
|
|
6
6
|
|
|
7
7
|
int main() {
|
|
8
|
-
|
|
8
|
+
const std::string name = "C++";
|
|
9
9
|
#if __cplusplus >= 202002L
|
|
10
|
-
|
|
10
|
+
std::cout << std::format("Hello, {}!\n", name);
|
|
11
11
|
#else
|
|
12
|
-
|
|
12
|
+
std::cout << "Hello, " << name << "!\n";
|
|
13
13
|
#endif
|
|
14
|
-
|
|
14
|
+
return 0;
|
|
15
15
|
}
|
|
@@ -5,18 +5,14 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
|
5
5
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
|
6
6
|
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
|
|
7
7
|
|
|
8
|
-
enable_testing()
|
|
9
|
-
|
|
10
8
|
add_subdirectory(greeting)
|
|
11
9
|
|
|
12
10
|
add_executable(app src/main.cpp)
|
|
13
11
|
target_compile_features(app PRIVATE cxx_std_23)
|
|
14
12
|
set_target_properties(app PROPERTIES
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
CXX_STANDARD 23
|
|
14
|
+
CXX_STANDARD_REQUIRED ON
|
|
15
|
+
CXX_EXTENSIONS OFF
|
|
18
16
|
)
|
|
19
|
-
|
|
17
|
+
# 警告与 -Werror 由工程按需要打开(set_target_warnings),模板不默认加在 target 上。
|
|
20
18
|
target_link_libraries(app PRIVATE greeting)
|
|
21
|
-
|
|
22
|
-
add_test(NAME app_smoke COMMAND app)
|
package/skills/building-cpp/assets/templates/projects/static-library/greeting/CMakeLists.txt
CHANGED
|
@@ -2,14 +2,7 @@ add_library(greeting STATIC src/greeting.cpp)
|
|
|
2
2
|
target_include_directories(greeting PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
|
3
3
|
target_compile_features(greeting PUBLIC cxx_std_23)
|
|
4
4
|
set_target_properties(greeting PROPERTIES
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
)
|
|
9
|
-
target_compile_options(greeting PRIVATE
|
|
10
|
-
-Wall
|
|
11
|
-
-Wextra
|
|
12
|
-
-Werror
|
|
13
|
-
-Wold-style-cast
|
|
14
|
-
-Wshadow
|
|
5
|
+
CXX_STANDARD 23
|
|
6
|
+
CXX_STANDARD_REQUIRED ON
|
|
7
|
+
CXX_EXTENSIONS OFF
|
|
15
8
|
)
|
package/skills/building-cpp/assets/templates/projects/static-library/greeting/include/greeting.h
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
#
|
|
2
|
-
#define CPP_WORKFLOW_TEMPLATE_GREETING_INCLUDE_GREETING_H_
|
|
1
|
+
#pragma once
|
|
3
2
|
|
|
4
3
|
#include <string>
|
|
5
4
|
|
|
@@ -8,5 +7,3 @@ namespace greeting {
|
|
|
8
7
|
std::string MakeGreeting(const std::string& name);
|
|
9
8
|
|
|
10
9
|
} // namespace greeting
|
|
11
|
-
|
|
12
|
-
#endif // CPP_WORKFLOW_TEMPLATE_GREETING_INCLUDE_GREETING_H_
|
package/skills/building-cpp/assets/templates/projects/static-library/greeting/src/greeting.cpp
CHANGED
|
@@ -9,9 +9,9 @@ namespace greeting {
|
|
|
9
9
|
|
|
10
10
|
std::string MakeGreeting(const std::string& name) {
|
|
11
11
|
#if __cplusplus >= 202002L
|
|
12
|
-
|
|
12
|
+
return std::format("Hello, {}!", name);
|
|
13
13
|
#else
|
|
14
|
-
|
|
14
|
+
return "Hello, " + name + "!";
|
|
15
15
|
#endif
|
|
16
16
|
}
|
|
17
17
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<!-- 读取时机:写构建脚本时;读完能回答:构建脚本;预算:一次只开这一份(
|
|
1
|
+
<!-- 读取时机:写构建脚本时;读完能回答:构建脚本;预算:一次只开这一份(73 行),不自动串联。 -->
|
|
2
2
|
# 构建脚本
|
|
3
3
|
|
|
4
4
|
`assets/templates/projects/common/running-build.sh` 是 POSIX build-host 脚本,适用于 Linux、macOS 和 Windows + WSL。`host=windows` + `build_host=native` 不是本版本支持的组合:声明校验会直接拒绝,脚本也不会在原生 Windows 壳里运行。脚本只做配置、构建、CTest 与运行;格式化与静态检查在编辑时手动跑,不混进构建入口。
|
|
@@ -7,13 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
不在 `running-build.sh` 里跑。构建脚本只做配置、构建、CTest 与运行;格式化是编辑时动作,不能让构建顺手改动源码。检查放在编辑器保存时或 CI 的独立步骤里。
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
```bash
|
|
13
|
-
clang-format --dry-run --Werror src/*.cpp include/*.h
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
需要修复时单独运行 `clang-format -i`,再回看 diff。`clang-format` 只排版,不会把行尾注释搬到上一行;数据成员注释位置按 `../../developing-cpp/references/comments/01-comment-rules.md` 的规则和 `checking-comments.mjs` 检查。
|
|
10
|
+
检查命令与顺序以 `../steps/03-check.md` 第 4 步为唯一出处:那里列出 `clang-format` 与 `clang-tidy` 的完整调用,本文件不再重复。需要修复时单独运行 `clang-format -i`,再回看 diff。`clang-format` 只排版,不会把行尾注释搬到上一行;数据成员注释位置按 `../../developing-cpp/references/comments/01-comment-rules.md` 的规则和 `checking-comments.mjs` 检查。
|
|
17
11
|
|
|
18
12
|
## 用法
|
|
19
13
|
|
|
@@ -24,18 +18,18 @@ clang-format --dry-run --Werror src/*.cpp include/*.h
|
|
|
24
18
|
|
|
25
19
|
- 参数是编译器名,只接受 `clang` / `gcc`;省略时默认 `clang`。
|
|
26
20
|
- 脚本以自身所在目录为工程根;把它放在工程根,从其他目录调用时会先 `cd` 到脚本目录。
|
|
27
|
-
-
|
|
21
|
+
- 构建目录默认 `build/clang` 或 `build/gcc`;脚本头声明的 `BUILD_DIR`、`BUILD_ROOT`、`APP_NAME` 只覆盖这一次运行,默认值不变。单一构建目录时把 `BUILD_ROOT` 设为与 `BUILD_DIR` 相同,脚本就不再补 `build/compile_commands.json` 链接;构建目录的取值以 `02-environment.md`「构建目录」的「单一目录」行为准。
|
|
28
22
|
- 脚本不安装依赖、不下载工具、不改 PATH;缺工具时让命令失败,由 `building-cpp` 的通过检查分诊。
|
|
29
23
|
|
|
30
24
|
## 结构
|
|
31
25
|
|
|
32
26
|
1. **shebang + 文件头**:标题一行、`#` 一行,再用「用法」「依赖」「构建目录」等行说明参数与支持的 build host;段落用单行注释,不画 `#===` 横幅。
|
|
33
|
-
2. **严格模式与目录**:`set -euo pipefail`;`cd "$(dirname "$0")"`;`
|
|
27
|
+
2. **严格模式与目录**:`set -euo pipefail`;`cd "$(dirname "$0")"`;`APP_NAME`、`BUILD_ROOT` 取环境变量默认值,`BUILD_DIR` 由编译器名推导;`COMPILER` 与 `CXX` 用 `readonly`。
|
|
34
28
|
3. **输出辅助**:`header()` 打印阶段名,`err()` 打印到 stderr;`trap ERR` 打印失败命令。
|
|
35
29
|
4. **configure**:每次运行都重新 configure,不复用缓存;传 `-G Ninja`、`-DCMAKE_BUILD_TYPE=Debug`、`-DCMAKE_C_COMPILER`、`-DCMAKE_CXX_COMPILER`、`-DCMAKE_EXPORT_COMPILE_COMMANDS=ON`。C 与 C++ 使用同一套编译器;没有 C 语言的工程会忽略 C 编译器设置。
|
|
36
30
|
5. **build**:`cmake --build "$BUILD_DIR"`。
|
|
37
|
-
6.
|
|
38
|
-
7. **test**:有 `tests/` 目录时 `ctest --test-dir "$BUILD_DIR" --output-on-failure --no-tests=error`,防止测试 discovery 失败后 0 条也算通过;没有测试目录时 `ctest --test-dir "$BUILD_DIR" --output-on-failure`,0
|
|
31
|
+
6. **编译数据库链接**:`BUILD_DIR` 与 `BUILD_ROOT` 相同时数据库就在该目录,不建链接;否则先 `rm -f "${BUILD_ROOT}/compile_commands.json"`,再用 `realpath --relative-to="${BUILD_ROOT}" "${BUILD_DIR}/compile_commands.json"` 求相对路径后 `ln -s` 到 `${BUILD_ROOT}/compile_commands.json`;不用 `ln -n`,它在 BSD/macOS 上不可移植。
|
|
32
|
+
7. **test**:有 `tests/` 目录时 `ctest --test-dir "$BUILD_DIR" --output-on-failure --no-tests=error`,防止测试 discovery 失败后 0 条也算通过;没有测试目录时 `ctest --test-dir "$BUILD_DIR" --output-on-failure --no-tests=ignore`,0 条不算失败;不写 `--no-tests=ignore` 时 ctest 会把 0 条当失败(退出码 8)。
|
|
39
33
|
8. **run**:`(cd "$BUILD_DIR/bin" && "./$APP_NAME")`;共享库的运行时路径由定构建的 CMake RPATH 负责,不在脚本里设全局 `LD_LIBRARY_PATH`。
|
|
40
34
|
|
|
41
35
|
最后一行是 `main "$@"`;函数定义在 `main()` 之前,`set` / `readonly` 在最前。
|
|
@@ -50,16 +44,7 @@ clang-format --dry-run --Werror src/*.cpp include/*.h
|
|
|
50
44
|
- 失败时保留原始命令输出;不要在脚本里 `2>/dev/null` 吞掉诊断。
|
|
51
45
|
- 脚本自身用 `bash -n` 检查语法;文件权限保持可执行。
|
|
52
46
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
工程需要 Release 时另建构建目录;模式只改变诊断详略,不改变失败方式:
|
|
56
|
-
|
|
57
|
-
- 返回值:两种模式的返回值相同,调用方不按 `NDEBUG` 分支。
|
|
58
|
-
- 自定义异常:两种模式都抛;Debug 的 `what()` 可以带 `file:line`,Release 只留值。
|
|
59
|
-
- `Check`:两种模式都在条件不成立时打印诊断并终止。
|
|
60
|
-
- `DebugCheck`:只在 Debug 生效,`NDEBUG` 后展开为空,参数不能带副作用。
|
|
61
|
-
|
|
62
|
-
不要写成 Debug 抛异常、Release 返回 false 的模式分支;这会把调用方逼成两套逻辑。规则详见 `../../developing-cpp/references/exceptions/01-exception-rules.md`。
|
|
47
|
+
Debug 与 Release 的返回值、异常与 `Check` / `DebugCheck` 合同见 `../../developing-cpp/references/exceptions/01-exception-rules.md`;构建脚本只换诊断详略,不改失败方式。
|
|
63
48
|
|
|
64
49
|
## 失败分诊
|
|
65
50
|
|
|
@@ -76,7 +61,7 @@ clang-format --dry-run --Werror src/*.cpp include/*.h
|
|
|
76
61
|
|
|
77
62
|
- `bash -n running-build.sh` 通过。
|
|
78
63
|
- 在声明的 build host 上,`./running-build.sh clang` 与 `./running-build.sh gcc` 都能从 configure 走到 run。
|
|
79
|
-
- 从工程根直接 `./build/clang/bin
|
|
64
|
+
- 从工程根直接 `./build/clang/bin/<入口 target>` 能启动(模板默认 `app`,本书是 `terminal`;共享库场景由 RPATH 保证)。
|
|
80
65
|
- 切换编译器后 `build/compile_commands.json` 指向新数据库。
|
|
81
66
|
- 调用者的 `LD_LIBRARY_PATH` 在脚本结束后不变。
|
|
82
67
|
- Windows 原生:声明校验已拒绝该组合;不要伪造 PowerShell 脚本,也不要绕过声明。
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
<!-- 读取时机:配环境或检查工具时;读完能回答:环境组合;预算:一次只开这一份(
|
|
1
|
+
<!-- 读取时机:配环境或检查工具时;读完能回答:环境组合;预算:一次只开这一份(116 行),不自动串联。 -->
|
|
2
2
|
# 环境组合
|
|
3
3
|
|
|
4
|
-
环境是**声明**,不是探测结果。项目技能 `cpp-workflow-project`
|
|
4
|
+
环境是**声明**,不是探测结果。项目技能 `cpp-workflow-project`(目录名也接受 `cpp_workflow_project`)的 `environment.json` 写清楚三个环境轴和一个语言标准;构建规则、工具配置和检查都按这个声明执行。声明为空或组合非法时,先修声明,不要靠猜。
|
|
5
5
|
|
|
6
6
|
## 四个轴
|
|
7
7
|
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
| --- | --- | --- |
|
|
10
10
|
| `host` | `windows` / `linux` / `macos` | 编辑器与代理壳所在的系统 |
|
|
11
11
|
| `build_host` | `native` / `wsl` | 编译在哪执行;`wsl` 只对 `host=windows` 合法 |
|
|
12
|
-
| `compiler` | `clang` / `gcc` | 一次只配一个;不支持 MSVC、clang-cl,也不把 C
|
|
12
|
+
| `compiler` | `clang` / `gcc` | 一次只配一个;不支持 MSVC、clang-cl,也不把 C 编译器单独作为一个轴;资产里的 MSVC 分支只供阅读,不进入声明 |
|
|
13
13
|
| `standard` | `c++11` / `c++14` / `c++17` / `c++20` / `c++23` | 一个工程一个标准;新工程默认 `c++23`,旧声明缺字段时也按 `c++23` 处理 |
|
|
14
14
|
|
|
15
15
|
```json
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
- `host=windows` + `build_host=native`:本版本不支持,静态校验直接返回失败。
|
|
41
41
|
- `compiler=msvc`、`compiler=clang-cl`、`compiler=c`。
|
|
42
42
|
|
|
43
|
-
`windows` + `native`
|
|
43
|
+
`windows` + `native` 不再作为「合法但不能构建」的声明保留;CMake 资产里的 MSVC 分支不是受支持的环境,不能据此写 `compiler=msvc`。声明校验直接返回 `ok: false`,`issues` 只有一句「本版本不能在 Windows 原生壳里构建」;不要建议改 WSL,也不要临时写 PowerShell 脚本伪装成已支持。
|
|
44
44
|
|
|
45
45
|
## 工具角色
|
|
46
46
|
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
| 语言服务 | `clangd` | `clangd` |
|
|
53
53
|
| 构建 | `cmake` + `ninja` | `cmake` + `ninja` |
|
|
54
54
|
| 测试 | `ctest` | `ctest` |
|
|
55
|
-
| 程序 |
|
|
55
|
+
| 程序 | 项目技能点名的 target | 同一名字 |
|
|
56
56
|
|
|
57
57
|
工具角色与编译器解耦:GCC 模式下仍用 Clang 的格式化、静态检查和语言服务。不要因为编译器是 GCC 就换成 `gcc-format` 之类不存在的工具。工程确有 C 源时,C 编译器用同一套的 `clang` 或 `gcc`,不能与 C++ 编译器分属两套;`--live` 的 compiler 角色核对的是 C++ 驱动,C 驱动缺失时由 configure 报错。
|
|
58
58
|
|
|
@@ -72,15 +72,16 @@
|
|
|
72
72
|
| clang | `build/clang/` | `build/clang/compile_commands.json` |
|
|
73
73
|
| gcc | `build/gcc/` | `build/gcc/compile_commands.json` |
|
|
74
74
|
| 当前 | `build/` | `build/compile_commands.json` → 上面之一的符号链接 |
|
|
75
|
+
| 单一目录 | 项目声明的目录(如 `games/tictactoe/build/`) | `<build-dir>/compile_commands.json`,不建符号链接 |
|
|
75
76
|
|
|
76
|
-
一次 configure 只用一个编译器;切换编译器时用另一个目录,不要在同一目录里改 `CMAKE_CXX_COMPILER`
|
|
77
|
+
一次 configure 只用一个编译器;切换编译器时用另一个目录,不要在同一目录里改 `CMAKE_CXX_COMPILER` 后复用缓存。项目技能声明单一构建目录时以项目为准(本书 `games/tictactoe/build/`),此时不建 `build/clang` 与 `build/gcc`,编译数据库仍在 `<build-dir>/compile_commands.json`。
|
|
77
78
|
|
|
78
79
|
## 活动校验
|
|
79
80
|
|
|
80
81
|
`<skill-dir>/scripts/checking-environment.mjs` 做两件事:
|
|
81
82
|
|
|
82
|
-
1.
|
|
83
|
-
2. **活动**(`--live`):先做声明校验,再核对当前进程是否在声明的 build host 上,然后查 PATH
|
|
83
|
+
1. **静态**(默认):读声明文件,检查三个环境轴、`standard` 和组合。轴为空或组合非法时以退出码 2 结束,不查找任何可执行文件。默认路径是 `.agents/skills/cpp-workflow-project/environment.json`(向上到 git 根);其他位置用 `--environment <file>` 指定。
|
|
84
|
+
2. **活动**(`--live`):先做声明校验,再核对当前进程是否在声明的 build host 上,然后查 PATH、角色和版本,并用 `-std=<standard> -fsyntax-only` 空编译确认编译器接受该标准。声明本身不合法时仍以退出码 2 结束;声明合法但活动核对失败时以退出码 3 结束。
|
|
84
85
|
|
|
85
86
|
```bash
|
|
86
87
|
node "<skill-dir>/scripts/checking-environment.mjs" --project . --json
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// cpp-workflow 项目技能的只读环境检查。
|
|
3
3
|
//
|
|
4
|
-
// 静态模式校验 `.agents/skills
|
|
4
|
+
// 静态模式校验 `.agents/skills/<项目技能目录>/environment.json`,不碰 PATH。
|
|
5
5
|
// `--live` 另查当前构建主机、声明的工具角色与版本兼容性。
|
|
6
6
|
//
|
|
7
7
|
// 不安装、不下载、不改 PATH,也不向工程写入。
|
|
@@ -16,7 +16,9 @@ export const BUILD_HOSTS = ['native', 'wsl']
|
|
|
16
16
|
export const COMPILERS = ['clang', 'gcc']
|
|
17
17
|
export const STANDARDS = ['c++11', 'c++14', 'c++17', 'c++20', 'c++23']
|
|
18
18
|
export const DEFAULT_STANDARD = 'c++23'
|
|
19
|
-
const
|
|
19
|
+
const SKILL_DIR_NAMES = ['cpp-workflow-project', 'cpp_workflow_project']
|
|
20
|
+
const PROJECT_SKILL_DIRS = SKILL_DIR_NAMES.map((name) => path.join('.agents', 'skills', name))
|
|
21
|
+
const PROJECT_SKILL_DIR = PROJECT_SKILL_DIRS[0]
|
|
20
22
|
const MAX_ASCENT = 64
|
|
21
23
|
|
|
22
24
|
export const EXIT = {
|
|
@@ -137,15 +139,17 @@ export async function findProjectEnvironment(startDir) {
|
|
|
137
139
|
return undefined
|
|
138
140
|
}
|
|
139
141
|
|
|
140
|
-
//
|
|
141
|
-
if (path.basename(cursor)
|
|
142
|
+
// 允许直接指向项目技能目录(下划线或连字符命名)。
|
|
143
|
+
if (SKILL_DIR_NAMES.includes(path.basename(cursor))) {
|
|
142
144
|
const direct = path.join(cursor, 'environment.json')
|
|
143
145
|
if (await exists(direct)) return direct
|
|
144
146
|
}
|
|
145
147
|
|
|
146
148
|
for (let depth = 0; depth < MAX_ASCENT; depth += 1) {
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
+
for (const dir of PROJECT_SKILL_DIRS) {
|
|
150
|
+
const candidate = path.join(cursor, dir, 'environment.json')
|
|
151
|
+
if (await exists(candidate)) return candidate
|
|
152
|
+
}
|
|
149
153
|
if (await exists(path.join(cursor, '.git'))) return undefined
|
|
150
154
|
const parent = path.dirname(cursor)
|
|
151
155
|
if (parent === cursor) return undefined
|
|
@@ -205,6 +209,13 @@ function firstLines(text, count = 3) {
|
|
|
205
209
|
.join(' | ')
|
|
206
210
|
}
|
|
207
211
|
|
|
212
|
+
/** Clang 16 等旧主版本只认 c++2b 拼写;其余情况原样返回。 */
|
|
213
|
+
export function standardFlag(compiler, major, standard) {
|
|
214
|
+
const level = Number.parseInt(String(standard).replace(/^c\+\+/, ''), 10)
|
|
215
|
+
if (compiler === 'clang' && Number.isFinite(major) && major < 17 && level === 23) return 'c++2b'
|
|
216
|
+
return standard
|
|
217
|
+
}
|
|
218
|
+
|
|
208
219
|
function parseVersion(text) {
|
|
209
220
|
const apple = /Apple clang version\s+(\d+)\.(\d+)(?:\.(\d+))?/i.exec(text)
|
|
210
221
|
if (apple) {
|
|
@@ -324,6 +335,16 @@ export function checkLive(declaration, options = {}) {
|
|
|
324
335
|
}
|
|
325
336
|
|
|
326
337
|
const compilerVersion = versions.get('compiler')
|
|
338
|
+
const compilerExecutable = resolved.get('compiler')
|
|
339
|
+
if (compilerExecutable && !options.runVersion) {
|
|
340
|
+
const flag = `-std=${standardFlag(compiler, compilerVersion?.major, validation.axes.standard)}`
|
|
341
|
+
const probe = run(compilerExecutable, ['-x', 'c++', flag, '-fsyntax-only', '-'], {})
|
|
342
|
+
if (probe.status !== 0) {
|
|
343
|
+
issues.push(
|
|
344
|
+
`compiler 不接受 ${flag};输出:${firstLines(`${probe.stdout}\n${probe.stderr}`) || '(空)'}。升级编译器或把 standard 降到实际可用的档位`,
|
|
345
|
+
)
|
|
346
|
+
}
|
|
347
|
+
}
|
|
327
348
|
const formatVersion = versions.get('clang-format')
|
|
328
349
|
const tidyVersion = versions.get('clang-tidy')
|
|
329
350
|
const clangdVersion = versions.get('clangd')
|
|
@@ -370,7 +391,7 @@ export function checkLive(declaration, options = {}) {
|
|
|
370
391
|
}
|
|
371
392
|
|
|
372
393
|
function parseArgs(argv) {
|
|
373
|
-
const options = { project: process.cwd(), live: false, json: false, help: false }
|
|
394
|
+
const options = { project: process.cwd(), live: false, json: false, help: false, environment: undefined }
|
|
374
395
|
for (let index = 0; index < argv.length; index += 1) {
|
|
375
396
|
const arg = argv[index]
|
|
376
397
|
if (arg === '--live') {
|
|
@@ -386,6 +407,14 @@ function parseArgs(argv) {
|
|
|
386
407
|
index += 1
|
|
387
408
|
} else if (arg.startsWith('--project=')) {
|
|
388
409
|
options.project = arg.slice('--project='.length)
|
|
410
|
+
} else if (arg === '--environment') {
|
|
411
|
+
const value = argv[index + 1]
|
|
412
|
+
if (!value) throw new Error('--environment 需要一个文件路径')
|
|
413
|
+
options.environment = value
|
|
414
|
+
index += 1
|
|
415
|
+
} else if (arg.startsWith('--environment=')) {
|
|
416
|
+
options.environment = arg.slice('--environment='.length)
|
|
417
|
+
if (!options.environment) throw new Error('--environment 不能为空')
|
|
389
418
|
} else {
|
|
390
419
|
throw new Error(`未知参数:${arg}`)
|
|
391
420
|
}
|
|
@@ -393,12 +422,13 @@ function parseArgs(argv) {
|
|
|
393
422
|
return options
|
|
394
423
|
}
|
|
395
424
|
|
|
396
|
-
const USAGE = `用法: node checking-environment.mjs [--project <dir>] [--live] [--json]
|
|
425
|
+
const USAGE = `用法: node checking-environment.mjs [--project <dir>] [--environment <file>] [--live] [--json]
|
|
397
426
|
|
|
398
|
-
默认读取 <dir>/.agents/skills/cpp-workflow-project/environment.json
|
|
399
|
-
|
|
427
|
+
默认读取 <dir>/.agents/skills/cpp-workflow-project/environment.json
|
|
428
|
+
(项目技能目录名也接受 cpp_workflow_project),
|
|
429
|
+
并向上走到 git 根(最多 64 层)。--environment 直接指定声明文件。
|
|
400
430
|
|
|
401
|
-
--live 校验当前 build host、PATH
|
|
431
|
+
--live 校验当前 build host、PATH 角色、版本与 -std 支持
|
|
402
432
|
--json 输出机器可读结果
|
|
403
433
|
--help 显示本帮助`
|
|
404
434
|
|
|
@@ -416,10 +446,12 @@ async function main(argv) {
|
|
|
416
446
|
}
|
|
417
447
|
|
|
418
448
|
const start = path.resolve(options.project)
|
|
419
|
-
const file =
|
|
449
|
+
const file = options.environment
|
|
450
|
+
? path.resolve(options.environment)
|
|
451
|
+
: await findProjectEnvironment(start)
|
|
420
452
|
if (!file) {
|
|
421
453
|
process.stderr.write(
|
|
422
|
-
`找不到环境声明:${path.join(start, PROJECT_SKILL_DIR, 'environment.json')}
|
|
454
|
+
`找不到环境声明:${path.join(start, PROJECT_SKILL_DIR, 'environment.json')}(或它的任一上层目录;项目技能目录名也接受 cpp_workflow_project);也可用 --environment <file> 指定\n`,
|
|
423
455
|
)
|
|
424
456
|
return EXIT.declaration
|
|
425
457
|
}
|
|
@@ -26,15 +26,15 @@ build/
|
|
|
26
26
|
| `compiler` | `clang` / `gcc` | 用哪个编译器;一次只配一个 |
|
|
27
27
|
| `standard` | `c++11`–`c++23` | 一个工程一个语言标准;新工程默认 `c++23`,已有工程按原声明 |
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
声明是否合法的唯一判据是 `../references/02-environment.md`「合法组合」:非法时停在定构建,让用户把 `environment.json` 填对;`standard` 缺失按默认 `c++23` 继续并提示升级;`windows` + `native` 只给一句「本版本不能在 Windows 原生壳里构建」。不要替用户选 WSL、Clang 或构建目录。
|
|
30
30
|
|
|
31
31
|
## 规则
|
|
32
32
|
|
|
33
|
-
- 生成器固定单配置 Ninja,不用 Ninja Multi-Config
|
|
33
|
+
- 生成器固定单配置 Ninja,不用 Ninja Multi-Config:同一源文件会写多条命令,读数据库时容易误选;构建目录固定 `build/clang` 或 `build/gcc`,项目技能声明单一构建目录时以项目目录为准。
|
|
34
34
|
- 一个 configure 只用一个编译器:`-DCMAKE_CXX_COMPILER=clang++` 或 `g++`;确有 C 源时 C 编译器与 C++ 编译器同属一套。
|
|
35
35
|
- `set(CMAKE_EXPORT_COMPILE_COMMANDS ON)` 由 `engineering` 写在顶层;Ninja 下产出 `compile_commands.json`。
|
|
36
36
|
- 输出目录由 `engineering` 统一设到 `build/<compiler>/bin` 与 `build/<compiler>/lib`;本步只核对,不重复设。
|
|
37
|
-
- 配置后把 `build/<compiler>/compile_commands.json` 符号链接到 `build/compile_commands.json`,让 clangd
|
|
37
|
+
- 配置后把 `build/<compiler>/compile_commands.json` 符号链接到 `build/compile_commands.json`,让 clangd 默认能找到当前数据库;换编译器时重建链接。项目声明单一构建目录时数据库就在该目录,不建这条链接(见 `../references/02-environment.md`「构建目录」的「单一目录」行)。
|
|
38
38
|
- 不写全局 `CMAKE_CXX_FLAGS`、`include_directories()` 或 `add_definitions()`;这些属于 target 契约,发现就转 `engineering`。
|
|
39
39
|
- 配置阶段不隐式下载依赖;测试框架由项目显式选择固定版本、带哈希的 `FetchContent`,或使用项目自带模块。
|
|
40
40
|
|
|
@@ -47,6 +47,7 @@ cmake -S . -B build/clang -G Ninja \
|
|
|
47
47
|
-DCMAKE_CXX_COMPILER=clang++ \
|
|
48
48
|
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
|
|
49
49
|
cmake --build build/clang
|
|
50
|
+
# 只有构建目录不是 build/ 时才建链接;单一构建目录时跳过这两行
|
|
50
51
|
rm -f build/compile_commands.json
|
|
51
52
|
ln -s clang/compile_commands.json build/compile_commands.json
|
|
52
53
|
```
|
|
@@ -55,7 +56,7 @@ GCC 模式把目录和编译器换成 `build/gcc`、`g++`(C 驱动换成 `gcc`
|
|
|
55
56
|
|
|
56
57
|
## 情况
|
|
57
58
|
|
|
58
|
-
**新工程**:从 `../assets/templates/projects/` 选形态;把 `../assets/templates/projects/common/running-build.sh` 复制到工程根,`chmod +x` 并用 `bash -n`
|
|
59
|
+
**新工程**:从 `../assets/templates/projects/` 选形态;把 `../assets/templates/projects/common/running-build.sh` 复制到工程根,`chmod +x` 并用 `bash -n` 验证语法;在 `.agents/skills/cpp-workflow-project/environment.json` 写出四个字段(`host`、`build_host`、`compiler`、`standard`),取值与组合见 `../references/02-environment.md`,不要替用户选。模板缺 target 时先转 `engineering`,target 齐了再 configure。
|
|
59
60
|
|
|
60
61
|
**已有工程**:先读现有 `CMakeLists.txt`,只核对 target、标准与输出目录;不改与当前任务无关的 target,大改前先让现有构建跑通。
|
|
61
62
|
|
|
@@ -10,14 +10,14 @@
|
|
|
10
10
|
- 只复制缺失的文件。已有配置先读再改,不覆盖;需要合并时逐项说明理由。
|
|
11
11
|
- 工具角色固定:`clang-format` 管排版,`clang-tidy` 管静态诊断,`clangd` 管补全/跳转/诊断;无论 `compiler` 是 clang 还是 gcc,这三件都用 Clang 工具链。
|
|
12
12
|
- 版本要求见 `../references/02-environment.md`:Clang 模式要求 `clang++`、`clang-format`、`clang-tidy`、`clangd` 同一 LLVM 主版本;GCC 模式要求 `clang-format` / `clang-tidy` ≥ 16,`clangd` 只要求可用。
|
|
13
|
-
- `.clangd` 的 `CompilationDatabase` 指向 `build/<compiler
|
|
13
|
+
- `.clangd` 的 `CompilationDatabase` 指向 `build/<compiler>`;切换编译器时同步改,不能留旧目录。项目技能声明单一构建目录时指向该目录。**不要写 `-std`**:它会覆盖 `compile_commands.json`,把 C++23 工程降成旧标准;标准只由构建声明和编译数据库决定。
|
|
14
14
|
- `--query-driver` 是 clangd 的命令行参数,写在 `.vscode/settings.json` 的 `clangd.arguments` 里,不是 `.clangd` 的 YAML 键。它告诉 clangd 允许执行哪个编译器驱动来取系统头路径;路径写绝对路径,并与 `environment.json` 的编译器一致。
|
|
15
|
-
- 首次复制时按轴替换三处:`compiler=clang` 用 `CompilationDatabase: build/clang`、`--query-driver=/usr/bin/clang++` 和 `cmake.debugConfig` 的 `MIMode: lldb` / `miDebuggerPath: /usr/bin/lldb`;`compiler=gcc` 用 `build/gcc
|
|
16
|
-
- `.clang-format` 保留 `Language: Cpp`、`BasedOnStyle: Google`、`Standard` 和 `LineEnding: LF`:模板资产写 `Latest`;工程按 `environment.json` 声明 C++11–C++20 时,把 `Standard` 改成 `c++NN`(映射见 `../../engineering/references/cmake/standards/index.md`)。`Standard` 不接受 `c++23` 的旧工具会报 `unknown enumerated scalar`;C++23 工程统一写 `Latest`,由工具取它支持的最高标准。缩进与列宽默认
|
|
17
|
-
- `.clang-tidy` 用 `Checks: '-*, google-*,
|
|
15
|
+
- 首次复制时按轴替换三处:`compiler=clang` 用 `CompilationDatabase: build/clang`、`--query-driver=/usr/bin/clang++` 和 `cmake.debugConfig` 的 `MIMode: lldb` / `miDebuggerPath: /usr/bin/lldb`;`compiler=gcc` 用 `build/gcc`、真实 GCC 驱动的绝对路径(macOS 用 Homebrew gcc;`/usr/bin/g++` 是 Apple clang 别名)和 `MIMode: gdb` / `/usr/bin/gdb`。只复制模板而不改这三处,GCC 模式会指向错误的数据库、驱动或调试器。原工程已有的 `.clangd` 不覆盖;要改先征得同意。
|
|
16
|
+
- `.clang-format` 保留 `Language: Cpp`、`BasedOnStyle: Google`、`Standard` 和 `LineEnding: LF`:模板资产写 `Latest`;工程按 `environment.json` 声明 C++11–C++20 时,把 `Standard` 改成 `c++NN`(映射见 `../../engineering/references/cmake/standards/index.md`)。`Standard` 不接受 `c++23` 的旧工具会报 `unknown enumerated scalar`;C++23 工程统一写 `Latest`,由工具取它支持的最高标准。缩进与列宽默认 4 个空格、120 列,模板资产显式写 `UseTab: Never`、`TabWidth: 4`、`IndentWidth: 4`、`ContinuationIndentWidth: 8`、`AccessModifierOffset: -4` 与 `ColumnLimit: 120`;工程有自己配置时按工程值执行。包含分块保持 include 现有空行分组,组内顺序交给 Google 预设。需要新规则时先改 `../../developing-cpp/references/style/01-google-cpp-style.md` 并说明理由。
|
|
17
|
+
- `.clang-tidy` 用 `Checks: '-*, google-*, readability-identifier-naming'`,保留 `WarningsAsErrors: '*'` 和 `FormatStyle: file`。`google-*` 覆盖 Google 风格能静态检查的部分;`readability-identifier-naming` 只配项目确实统一的命名项:类型/枚举/别名/模板参数 PascalCase,变量/参数/成员 snake_case,类成员尾随 `_`,结构体成员不尾随,常量 `k` + PascalCase。不要设 `FunctionCase`,Google 允许 `count()` / `set_count()` 这类访问器;不要打开 `portability-avoid-pragma-once`,工程统一用 `#pragma once`,打开会把每个头文件报成问题;不要重新打开 `cppcoreguidelines-*` 整包。
|
|
18
18
|
- `.clang-tidy` 关闭项和例外必须写理由;不要借 `-*` 之后再打开 `cppcoreguidelines-*` 这类非 Google 整族检查。
|
|
19
19
|
- `.vscode/extensions.json` 推荐 clangd、CMake Tools 与 C/C++ 扩展;`.vscode/settings.json` 固定 clangd 参数、CMake 构建目录和 `cmake.debugConfig`。配置、构建、运行和调试只从 CMake Tools 进入,不写 `tasks.json` 或 `launch.json`;C/C++ 扩展只作为 CMake 快速调试的后端,IntelliSense 关闭,避免和 clangd 并行。配置注释用 JSONC 的 `//`,贴在被解释的键上方。
|
|
20
|
-
-
|
|
20
|
+
- 配置文件的键注释分两类:运行时配置(如 `config.toml`)用「对象:条件时,后果」,写清比较对象与执行者,不写含糊代词、不复述键名,也不写 `sed`/CI 这类实现细节,正例见 `../../engineering/references/formatting/examples/04-config-comments.md`;固定值配置(`.clang-format`、`.clang-tidy`、`.clangd`、`.vscode/`)按 `../../developing-cpp/references/comments/02-style.md` 写。两类都不写策略说明段,也不指向文档路径;标题只在需要交代文件整体用途时写一行。
|
|
21
21
|
- 格式化与静态检查只扫描项目源码:`src/`、`include/`、`app/`、`tests/` 等实际存在的源码根;不扫描 `build/`、第三方目录、生成文件。
|
|
22
22
|
- `clang-tidy` 用 `-p build/<compiler>` 读取编译数据库,`--header-filter` 只列项目根下的顶层目录,避免把第三方头纳入;不要加 `--system-headers`,也不要用 clang-tidy 19 才加入、16 没有的 `--exclude-header-filter`。
|
|
23
23
|
- 格式化用 `--dry-run --Werror` 做检查;需要自动修复时单独运行 `-i`,不要让构建脚本悄悄改源码。`clang-format` 只排版,不会把行尾注释搬到上一行;数据成员注释位置按 `../../developing-cpp/references/comments/01-comment-rules.md` 检查。
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
|
|
26
26
|
## 编辑器接线
|
|
27
27
|
|
|
28
|
-
-
|
|
28
|
+
- 只在工程根(当前目录向上到含 `CMakeLists.txt` 的目录)已经有 `.vscode/` 时读 `04-vscode.md`;没有 `.vscode/` 时不打开,也不新建目录。
|
|
29
29
|
- 顺序:先定 `.clang-format`、`.clang-tidy`、`.clangd`,再对照现有 `.vscode/extensions.json` 与 `settings.json` 只补缺、只改与 `environment.json` 冲突的项。
|
|
30
30
|
- 一次只读 `04-vscode.md`;扩展与设置不要同时展开。工具条文一次只开 `../../engineering/references/formatting/` 里被点名的一份,其余留到下一次激活。
|
|
31
31
|
- 不写 `tasks.json` 或 `launch.json`,构建与运行仍走 `running-build.sh`。
|
|
@@ -8,13 +8,15 @@
|
|
|
8
8
|
|
|
9
9
|
按固定顺序跑,前一步失败就停,不要带着失败继续:
|
|
10
10
|
|
|
11
|
-
1. **校验编码与行尾**:在仓库根跑编码检查。默认 UTF-8 无 BOM、LF
|
|
12
|
-
2. **校验环境声明(不带 `--live`)**:读 `environment.json
|
|
11
|
+
1. **校验编码与行尾**:在仓库根跑编码检查。默认 UTF-8 无 BOM、LF;不是本工作流构建的 MSVC 源码库加 `--profile msvc`(本工作流拒绝 `windows + native`)。有问题先按 `../../engineering/references/formatting/01-encoding.md` 修正,不带着 BOM 或混用行尾继续;脚本退出码为 0 后再跑 `git diff --check`,有空白错误同样停在这里。
|
|
12
|
+
2. **校验环境声明(不带 `--live`)**:读 `environment.json`,按 `../references/02-environment.md`「合法组合」的规则判定;非法时停在这里,不找可执行文件。
|
|
13
13
|
3. **活动校验**:在声明的 build host 上跑 `--live`,核对 PATH 中的角色和版本。`windows` + `native` 在声明校验阶段就返回失败,不会进入这一步。
|
|
14
|
-
4. **跑项目入口命令**:项目技能里写的入口命令(如 `./running-build.sh clang`)。入口命令仍是 `<entry-command>` 或空白时,停在这里,不要猜命令;是真实命令才跑。入口内部依次 configure → build → ctest → run;每一步失败都保留原始输出。有 `tests/` 目录时,脚本里的 ctest 必须带 `--output-on-failure --no-tests=error`;缺参数时按 `ctest --test-dir build
|
|
15
|
-
5.
|
|
14
|
+
4. **跑项目入口命令**:项目技能里写的入口命令(如 `./running-build.sh clang`)。入口命令仍是 `<entry-command>` 或空白时,停在这里,不要猜命令;是真实命令才跑。入口内部依次 configure → build → ctest → run;每一步失败都保留原始输出。有 `tests/` 目录时,脚本里的 ctest 必须带 `--output-on-failure --no-tests=error`;缺参数时按 `ctest --test-dir <build-dir> --output-on-failure --no-tests=error` 补跑,并以补跑的退出码为准;没有测试目录时按「没有测试」情形处理,不补 `--no-tests=error`。入口通过后,再对项目源码跑 `clang-format --dry-run --Werror <源码根>/*.cpp <源码根>/*.h`(只列工程实际存在的源码根,目录不存在就不写这一项)与 `clang-tidy -p <build-dir> --header-filter="^$(pwd -P)/(include|src|app|tests|greeting)/" --warnings-as-errors='*' <源码根>/*.cpp`(clang-tidy 必须显式列出源文件,否则报 no input files;模板的 `greeting/` 也要覆盖);任一非零即失败,命令细节见 `../../engineering/references/formatting/02-clang-format.md` 与 `03-clang-tidy.md`。
|
|
15
|
+
5. **核对产物**:`<build-dir>/compile_commands.json` 存在(默认 `build/<compiler>`,项目声明单一目录时用该目录);没有声明单一目录时,`build/compile_commands.json` 还要指向当前数据库;入口 target(模板默认 `app`,本书是 `terminal`)的可执行文件在 `<build-dir>/bin`。对它跑 `file` 与 `ldd`(macOS 用 `otool -L`),`ldd` 不得出现 `not found`,并做一次最小启动;见 `../../engineering/references/cmake/linking/04-inspect.md`。
|
|
16
16
|
6. **记录结果**:通过时记录编译器版本、构建目录、测试数量;失败时记录命令、退出码、第一条根因诊断。
|
|
17
17
|
|
|
18
|
+
检查命令只写终端输出,不落盘:不要用重定向在仓库根生成 `temp-enc.log` 这类文件。需要留证据时写已忽略的 `temp/`,没有就只引用终端输出。收尾核 `git status --porcelain`,只应剩下本轮有意改动的文件。
|
|
19
|
+
|
|
18
20
|
编码检查与环境校验脚本(`<skill-dir>` 换成插件里 `building-cpp` 的实际目录):
|
|
19
21
|
|
|
20
22
|
```bash
|
|
@@ -35,7 +37,7 @@ node "<skill-dir>/scripts/checking-environment.mjs" --project . --live # 活
|
|
|
35
37
|
| clangd 跳转失败、数据库指向旧编译器 | `.clangd` 的 `CompilationDatabase`、`build/compile_commands.json` 链接 | 定工具配置 |
|
|
36
38
|
| 编译错误指向函数体 | 实现 | `developing-cpp` 的定实现;先修语义,再重跑 |
|
|
37
39
|
| CTest 失败 | 测试断言、行为句 | `developing-cpp` 的定行为 / 定实现 |
|
|
38
|
-
|
|
|
40
|
+
| 入口程序启动失败、找不到共享库 | 输出目录、RPATH / 运行时库路径 | `engineering` 的定 target(输出目录与 RPATH) |
|
|
39
41
|
|
|
40
42
|
分诊只定位,不自动改。一次只改一类问题;改完从失败的那一步重跑,只有行为或边界也变了才回到定行为。
|
|
41
43
|
|
|
@@ -58,5 +60,5 @@ node "<skill-dir>/scripts/checking-environment.mjs" --project . --live # 活
|
|
|
58
60
|
## 正反例指针
|
|
59
61
|
|
|
60
62
|
- 环境组合与 `--live` 输出:`../examples/environment.md`。
|
|
61
|
-
-
|
|
63
|
+
- 构建输出的失败分诊:`../references/01-build-script.md`「失败分诊」(按输出定位先看哪一层)。
|
|
62
64
|
- 工具调用细节:`../../engineering/references/formatting/03-clang-tidy.md`。
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# 第 4 步:VS Code 接线
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
只在工程根已经有 `.vscode/` 时读本页;从当前目录向上到工程根(含 `CMakeLists.txt` 的目录)都没有 `.vscode/` 时,不打开本页,也不新建目录;外层仓库根或书根的 `.vscode/` 不算工程配置。新工程从 `../assets/config/.vscode/` 复制时除外。
|
|
4
4
|
|
|
5
5
|
## 三个角色
|
|
6
6
|
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
| --- | --- | --- |
|
|
9
9
|
| `llvm-vs-code-extensions.vscode-clangd` | 补全、跳转、诊断、格式化 | 配置与构建 |
|
|
10
10
|
| `ms-vscode.cmake-tools` | 配置、构建、运行、调试入口 | 语言服务 |
|
|
11
|
-
| `ms-vscode.cpptools` | 只作为 `CMake: Debug`
|
|
11
|
+
| `ms-vscode.cpptools` | 只作为 `CMake: Debug` 的调试后端;这是对 clangd 默认建议的明确例外 | IntelliSense 与格式化(关闭) |
|
|
12
12
|
|
|
13
13
|
不要同时开两套语言服务;C/C++ 扩展的 IntelliSense 必须关闭,否则会和 clangd 抢编译数据库。
|
|
14
14
|
|
|
@@ -24,14 +24,14 @@
|
|
|
24
24
|
| 键 | clang 模式 | gcc 模式 |
|
|
25
25
|
| --- | --- | --- |
|
|
26
26
|
| `cmake.buildDirectory` | `${workspaceFolder}/build/clang` | `${workspaceFolder}/build/gcc` |
|
|
27
|
-
| `clangd.arguments` 的 `--query-driver` | `/usr/bin/clang++` |
|
|
27
|
+
| `clangd.arguments` 的 `--query-driver` | `/usr/bin/clang++` | 真实 GCC 的绝对路径(如 Homebrew `gcc-14`);`/usr/bin/g++` 在 macOS 是 Apple clang 别名,不能用 |
|
|
28
28
|
| `cmake.debugConfig` | `MIMode: lldb`、`miDebuggerPath: /usr/bin/lldb` | `MIMode: gdb`、`miDebuggerPath: /usr/bin/gdb` |
|
|
29
29
|
|
|
30
30
|
只改与 `environment.json` 冲突的键;其余保持工程原值。`.clangd` 的 `CompilationDatabase` 也随编译器改,见 `../../engineering/references/formatting/04-clangd.md`。
|
|
31
31
|
|
|
32
32
|
## 各键分工
|
|
33
33
|
|
|
34
|
-
- `cmake.buildDirectory`:与定构建的 `build/<compiler>` 一致;不要在 CMake Tools
|
|
34
|
+
- `cmake.buildDirectory`:与定构建的 `build/<compiler>` 一致;不要在 CMake Tools 里再选第二个构建目录。项目技能声明单一构建目录时,这里与 `.clangd` 的 `CompilationDatabase` 都指向该目录(如 `games/tictactoe/build`),不再按编译器分子目录。
|
|
35
35
|
- `cmake.generator: Ninja`、`cmake.configureOnOpen: false`、`cmake.buildBeforeRun: true`、`cmake.exportCompileCommandsFile: true`:构建仍由入口脚本和 CMake 决定,编辑器只消费同一份数据库。
|
|
36
36
|
- `clangd.arguments`:`--query-driver` 写绝对路径且只列一个驱动,不用 `*`;`--header-insertion=never` 与 `--background-index` 沿用模板。
|
|
37
37
|
- `cmake.debugConfig`:只改调试后端;`type` 仍是 `cppdbg`。
|