element-desktop/scripts/hak/target.ts

218 lines
5.6 KiB
TypeScript
Raw Normal View History

/*
2024-09-06 18:56:18 +02:00
Copyright 2024 New Vector Ltd.
Copyright 2021 The Matrix.org Foundation C.I.C.
2024-09-06 18:56:18 +02:00
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import { GLIBC, MUSL, familySync as processLibC } from "detect-libc";
// We borrow Rust's target naming scheme as a way of expressing all target
// details in a single string.
// See https://doc.rust-lang.org/rustc/platform-support.html.
export type TargetId =
2022-12-15 12:00:58 +01:00
| "aarch64-apple-darwin"
| "x86_64-apple-darwin"
| "universal-apple-darwin"
| "i686-pc-windows-msvc"
| "x86_64-pc-windows-msvc"
2023-03-22 10:41:51 +01:00
| "aarch64-pc-windows-msvc"
| "i686-unknown-freebsd"
| "x86_64-unknown-freebsd"
| "aarch64-unknown-freebsd"
2022-12-15 12:00:58 +01:00
| "i686-unknown-linux-musl"
| "i686-unknown-linux-gnu"
| "x86_64-unknown-linux-musl"
| "x86_64-unknown-linux-gnu"
| "aarch64-unknown-linux-musl"
| "aarch64-unknown-linux-gnu"
| "powerpc64le-unknown-linux-musl"
| "powerpc64le-unknown-linux-gnu";
// Values are expected to match those used in `process.platform`.
export type Platform = "darwin" | "freebsd" | "linux" | "win32";
// Values are expected to match those used in `process.arch`.
2022-12-15 12:00:58 +01:00
export type Arch = "arm64" | "ia32" | "x64" | "ppc64" | "universal";
// Values are expected to match those used by Visual Studio's `vcvarsall.bat`.
// See https://docs.microsoft.com/cpp/build/building-on-the-command-line?view=msvc-160#vcvarsall-syntax
2022-12-15 12:00:58 +01:00
export type VcVarsArch = "amd64" | "arm64" | "x86";
export type Target = {
id: TargetId;
platform: Platform;
arch: Arch;
};
export type WindowsTarget = Target & {
2022-12-15 12:00:58 +01:00
platform: "win32";
vcVarsArch: VcVarsArch;
};
export type LinuxTarget = Target & {
2022-12-15 12:00:58 +01:00
platform: "linux";
libC: typeof GLIBC | typeof MUSL;
};
export type UniversalTarget = Target & {
2022-12-15 12:00:58 +01:00
arch: "universal";
subtargets: Target[];
};
const aarch64AppleDarwin: Target = {
2022-12-15 12:00:58 +01:00
id: "aarch64-apple-darwin",
platform: "darwin",
arch: "arm64",
};
const x8664AppleDarwin: Target = {
2022-12-15 12:00:58 +01:00
id: "x86_64-apple-darwin",
platform: "darwin",
arch: "x64",
};
const universalAppleDarwin: UniversalTarget = {
2022-12-15 12:00:58 +01:00
id: "universal-apple-darwin",
platform: "darwin",
arch: "universal",
subtargets: [aarch64AppleDarwin, x8664AppleDarwin],
};
const i686PcWindowsMsvc: WindowsTarget = {
2022-12-15 12:00:58 +01:00
id: "i686-pc-windows-msvc",
platform: "win32",
arch: "ia32",
vcVarsArch: "x86",
};
const x8664PcWindowsMsvc: WindowsTarget = {
2022-12-15 12:00:58 +01:00
id: "x86_64-pc-windows-msvc",
platform: "win32",
arch: "x64",
vcVarsArch: "amd64",
};
2023-03-22 10:41:51 +01:00
const aarch64WindowsMsvc: WindowsTarget = {
2023-03-23 14:22:29 +01:00
id: "aarch64-pc-windows-msvc",
platform: "win32",
arch: "arm64",
vcVarsArch: "arm64",
2023-03-22 10:41:51 +01:00
};
const i686UnknownFreebsd: Target = {
id: "i686-unknown-freebsd",
platform: "freebsd",
arch: "ia32",
};
const x8664UnknownFreebsd: Target = {
id: "x86_64-unknown-freebsd",
platform: "freebsd",
arch: "x64",
};
const aarch64UnknownFreebsd: Target = {
id: "aarch64-unknown-freebsd",
platform: "freebsd",
arch: "arm64",
};
const x8664UnknownLinuxGnu: LinuxTarget = {
2022-12-15 12:00:58 +01:00
id: "x86_64-unknown-linux-gnu",
platform: "linux",
arch: "x64",
2022-09-23 13:14:09 +02:00
libC: GLIBC,
};
const x8664UnknownLinuxMusl: LinuxTarget = {
2022-12-15 12:00:58 +01:00
id: "x86_64-unknown-linux-musl",
platform: "linux",
arch: "x64",
2022-09-23 13:14:09 +02:00
libC: MUSL,
};
const i686UnknownLinuxGnu: LinuxTarget = {
2022-12-15 12:00:58 +01:00
id: "i686-unknown-linux-gnu",
platform: "linux",
arch: "ia32",
2022-09-23 13:14:09 +02:00
libC: GLIBC,
};
const i686UnknownLinuxMusl: LinuxTarget = {
2022-12-15 12:00:58 +01:00
id: "i686-unknown-linux-musl",
platform: "linux",
arch: "ia32",
2022-09-23 13:14:09 +02:00
libC: MUSL,
};
const aarch64UnknownLinuxGnu: LinuxTarget = {
2022-12-15 12:00:58 +01:00
id: "aarch64-unknown-linux-gnu",
platform: "linux",
arch: "arm64",
2022-09-23 13:14:09 +02:00
libC: GLIBC,
};
const aarch64UnknownLinuxMusl: LinuxTarget = {
2022-12-15 12:00:58 +01:00
id: "aarch64-unknown-linux-musl",
platform: "linux",
arch: "arm64",
2022-09-23 13:14:09 +02:00
libC: MUSL,
};
const powerpc64leUnknownLinuxGnu: LinuxTarget = {
2022-12-15 12:00:58 +01:00
id: "powerpc64le-unknown-linux-gnu",
platform: "linux",
arch: "ppc64",
2022-09-23 13:14:09 +02:00
libC: GLIBC,
};
const powerpc64leUnknownLinuxMusl: LinuxTarget = {
2022-12-15 12:00:58 +01:00
id: "powerpc64le-unknown-linux-musl",
platform: "linux",
arch: "ppc64",
2022-09-23 13:14:09 +02:00
libC: MUSL,
};
export const TARGETS: Record<TargetId, Target> = {
// macOS
2022-12-15 12:00:58 +01:00
"aarch64-apple-darwin": aarch64AppleDarwin,
"x86_64-apple-darwin": x8664AppleDarwin,
"universal-apple-darwin": universalAppleDarwin,
// Windows
2022-12-15 12:00:58 +01:00
"i686-pc-windows-msvc": i686PcWindowsMsvc,
"x86_64-pc-windows-msvc": x8664PcWindowsMsvc,
2023-03-23 14:22:29 +01:00
"aarch64-pc-windows-msvc": aarch64WindowsMsvc,
// FreeBSD
"i686-unknown-freebsd": i686UnknownFreebsd,
"x86_64-unknown-freebsd": x8664UnknownFreebsd,
"aarch64-unknown-freebsd": aarch64UnknownFreebsd,
// Linux
2022-12-15 12:00:58 +01:00
"i686-unknown-linux-musl": i686UnknownLinuxMusl,
"i686-unknown-linux-gnu": i686UnknownLinuxGnu,
"x86_64-unknown-linux-musl": x8664UnknownLinuxMusl,
"x86_64-unknown-linux-gnu": x8664UnknownLinuxGnu,
"aarch64-unknown-linux-musl": aarch64UnknownLinuxMusl,
"aarch64-unknown-linux-gnu": aarch64UnknownLinuxGnu,
"powerpc64le-unknown-linux-musl": powerpc64leUnknownLinuxMusl,
"powerpc64le-unknown-linux-gnu": powerpc64leUnknownLinuxGnu,
};
2022-09-23 12:04:38 +02:00
export function getHost(): Target | undefined {
2022-12-15 12:00:58 +01:00
return Object.values(TARGETS).find(
(target) =>
target.platform === process.platform &&
target.arch === process.arch &&
(process.platform !== "linux" || (target as LinuxTarget).libC === processLibC()),
2022-12-15 12:00:58 +01:00
);
}
export function isHostId(id: TargetId): boolean {
return getHost()?.id === id;
}
export function isHost(target: Target): boolean {
return getHost()?.id === target.id;
}