-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtargets.ts
More file actions
216 lines (189 loc) · 6.06 KB
/
targets.ts
File metadata and controls
216 lines (189 loc) · 6.06 KB
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
import assert from "node:assert";
import cp from "node:child_process";
import { assertFixable } from "@react-native-node-api/cli-utils";
import { getInstalledTargets } from "./rustup.js";
export const ANDROID_TARGETS = [
"aarch64-linux-android",
"armv7-linux-androideabi",
"i686-linux-android",
"x86_64-linux-android",
// "arm-linux-androideabi",
// "thumbv7neon-linux-androideabi",
] as const;
export type AndroidTargetName = (typeof ANDROID_TARGETS)[number];
// TODO: Consider calling out to rustup to generate this list or just use @napi-rs/triples
export const APPLE_TARGETS = [
"aarch64-apple-darwin",
"x86_64-apple-darwin",
"aarch64-apple-ios",
"aarch64-apple-ios-sim",
"x86_64-apple-ios", // Simulator (despite the missing -sim suffix)
// "aarch64-apple-ios-macabi", // Catalyst
// "x86_64-apple-ios-macabi", // Catalyst
"aarch64-apple-visionos",
"aarch64-apple-visionos-sim",
"aarch64-apple-tvos",
// "arm64e-apple-tvos",
"aarch64-apple-tvos-sim",
"x86_64-apple-tvos", // Simulator (despite the missing -sim suffix)
// "aarch64-apple-watchos",
// "aarch64-apple-watchos-sim",
// "arm64_32-apple-watchos",
// "arm64e-apple-darwin",
// "arm64e-apple-ios",
// "armv7k-apple-watchos",
// "armv7s-apple-ios",
// "i386-apple-ios",
// "i686-apple-darwin",
// "x86_64-apple-watchos-sim",
// "x86_64h-apple-darwin",
] as const;
export type AppleTargetName = (typeof APPLE_TARGETS)[number];
const APPLE_ARCHITECTURES = [
"aarch64",
"arm64_32",
"arm64e",
"armv7",
"armv7s",
"x86_64",
"x86_64h",
"i386",
"i686",
] as const;
export type AppleArchitecture = (typeof APPLE_ARCHITECTURES)[number];
export function isAppleArchitecture(
architecture: string,
): architecture is AppleArchitecture {
return (APPLE_ARCHITECTURES as readonly string[]).includes(architecture);
}
const APPLE_OPERATING_SYSTEMS = [
"darwin",
"ios",
"tvos",
"visionos",
"watchos",
] as const;
export type AppleOperatingSystem = (typeof APPLE_OPERATING_SYSTEMS)[number];
export function isAppleOperatingSystem(os: string): os is AppleOperatingSystem {
return (APPLE_OPERATING_SYSTEMS as readonly string[]).includes(os);
}
const APPLE_VARIANTS = ["sim", "macabi"] as const;
export type AppleVariant = (typeof APPLE_VARIANTS)[number];
export function isAppleVariant(variant: string): variant is AppleVariant {
return (APPLE_VARIANTS as readonly string[]).includes(variant);
}
export function parseAppleTargetName(target: AppleTargetName): {
architecture: AppleArchitecture;
os: AppleOperatingSystem;
variant?: AppleVariant;
} {
const [architecture, vendor, os, variant] = target.split("-");
assert(vendor === "apple", "Expected vendor to be apple");
assert(
isAppleArchitecture(architecture),
`Unexpected architecture: ${architecture}`,
);
assert(isAppleOperatingSystem(os), `Unexpected operating system: ${os}`);
assert(
typeof variant === "undefined" || isAppleVariant(variant),
`Unexpected variant: ${variant}`,
);
return {
architecture,
os,
variant,
};
}
export const ALL_TARGETS = [...ANDROID_TARGETS, ...APPLE_TARGETS] as const;
export type TargetName = (typeof ALL_TARGETS)[number];
const THIRD_TIER_TARGETS: Set<TargetName> = new Set([
"aarch64-apple-visionos",
"aarch64-apple-visionos-sim",
"aarch64-apple-tvos",
"aarch64-apple-tvos-sim",
"x86_64-apple-tvos",
]);
export function assertNightlyToolchain() {
const toolchainLines = cp
.execFileSync("rustup", ["toolchain", "list"], {
encoding: "utf-8",
})
.split("\n");
const nightlyLines = toolchainLines.filter((line) =>
line.startsWith("nightly-"),
);
assertFixable(
nightlyLines.length > 0,
"You need to use a nightly Rust toolchain",
{
command: "rustup toolchain install nightly --component rust-src",
},
);
const componentLines = cp
.execFileSync("rustup", ["component", "list", "--toolchain", "nightly"], {
encoding: "utf-8",
})
.split("\n");
assertFixable(
componentLines.some((line) => line === "rust-src (installed)"),
"You need to install the rust-src component for the nightly Rust toolchain",
{
command: "rustup toolchain install nightly --component rust-src",
},
);
}
/**
* Ensure the targets are either installed into the Rust toolchain or available via nightly Rust toolchain.
* We do this up-front because the error message and fix is very unclear from the failure when missing.
*/
export function ensureAvailableTargets(expectedTargets: Set<TargetName>) {
const installedTargets = getInstalledTargets();
const missingInstallableTargets = expectedTargets
.difference(installedTargets)
.difference(THIRD_TIER_TARGETS);
assertFixable(
missingInstallableTargets.size === 0,
`You need to add these targets to your toolchain: ${[
...missingInstallableTargets,
].join(", ")}`,
{
command: `rustup target add ${[...missingInstallableTargets].join(" ")}`,
},
);
const expectedThirdTierTargets =
expectedTargets.intersection(THIRD_TIER_TARGETS);
if (expectedThirdTierTargets.size > 0) {
assertNightlyToolchain();
}
}
export function isAndroidTarget(
target: TargetName,
): target is AndroidTargetName {
return ANDROID_TARGETS.includes(target as (typeof ANDROID_TARGETS)[number]);
}
export function isAppleTarget(target: TargetName): target is AppleTargetName {
return APPLE_TARGETS.includes(target as (typeof APPLE_TARGETS)[number]);
}
export function isThirdTierTarget(target: TargetName): boolean {
return THIRD_TIER_TARGETS.has(target);
}
export function filterTargetsByPlatform(
targets: Set<TargetName>,
platform: "android",
): AndroidTargetName[];
export function filterTargetsByPlatform(
targets: Set<TargetName>,
platform: "apple",
): AppleTargetName[];
export function filterTargetsByPlatform(
targets: Set<TargetName>,
platform: "apple" | "android",
) {
if (platform === "android") {
return [...targets].filter(isAndroidTarget);
} else if (platform === "apple") {
return [...targets].filter(isAppleTarget);
} else {
throw new Error(`Unexpected platform ${platform as string}`);
}
}