dsh-stm32 0.1.1 → 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 +52 -23
- package/README_EN.md +50 -21
- package/docs/development-notes.md +466 -4
- package/docs/reading-guide.md +738 -0
- package/lib/commands/register.js +49 -0
- package/lib/data/chips.js +134 -4
- package/lib/data/clock-f1.js +158 -0
- package/lib/data/cmsis-sources.js +211 -42
- package/lib/data/f1-ram.generated.js +34 -0
- package/lib/data/stm32f1.js +186 -0
- package/lib/engine/build.js +115 -16
- package/lib/engine/cache.js +56 -0
- package/lib/engine/clock-plan.js +608 -0
- package/lib/engine/cmake-sources.js +428 -0
- package/lib/engine/debug.js +75 -10
- package/lib/engine/doctor.js +395 -4
- package/lib/engine/fetch.js +174 -30
- package/lib/engine/flash.js +95 -6
- package/lib/engine/generate.js +11 -0
- package/lib/engine/hal-chip.js +230 -0
- package/lib/engine/ioc-plan.js +28 -2
- package/lib/engine/msys2-install.js +332 -61
- 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 +88 -17
- package/lib/engine/scaffold.js +104 -28
- package/lib/engine/zip.js +142 -0
- package/lib/index.js +2 -0
- package/lib/skill/stm32-workflow.js +3 -3
- package/lib/tools/build.js +12 -0
- package/lib/tools/cache.js +39 -8
- package/lib/tools/debug.js +7 -0
- package/lib/tools/doctor.js +11 -2
- package/lib/tools/flash.js +3 -0
- package/lib/tools/ioc-plan.js +195 -4
- package/lib/tools/project-create.js +26 -1
- package/lib/tools/project-export.js +100 -0
- package/package.json +5 -3
- package/templates/register/CMakeLists.txt +19 -6
- package/templates/register/Core/Src/main.c +18 -9
- package/templates/scaffold/openocd.cfg +8 -0
|
@@ -37,9 +37,36 @@ const WINDOWS = process.platform === "win32";
|
|
|
37
37
|
* 它解压出一个顶层 msys64 目录,所以官方文档写 -y -oC:\ 就会得到 C:\msys64。
|
|
38
38
|
*/
|
|
39
39
|
const INSTALLER_FILE = "msys2-base-x86_64-latest.sfx.exe";
|
|
40
|
-
|
|
40
|
+
/**
|
|
41
|
+
* 同一个安装包在不同地方有两个文件名:镜像用 msys2-x86_64-latest.sfx.exe,
|
|
42
|
+
* GitHub releases 用 msys2-base-x86_64-latest.sfx.exe。两个都试——一个名字 404
|
|
43
|
+
* 不代表这个源没有包(实测 TUNA 只有 msys2-x86_64- 那个)。
|
|
44
|
+
*/
|
|
45
|
+
const INSTALLER_NAMES = ["msys2-x86_64-latest.sfx.exe", INSTALLER_FILE];
|
|
46
|
+
/**
|
|
47
|
+
* 安装包候选源(实测 2026-10,HEAD):TUNA / NJU / 阿里云 均 200 且 43,117,824 B;
|
|
48
|
+
* USTC 的 distrib 目录回 403(仍留作候选:它可能只挡 HEAD,真下能成)。
|
|
49
|
+
* 镜像同步时点不同 → 字节数不同 → 不能跨源钉同一个 sha256,只能做 F3d 的三道粗检。
|
|
50
|
+
*/
|
|
51
|
+
const INSTALLER_MIRRORS = [
|
|
52
|
+
{ label: "清华 TUNA", base: "https://mirrors.tuna.tsinghua.edu.cn/msys2/distrib" },
|
|
53
|
+
{ label: "中科大 USTC", base: "https://mirrors.ustc.edu.cn/msys2/distrib" },
|
|
54
|
+
{ label: "南大 NJU", base: "https://mirror.nju.edu.cn/msys2/distrib" },
|
|
55
|
+
{ label: "阿里云", base: "https://mirrors.aliyun.com/msys2/distrib" },
|
|
56
|
+
];
|
|
57
|
+
/** 官方兜底:GitHub releases/latest。F3e 要求「GitHub 类下载不改顺序」,它在候选链的最后。 */
|
|
58
|
+
const INSTALLER_GITHUB = "https://github.com/msys2/msys2-installer/releases/latest/download";
|
|
59
|
+
export const MSYS2_INSTALLER_URL = INSTALLER_GITHUB + "/" + INSTALLER_FILE;
|
|
41
60
|
/** 该资产的字节数(实测 43,060,392);只用于计划里的用量估算,不参与校验。 */
|
|
42
61
|
export const MSYS2_INSTALLER_BYTES = 43_060_392;
|
|
62
|
+
/**
|
|
63
|
+
* 真包的体量下限(实测 42.9–43.1 MB)。小于它就是半截文件或错误页:
|
|
64
|
+
* 半截文件若被当成有效缓存,解压会永远失败,用户只能自己猜该删哪个文件(F3d)。
|
|
65
|
+
*/
|
|
66
|
+
const MIN_INSTALLER_BYTES = 40_000_000;
|
|
67
|
+
/** 下载中被判「这条线不行」的两个阈值:10 s 内平均不足 1 KB/s 就换源。 */
|
|
68
|
+
const DOWNLOAD_STALL_MS = 10_000;
|
|
69
|
+
const DOWNLOAD_MIN_BYTES_PER_SEC = 1024;
|
|
43
70
|
/** 默认补的包:编译器 / 调试器 / 烧录服务端 / 构建系统 / 构建工具。 */
|
|
44
71
|
export const DEFAULT_MSYS2_PACKAGES = [
|
|
45
72
|
"mingw-w64-ucrt-x86_64-arm-none-eabi-gcc",
|
|
@@ -156,6 +183,17 @@ function looksLikeExecutable(path) {
|
|
|
156
183
|
}
|
|
157
184
|
}
|
|
158
185
|
}
|
|
186
|
+
/**
|
|
187
|
+
* 缓存能不能当"已经下好的安装包"用(F3d):MZ 头 + 体量下限。
|
|
188
|
+
* 只查 size>0 时,一次超时留下的 12 MB 半截文件会被下一次当成有效缓存,解压永远失败。
|
|
189
|
+
* 返回可用字节数,0 表示不可用。
|
|
190
|
+
*/
|
|
191
|
+
function cachedInstallerBytes(path) {
|
|
192
|
+
const bytes = fileSize(path);
|
|
193
|
+
if (bytes < MIN_INSTALLER_BYTES)
|
|
194
|
+
return 0;
|
|
195
|
+
return looksLikeExecutable(path) ? bytes : 0;
|
|
196
|
+
}
|
|
159
197
|
function safeList(dir) {
|
|
160
198
|
try {
|
|
161
199
|
return readdirSync(dir);
|
|
@@ -185,6 +223,32 @@ function removeDirQuietly(dir) {
|
|
|
185
223
|
// 删不掉就留着,不影响结论
|
|
186
224
|
}
|
|
187
225
|
}
|
|
226
|
+
/**
|
|
227
|
+
* 是不是 Windows 盘根(C:/、C:、D:\ 这类)。
|
|
228
|
+
* Node 的 mkdirSync 对盘根会抛 EPERM(Windows 不允许 mkdir 在盘根上"创建"它),
|
|
229
|
+
* 而 targetDir=C:/msys64 时 parent 恰好就是盘根——那是本插件自己建议的默认装法,
|
|
230
|
+
* 不能让推荐路径变成必炸路径。
|
|
231
|
+
*/
|
|
232
|
+
export function isDriveRoot(dir) {
|
|
233
|
+
const posix = toPosix(dir).replace(/\/+$/u, "");
|
|
234
|
+
return /^[A-Za-z]:$/u.test(posix);
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* 确保父目录存在:盘根直接 existsSync 校验(永远存在),其余才走 mkdirSync。
|
|
238
|
+
* 返回空串表示成功,非空是中文错误原因。
|
|
239
|
+
*/
|
|
240
|
+
function ensureParentDir(parent) {
|
|
241
|
+
if (isDriveRoot(parent)) {
|
|
242
|
+
return existsSync(parent) ? "" : "盘根不存在:" + parent;
|
|
243
|
+
}
|
|
244
|
+
try {
|
|
245
|
+
mkdirSync(parent, { recursive: true });
|
|
246
|
+
return "";
|
|
247
|
+
}
|
|
248
|
+
catch (error) {
|
|
249
|
+
return describeError(error);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
188
252
|
function isMsys2Root(dir) {
|
|
189
253
|
return dir !== "" && existsSync(dir + "/usr/bin/bash.exe");
|
|
190
254
|
}
|
|
@@ -197,23 +261,50 @@ function msys2PackagesTotal(packages) {
|
|
|
197
261
|
function isAborted(signal) {
|
|
198
262
|
return signal?.aborted === true;
|
|
199
263
|
}
|
|
264
|
+
/** 候选链的人话标签,报错时能说清"试过哪些源、各是怎么失败的"。 */
|
|
265
|
+
export function installerSourceLabel(url) {
|
|
266
|
+
for (const mirror of INSTALLER_MIRRORS) {
|
|
267
|
+
if (url.startsWith(mirror.base))
|
|
268
|
+
return mirror.label;
|
|
269
|
+
}
|
|
270
|
+
return url.startsWith(INSTALLER_GITHUB) ? "GitHub" : "自定义地址";
|
|
271
|
+
}
|
|
200
272
|
/**
|
|
201
|
-
*
|
|
202
|
-
*
|
|
273
|
+
* 安装包候选地址(F3a):环境变量整体替换 > MSYS2_MIRROR 前缀 > 国内镜像链 → GitHub。
|
|
274
|
+
*
|
|
275
|
+
* 为什么不再只写死 GitHub:releases/latest 在部分网络里连不上(与 CMSIS 头同一个墙),
|
|
276
|
+
* 一次 43 MB 的下载失败就等于这次安装整个作废,而国内镜像本来就有同名包。
|
|
277
|
+
* 每个源都试两个文件名(镜像用 msys2-x86_64-,GitHub 用 msys2-base-)。
|
|
278
|
+
* MSYS2_MIRROR 给全(以 .exe/.7z/.zip 结尾)就直接当完整地址,否则当前缀拼两个文件名。
|
|
203
279
|
*/
|
|
204
|
-
export function
|
|
280
|
+
export function resolveInstallerUrls(env) {
|
|
205
281
|
const direct = (env["MSYS2_INSTALLER_URL"] ?? "").trim();
|
|
206
|
-
if (direct !== "")
|
|
207
|
-
return {
|
|
282
|
+
if (direct !== "") {
|
|
283
|
+
return { urls: [direct], note: "安装包地址来自环境变量 MSYS2_INSTALLER_URL(只用这一个源)" };
|
|
284
|
+
}
|
|
208
285
|
const mirror = (env["MSYS2_MIRROR"] ?? "").trim().replace(/\/+$/u, "");
|
|
209
286
|
if (mirror !== "") {
|
|
210
|
-
const
|
|
287
|
+
const urls = /\.(exe|7z|zip)$/iu.test(mirror)
|
|
288
|
+
? [mirror]
|
|
289
|
+
: INSTALLER_NAMES.map((name) => mirror + "/" + name);
|
|
211
290
|
return {
|
|
212
|
-
|
|
291
|
+
urls,
|
|
213
292
|
note: "安装包地址来自环境变量 MSYS2_MIRROR(镜像);官方源是 " + MSYS2_INSTALLER_URL,
|
|
214
293
|
};
|
|
215
294
|
}
|
|
216
|
-
|
|
295
|
+
const urls = [];
|
|
296
|
+
for (const source of INSTALLER_MIRRORS) {
|
|
297
|
+
for (const name of INSTALLER_NAMES)
|
|
298
|
+
urls.push(source.base + "/" + name);
|
|
299
|
+
}
|
|
300
|
+
for (const name of INSTALLER_NAMES)
|
|
301
|
+
urls.push(INSTALLER_GITHUB + "/" + name);
|
|
302
|
+
return {
|
|
303
|
+
urls,
|
|
304
|
+
note: "安装包按 " +
|
|
305
|
+
INSTALLER_MIRRORS.map((source) => source.label).join(" → ") +
|
|
306
|
+
" → GitHub 依次尝试,前一个源不通就换下一个(每个源两个文件名)",
|
|
307
|
+
};
|
|
217
308
|
}
|
|
218
309
|
/** 配置目录(与 toolchain-config 同目录);不可用时退回系统临时目录。 */
|
|
219
310
|
function stateDir(env) {
|
|
@@ -225,6 +316,84 @@ function cacheFilePath(env) {
|
|
|
225
316
|
// 固定文件名:换镜像/换版本不会堆一堆包体;要换版本就自己删这个文件。
|
|
226
317
|
return stateDir(env) + "/msys2-latest.sfx.exe";
|
|
227
318
|
}
|
|
319
|
+
/* --------------------------- pacman 镜像(F3b) --------------------------- */
|
|
320
|
+
/**
|
|
321
|
+
* 国内镜像的 pacman 地址。实测 2026-10:四家的 msys/x86_64/msys.db 都是 200 且同一份
|
|
322
|
+
* 516,869 B(校验和一致 → 同步是齐的)。放在官方列表**前面**,官方条目原样保留在后面兜底。
|
|
323
|
+
*/
|
|
324
|
+
const PACMAN_MIRRORS = [
|
|
325
|
+
{
|
|
326
|
+
label: "清华 TUNA",
|
|
327
|
+
msys: "https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/$arch/",
|
|
328
|
+
mingw: "https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/$repo/",
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
label: "中科大 USTC",
|
|
332
|
+
msys: "https://mirrors.ustc.edu.cn/msys2/msys/$arch/",
|
|
333
|
+
mingw: "https://mirrors.ustc.edu.cn/msys2/mingw/$repo/",
|
|
334
|
+
},
|
|
335
|
+
{
|
|
336
|
+
label: "南大 NJU",
|
|
337
|
+
msys: "https://mirror.nju.edu.cn/msys2/msys/$arch/",
|
|
338
|
+
mingw: "https://mirror.nju.edu.cn/msys2/mingw/$repo/",
|
|
339
|
+
},
|
|
340
|
+
{
|
|
341
|
+
label: "阿里云",
|
|
342
|
+
msys: "https://mirrors.aliyun.com/msys2/msys/$arch/",
|
|
343
|
+
mingw: "https://mirrors.aliyun.com/msys2/mingw/$repo/",
|
|
344
|
+
},
|
|
345
|
+
];
|
|
346
|
+
const MIRRORLIST_HEADER = "# dsh-stm32:把国内镜像放到最前面(官方源原样保留在后面兜底)。\n" +
|
|
347
|
+
"# 想去掉这段:删掉本注释与紧随其后的几行 Server = 即可,其余内容不受影响。\n";
|
|
348
|
+
/** 把一组 Server 行提到最前;已有的不重复添加(幂等,重复调不会越堆越长)。 */
|
|
349
|
+
export function preferMirrorServers(text, servers) {
|
|
350
|
+
const present = new Set(text
|
|
351
|
+
.split(/\r?\n/u)
|
|
352
|
+
.map((line) => line.trim())
|
|
353
|
+
.filter((line) => line.toLowerCase().startsWith("server ="))
|
|
354
|
+
.map((line) => line.slice("server =".length).trim()));
|
|
355
|
+
const missing = servers.filter((server) => !present.has(server));
|
|
356
|
+
if (missing.length === 0)
|
|
357
|
+
return text;
|
|
358
|
+
const block = MIRRORLIST_HEADER + missing.map((server) => "Server = " + server).join("\n") + "\n\n";
|
|
359
|
+
return block + text;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* 给新解压出来的 MSYS2 换上国内镜像(F3b)。只在"刚解压完"时调用:
|
|
363
|
+
* 已有 MSYS2 是用户自己的东西,改人家的 pacman 配置不在本工具的授权范围内。
|
|
364
|
+
* 只动 mirrorlist.msys($arch)与 mirrorlist.mingw($repo)两个文件,写前不备份也不删内容。
|
|
365
|
+
*/
|
|
366
|
+
export function preferChinaMirrors(msys64) {
|
|
367
|
+
const dir = msys64 + "/etc/pacman.d";
|
|
368
|
+
const targets = [
|
|
369
|
+
{ file: dir + "/mirrorlist.msys", servers: PACMAN_MIRRORS.map((item) => item.msys) },
|
|
370
|
+
{ file: dir + "/mirrorlist.mingw", servers: PACMAN_MIRRORS.map((item) => item.mingw) },
|
|
371
|
+
];
|
|
372
|
+
const updated = [];
|
|
373
|
+
for (const target of targets) {
|
|
374
|
+
if (!existsSync(target.file))
|
|
375
|
+
continue;
|
|
376
|
+
try {
|
|
377
|
+
const before = readFileSync(target.file, "utf8");
|
|
378
|
+
const after = preferMirrorServers(before, target.servers);
|
|
379
|
+
if (after === before)
|
|
380
|
+
continue;
|
|
381
|
+
writeFileSync(target.file, after, "utf8");
|
|
382
|
+
updated.push(target.file.slice(dir.length + 1));
|
|
383
|
+
}
|
|
384
|
+
catch {
|
|
385
|
+
// 改不动就算了:官方源还在列表里,pacman 仍能装,只是慢一点。
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
const note = updated.length === 0
|
|
389
|
+
? ""
|
|
390
|
+
: "已把 " +
|
|
391
|
+
PACMAN_MIRRORS.map((item) => item.label).join(" / ") +
|
|
392
|
+
" 放到 pacman 镜像列表最前(" +
|
|
393
|
+
updated.join("、") +
|
|
394
|
+
"),官方源保留在后面兜底;pacman 补包因此走国内线路。";
|
|
395
|
+
return { files: updated, note };
|
|
396
|
+
}
|
|
228
397
|
/** 路径所在盘符是否真的存在:跨机器的配置(配置里是 D:/app/msys64,本机没有 D 盘)不能当安装目标。 */
|
|
229
398
|
function driveUsable(path, exists = existsSync) {
|
|
230
399
|
const match = /^([A-Za-z]):/u.exec(path);
|
|
@@ -756,13 +925,15 @@ function fillPlanDetails(steps, context) {
|
|
|
756
925
|
? "已有完整 MSYS2,不需要安装包(省 " + formatBytes(MSYS2_INSTALLER_BYTES) + " 下载)"
|
|
757
926
|
: context.cacheOk
|
|
758
927
|
? "复用缓存:" + context.cachePath + "(" + formatBytes(MSYS2_INSTALLER_BYTES) + ",不会再下载)"
|
|
759
|
-
: "
|
|
928
|
+
: "按候选链下载(国内镜像优先,GitHub 兜底),第一个成功的落盘到 " +
|
|
760
929
|
context.cachePath +
|
|
761
|
-
" " +
|
|
762
|
-
context.cacheUrl +
|
|
763
930
|
"(约 " +
|
|
764
931
|
formatBytes(MSYS2_INSTALLER_BYTES) +
|
|
765
|
-
"
|
|
932
|
+
",curl -C - 断点续传、10 s 内不足 1 KB/s 就换源;缓存已有且是有效 exe 直接复用,命令示例:curl.exe -L --fail -C - -o " +
|
|
933
|
+
context.cachePath +
|
|
934
|
+
" " +
|
|
935
|
+
context.cacheUrl +
|
|
936
|
+
")");
|
|
766
937
|
setStep(stepOf(steps, "extract"), context.existing ? "skipped" : "pending", context.existing
|
|
767
938
|
? "已有 MSYS2,不需要解压"
|
|
768
939
|
: context.msys64 === ""
|
|
@@ -957,7 +1128,89 @@ function toVerifyState(results) {
|
|
|
957
1128
|
})),
|
|
958
1129
|
};
|
|
959
1130
|
}
|
|
960
|
-
|
|
1131
|
+
/** curl 的一次调用:返回退出码/超时/spawn 错误,调用方自己决定"这条线算不算死"。 */
|
|
1132
|
+
async function curlOnce(subprocess, url, partPath, cacheDir, timeoutMs, resume, signal) {
|
|
1133
|
+
const argv = [
|
|
1134
|
+
WINDOWS ? "curl.exe" : "curl",
|
|
1135
|
+
"-L",
|
|
1136
|
+
"--fail",
|
|
1137
|
+
"--retry",
|
|
1138
|
+
"2",
|
|
1139
|
+
"--connect-timeout",
|
|
1140
|
+
"20",
|
|
1141
|
+
// F3c:10 s 内平均不足 1 KB/s 就自己退出(退出码 28),由外层换下一个源。
|
|
1142
|
+
"--speed-limit",
|
|
1143
|
+
String(DOWNLOAD_MIN_BYTES_PER_SEC),
|
|
1144
|
+
"--speed-time",
|
|
1145
|
+
String(Math.round(DOWNLOAD_STALL_MS / 1000)),
|
|
1146
|
+
];
|
|
1147
|
+
// F3c:-C - 断点续传。缓存目录里有 .part 时从它的长度接着下;没有就当普通下载。
|
|
1148
|
+
if (resume)
|
|
1149
|
+
argv.push("-C", "-");
|
|
1150
|
+
argv.push("-o", partPath, url);
|
|
1151
|
+
return runCapture(subprocess, {
|
|
1152
|
+
argv,
|
|
1153
|
+
cwd: cacheDir,
|
|
1154
|
+
timeoutMs,
|
|
1155
|
+
maxBytes: 64 * 1024,
|
|
1156
|
+
...(signal === undefined ? {} : { signal }),
|
|
1157
|
+
});
|
|
1158
|
+
}
|
|
1159
|
+
/**
|
|
1160
|
+
* 按候选链下载安装包(F3a):一个源不通就换下一个,全都不通才报错。
|
|
1161
|
+
*
|
|
1162
|
+
* 换源时**一定从零开始**(先删 .part):镜像与 GitHub 的包体字节数不同(同步时点不同),
|
|
1163
|
+
* 把 A 源的前半段接在 B 源的后半段上,拼出来的 exe 一定解不开。
|
|
1164
|
+
* 同一个源内部的续传是安全的:curl -C - 续的是同一条线路上的同一个文件。
|
|
1165
|
+
*/
|
|
1166
|
+
async function downloadInstaller(subprocess, urls, partPath, cacheDir, request, timeoutMs) {
|
|
1167
|
+
const attempts = [];
|
|
1168
|
+
for (const [index, url] of urls.entries()) {
|
|
1169
|
+
if (isAborted(request.signal))
|
|
1170
|
+
return { ok: false, bytes: 0, url: "", attempts };
|
|
1171
|
+
request.onProgress?.("下载 " + formatBytes(MSYS2_INSTALLER_BYTES) + "(" +
|
|
1172
|
+
installerSourceLabel(url) + ",第 " + String(index + 1) + "/" + String(urls.length) + " 个候选):" + url);
|
|
1173
|
+
removeFileQuietly(partPath);
|
|
1174
|
+
let outcome = await curlOnce(subprocess, url, partPath, cacheDir, timeoutMs, true, request.signal);
|
|
1175
|
+
// 33 = 服务器不支持字节范围,续传请求被拒;去掉 -C - 再来一次(从头下)。
|
|
1176
|
+
if (outcome.exitCode === 33) {
|
|
1177
|
+
removeFileQuietly(partPath);
|
|
1178
|
+
outcome = await curlOnce(subprocess, url, partPath, cacheDir, timeoutMs, false, request.signal);
|
|
1179
|
+
}
|
|
1180
|
+
const bytes = fileSize(partPath);
|
|
1181
|
+
if (outcome.spawnError !== null) {
|
|
1182
|
+
attempts.push(installerSourceLabel(url) + " → curl 起不来:" + outcome.spawnError);
|
|
1183
|
+
removeFileQuietly(partPath);
|
|
1184
|
+
continue;
|
|
1185
|
+
}
|
|
1186
|
+
if (outcome.timedOut) {
|
|
1187
|
+
attempts.push(installerSourceLabel(url) + " → 超时(" + String(Math.round(timeoutMs / 1000)) + " s," + formatBytes(bytes) + ")");
|
|
1188
|
+
removeFileQuietly(partPath);
|
|
1189
|
+
continue;
|
|
1190
|
+
}
|
|
1191
|
+
if (outcome.exitCode !== 0) {
|
|
1192
|
+
const detail = lastLine(outcome.stderr) || lastLine(outcome.stdout) || "(无输出)";
|
|
1193
|
+
// 28 是 --speed-time 触发的"太慢",单独说清楚,否则用户只看到一个退出码。
|
|
1194
|
+
const kind = outcome.exitCode === 28 ? "太慢(10 s 内不足 1 KB/s)" : "退出码 " + String(outcome.exitCode);
|
|
1195
|
+
attempts.push(installerSourceLabel(url) + " → " + kind + ":" + detail);
|
|
1196
|
+
removeFileQuietly(partPath);
|
|
1197
|
+
continue;
|
|
1198
|
+
}
|
|
1199
|
+
if (bytes < MIN_INSTALLER_BYTES) {
|
|
1200
|
+
attempts.push(installerSourceLabel(url) + " → 只下到 " + formatBytes(bytes) + "(小于 " + formatBytes(MIN_INSTALLER_BYTES) + ",像半截文件或错误页)");
|
|
1201
|
+
removeFileQuietly(partPath);
|
|
1202
|
+
continue;
|
|
1203
|
+
}
|
|
1204
|
+
if (!looksLikeExecutable(partPath)) {
|
|
1205
|
+
attempts.push(installerSourceLabel(url) + " → 下回来的开头不是 MZ(不是 exe)");
|
|
1206
|
+
removeFileQuietly(partPath);
|
|
1207
|
+
continue;
|
|
1208
|
+
}
|
|
1209
|
+
return { ok: true, bytes, url, attempts };
|
|
1210
|
+
}
|
|
1211
|
+
removeFileQuietly(partPath);
|
|
1212
|
+
return { ok: false, bytes: 0, url: "", attempts };
|
|
1213
|
+
}
|
|
961
1214
|
export async function planOrInstallMsys2(subprocess, request, env = process.env) {
|
|
962
1215
|
const started = Date.now();
|
|
963
1216
|
const configRead = readToolchainConfig(env);
|
|
@@ -966,15 +1219,18 @@ export async function planOrInstallMsys2(subprocess, request, env = process.env)
|
|
|
966
1219
|
const packages = dedupeStrings(request.packages !== undefined && request.packages.length > 0
|
|
967
1220
|
? request.packages
|
|
968
1221
|
: [...DEFAULT_MSYS2_PACKAGES]);
|
|
969
|
-
const installer =
|
|
1222
|
+
const installer = resolveInstallerUrls(env);
|
|
1223
|
+
// 计划卡片上只能写一个地址:写候选链的第一个(真装时按链逐个试)。
|
|
1224
|
+
const primaryUrl = installer.urls[0] ?? MSYS2_INSTALLER_URL;
|
|
970
1225
|
const cachePath = cacheFilePath(env);
|
|
971
|
-
|
|
972
|
-
const
|
|
1226
|
+
// F3d:缓存只认「MZ 头 + ≥40 MB」。半截文件被当成已下好,解压就会永远失败。
|
|
1227
|
+
const cachedBytes = cachedInstallerBytes(cachePath);
|
|
1228
|
+
const cachedOk = cachedBytes > 0;
|
|
973
1229
|
const msys64 = target.msys64;
|
|
974
1230
|
const binDir = msys64 === "" ? "" : msys64 + "/ucrt64/bin";
|
|
975
1231
|
const parent = msys64 === "" ? "" : toPosix(dirname(msys64));
|
|
976
1232
|
let download = {
|
|
977
|
-
url:
|
|
1233
|
+
url: primaryUrl,
|
|
978
1234
|
bytes: cachedOk ? cachedBytes : MSYS2_INSTALLER_BYTES,
|
|
979
1235
|
cached: cachedOk,
|
|
980
1236
|
};
|
|
@@ -1084,7 +1340,7 @@ export async function planOrInstallMsys2(subprocess, request, env = process.env)
|
|
|
1084
1340
|
};
|
|
1085
1341
|
// 已有 MSYS2 时"下载 0 字节"才是实话:安装包根本不会碰。
|
|
1086
1342
|
if (existing)
|
|
1087
|
-
download = { url:
|
|
1343
|
+
download = { url: primaryUrl, bytes: 0, cached: cachedOk };
|
|
1088
1344
|
const decided = msys64 !== "";
|
|
1089
1345
|
const nothingToDo = decided && existing && missing.length === 0 && !pacmanUnknown;
|
|
1090
1346
|
// 什么都不用做时只回一句话:不列 7 个"已跳过"的步骤,也不贴 notes。
|
|
@@ -1125,7 +1381,7 @@ export async function planOrInstallMsys2(subprocess, request, env = process.env)
|
|
|
1125
1381
|
binDir,
|
|
1126
1382
|
packages,
|
|
1127
1383
|
cachePath,
|
|
1128
|
-
cacheUrl:
|
|
1384
|
+
cacheUrl: primaryUrl,
|
|
1129
1385
|
configPath: configRead.path,
|
|
1130
1386
|
writePath: request.writePath,
|
|
1131
1387
|
});
|
|
@@ -1173,7 +1429,7 @@ export async function planOrInstallMsys2(subprocess, request, env = process.env)
|
|
|
1173
1429
|
binDir,
|
|
1174
1430
|
packages,
|
|
1175
1431
|
cachePath,
|
|
1176
|
-
cacheUrl:
|
|
1432
|
+
cacheUrl: primaryUrl,
|
|
1177
1433
|
configPath: configRead.path,
|
|
1178
1434
|
writePath: request.writePath,
|
|
1179
1435
|
});
|
|
@@ -1239,7 +1495,9 @@ export async function planOrInstallMsys2(subprocess, request, env = process.env)
|
|
|
1239
1495
|
}
|
|
1240
1496
|
else {
|
|
1241
1497
|
try {
|
|
1242
|
-
|
|
1498
|
+
const mkdirError = ensureParentDir(parent);
|
|
1499
|
+
if (mkdirError !== "")
|
|
1500
|
+
throw new Error(mkdirError + "(父目录 " + parent + ")");
|
|
1243
1501
|
if (existsSync(msys64)) {
|
|
1244
1502
|
const entries = safeList(msys64);
|
|
1245
1503
|
if (entries.length > 0) {
|
|
@@ -1258,7 +1516,16 @@ export async function planOrInstallMsys2(subprocess, request, env = process.env)
|
|
|
1258
1516
|
const reason = describeError(error);
|
|
1259
1517
|
setStep(prepareStep, "failed", reason);
|
|
1260
1518
|
skipAfter(steps, "prepare", "父目录建不了,未执行");
|
|
1261
|
-
|
|
1519
|
+
// 0c:失败时给一条不依赖本工具的手动兜底——下载好的 sfx 安装包自己就能解压,
|
|
1520
|
+
// 手动装好后重跑本工具会直接走 alreadyInstalled(检测的是 usr/bin/bash.exe,不是本工具写的标记)。
|
|
1521
|
+
const manualSteps = [
|
|
1522
|
+
"手动兜底(不依赖本工具):",
|
|
1523
|
+
" 1. 下载安装包(任一源都行):" + primaryUrl,
|
|
1524
|
+
" 2. 手动解压到目标盘:进入下载目录后执行 <安装包文件> -y -o" + (parent === "" ? "C:\\\\" : toWindowsDir(parent)),
|
|
1525
|
+
" (sfx 会在所选目录下解压出 msys64/;把它改名或移动到 " + msys64 + " 即可)",
|
|
1526
|
+
" 3. 重跑本工具:它会检测到 usr/bin/bash.exe 并直接复用,不再下载、不再解压。",
|
|
1527
|
+
].join("\n");
|
|
1528
|
+
return finish(false, "没能准备安装目录:" + reason, "检查路径是否合法、有没有写权限,或换一个 targetDir。\n" + manualSteps);
|
|
1262
1529
|
}
|
|
1263
1530
|
}
|
|
1264
1531
|
// b) 下载安装包
|
|
@@ -1269,7 +1536,7 @@ export async function planOrInstallMsys2(subprocess, request, env = process.env)
|
|
|
1269
1536
|
setStep(downloadStep, "skipped", "目标里已有 MSYS2,不需要安装包");
|
|
1270
1537
|
}
|
|
1271
1538
|
else if (cachedOk) {
|
|
1272
|
-
download = { url:
|
|
1539
|
+
download = { url: primaryUrl, bytes: cachedBytes, cached: true };
|
|
1273
1540
|
setStep(downloadStep, "done", "复用缓存:" + cachePath + "(" + formatBytes(cachedBytes) + ")");
|
|
1274
1541
|
}
|
|
1275
1542
|
else {
|
|
@@ -1285,43 +1552,22 @@ export async function planOrInstallMsys2(subprocess, request, env = process.env)
|
|
|
1285
1552
|
skipAfter(steps, "download", "缓存目录不可用,未执行");
|
|
1286
1553
|
return finish(false, "没能下载安装包:" + reason, "换一个可写目录后重试。");
|
|
1287
1554
|
}
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
"
|
|
1294
|
-
"
|
|
1295
|
-
|
|
1296
|
-
"-o",
|
|
1297
|
-
partPath,
|
|
1298
|
-
installer.url,
|
|
1299
|
-
],
|
|
1300
|
-
cwd: cacheDir,
|
|
1301
|
-
timeoutMs,
|
|
1302
|
-
maxBytes: 64 * 1024,
|
|
1303
|
-
...(request.signal === undefined ? {} : { signal: request.signal }),
|
|
1304
|
-
});
|
|
1305
|
-
const downloadedBytes = fileSize(partPath);
|
|
1306
|
-
if (outcome.spawnError !== null ||
|
|
1307
|
-
outcome.timedOut ||
|
|
1308
|
-
outcome.exitCode !== 0 ||
|
|
1309
|
-
downloadedBytes === 0) {
|
|
1310
|
-
removeFileQuietly(partPath);
|
|
1311
|
-
const reason = outcome.spawnError !== null
|
|
1312
|
-
? "curl 起不来:" + outcome.spawnError
|
|
1313
|
-
: outcome.timedOut
|
|
1314
|
-
? "下载超时(" + String(Math.round(timeoutMs / 1000)) + " s)"
|
|
1315
|
-
: outcome.exitCode !== 0
|
|
1316
|
-
? "curl 退出码 " + String(outcome.exitCode) + ":" + (lastLine(outcome.stderr) || "(无输出)")
|
|
1317
|
-
: "下载回来是空文件";
|
|
1318
|
-
setStep(downloadStep, "failed", reason);
|
|
1555
|
+
const fetched = await downloadInstaller(subprocess, installer.urls, partPath, cacheDir, request, timeoutMs);
|
|
1556
|
+
if (isAborted(request.signal))
|
|
1557
|
+
return abortRest(downloadStep);
|
|
1558
|
+
if (!fetched.ok) {
|
|
1559
|
+
const reason = fetched.attempts.length === 0
|
|
1560
|
+
? "一个候选源都没试(已取消)"
|
|
1561
|
+
: fetched.attempts.join(";");
|
|
1562
|
+
setStep(downloadStep, "failed", "所有候选源都没下下来:" + reason);
|
|
1319
1563
|
skipAfter(steps, "download", "安装包没下来,未执行");
|
|
1320
|
-
return finish(false, "
|
|
1321
|
-
installer.
|
|
1322
|
-
"
|
|
1564
|
+
return finish(false, "下载安装包失败:试过 " +
|
|
1565
|
+
String(installer.urls.length) +
|
|
1566
|
+
" 个候选源(国内镜像优先,GitHub 兜底)都不通。", "逐源原因:\n " +
|
|
1567
|
+
fetched.attempts.join("\n ") +
|
|
1568
|
+
"\n可以自己下到 " +
|
|
1323
1569
|
cachePath +
|
|
1324
|
-
"
|
|
1570
|
+
" 再重试(有效缓存会直接复用,不重新下载);内网/代理可在 MSYS2_MIRROR 或 MSYS2_INSTALLER_URL 里指定一个能用的源。");
|
|
1325
1571
|
}
|
|
1326
1572
|
try {
|
|
1327
1573
|
renameSync(partPath, cachePath);
|
|
@@ -1333,8 +1579,8 @@ export async function planOrInstallMsys2(subprocess, request, env = process.env)
|
|
|
1333
1579
|
skipAfter(steps, "download", "安装包落位失败,未执行");
|
|
1334
1580
|
return finish(false, "安装包下载完成但没法落位:" + reason, "检查缓存目录权限后重试。");
|
|
1335
1581
|
}
|
|
1336
|
-
download = { url:
|
|
1337
|
-
setStep(downloadStep, "done", "
|
|
1582
|
+
download = { url: fetched.url, bytes: fileSize(cachePath), cached: false };
|
|
1583
|
+
setStep(downloadStep, "done", "已从 " + installerSourceLabel(fetched.url) + " 下载:" + cachePath + "(" + formatBytes(download.bytes) + ")");
|
|
1338
1584
|
}
|
|
1339
1585
|
// c) 静默解压
|
|
1340
1586
|
beginStep(steps, extractStep, request.onProgress);
|
|
@@ -1359,8 +1605,33 @@ export async function planOrInstallMsys2(subprocess, request, env = process.env)
|
|
|
1359
1605
|
skipAfter(steps, "extract", "解压没成功,未执行");
|
|
1360
1606
|
return finish(false, "解压失败:" + extracted.detail, "安装包已缓存在 " + cachePath + ",修好问题后重试不会重新下载;也可以手动把它解压到 " + msys64 + "。");
|
|
1361
1607
|
}
|
|
1608
|
+
// F3d 第三道:解压自检。sfx 退出码 0 不代表解出来的是完整 MSYS2——
|
|
1609
|
+
// 半截安装包有时也能压出一半目录,缺 usr/bin/bash.exe 就等于白装。
|
|
1610
|
+
if (!existsSync(msys64 + "/usr/bin/bash.exe")) {
|
|
1611
|
+
setStep(extractStep, "failed", "解压完了但 " + msys64 + "/usr/bin/bash.exe 不存在,这份 MSYS2 不完整");
|
|
1612
|
+
// 缓存大概率是坏的:改名成 .broken 而不是删掉,留证据也留在用户手里;下次会重新下载。
|
|
1613
|
+
try {
|
|
1614
|
+
if (existsSync(cachePath))
|
|
1615
|
+
renameSync(cachePath, cachePath + ".broken");
|
|
1616
|
+
}
|
|
1617
|
+
catch {
|
|
1618
|
+
// 改名失败也不影响结论:下次同样会走到这里。
|
|
1619
|
+
}
|
|
1620
|
+
skipAfter(steps, "extract", "MSYS2 本体不完整,未执行");
|
|
1621
|
+
return finish(false, "解压出来的 MSYS2 不完整(缺 usr/bin/bash.exe):" + msys64, "坏掉的安装包已改名成 " + cachePath + ".broken(没有删,想留证据就留着);重跑一次会重新下载并重装," +
|
|
1622
|
+
"也可以手动把这个安装包解压到 " + msys64 + "。");
|
|
1623
|
+
}
|
|
1362
1624
|
setStep(extractStep, "done", "已解压到 " + msys64);
|
|
1363
1625
|
}
|
|
1626
|
+
// c2) 把国内镜像放到 pacman 列表最前(F3b)。只对刚解压出来的这份生效:
|
|
1627
|
+
// 已有的 MSYS2 是用户自己的环境,改人家的 pacman 配置不在本工具的授权范围内。
|
|
1628
|
+
if (!alreadyInstalled) {
|
|
1629
|
+
const mirrors = preferChinaMirrors(msys64);
|
|
1630
|
+
if (mirrors.note !== "") {
|
|
1631
|
+
notes.push(mirrors.note);
|
|
1632
|
+
request.onProgress?.(mirrors.note);
|
|
1633
|
+
}
|
|
1634
|
+
}
|
|
1364
1635
|
// d) pacman 补包
|
|
1365
1636
|
beginStep(steps, pacmanStep, request.onProgress);
|
|
1366
1637
|
if (isAborted(request.signal))
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L2:工程交付(export)—— 把「能编译的工程」变成「别人拿到就能用」的一个 zip。
|
|
3
|
+
*
|
|
4
|
+
* 两条铁律(P1-4):
|
|
5
|
+
* 1. build/ 绝不进包:CMakeCache.txt / build.ninja / compile_commands.json 里全是
|
|
6
|
+
* 本机绝对路径,拷到别人机器上 cmake 退出码 0 但产物是旧的(实测),比不带还糟。
|
|
7
|
+
* 2. 两种意图分开:「拿到就能烧」带 prebuilt/(elf/hex/bin),「拿到就能改」绝不能带 build 缓存。
|
|
8
|
+
*/
|
|
9
|
+
import { existsSync, readdirSync, statSync } from "node:fs";
|
|
10
|
+
import { basename, join, resolve } from "node:path";
|
|
11
|
+
import { detectBuildDir, findElf } from "./build.js";
|
|
12
|
+
/** 进包的排除目录:构建缓存(绝对路径雷区)、插件备份、git、node_modules。 */
|
|
13
|
+
const SKIP_DIRS = new Set(["build", ".dsh-stm32", "node_modules", ".git"]);
|
|
14
|
+
function collectSources(projectDir) {
|
|
15
|
+
const sources = new Map();
|
|
16
|
+
const walk = (dir, relDir) => {
|
|
17
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
18
|
+
const rel = relDir === "" ? entry.name : relDir + "/" + entry.name;
|
|
19
|
+
if (entry.isDirectory()) {
|
|
20
|
+
if (SKIP_DIRS.has(entry.name))
|
|
21
|
+
continue;
|
|
22
|
+
walk(join(dir, entry.name), rel);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
sources.set(rel, join(dir, entry.name));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
walk(projectDir, "");
|
|
30
|
+
return sources;
|
|
31
|
+
}
|
|
32
|
+
function readmeText(projectName, prebuilt, includePrebuilt) {
|
|
33
|
+
const lines = [
|
|
34
|
+
"# " + projectName,
|
|
35
|
+
"",
|
|
36
|
+
"由 dsh-stm32 插件导出(stm32_project_export)。",
|
|
37
|
+
"",
|
|
38
|
+
"## 拿到就能改",
|
|
39
|
+
"",
|
|
40
|
+
"用 CMake 正常配置编译即可(VS Code CMake Tools 或 cmake --preset Debug)。",
|
|
41
|
+
"",
|
|
42
|
+
"build/ 不在包里是有意的:CMakeCache.txt 记录的是原机器的绝对路径,",
|
|
43
|
+
"拷贝过去的 build 目录会让 cmake 静默用旧缓存编译出旧产物。",
|
|
44
|
+
"",
|
|
45
|
+
];
|
|
46
|
+
if (includePrebuilt && prebuilt.length > 0) {
|
|
47
|
+
lines.push("## 拿到就能烧(prebuilt/)");
|
|
48
|
+
lines.push("");
|
|
49
|
+
// 包里这些文件在 prebuilt/ 下,README 里的路径要对得上(别让人按裸文件名去找)。
|
|
50
|
+
for (const name of prebuilt)
|
|
51
|
+
lines.push("- prebuilt/" + name);
|
|
52
|
+
lines.push("");
|
|
53
|
+
lines.push("用 STM32CubeProgrammer / J-Flash / openocd 直接烧;改过源码后请重新编译再烧。");
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
lines.push("## 烧录");
|
|
57
|
+
lines.push("");
|
|
58
|
+
lines.push("包里没带预编译产物:先编译得到 .elf 再烧。");
|
|
59
|
+
}
|
|
60
|
+
return lines.join("\n");
|
|
61
|
+
}
|
|
62
|
+
export async function runExport(request) {
|
|
63
|
+
const startedAt = Date.now();
|
|
64
|
+
const projectDir = resolve(request.projectDir);
|
|
65
|
+
const projectName = basename(projectDir);
|
|
66
|
+
const fail = (message) => ({
|
|
67
|
+
ok: false, projectDir, zipPath: "", zipBytes: 0, fileCount: 0, prebuilt: [],
|
|
68
|
+
durationMs: Date.now() - startedAt, message, notes: [],
|
|
69
|
+
});
|
|
70
|
+
const cancelled = () => request.signal !== undefined && request.signal.aborted;
|
|
71
|
+
if (!existsSync(projectDir))
|
|
72
|
+
return fail("工程目录不存在:" + projectDir);
|
|
73
|
+
if (cancelled())
|
|
74
|
+
return fail("已取消:没有生成 zip(连文件都没开始收集)。");
|
|
75
|
+
const includePrebuilt = request.includePrebuilt === true;
|
|
76
|
+
const prebuilt = [];
|
|
77
|
+
const buildDir = detectBuildDir(projectDir) ?? "";
|
|
78
|
+
if (includePrebuilt && buildDir !== "") {
|
|
79
|
+
const elf = findElf(buildDir);
|
|
80
|
+
if (elf !== null) {
|
|
81
|
+
prebuilt.push({ path: elf.path, size: elf.size });
|
|
82
|
+
for (const ext of ["hex", "bin"]) {
|
|
83
|
+
const candidate = elf.path.replace(/\.elf$/u, "") + "." + ext;
|
|
84
|
+
if (existsSync(candidate))
|
|
85
|
+
prebuilt.push({ path: candidate, size: statSync(candidate).size });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const sources = collectSources(projectDir);
|
|
90
|
+
for (const item of prebuilt)
|
|
91
|
+
sources.set("prebuilt/" + basename(item.path), item.path);
|
|
92
|
+
const notes = [];
|
|
93
|
+
notes.push("build/ 未进包:CMakeCache.txt 里是本机绝对路径,拷贝过去会静默复用旧缓存。");
|
|
94
|
+
if (includePrebuilt && prebuilt.length === 0)
|
|
95
|
+
notes.push("要求带 prebuilt 但构建目录里没有 .elf:先 stm32_build 再导出。");
|
|
96
|
+
if (request.readme !== false) {
|
|
97
|
+
const text = readmeText(projectName, prebuilt.map((item) => basename(item.path)), includePrebuilt);
|
|
98
|
+
sources.set("README.md", "__inline__\n" + text);
|
|
99
|
+
}
|
|
100
|
+
if (cancelled())
|
|
101
|
+
return fail("已取消:没有生成 zip(工程里 " + String(sources.size) + " 个文件已读完,但没写盘)。");
|
|
102
|
+
const outDir = request.outDir === undefined ? resolve(projectDir, "..") : resolve(request.outDir);
|
|
103
|
+
const zipPath = join(outDir, projectName + "-export.zip");
|
|
104
|
+
const zipImpl = request.zipImpl ??
|
|
105
|
+
(async (out, srcs) => {
|
|
106
|
+
const { makeZip } = await import("./zip.js");
|
|
107
|
+
await makeZip(out, srcs);
|
|
108
|
+
});
|
|
109
|
+
try {
|
|
110
|
+
await zipImpl(zipPath, sources);
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
return fail("打包失败:" + (error instanceof Error ? error.message : String(error)));
|
|
114
|
+
}
|
|
115
|
+
let zipBytes = 0;
|
|
116
|
+
try {
|
|
117
|
+
zipBytes = statSync(zipPath).size;
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
// 注入的 zipImpl 可能没真写文件(测试替身);报告里的字节数只是给人看的,不该让它炸掉。
|
|
121
|
+
zipBytes = 0;
|
|
122
|
+
}
|
|
123
|
+
const message = "导出完成:" + zipPath + "(" + String(zipBytes) + " 字节、" + String(sources.size) + " 个文件" +
|
|
124
|
+
(includePrebuilt ? ",含 prebuilt/" + String(prebuilt.length) + " 个产物" : "") + ")。";
|
|
125
|
+
return {
|
|
126
|
+
ok: true, projectDir, zipPath, zipBytes, fileCount: sources.size, prebuilt,
|
|
127
|
+
durationMs: Date.now() - startedAt, message, notes,
|
|
128
|
+
};
|
|
129
|
+
}
|