@chengzhao-dev/dsh-cpp-workflow 0.7.8 → 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.
- package/README.en.md +2 -2
- package/README.md +2 -2
- package/package.json +4 -3
- package/skills/building-cpp/SKILL.md +3 -3
- package/skills/building-cpp/references/01-build-script.md +2 -2
- package/skills/building-cpp/steps/02-tooling.md +6 -6
- package/skills/building-cpp/steps/03-check.md +2 -2
- package/skills/developing-cpp/SKILL.md +1 -1
- package/skills/developing-cpp/references/comments/01-comment-rules.md +8 -7
- package/skills/developing-cpp/references/comments/02-style.md +7 -6
- package/skills/developing-cpp/references/comments/examples/01-declaration-class.md +2 -2
- package/skills/developing-cpp/references/comments/glossary/01-comment-terms.md +2 -2
- package/skills/developing-cpp/references/design/01-project-design.md +4 -4
- package/skills/developing-cpp/references/design/examples/07-preview-read.md +1 -1
- package/skills/developing-cpp/references/design/session-layer.md +4 -10
- package/skills/developing-cpp/references/exceptions/01-exception-rules.md +1 -1
- package/skills/developing-cpp/references/exceptions/examples/05-throw-format.md +1 -1
- package/skills/developing-cpp/references/index.md +3 -3
- package/skills/developing-cpp/references/language/language-basics/06-functions.md +1 -1
- package/skills/developing-cpp/references/language/language-basics/07-classes.md +1 -1
- package/skills/developing-cpp/references/language/standard-library/10-library-checklist.md +1 -1
- package/skills/developing-cpp/references/language/value-types/04-enum.md +1 -1
- package/skills/developing-cpp/references/layout/01-source-layout.md +2 -2
- package/skills/developing-cpp/references/layout/02-file-template.md +2 -2
- package/skills/developing-cpp/references/layout/03-declaration-order.md +4 -4
- package/skills/developing-cpp/references/naming/01-identifier-names.md +6 -6
- package/skills/developing-cpp/references/naming/examples/13-naming-state-change.md +1 -1
- package/skills/developing-cpp/references/naming/glossary/04-square.md +1 -1
- package/skills/developing-cpp/references/style/01-google-cpp-style.md +5 -5
- package/skills/developing-cpp/scripts/checking-comments.mjs +16 -2
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +5 -1
- package/skills/developing-cpp/steps/01-behavior.md +1 -1
- package/skills/developing-cpp/steps/02-boundary.md +2 -2
- package/skills/developing-cpp/steps/03-declaration.md +3 -3
- package/skills/developing-cpp/steps/04-implementation.md +4 -4
- package/skills/engineering/SKILL.md +2 -2
- package/skills/engineering/references/cmake/assets/OutputSettings.cmake +3 -3
- package/skills/engineering/references/cmake/modules/01-functions.md +2 -1
- package/skills/engineering/references/cmake/standards/versions.md +5 -5
- package/skills/engineering/references/cmake/targets/01-target-rules.md +10 -8
- package/skills/engineering/references/formatting/01-encoding.md +7 -5
- package/skills/engineering/references/formatting/examples/04-config-comments.md +36 -9
- package/skills/engineering/references/index.md +4 -4
- package/skills/engineering/scripts/checking-encoding.mjs +270 -5
- package/skills/engineering/steps/01-targets.md +2 -10
- package/skills/engineering/steps/03-check.md +7 -14
- package/templates/cpp-workflow-project/SKILL.md +6 -3
|
@@ -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,7 +123,242 @@ function isBinary(buffer) {
|
|
|
116
123
|
return false
|
|
117
124
|
}
|
|
118
125
|
|
|
119
|
-
function
|
|
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
363
|
const base = path.basename(file).toLowerCase()
|
|
122
364
|
const extension = base.startsWith('.') ? base : path.extname(file).toLowerCase()
|
|
@@ -125,6 +367,9 @@ function inspect(file, root, profile) {
|
|
|
125
367
|
const issues = []
|
|
126
368
|
const add = (kind, message) => issues.push({ path: rel, kind, message })
|
|
127
369
|
|
|
370
|
+
const attributes = attributesFor(root, rel, caches.gitAttributes)
|
|
371
|
+
const editor = editorConfigFor(root, rel, caches.editorConfig)
|
|
372
|
+
|
|
128
373
|
let buffer
|
|
129
374
|
try {
|
|
130
375
|
buffer = readFileSync(file)
|
|
@@ -133,9 +378,21 @@ function inspect(file, root, profile) {
|
|
|
133
378
|
return { rel, issues }
|
|
134
379
|
}
|
|
135
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
|
+
|
|
136
392
|
const bom = bomOf(buffer)
|
|
393
|
+
const bomAllowed = charset === 'utf-8-bom' || (profile === 'msvc' && source)
|
|
137
394
|
if (bom === 'UTF-8') {
|
|
138
|
-
if (
|
|
395
|
+
if (!bomAllowed) {
|
|
139
396
|
add('bom', profile === 'msvc' && !source ? '只有 MSVC 编译的 C/C++ 源码可以带 BOM' : '不应带 BOM')
|
|
140
397
|
}
|
|
141
398
|
} else if (bom) {
|
|
@@ -162,7 +419,7 @@ function inspect(file, root, profile) {
|
|
|
162
419
|
add('bom', '文件中间含 U+FEFF')
|
|
163
420
|
}
|
|
164
421
|
|
|
165
|
-
if (buffer.length > 0 && !body.endsWith('\n')) {
|
|
422
|
+
if (buffer.length > 0 && !body.endsWith('\n') && editor.get('insert_final_newline') !== 'false') {
|
|
166
423
|
add('newline', '缺少末尾换行')
|
|
167
424
|
}
|
|
168
425
|
|
|
@@ -170,10 +427,17 @@ function inspect(file, root, profile) {
|
|
|
170
427
|
const lf = (body.match(/(?<!\r)\n/g) ?? []).length
|
|
171
428
|
const cr = (body.match(/\r(?!\n)/g) ?? []).length
|
|
172
429
|
|
|
173
|
-
|
|
430
|
+
const expected = configuredLineEnding(editor, attributes)
|
|
431
|
+
if (expected === 'crlf') {
|
|
174
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')
|
|
175
437
|
} else if (profile === 'msvc' && source && !alwaysLf) {
|
|
176
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')
|
|
177
441
|
} else if (crlf > 0 || cr > 0) {
|
|
178
442
|
add('eol', '应为 LF')
|
|
179
443
|
}
|
|
@@ -183,9 +447,10 @@ function inspect(file, root, profile) {
|
|
|
183
447
|
|
|
184
448
|
export function checkEncoding(root, profile = 'portable') {
|
|
185
449
|
const files = listFiles(root)
|
|
450
|
+
const caches = { gitAttributes: new Map(), editorConfig: new Map() }
|
|
186
451
|
const results = []
|
|
187
452
|
for (const file of files) {
|
|
188
|
-
results.push(inspect(file, root, profile))
|
|
453
|
+
results.push(inspect(file, root, profile, caches))
|
|
189
454
|
}
|
|
190
455
|
const issues = results.flatMap((result) => result.issues)
|
|
191
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
|
|
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,15 +29,7 @@ target 规则:
|
|
|
29
29
|
|
|
30
30
|
## 命令形状
|
|
31
31
|
|
|
32
|
-
|
|
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
|
|
|
@@ -6,27 +6,20 @@
|
|
|
6
6
|
|
|
7
7
|
## 命令
|
|
8
8
|
|
|
9
|
-
|
|
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
|
-
|
|
23
|
-
|
|
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
|
-
- **产物位置**:可执行文件在
|
|
29
|
-
- **产物可加载**:对
|
|
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
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: cpp-workflow-project
|
|
3
|
-
description: 当前 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`。环境轴留空时技能仍可能出现,但定构建会停。
|
|
@@ -40,7 +43,7 @@ metadata:
|
|
|
40
43
|
- CTest 在工程内注册并运行;仓库根不构建、不注册测试。
|
|
41
44
|
- 头文件用 `#pragma once`,不写路径式包含守卫。
|
|
42
45
|
- 命名按 Google C++ 风格与工程白名单执行;谓词用 `Is/Has/Can/Completes` 等肯定式。
|
|
43
|
-
- 标准由 `cmake/CompileSettings.cmake` 的 `set_target_cxx_standard`
|
|
46
|
+
- 标准由 `cmake/CompileSettings.cmake` 的 `set_target_cxx_standard` 统一施加;该模块不在模板内,需要时从插件资产 `references/cmake/assets/CompileSettings.cmake` 复制到工程根 `cmake/` 并导入。工程需要警告时再调用 `set_target_warnings`,不默认给所有 target 加 `-Werror`。`environment.json` 的 `standard` 与 `.clang-format` 同步。
|
|
44
47
|
- 每个可独立构建、运行的程序一个工程;共享库等第二个真实使用方出现、语义稳定后再抽。
|
|
45
48
|
- 阶段表、代码目录与文档章节保持对齐;没有阶段表时只写用户点名的行为。
|
|
46
49
|
|
|
@@ -65,7 +68,7 @@ metadata:
|
|
|
65
68
|
|
|
66
69
|
- clangd 提供补全、跳转和诊断;`.clangd` 指向 `build/<compiler>` 的编译数据库,不写 `-std`。
|
|
67
70
|
- `--query-driver` 放在 `.vscode/settings.json` 的 `clangd.arguments`,不要写成 `.clangd` 的 YAML 键。
|
|
68
|
-
- CMake Tools
|
|
71
|
+
- 编辑器内的配置、构建与调试从 CMake Tools 进入,命令行与 CI 一律走入口命令;两者共用同一构建目录;C/C++ 扩展只做 `CMake: Debug` 的后端,IntelliSense 关闭。
|
|
69
72
|
|
|
70
73
|
## 完成判据
|
|
71
74
|
|