-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtargets.ts
More file actions
156 lines (133 loc) · 4.44 KB
/
targets.ts
File metadata and controls
156 lines (133 loc) · 4.44 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
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];
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}`);
}
}