diff options
Diffstat (limited to 'packages/bun-npm/src/platform.ts')
-rw-r--r-- | packages/bun-npm/src/platform.ts | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/packages/bun-npm/src/platform.ts b/packages/bun-npm/src/platform.ts new file mode 100644 index 000000000..a01cc3ddc --- /dev/null +++ b/packages/bun-npm/src/platform.ts @@ -0,0 +1,100 @@ +import { read, spawn } from "./util"; + +export const os = process.platform; + +export const arch = + os === "darwin" && process.arch === "x64" && isRosetta2() + ? "arm64" + : process.arch; + +export const avx2 = + (arch === "x64" && os === "linux" && isLinuxAVX2()) || + (os === "darwin" && isDarwinAVX2()); + +export type Platform = { + os: string; + arch: string; + avx2?: boolean; + bin: string; + exe: string; +}; + +export const platforms: Platform[] = [ + { + os: "darwin", + arch: "arm64", + bin: "bun-darwin-aarch64", + exe: "bin/bun", + }, + { + os: "darwin", + arch: "x64", + avx2: true, + bin: "bun-darwin-x64", + exe: "bin/bun", + }, + { + os: "darwin", + arch: "x64", + bin: "bun-darwin-x64-baseline", + exe: "bin/bun", + }, + { + os: "linux", + arch: "arm64", + bin: "bun-linux-aarch64", + exe: "bin/bun", + }, + { + os: "linux", + arch: "x64", + avx2: true, + bin: "bun-linux-x64", + exe: "bin/bun", + }, + { + os: "linux", + arch: "x64", + bin: "bun-linux-x64-baseline", + exe: "bin/bun", + }, +]; + +export const supportedPlatforms: Platform[] = platforms + .filter( + (platform) => + platform.os === os && platform.arch === arch && (!platform.avx2 || avx2), + ) + .sort((a, b) => (a.avx2 === b.avx2 ? 0 : a.avx2 ? -1 : 1)); + +function isLinuxAVX2(): boolean { + try { + return read("/proc/cpuinfo").includes("avx2"); + } catch (error) { + console.debug("isLinuxAVX2 failed", error); + return false; + } +} + +function isDarwinAVX2(): boolean { + try { + const { exitCode, stdout } = spawn("sysctl", ["-n", "machdep.cpu"]); + return exitCode === 0 && stdout.includes("AVX2"); + } catch (error) { + console.debug("isDarwinAVX2 failed", error); + return false; + } +} + +function isRosetta2(): boolean { + try { + const { exitCode, stdout } = spawn("sysctl", [ + "-n", + "sysctl.proc_translated", + ]); + return exitCode === 0 && stdout.includes("1"); + } catch (error) { + console.debug("isRosetta2 failed", error); + return false; + } +} |