Debian Codegraph @

debian/rust-vendor/cc/src/target/llvm.rs

raw
1 use std::borrow::Cow;
2
3 use super::{generated, TargetInfo};
4
5 impl TargetInfo<'_> {
6 /// The LLVM/Clang target triple.
7 ///
8 /// See <https://clang.llvm.org/docs/CrossCompilation.html#target-triple>.
9 ///
10 /// Rust and Clang don't really agree on target naming, so we first try to
11 /// find the matching trible based on `rustc`'s output, but if no such
12 /// triple exists, we attempt to construct the triple from scratch.
13 ///
14 /// NOTE: You should never need to match on this explicitly, use the
15 /// fields on [`TargetInfo`] instead.
16 pub(crate) fn llvm_target(
17 &self,
18 rustc_target: &str,
19 version: Option<&str>,
20 ) -> Cow<'static, str> {
21 if rustc_target == "armv7-apple-ios" {
22 // FIXME(madsmtm): Unnecessary once we bump MSRV to Rust 1.74
23 return Cow::Borrowed("armv7-apple-ios");
24 } else if rustc_target == "aarch64-unknown-linux-pauthtest" {
25 // `aarch64-unknown-linux-pauthtest` rustc target sets both
26 // environment and abi (to `musl` and `pauthtest` respectively`).
27 // However, the abi field exists mainly for `cfg(...)` evaluation in
28 // Rust, not for generating the actual LLVM triple. The logic of:
29 // `arch-vendor-os-env+abi` does not applay here.
30 return Cow::Borrowed("aarch64-unknown-linux-pauthtest");
31 } else if self.os == "uefi" {
32 // Override the UEFI LLVM targets.
33 //
34 // The rustc mappings (as of 1.82) for the UEFI targets are:
35 // * i686-unknown-uefi -> i686-unknown-windows-gnu
36 // * x86_64-unknown-uefi -> x86_64-unknown-windows
37 // * aarch64-unknown-uefi -> aarch64-unknown-windows
38 //
39 // However, in cc-rs all the UEFI targets use
40 // -windows-gnu. This has been the case since 2021 [1].
41 // * i686-unknown-uefi -> i686-unknown-windows-gnu
42 // * x86_64-unknown-uefi -> x86_64-unknown-windows-gnu
43 // * aarch64-unknown-uefi -> aarch64-unknown-windows-gnu
44 //
45 // For now, override the UEFI mapping to keep the behavior
46 // of cc-rs unchanged.
47 //
48 // TODO: as discussed in [2], it may be possible to switch
49 // to new UEFI targets added to clang, and regardless it
50 // would be good to have consistency between rustc and
51 // cc-rs.
52 //
53 // [1]: https://github.com/rust-lang/cc-rs/pull/623
54 // [2]: https://github.com/rust-lang/cc-rs/pull/1264
55 return Cow::Owned(format!("{}-unknown-windows-gnu", self.full_arch));
56 }
57
58 // If no version is requested, let's take the triple directly from
59 // `rustc` (the logic below is not yet good enough for most targets).
60 //
61 // FIXME(madsmtm): This should ideally be removed.
62 if version.is_none() {
63 if let Ok(index) = generated::LLVM_TARGETS
64 .binary_search_by_key(&rustc_target, |(rustc_target, _)| rustc_target)
65 {
66 let (_, llvm_target) = &generated::LLVM_TARGETS[index];
67 return Cow::Borrowed(llvm_target);
68 }
69 }
70
71 // Otherwise, attempt to construct the triple from the target info.
72
73 let arch = match self.full_arch {
74 riscv32 if riscv32.starts_with("riscv32") => "riscv32",
75 riscv64 if riscv64.starts_with("riscv64") => "riscv64",
76 "aarch64" if self.vendor == "apple" => "arm64",
77 "armv7" if self.vendor == "sony" => "thumbv7a", // FIXME
78 arch => arch,
79 };
80 let vendor = match self.vendor {
81 "kmc" | "nintendo" => "unknown",
82 "unknown" if self.os == "android" => "linux",
83 "uwp" => "pc",
84 "espressif" => "",
85 _ if self.arch == "msp430" => "",
86 vendor => vendor,
87 };
88 let os = match self.os {
89 "macos" => "macosx",
90 "visionos" => "xros",
91 "uefi" => "windows",
92 "solid_asp3" | "horizon" | "teeos" | "nuttx" | "espidf" => "none",
93 "qnx" | "nto" => "unknown", // LLVM doesn't know about QNX
94 "trusty" => "unknown", // FIXME
95 os => os,
96 };
97 let version = version.unwrap_or("");
98 let env = match self.env {
99 "newlib" | "nto70" | "nto71" | "nto71_iosock" | "p1" | "p2" | "relibc" | "sgx"
100 | "uclibc" => "",
101 "sim" => "simulator",
102 env => env,
103 };
104 let abi = match self.abi {
105 "llvm" | "softfloat" | "uwp" | "vec-extabi" => "",
106 "ilp32" => "_ilp32",
107 "abi64" => "",
108 "elfv1" | "elfv2" => "",
109 abi => abi,
110 };
111 Cow::Owned(match (vendor, env, abi) {
112 ("", "", "") => format!("{arch}-{os}{version}"),
113 ("", env, abi) => format!("{arch}-{os}{version}-{env}{abi}"),
114 (vendor, "", "") => format!("{arch}-{vendor}-{os}{version}"),
115 (vendor, env, abi) => format!("{arch}-{vendor}-{os}{version}-{env}{abi}"),
116 })
117 }
118 }
119
120 #[cfg(test)]
121 #[allow(clippy::disallowed_methods)]
122 mod tests {
123 use std::process::Command;
124
125 use crate::TargetInfo;
126
127 #[test]
128 fn test_old_ios_target() {
129 assert_eq!(
130 TargetInfo {
131 full_arch: "armv7",
132 arch: "armv7",
133 vendor: "apple",
134 os: "ios",
135 env: "",
136 abi: "",
137 }
138 .llvm_target("armv7-apple-ios", None),
139 "armv7-apple-ios"
140 );
141 }
142
143 #[test]
144 fn basic_llvm_triple_guessing() {
145 assert_eq!(
146 TargetInfo {
147 full_arch: "aarch64",
148 arch: "aarch64",
149 vendor: "unknown",
150 os: "linux",
151 env: "",
152 abi: "",
153 }
154 .llvm_target("invalid", None),
155 "aarch64-unknown-linux"
156 );
157 assert_eq!(
158 TargetInfo {
159 full_arch: "x86_64",
160 arch: "x86_64",
161 vendor: "unknown",
162 os: "linux",
163 env: "gnu",
164 abi: "",
165 }
166 .llvm_target("invalid", None),
167 "x86_64-unknown-linux-gnu"
168 );
169 assert_eq!(
170 TargetInfo {
171 full_arch: "x86_64",
172 arch: "x86_64",
173 vendor: "unknown",
174 os: "linux",
175 env: "gnu",
176 abi: "eabi",
177 }
178 .llvm_target("invalid", None),
179 "x86_64-unknown-linux-gnueabi"
180 );
181 assert_eq!(
182 TargetInfo {
183 full_arch: "x86_64",
184 arch: "x86_64",
185 vendor: "apple",
186 os: "macos",
187 env: "",
188 abi: "",
189 }
190 .llvm_target("invalid", None),
191 "x86_64-apple-macosx"
192 );
193 }
194
195 #[test]
196 fn llvm_version() {
197 assert_eq!(
198 TargetInfo {
199 full_arch: "aarch64",
200 arch: "aarch64",
201 vendor: "apple",
202 os: "ios",
203 env: "sim",
204 abi: "",
205 }
206 .llvm_target("aarch64-apple-ios-sim", Some("14.0")),
207 "arm64-apple-ios14.0-simulator"
208 );
209 assert_eq!(
210 TargetInfo {
211 full_arch: "aarch64",
212 arch: "aarch64",
213 vendor: "apple",
214 os: "visionos",
215 env: "",
216 abi: "",
217 }
218 .llvm_target("aarch64-apple-visionos", Some("2.0")),
219 "arm64-apple-xros2.0"
220 );
221 assert_eq!(
222 TargetInfo {
223 full_arch: "aarch64",
224 arch: "aarch64",
225 vendor: "apple",
226 os: "ios",
227 env: "",
228 abi: "macabi",
229 }
230 .llvm_target("aarch64-apple-ios-macabi", Some("13.1")),
231 "arm64-apple-ios13.1-macabi"
232 );
233 }
234
235 #[test]
236 fn uefi() {
237 assert_eq!(
238 TargetInfo {
239 full_arch: "i686",
240 arch: "x86",
241 vendor: "unknown",
242 os: "uefi",
243 env: "",
244 abi: "",
245 }
246 .llvm_target("i686-unknown-uefi", None),
247 "i686-unknown-windows-gnu"
248 );
249 assert_eq!(
250 TargetInfo {
251 full_arch: "x86_64",
252 arch: "x86_64",
253 vendor: "unknown",
254 os: "uefi",
255 env: "",
256 abi: "",
257 }
258 .llvm_target("x86_64-unknown-uefi", None),
259 "x86_64-unknown-windows-gnu"
260 );
261 assert_eq!(
262 TargetInfo {
263 full_arch: "aarch64",
264 arch: "aarch64",
265 vendor: "unknown",
266 os: "uefi",
267 env: "",
268 abi: "",
269 }
270 .llvm_target("aarch64-unknown-uefi", None),
271 "aarch64-unknown-windows-gnu"
272 );
273 }
274
275 #[test]
276 #[ignore = "not yet done"]
277 fn llvm_for_all_rustc_targets() {
278 #[allow(clippy::disallowed_methods)]
279 let rustc = std::env::var("RUSTC").unwrap_or_else(|_| "rustc".to_string());
280
281 let target_list = Command::new(&rustc)
282 .arg("--print=target-list")
283 .output()
284 .unwrap()
285 .stdout;
286 let target_list = String::from_utf8(target_list).unwrap();
287
288 let mut has_failure = false;
289 for target in target_list.lines() {
290 let spec_json = Command::new(&rustc)
291 .arg("--target")
292 .arg(target)
293 .arg("-Zunstable-options")
294 .arg("--print=target-spec-json")
295 .env("RUSTC_BOOTSTRAP", "1") // Crimes
296 .output()
297 .unwrap()
298 .stdout;
299 let spec_json = String::from_utf8(spec_json).unwrap();
300
301 // JSON crimes
302 let expected = spec_json
303 .split_once("llvm-target\": \"")
304 .unwrap()
305 .1
306 .split_once("\"")
307 .unwrap()
308 .0;
309 let actual = TargetInfo::from_rustc_target(target)
310 .map(|target| target.llvm_target("invalid", None));
311
312 if Some(expected) != actual.as_deref().ok() {
313 eprintln!("failed comparing {target}:");
314 eprintln!(" expected: Ok({expected:?})");
315 eprintln!(" actual: {actual:?}");
316 eprintln!();
317 has_failure = true;
318 }
319 }
320
321 if has_failure {
322 panic!("failed comparing targets");
323 }
324 }
325 }