@chengzhao-dev/dsh-cpp-workflow 0.7.7 → 0.7.9

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.
Files changed (82) hide show
  1. package/README.en.md +6 -6
  2. package/README.md +8 -8
  3. package/lib/client.js +7 -3
  4. package/lib/comment-policy.js +7 -3
  5. package/lib/index.js +2 -1
  6. package/lib/workspace.js +17 -3
  7. package/package.json +4 -3
  8. package/skills/building-cpp/SKILL.md +6 -6
  9. package/skills/building-cpp/assets/config/.vscode/settings.json +11 -0
  10. package/skills/building-cpp/assets/templates/projects/common/running-build.sh +13 -6
  11. package/skills/building-cpp/assets/templates/projects/multi/CMakeLists.txt +1 -5
  12. package/skills/building-cpp/assets/templates/projects/shared-library/CMakeLists.txt +1 -5
  13. package/skills/building-cpp/assets/templates/projects/shared-library/greeting/CMakeLists.txt +0 -7
  14. package/skills/building-cpp/assets/templates/projects/single/CMakeLists.txt +1 -5
  15. package/skills/building-cpp/assets/templates/projects/static-library/CMakeLists.txt +1 -5
  16. package/skills/building-cpp/assets/templates/projects/static-library/greeting/CMakeLists.txt +0 -7
  17. package/skills/building-cpp/references/01-build-script.md +8 -23
  18. package/skills/building-cpp/references/02-environment.md +5 -4
  19. package/skills/building-cpp/scripts/checking-environment.mjs +45 -13
  20. package/skills/building-cpp/steps/01-build.md +5 -4
  21. package/skills/building-cpp/steps/02-tooling.md +7 -7
  22. package/skills/building-cpp/steps/03-check.md +6 -6
  23. package/skills/building-cpp/steps/04-vscode.md +3 -3
  24. package/skills/developing-cpp/SKILL.md +4 -5
  25. package/skills/developing-cpp/references/comments/01-comment-rules.md +13 -12
  26. package/skills/developing-cpp/references/comments/02-style.md +21 -8
  27. package/skills/developing-cpp/references/comments/examples/01-declaration-class.md +4 -4
  28. package/skills/developing-cpp/references/comments/examples/04-declaration-file.md +5 -3
  29. package/skills/developing-cpp/references/comments/examples/05-declaration-function.md +1 -1
  30. package/skills/developing-cpp/references/comments/glossary/01-comment-terms.md +7 -7
  31. package/skills/developing-cpp/references/design/01-project-design.md +5 -5
  32. package/skills/developing-cpp/references/design/examples/07-preview-read.md +1 -1
  33. package/skills/developing-cpp/references/design/session-layer.md +4 -10
  34. package/skills/developing-cpp/references/exceptions/01-exception-rules.md +2 -2
  35. package/skills/developing-cpp/references/exceptions/examples/05-throw-format.md +1 -1
  36. package/skills/developing-cpp/references/index.md +13 -12
  37. package/skills/developing-cpp/references/language/glossary/01-language-terms.md +2 -2
  38. package/skills/developing-cpp/references/language/language-basics/02-initialization.md +2 -2
  39. package/skills/developing-cpp/references/language/language-basics/05-expressions.md +2 -2
  40. package/skills/developing-cpp/references/language/language-basics/06-functions.md +5 -4
  41. package/skills/developing-cpp/references/language/language-basics/07-classes.md +2 -2
  42. package/skills/developing-cpp/references/language/performance/02-complexity.md +2 -3
  43. package/skills/developing-cpp/references/language/performance/03-memory.md +1 -1
  44. package/skills/developing-cpp/references/language/performance/05-pitfalls.md +1 -1
  45. package/skills/developing-cpp/references/language/standard-library/04-algorithms.md +1 -1
  46. package/skills/developing-cpp/references/language/standard-library/06-format.md +1 -31
  47. package/skills/developing-cpp/references/language/standard-library/10-library-checklist.md +1 -1
  48. package/skills/developing-cpp/references/language/standard-library/11-format-ecosystem.md +32 -0
  49. package/skills/developing-cpp/references/language/value-types/02-signed.md +2 -2
  50. package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +3 -2
  51. package/skills/developing-cpp/references/language/value-types/04-enum.md +5 -5
  52. package/skills/developing-cpp/references/layout/01-source-layout.md +2 -2
  53. package/skills/developing-cpp/references/layout/02-file-template.md +10 -9
  54. package/skills/developing-cpp/references/layout/03-declaration-order.md +9 -8
  55. package/skills/developing-cpp/references/naming/01-identifier-names.md +8 -8
  56. package/skills/developing-cpp/references/naming/examples/13-naming-state-change.md +1 -1
  57. package/skills/developing-cpp/references/naming/glossary/04-square.md +1 -1
  58. package/skills/developing-cpp/references/style/01-google-cpp-style.md +9 -9
  59. package/skills/developing-cpp/scripts/checking-comments.mjs +26 -10
  60. package/skills/developing-cpp/scripts/checking-declaration-order.mjs +81 -10
  61. package/skills/developing-cpp/steps/01-behavior.md +1 -1
  62. package/skills/developing-cpp/steps/02-boundary.md +2 -2
  63. package/skills/developing-cpp/steps/03-declaration.md +6 -6
  64. package/skills/developing-cpp/steps/04-implementation.md +6 -6
  65. package/skills/engineering/SKILL.md +3 -2
  66. package/skills/engineering/references/cmake/assets/OutputSettings.cmake +3 -3
  67. package/skills/engineering/references/cmake/modules/01-functions.md +10 -9
  68. package/skills/engineering/references/cmake/standards/index.md +6 -6
  69. package/skills/engineering/references/cmake/standards/versions.md +5 -5
  70. package/skills/engineering/references/cmake/targets/01-target-rules.md +10 -18
  71. package/skills/engineering/references/formatting/01-encoding.md +9 -7
  72. package/skills/engineering/references/formatting/02-clang-format.md +3 -3
  73. package/skills/engineering/references/formatting/04-clangd.md +2 -2
  74. package/skills/engineering/references/formatting/examples/04-config-comments.md +70 -0
  75. package/skills/engineering/references/index.md +4 -3
  76. package/skills/engineering/scripts/checking-encoding.mjs +272 -6
  77. package/skills/engineering/steps/01-targets.md +3 -11
  78. package/skills/engineering/steps/02-modules.md +2 -8
  79. package/skills/engineering/steps/03-check.md +7 -14
  80. package/skills/shared/operations.md +1 -1
  81. package/skills/shared/rule-priority.md +1 -1
  82. package/templates/cpp-workflow-project/SKILL.md +8 -4
