-
Notifications
You must be signed in to change notification settings - Fork 1
/
host-targets.js
270 lines (245 loc) · 7.06 KB
/
host-targets.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
'use strict';
/** @typedef {import('./types.js').OsString} OsString */
/** @typedef {import('./types.js').LibcString} LibcString */
/** @typedef {import('./types.js').ArchString} ArchString */
/** @typedef {import('./types.js').TargetTriplet} TargetTriplet */
var HostTargets = module.exports;
let reVersionOnly = /^[\d\.]+(-RELEASE)?$/;
let reLeadingVer = /^\d([\d\.\-\+_])+/;
// Notes:
// gki = Generic Kernel Image
// qgki = Qualcomm Generic Kernel Image
let X86_64 = {
x86_64_rocm: ['x86_64_rocm', 'x86_64_v4', 'x86_64_v3', 'x86_64_v2', 'x86_64'],
x86_64_v4: ['x86_64_v4', 'x86_64_v3', 'x86_64_v2', 'x86_64'],
x86_64_v3: ['x86_64_v3', 'x86_64_v2', 'x86_64', 'x86'],
x86_64_v2: ['x86_64_v2', 'x86_64', 'x86'],
x86_64: ['x86_64', 'x86'],
};
HostTargets.WATERFALL = {
darwin: { aarch64: ['aarch64', 'x86_64'] },
windows: Object.assign(
{
aarch64: ['aarch64', 'x86_64'],
},
X86_64,
),
linux: Object.assign(
{
// NOTE: the libc:armhf will need to be installed
aarch64: ['aarch64', 'armv7a', 'armv7', 'armhf'],
},
X86_64,
),
ANYOS: Object.assign({}, X86_64, {
// arches
armv7: ['armv7a', 'armv7', 'armhf', 'armv6', 'armel', 'armv5'],
armv6: ['armv6', 'armel', 'armv5'],
armv5: ['armv5', 'armel'],
// libcs
// prefer 'none' because dep can be crazy otherwise
gnu: ['none', 'gnu'],
libc: ['none', 'libc'],
musl: ['none', 'musl'],
// prefer 'msvc' because the install is automated
// (TODO does this work for x86_64 on aarch64?)
msvc: ['msvc', 'none', 'gnu'],
// prefer 'bionic' because it's built-in
// (TODO test to see if statically-compiled linux bins work)
bionic: ['bionic', 'none'],
}),
};
// The Terms
let T = {
ANDROID: { android: true, vendor: 'unknown', libc: 'bionic' },
LINUX: { os: 'linux', vendor: 'unknown' },
WINDOWS: { os: 'windows', vendor: 'pc' },
DARWIN: { os: 'darwin', vendor: 'apple' },
};
// OS, Arch, Libc
HostTargets.TERMS = {
// agent
webi: {},
curl: {},
Wget: {},
wget: {},
'wget+curl': {},
'curl+wget': {},
PowerShell: {},
'PowerShell+curl': {},
unknown: {},
// OS
Android: T.ANDROID,
Linux: T.LINUX,
MINGW: T.LINUX,
CYGWIN: T.LINUX,
Darwin: T.DARWIN,
Windows: T.WINDOWS,
DragonFly: { os: 'dragonfly', vendor: 'unknown' },
NetBSD: { os: 'netbsd', vendor: 'unknown' },
OpenBSD: { os: 'openbsd', vendor: 'unknown' },
FreeBSD: { os: 'freebsd', vendor: 'unknown' },
illumos: { os: 'illumos', vendor: 'unknown' },
SunOS: {},
MS: { os: 'windows' },
// arch
AMD64: { arch: 'x86_64' },
amd64: { arch: 'x86_64' },
x86_64: { arch: 'x86_64' },
ARM64: { arch: 'aarch64' },
arm64: { arch: 'aarch64' },
aarch64: { arch: 'aarch64' },
armv7l: { arch: 'armv7' },
armv6l: { arch: 'armv6' },
earmv6hf: { arch: 'armhf' },
arm: {},
evbarm: {},
i86pc: { arch: 'x86' },
i386: { arch: 'x86' },
i686: { arch: 'x86' },
// libc
gnu: { libc: 'gnu' },
GNU: { libc: 'gnu' },
libc: { libc: 'libc' },
msvc: { libc: 'msvc' },
// See <https://android.googlesource.com/platform/bionic/>
bionic: { libc: 'bionic' },
musl: { libc: 'musl' },
// meta, test, misc
test: {},
};
{
let keys = Object.keys(HostTargets.TERMS);
for (let key of keys) {
let lkey = key.toLowerCase();
HostTargets.TERMS[lkey] = HostTargets.TERMS[key];
}
}
HostTargets._MATCHERS = {
// seems to be some sort of android-specific kernel build hash
androidBuild: /^(ab)[0-9A-Z]+$/,
// el = enterprise linux
// fc = fedora core
// amzn = amazon
// ex: CYGWIN_NT-10.0-WOW/3.3.5(0.341/5/3)
cygwin: /^CYGWIN(_NT)?/,
distroRelease: /^(android|amzn|el|fc)\d+$/,
// ex: MINGW64_NT-10.0-19045/3.3.6-341.x86_64
mingw: /^MINGW(64_NT)?/,
};
/**
* @param {Object.<"os"|"arch"|"libc", String>} target
* @param {Array<String>} terms
*/
HostTargets.termsToTarget = function (target, terms) {
let bogoTerms = [];
Object.assign(target, { errors: [] });
for (let term of terms) {
let lterm = term.toLowerCase();
let hints = HostTargets.TERMS[lterm];
if (hints) {
upsertHints(target, terms, term, hints);
continue;
}
if (reVersionOnly.test(term)) {
continue;
}
if (HostTargets._MATCHERS.androidBuild.test(term)) {
Object.assign(target, { android: true });
continue;
}
if (term.includes('MINGW')) {
Object.assign(target, HostTargets.TERMS.MINGW);
continue;
}
if (term.includes('CYGWIN')) {
Object.assign(target, HostTargets.TERMS.CYGWIN);
continue;
}
let m = term.match(HostTargets._MATCHERS.distroRelease);
if (m) {
// ex: android12 => android, fc39 => fc
term = m[1];
} else {
// ex: 6.2.0-1014-aws => aws
term = term.replace(reLeadingVer, '');
}
if (!term) {
continue;
}
// ex: android12-qgki
let isAndroid = term.startsWith('android');
if (isAndroid) {
Object.assign(target, { android: true });
continue;
}
bogoTerms.push(term);
}
upsertAndroidTerms(target, terms);
return bogoTerms;
};
function upsertHints(target, ua, terms, hints) {
if (!hints) {
throw new Error("[SANITY FAIL] 'hints' not provided");
}
let keys = Object.keys(hints);
for (let key of keys) {
if (!target[key]) {
target[key] = hints[key];
}
if (target[key] !== hints[key]) {
let msg = `'${key}' already set to '${target[key]}', not updated to '${hints[key]}'`;
let ignore = false;
let targetIsLinuxy =
target[key] === 'gnu' ||
target[key] === 'bionic' ||
target[key] === 'musl';
if (targetIsLinuxy) {
if (hints[key] === 'libc') {
// likely an old webi script with bad generic categorization
ignore = true;
} else if (hints[key] === 'musl') {
// musl can be installed on a GNU system
target.libs = [target.libc, 'musl'];
ignore = true;
}
}
if (!ignore) {
target.errors.push({ [key]: hints[key], message: msg, terms: terms });
throw new Error(`${msg} for '${ua}' / '${terms}'`);
}
}
}
}
// Workaround for current (2023-q4) Android misclassification
function upsertAndroidTerms(target, terms) {
if (!target.android) {
return;
}
if (!target.os) {
target.os = 'android';
} else if (target.os === 'linux') {
target.os = 'android';
if (target.libc === 'gnu') {
target.libc = 'bionic';
}
} else if (target.os !== 'android') {
let msg = `Android 'os' already set to '${target.os}', but should be 'android'`;
target.errors.push({ os: 'android', message: msg, terms: terms });
throw new Error(`${msg} for '${terms}'`);
}
if (!target.libc) {
target.libc = 'bionic';
}
if (target.libc !== 'bionic') {
if (target.libc === 'musl') {
// musl can be installed on Android
target.libcs = ['bionic', 'musl'];
target.libc = 'musl';
} else {
let msg = `Android 'libc' already set to '${target.libc}', but should be 'bionic'`;
target.errors.push({ libc: 'bionic', message: msg, terms: terms });
throw new Error(`${msg} for '${terms}'`);
}
}
}