aboutsummaryrefslogtreecommitdiff
path: root/packages/bun-release/src/platform.ts
blob: 460f533e022755cd42c51033c6efb558e9db9def (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { spawn } from "./spawn";
import { read } from "./fs";
import { debug } from "./console";

export const os = process.platform;

export const arch = os === "darwin" && process.arch === "x64" && isRosetta2() ? "arm64" : process.arch;

export const avx = arch === "x64" && ((os === "linux" && isLinuxAVX()) || (os === "darwin" && isDarwinAVX()));

export type Platform = {
  os: string;
  arch: string;
  avx?: boolean;
  bin: string;
  exe: string;
};

export const platforms: Platform[] = [
  {
    os: "darwin",
    arch: "arm64",
    bin: "bun-darwin-aarch64",
    exe: "bin/bun",
  },
  {
    os: "darwin",
    arch: "x64",
    avx: 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",
    avx: 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.avx || avx))
  .sort((a, b) => (a.avx === b.avx ? 0 : a.avx ? -1 : 1));

function isLinuxAVX(): boolean {
  try {
    return read("/proc/cpuinfo").includes("avx");
  } catch (error) {
    debug("isLinuxAVX failed", error);
    return false;
  }
}

function isDarwinAVX(): boolean {
  try {
    const { exitCode, stdout } = spawn("sysctl", ["-n", "machdep.cpu"]);
    return exitCode === 0 && stdout.includes("AVX");
  } catch (error) {
    debug("isDarwinAVX 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) {
    debug("isRosetta2 failed", error);
    return false;
  }
}