Skip to content

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

feat(build): add pure Windows helper arch-resolution module

Christian Schmittele00f7ae85e5f
diff --git a/scripts/windows-helper-arch.mjs b/scripts/windows-helper-arch.mjs
new file mode 100644
index 000000000..e9f0503a6
--- /dev/null
+++ b/scripts/windows-helper-arch.mjs
@@ -0,0 +1,62 @@
+// Pure helpers that map a target CPU architecture to the parameters the Windows
+// native-helper build needs. Kept side-effect free so it is unit-testable and
+// importable from both the build script and Vitest.
+
+export const SUPPORTED_ARCHES = ["x64", "arm64"];
+
+const ALIASES = new Map([
+ ["x64", "x64"],
+ ["amd64", "x64"],
+ ["x86_64", "x64"],
+ ["arm64", "arm64"],
+ ["aarch64", "arm64"],
+]);
+
+export function normalizeArch(value) {
+ if (value === undefined || value === null || value === "") {
+ return undefined;
+ }
+ return ALIASES.get(String(value).toLowerCase());
+}
+
+export function resolveTargetArch({ cliArch, envArch, hostArch } = {}) {
+ for (const [label, raw] of [
+ ["--arch", cliArch],
+ ["OPENSCREEN_WIN_HELPER_ARCH", envArch],
+ ]) {
+ if (raw !== undefined && raw !== null && raw !== "") {
+ const normalized = normalizeArch(raw);
+ if (!normalized) {
+ throw new Error(
+ `Invalid ${label} value "${raw}". Expected one of: ${SUPPORTED_ARCHES.join(", ")}.`,
+ );
+ }
+ return normalized;
+ }
+ }
+
+ const host = normalizeArch(hostArch);
+ if (!host) {
+ throw new Error(
+ `Unsupported host architecture "${hostArch}". Pass --arch with one of: ${SUPPORTED_ARCHES.join(", ")}.`,
+ );
+ }
+ return host;
+}
+
+export function resolveVcvarsArch(hostArch, targetArch) {
+ const host = normalizeArch(hostArch);
+ const target = normalizeArch(targetArch);
+ if (!host || !target) {
+ throw new Error(`Unsupported architecture pair host="${hostArch}" target="${targetArch}".`);
+ }
+ return host === target ? target : `${host}_${target}`;
+}
+
+export function winBinDirName(targetArch) {
+ const target = normalizeArch(targetArch);
+ if (!target) {
+ throw new Error(`Unsupported target architecture "${targetArch}".`);
+ }
+ return `win32-${target}`;
+}
diff --git a/scripts/windows-helper-arch.test.mjs b/scripts/windows-helper-arch.test.mjs
new file mode 100644
index 000000000..078620347
--- /dev/null
+++ b/scripts/windows-helper-arch.test.mjs
@@ -0,0 +1,71 @@
+import { describe, expect, it } from "vitest";
+import {
+ SUPPORTED_ARCHES,
+ normalizeArch,
+ resolveTargetArch,
+ resolveVcvarsArch,
+ winBinDirName,
+} from "./windows-helper-arch.mjs";
+
+describe("normalizeArch", () => {
+ it("maps known aliases to canonical arches", () => {
+ expect(normalizeArch("x64")).toBe("x64");
+ expect(normalizeArch("amd64")).toBe("x64");
+ expect(normalizeArch("x86_64")).toBe("x64");
+ expect(normalizeArch("arm64")).toBe("arm64");
+ expect(normalizeArch("aarch64")).toBe("arm64");
+ expect(normalizeArch("ARM64")).toBe("arm64");
+ });
+
+ it("returns undefined for empty or unknown values", () => {
+ expect(normalizeArch(undefined)).toBeUndefined();
+ expect(normalizeArch("")).toBeUndefined();
+ expect(normalizeArch("mips")).toBeUndefined();
+ });
+});
+
+describe("resolveTargetArch", () => {
+ it("prefers the CLI arch over env and host", () => {
+ expect(resolveTargetArch({ cliArch: "arm64", envArch: "x64", hostArch: "x64" })).toBe("arm64");
+ });
+
+ it("falls back to env when no CLI arch", () => {
+ expect(resolveTargetArch({ envArch: "arm64", hostArch: "x64" })).toBe("arm64");
+ });
+
+ it("defaults to the host arch when nothing explicit is given", () => {
+ expect(resolveTargetArch({ hostArch: "arm64" })).toBe("arm64");
+ expect(resolveTargetArch({ hostArch: "x64" })).toBe("x64");
+ });
+
+ it("throws on an invalid explicit arch", () => {
+ expect(() => resolveTargetArch({ cliArch: "mips", hostArch: "x64" })).toThrow(/Invalid/);
+ });
+
+ it("throws on an unsupported host with no explicit arch", () => {
+ expect(() => resolveTargetArch({ hostArch: "ppc64" })).toThrow(/host/i);
+ });
+});
+
+describe("resolveVcvarsArch", () => {
+ it("returns native tokens when host equals target", () => {
+ expect(resolveVcvarsArch("x64", "x64")).toBe("x64");
+ expect(resolveVcvarsArch("arm64", "arm64")).toBe("arm64");
+ });
+
+ it("returns cross tokens as <host>_<target>", () => {
+ expect(resolveVcvarsArch("x64", "arm64")).toBe("x64_arm64");
+ expect(resolveVcvarsArch("arm64", "x64")).toBe("arm64_x64");
+ });
+});
+
+describe("winBinDirName", () => {
+ it("builds the packaged bin folder name", () => {
+ expect(winBinDirName("x64")).toBe("win32-x64");
+ expect(winBinDirName("arm64")).toBe("win32-arm64");
+ });
+});
+
+it("exposes the supported arch list", () => {
+ expect(SUPPORTED_ARCHES).toEqual(["x64", "arm64"]);
+});