-
-
Notifications
You must be signed in to change notification settings - Fork 240
/
parse-triple.ts
125 lines (116 loc) · 3.21 KB
/
parse-triple.ts
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { execSync } from 'child_process'
// https://nodejs.org/api/process.html#process_process_arch
type NodeJSArch =
| 'arm'
| 'arm64'
| 'ia32'
| 'mips'
| 'mipsel'
| 'ppc'
| 'ppc64'
| 's390'
| 's390x'
| 'x32'
| 'x64'
| 'universal'
const CpuToNodeArch: { [index: string]: NodeJSArch } = {
x86_64: 'x64',
aarch64: 'arm64',
i686: 'ia32',
armv7: 'arm',
}
export const NodeArchToCpu: { [index: string]: string } = {
x64: 'x86_64',
arm64: 'aarch64',
ia32: 'i686',
arm: 'armv7',
}
const SysToNodePlatform: { [index: string]: NodeJS.Platform } = {
linux: 'linux',
freebsd: 'freebsd',
darwin: 'darwin',
windows: 'win32',
}
export const UniArchsByPlatform: Record<string, NodeJSArch[]> = {
darwin: ['x64', 'arm64'],
}
export interface PlatformDetail {
platform: NodeJS.Platform
platformArchABI: string
arch: NodeJSArch
raw: string
abi: string | null
}
export const DefaultPlatforms: PlatformDetail[] = [
{
platform: 'win32',
arch: 'x64',
abi: 'msvc',
platformArchABI: 'win32-x64-msvc',
raw: 'x86_64-pc-windows-msvc',
},
{
platform: 'darwin',
arch: 'x64',
abi: null,
platformArchABI: 'darwin-x64',
raw: 'x86_64-apple-darwin',
},
{
platform: 'linux',
arch: 'x64',
abi: 'gnu',
platformArchABI: 'linux-x64-gnu',
raw: 'x86_64-unknown-linux-gnu',
},
]
/**
* A triple is a specific format for specifying a target architecture.
* Triples may be referred to as a target triple which is the architecture for the artifact produced, and the host triple which is the architecture that the compiler is running on.
* The general format of the triple is `<arch><sub>-<vendor>-<sys>-<abi>` where:
* - `arch` = The base CPU architecture, for example `x86_64`, `i686`, `arm`, `thumb`, `mips`, etc.
* - `sub` = The CPU sub-architecture, for example `arm` has `v7`, `v7s`, `v5te`, etc.
* - `vendor` = The vendor, for example `unknown`, `apple`, `pc`, `nvidia`, etc.
* - `sys` = The system name, for example `linux`, `windows`, `darwin`, etc. none is typically used for bare-metal without an OS.
* - `abi` = The ABI, for example `gnu`, `android`, `eabi`, etc.
*/
export function parseTriple(rawTriple: string): PlatformDetail {
const triple = rawTriple.endsWith('eabi')
? `${rawTriple.slice(0, -4)}-eabi`
: rawTriple
const triples = triple.split('-')
let cpu: string
let sys: string
let abi: string | null = null
if (triples.length === 4) {
;[cpu, , sys, abi = null] = triples
} else if (triples.length === 3) {
;[cpu, , sys] = triples
} else {
;[cpu, sys] = triples
}
const platformName = SysToNodePlatform[sys] ?? sys
const arch = CpuToNodeArch[cpu] ?? cpu
return {
platform: platformName,
arch,
abi,
platformArchABI: abi
? `${platformName}-${arch}-${abi}`
: `${platformName}-${arch}`,
raw: rawTriple,
}
}
export function getHostTargetTriple(): PlatformDetail {
const host = execSync(`rustc -vV`, {
env: process.env,
})
.toString('utf8')
.split('\n')
.find((line) => line.startsWith('host: '))
const triple = host?.slice('host: '.length)
if (!triple) {
throw new TypeError(`Can not parse target triple from host`)
}
return parseTriple(triple)
}