@@ -5,6 +5,10 @@
5
5
  // profile "msvc" 只对 MSVC 编译的 C/C++ 源放宽为允许 UTF-8 BOM 与 CRLF;
6
6
  // Markdown、CMake、脚本与配置仍按 portable 检查。从不改写文件。
7
7
  //
8
+ // 检查前按文件的真实路径读取逐级 .gitattributes(text / binary / eol /
9
+ // working-tree-encoding)与逐级向上的 .editorconfig(charset / end_of_line /
10
+ // insert_final_newline),两者都没有声明时才用上面的默认组合。
11
+ //
8
12
  // 用法:
9
13
  // node checking-encoding.mjs [--root DIR] [--profile portable|msvc] [--json] [--quiet]
10
14
  //
@@ -33,6 +37,9 @@ const ALWAYS_LF = new Set([
33
37
  '.svg', '.cmake', '.lua', '.sql', '.gitignore', '.gitattributes', '.editorconfig',
34
38
  ])
35
39
  const NO_EXTENSION_LF = new Set(['CMakeLists.txt', 'Makefile', 'Justfile', 'Dockerfile', 'LICENSE'])
40
+ // .editorconfig 里不是 UTF-8 的 charset:工作树编码由别的工具负责,本脚本不判。
41
+ const NON_UTF8_CHARSETS = new Set(['latin1', 'utf-16be', 'utf-16le'])
42
+ const EOL_VALUES = new Set(['lf', 'crlf', 'cr'])
36
43
 
