dsh-stm32 0.1.0 → 0.1.2
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.md +127 -29
- package/README_EN.md +125 -29
- package/client/client.js +682 -0
- package/docs/development-notes.md +1385 -5
- package/docs/logic-audit.md +607 -0
- package/docs/reading-guide.md +738 -0
- package/lib/client.js +682 -0
- package/lib/commands/register.js +214 -12
- package/lib/data/chips.js +134 -4
- package/lib/data/clock-f1.js +158 -0
- package/lib/data/cmsis-sources.js +316 -0
- package/lib/data/f1-ram.generated.js +34 -0
- package/lib/data/firmware-packages.js +352 -0
- package/lib/data/install-guides.js +190 -0
- package/lib/data/search-roots.js +135 -15
- package/lib/data/stm32f1.js +186 -0
- package/lib/data/toolchain-config.js +310 -0
- package/lib/engine/build.js +121 -16
- package/lib/engine/cache.js +336 -0
- package/lib/engine/clock-plan.js +608 -0
- package/lib/engine/cmake-sources.js +428 -0
- package/lib/engine/code-patch.js +18 -6
- package/lib/engine/debug-session.js +163 -4
- package/lib/engine/debug.js +84 -19
- package/lib/engine/doctor.js +395 -4
- package/lib/engine/fetch.js +287 -0
- package/lib/engine/firmware-install.js +668 -0
- package/lib/engine/flash.js +145 -15
- package/lib/engine/fs-longpath.js +107 -0
- package/lib/engine/generate-cache.js +118 -0
- package/lib/engine/generate-script.js +30 -8
- package/lib/engine/generate.js +296 -14
- package/lib/engine/hal-chip.js +230 -0
- package/lib/engine/ioc-plan.js +47 -8
- package/lib/engine/msys2-install.js +1870 -0
- package/lib/engine/probe.js +57 -6
- package/lib/engine/project-export.js +129 -0
- package/lib/engine/register-clock.js +132 -0
- package/lib/engine/register-linker.js +129 -0
- package/lib/engine/register-template.js +449 -0
- package/lib/engine/regs.js +18 -4
- package/lib/engine/route-plan.js +553 -0
- package/lib/engine/scaffold.js +303 -72
- package/lib/engine/subprocess-runner.js +2 -0
- package/lib/engine/tar.js +250 -0
- package/lib/engine/text-file.js +150 -0
- package/lib/engine/thirdparty.js +243 -0
- package/lib/engine/tool-env.js +51 -0
- package/lib/engine/toolchain-gate.js +386 -0
- package/lib/engine/vscode-config.js +99 -0
- package/lib/engine/web-api.js +375 -0
- package/lib/engine/web-routes.js +100 -0
- package/lib/engine/xcube-catalog.js +102 -0
- package/lib/engine/xcube.js +254 -0
- package/lib/engine/zip.js +142 -0
- package/lib/index.js +44 -2
- package/lib/skill/stm32-workflow.js +3 -3
- package/lib/tools/build.js +55 -3
- package/lib/tools/cache.js +250 -0
- package/lib/tools/code-patch.js +29 -2
- package/lib/tools/debug-session.js +56 -14
- package/lib/tools/debug.js +43 -2
- package/lib/tools/doctor.js +16 -3
- package/lib/tools/env-install.js +472 -0
- package/lib/tools/env-probe.js +6 -2
- package/lib/tools/firmware-install.js +255 -0
- package/lib/tools/flash.js +41 -2
- package/lib/tools/generate.js +96 -10
- package/lib/tools/ioc-apply.js +27 -11
- package/lib/tools/ioc-inspect.js +4 -2
- package/lib/tools/ioc-plan.js +209 -11
- package/lib/tools/project-create.js +229 -21
- package/lib/tools/project-dir.js +95 -0
- package/lib/tools/project-export.js +100 -0
- package/lib/tools/regs.js +82 -7
- package/lib/tools/route-plan.js +467 -0
- package/package.json +11 -4
- package/templates/register/CMakeLists.txt +52 -0
- package/templates/register/Core/Src/main.c +72 -0
- package/templates/scaffold/openocd.cfg +8 -0
- package/templates/scaffold/vscode/launch.json +8 -6
- package/templates/scaffold/vscode/settings.json +5 -11
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 用户级工具链配置:插件自己记住"这台机器把工具装在哪"。
|
|
3
|
+
*
|
|
4
|
+
* 为什么要有它:以前只认三个写死的候选目录 + 两个环境变量,用户把 MSYS2 装在
|
|
5
|
+
* E:/msys64 时,每次都得自己去设 STM32_BIN_DIRS,还得重开 DSH 才生效。这里把
|
|
6
|
+
* 路径落到一个纯文本 JSON 里,插件每次自己读——装完立刻能用,与进程环境变量无关。
|
|
7
|
+
*
|
|
8
|
+
* 位置:%USERPROFILE%/.dsh-stm32/config.json;DSH_STM32_CONFIG 可整体改指到别处
|
|
9
|
+
* (单测与多工程隔离用)。DSH_STM32_NO_CONFIG=1 让所有读配置的地方当它不存在。
|
|
10
|
+
*
|
|
11
|
+
* 文件是可选增强,不是前提:读不到、写坏了都只是"没配置",不抛异常给调用方。
|
|
12
|
+
*/
|
|
13
|
+
import { randomBytes } from "node:crypto";
|
|
14
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
15
|
+
import { homedir } from "node:os";
|
|
16
|
+
import { dirname, isAbsolute } from "node:path";
|
|
17
|
+
export const TOOLCHAIN_CONFIG_VERSION = 1;
|
|
18
|
+
let cache = null;
|
|
19
|
+
function envValue(env, key) {
|
|
20
|
+
const value = env[key];
|
|
21
|
+
return value === undefined ? "" : value.trim();
|
|
22
|
+
}
|
|
23
|
+
/** 用户主目录:Windows 上 USERPROFILE 最准,其余退回 os.homedir()。 */
|
|
24
|
+
function homeDir(env) {
|
|
25
|
+
const userProfile = envValue(env, "USERPROFILE");
|
|
26
|
+
if (userProfile !== "")
|
|
27
|
+
return userProfile;
|
|
28
|
+
const home = envValue(env, "HOME");
|
|
29
|
+
if (home !== "")
|
|
30
|
+
return home;
|
|
31
|
+
return homedir();
|
|
32
|
+
}
|
|
33
|
+
/** 配置文件路径。DSH_STM32_CONFIG 优先(相对路径按当前工作目录解析)。 */
|
|
34
|
+
export function toolchainConfigPath(env = process.env) {
|
|
35
|
+
const override = envValue(env, "DSH_STM32_CONFIG");
|
|
36
|
+
if (override !== "")
|
|
37
|
+
return isAbsolute(override) ? override : homeDir(env) + "/" + override;
|
|
38
|
+
return toPosix(homeDir(env)) + "/.dsh-stm32/config.json";
|
|
39
|
+
}
|
|
40
|
+
/** 关掉配置读取(单测 / 想完全走环境变量时用)。 */
|
|
41
|
+
export function configReadingDisabled(env = process.env) {
|
|
42
|
+
const flag = envValue(env, "DSH_STM32_NO_CONFIG");
|
|
43
|
+
return flag === "1" || flag === "true";
|
|
44
|
+
}
|
|
45
|
+
function toPosix(value) {
|
|
46
|
+
return value.replace(/\\/gu, "/").replace(/\/+$/u, "");
|
|
47
|
+
}
|
|
48
|
+
function asString(value) {
|
|
49
|
+
return typeof value === "string" ? value.trim() : "";
|
|
50
|
+
}
|
|
51
|
+
function asStringArray(value) {
|
|
52
|
+
if (!Array.isArray(value))
|
|
53
|
+
return [];
|
|
54
|
+
const out = [];
|
|
55
|
+
for (const item of value) {
|
|
56
|
+
if (typeof item !== "string")
|
|
57
|
+
continue;
|
|
58
|
+
const trimmed = item.trim();
|
|
59
|
+
if (trimmed !== "")
|
|
60
|
+
out.push(toPosix(trimmed));
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
function dedupe(values) {
|
|
65
|
+
const seen = new Set();
|
|
66
|
+
const out = [];
|
|
67
|
+
for (const value of values) {
|
|
68
|
+
const key = value.toLowerCase();
|
|
69
|
+
if (seen.has(key))
|
|
70
|
+
continue;
|
|
71
|
+
seen.add(key);
|
|
72
|
+
out.push(value);
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
export function defaultToolchainConfig() {
|
|
77
|
+
return {
|
|
78
|
+
version: TOOLCHAIN_CONFIG_VERSION,
|
|
79
|
+
msys64: "",
|
|
80
|
+
binDirs: [],
|
|
81
|
+
packages: [],
|
|
82
|
+
cubeMxRoot: "",
|
|
83
|
+
updatedAt: "",
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/** 把任意 JSON 归一成合法配置:未知字段丢掉,类型不对的字段按空处理并记一条 errors。 */
|
|
87
|
+
export function normalizeToolchainConfig(raw, errors) {
|
|
88
|
+
const base = defaultToolchainConfig();
|
|
89
|
+
if (raw === null || typeof raw !== "object") {
|
|
90
|
+
errors.push("配置内容不是对象,已按空配置处理");
|
|
91
|
+
return base;
|
|
92
|
+
}
|
|
93
|
+
const record = raw;
|
|
94
|
+
const version = typeof record["version"] === "number" ? record["version"] : TOOLCHAIN_CONFIG_VERSION;
|
|
95
|
+
if (typeof record["version"] !== "number")
|
|
96
|
+
errors.push("version 不是数字,按 " + String(TOOLCHAIN_CONFIG_VERSION));
|
|
97
|
+
const msys64 = toPosix(asString(record["msys64"]));
|
|
98
|
+
const binDirs = dedupe(asStringArray(record["binDirs"]));
|
|
99
|
+
const packages = dedupe(asStringArray(record["packages"]));
|
|
100
|
+
const cubeMxRoot = toPosix(asString(record["cubeMxRoot"]));
|
|
101
|
+
return {
|
|
102
|
+
version,
|
|
103
|
+
msys64,
|
|
104
|
+
binDirs,
|
|
105
|
+
packages,
|
|
106
|
+
cubeMxRoot,
|
|
107
|
+
updatedAt: asString(record["updatedAt"]),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/** 读配置;坏文件不抛异常,返回 config=null + errors,并把原文错误写进 errors。 */
|
|
111
|
+
export function readToolchainConfig(env = process.env) {
|
|
112
|
+
const path = toolchainConfigPath(env);
|
|
113
|
+
if (configReadingDisabled(env)) {
|
|
114
|
+
return { path, exists: false, config: null, errors: [], mtimeMs: 0 };
|
|
115
|
+
}
|
|
116
|
+
let mtimeMs = 0;
|
|
117
|
+
let size = -1;
|
|
118
|
+
try {
|
|
119
|
+
const stat = statSync(path);
|
|
120
|
+
mtimeMs = stat.mtimeMs;
|
|
121
|
+
size = stat.size;
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
cache = null;
|
|
125
|
+
return { path, exists: false, config: null, errors: [], mtimeMs: 0 };
|
|
126
|
+
}
|
|
127
|
+
if (cache !== null && cache.path === path && cache.mtimeMs === mtimeMs && cache.size === size) {
|
|
128
|
+
return cache.read;
|
|
129
|
+
}
|
|
130
|
+
let text = "";
|
|
131
|
+
try {
|
|
132
|
+
text = readFileSync(path, "utf8");
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
const read = {
|
|
136
|
+
path,
|
|
137
|
+
exists: true,
|
|
138
|
+
config: null,
|
|
139
|
+
errors: ["配置文件读不了:" + describeError(error)],
|
|
140
|
+
mtimeMs,
|
|
141
|
+
};
|
|
142
|
+
cache = { path, mtimeMs, size, read };
|
|
143
|
+
return read;
|
|
144
|
+
}
|
|
145
|
+
const errors = [];
|
|
146
|
+
let parsed = null;
|
|
147
|
+
try {
|
|
148
|
+
parsed = text.trim() === "" ? null : JSON.parse(text);
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
const read = {
|
|
152
|
+
path,
|
|
153
|
+
exists: true,
|
|
154
|
+
config: null,
|
|
155
|
+
errors: ["配置文件不是合法 JSON:" + describeError(error) + ";已按未配置处理(重新写入会覆盖它)"],
|
|
156
|
+
mtimeMs,
|
|
157
|
+
};
|
|
158
|
+
cache = { path, mtimeMs, size, read };
|
|
159
|
+
return read;
|
|
160
|
+
}
|
|
161
|
+
const config = parsed === null ? defaultToolchainConfig() : normalizeToolchainConfig(parsed, errors);
|
|
162
|
+
const read = { path, exists: true, config, errors, mtimeMs };
|
|
163
|
+
cache = { path, mtimeMs, size, read };
|
|
164
|
+
return read;
|
|
165
|
+
}
|
|
166
|
+
function describeError(error) {
|
|
167
|
+
return error instanceof Error ? error.message : String(error);
|
|
168
|
+
}
|
|
169
|
+
/** 写前快照:mtime + 文件里记的 updatedAt,用来判断"读完之后有没有别人动过"。 */
|
|
170
|
+
function snapshotConfig(path) {
|
|
171
|
+
let mtimeMs = 0;
|
|
172
|
+
try {
|
|
173
|
+
mtimeMs = statSync(path).mtimeMs;
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
// 文件不存在:和 readToolchainConfig 一样给 0,两边才能对上
|
|
177
|
+
return { mtimeMs: 0, updatedAt: "" };
|
|
178
|
+
}
|
|
179
|
+
let updatedAt = "";
|
|
180
|
+
try {
|
|
181
|
+
const parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
182
|
+
if (parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
183
|
+
const value = parsed["updatedAt"];
|
|
184
|
+
updatedAt = typeof value === "string" ? value : "";
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
// 坏 JSON 的 updatedAt 当空串;要不要算冲突交给 mtime 判断
|
|
189
|
+
}
|
|
190
|
+
return { mtimeMs, updatedAt };
|
|
191
|
+
}
|
|
192
|
+
function conflictOf(current, expect) {
|
|
193
|
+
if (expect.mtimeMs !== undefined && current.mtimeMs !== expect.mtimeMs)
|
|
194
|
+
return true;
|
|
195
|
+
if (expect.updatedAt !== undefined && current.updatedAt !== expect.updatedAt)
|
|
196
|
+
return true;
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* 原子写入:先写同目录 tmp 再 rename。
|
|
201
|
+
* 半截写入比没有配置更糟(下次启动直接坏 JSON),所以不做原地覆盖。
|
|
202
|
+
* 给了 options.expect 就先做一次 CAS:读完之后别人改过,宁可这次不写也不把对方的内容整段覆盖掉。
|
|
203
|
+
*/
|
|
204
|
+
export function writeToolchainConfig(config, env = process.env, options = {}) {
|
|
205
|
+
const path = toolchainConfigPath(env);
|
|
206
|
+
if (options.expect !== undefined && conflictOf(snapshotConfig(path), options.expect)) {
|
|
207
|
+
return {
|
|
208
|
+
ok: false,
|
|
209
|
+
path,
|
|
210
|
+
error: "配置文件在读完之后被别处改过:为免覆盖对方的内容,这次没有写",
|
|
211
|
+
conflict: true,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
const normalized = {
|
|
215
|
+
version: TOOLCHAIN_CONFIG_VERSION,
|
|
216
|
+
msys64: toPosix(config.msys64),
|
|
217
|
+
binDirs: dedupe(config.binDirs.map(toPosix)),
|
|
218
|
+
packages: dedupe(config.packages.map((name) => name.trim()).filter((name) => name !== "")),
|
|
219
|
+
cubeMxRoot: toPosix(config.cubeMxRoot),
|
|
220
|
+
updatedAt: new Date().toISOString(),
|
|
221
|
+
};
|
|
222
|
+
const body = JSON.stringify(normalized, null, 2) + "\n";
|
|
223
|
+
// tmp 名带随机后缀:多个 DSH 窗口同时写时不会挤同一个临时文件(pid 在 Windows 上复用很快)。
|
|
224
|
+
const tmpPath = path + ".tmp-" + String(process.pid) + "-" + randomBytes(4).toString("hex");
|
|
225
|
+
try {
|
|
226
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
227
|
+
writeFileSync(tmpPath, body, "utf8");
|
|
228
|
+
renameSync(tmpPath, path);
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
231
|
+
try {
|
|
232
|
+
if (existsSync(tmpPath))
|
|
233
|
+
unlinkSync(tmpPath);
|
|
234
|
+
}
|
|
235
|
+
catch {
|
|
236
|
+
// 清理失败不影响主错误
|
|
237
|
+
}
|
|
238
|
+
cache = null;
|
|
239
|
+
return { ok: false, path, error: "写配置失败:" + describeError(error), conflict: false };
|
|
240
|
+
}
|
|
241
|
+
cache = null;
|
|
242
|
+
return { ok: true, path, error: "", conflict: false };
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* 只改一部分字段(其余保持原样)。读不到原配置时按默认值起步。
|
|
246
|
+
* 带 options.expect 时先 CAS;一旦发现别人在这中间写过,就重读、把 patch 叠到新内容上再写一次
|
|
247
|
+
* (attempts=2)。安装器耗时几分钟,期间用户完全可能在另一个窗口改配置,直接覆盖会丢对方的设置。
|
|
248
|
+
*/
|
|
249
|
+
export function updateToolchainConfig(patch, env = process.env, options = {}) {
|
|
250
|
+
let read = readToolchainConfig(env);
|
|
251
|
+
let expect = options.expect;
|
|
252
|
+
let attempts = 0;
|
|
253
|
+
for (;;) {
|
|
254
|
+
attempts += 1;
|
|
255
|
+
const base = read.config ?? defaultToolchainConfig();
|
|
256
|
+
const next = {
|
|
257
|
+
version: TOOLCHAIN_CONFIG_VERSION,
|
|
258
|
+
msys64: patch.msys64 ?? base.msys64,
|
|
259
|
+
binDirs: patch.binDirs ?? base.binDirs,
|
|
260
|
+
packages: patch.packages ?? base.packages,
|
|
261
|
+
cubeMxRoot: patch.cubeMxRoot ?? base.cubeMxRoot,
|
|
262
|
+
updatedAt: base.updatedAt,
|
|
263
|
+
};
|
|
264
|
+
const written = writeToolchainConfig(next, env, expect === undefined ? {} : { expect });
|
|
265
|
+
if (written.conflict && attempts === 1) {
|
|
266
|
+
resetToolchainConfigCache();
|
|
267
|
+
read = readToolchainConfig(env);
|
|
268
|
+
expect = { mtimeMs: read.mtimeMs, updatedAt: read.config?.updatedAt ?? "" };
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
return { ok: written.ok, path: written.path, error: written.error, config: next, attempts };
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* 配置里认的工具链 bin 目录。
|
|
276
|
+
* binDirs 为空但有 msys64 时按 MSYS2 的 UCRT64 布局推导——这是安装器写配置的唯一形状。
|
|
277
|
+
*/
|
|
278
|
+
export function binDirsFromConfig(config) {
|
|
279
|
+
if (config === null)
|
|
280
|
+
return [];
|
|
281
|
+
if (config.binDirs.length > 0)
|
|
282
|
+
return [...config.binDirs];
|
|
283
|
+
if (config.msys64 !== "")
|
|
284
|
+
return [config.msys64 + "/ucrt64/bin"];
|
|
285
|
+
return [];
|
|
286
|
+
}
|
|
287
|
+
/** 配置里认的 CubeMX 安装根。 */
|
|
288
|
+
export function cubeMxRootsFromConfig(config) {
|
|
289
|
+
if (config === null || config.cubeMxRoot === "")
|
|
290
|
+
return [];
|
|
291
|
+
return [config.cubeMxRoot];
|
|
292
|
+
}
|
|
293
|
+
/** 单测用:清掉 mtime 缓存。 */
|
|
294
|
+
export function resetToolchainConfigCache() {
|
|
295
|
+
cache = null;
|
|
296
|
+
}
|
|
297
|
+
/** 给报告 / 命令卡片用的一句话描述。 */
|
|
298
|
+
export function describeToolchainConfig(read) {
|
|
299
|
+
if (!read.exists)
|
|
300
|
+
return "还没有配置(没有 " + read.path + ")";
|
|
301
|
+
if (read.config === null)
|
|
302
|
+
return "配置读不了:" + (read.errors[0] ?? "未知原因");
|
|
303
|
+
const parts = [];
|
|
304
|
+
parts.push(read.config.msys64 === "" ? "MSYS2 未配置" : "MSYS2 " + read.config.msys64);
|
|
305
|
+
if (read.config.packages.length > 0)
|
|
306
|
+
parts.push("包 " + String(read.config.packages.length) + " 个");
|
|
307
|
+
if (read.config.cubeMxRoot !== "")
|
|
308
|
+
parts.push("CubeMX " + read.config.cubeMxRoot);
|
|
309
|
+
return parts.join(";") + "(" + read.path + ")";
|
|
310
|
+
}
|
package/lib/engine/build.js
CHANGED
|
@@ -8,7 +8,9 @@
|
|
|
8
8
|
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
9
9
|
import { join, resolve } from "node:path";
|
|
10
10
|
import { resolveSearchConfig } from "../data/search-roots.js";
|
|
11
|
-
import { runStreaming } from "./subprocess-runner.js";
|
|
11
|
+
import { runCapture, runStreaming } from "./subprocess-runner.js";
|
|
12
|
+
import { resolveToolIn } from "./probe.js";
|
|
13
|
+
import { envWithBinDirs } from "./tool-env.js";
|
|
12
14
|
const DEFAULT_TIMEOUT_MS = 300_000;
|
|
13
15
|
function toBytes(value, unit) {
|
|
14
16
|
const parsed = Number.parseFloat(value);
|
|
@@ -137,22 +139,85 @@ export function pickConfigurePreset(projectDir) {
|
|
|
137
139
|
});
|
|
138
140
|
return sorted[0] ?? null;
|
|
139
141
|
}
|
|
140
|
-
/**
|
|
141
|
-
|
|
142
|
-
|
|
142
|
+
/** CMakeCache 里记的工程名(CMAKE_PROJECT_NAME:<TYPE>=<名字>);读不到返回空串。 */
|
|
143
|
+
function readCmakeProjectName(buildDir) {
|
|
144
|
+
try {
|
|
145
|
+
const text = readFileSync(join(buildDir, "CMakeCache.txt"), "utf8");
|
|
146
|
+
for (const raw of text.split(/\r?\n/u)) {
|
|
147
|
+
const match = /^CMAKE_PROJECT_NAME:[A-Z]+=(.+)$/u.exec(raw.trim());
|
|
148
|
+
if (match !== null && match[1] !== undefined && match[1].trim() !== "")
|
|
149
|
+
return match[1].trim();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
// 没有缓存文件或读不了:退回"最新的那个",不影响出结果。
|
|
154
|
+
}
|
|
155
|
+
return "";
|
|
156
|
+
}
|
|
157
|
+
/** 构建目录里全部 .elf,按修改时间从新到旧。 */
|
|
158
|
+
export function elfCandidates(buildDir) {
|
|
159
|
+
let names;
|
|
160
|
+
try {
|
|
161
|
+
names = readdirSync(buildDir).filter((name) => name.toLowerCase().endsWith(".elf"));
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
return [];
|
|
165
|
+
}
|
|
166
|
+
const out = [];
|
|
167
|
+
for (const name of names) {
|
|
168
|
+
const file = join(buildDir, name);
|
|
169
|
+
try {
|
|
170
|
+
const stat = statSync(file);
|
|
171
|
+
out.push({ name, path: file, size: stat.size, mtimeMs: stat.mtimeMs });
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
// 刚被删掉或被独占:跳过,下一次再找。
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
out.sort((left, right) => right.mtimeMs - left.mtimeMs);
|
|
178
|
+
return out;
|
|
179
|
+
}
|
|
180
|
+
function pickElf(buildDir) {
|
|
181
|
+
const candidates = elfCandidates(buildDir);
|
|
182
|
+
if (candidates.length === 0)
|
|
143
183
|
return null;
|
|
144
|
-
const
|
|
145
|
-
if (
|
|
184
|
+
const projectName = readCmakeProjectName(buildDir).toLowerCase();
|
|
185
|
+
if (projectName !== "") {
|
|
186
|
+
const exact = candidates.find((item) => item.name.toLowerCase() === projectName + ".elf");
|
|
187
|
+
if (exact !== undefined)
|
|
188
|
+
return { chosen: exact, candidates, byProjectName: true };
|
|
189
|
+
}
|
|
190
|
+
const newest = candidates[0];
|
|
191
|
+
if (newest === undefined)
|
|
146
192
|
return null;
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
193
|
+
return { chosen: newest, candidates, byProjectName: false };
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* 构建目录里的 .elf(F1)。
|
|
197
|
+
*
|
|
198
|
+
* 旧规则是"与构建目录同名的优先",但构建目录叫 Debug、产物叫 demo.elf,两者永不相等,
|
|
199
|
+
* 于是退化成 readdir 顺序——改过工程名重新生成之后,会把旧固件当产物烧进板子。
|
|
200
|
+
* 现在的顺序:CMakeCache 里记的工程名(最权威)→ 修改时间最新(多产物时唯一有意义的判据)。
|
|
201
|
+
*/
|
|
202
|
+
export function findElf(buildDir) {
|
|
203
|
+
const picked = pickElf(buildDir);
|
|
204
|
+
if (picked === null)
|
|
205
|
+
return null;
|
|
206
|
+
const { chosen } = picked;
|
|
207
|
+
return { path: chosen.path, size: chosen.size, mtimeMs: chosen.mtimeMs };
|
|
208
|
+
}
|
|
209
|
+
/** 多个 .elf 并存时给一句说明(只有一个时返回空串,报告不加噪音)。 */
|
|
210
|
+
export function elfSelectionNote(buildDir) {
|
|
211
|
+
const picked = pickElf(buildDir);
|
|
212
|
+
if (picked === null || picked.candidates.length < 2)
|
|
213
|
+
return "";
|
|
214
|
+
const stamp = (ms) => new Date(ms).toISOString().slice(11, 19);
|
|
215
|
+
const others = picked.candidates.filter((item) => item.name !== picked.chosen.name);
|
|
216
|
+
const list = others.slice(0, 4).map((item) => item.name + "(" + stamp(item.mtimeMs) + ")").join("、");
|
|
217
|
+
return ("构建目录里有 " + String(picked.candidates.length) + " 个 .elf,用了 " + picked.chosen.name +
|
|
218
|
+
"(" + stamp(picked.chosen.mtimeMs) + "," +
|
|
219
|
+
(picked.byProjectName ? "按 CMakeCache 的工程名选" : "按修改时间最新选") +
|
|
220
|
+
");没动的 " + String(others.length) + " 个:" + list);
|
|
156
221
|
}
|
|
157
222
|
/** 解析 cmake 可执行文件:优先显式 binDirs(msys64),否则交给 PATH。 */
|
|
158
223
|
function resolveCmake(subprocess, binDirs) {
|
|
@@ -192,6 +257,9 @@ export async function runBuild(subprocess, request) {
|
|
|
192
257
|
return fail(request, "工程目录不存在:" + projectDir);
|
|
193
258
|
const config = resolveSearchConfig(process.env, request.binDirs === undefined ? {} : { binDirs: request.binDirs });
|
|
194
259
|
const cmake = resolveCmake(subprocess, config.binDirs);
|
|
260
|
+
// 子进程带上 binDirs 组成的 PATH:cmake 会再去调 arm-none-eabi-gcc / ninja / objcopy,
|
|
261
|
+
// 只把这些目录用于"找 cmake 本身"是不够的(宿主进程的 PATH 可能还是安装前的旧值)。
|
|
262
|
+
const childEnv = envWithBinDirs(config.binDirs);
|
|
195
263
|
const progress = request.onProgress ?? (() => undefined);
|
|
196
264
|
const preNotes = [];
|
|
197
265
|
let buildDir = request.buildDir === undefined ? detectBuildDir(projectDir) : resolve(projectDir, request.buildDir);
|
|
@@ -210,6 +278,7 @@ export async function runBuild(subprocess, request) {
|
|
|
210
278
|
cwd: projectDir,
|
|
211
279
|
timeoutMs: request.timeoutMs ?? DEFAULT_TIMEOUT_MS,
|
|
212
280
|
maxBytes: 1024 * 1024,
|
|
281
|
+
env: childEnv,
|
|
213
282
|
...(request.signal === undefined ? {} : { signal: request.signal }),
|
|
214
283
|
onOutput: (stream, text) => {
|
|
215
284
|
for (const line of text.split(/\r?\n/u)) {
|
|
@@ -260,6 +329,7 @@ export async function runBuild(subprocess, request) {
|
|
|
260
329
|
cwd: projectDir,
|
|
261
330
|
timeoutMs: request.timeoutMs ?? DEFAULT_TIMEOUT_MS,
|
|
262
331
|
maxBytes: 2 * 1024 * 1024,
|
|
332
|
+
env: childEnv,
|
|
263
333
|
...(request.signal === undefined ? {} : { signal: request.signal }),
|
|
264
334
|
onOutput: (stream, text) => {
|
|
265
335
|
for (const line of text.split(/\r?\n/u)) {
|
|
@@ -287,8 +357,12 @@ export async function runBuild(subprocess, request) {
|
|
|
287
357
|
logTail: tail(log, 30),
|
|
288
358
|
};
|
|
289
359
|
const notes = [...preNotes];
|
|
290
|
-
if (elf !== null)
|
|
360
|
+
if (elf !== null) {
|
|
291
361
|
notes.push("产物:" + elf.path + "(" + String(elf.size) + " B)");
|
|
362
|
+
const selectionNote = elfSelectionNote(buildDir);
|
|
363
|
+
if (selectionNote !== "")
|
|
364
|
+
notes.push(selectionNote);
|
|
365
|
+
}
|
|
292
366
|
if (memoryUsage.length > 0)
|
|
293
367
|
notes.push("占用:" + memoryUsage.map((item) => item.region + " " + String(item.percent) + "%").join(","));
|
|
294
368
|
else if (elf !== null)
|
|
@@ -312,9 +386,40 @@ export async function runBuild(subprocess, request) {
|
|
|
312
386
|
if (cacheMismatch !== undefined) {
|
|
313
387
|
return fail(request, "build 目录是从别处复制或移动过来的:CMakeCache.txt 里记的绝对路径对不上(cmake 报错但退出码是 0,不拦会误报成功)。先 clean 或删掉 " + base.buildDir + " 再编译。", { ...base, errors, notes });
|
|
314
388
|
}
|
|
389
|
+
// 2b:按请求转换 hex / bin。objcopy 失败不判 build 失败——编译本身成功了,
|
|
390
|
+
// 转不出来只降级为 warning(给 CubeProgrammer 用户一句怎么自己转)。
|
|
391
|
+
const wanted = new Set(request.artifacts ?? []);
|
|
392
|
+
const converted = [];
|
|
393
|
+
if (elf !== null && (wanted.has("hex") || wanted.has("bin"))) {
|
|
394
|
+
const objcopy = await resolveToolIn(subprocess, ["arm-none-eabi-objcopy"], config.binDirs, request.signal);
|
|
395
|
+
for (const kind of ["hex", "bin"]) {
|
|
396
|
+
if (!wanted.has(kind))
|
|
397
|
+
continue;
|
|
398
|
+
if (objcopy === null) {
|
|
399
|
+
notes.push("要 " + kind + " 但找不到 arm-none-eabi-objcopy(工具链不完整);跳过转换,elf 仍可用");
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
const out = elf.path.replace(/\.elf$/u, "") + (kind === "hex" ? ".hex" : ".bin");
|
|
403
|
+
const conv = await runCapture(subprocess, {
|
|
404
|
+
argv: [objcopy, "-O", kind === "hex" ? "ihex" : "binary", elf.path, out],
|
|
405
|
+
cwd: buildDir,
|
|
406
|
+
timeoutMs: 60_000,
|
|
407
|
+
...(request.signal === undefined ? {} : { signal: request.signal }),
|
|
408
|
+
});
|
|
409
|
+
if (conv.spawnError === null && conv.exitCode === 0 && existsSync(out)) {
|
|
410
|
+
const size = statSync(out).size;
|
|
411
|
+
converted.push({ kind, path: out, size });
|
|
412
|
+
notes.push(kind + ":" + out + "(" + String(size) + " B)");
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
notes.push(kind + " 转换失败(" + (conv.spawnError ?? "退出码 " + String(conv.exitCode)) + ");elf 仍可用");
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
315
419
|
return {
|
|
316
420
|
ok: true,
|
|
317
421
|
...base,
|
|
422
|
+
...(converted.length === 0 ? {} : { converted }),
|
|
318
423
|
notes,
|
|
319
424
|
message: "编译成功(" + (durationMs / 1000).toFixed(1) + " s," + String(errors.length) + " 个错误、" + String(base.warningCount) + " 个警告)",
|
|
320
425
|
};
|