Open History to see the last commit
1export type BuildArch = "arm64" | "x64" | "universal";
2export type BuildPlatform = "mac" | "linux" | "win";
3
4interface PlatformConfig {
5 readonly archChoices: ReadonlyArray<BuildArch>;
6}
7
8function normalizeWindowsArch(value: string | undefined): BuildArch | undefined {
9 const normalized = value?.trim().toLowerCase();
10 if (!normalized) return undefined;
11 if (normalized.includes("arm64") || normalized === "aarch64") return "arm64";
12 if (normalized.includes("amd64") || normalized.includes("x64")) return "x64";
13 return undefined;
14}
15
16export function resolveHostProcessArch(
17 platform: NodeJS.Platform,
18 processArch: NodeJS.Architecture,
19 env: NodeJS.ProcessEnv,
20): BuildArch | undefined {
21 if (processArch === "arm64") return "arm64";
22 if (processArch === "x64") {
23 if (platform !== "win32") return "x64";
24
25 // On Windows-on-Arm, x64 Node/Bun can run under emulation while the host
26 // still reports ARM64 via the processor environment variables.
27 return (
28 normalizeWindowsArch(env.PROCESSOR_ARCHITEW6432) ??
29 normalizeWindowsArch(env.PROCESSOR_ARCHITECTURE) ??
30 "x64"
31 );
32 }
33 return undefined;
34}
35
36export function getDefaultBuildArch(
37 platform: BuildPlatform,
38 processArch: NodeJS.Architecture,
39 env: NodeJS.ProcessEnv,
40 platformConfig: PlatformConfig,
41): BuildArch {
42 const hostPlatform: NodeJS.Platform =
43 platform === "win" ? "win32" : platform === "mac" ? "darwin" : "linux";
44 const hostArch = resolveHostProcessArch(hostPlatform, processArch, env);
45 if (hostArch && platformConfig.archChoices.includes(hostArch)) {
46 return hostArch;
47 }
48
49 return platformConfig.archChoices[0] ?? "x64";
50}