37
44
  function usage(message) {
38
45
  if (message) process.stderr.write(`错误:${message}\n`)
@@ -116,14 +123,253 @@ function isBinary(buffer) {
116
123
  return false
117
124
  }
118
125
 
119
- function inspect(file, root, profile) {
126
+ function readText(file) {
127
+ try {
128
+ return readFileSync(file, 'utf8')
129
+ } catch {
130
+ return undefined
131
+ }
132
+ }
133
+
134
+ function escapeRegExp(text) {
135
+ return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
136
+ }
137
+
138
+ // 把 .gitattributes / .editorconfig 的 glob 片段翻成正则源码。
139
+ // * 与 ? 不跨目录,** 跨目录,[...] 是字符类,反斜杠转义下一个字符。
140
+ function globSource(pattern) {
141
+ let source = ''
142
+ for (let index = 0; index < pattern.length; index += 1) {
143
+ const char = pattern[index]
144
+ if (char === '\\' && index + 1 < pattern.length) {
145
+ index += 1
146
+ source += escapeRegExp(pattern[index])
147
+ } else if (char === '*') {
148
+ if (pattern[index + 1] === '*') {
149
+ index += 1
150
+ if (pattern[index + 1] === '/') {
151
+ index += 1
152
+ source += '(?:.*/)?'
153
+ } else {
154
+ source += '.*'
155
+ }
156
+ } else {
157
+ source += '[^/]*'
158
+ }
159
+ } else if (char === '?') {
160
+ source += '[^/]'
161
+ } else if (char === '[') {
162
+ let close = index + 1
163
+ if (pattern[close] === '!' || pattern[close] === '^') close += 1
164
+ if (pattern[close] === ']') close += 1
165
+ while (close < pattern.length && pattern[close] !== ']') close += 1
166
+ if (close >= pattern.length) {
167
+ source += '\\['
168
+ } else {
169
+ let body = pattern.slice(index + 1, close)
170
+ if (body.startsWith('!')) body = `^${body.slice(1)}`
171
+ source += `[${body.replace(/\\/g, '\\\\')}]`
172
+ index = close
173
+ }
174
+ } else {
175
+ source += escapeRegExp(char)
176
+ }
177
+ }
178
+ return source
179
+ }
180
+
181
+ function splitBraces(body) {
182
+ const parts = []
183
+ let depth = 0
184
+ let start = 0
185
+ for (let index = 0; index < body.length; index += 1) {
186
+ const char = body[index]
187
+ if (char === '{') depth += 1
188
+ else if (char === '}') depth -= 1
189
+ else if (char === ',' && depth === 0) {
190
+ parts.push(body.slice(start, index))
191
+ start = index + 1
192
+ }
193
+ }
194
+ parts.push(body.slice(start))
195
+ return parts
196
+ }
197
+
198
+ // EditorConfig 的 {a,b} 写法;展开后每条再翻正则。
199
+ function expandBraces(pattern) {
200
+ const open = pattern.indexOf('{')
201
+ if (open === -1) return [pattern]
202
+ let depth = 0
203
+ let close = -1
204
+ for (let index = open; index < pattern.length; index += 1) {
205
+ if (pattern[index] === '{') depth += 1
206
+ else if (pattern[index] === '}') {
207
+ depth -= 1
208
+ if (depth === 0) {
209
+ close = index
210
+ break
211
+ }
212
+ }
213
+ }
214
+ if (close === -1) return [pattern]
215
+ const before = pattern.slice(0, open)
216
+ const after = pattern.slice(close + 1)
217
+ const expanded = []
218
+ for (const part of splitBraces(pattern.slice(open + 1, close))) {
219
+ expanded.push(...expandBraces(before + part + after))
220
+ }
221
+ return expanded
222
+ }
223
+
224
+ function compilePattern(pattern) {
225
+ const anchored = pattern.includes('/')
226
+ const body = pattern.startsWith('/') ? pattern.slice(1) : pattern
227
+ return { anchored, regex: new RegExp(`^${globSource(body)}$`) }
228
+ }
229
+
230
+ function compilePatterns(pattern) {
231
+ return expandBraces(pattern).map(compilePattern)
232
+ }
233
+
234
+ function matchesAny(compiled, relativePath) {
235
+ const name = path.posix.basename(relativePath)
236
+ return compiled.some((item) => item.regex.test(item.anchored ? relativePath : name))
237
+ }
238
+
239
+ function parseAttributes(text) {
240
+ const rules = []
241
+ for (const raw of text.split(/\r?\n/)) {
242
+ const line = raw.trim()
243
+ if (!line || line.startsWith('#') || line.startsWith('[attr]')) continue
244
+ const [pattern, ...pairs] = line.split(/\s+/)
245
+ if (!pattern || pattern.startsWith('!')) continue
246
+ const attributes = new Map()
247
+ for (const pair of pairs) {
248
+ if (pair.startsWith('-')) {
249
+ attributes.set(pair.slice(1).toLowerCase(), 'false')
250
+ } else {
251
+ const equals = pair.indexOf('=')
252
+ if (equals === -1) {
253
+ attributes.set(pair.toLowerCase(), 'true')
254
+ } else {
255
+ attributes.set(pair.slice(0, equals).toLowerCase(), pair.slice(equals + 1).toLowerCase())
256
+ }
257
+ }
258
+ }
259
+ if (attributes.has('binary')) attributes.set('text', 'false')
260
+ rules.push({ pattern, compiled: compilePatterns(pattern), attributes })
261
+ }
262
+ return rules
263
+ }
264
+
265
+ // 逐级读取 .gitattributes:根目录先应用,子目录的规则后覆盖。
266
+ function attributesFor(root, relative, cache) {
267
+ const directories = []
268
+ let directory = path.posix.dirname(relative)
269
+ while (true) {
270
+ directories.push(directory === '.' ? '' : directory)
271
+ if (directory === '.' || directory === '') break
272
+ directory = path.posix.dirname(directory)
273
+ }
274
+ directories.reverse()
275
+
276
+ const result = new Map()
277
+ for (const prefix of directories) {
278
+ const file = path.join(root, prefix, '.gitattributes')
279
+ let rules = cache.get(file)
280
+ if (rules === undefined) {
281
+ const text = readText(file)
282
+ rules = text === undefined ? [] : parseAttributes(text)
283
+ cache.set(file, rules)
284
+ }
285
+ const within = relative.slice(prefix ? prefix.length + 1 : 0)
286
+ for (const rule of rules) {
287
+ if (!matchesAny(rule.compiled, within)) continue
288
+ for (const [name, value] of rule.attributes) result.set(name, value)
289
+ }
290
+ }
291
+ return result
292
+ }
293
+
294
+ function parseEditorConfig(text) {
295
+ const sections = []
296
+ let root = false
297
+ let current
298
+ for (const raw of text.split(/\r?\n/)) {
299
+ const line = raw.trim()
300
+ if (!line || line.startsWith('#') || line.startsWith(';')) continue
301
+ if (line.startsWith('[') && line.endsWith(']')) {
302
+ current = { compiled: compilePatterns(line.slice(1, -1)), properties: new Map() }
303
+ sections.push(current)
304
+ continue
305
+ }
306
+ const equals = line.indexOf('=')
307
+ if (equals === -1) continue
308
+ const key = line.slice(0, equals).trim().toLowerCase()
309
+ const value = line.slice(equals + 1).trim().toLowerCase()
310
+ if (!current) {
311
+ if (key === 'root' && value === 'true') root = true
312
+ continue
313
+ }
314
+ current.properties.set(key, value)
315
+ }
316
+ return { root, sections }
317
+ }
318
+
319
+ // 从文件所在目录向上找 .editorconfig,遇到 root = true 停止;
320
+ // 远的先应用,近的后覆盖,同一文件内后面的节覆盖前面的节。
321
+ function editorConfigFor(root, relative, cache) {
322
+ const chain = []
323
+ let directory = path.posix.dirname(relative)
324
+ while (true) {
325
+ const prefix = directory === '.' ? '' : directory
326
+ const file = path.join(root, prefix, '.editorconfig')
327
+ let config = cache.get(file)
328
+ if (config === undefined) {
329
+ const text = readText(file)
330
+ config = text === undefined ? null : parseEditorConfig(text)
331
+ cache.set(file, config)
332
+ }
333
+ if (config) {
334
+ chain.push({ prefix, config })
335
+ if (config.root) break
336
+ }
337
+ if (directory === '.' || directory === '') break
338
+ directory = path.posix.dirname(directory)
339
+ }
340
+ chain.reverse()
341
+
342
+ const properties = new Map()
343
+ for (const { prefix, config } of chain) {
344
+ const within = relative.slice(prefix ? prefix.length + 1 : 0)
345
+ for (const section of config.sections) {
346
+ if (!matchesAny(section.compiled, within)) continue
347
+ for (const [key, value] of section.properties) properties.set(key, value)
348
+ }
349
+ }
350
+ return properties
351
+ }
352
+
353
+ function configuredLineEnding(editor, attributes) {
354
+ const editorEol = editor.get('end_of_line')
355
+ if (EOL_VALUES.has(editorEol)) return editorEol
356
+ const attributeEol = attributes.get('eol')
357
+ if (EOL_VALUES.has(attributeEol)) return attributeEol
358
+ return undefined
359
+ }
360
+
361
+ function inspect(file, root, profile, caches) {
120
362
  const rel = path.relative(root, file).split(path.sep).join('/')
121
- const extension = path.extname(file).toLowerCase()
363
+ const base = path.basename(file).toLowerCase()
364
+ const extension = base.startsWith('.') ? base : path.extname(file).toLowerCase()
122
365
  const source = MSVC_SOURCE.has(extension)
123
366
  const alwaysLf = ALWAYS_LF.has(extension) || NO_EXTENSION_LF.has(path.basename(file))
124
367
  const issues = []
125
368
  const add = (kind, message) => issues.push({ path: rel, kind, message })
126
369
 
370
+ const attributes = attributesFor(root, rel, caches.gitAttributes)
371
+ const editor = editorConfigFor(root, rel, caches.editorConfig)
372
+
127
373
  let buffer
128
374
  try {
129
375
  buffer = readFileSync(file)
@@ -132,9 +378,21 @@ function inspect(file, root, profile) {
132
378
  return { rel, issues }
133
379
  }
134
380
 
381
+ if (attributes.get('binary') === 'true' || attributes.get('text') === 'false') {
382
+ return { rel, issues }
383
+ }
384
+
385
+ const charset = editor.get('charset') ?? 'utf-8'
386
+ const workingTreeEncoding = attributes.get('working-tree-encoding')
387
+ const transcoded = workingTreeEncoding !== undefined && !/^utf-?8$/i.test(workingTreeEncoding)
388
+ if (transcoded || NON_UTF8_CHARSETS.has(charset)) {
389
+ return { rel, issues }
390
+ }
391
+
135
392
  const bom = bomOf(buffer)
393
+ const bomAllowed = charset === 'utf-8-bom' || (profile === 'msvc' && source)
136
394
  if (bom === 'UTF-8') {
137
- if (profile !== 'msvc' || !source) {
395
+ if (!bomAllowed) {
138
396
  add('bom', profile === 'msvc' && !source ? '只有 MSVC 编译的 C/C++ 源码可以带 BOM' : '不应带 BOM')
139
397
  }
140
398
  } else if (bom) {
@@ -161,7 +419,7 @@ function inspect(file, root, profile) {
161
419
  add('bom', '文件中间含 U+FEFF')
162
420
  }
163
421
 
164
- if (buffer.length > 0 && !body.endsWith('\n')) {
422
+ if (buffer.length > 0 && !body.endsWith('\n') && editor.get('insert_final_newline') !== 'false') {
165
423
  add('newline', '缺少末尾换行')
166
424
  }
167
425
 
@@ -169,10 +427,17 @@ function inspect(file, root, profile) {
169
427
  const lf = (body.match(/(?<!\r)\n/g) ?? []).length
170
428
  const cr = (body.match(/\r(?!\n)/g) ?? []).length
171
429
 
172
- if (CRLF_REQUIRED.has(extension)) {
430
+ const expected = configuredLineEnding(editor, attributes)
431
+ if (expected === 'crlf') {
173
432
  if (crlf === 0 || lf > 0 || cr > 0) add('eol', '应为 CRLF')
433
+ } else if (expected === 'cr') {
434
+ if (cr === 0 || lf > 0 || crlf > 0) add('eol', '应为 CR')
435
+ } else if (expected === 'lf') {
436
+ if (crlf > 0 || cr > 0) add('eol', '应为 LF')
174
437
  } else if (profile === 'msvc' && source && !alwaysLf) {
175
438
  if ((crlf > 0 && lf > 0) || cr > 0) add('eol', '不要混用 CRLF 与 LF')
439
+ } else if (CRLF_REQUIRED.has(extension)) {
440
+ if (crlf === 0 || lf > 0 || cr > 0) add('eol', '应为 CRLF')
176
441
  } else if (crlf > 0 || cr > 0) {
177
442
  add('eol', '应为 LF')
178
443
  }
@@ -182,9 +447,10 @@ function inspect(file, root, profile) {
182
447
 
183
448
  export function checkEncoding(root, profile = 'portable') {
184
449
  const files = listFiles(root)
450
+ const caches = { gitAttributes: new Map(), editorConfig: new Map() }
185
451
  const results = []
186
452
  for (const file of files) {
187
- results.push(inspect(file, root, profile))
453
+ results.push(inspect(file, root, profile, caches))
188
454
  }
189
455
  const issues = results.flatMap((result) => result.issues)
190
456
  const counts = { files: results.length, issues: issues.length, bom: 0, eol: 0, utf8: 0, newline: 0, read: 0 }
@@ -18,7 +18,7 @@
18
18
 
19
19
  target 规则:
20
20
 
21
- - 程序 target 用项目技能点名的名字(模板默认 `app`,本书 `terminal`);库 target 用组件名,程序链接库;不把 `main()` 编进库。
21
+ - 程序 target 用项目技能点名的名字(模板默认 `app`);库 target 用组件名,程序链接库;不把 `main()` 编进库。
22
22
  - 每个 target 自己声明 include、标准、编译选项和链接库;公共头用 `PUBLIC`,实现目录用 `PRIVATE`。
23
23
  - 用 `target_include_directories`、`target_compile_features`、`target_link_libraries`;禁止全局 `include_directories()`、`add_definitions()`、`CMAKE_CXX_FLAGS`。target 内的命令按 `../references/cmake/targets/01-target-rules.md` 的块顺序写,标准与警告放在链接之前。
24
24
  - 源文件显式列出;不写 `file(GLOB ...)`,新增文件时改 CMake。
@@ -29,19 +29,11 @@ target 规则:
29
29
 
30
30
  ## 命令形状
31
31
 
32
- ```bash
33
- cmake -S . -B build/clang -G Ninja \
34
- -DCMAKE_BUILD_TYPE=Debug \
35
- -DCMAKE_C_COMPILER=clang \
36
- -DCMAKE_CXX_COMPILER=clang++ \
37
- -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
38
- ```
39
-
40
- GCC 模式把目录和编译器换成 `build/gcc`、`g++`(C 驱动换成 `gcc`)。一个 configure 只用一个编译器,C 与 C++ 驱动同属一套。模板的 `cmake_minimum_required` 默认 3.20;目标标准要求更低时可以下调,但不能低于上表对该标准的下限,不要静默降标准。
32
+ configure 的目录与编译器写法以 `building-cpp` 的 `steps/01-build.md`「命令」为唯一出处(`build/clang` 或 `build/gcc`、`clang`/`clang++` 或 `gcc`/`g++`、`CMAKE_EXPORT_COMPILE_COMMANDS=ON`)。一个 configure 只用一个编译器,C 与 C++ 驱动同属一套。模板的 `cmake_minimum_required` 默认 3.20;目标标准要求更低时可以下调,但不能低于上表对该标准的下限,不要静默降标准。
41
33
 
42
34
  ## 情况
43
35
 
44
- **新工程**:工程模板仍由 `building-cpp` 提供,从 `../../building-cpp/assets/templates/projects/` 选形态,只复制目标工程缺的文件;模板里的三属性与 `-Wall -Wextra -Werror` 只是可运行的最小形状,接管时按本节换成 `set_target_cxx_standard`;需要警告时再加 `set_target_warnings`;模板里的 `{{PROJECT_NAME}}` 换成真实工程名,再按本节改 target 与标准。
36
+ **新工程**:工程模板仍由 `building-cpp` 提供,从 `../../building-cpp/assets/templates/projects/` 选形态,只复制目标工程缺的文件;模板里的三属性只是可运行的最小形状,接管时按本节换成 `set_target_cxx_standard`;警告与 `-Werror` 由 `set_target_warnings` 按需打开,模板不默认加;模板里的 `{{PROJECT_NAME}}` 换成真实工程名,再按本节改 target 与标准。
45
37
 
46
38
  **已有工程**:先读现有 `CMakeLists.txt`,找出全局设置、GLOB、输出目录和 target 命名;只改与当前任务相关的一处。大改前先让现有构建跑通,保留可回退点。
47
39
 
@@ -14,18 +14,12 @@
14
14
 
15
15
  ## 文件与命名
16
16
 
17
+ - 文件名用 UpperCamelCase、函数名用 snake_case;命名细则、模块划分与 `include_guard(GLOBAL)` 写法见 `../references/cmake/modules/01-functions.md`「文件布局」。
17
18
  - 一个文件放一组语义相近的函数,不为每个函数建单独文件;文件过大或出现无关职责时再拆。
18
- - 文件名用 UpperCamelCase,按职责命名,例如 `CompileSettings.cmake`、`LinkSettings.cmake`。
19
- - 函数名用 snake_case,按“动词 + 对象”直接描述动作,例如
20
- `set_target_cxx_standard`、`set_target_warnings`、`set_output_directories`;
21
- 不要机械重复模块名。CMake 没有命名空间,函数全局可见;确有重名风险时
22
- 再加短项目前缀,而不是完整模块名。
23
- - 工程自己新增、不准备回流的函数同样先取动作名;与外部模块可能冲突时再加短项目前缀,例如 `my_project_do_something`。
24
- - 模块开头写 `include_guard(GLOBAL)`,避免重复导入时重定义函数。
25
19
 
26
20
  ## 导入
27
21
 
28
- 根 `CMakeLists.txt` 在 `project()` 之后、`add_subdirectory()` 之前追加一次 `CMAKE_MODULE_PATH`,再 `include()` 需要的模块。子目录只调用函数,不再次导入。
22
+ 根 `CMakeLists.txt` 在 `project()` 之后、`add_subdirectory()` 之前追加一次 `CMAKE_MODULE_PATH`,再 `include()` 需要的模块;子目录只调用函数,不再次导入。
29
23
 
30
24
  ## 契约
31
25
 
@@ -6,27 +6,20 @@
6
6
 
7
7
  ## 命令
8
8
 
9
- ```bash
10
- cmake -S . -B build/clang -G Ninja \
11
- -DCMAKE_BUILD_TYPE=Debug \
12
- -DCMAKE_C_COMPILER=clang \
13
- -DCMAKE_CXX_COMPILER=clang++ \
14
- -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
15
- cmake --build build/clang
16
- ```
17
-
18
- GCC 模式把目录和编译器换成 `build/gcc`、`g++`。每次检查都重新 configure,不依赖上一次缓存;同一构建目录只放同一编译器的配置。
9
+ configure 与构建的完整命令(Ninja、编译器、`CMAKE_EXPORT_COMPILE_COMMANDS` 与编译数据库链接)以 `building-cpp` 的 `steps/01-build.md`「命令」为唯一出处,本步不重复;GCC 模式也按那一处换目录与编译器。每次检查都重新 configure,不依赖上一次缓存;同一构建目录只放同一编译器的配置。
19
10
 
20
11
  ## 核对项
21
12
 
22
- - **target 图**:`app` 链接库,库的公共 include 用 `PUBLIC`;没有把 `main()` 编进库。
23
- - **标准**:读 `build/clang/compile_commands.json` 中每个 target 的 `-std=`;每个 target 只能有一条,必须与 `environment.json` 的 `standard` 一致,不能是 `gnu++NN` 或更高默认标准,不同值即失败。
13
+ 下文 `<构建目录>` 取项目技能声明的单一构建目录(如 `games/<game>/build`),没声明时用 `build/<compiler>`;`<程序 target>` 取项目技能「程序 target」字段,模板默认 `app`。
14
+
15
+ - **target 图**:`<程序 target>` 链接库,库的公共 include 用 `PUBLIC`;没有把 `main()` 编进库。
16
+ - **标准**:读 `<构建目录>/compile_commands.json` 中每个 target 的 `-std=`;每个 target 只能有一条,必须与 `environment.json` 的 `standard` 一致,不能是 `gnu++NN` 或更高默认标准,不同值即失败。
24
17
  - **没有全局污染**:全仓搜索 `include_directories`、`add_definitions`、`CMAKE_CXX_FLAGS`、`file(GLOB`,命中就回第 1 步。
25
18
  - **模块导入**:根 `CMakeLists.txt` 只导入一次;函数名与文件名可追溯;删除旧模块后没有残留 `include()`。
26
19
  - **排版**:`CMakeLists.txt` 与 `*.cmake` 缩进用 4 个空格,行内对齐也用空格,UTF-8 无 BOM、LF;抽查 120 列(中文按 2 列计)以上的行是否有必要。
27
20
  - **文档块**:每个模块文件开头有 `#[=` 文档块与 `.. command::`,块内散文写中文,签名留在 `function()` 那一行。
28
- - **产物位置**:可执行文件在 `build/<compiler>/bin`,库在 `build/<compiler>/lib`;源码目录没有新增构建产物。
29
- - **产物可加载**:对 `build/<compiler>/bin/app` 跑 `file` 与 `ldd`(macOS 用 `otool -L`),不得出现 `not found`;出现缺失按 RPATH 回第 1 步。启动检查由 `building-cpp` 的入口命令负责。见 `../references/cmake/linking/04-inspect.md`。
21
+ - **产物位置**:可执行文件在 `<构建目录>/bin`,库在 `<构建目录>/lib`;源码目录没有新增构建产物。
22
+ - **产物可加载**:对 `<构建目录>/bin/<程序 target>` 跑 `file` 与 `ldd`(macOS 用 `otool -L`),不得出现 `not found`;出现缺失按 RPATH 回第 1 步。启动检查由 `building-cpp` 的入口命令负责。见 `../references/cmake/linking/04-inspect.md`。
30
23
 
31
24
  ## 失败分流
32
25
 
@@ -55,7 +55,7 @@
55
55
  ## 写作边界
56
56
 
57
57
  - 不新增原文没有的事实、数字、命令、性能结论、引文和出处;不把「可能」「通常」「在本书里」改成确定说法;保留否定、条件、范围、时间和归属。
58
- - 不写否定式能力边界:不列「不检查…」「不校验…」「不负责…」「不判断…」清单;前置条件写成「调用方保证…」「仅接受…」或 `@pre`。状态结果「不做修改」「保持不变」是本次调用的结果,不是能力边界。
58
+ - 读者正文与源码注释不写否定式能力边界:不列「不检查…」「不校验…」「不负责…」「不判断…」清单;前置条件写成「调用方保证…」「仅接受…」或 `@pre`。状态结果「不做修改」「保持不变」是本次调用的结果,不是能力边界。三份 `SKILL.md` 的「适用场景」用「不负责」划交付边界,属路由写法,不受此限。
59
59
  - 同一概念只用一个首选词;术语、命令、标识符、路径、URL 与围栏内容不改。
60
60
  - 注释写声明里看不出的事实;实现注释写为什么、边界与失败后果,不复述代码。
61
61
 
@@ -17,7 +17,7 @@
17
17
 
18
18
  规则依据的效力顺序是 ISO C++ 标准、Google C++ Style、Stack Overflow。标准定义语言语义,Google 规定本工作流采用的工程风格,Stack Overflow 只补例子与常见误解;三者冲突时,前面的覆盖后面的。
19
19
 
20
- 已写明的局部偏离高于 Google 对应条款,但不得越过 ISO 标准语义。先读本页和 `02-initialization.md` 这类专项页,再按 Google 原文补齐未覆盖的部分。新增或改写的依据节按 Stack Overflow、Google、ISO 的顺序书写,先给可读解释,再落到权威条文。
20
+ 已写明的局部偏离高于 Google 对应条款,但不得越过 ISO 标准语义。先读本页和 `02-initialization.md` 这类专项页,再按 Google 原文补齐未覆盖的部分。依据节直接列实际采用的来源,不按固定顺序凑三种来源。
21
21
 
22
22
  ## 常见冲突
23
23
 
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: cpp-workflow-project
3
- description: 当前 C++ 工程的本地事实与通用工作流约定:工程与组件名、库和程序的边界、入口命令、阶段表、术语增补。开始任何 C++ 任务前先读;工程特有的值写在这里,通用条文由插件三个技能维护。
3
+ description: 当前 C++ 工程的本地事实与通用工作流约定:工程与组件名、程序 target、构建目录、库和程序的边界、入口命令、阶段表、术语增补。开始任何 C++ 任务前先读;工程特有的值写在这里,通用条文由插件三个技能维护。
4
4
  metadata:
5
5
  short-description: 当前 C++ 工程事实与通用约定
6
6
  ---
@@ -18,6 +18,9 @@ metadata:
18
18
  | 工程名 | `<project-name>` |
19
19
  | 一句话职责 | `<程序用一句话说明做什么>` |
20
20
  | 组件名 | `<component-name>` |
21
+ | 程序 target | `<程序 target 名;没有程序时写“无”,默认 app>` |
22
+ | 构建目录 | `<构建目录;单一目录时写实际路径,如 games/<game>/build,多编译器时写 build/<compiler>>` |
23
+ | 文档检查命令 | `<书稿或文档的检查命令;没有时写“无”>` |
21
24
  | 源码根 | `<src-root>` |
22
25
 
23
26
  同目录的 `environment.json` 三个环境轴必须填本工程的真实组合;`standard` 默认 `c++23`,可改成 `c++11`–`c++23`。环境轴留空时技能仍可能出现,但定构建会停。
@@ -34,12 +37,13 @@ metadata:
34
37
  以下约定不随工程变化;与本工程的真实情况冲突时,在项目技能里写一行偏离和理由。
35
38
 
36
39
  - 环境以 `environment.json` 为唯一来源;新工程默认 C++23,不猜轴、不猜编译器。
37
- - `.clang-format`、`.clang-tidy`、`.clangd`、`.vscode/` 随工程存放,不继承仓库根或用户主目录。
40
+ - `.clang-format`、`.clang-tidy`、`.clangd`、`.vscode/` 随工程存放(工程根=含 `CMakeLists.txt` 的目录);三件 clang 工具会从文件所在目录向上查找配置,工程根必须自带这三份,不依赖仓库根、书根或用户主目录的那份;`.vscode/` 只对打开为工作区的那个目录生效,不开在仓库根或书根。
41
+ - `.editorconfig`、`.gitattributes`、`.gitignore` 与三个 clang 配置不同:它们按目录向上查找,工程根没有自己的副本时用仓库根那份;`.gitignore` 的前导 `/` 锚定到该文件所在目录,仓库根与工程根各自解释,同一路径在两级都忽略时两级都要写。
38
42
  - 库与程序分开 target;程序链接库,终端输入输出留在程序。
39
43
  - CTest 在工程内注册并运行;仓库根不构建、不注册测试。
40
44
  - 头文件用 `#pragma once`,不写路径式包含守卫。
41
45
  - 命名按 Google C++ 风格与工程白名单执行;谓词用 `Is/Has/Can/Completes` 等肯定式。
42
- - 标准由 `cmake/CompileSettings.cmake` 的 `set_target_cxx_standard` 统一施加;工程需要警告时再调用 `set_target_warnings`,不默认给所有 target 加 `-Werror`。`environment.json` 的 `standard` 与 `.clang-format` 同步。
46
+ - 标准由 `cmake/CompileSettings.cmake` 的 `set_target_cxx_standard` 统一施加;该模块不在模板内,需要时从插件资产 `references/cmake/assets/CompileSettings.cmake` 复制到工程根 `cmake/` 并导入。工程需要警告时再调用 `set_target_warnings`,不默认给所有 target 加 `-Werror`。`environment.json` 的 `standard` 与 `.clang-format` 同步。
43
47
  - 每个可独立构建、运行的程序一个工程;共享库等第二个真实使用方出现、语义稳定后再抽。
44
48
  - 阶段表、代码目录与文档章节保持对齐;没有阶段表时只写用户点名的行为。
45
49
 
@@ -64,7 +68,7 @@ metadata:
64
68
 
65
69
  - clangd 提供补全、跳转和诊断;`.clangd` 指向 `build/<compiler>` 的编译数据库,不写 `-std`。
66
70
  - `--query-driver` 放在 `.vscode/settings.json` 的 `clangd.arguments`,不要写成 `.clangd` 的 YAML 键。
67
- - CMake Tools 是唯一的配置、构建、运行和调试入口;C/C++ 扩展只做 `CMake: Debug` 的后端,IntelliSense 关闭。
71
+ - 编辑器内的配置、构建与调试从 CMake Tools 进入,命令行与 CI 一律走入口命令;两者共用同一构建目录;C/C++ 扩展只做 `CMake: Debug` 的后端,IntelliSense 关闭。
68
72
 
69
73
  ## 完成判据
70
74