-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtargets.ts
More file actions
145 lines (131 loc) · 4.88 KB
/
targets.ts
File metadata and controls
145 lines (131 loc) · 4.88 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
import { chalk, UsageError } 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",
// "aarch64-apple-ios-macabi", // Catalyst
// "x86_64-apple-ios",
// "x86_64-apple-ios-macabi", // Catalyst
// TODO: Re-enabled these when we know how to install them 🙈
/*
"aarch64-apple-tvos",
"aarch64-apple-tvos-sim",
*/
"aarch64-apple-visionos",
"aarch64-apple-visionos-sim",
// "aarch64-apple-watchos",
// "aarch64-apple-watchos-sim",
// "arm64_32-apple-watchos",
// "arm64e-apple-darwin",
// "arm64e-apple-ios",
// "arm64e-apple-tvos",
// "armv7k-apple-watchos",
// "armv7s-apple-ios",
// "i386-apple-ios",
// "i686-apple-darwin",
// "x86_64-apple-tvos",
// "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];
/**
* Tier 3 Rust targets that are not available via `rustup target add`
* and require building the standard library from source using `-Zbuild-std`.
*
* @see https://doc.rust-lang.org/rustc/platform-support.html
* @see https://doc.rust-lang.org/cargo/reference/unstable.html#build-std
*/
export const TIER_3_TARGETS: readonly TargetName[] = [
"aarch64-apple-visionos",
"aarch64-apple-visionos-sim",
] as const;
/**
* Check if a target is a tier 3 target that requires build-std
*/
export function isTier3Target(target: TargetName): boolean {
return TIER_3_TARGETS.includes(target);
}
/**
* Ensure the targets are installed into the Rust toolchain
* We do this up-front because the error message and fix is very unclear from the failure when missing.
*/
export function ensureInstalledTargets(expectedTargets: Set<TargetName>) {
const installedTargets = getInstalledTargets();
const missingStandardTargets = new Set([
...[...expectedTargets].filter(
(target) => !installedTargets.has(target) && !isTier3Target(target),
),
]);
const tier3Targets = new Set([
...[...expectedTargets].filter((target) => isTier3Target(target)),
]);
// Handle standard targets that can be installed via rustup
if (missingStandardTargets.size > 0) {
throw new UsageError(
`You're missing ${
missingStandardTargets.size
} targets - to fix this, run:\n\n${chalk.italic(
`rustup target add ${[...missingStandardTargets].join(" ")}`,
)}`,
);
}
// Handle tier 3 targets that require build-std setup
if (tier3Targets.size > 0) {
throw new UsageError(
`You're using tier 3 targets (${[...tier3Targets].join(", ")}) that require building the standard library from source.\n\n` +
`To set up support for these targets:\n\n` +
`1. Install nightly Rust with the rust-src component:\n` +
` ${chalk.italic("rustup toolchain install nightly --component rust-src")}\n\n` +
`2. Configure Cargo to use build-std by creating a .cargo/config.toml file:\n` +
` ${chalk.italic("[unstable]")}\n` +
` ${chalk.italic('build-std = ["std", "panic_abort"]')}\n\n` +
`3. Set your default toolchain to nightly:\n` +
` ${chalk.italic("rustup default nightly")}\n\n` +
`For more information, see:\n` +
`- Rust Platform Support: ${chalk.italic("https://doc.rust-lang.org/rustc/platform-support.html")}\n` +
`- Cargo build-std: ${chalk.italic("https://doc.rust-lang.org/cargo/reference/unstable.html#build-std")}`,
);
}
}
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 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}`);
}
}