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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use std::borrow::Cow;
use super::{generated, TargetInfo};
impl TargetInfo<'_> {
/// The LLVM/Clang target triple.
///
/// See <https://clang.llvm.org/docs/CrossCompilation.html#target-triple>.
///
/// Rust and Clang don't really agree on target naming, so we first try to
/// find the matching trible based on `rustc`'s output, but if no such
/// triple exists, we attempt to construct the triple from scratch.
///
/// NOTE: You should never need to match on this explicitly, use the
/// fields on [`TargetInfo`] instead.
pub(crate) fn llvm_target(
&self,
rustc_target: &str,
version: Option<&str>,
) -> Cow<'static, str> {
if rustc_target == "armv7-apple-ios" {
// FIXME(madsmtm): Unnecessary once we bump MSRV to Rust 1.74
return Cow::Borrowed("armv7-apple-ios");
} else if self.os == "uefi" {
// Override the UEFI LLVM targets.
//
// The rustc mappings (as of 1.82) for the UEFI targets are:
// * i686-unknown-uefi -> i686-unknown-windows-gnu
// * x86_64-unknown-uefi -> x86_64-unknown-windows
// * aarch64-unknown-uefi -> aarch64-unknown-windows
//
// However, in cc-rs all the UEFI targets use
// -windows-gnu. This has been the case since 2021 [1].
// * i686-unknown-uefi -> i686-unknown-windows-gnu
// * x86_64-unknown-uefi -> x86_64-unknown-windows-gnu
// * aarch64-unknown-uefi -> aarch64-unknown-windows-gnu
//
// For now, override the UEFI mapping to keep the behavior
// of cc-rs unchanged.
//
// TODO: as discussed in [2], it may be possible to switch
// to new UEFI targets added to clang, and regardless it
// would be good to have consistency between rustc and
// cc-rs.
//
// [1]: https://github.com/rust-lang/cc-rs/pull/623
// [2]: https://github.com/rust-lang/cc-rs/pull/1264
return Cow::Owned(format!("{}-unknown-windows-gnu", self.full_arch));
}
// If no version is requested, let's take the triple directly from
// `rustc` (the logic below is not yet good enough for most targets).
//
// FIXME(madsmtm): This should ideally be removed.
if version.is_none() {
if let Ok(index) = generated::LLVM_TARGETS
.binary_search_by_key(&rustc_target, |(rustc_target, _)| rustc_target)
{
let (_, llvm_target) = &generated::LLVM_TARGETS[index];
return Cow::Borrowed(llvm_target);
}
}
// Otherwise, attempt to construct the triple from the target info.
let arch = match self.full_arch {
riscv32 if riscv32.starts_with("riscv32") => "riscv32",
riscv64 if riscv64.starts_with("riscv64") => "riscv64",
"aarch64" if self.vendor == "apple" => "arm64",
"armv7" if self.vendor == "sony" => "thumbv7a", // FIXME
arch => arch,
};
let vendor = match self.vendor {
"kmc" | "nintendo" => "unknown",
"unknown" if self.os == "android" => "linux",
"uwp" => "pc",
"espressif" => "",
_ if self.arch == "msp430" => "",
vendor => vendor,
};
let os = match self.os {
"macos" => "macosx",
"visionos" => "xros",
"uefi" => "windows",
"solid_asp3" | "horizon" | "teeos" | "nuttx" | "espidf" => "none",
"nto" => "unknown", // FIXME
"trusty" => "unknown", // FIXME
os => os,
};
let version = version.unwrap_or("");
let env = match self.env {
"newlib" | "nto70" | "nto71" | "nto71_iosock" | "p1" | "p2" | "relibc" | "sgx"
| "uclibc" => "",
"sim" => "simulator",
env => env,
};
let abi = match self.abi {
"sim" => {
if env != "simulator" {
"simulator"
} else {
""
}
}
"llvm" | "softfloat" | "uwp" | "vec-extabi" => "",
"ilp32" => "_ilp32",
"abi64" => "",
abi => abi,
};
Cow::Owned(match (vendor, env, abi) {
("", "", "") => format!("{arch}-{os}{version}"),
("", env, abi) => format!("{arch}-{os}{version}-{env}{abi}"),
(vendor, "", "") => format!("{arch}-{vendor}-{os}{version}"),
(vendor, env, abi) => format!("{arch}-{vendor}-{os}{version}-{env}{abi}"),
})
}
}
#[cfg(test)]
mod tests {
use std::process::Command;
use crate::TargetInfo;
#[test]
fn test_old_ios_target() {
assert_eq!(
TargetInfo {
full_arch: "armv7",
arch: "armv7",
vendor: "apple",
os: "ios",
env: "",
abi: "",
}
.llvm_target("armv7-apple-ios", None),
"armv7-apple-ios"
);
}
#[test]
fn basic_llvm_triple_guessing() {
assert_eq!(
TargetInfo {
full_arch: "aarch64",
arch: "aarch64",
vendor: "unknown",
os: "linux",
env: "",
abi: "",
}
.llvm_target("invalid", None),
"aarch64-unknown-linux"
);
assert_eq!(
TargetInfo {
full_arch: "x86_64",
arch: "x86_64",
vendor: "unknown",
os: "linux",
env: "gnu",
abi: "",
}
.llvm_target("invalid", None),
"x86_64-unknown-linux-gnu"
);
assert_eq!(
TargetInfo {
full_arch: "x86_64",
arch: "x86_64",
vendor: "unknown",
os: "linux",
env: "gnu",
abi: "eabi",
}
.llvm_target("invalid", None),
"x86_64-unknown-linux-gnueabi"
);
assert_eq!(
TargetInfo {
full_arch: "x86_64",
arch: "x86_64",
vendor: "apple",
os: "macos",
env: "",
abi: "",
}
.llvm_target("invalid", None),
"x86_64-apple-macosx"
);
}
#[test]
fn llvm_version() {
assert_eq!(
TargetInfo {
full_arch: "aarch64",
arch: "aarch64",
vendor: "apple",
os: "ios",
env: "",
abi: "sim",
}
.llvm_target("aarch64-apple-ios-sim", Some("14.0")),
"arm64-apple-ios14.0-simulator"
);
assert_eq!(
TargetInfo {
full_arch: "aarch64",
arch: "aarch64",
vendor: "apple",
os: "visionos",
env: "",
abi: "",
}
.llvm_target("aarch64-apple-visionos", Some("2.0")),
"arm64-apple-xros2.0"
);
assert_eq!(
TargetInfo {
full_arch: "aarch64",
arch: "aarch64",
vendor: "apple",
os: "ios",
env: "",
abi: "macabi",
}
.llvm_target("aarch64-apple-ios-macabi", Some("13.1")),
"arm64-apple-ios13.1-macabi"
);
}
#[test]
fn uefi() {
assert_eq!(
TargetInfo {
full_arch: "i686",
arch: "x86",
vendor: "unknown",
os: "uefi",
env: "",
abi: "",
}
.llvm_target("i686-unknown-uefi", None),
"i686-unknown-windows-gnu"
);
assert_eq!(
TargetInfo {
full_arch: "x86_64",
arch: "x86_64",
vendor: "unknown",
os: "uefi",
env: "",
abi: "",
}
.llvm_target("x86_64-unknown-uefi", None),
"x86_64-unknown-windows-gnu"
);
assert_eq!(
TargetInfo {
full_arch: "aarch64",
arch: "aarch64",
vendor: "unknown",
os: "uefi",
env: "",
abi: "",
}
.llvm_target("aarch64-unknown-uefi", None),
"aarch64-unknown-windows-gnu"
);
}
#[test]
#[ignore = "not yet done"]
fn llvm_for_all_rustc_targets() {
let rustc = std::env::var("RUSTC").unwrap_or_else(|_| "rustc".to_string());
let target_list = Command::new(&rustc)
.arg("--print=target-list")
.output()
.unwrap()
.stdout;
let target_list = String::from_utf8(target_list).unwrap();
let mut has_failure = false;
for target in target_list.lines() {
let spec_json = Command::new(&rustc)
.arg("--target")
.arg(target)
.arg("-Zunstable-options")
.arg("--print=target-spec-json")
.env("RUSTC_BOOTSTRAP", "1") // Crimes
.output()
.unwrap()
.stdout;
let spec_json = String::from_utf8(spec_json).unwrap();
// JSON crimes
let expected = spec_json
.split_once("llvm-target\": \"")
.unwrap()
.1
.split_once("\"")
.unwrap()
.0;
let actual = TargetInfo::from_rustc_target(target)
.map(|target| target.llvm_target("invalid", None));
if Some(expected) != actual.as_deref().ok() {
eprintln!("failed comparing {target}:");
eprintln!(" expected: Ok({expected:?})");
eprintln!(" actual: {actual:?}");
eprintln!();
has_failure = true;
}
}
if has_failure {
panic!("failed comparing targets");
}
}
}