Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(build): ship the x64 OpenMP runtime with the arm64 installer's whisper fallback

Christian Schmittel5b416a27cffc
diff --git a/scripts/before-pack.cjs b/scripts/before-pack.cjs
index 7cedb2921..f71802635 100644
--- a/scripts/before-pack.cjs
+++ b/scripts/before-pack.cjs
@@ -381,6 +381,19 @@ function importedDlls(file) {
return names;
}
+/** IMAGE_FILE_HEADER.Machine, as the arch tag of the `win32-<arch>` directories. */
+const PE_MACHINE = { 0x8664: "x64", 0xaa64: "arm64" };
+
+/** The architecture a PE binary is built for, or undefined for a machine we do not ship. */
+function peArch(file) {
+ const b = fs.readFileSync(file);
+ const notPe = () => new Error(`${file} is not a PE binary, or is truncated`);
+ if (b.length < 0x40 || b.readUInt16LE(0) !== 0x5a4d) throw notPe();
+ const pe = b.readUInt32LE(0x3c);
+ if (pe + 6 > b.length || b.readUInt32LE(pe) !== 0x00004550) throw notPe();
+ return PE_MACHINE[b.readUInt16LE(pe + 4)];
+}
+
/**
* Nothing we ship may depend on the Visual C++ Redistributable.
*
@@ -445,24 +458,62 @@ function checkWinNoRedistDependency(dir) {
bad: entry.imports.filter((d) => VC_REDIST_DLL.test(d) && !shipped.has(d.toLowerCase())),
}))
.filter((entry) => entry.bad.length > 0);
- if (offenders.length === 0) {
+ if (offenders.length > 0) {
+ throw new Error(
+ "Refusing to package binaries that need the Visual C++ Redistributable.\n\n" +
+ ` looked in: ${path.relative(ROOT, dir)}\n\n` +
+ `${offenders.map((o) => ` - ${o.name} imports ${o.bad.join(", ")}`).join("\n")}\n\n` +
+ "Those DLLs are not part of Windows. On a clean image the loader kills the process\n" +
+ "before main() (0xC0000135) or fails require(), and the app can only report an exit\n" +
+ "code. It works on every developer machine, which is why this is checked here.\n\n" +
+ "Build against the static CRT instead:\n" +
+ " - CMake helpers: CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded (electron/native/wgc-capture)\n" +
+ " - Rust addon: -C target-feature=+crt-static (crates/.cargo/config.toml)\n\n" +
+ "For a prebuilt binary that is not ours to recompile, ship the DLL it needs into this\n" +
+ "same directory and the check passes — that is what scripts/stage-vcomp-runtime.mjs\n" +
+ "does for the OpenMP runtime the whisper/ggml libraries import.",
+ );
+ }
+
+ // A colocated copy only counts when the loader can use it, and the name check above
+ // cannot tell: the Redist tree holds an x64 and an arm64 vcomp140.dll under the same
+ // name, and either one satisfies `shipped`. The x64 loader refuses the arm64 copy
+ // with the same 0xC0000135 as a missing file.
+ //
+ // Only the copies something here imports. The arm64 Redist folder carries a
+ // vcruntime140_1.dll whose header says x64 — it is the ARM64EC build Microsoft ships
+ // for emulated x64 code — and no ARM64 binary loads it, so its machine is irrelevant.
+ const dirArch = path.basename(dir).match(/^win32-(.+)$/)?.[1];
+ if (!dirArch) {
return;
}
+ const imported = new Set(scanned.flatMap((entry) => entry.imports.map((d) => d.toLowerCase())));
+ const wrongArch = files
+ .filter((name) => VC_REDIST_DLL.test(name) && imported.has(name.toLowerCase()))
+ .map((name) => ({ name, arch: peArch(path.join(dir, name)) }))
+ .filter((entry) => entry.arch !== dirArch);
+ if (wrongArch.length > 0) {
+ throw new Error(
+ "Refusing to package Visual C++ runtime DLLs built for the wrong architecture.\n\n" +
+ `${wrongArch.map((e) => ` - ${e.name} in ${path.basename(dir)} is built for ${e.arch ?? "an unknown machine"}`).join("\n")}\n\n` +
+ "The loader skips a DLL for another architecture as if it were absent. Stage the\n" +
+ `${dirArch} copy from VC\\Redist\\MSVC\\<version>\\${dirArch}\\ — scripts/stage-vcomp-runtime.mjs does.`,
+ );
+ }
+}
- throw new Error(
- "Refusing to package binaries that need the Visual C++ Redistributable.\n\n" +
- ` looked in: ${path.relative(ROOT, dir)}\n\n` +
- `${offenders.map((o) => ` - ${o.name} imports ${o.bad.join(", ")}`).join("\n")}\n\n` +
- "Those DLLs are not part of Windows. On a clean image the loader kills the process\n" +
- "before main() (0xC0000135) or fails require(), and the app can only report an exit\n" +
- "code. It works on every developer machine, which is why this is checked here.\n\n" +
- "Build against the static CRT instead:\n" +
- " - CMake helpers: CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded (electron/native/wgc-capture)\n" +
- " - Rust addon: -C target-feature=+crt-static (crates/.cargo/config.toml)\n\n" +
- "For a prebuilt binary that is not ours to recompile, ship the DLL it needs into this\n" +
- "same directory and the check passes — that is what scripts/stage-vcomp-runtime.mjs\n" +
- "does for the OpenMP runtime the whisper/ggml libraries import.",
- );
+/**
+ * Every `win32-*` directory, because that is what ships: the extraResources filter is
+ * `win32-*` / `*`, not the target's directory alone. The arm64 installer carries
+ * win32-x64 for the whisper helper that runs under emulation, and checking only
+ * win32-arm64 let that helper ship without the OpenMP runtime its ggml libraries import.
+ */
+function checkWinShippedRedist(binDir) {
+ for (const entry of fs.readdirSync(binDir, { withFileTypes: true })) {
+ if (entry.isDirectory() && /^win32-/.test(entry.name)) {
+ checkWinNoRedistDependency(path.join(binDir, entry.name));
+ }
+ }
}
function checkWinNativePayload(context) {
@@ -474,7 +525,7 @@ function checkWinNativePayload(context) {
bundleNoun: "the installer",
emptyDirFix: `${FIX}\n\nThe STT helper and the capture helper are separate builds — see\ntechnical-documentation/engineering/build-and-packaging.md.`,
});
- checkWinNoRedistDependency(dir);
+ checkWinShippedRedist(path.dirname(dir));
}
function checkMacNativePayload(context) {
@@ -806,6 +857,7 @@ exports.__testing = {
MAC_MIN_OS_FLOOR,
MAC_REQUIRED,
checkNativePayload,
+ checkWinShippedRedist,
};
/** Every ELF under `dir`, recursively — the helper's ffmpeg sits in a subdirectory. */
diff --git a/scripts/before-pack.test.mjs b/scripts/before-pack.test.mjs
index a0c356894..6abd46a8c 100644
--- a/scripts/before-pack.test.mjs
+++ b/scripts/before-pack.test.mjs
@@ -101,7 +101,7 @@ describe("symbol-version ceiling", () => {
// The parser is separately cross-checked against the real thing — on a machine with a
// staged macOS payload, every Mach-O in it agreed with `vtool -show-build` (44/44).
-import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { declaredAppVersionFrom } from "./macos-floor.mjs";
@@ -357,3 +357,105 @@ describe("MAC_REQUIRED", () => {
});
});
});
+
+// The Windows counterpart. A synthetic PE32+ carrying only what importedDlls() reads: the
+// COFF machine, one section, and an import directory naming `imports`. The tests run on
+// the Linux CI, where no real Windows binary exists to point the guard at.
+const MACHINE = { x64: 0x8664, arm64: 0xaa64 };
+
+function pe(machine, imports) {
+ const pe = 0x40;
+ const opt = pe + 24;
+ const dirs = opt + 112;
+ const sectionTable = dirs + 16 * 8;
+ const raw = 0x200;
+ const va = 0x1000;
+ const descriptors = (imports.length + 1) * 20;
+ const names = imports.map((name) => Buffer.from(`${name}\0`, "latin1"));
+ const b = Buffer.alloc(raw + descriptors + names.reduce((n, name) => n + name.length, 0));
+ b.writeUInt16LE(0x5a4d, 0); // "MZ"
+ b.writeUInt32LE(pe, 0x3c);
+ b.writeUInt32LE(0x00004550, pe); // "PE\0\0"
+ b.writeUInt16LE(machine, pe + 4);
+ b.writeUInt16LE(1, pe + 6); // NumberOfSections
+ b.writeUInt16LE(sectionTable - opt, pe + 20); // SizeOfOptionalHeader
+ b.writeUInt16LE(0x20b, opt); // PE32+
+ b.writeUInt32LE(16, dirs - 4); // NumberOfRvaAndSizes
+ b.writeUInt32LE(va, dirs + 8); // import directory RVA
+ b.writeUInt32LE(va, sectionTable + 12); // VirtualAddress
+ b.writeUInt32LE(b.length - raw, sectionTable + 16); // VirtualSize
+ b.writeUInt32LE(raw, sectionTable + 20); // PointerToRawData
+ let at = raw + descriptors;
+ names.forEach((name, i) => {
+ b.writeUInt32LE(va + (at - raw), raw + i * 20 + 12); // IMAGE_IMPORT_DESCRIPTOR.Name
+ name.copy(b, at);
+ at += name.length;
+ });
+ return b;
+}
+
+/** A bin/ directory holding one `win32-<arch>` folder per key, the way electron-builder sees it. */
+function withWinBin(dirs, body) {
+ const bin = mkdtempSync(path.join(tmpdir(), "openscreen-winbin-"));
+ try {
+ for (const [tag, files] of Object.entries(dirs)) {
+ mkdirSync(path.join(bin, tag));
+ for (const [name, bytes] of Object.entries(files)) {
+ writeFileSync(path.join(bin, tag, name), bytes);
+ }
+ }
+ return body(bin);
+ } finally {
+ rmSync(bin, { recursive: true, force: true });
+ }
+}
+
+describe("checkWinShippedRedist", () => {
+ // The arm64 installer as CI builds it: the native payload, plus the x64 whisper
+ // helper that runs under emulation until a native one is staged. `win32-*/*` ships
+ // both directories.
+ const arm64Native = {
+ "onnxruntime.dll": pe(MACHINE.arm64, ["MSVCP140.dll", "KERNEL32.dll"]),
+ "msvcp140.dll": pe(MACHINE.arm64, ["KERNEL32.dll"]),
+ };
+ const x64Fallback = {
+ "whisper-stt-server.exe": pe(MACHINE.x64, ["ggml-base.dll", "KERNEL32.dll"]),
+ "ggml-base.dll": pe(MACHINE.x64, ["VCOMP140.DLL", "KERNEL32.dll"]),
+ };
+
+ it("refuses the arm64 package when its x64 fallback has no x64 OpenMP runtime", () => {
+ withWinBin({ "win32-arm64": arm64Native, "win32-x64": x64Fallback }, (bin) => {
+ expect(() => testing().checkWinShippedRedist(bin)).toThrow(
+ /win32-x64[\s\S]*ggml-base\.dll imports VCOMP140\.DLL/,
+ );
+ });
+ });
+
+ it("passes once the x64 runtime sits beside the x64 libraries", () => {
+ const x64 = { ...x64Fallback, "vcomp140.dll": pe(MACHINE.x64, ["KERNEL32.dll"]) };
+ withWinBin({ "win32-arm64": arm64Native, "win32-x64": x64 }, (bin) => {
+ expect(() => testing().checkWinShippedRedist(bin)).not.toThrow();
+ });
+ });
+
+ // The name check alone is satisfied by the wrong file: an ARM64 vcomp140.dll in
+ // win32-x64 is exactly the copy the x64 loader cannot use.
+ it("refuses a runtime DLL built for the other architecture", () => {
+ const x64 = { ...x64Fallback, "vcomp140.dll": pe(MACHINE.arm64, ["KERNEL32.dll"]) };
+ withWinBin({ "win32-arm64": arm64Native, "win32-x64": x64 }, (bin) => {
+ expect(() => testing().checkWinShippedRedist(bin)).toThrow(
+ /vcomp140\.dll in win32-x64 is built for arm64/,
+ );
+ });
+ });
+
+ // The arm64 Redist folder's vcruntime140_1.dll really does carry an x64 header (it is
+ // the ARM64EC build for emulated code). Nothing ARM64 imports it, so it must not fail
+ // the package — measured on the 1.13.0 arm64 installer, where it sits unused.
+ it("ignores a runtime DLL of another machine that nothing in the directory imports", () => {
+ const arm64 = { ...arm64Native, "vcruntime140_1.dll": pe(MACHINE.x64, ["KERNEL32.dll"]) };
+ withWinBin({ "win32-arm64": arm64 }, (bin) => {
+ expect(() => testing().checkWinShippedRedist(bin)).not.toThrow();
+ });
+ });
+});
diff --git a/scripts/stage-vcomp-runtime.mjs b/scripts/stage-vcomp-runtime.mjs
index 72c8b2fd9..4218fc2f9 100644
--- a/scripts/stage-vcomp-runtime.mjs
+++ b/scripts/stage-vcomp-runtime.mjs
@@ -50,8 +50,7 @@ const TARGET_ARCH = resolveTargetArch({
envArch: process.env.OPENSCREEN_WIN_HELPER_ARCH,
hostArch: process.arch,
});
-const ARCH_SEGMENT = TARGET_ARCH === "arm64" ? /\\arm64\\/i : /\\x64\\/i;
-const DEST_DIR = path.join(ROOT, "electron", "native", "bin", winBinDirName(TARGET_ARCH));
+const binDir = (arch) => path.join(ROOT, "electron", "native", "bin", winBinDirName(arch));
// Lower-case, because that is how they are compared against `readdirSync` names.
// vcomp140 lives in Microsoft.VC<nnn>.OpenMP, the other four in Microsoft.VC<nnn>.CRT —
// sibling directories under the same Redist tree, so one walk finds them all.
@@ -63,6 +62,17 @@ const DLLS = [
"vcruntime140_1.dll",
];
+// What goes where. The arm64 installer also ships win32-x64: until a native whisper
+// helper is staged, the x64 one runs there under emulation, and its ggml-base.dll and
+// ggml-cpu.dll import the x64 vcomp140.dll. The arm64 copy beside the native payload
+// cannot stand in for it — the x64 loader skips a DLL of another architecture as if it
+// were absent — so on a machine without the x64 Redistributable transcription died in
+// the loader. That is the only x64 import there; whisper-stt-server.exe needs no CRT.
+const STAGINGS = [{ arch: TARGET_ARCH, names: DLLS }];
+if (TARGET_ARCH === "arm64" && fs.existsSync(path.join(binDir("x64"), "ggml-base.dll"))) {
+ STAGINGS.push({ arch: "x64", names: ["vcomp140.dll"] });
+}
+
if (process.platform !== "win32") {
console.log("Skipping Visual C++ runtime staging: Windows-only.");
process.exit(0);
@@ -99,11 +109,18 @@ function searchRoots() {
];
}
-/** Candidate paths per DLL name, from ONE walk — the trees are large enough that
- * walking them once per name would be the slowest part of the build. */
+// The architecture is the directory right under the toolset version:
+// VC\Redist\MSVC\<version>\<arch>\Microsoft.VC<nnn>.{CRT,OpenMP}\.
+const REDIST_ARCH = /\\MSVC\\[\d.]+\\(x64|arm64)\\/i;
+const key = (arch, name) => `${arch}/${name}`;
+
+/** Candidate paths per arch and DLL name, from ONE walk — the trees are large enough
+ * that walking them once per name would be the slowest part of the build. */
function findRedistCopies() {
const wanted = new Set(DLLS);
- const found = new Map(DLLS.map((name) => [name, []]));
+ const found = new Map(
+ STAGINGS.flatMap(({ arch, names }) => names.map((name) => [key(arch, name), []])),
+ );
const walk = (dir, depth) => {
if (depth > 8) return;
let entries;
@@ -117,10 +134,12 @@ function findRedistCopies() {
const lower = entry.name.toLowerCase();
if (entry.isDirectory()) {
walk(full, depth + 1);
- } else if (wanted.has(lower) && /\\Redist\\/i.test(full) && ARCH_SEGMENT.test(full)) {
+ } else if (wanted.has(lower) && /\\Redist\\/i.test(full)) {
// `onecore\x64` is a trimmed variant for Windows Core headless SKUs; the
// desktop app wants the ordinary one.
- if (!/\\onecore\\/i.test(full)) found.get(lower).push(full);
+ const arch = full.match(REDIST_ARCH)?.[1].toLowerCase();
+ const bucket = arch && found.get(key(arch, lower));
+ if (bucket && !/\\onecore\\/i.test(full)) bucket.push(full);
}
}
};
@@ -145,11 +164,11 @@ const copies = findRedistCopies();
// Report every missing name at once. Staging four of five and failing on the fifth
// would send someone back through the same install-and-retry loop per DLL.
-const missing = DLLS.filter((name) => copies.get(name).length === 0);
+const missing = [...copies].filter(([, paths]) => paths.length === 0).map(([k]) => k);
if (missing.length > 0) {
throw new Error(
`Could not find a redistributable ${missing.join(", ")} under any Visual Studio installation.\n\n` +
- `They live in VC\\Redist\\MSVC\\<version>\\${TARGET_ARCH}\\ — vcomp140.dll under\n` +
+ "They live in VC\\Redist\\MSVC\\<version>\\<arch>\\ — vcomp140.dll under\n" +
"Microsoft.VC<nnn>.OpenMP, the rest under Microsoft.VC<nnn>.CRT.\n" +
"Install the Visual Studio C++ workload, which is required to build the native\n" +
"helpers anyway. Without these files the shipped whisper/ggml libraries and the\n" +
@@ -159,11 +178,13 @@ if (missing.length > 0) {
);
}
-fs.mkdirSync(DEST_DIR, { recursive: true });
-for (const name of DLLS) {
- const source = copies.get(name).sort(newestFirst)[0];
- const dest = path.join(DEST_DIR, name);
- fs.copyFileSync(source, dest);
- console.log(`Staged ${name} from ${source}`);
- console.log(` -> ${path.relative(ROOT, dest)}`);
+for (const { arch, names } of STAGINGS) {
+ fs.mkdirSync(binDir(arch), { recursive: true });
+ for (const name of names) {
+ const source = copies.get(key(arch, name)).sort(newestFirst)[0];
+ const dest = path.join(binDir(arch), name);
+ fs.copyFileSync(source, dest);
+ console.log(`Staged ${name} from ${source}`);
+ console.log(` -> ${path.relative(ROOT, dest)}`);
+ }
}