rustc_target/spec/
mod.rs

1//! [Flexible target specification.](https://github.com/rust-lang/rfcs/pull/131)
2//!
3//! Rust targets a wide variety of usecases, and in the interest of flexibility,
4//! allows new target tuples to be defined in configuration files. Most users
5//! will not need to care about these, but this is invaluable when porting Rust
6//! to a new platform, and allows for an unprecedented level of control over how
7//! the compiler works.
8//!
9//! # Using targets and target.json
10//!
11//! Invoking "rustc --target=${TUPLE}" will result in rustc initiating the [`Target::search`] by
12//! - checking if "$TUPLE" is a complete path to a json (ending with ".json") and loading if so
13//! - checking builtin targets for "${TUPLE}"
14//! - checking directories in "${RUST_TARGET_PATH}" for "${TUPLE}.json"
15//! - checking for "${RUSTC_SYSROOT}/lib/rustlib/${TUPLE}/target.json"
16//!
17//! Code will then be compiled using the first discovered target spec.
18//!
19//! # Defining a new target
20//!
21//! Targets are defined using a struct which additionally has serialization to and from [JSON].
22//! The `Target` struct in this module loosely corresponds with the format the JSON takes.
23//! We usually try to make the fields equivalent but we have given up on a 1:1 correspondence
24//! between the JSON and the actual structure itself.
25//!
26//! Some fields are required in every target spec, and they should be embedded in Target directly.
27//! Optional keys are in TargetOptions, but Target derefs to it, for no practical difference.
28//! Most notable is the "data-layout" field which specifies Rust's notion of sizes and alignments
29//! for several key types, such as f64, pointers, and so on.
30//!
31//! At one point we felt `-C` options should override the target's settings, like in C compilers,
32//! but that was an essentially-unmarked route for making code incorrect and Rust unsound.
33//! Confronted with programmers who prefer a compiler with a good UX instead of a lethal weapon,
34//! we have almost-entirely recanted that notion, though we hope "target modifiers" will offer
35//! a way to have a decent UX yet still extend the necessary compiler controls, without
36//! requiring a new target spec for each and every single possible target micro-variant.
37//!
38//! [JSON]: https://json.org
39
40use std::borrow::Cow;
41use std::collections::BTreeMap;
42use std::hash::{Hash, Hasher};
43use std::ops::{Deref, DerefMut};
44use std::path::{Path, PathBuf};
45use std::str::FromStr;
46use std::{fmt, io};
47
48use rustc_abi::{
49    Align, CanonAbi, Endian, ExternAbi, Integer, Size, TargetDataLayout, TargetDataLayoutErrors,
50};
51use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
52use rustc_fs_util::try_canonicalize;
53use rustc_macros::{Decodable, Encodable, HashStable_Generic};
54use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
55use rustc_span::{Symbol, kw, sym};
56use serde_json::Value;
57use tracing::debug;
58
59use crate::json::{Json, ToJson};
60use crate::spec::crt_objects::CrtObjects;
61
62pub mod crt_objects;
63
64mod abi_map;
65mod base;
66mod json;
67
68pub use abi_map::{AbiMap, AbiMapping};
69pub use base::apple;
70pub use base::avr::ef_avr_arch;
71
72/// Linker is called through a C/C++ compiler.
73#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
74pub enum Cc {
75    Yes,
76    No,
77}
78
79/// Linker is LLD.
80#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
81pub enum Lld {
82    Yes,
83    No,
84}
85
86/// All linkers have some kinds of command line interfaces and rustc needs to know which commands
87/// to use with each of them. So we cluster all such interfaces into a (somewhat arbitrary) number
88/// of classes that we call "linker flavors".
89///
90/// Technically, it's not even necessary, we can nearly always infer the flavor from linker name
91/// and target properties like `is_like_windows`/`is_like_darwin`/etc. However, the PRs originally
92/// introducing `-Clinker-flavor` (#40018 and friends) were aiming to reduce this kind of inference
93/// and provide something certain and explicitly specified instead, and that design goal is still
94/// relevant now.
95///
96/// The second goal is to keep the number of flavors to the minimum if possible.
97/// LLD somewhat forces our hand here because that linker is self-sufficient only if its executable
98/// (`argv[0]`) is named in specific way, otherwise it doesn't work and requires a
99/// `-flavor LLD_FLAVOR` argument to choose which logic to use. Our shipped `rust-lld` in
100/// particular is not named in such specific way, so it needs the flavor option, so we make our
101/// linker flavors sufficiently fine-grained to satisfy LLD without inferring its flavor from other
102/// target properties, in accordance with the first design goal.
103///
104/// The first component of the flavor is tightly coupled with the compilation target,
105/// while the `Cc` and `Lld` flags can vary within the same target.
106#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
107pub enum LinkerFlavor {
108    /// Unix-like linker with GNU extensions (both naked and compiler-wrapped forms).
109    /// Besides similar "default" Linux/BSD linkers this also includes Windows/GNU linker,
110    /// which is somewhat different because it doesn't produce ELFs.
111    Gnu(Cc, Lld),
112    /// Unix-like linker for Apple targets (both naked and compiler-wrapped forms).
113    /// Extracted from the "umbrella" `Unix` flavor due to its corresponding LLD flavor.
114    Darwin(Cc, Lld),
115    /// Unix-like linker for Wasm targets (both naked and compiler-wrapped forms).
116    /// Extracted from the "umbrella" `Unix` flavor due to its corresponding LLD flavor.
117    /// Non-LLD version does not exist, so the lld flag is currently hardcoded here.
118    WasmLld(Cc),
119    /// Basic Unix-like linker for "any other Unix" targets (Solaris/illumos, L4Re, MSP430, etc),
120    /// possibly with non-GNU extensions (both naked and compiler-wrapped forms).
121    /// LLD doesn't support any of these.
122    Unix(Cc),
123    /// MSVC-style linker for Windows and UEFI, LLD supports it.
124    Msvc(Lld),
125    /// Emscripten Compiler Frontend, a wrapper around `WasmLld(Cc::Yes)` that has a different
126    /// interface and produces some additional JavaScript output.
127    EmCc,
128    // Below: other linker-like tools with unique interfaces for exotic targets.
129    /// Linker tool for BPF.
130    Bpf,
131    /// Linker tool for Nvidia PTX.
132    Ptx,
133    /// LLVM bitcode linker that can be used as a `self-contained` linker
134    Llbc,
135}
136
137/// Linker flavors available externally through command line (`-Clinker-flavor`)
138/// or json target specifications.
139/// This set has accumulated historically, and contains both (stable and unstable) legacy values, as
140/// well as modern ones matching the internal linker flavors (`LinkerFlavor`).
141#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
142pub enum LinkerFlavorCli {
143    // Modern (unstable) flavors, with direct counterparts in `LinkerFlavor`.
144    Gnu(Cc, Lld),
145    Darwin(Cc, Lld),
146    WasmLld(Cc),
147    Unix(Cc),
148    // Note: `Msvc(Lld::No)` is also a stable value.
149    Msvc(Lld),
150    EmCc,
151    Bpf,
152    Ptx,
153    Llbc,
154
155    // Legacy stable values
156    Gcc,
157    Ld,
158    Lld(LldFlavor),
159    Em,
160}
161
162impl LinkerFlavorCli {
163    /// Returns whether this `-C linker-flavor` option is one of the unstable values.
164    pub fn is_unstable(&self) -> bool {
165        match self {
166            LinkerFlavorCli::Gnu(..)
167            | LinkerFlavorCli::Darwin(..)
168            | LinkerFlavorCli::WasmLld(..)
169            | LinkerFlavorCli::Unix(..)
170            | LinkerFlavorCli::Msvc(Lld::Yes)
171            | LinkerFlavorCli::EmCc
172            | LinkerFlavorCli::Bpf
173            | LinkerFlavorCli::Llbc
174            | LinkerFlavorCli::Ptx => true,
175            LinkerFlavorCli::Gcc
176            | LinkerFlavorCli::Ld
177            | LinkerFlavorCli::Lld(..)
178            | LinkerFlavorCli::Msvc(Lld::No)
179            | LinkerFlavorCli::Em => false,
180        }
181    }
182}
183
184#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
185pub enum LldFlavor {
186    Wasm,
187    Ld64,
188    Ld,
189    Link,
190}
191
192impl LldFlavor {
193    pub fn as_str(&self) -> &'static str {
194        match self {
195            LldFlavor::Wasm => "wasm",
196            LldFlavor::Ld64 => "darwin",
197            LldFlavor::Ld => "gnu",
198            LldFlavor::Link => "link",
199        }
200    }
201
202    fn from_str(s: &str) -> Option<Self> {
203        Some(match s {
204            "darwin" => LldFlavor::Ld64,
205            "gnu" => LldFlavor::Ld,
206            "link" => LldFlavor::Link,
207            "wasm" => LldFlavor::Wasm,
208            _ => return None,
209        })
210    }
211}
212
213impl ToJson for LldFlavor {
214    fn to_json(&self) -> Json {
215        self.as_str().to_json()
216    }
217}
218
219impl LinkerFlavor {
220    /// At this point the target's reference linker flavor doesn't yet exist and we need to infer
221    /// it. The inference always succeeds and gives some result, and we don't report any flavor
222    /// incompatibility errors for json target specs. The CLI flavor is used as the main source
223    /// of truth, other flags are used in case of ambiguities.
224    fn from_cli_json(cli: LinkerFlavorCli, lld_flavor: LldFlavor, is_gnu: bool) -> LinkerFlavor {
225        match cli {
226            LinkerFlavorCli::Gnu(cc, lld) => LinkerFlavor::Gnu(cc, lld),
227            LinkerFlavorCli::Darwin(cc, lld) => LinkerFlavor::Darwin(cc, lld),
228            LinkerFlavorCli::WasmLld(cc) => LinkerFlavor::WasmLld(cc),
229            LinkerFlavorCli::Unix(cc) => LinkerFlavor::Unix(cc),
230            LinkerFlavorCli::Msvc(lld) => LinkerFlavor::Msvc(lld),
231            LinkerFlavorCli::EmCc => LinkerFlavor::EmCc,
232            LinkerFlavorCli::Bpf => LinkerFlavor::Bpf,
233            LinkerFlavorCli::Llbc => LinkerFlavor::Llbc,
234            LinkerFlavorCli::Ptx => LinkerFlavor::Ptx,
235
236            // Below: legacy stable values
237            LinkerFlavorCli::Gcc => match lld_flavor {
238                LldFlavor::Ld if is_gnu => LinkerFlavor::Gnu(Cc::Yes, Lld::No),
239                LldFlavor::Ld64 => LinkerFlavor::Darwin(Cc::Yes, Lld::No),
240                LldFlavor::Wasm => LinkerFlavor::WasmLld(Cc::Yes),
241                LldFlavor::Ld | LldFlavor::Link => LinkerFlavor::Unix(Cc::Yes),
242            },
243            LinkerFlavorCli::Ld => match lld_flavor {
244                LldFlavor::Ld if is_gnu => LinkerFlavor::Gnu(Cc::No, Lld::No),
245                LldFlavor::Ld64 => LinkerFlavor::Darwin(Cc::No, Lld::No),
246                LldFlavor::Ld | LldFlavor::Wasm | LldFlavor::Link => LinkerFlavor::Unix(Cc::No),
247            },
248            LinkerFlavorCli::Lld(LldFlavor::Ld) => LinkerFlavor::Gnu(Cc::No, Lld::Yes),
249            LinkerFlavorCli::Lld(LldFlavor::Ld64) => LinkerFlavor::Darwin(Cc::No, Lld::Yes),
250            LinkerFlavorCli::Lld(LldFlavor::Wasm) => LinkerFlavor::WasmLld(Cc::No),
251            LinkerFlavorCli::Lld(LldFlavor::Link) => LinkerFlavor::Msvc(Lld::Yes),
252            LinkerFlavorCli::Em => LinkerFlavor::EmCc,
253        }
254    }
255
256    /// Returns the corresponding backwards-compatible CLI flavor.
257    fn to_cli(self) -> LinkerFlavorCli {
258        match self {
259            LinkerFlavor::Gnu(Cc::Yes, _)
260            | LinkerFlavor::Darwin(Cc::Yes, _)
261            | LinkerFlavor::WasmLld(Cc::Yes)
262            | LinkerFlavor::Unix(Cc::Yes) => LinkerFlavorCli::Gcc,
263            LinkerFlavor::Gnu(_, Lld::Yes) => LinkerFlavorCli::Lld(LldFlavor::Ld),
264            LinkerFlavor::Darwin(_, Lld::Yes) => LinkerFlavorCli::Lld(LldFlavor::Ld64),
265            LinkerFlavor::WasmLld(..) => LinkerFlavorCli::Lld(LldFlavor::Wasm),
266            LinkerFlavor::Gnu(..) | LinkerFlavor::Darwin(..) | LinkerFlavor::Unix(..) => {
267                LinkerFlavorCli::Ld
268            }
269            LinkerFlavor::Msvc(Lld::Yes) => LinkerFlavorCli::Lld(LldFlavor::Link),
270            LinkerFlavor::Msvc(..) => LinkerFlavorCli::Msvc(Lld::No),
271            LinkerFlavor::EmCc => LinkerFlavorCli::Em,
272            LinkerFlavor::Bpf => LinkerFlavorCli::Bpf,
273            LinkerFlavor::Llbc => LinkerFlavorCli::Llbc,
274            LinkerFlavor::Ptx => LinkerFlavorCli::Ptx,
275        }
276    }
277
278    /// Returns the modern CLI flavor that is the counterpart of this flavor.
279    fn to_cli_counterpart(self) -> LinkerFlavorCli {
280        match self {
281            LinkerFlavor::Gnu(cc, lld) => LinkerFlavorCli::Gnu(cc, lld),
282            LinkerFlavor::Darwin(cc, lld) => LinkerFlavorCli::Darwin(cc, lld),
283            LinkerFlavor::WasmLld(cc) => LinkerFlavorCli::WasmLld(cc),
284            LinkerFlavor::Unix(cc) => LinkerFlavorCli::Unix(cc),
285            LinkerFlavor::Msvc(lld) => LinkerFlavorCli::Msvc(lld),
286            LinkerFlavor::EmCc => LinkerFlavorCli::EmCc,
287            LinkerFlavor::Bpf => LinkerFlavorCli::Bpf,
288            LinkerFlavor::Llbc => LinkerFlavorCli::Llbc,
289            LinkerFlavor::Ptx => LinkerFlavorCli::Ptx,
290        }
291    }
292
293    fn infer_cli_hints(cli: LinkerFlavorCli) -> (Option<Cc>, Option<Lld>) {
294        match cli {
295            LinkerFlavorCli::Gnu(cc, lld) | LinkerFlavorCli::Darwin(cc, lld) => {
296                (Some(cc), Some(lld))
297            }
298            LinkerFlavorCli::WasmLld(cc) => (Some(cc), Some(Lld::Yes)),
299            LinkerFlavorCli::Unix(cc) => (Some(cc), None),
300            LinkerFlavorCli::Msvc(lld) => (Some(Cc::No), Some(lld)),
301            LinkerFlavorCli::EmCc => (Some(Cc::Yes), Some(Lld::Yes)),
302            LinkerFlavorCli::Bpf | LinkerFlavorCli::Ptx => (None, None),
303            LinkerFlavorCli::Llbc => (None, None),
304
305            // Below: legacy stable values
306            LinkerFlavorCli::Gcc => (Some(Cc::Yes), None),
307            LinkerFlavorCli::Ld => (Some(Cc::No), Some(Lld::No)),
308            LinkerFlavorCli::Lld(_) => (Some(Cc::No), Some(Lld::Yes)),
309            LinkerFlavorCli::Em => (Some(Cc::Yes), Some(Lld::Yes)),
310        }
311    }
312
313    fn infer_linker_hints(linker_stem: &str) -> Result<Self, (Option<Cc>, Option<Lld>)> {
314        // Remove any version postfix.
315        let stem = linker_stem
316            .rsplit_once('-')
317            .and_then(|(lhs, rhs)| rhs.chars().all(char::is_numeric).then_some(lhs))
318            .unwrap_or(linker_stem);
319
320        if stem == "llvm-bitcode-linker" {
321            Ok(Self::Llbc)
322        } else if stem == "emcc" // GCC/Clang can have an optional target prefix.
323            || stem == "gcc"
324            || stem.ends_with("-gcc")
325            || stem == "g++"
326            || stem.ends_with("-g++")
327            || stem == "clang"
328            || stem.ends_with("-clang")
329            || stem == "clang++"
330            || stem.ends_with("-clang++")
331        {
332            Err((Some(Cc::Yes), Some(Lld::No)))
333        } else if stem == "wasm-ld"
334            || stem.ends_with("-wasm-ld")
335            || stem == "ld.lld"
336            || stem == "lld"
337            || stem == "rust-lld"
338            || stem == "lld-link"
339        {
340            Err((Some(Cc::No), Some(Lld::Yes)))
341        } else if stem == "ld" || stem.ends_with("-ld") || stem == "link" {
342            Err((Some(Cc::No), Some(Lld::No)))
343        } else {
344            Err((None, None))
345        }
346    }
347
348    fn with_hints(self, (cc_hint, lld_hint): (Option<Cc>, Option<Lld>)) -> LinkerFlavor {
349        match self {
350            LinkerFlavor::Gnu(cc, lld) => {
351                LinkerFlavor::Gnu(cc_hint.unwrap_or(cc), lld_hint.unwrap_or(lld))
352            }
353            LinkerFlavor::Darwin(cc, lld) => {
354                LinkerFlavor::Darwin(cc_hint.unwrap_or(cc), lld_hint.unwrap_or(lld))
355            }
356            LinkerFlavor::WasmLld(cc) => LinkerFlavor::WasmLld(cc_hint.unwrap_or(cc)),
357            LinkerFlavor::Unix(cc) => LinkerFlavor::Unix(cc_hint.unwrap_or(cc)),
358            LinkerFlavor::Msvc(lld) => LinkerFlavor::Msvc(lld_hint.unwrap_or(lld)),
359            LinkerFlavor::EmCc | LinkerFlavor::Bpf | LinkerFlavor::Llbc | LinkerFlavor::Ptx => self,
360        }
361    }
362
363    pub fn with_cli_hints(self, cli: LinkerFlavorCli) -> LinkerFlavor {
364        self.with_hints(LinkerFlavor::infer_cli_hints(cli))
365    }
366
367    pub fn with_linker_hints(self, linker_stem: &str) -> LinkerFlavor {
368        match LinkerFlavor::infer_linker_hints(linker_stem) {
369            Ok(linker_flavor) => linker_flavor,
370            Err(hints) => self.with_hints(hints),
371        }
372    }
373
374    pub fn check_compatibility(self, cli: LinkerFlavorCli) -> Option<String> {
375        let compatible = |cli| {
376            // The CLI flavor should be compatible with the target if:
377            match (self, cli) {
378                // 1. they are counterparts: they have the same principal flavor.
379                (LinkerFlavor::Gnu(..), LinkerFlavorCli::Gnu(..))
380                | (LinkerFlavor::Darwin(..), LinkerFlavorCli::Darwin(..))
381                | (LinkerFlavor::WasmLld(..), LinkerFlavorCli::WasmLld(..))
382                | (LinkerFlavor::Unix(..), LinkerFlavorCli::Unix(..))
383                | (LinkerFlavor::Msvc(..), LinkerFlavorCli::Msvc(..))
384                | (LinkerFlavor::EmCc, LinkerFlavorCli::EmCc)
385                | (LinkerFlavor::Bpf, LinkerFlavorCli::Bpf)
386                | (LinkerFlavor::Llbc, LinkerFlavorCli::Llbc)
387                | (LinkerFlavor::Ptx, LinkerFlavorCli::Ptx) => return true,
388                // 2. The linker flavor is independent of target and compatible
389                (LinkerFlavor::Ptx, LinkerFlavorCli::Llbc) => return true,
390                _ => {}
391            }
392
393            // 3. or, the flavor is legacy and survives this roundtrip.
394            cli == self.with_cli_hints(cli).to_cli()
395        };
396        (!compatible(cli)).then(|| {
397            LinkerFlavorCli::all()
398                .iter()
399                .filter(|cli| compatible(**cli))
400                .map(|cli| cli.desc())
401                .intersperse(", ")
402                .collect()
403        })
404    }
405
406    pub fn lld_flavor(self) -> LldFlavor {
407        match self {
408            LinkerFlavor::Gnu(..)
409            | LinkerFlavor::Unix(..)
410            | LinkerFlavor::EmCc
411            | LinkerFlavor::Bpf
412            | LinkerFlavor::Llbc
413            | LinkerFlavor::Ptx => LldFlavor::Ld,
414            LinkerFlavor::Darwin(..) => LldFlavor::Ld64,
415            LinkerFlavor::WasmLld(..) => LldFlavor::Wasm,
416            LinkerFlavor::Msvc(..) => LldFlavor::Link,
417        }
418    }
419
420    pub fn is_gnu(self) -> bool {
421        matches!(self, LinkerFlavor::Gnu(..))
422    }
423
424    /// Returns whether the flavor uses the `lld` linker.
425    pub fn uses_lld(self) -> bool {
426        // Exhaustive match in case new flavors are added in the future.
427        match self {
428            LinkerFlavor::Gnu(_, Lld::Yes)
429            | LinkerFlavor::Darwin(_, Lld::Yes)
430            | LinkerFlavor::WasmLld(..)
431            | LinkerFlavor::EmCc
432            | LinkerFlavor::Msvc(Lld::Yes) => true,
433            LinkerFlavor::Gnu(..)
434            | LinkerFlavor::Darwin(..)
435            | LinkerFlavor::Msvc(_)
436            | LinkerFlavor::Unix(_)
437            | LinkerFlavor::Bpf
438            | LinkerFlavor::Llbc
439            | LinkerFlavor::Ptx => false,
440        }
441    }
442
443    /// Returns whether the flavor calls the linker via a C/C++ compiler.
444    pub fn uses_cc(self) -> bool {
445        // Exhaustive match in case new flavors are added in the future.
446        match self {
447            LinkerFlavor::Gnu(Cc::Yes, _)
448            | LinkerFlavor::Darwin(Cc::Yes, _)
449            | LinkerFlavor::WasmLld(Cc::Yes)
450            | LinkerFlavor::Unix(Cc::Yes)
451            | LinkerFlavor::EmCc => true,
452            LinkerFlavor::Gnu(..)
453            | LinkerFlavor::Darwin(..)
454            | LinkerFlavor::WasmLld(_)
455            | LinkerFlavor::Msvc(_)
456            | LinkerFlavor::Unix(_)
457            | LinkerFlavor::Bpf
458            | LinkerFlavor::Llbc
459            | LinkerFlavor::Ptx => false,
460        }
461    }
462
463    /// For flavors with an `Lld` component, ensure it's enabled. Otherwise, returns the given
464    /// flavor unmodified.
465    pub fn with_lld_enabled(self) -> LinkerFlavor {
466        match self {
467            LinkerFlavor::Gnu(cc, Lld::No) => LinkerFlavor::Gnu(cc, Lld::Yes),
468            LinkerFlavor::Darwin(cc, Lld::No) => LinkerFlavor::Darwin(cc, Lld::Yes),
469            LinkerFlavor::Msvc(Lld::No) => LinkerFlavor::Msvc(Lld::Yes),
470            _ => self,
471        }
472    }
473
474    /// For flavors with an `Lld` component, ensure it's disabled. Otherwise, returns the given
475    /// flavor unmodified.
476    pub fn with_lld_disabled(self) -> LinkerFlavor {
477        match self {
478            LinkerFlavor::Gnu(cc, Lld::Yes) => LinkerFlavor::Gnu(cc, Lld::No),
479            LinkerFlavor::Darwin(cc, Lld::Yes) => LinkerFlavor::Darwin(cc, Lld::No),
480            LinkerFlavor::Msvc(Lld::Yes) => LinkerFlavor::Msvc(Lld::No),
481            _ => self,
482        }
483    }
484}
485
486macro_rules! linker_flavor_cli_impls {
487    ($(($($flavor:tt)*) $string:literal)*) => (
488        impl LinkerFlavorCli {
489            const fn all() -> &'static [LinkerFlavorCli] {
490                &[$($($flavor)*,)*]
491            }
492
493            pub const fn one_of() -> &'static str {
494                concat!("one of: ", $($string, " ",)*)
495            }
496
497            pub fn from_str(s: &str) -> Option<LinkerFlavorCli> {
498                Some(match s {
499                    $($string => $($flavor)*,)*
500                    _ => return None,
501                })
502            }
503
504            pub fn desc(self) -> &'static str {
505                match self {
506                    $($($flavor)* => $string,)*
507                }
508            }
509        }
510    )
511}
512
513linker_flavor_cli_impls! {
514    (LinkerFlavorCli::Gnu(Cc::No, Lld::No)) "gnu"
515    (LinkerFlavorCli::Gnu(Cc::No, Lld::Yes)) "gnu-lld"
516    (LinkerFlavorCli::Gnu(Cc::Yes, Lld::No)) "gnu-cc"
517    (LinkerFlavorCli::Gnu(Cc::Yes, Lld::Yes)) "gnu-lld-cc"
518    (LinkerFlavorCli::Darwin(Cc::No, Lld::No)) "darwin"
519    (LinkerFlavorCli::Darwin(Cc::No, Lld::Yes)) "darwin-lld"
520    (LinkerFlavorCli::Darwin(Cc::Yes, Lld::No)) "darwin-cc"
521    (LinkerFlavorCli::Darwin(Cc::Yes, Lld::Yes)) "darwin-lld-cc"
522    (LinkerFlavorCli::WasmLld(Cc::No)) "wasm-lld"
523    (LinkerFlavorCli::WasmLld(Cc::Yes)) "wasm-lld-cc"
524    (LinkerFlavorCli::Unix(Cc::No)) "unix"
525    (LinkerFlavorCli::Unix(Cc::Yes)) "unix-cc"
526    (LinkerFlavorCli::Msvc(Lld::Yes)) "msvc-lld"
527    (LinkerFlavorCli::Msvc(Lld::No)) "msvc"
528    (LinkerFlavorCli::EmCc) "em-cc"
529    (LinkerFlavorCli::Bpf) "bpf"
530    (LinkerFlavorCli::Llbc) "llbc"
531    (LinkerFlavorCli::Ptx) "ptx"
532
533    // Legacy stable flavors
534    (LinkerFlavorCli::Gcc) "gcc"
535    (LinkerFlavorCli::Ld) "ld"
536    (LinkerFlavorCli::Lld(LldFlavor::Ld)) "ld.lld"
537    (LinkerFlavorCli::Lld(LldFlavor::Ld64)) "ld64.lld"
538    (LinkerFlavorCli::Lld(LldFlavor::Link)) "lld-link"
539    (LinkerFlavorCli::Lld(LldFlavor::Wasm)) "wasm-ld"
540    (LinkerFlavorCli::Em) "em"
541}
542
543impl ToJson for LinkerFlavorCli {
544    fn to_json(&self) -> Json {
545        self.desc().to_json()
546    }
547}
548
549/// The different `-Clink-self-contained` options that can be specified in a target spec:
550/// - enabling or disabling in bulk
551/// - some target-specific pieces of inference to determine whether to use self-contained linking
552///   if `-Clink-self-contained` is not specified explicitly (e.g. on musl/mingw)
553/// - explicitly enabling some of the self-contained linking components, e.g. the linker component
554///   to use `rust-lld`
555#[derive(Clone, Copy, PartialEq, Debug)]
556pub enum LinkSelfContainedDefault {
557    /// The target spec explicitly enables self-contained linking.
558    True,
559
560    /// The target spec explicitly disables self-contained linking.
561    False,
562
563    /// The target spec requests that the self-contained mode is inferred, in the context of musl.
564    InferredForMusl,
565
566    /// The target spec requests that the self-contained mode is inferred, in the context of mingw.
567    InferredForMingw,
568
569    /// The target spec explicitly enables a list of self-contained linking components: e.g. for
570    /// targets opting into a subset of components like the CLI's `-C link-self-contained=+linker`.
571    WithComponents(LinkSelfContainedComponents),
572}
573
574/// Parses a backwards-compatible `-Clink-self-contained` option string, without components.
575impl FromStr for LinkSelfContainedDefault {
576    type Err = ();
577
578    fn from_str(s: &str) -> Result<LinkSelfContainedDefault, ()> {
579        Ok(match s {
580            "false" => LinkSelfContainedDefault::False,
581            "true" | "wasm" => LinkSelfContainedDefault::True,
582            "musl" => LinkSelfContainedDefault::InferredForMusl,
583            "mingw" => LinkSelfContainedDefault::InferredForMingw,
584            _ => return Err(()),
585        })
586    }
587}
588
589impl ToJson for LinkSelfContainedDefault {
590    fn to_json(&self) -> Json {
591        match *self {
592            LinkSelfContainedDefault::WithComponents(components) => {
593                // Serialize the components in a json object's `components` field, to prepare for a
594                // future where `crt-objects-fallback` is removed from the json specs and
595                // incorporated as a field here.
596                let mut map = BTreeMap::new();
597                map.insert("components", components);
598                map.to_json()
599            }
600
601            // Stable backwards-compatible values
602            LinkSelfContainedDefault::True => "true".to_json(),
603            LinkSelfContainedDefault::False => "false".to_json(),
604            LinkSelfContainedDefault::InferredForMusl => "musl".to_json(),
605            LinkSelfContainedDefault::InferredForMingw => "mingw".to_json(),
606        }
607    }
608}
609
610impl LinkSelfContainedDefault {
611    /// Returns whether the target spec has self-contained linking explicitly disabled. Used to emit
612    /// errors if the user then enables it on the CLI.
613    pub fn is_disabled(self) -> bool {
614        self == LinkSelfContainedDefault::False
615    }
616
617    /// Returns the key to use when serializing the setting to json:
618    /// - individual components in a `link-self-contained` object value
619    /// - the other variants as a backwards-compatible `crt-objects-fallback` string
620    fn json_key(self) -> &'static str {
621        match self {
622            LinkSelfContainedDefault::WithComponents(_) => "link-self-contained",
623            _ => "crt-objects-fallback",
624        }
625    }
626
627    /// Creates a `LinkSelfContainedDefault` enabling the self-contained linker for target specs
628    /// (the equivalent of `-Clink-self-contained=+linker` on the CLI).
629    pub fn with_linker() -> LinkSelfContainedDefault {
630        LinkSelfContainedDefault::WithComponents(LinkSelfContainedComponents::LINKER)
631    }
632}
633
634bitflags::bitflags! {
635    #[derive(Clone, Copy, PartialEq, Eq, Default)]
636    /// The `-C link-self-contained` components that can individually be enabled or disabled.
637    pub struct LinkSelfContainedComponents: u8 {
638        /// CRT objects (e.g. on `windows-gnu`, `musl`, `wasi` targets)
639        const CRT_OBJECTS = 1 << 0;
640        /// libc static library (e.g. on `musl`, `wasi` targets)
641        const LIBC        = 1 << 1;
642        /// libgcc/libunwind (e.g. on `windows-gnu`, `fuchsia`, `fortanix`, `gnullvm` targets)
643        const UNWIND      = 1 << 2;
644        /// Linker, dlltool, and their necessary libraries (e.g. on `windows-gnu` and for `rust-lld`)
645        const LINKER      = 1 << 3;
646        /// Sanitizer runtime libraries
647        const SANITIZERS  = 1 << 4;
648        /// Other MinGW libs and Windows import libs
649        const MINGW       = 1 << 5;
650    }
651}
652rustc_data_structures::external_bitflags_debug! { LinkSelfContainedComponents }
653
654impl LinkSelfContainedComponents {
655    /// Parses a single `-Clink-self-contained` well-known component, not a set of flags.
656    pub fn from_str(s: &str) -> Option<LinkSelfContainedComponents> {
657        Some(match s {
658            "crto" => LinkSelfContainedComponents::CRT_OBJECTS,
659            "libc" => LinkSelfContainedComponents::LIBC,
660            "unwind" => LinkSelfContainedComponents::UNWIND,
661            "linker" => LinkSelfContainedComponents::LINKER,
662            "sanitizers" => LinkSelfContainedComponents::SANITIZERS,
663            "mingw" => LinkSelfContainedComponents::MINGW,
664            _ => return None,
665        })
666    }
667
668    /// Return the component's name.
669    ///
670    /// Returns `None` if the bitflags aren't a singular component (but a mix of multiple flags).
671    pub fn as_str(self) -> Option<&'static str> {
672        Some(match self {
673            LinkSelfContainedComponents::CRT_OBJECTS => "crto",
674            LinkSelfContainedComponents::LIBC => "libc",
675            LinkSelfContainedComponents::UNWIND => "unwind",
676            LinkSelfContainedComponents::LINKER => "linker",
677            LinkSelfContainedComponents::SANITIZERS => "sanitizers",
678            LinkSelfContainedComponents::MINGW => "mingw",
679            _ => return None,
680        })
681    }
682
683    /// Returns an array of all the components.
684    fn all_components() -> [LinkSelfContainedComponents; 6] {
685        [
686            LinkSelfContainedComponents::CRT_OBJECTS,
687            LinkSelfContainedComponents::LIBC,
688            LinkSelfContainedComponents::UNWIND,
689            LinkSelfContainedComponents::LINKER,
690            LinkSelfContainedComponents::SANITIZERS,
691            LinkSelfContainedComponents::MINGW,
692        ]
693    }
694
695    /// Returns whether at least a component is enabled.
696    pub fn are_any_components_enabled(self) -> bool {
697        !self.is_empty()
698    }
699
700    /// Returns whether `LinkSelfContainedComponents::LINKER` is enabled.
701    pub fn is_linker_enabled(self) -> bool {
702        self.contains(LinkSelfContainedComponents::LINKER)
703    }
704
705    /// Returns whether `LinkSelfContainedComponents::CRT_OBJECTS` is enabled.
706    pub fn is_crt_objects_enabled(self) -> bool {
707        self.contains(LinkSelfContainedComponents::CRT_OBJECTS)
708    }
709}
710
711impl ToJson for LinkSelfContainedComponents {
712    fn to_json(&self) -> Json {
713        let components: Vec<_> = Self::all_components()
714            .into_iter()
715            .filter(|c| self.contains(*c))
716            .map(|c| {
717                // We can unwrap because we're iterating over all the known singular components,
718                // not an actual set of flags where `as_str` can fail.
719                c.as_str().unwrap().to_owned()
720            })
721            .collect();
722
723        components.to_json()
724    }
725}
726
727bitflags::bitflags! {
728    /// The `-Z linker-features` components that can individually be enabled or disabled.
729    ///
730    /// They are feature flags intended to be a more flexible mechanism than linker flavors, and
731    /// also to prevent a combinatorial explosion of flavors whenever a new linker feature is
732    /// required. These flags are "generic", in the sense that they can work on multiple targets on
733    /// the CLI. Otherwise, one would have to select different linkers flavors for each target.
734    ///
735    /// Here are some examples of the advantages they offer:
736    /// - default feature sets for principal flavors, or for specific targets.
737    /// - flavor-specific features: for example, clang offers automatic cross-linking with
738    ///   `--target`, which gcc-style compilers don't support. The *flavor* is still a C/C++
739    ///   compiler, and we don't need to multiply the number of flavors for this use-case. Instead,
740    ///   we can have a single `+target` feature.
741    /// - umbrella features: for example if clang accumulates more features in the future than just
742    ///   the `+target` above. That could be modeled as `+clang`.
743    /// - niche features for resolving specific issues: for example, on Apple targets the linker
744    ///   flag implementing the `as-needed` native link modifier (#99424) is only possible on
745    ///   sufficiently recent linker versions.
746    /// - still allows for discovery and automation, for example via feature detection. This can be
747    ///   useful in exotic environments/build systems.
748    #[derive(Clone, Copy, PartialEq, Eq, Default)]
749    pub struct LinkerFeatures: u8 {
750        /// Invoke the linker via a C/C++ compiler (e.g. on most unix targets).
751        const CC  = 1 << 0;
752        /// Use the lld linker, either the system lld or the self-contained linker `rust-lld`.
753        const LLD = 1 << 1;
754    }
755}
756rustc_data_structures::external_bitflags_debug! { LinkerFeatures }
757
758impl LinkerFeatures {
759    /// Parses a single `-Z linker-features` well-known feature, not a set of flags.
760    pub fn from_str(s: &str) -> Option<LinkerFeatures> {
761        Some(match s {
762            "cc" => LinkerFeatures::CC,
763            "lld" => LinkerFeatures::LLD,
764            _ => return None,
765        })
766    }
767
768    /// Returns whether the `lld` linker feature is enabled.
769    pub fn is_lld_enabled(self) -> bool {
770        self.contains(LinkerFeatures::LLD)
771    }
772
773    /// Returns whether the `cc` linker feature is enabled.
774    pub fn is_cc_enabled(self) -> bool {
775        self.contains(LinkerFeatures::CC)
776    }
777}
778
779#[derive(Clone, Copy, Debug, PartialEq, Hash, Encodable, Decodable, HashStable_Generic)]
780pub enum PanicStrategy {
781    Unwind,
782    Abort,
783}
784
785#[derive(Clone, Copy, Debug, PartialEq, Hash, Encodable, Decodable, HashStable_Generic)]
786pub enum OnBrokenPipe {
787    Default,
788    Kill,
789    Error,
790    Inherit,
791}
792
793impl PanicStrategy {
794    pub fn desc(&self) -> &str {
795        match *self {
796            PanicStrategy::Unwind => "unwind",
797            PanicStrategy::Abort => "abort",
798        }
799    }
800
801    pub const fn desc_symbol(&self) -> Symbol {
802        match *self {
803            PanicStrategy::Unwind => sym::unwind,
804            PanicStrategy::Abort => sym::abort,
805        }
806    }
807
808    pub const fn all() -> [Symbol; 2] {
809        [Self::Abort.desc_symbol(), Self::Unwind.desc_symbol()]
810    }
811}
812
813impl ToJson for PanicStrategy {
814    fn to_json(&self) -> Json {
815        match *self {
816            PanicStrategy::Abort => "abort".to_json(),
817            PanicStrategy::Unwind => "unwind".to_json(),
818        }
819    }
820}
821
822#[derive(Clone, Copy, Debug, PartialEq, Hash)]
823pub enum RelroLevel {
824    Full,
825    Partial,
826    Off,
827    None,
828}
829
830impl RelroLevel {
831    pub fn desc(&self) -> &str {
832        match *self {
833            RelroLevel::Full => "full",
834            RelroLevel::Partial => "partial",
835            RelroLevel::Off => "off",
836            RelroLevel::None => "none",
837        }
838    }
839}
840
841#[derive(Clone, Copy, Debug, PartialEq, Hash)]
842pub enum SymbolVisibility {
843    Hidden,
844    Protected,
845    Interposable,
846}
847
848impl SymbolVisibility {
849    pub fn desc(&self) -> &str {
850        match *self {
851            SymbolVisibility::Hidden => "hidden",
852            SymbolVisibility::Protected => "protected",
853            SymbolVisibility::Interposable => "interposable",
854        }
855    }
856}
857
858impl FromStr for SymbolVisibility {
859    type Err = ();
860
861    fn from_str(s: &str) -> Result<SymbolVisibility, ()> {
862        match s {
863            "hidden" => Ok(SymbolVisibility::Hidden),
864            "protected" => Ok(SymbolVisibility::Protected),
865            "interposable" => Ok(SymbolVisibility::Interposable),
866            _ => Err(()),
867        }
868    }
869}
870
871impl ToJson for SymbolVisibility {
872    fn to_json(&self) -> Json {
873        match *self {
874            SymbolVisibility::Hidden => "hidden".to_json(),
875            SymbolVisibility::Protected => "protected".to_json(),
876            SymbolVisibility::Interposable => "interposable".to_json(),
877        }
878    }
879}
880
881impl FromStr for RelroLevel {
882    type Err = ();
883
884    fn from_str(s: &str) -> Result<RelroLevel, ()> {
885        match s {
886            "full" => Ok(RelroLevel::Full),
887            "partial" => Ok(RelroLevel::Partial),
888            "off" => Ok(RelroLevel::Off),
889            "none" => Ok(RelroLevel::None),
890            _ => Err(()),
891        }
892    }
893}
894
895impl ToJson for RelroLevel {
896    fn to_json(&self) -> Json {
897        match *self {
898            RelroLevel::Full => "full".to_json(),
899            RelroLevel::Partial => "partial".to_json(),
900            RelroLevel::Off => "off".to_json(),
901            RelroLevel::None => "None".to_json(),
902        }
903    }
904}
905
906#[derive(Clone, Debug, PartialEq, Hash)]
907pub enum SmallDataThresholdSupport {
908    None,
909    DefaultForArch,
910    LlvmModuleFlag(StaticCow<str>),
911    LlvmArg(StaticCow<str>),
912}
913
914impl FromStr for SmallDataThresholdSupport {
915    type Err = ();
916
917    fn from_str(s: &str) -> Result<Self, Self::Err> {
918        if s == "none" {
919            Ok(Self::None)
920        } else if s == "default-for-arch" {
921            Ok(Self::DefaultForArch)
922        } else if let Some(flag) = s.strip_prefix("llvm-module-flag=") {
923            Ok(Self::LlvmModuleFlag(flag.to_string().into()))
924        } else if let Some(arg) = s.strip_prefix("llvm-arg=") {
925            Ok(Self::LlvmArg(arg.to_string().into()))
926        } else {
927            Err(())
928        }
929    }
930}
931
932impl ToJson for SmallDataThresholdSupport {
933    fn to_json(&self) -> Value {
934        match self {
935            Self::None => "none".to_json(),
936            Self::DefaultForArch => "default-for-arch".to_json(),
937            Self::LlvmModuleFlag(flag) => format!("llvm-module-flag={flag}").to_json(),
938            Self::LlvmArg(arg) => format!("llvm-arg={arg}").to_json(),
939        }
940    }
941}
942
943#[derive(Clone, Copy, Debug, PartialEq, Hash)]
944pub enum MergeFunctions {
945    Disabled,
946    Trampolines,
947    Aliases,
948}
949
950impl MergeFunctions {
951    pub fn desc(&self) -> &str {
952        match *self {
953            MergeFunctions::Disabled => "disabled",
954            MergeFunctions::Trampolines => "trampolines",
955            MergeFunctions::Aliases => "aliases",
956        }
957    }
958}
959
960impl FromStr for MergeFunctions {
961    type Err = ();
962
963    fn from_str(s: &str) -> Result<MergeFunctions, ()> {
964        match s {
965            "disabled" => Ok(MergeFunctions::Disabled),
966            "trampolines" => Ok(MergeFunctions::Trampolines),
967            "aliases" => Ok(MergeFunctions::Aliases),
968            _ => Err(()),
969        }
970    }
971}
972
973impl ToJson for MergeFunctions {
974    fn to_json(&self) -> Json {
975        match *self {
976            MergeFunctions::Disabled => "disabled".to_json(),
977            MergeFunctions::Trampolines => "trampolines".to_json(),
978            MergeFunctions::Aliases => "aliases".to_json(),
979        }
980    }
981}
982
983#[derive(Clone, Copy, PartialEq, Hash, Debug)]
984pub enum RelocModel {
985    Static,
986    Pic,
987    Pie,
988    DynamicNoPic,
989    Ropi,
990    Rwpi,
991    RopiRwpi,
992}
993
994impl RelocModel {
995    pub fn desc(&self) -> &str {
996        match *self {
997            RelocModel::Static => "static",
998            RelocModel::Pic => "pic",
999            RelocModel::Pie => "pie",
1000            RelocModel::DynamicNoPic => "dynamic-no-pic",
1001            RelocModel::Ropi => "ropi",
1002            RelocModel::Rwpi => "rwpi",
1003            RelocModel::RopiRwpi => "ropi-rwpi",
1004        }
1005    }
1006    pub const fn desc_symbol(&self) -> Symbol {
1007        match *self {
1008            RelocModel::Static => kw::Static,
1009            RelocModel::Pic => sym::pic,
1010            RelocModel::Pie => sym::pie,
1011            RelocModel::DynamicNoPic => sym::dynamic_no_pic,
1012            RelocModel::Ropi => sym::ropi,
1013            RelocModel::Rwpi => sym::rwpi,
1014            RelocModel::RopiRwpi => sym::ropi_rwpi,
1015        }
1016    }
1017
1018    pub const fn all() -> [Symbol; 7] {
1019        [
1020            RelocModel::Static.desc_symbol(),
1021            RelocModel::Pic.desc_symbol(),
1022            RelocModel::Pie.desc_symbol(),
1023            RelocModel::DynamicNoPic.desc_symbol(),
1024            RelocModel::Ropi.desc_symbol(),
1025            RelocModel::Rwpi.desc_symbol(),
1026            RelocModel::RopiRwpi.desc_symbol(),
1027        ]
1028    }
1029}
1030
1031impl FromStr for RelocModel {
1032    type Err = ();
1033
1034    fn from_str(s: &str) -> Result<RelocModel, ()> {
1035        Ok(match s {
1036            "static" => RelocModel::Static,
1037            "pic" => RelocModel::Pic,
1038            "pie" => RelocModel::Pie,
1039            "dynamic-no-pic" => RelocModel::DynamicNoPic,
1040            "ropi" => RelocModel::Ropi,
1041            "rwpi" => RelocModel::Rwpi,
1042            "ropi-rwpi" => RelocModel::RopiRwpi,
1043            _ => return Err(()),
1044        })
1045    }
1046}
1047
1048impl ToJson for RelocModel {
1049    fn to_json(&self) -> Json {
1050        self.desc().to_json()
1051    }
1052}
1053
1054#[derive(Clone, Copy, PartialEq, Hash, Debug)]
1055pub enum CodeModel {
1056    Tiny,
1057    Small,
1058    Kernel,
1059    Medium,
1060    Large,
1061}
1062
1063impl FromStr for CodeModel {
1064    type Err = ();
1065
1066    fn from_str(s: &str) -> Result<CodeModel, ()> {
1067        Ok(match s {
1068            "tiny" => CodeModel::Tiny,
1069            "small" => CodeModel::Small,
1070            "kernel" => CodeModel::Kernel,
1071            "medium" => CodeModel::Medium,
1072            "large" => CodeModel::Large,
1073            _ => return Err(()),
1074        })
1075    }
1076}
1077
1078impl ToJson for CodeModel {
1079    fn to_json(&self) -> Json {
1080        match *self {
1081            CodeModel::Tiny => "tiny",
1082            CodeModel::Small => "small",
1083            CodeModel::Kernel => "kernel",
1084            CodeModel::Medium => "medium",
1085            CodeModel::Large => "large",
1086        }
1087        .to_json()
1088    }
1089}
1090
1091/// The float ABI setting to be configured in the LLVM target machine.
1092#[derive(Clone, Copy, PartialEq, Hash, Debug)]
1093pub enum FloatAbi {
1094    Soft,
1095    Hard,
1096}
1097
1098impl FromStr for FloatAbi {
1099    type Err = ();
1100
1101    fn from_str(s: &str) -> Result<FloatAbi, ()> {
1102        Ok(match s {
1103            "soft" => FloatAbi::Soft,
1104            "hard" => FloatAbi::Hard,
1105            _ => return Err(()),
1106        })
1107    }
1108}
1109
1110impl ToJson for FloatAbi {
1111    fn to_json(&self) -> Json {
1112        match *self {
1113            FloatAbi::Soft => "soft",
1114            FloatAbi::Hard => "hard",
1115        }
1116        .to_json()
1117    }
1118}
1119
1120/// The Rustc-specific variant of the ABI used for this target.
1121#[derive(Clone, Copy, PartialEq, Hash, Debug)]
1122pub enum RustcAbi {
1123    /// On x86-32 only: make use of SSE and SSE2 for ABI purposes.
1124    X86Sse2,
1125    /// On x86-32/64 only: do not use any FPU or SIMD registers for the ABI.
1126    X86Softfloat,
1127}
1128
1129impl FromStr for RustcAbi {
1130    type Err = ();
1131
1132    fn from_str(s: &str) -> Result<RustcAbi, ()> {
1133        Ok(match s {
1134            "x86-sse2" => RustcAbi::X86Sse2,
1135            "x86-softfloat" => RustcAbi::X86Softfloat,
1136            _ => return Err(()),
1137        })
1138    }
1139}
1140
1141impl ToJson for RustcAbi {
1142    fn to_json(&self) -> Json {
1143        match *self {
1144            RustcAbi::X86Sse2 => "x86-sse2",
1145            RustcAbi::X86Softfloat => "x86-softfloat",
1146        }
1147        .to_json()
1148    }
1149}
1150
1151#[derive(Clone, Copy, PartialEq, Hash, Debug)]
1152pub enum TlsModel {
1153    GeneralDynamic,
1154    LocalDynamic,
1155    InitialExec,
1156    LocalExec,
1157    Emulated,
1158}
1159
1160impl FromStr for TlsModel {
1161    type Err = ();
1162
1163    fn from_str(s: &str) -> Result<TlsModel, ()> {
1164        Ok(match s {
1165            // Note the difference "general" vs "global" difference. The model name is "general",
1166            // but the user-facing option name is "global" for consistency with other compilers.
1167            "global-dynamic" => TlsModel::GeneralDynamic,
1168            "local-dynamic" => TlsModel::LocalDynamic,
1169            "initial-exec" => TlsModel::InitialExec,
1170            "local-exec" => TlsModel::LocalExec,
1171            "emulated" => TlsModel::Emulated,
1172            _ => return Err(()),
1173        })
1174    }
1175}
1176
1177impl ToJson for TlsModel {
1178    fn to_json(&self) -> Json {
1179        match *self {
1180            TlsModel::GeneralDynamic => "global-dynamic",
1181            TlsModel::LocalDynamic => "local-dynamic",
1182            TlsModel::InitialExec => "initial-exec",
1183            TlsModel::LocalExec => "local-exec",
1184            TlsModel::Emulated => "emulated",
1185        }
1186        .to_json()
1187    }
1188}
1189
1190/// Everything is flattened to a single enum to make the json encoding/decoding less annoying.
1191#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
1192pub enum LinkOutputKind {
1193    /// Dynamically linked non position-independent executable.
1194    DynamicNoPicExe,
1195    /// Dynamically linked position-independent executable.
1196    DynamicPicExe,
1197    /// Statically linked non position-independent executable.
1198    StaticNoPicExe,
1199    /// Statically linked position-independent executable.
1200    StaticPicExe,
1201    /// Regular dynamic library ("dynamically linked").
1202    DynamicDylib,
1203    /// Dynamic library with bundled libc ("statically linked").
1204    StaticDylib,
1205    /// WASI module with a lifetime past the _initialize entry point
1206    WasiReactorExe,
1207}
1208
1209impl LinkOutputKind {
1210    fn as_str(&self) -> &'static str {
1211        match self {
1212            LinkOutputKind::DynamicNoPicExe => "dynamic-nopic-exe",
1213            LinkOutputKind::DynamicPicExe => "dynamic-pic-exe",
1214            LinkOutputKind::StaticNoPicExe => "static-nopic-exe",
1215            LinkOutputKind::StaticPicExe => "static-pic-exe",
1216            LinkOutputKind::DynamicDylib => "dynamic-dylib",
1217            LinkOutputKind::StaticDylib => "static-dylib",
1218            LinkOutputKind::WasiReactorExe => "wasi-reactor-exe",
1219        }
1220    }
1221
1222    pub(super) fn from_str(s: &str) -> Option<LinkOutputKind> {
1223        Some(match s {
1224            "dynamic-nopic-exe" => LinkOutputKind::DynamicNoPicExe,
1225            "dynamic-pic-exe" => LinkOutputKind::DynamicPicExe,
1226            "static-nopic-exe" => LinkOutputKind::StaticNoPicExe,
1227            "static-pic-exe" => LinkOutputKind::StaticPicExe,
1228            "dynamic-dylib" => LinkOutputKind::DynamicDylib,
1229            "static-dylib" => LinkOutputKind::StaticDylib,
1230            "wasi-reactor-exe" => LinkOutputKind::WasiReactorExe,
1231            _ => return None,
1232        })
1233    }
1234
1235    pub fn can_link_dylib(self) -> bool {
1236        match self {
1237            LinkOutputKind::StaticNoPicExe | LinkOutputKind::StaticPicExe => false,
1238            LinkOutputKind::DynamicNoPicExe
1239            | LinkOutputKind::DynamicPicExe
1240            | LinkOutputKind::DynamicDylib
1241            | LinkOutputKind::StaticDylib
1242            | LinkOutputKind::WasiReactorExe => true,
1243        }
1244    }
1245}
1246
1247impl fmt::Display for LinkOutputKind {
1248    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1249        f.write_str(self.as_str())
1250    }
1251}
1252
1253pub type LinkArgs = BTreeMap<LinkerFlavor, Vec<StaticCow<str>>>;
1254pub type LinkArgsCli = BTreeMap<LinkerFlavorCli, Vec<StaticCow<str>>>;
1255
1256/// Which kind of debuginfo does the target use?
1257///
1258/// Useful in determining whether a target supports Split DWARF (a target with
1259/// `DebuginfoKind::Dwarf` and supporting `SplitDebuginfo::Unpacked` for example).
1260#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
1261pub enum DebuginfoKind {
1262    /// DWARF debuginfo (such as that used on `x86_64_unknown_linux_gnu`).
1263    #[default]
1264    Dwarf,
1265    /// DWARF debuginfo in dSYM files (such as on Apple platforms).
1266    DwarfDsym,
1267    /// Program database files (such as on Windows).
1268    Pdb,
1269}
1270
1271impl DebuginfoKind {
1272    fn as_str(&self) -> &'static str {
1273        match self {
1274            DebuginfoKind::Dwarf => "dwarf",
1275            DebuginfoKind::DwarfDsym => "dwarf-dsym",
1276            DebuginfoKind::Pdb => "pdb",
1277        }
1278    }
1279}
1280
1281impl FromStr for DebuginfoKind {
1282    type Err = ();
1283
1284    fn from_str(s: &str) -> Result<Self, ()> {
1285        Ok(match s {
1286            "dwarf" => DebuginfoKind::Dwarf,
1287            "dwarf-dsym" => DebuginfoKind::DwarfDsym,
1288            "pdb" => DebuginfoKind::Pdb,
1289            _ => return Err(()),
1290        })
1291    }
1292}
1293
1294impl ToJson for DebuginfoKind {
1295    fn to_json(&self) -> Json {
1296        self.as_str().to_json()
1297    }
1298}
1299
1300impl fmt::Display for DebuginfoKind {
1301    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1302        f.write_str(self.as_str())
1303    }
1304}
1305
1306#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
1307pub enum SplitDebuginfo {
1308    /// Split debug-information is disabled, meaning that on supported platforms
1309    /// you can find all debug information in the executable itself. This is
1310    /// only supported for ELF effectively.
1311    ///
1312    /// * Windows - not supported
1313    /// * macOS - don't run `dsymutil`
1314    /// * ELF - `.debug_*` sections
1315    #[default]
1316    Off,
1317
1318    /// Split debug-information can be found in a "packed" location separate
1319    /// from the final artifact. This is supported on all platforms.
1320    ///
1321    /// * Windows - `*.pdb`
1322    /// * macOS - `*.dSYM` (run `dsymutil`)
1323    /// * ELF - `*.dwp` (run `thorin`)
1324    Packed,
1325
1326    /// Split debug-information can be found in individual object files on the
1327    /// filesystem. The main executable may point to the object files.
1328    ///
1329    /// * Windows - not supported
1330    /// * macOS - supported, scattered object files
1331    /// * ELF - supported, scattered `*.dwo` or `*.o` files (see `SplitDwarfKind`)
1332    Unpacked,
1333}
1334
1335impl SplitDebuginfo {
1336    fn as_str(&self) -> &'static str {
1337        match self {
1338            SplitDebuginfo::Off => "off",
1339            SplitDebuginfo::Packed => "packed",
1340            SplitDebuginfo::Unpacked => "unpacked",
1341        }
1342    }
1343}
1344
1345impl FromStr for SplitDebuginfo {
1346    type Err = ();
1347
1348    fn from_str(s: &str) -> Result<Self, ()> {
1349        Ok(match s {
1350            "off" => SplitDebuginfo::Off,
1351            "unpacked" => SplitDebuginfo::Unpacked,
1352            "packed" => SplitDebuginfo::Packed,
1353            _ => return Err(()),
1354        })
1355    }
1356}
1357
1358impl ToJson for SplitDebuginfo {
1359    fn to_json(&self) -> Json {
1360        self.as_str().to_json()
1361    }
1362}
1363
1364impl fmt::Display for SplitDebuginfo {
1365    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1366        f.write_str(self.as_str())
1367    }
1368}
1369
1370#[derive(Clone, Debug, PartialEq, Eq)]
1371pub enum StackProbeType {
1372    /// Don't emit any stack probes.
1373    None,
1374    /// It is harmless to use this option even on targets that do not have backend support for
1375    /// stack probes as the failure mode is the same as if no stack-probe option was specified in
1376    /// the first place.
1377    Inline,
1378    /// Call `__rust_probestack` whenever stack needs to be probed.
1379    Call,
1380    /// Use inline option for LLVM versions later than specified in `min_llvm_version_for_inline`
1381    /// and call `__rust_probestack` otherwise.
1382    InlineOrCall { min_llvm_version_for_inline: (u32, u32, u32) },
1383}
1384
1385impl StackProbeType {
1386    fn from_json(json: &Json) -> Result<Self, String> {
1387        let object = json.as_object().ok_or_else(|| "expected a JSON object")?;
1388        let kind = object
1389            .get("kind")
1390            .and_then(|o| o.as_str())
1391            .ok_or_else(|| "expected `kind` to be a string")?;
1392        match kind {
1393            "none" => Ok(StackProbeType::None),
1394            "inline" => Ok(StackProbeType::Inline),
1395            "call" => Ok(StackProbeType::Call),
1396            "inline-or-call" => {
1397                let min_version = object
1398                    .get("min-llvm-version-for-inline")
1399                    .and_then(|o| o.as_array())
1400                    .ok_or_else(|| "expected `min-llvm-version-for-inline` to be an array")?;
1401                let mut iter = min_version.into_iter().map(|v| {
1402                    let int = v.as_u64().ok_or_else(
1403                        || "expected `min-llvm-version-for-inline` values to be integers",
1404                    )?;
1405                    u32::try_from(int)
1406                        .map_err(|_| "`min-llvm-version-for-inline` values don't convert to u32")
1407                });
1408                let min_llvm_version_for_inline = (
1409                    iter.next().unwrap_or(Ok(11))?,
1410                    iter.next().unwrap_or(Ok(0))?,
1411                    iter.next().unwrap_or(Ok(0))?,
1412                );
1413                Ok(StackProbeType::InlineOrCall { min_llvm_version_for_inline })
1414            }
1415            _ => Err(String::from(
1416                "`kind` expected to be one of `none`, `inline`, `call` or `inline-or-call`",
1417            )),
1418        }
1419    }
1420}
1421
1422impl ToJson for StackProbeType {
1423    fn to_json(&self) -> Json {
1424        Json::Object(match self {
1425            StackProbeType::None => {
1426                [(String::from("kind"), "none".to_json())].into_iter().collect()
1427            }
1428            StackProbeType::Inline => {
1429                [(String::from("kind"), "inline".to_json())].into_iter().collect()
1430            }
1431            StackProbeType::Call => {
1432                [(String::from("kind"), "call".to_json())].into_iter().collect()
1433            }
1434            StackProbeType::InlineOrCall { min_llvm_version_for_inline: (maj, min, patch) } => [
1435                (String::from("kind"), "inline-or-call".to_json()),
1436                (
1437                    String::from("min-llvm-version-for-inline"),
1438                    Json::Array(vec![maj.to_json(), min.to_json(), patch.to_json()]),
1439                ),
1440            ]
1441            .into_iter()
1442            .collect(),
1443        })
1444    }
1445}
1446
1447#[derive(Default, Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, HashStable_Generic)]
1448pub struct SanitizerSet(u16);
1449bitflags::bitflags! {
1450    impl SanitizerSet: u16 {
1451        const ADDRESS = 1 << 0;
1452        const LEAK    = 1 << 1;
1453        const MEMORY  = 1 << 2;
1454        const THREAD  = 1 << 3;
1455        const HWADDRESS = 1 << 4;
1456        const CFI     = 1 << 5;
1457        const MEMTAG  = 1 << 6;
1458        const SHADOWCALLSTACK = 1 << 7;
1459        const KCFI    = 1 << 8;
1460        const KERNELADDRESS = 1 << 9;
1461        const SAFESTACK = 1 << 10;
1462        const DATAFLOW = 1 << 11;
1463    }
1464}
1465rustc_data_structures::external_bitflags_debug! { SanitizerSet }
1466
1467impl SanitizerSet {
1468    // Taken from LLVM's sanitizer compatibility logic:
1469    // https://github.com/llvm/llvm-project/blob/release/18.x/clang/lib/Driver/SanitizerArgs.cpp#L512
1470    const MUTUALLY_EXCLUSIVE: &'static [(SanitizerSet, SanitizerSet)] = &[
1471        (SanitizerSet::ADDRESS, SanitizerSet::MEMORY),
1472        (SanitizerSet::ADDRESS, SanitizerSet::THREAD),
1473        (SanitizerSet::ADDRESS, SanitizerSet::HWADDRESS),
1474        (SanitizerSet::ADDRESS, SanitizerSet::MEMTAG),
1475        (SanitizerSet::ADDRESS, SanitizerSet::KERNELADDRESS),
1476        (SanitizerSet::ADDRESS, SanitizerSet::SAFESTACK),
1477        (SanitizerSet::LEAK, SanitizerSet::MEMORY),
1478        (SanitizerSet::LEAK, SanitizerSet::THREAD),
1479        (SanitizerSet::LEAK, SanitizerSet::KERNELADDRESS),
1480        (SanitizerSet::LEAK, SanitizerSet::SAFESTACK),
1481        (SanitizerSet::MEMORY, SanitizerSet::THREAD),
1482        (SanitizerSet::MEMORY, SanitizerSet::HWADDRESS),
1483        (SanitizerSet::MEMORY, SanitizerSet::KERNELADDRESS),
1484        (SanitizerSet::MEMORY, SanitizerSet::SAFESTACK),
1485        (SanitizerSet::THREAD, SanitizerSet::HWADDRESS),
1486        (SanitizerSet::THREAD, SanitizerSet::KERNELADDRESS),
1487        (SanitizerSet::THREAD, SanitizerSet::SAFESTACK),
1488        (SanitizerSet::HWADDRESS, SanitizerSet::MEMTAG),
1489        (SanitizerSet::HWADDRESS, SanitizerSet::KERNELADDRESS),
1490        (SanitizerSet::HWADDRESS, SanitizerSet::SAFESTACK),
1491        (SanitizerSet::CFI, SanitizerSet::KCFI),
1492        (SanitizerSet::MEMTAG, SanitizerSet::KERNELADDRESS),
1493        (SanitizerSet::KERNELADDRESS, SanitizerSet::SAFESTACK),
1494    ];
1495
1496    /// Return sanitizer's name
1497    ///
1498    /// Returns none if the flags is a set of sanitizers numbering not exactly one.
1499    pub fn as_str(self) -> Option<&'static str> {
1500        Some(match self {
1501            SanitizerSet::ADDRESS => "address",
1502            SanitizerSet::CFI => "cfi",
1503            SanitizerSet::DATAFLOW => "dataflow",
1504            SanitizerSet::KCFI => "kcfi",
1505            SanitizerSet::KERNELADDRESS => "kernel-address",
1506            SanitizerSet::LEAK => "leak",
1507            SanitizerSet::MEMORY => "memory",
1508            SanitizerSet::MEMTAG => "memtag",
1509            SanitizerSet::SAFESTACK => "safestack",
1510            SanitizerSet::SHADOWCALLSTACK => "shadow-call-stack",
1511            SanitizerSet::THREAD => "thread",
1512            SanitizerSet::HWADDRESS => "hwaddress",
1513            _ => return None,
1514        })
1515    }
1516
1517    pub fn mutually_exclusive(self) -> Option<(SanitizerSet, SanitizerSet)> {
1518        Self::MUTUALLY_EXCLUSIVE
1519            .into_iter()
1520            .find(|&(a, b)| self.contains(*a) && self.contains(*b))
1521            .copied()
1522    }
1523}
1524
1525/// Formats a sanitizer set as a comma separated list of sanitizers' names.
1526impl fmt::Display for SanitizerSet {
1527    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1528        let mut first = true;
1529        for s in *self {
1530            let name = s.as_str().unwrap_or_else(|| panic!("unrecognized sanitizer {s:?}"));
1531            if !first {
1532                f.write_str(", ")?;
1533            }
1534            f.write_str(name)?;
1535            first = false;
1536        }
1537        Ok(())
1538    }
1539}
1540
1541impl ToJson for SanitizerSet {
1542    fn to_json(&self) -> Json {
1543        self.into_iter()
1544            .map(|v| Some(v.as_str()?.to_json()))
1545            .collect::<Option<Vec<_>>>()
1546            .unwrap_or_default()
1547            .to_json()
1548    }
1549}
1550
1551#[derive(Clone, Copy, PartialEq, Hash, Debug)]
1552pub enum FramePointer {
1553    /// Forces the machine code generator to always preserve the frame pointers.
1554    Always,
1555    /// Forces the machine code generator to preserve the frame pointers except for the leaf
1556    /// functions (i.e. those that don't call other functions).
1557    NonLeaf,
1558    /// Allows the machine code generator to omit the frame pointers.
1559    ///
1560    /// This option does not guarantee that the frame pointers will be omitted.
1561    MayOmit,
1562}
1563
1564impl FramePointer {
1565    /// It is intended that the "force frame pointer" transition is "one way"
1566    /// so this convenience assures such if used
1567    #[inline]
1568    pub fn ratchet(&mut self, rhs: FramePointer) -> FramePointer {
1569        *self = match (*self, rhs) {
1570            (FramePointer::Always, _) | (_, FramePointer::Always) => FramePointer::Always,
1571            (FramePointer::NonLeaf, _) | (_, FramePointer::NonLeaf) => FramePointer::NonLeaf,
1572            _ => FramePointer::MayOmit,
1573        };
1574        *self
1575    }
1576}
1577
1578impl FromStr for FramePointer {
1579    type Err = ();
1580    fn from_str(s: &str) -> Result<Self, ()> {
1581        Ok(match s {
1582            "always" => Self::Always,
1583            "non-leaf" => Self::NonLeaf,
1584            "may-omit" => Self::MayOmit,
1585            _ => return Err(()),
1586        })
1587    }
1588}
1589
1590impl ToJson for FramePointer {
1591    fn to_json(&self) -> Json {
1592        match *self {
1593            Self::Always => "always",
1594            Self::NonLeaf => "non-leaf",
1595            Self::MayOmit => "may-omit",
1596        }
1597        .to_json()
1598    }
1599}
1600
1601/// Controls use of stack canaries.
1602#[derive(Clone, Copy, Debug, PartialEq, Hash, Eq)]
1603pub enum StackProtector {
1604    /// Disable stack canary generation.
1605    None,
1606
1607    /// On LLVM, mark all generated LLVM functions with the `ssp` attribute (see
1608    /// llvm/docs/LangRef.rst). This triggers stack canary generation in
1609    /// functions which contain an array of a byte-sized type with more than
1610    /// eight elements.
1611    Basic,
1612
1613    /// On LLVM, mark all generated LLVM functions with the `sspstrong`
1614    /// attribute (see llvm/docs/LangRef.rst). This triggers stack canary
1615    /// generation in functions which either contain an array, or which take
1616    /// the address of a local variable.
1617    Strong,
1618
1619    /// Generate stack canaries in all functions.
1620    All,
1621}
1622
1623impl StackProtector {
1624    fn as_str(&self) -> &'static str {
1625        match self {
1626            StackProtector::None => "none",
1627            StackProtector::Basic => "basic",
1628            StackProtector::Strong => "strong",
1629            StackProtector::All => "all",
1630        }
1631    }
1632}
1633
1634impl FromStr for StackProtector {
1635    type Err = ();
1636
1637    fn from_str(s: &str) -> Result<StackProtector, ()> {
1638        Ok(match s {
1639            "none" => StackProtector::None,
1640            "basic" => StackProtector::Basic,
1641            "strong" => StackProtector::Strong,
1642            "all" => StackProtector::All,
1643            _ => return Err(()),
1644        })
1645    }
1646}
1647
1648impl fmt::Display for StackProtector {
1649    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1650        f.write_str(self.as_str())
1651    }
1652}
1653
1654#[derive(PartialEq, Clone, Debug)]
1655pub enum BinaryFormat {
1656    Coff,
1657    Elf,
1658    MachO,
1659    Wasm,
1660    Xcoff,
1661}
1662
1663impl BinaryFormat {
1664    /// Returns [`object::BinaryFormat`] for given `BinaryFormat`
1665    pub fn to_object(&self) -> object::BinaryFormat {
1666        match self {
1667            Self::Coff => object::BinaryFormat::Coff,
1668            Self::Elf => object::BinaryFormat::Elf,
1669            Self::MachO => object::BinaryFormat::MachO,
1670            Self::Wasm => object::BinaryFormat::Wasm,
1671            Self::Xcoff => object::BinaryFormat::Xcoff,
1672        }
1673    }
1674}
1675
1676impl FromStr for BinaryFormat {
1677    type Err = ();
1678    fn from_str(s: &str) -> Result<Self, Self::Err> {
1679        match s {
1680            "coff" => Ok(Self::Coff),
1681            "elf" => Ok(Self::Elf),
1682            "mach-o" => Ok(Self::MachO),
1683            "wasm" => Ok(Self::Wasm),
1684            "xcoff" => Ok(Self::Xcoff),
1685            _ => Err(()),
1686        }
1687    }
1688}
1689
1690impl ToJson for BinaryFormat {
1691    fn to_json(&self) -> Json {
1692        match self {
1693            Self::Coff => "coff",
1694            Self::Elf => "elf",
1695            Self::MachO => "mach-o",
1696            Self::Wasm => "wasm",
1697            Self::Xcoff => "xcoff",
1698        }
1699        .to_json()
1700    }
1701}
1702
1703impl ToJson for Align {
1704    fn to_json(&self) -> Json {
1705        self.bits().to_json()
1706    }
1707}
1708
1709macro_rules! supported_targets {
1710    ( $(($tuple:literal, $module:ident),)+ ) => {
1711        mod targets {
1712            $(pub(crate) mod $module;)+
1713        }
1714
1715        /// List of supported targets
1716        pub static TARGETS: &[&str] = &[$($tuple),+];
1717
1718        fn load_builtin(target: &str) -> Option<Target> {
1719            let t = match target {
1720                $( $tuple => targets::$module::target(), )+
1721                _ => return None,
1722            };
1723            debug!("got builtin target: {:?}", t);
1724            Some(t)
1725        }
1726
1727        fn load_all_builtins() -> impl Iterator<Item = Target> {
1728            [
1729                $( targets::$module::target, )+
1730            ]
1731            .into_iter()
1732            .map(|f| f())
1733        }
1734
1735        #[cfg(test)]
1736        mod tests {
1737            // Cannot put this into a separate file without duplication, make an exception.
1738            $(
1739                #[test] // `#[test]`
1740                fn $module() {
1741                    crate::spec::targets::$module::target().test_target()
1742                }
1743            )+
1744        }
1745    };
1746}
1747
1748supported_targets! {
1749    ("x86_64-unknown-linux-gnu", x86_64_unknown_linux_gnu),
1750    ("x86_64-unknown-linux-gnux32", x86_64_unknown_linux_gnux32),
1751    ("i686-unknown-linux-gnu", i686_unknown_linux_gnu),
1752    ("i586-unknown-linux-gnu", i586_unknown_linux_gnu),
1753    ("loongarch64-unknown-linux-gnu", loongarch64_unknown_linux_gnu),
1754    ("loongarch64-unknown-linux-musl", loongarch64_unknown_linux_musl),
1755    ("m68k-unknown-linux-gnu", m68k_unknown_linux_gnu),
1756    ("m68k-unknown-none-elf", m68k_unknown_none_elf),
1757    ("csky-unknown-linux-gnuabiv2", csky_unknown_linux_gnuabiv2),
1758    ("csky-unknown-linux-gnuabiv2hf", csky_unknown_linux_gnuabiv2hf),
1759    ("mips-unknown-linux-gnu", mips_unknown_linux_gnu),
1760    ("mips64-unknown-linux-gnuabi64", mips64_unknown_linux_gnuabi64),
1761    ("mips64el-unknown-linux-gnuabi64", mips64el_unknown_linux_gnuabi64),
1762    ("mipsisa32r6-unknown-linux-gnu", mipsisa32r6_unknown_linux_gnu),
1763    ("mipsisa32r6el-unknown-linux-gnu", mipsisa32r6el_unknown_linux_gnu),
1764    ("mipsisa64r6-unknown-linux-gnuabi64", mipsisa64r6_unknown_linux_gnuabi64),
1765    ("mipsisa64r6el-unknown-linux-gnuabi64", mipsisa64r6el_unknown_linux_gnuabi64),
1766    ("mipsel-unknown-linux-gnu", mipsel_unknown_linux_gnu),
1767    ("powerpc-unknown-linux-gnu", powerpc_unknown_linux_gnu),
1768    ("powerpc-unknown-linux-gnuspe", powerpc_unknown_linux_gnuspe),
1769    ("powerpc-unknown-linux-musl", powerpc_unknown_linux_musl),
1770    ("powerpc-unknown-linux-muslspe", powerpc_unknown_linux_muslspe),
1771    ("powerpc64-ibm-aix", powerpc64_ibm_aix),
1772    ("powerpc64-unknown-linux-gnu", powerpc64_unknown_linux_gnu),
1773    ("powerpc64-unknown-linux-musl", powerpc64_unknown_linux_musl),
1774    ("powerpc64le-unknown-linux-gnu", powerpc64le_unknown_linux_gnu),
1775    ("powerpc64le-unknown-linux-musl", powerpc64le_unknown_linux_musl),
1776    ("s390x-unknown-linux-gnu", s390x_unknown_linux_gnu),
1777    ("s390x-unknown-linux-musl", s390x_unknown_linux_musl),
1778    ("sparc-unknown-linux-gnu", sparc_unknown_linux_gnu),
1779    ("sparc64-unknown-linux-gnu", sparc64_unknown_linux_gnu),
1780    ("arm-unknown-linux-gnueabi", arm_unknown_linux_gnueabi),
1781    ("arm-unknown-linux-gnueabihf", arm_unknown_linux_gnueabihf),
1782    ("armeb-unknown-linux-gnueabi", armeb_unknown_linux_gnueabi),
1783    ("arm-unknown-linux-musleabi", arm_unknown_linux_musleabi),
1784    ("arm-unknown-linux-musleabihf", arm_unknown_linux_musleabihf),
1785    ("armv4t-unknown-linux-gnueabi", armv4t_unknown_linux_gnueabi),
1786    ("armv5te-unknown-linux-gnueabi", armv5te_unknown_linux_gnueabi),
1787    ("armv5te-unknown-linux-musleabi", armv5te_unknown_linux_musleabi),
1788    ("armv5te-unknown-linux-uclibceabi", armv5te_unknown_linux_uclibceabi),
1789    ("armv7-unknown-linux-gnueabi", armv7_unknown_linux_gnueabi),
1790    ("armv7-unknown-linux-gnueabihf", armv7_unknown_linux_gnueabihf),
1791    ("thumbv7neon-unknown-linux-gnueabihf", thumbv7neon_unknown_linux_gnueabihf),
1792    ("thumbv7neon-unknown-linux-musleabihf", thumbv7neon_unknown_linux_musleabihf),
1793    ("armv7-unknown-linux-musleabi", armv7_unknown_linux_musleabi),
1794    ("armv7-unknown-linux-musleabihf", armv7_unknown_linux_musleabihf),
1795    ("aarch64-unknown-linux-gnu", aarch64_unknown_linux_gnu),
1796    ("aarch64-unknown-linux-musl", aarch64_unknown_linux_musl),
1797    ("x86_64-unknown-linux-musl", x86_64_unknown_linux_musl),
1798    ("i686-unknown-linux-musl", i686_unknown_linux_musl),
1799    ("i586-unknown-linux-musl", i586_unknown_linux_musl),
1800    ("mips-unknown-linux-musl", mips_unknown_linux_musl),
1801    ("mipsel-unknown-linux-musl", mipsel_unknown_linux_musl),
1802    ("mips64-unknown-linux-muslabi64", mips64_unknown_linux_muslabi64),
1803    ("mips64el-unknown-linux-muslabi64", mips64el_unknown_linux_muslabi64),
1804    ("hexagon-unknown-linux-musl", hexagon_unknown_linux_musl),
1805    ("hexagon-unknown-none-elf", hexagon_unknown_none_elf),
1806
1807    ("mips-unknown-linux-uclibc", mips_unknown_linux_uclibc),
1808    ("mipsel-unknown-linux-uclibc", mipsel_unknown_linux_uclibc),
1809
1810    ("i686-linux-android", i686_linux_android),
1811    ("x86_64-linux-android", x86_64_linux_android),
1812    ("arm-linux-androideabi", arm_linux_androideabi),
1813    ("armv7-linux-androideabi", armv7_linux_androideabi),
1814    ("thumbv7neon-linux-androideabi", thumbv7neon_linux_androideabi),
1815    ("aarch64-linux-android", aarch64_linux_android),
1816    ("riscv64-linux-android", riscv64_linux_android),
1817
1818    ("aarch64-unknown-freebsd", aarch64_unknown_freebsd),
1819    ("armv6-unknown-freebsd", armv6_unknown_freebsd),
1820    ("armv7-unknown-freebsd", armv7_unknown_freebsd),
1821    ("i686-unknown-freebsd", i686_unknown_freebsd),
1822    ("powerpc-unknown-freebsd", powerpc_unknown_freebsd),
1823    ("powerpc64-unknown-freebsd", powerpc64_unknown_freebsd),
1824    ("powerpc64le-unknown-freebsd", powerpc64le_unknown_freebsd),
1825    ("riscv64gc-unknown-freebsd", riscv64gc_unknown_freebsd),
1826    ("x86_64-unknown-freebsd", x86_64_unknown_freebsd),
1827
1828    ("x86_64-unknown-dragonfly", x86_64_unknown_dragonfly),
1829
1830    ("aarch64-unknown-openbsd", aarch64_unknown_openbsd),
1831    ("i686-unknown-openbsd", i686_unknown_openbsd),
1832    ("powerpc-unknown-openbsd", powerpc_unknown_openbsd),
1833    ("powerpc64-unknown-openbsd", powerpc64_unknown_openbsd),
1834    ("riscv64gc-unknown-openbsd", riscv64gc_unknown_openbsd),
1835    ("sparc64-unknown-openbsd", sparc64_unknown_openbsd),
1836    ("x86_64-unknown-openbsd", x86_64_unknown_openbsd),
1837
1838    ("aarch64-unknown-netbsd", aarch64_unknown_netbsd),
1839    ("aarch64_be-unknown-netbsd", aarch64_be_unknown_netbsd),
1840    ("armv6-unknown-netbsd-eabihf", armv6_unknown_netbsd_eabihf),
1841    ("armv7-unknown-netbsd-eabihf", armv7_unknown_netbsd_eabihf),
1842    ("i586-unknown-netbsd", i586_unknown_netbsd),
1843    ("i686-unknown-netbsd", i686_unknown_netbsd),
1844    ("mipsel-unknown-netbsd", mipsel_unknown_netbsd),
1845    ("powerpc-unknown-netbsd", powerpc_unknown_netbsd),
1846    ("riscv64gc-unknown-netbsd", riscv64gc_unknown_netbsd),
1847    ("sparc64-unknown-netbsd", sparc64_unknown_netbsd),
1848    ("x86_64-unknown-netbsd", x86_64_unknown_netbsd),
1849
1850    ("i686-unknown-haiku", i686_unknown_haiku),
1851    ("x86_64-unknown-haiku", x86_64_unknown_haiku),
1852
1853    ("i686-unknown-hurd-gnu", i686_unknown_hurd_gnu),
1854    ("x86_64-unknown-hurd-gnu", x86_64_unknown_hurd_gnu),
1855
1856    ("aarch64-apple-darwin", aarch64_apple_darwin),
1857    ("arm64e-apple-darwin", arm64e_apple_darwin),
1858    ("x86_64-apple-darwin", x86_64_apple_darwin),
1859    ("x86_64h-apple-darwin", x86_64h_apple_darwin),
1860    ("i686-apple-darwin", i686_apple_darwin),
1861
1862    ("aarch64-unknown-fuchsia", aarch64_unknown_fuchsia),
1863    ("riscv64gc-unknown-fuchsia", riscv64gc_unknown_fuchsia),
1864    ("x86_64-unknown-fuchsia", x86_64_unknown_fuchsia),
1865
1866    ("avr-none", avr_none),
1867
1868    ("x86_64-unknown-l4re-uclibc", x86_64_unknown_l4re_uclibc),
1869
1870    ("aarch64-unknown-redox", aarch64_unknown_redox),
1871    ("i586-unknown-redox", i586_unknown_redox),
1872    ("x86_64-unknown-redox", x86_64_unknown_redox),
1873
1874    ("i386-apple-ios", i386_apple_ios),
1875    ("x86_64-apple-ios", x86_64_apple_ios),
1876    ("aarch64-apple-ios", aarch64_apple_ios),
1877    ("arm64e-apple-ios", arm64e_apple_ios),
1878    ("armv7s-apple-ios", armv7s_apple_ios),
1879    ("x86_64-apple-ios-macabi", x86_64_apple_ios_macabi),
1880    ("aarch64-apple-ios-macabi", aarch64_apple_ios_macabi),
1881    ("aarch64-apple-ios-sim", aarch64_apple_ios_sim),
1882
1883    ("aarch64-apple-tvos", aarch64_apple_tvos),
1884    ("aarch64-apple-tvos-sim", aarch64_apple_tvos_sim),
1885    ("arm64e-apple-tvos", arm64e_apple_tvos),
1886    ("x86_64-apple-tvos", x86_64_apple_tvos),
1887
1888    ("armv7k-apple-watchos", armv7k_apple_watchos),
1889    ("arm64_32-apple-watchos", arm64_32_apple_watchos),
1890    ("x86_64-apple-watchos-sim", x86_64_apple_watchos_sim),
1891    ("aarch64-apple-watchos", aarch64_apple_watchos),
1892    ("aarch64-apple-watchos-sim", aarch64_apple_watchos_sim),
1893
1894    ("aarch64-apple-visionos", aarch64_apple_visionos),
1895    ("aarch64-apple-visionos-sim", aarch64_apple_visionos_sim),
1896
1897    ("armebv7r-none-eabi", armebv7r_none_eabi),
1898    ("armebv7r-none-eabihf", armebv7r_none_eabihf),
1899    ("armv7r-none-eabi", armv7r_none_eabi),
1900    ("armv7r-none-eabihf", armv7r_none_eabihf),
1901    ("armv8r-none-eabihf", armv8r_none_eabihf),
1902
1903    ("armv7-rtems-eabihf", armv7_rtems_eabihf),
1904
1905    ("x86_64-pc-solaris", x86_64_pc_solaris),
1906    ("sparcv9-sun-solaris", sparcv9_sun_solaris),
1907
1908    ("x86_64-unknown-illumos", x86_64_unknown_illumos),
1909    ("aarch64-unknown-illumos", aarch64_unknown_illumos),
1910
1911    ("x86_64-pc-windows-gnu", x86_64_pc_windows_gnu),
1912    ("x86_64-uwp-windows-gnu", x86_64_uwp_windows_gnu),
1913    ("x86_64-win7-windows-gnu", x86_64_win7_windows_gnu),
1914    ("i686-pc-windows-gnu", i686_pc_windows_gnu),
1915    ("i686-uwp-windows-gnu", i686_uwp_windows_gnu),
1916    ("i686-win7-windows-gnu", i686_win7_windows_gnu),
1917
1918    ("aarch64-pc-windows-gnullvm", aarch64_pc_windows_gnullvm),
1919    ("i686-pc-windows-gnullvm", i686_pc_windows_gnullvm),
1920    ("x86_64-pc-windows-gnullvm", x86_64_pc_windows_gnullvm),
1921
1922    ("aarch64-pc-windows-msvc", aarch64_pc_windows_msvc),
1923    ("aarch64-uwp-windows-msvc", aarch64_uwp_windows_msvc),
1924    ("arm64ec-pc-windows-msvc", arm64ec_pc_windows_msvc),
1925    ("x86_64-pc-windows-msvc", x86_64_pc_windows_msvc),
1926    ("x86_64-uwp-windows-msvc", x86_64_uwp_windows_msvc),
1927    ("x86_64-win7-windows-msvc", x86_64_win7_windows_msvc),
1928    ("i686-pc-windows-msvc", i686_pc_windows_msvc),
1929    ("i686-uwp-windows-msvc", i686_uwp_windows_msvc),
1930    ("i686-win7-windows-msvc", i686_win7_windows_msvc),
1931    ("thumbv7a-pc-windows-msvc", thumbv7a_pc_windows_msvc),
1932    ("thumbv7a-uwp-windows-msvc", thumbv7a_uwp_windows_msvc),
1933
1934    ("wasm32-unknown-emscripten", wasm32_unknown_emscripten),
1935    ("wasm32-unknown-unknown", wasm32_unknown_unknown),
1936    ("wasm32v1-none", wasm32v1_none),
1937    ("wasm32-wasip1", wasm32_wasip1),
1938    ("wasm32-wasip2", wasm32_wasip2),
1939    ("wasm32-wasip1-threads", wasm32_wasip1_threads),
1940    ("wasm32-wali-linux-musl", wasm32_wali_linux_musl),
1941    ("wasm64-unknown-unknown", wasm64_unknown_unknown),
1942
1943    ("thumbv6m-none-eabi", thumbv6m_none_eabi),
1944    ("thumbv7m-none-eabi", thumbv7m_none_eabi),
1945    ("thumbv7em-none-eabi", thumbv7em_none_eabi),
1946    ("thumbv7em-none-eabihf", thumbv7em_none_eabihf),
1947    ("thumbv8m.base-none-eabi", thumbv8m_base_none_eabi),
1948    ("thumbv8m.main-none-eabi", thumbv8m_main_none_eabi),
1949    ("thumbv8m.main-none-eabihf", thumbv8m_main_none_eabihf),
1950
1951    ("armv7a-none-eabi", armv7a_none_eabi),
1952    ("armv7a-none-eabihf", armv7a_none_eabihf),
1953    ("armv7a-nuttx-eabi", armv7a_nuttx_eabi),
1954    ("armv7a-nuttx-eabihf", armv7a_nuttx_eabihf),
1955
1956    ("msp430-none-elf", msp430_none_elf),
1957
1958    ("aarch64-unknown-hermit", aarch64_unknown_hermit),
1959    ("riscv64gc-unknown-hermit", riscv64gc_unknown_hermit),
1960    ("x86_64-unknown-hermit", x86_64_unknown_hermit),
1961
1962    ("x86_64-unikraft-linux-musl", x86_64_unikraft_linux_musl),
1963
1964    ("armv7-unknown-trusty", armv7_unknown_trusty),
1965    ("aarch64-unknown-trusty", aarch64_unknown_trusty),
1966    ("x86_64-unknown-trusty", x86_64_unknown_trusty),
1967
1968    ("riscv32i-unknown-none-elf", riscv32i_unknown_none_elf),
1969    ("riscv32im-risc0-zkvm-elf", riscv32im_risc0_zkvm_elf),
1970    ("riscv32im-unknown-none-elf", riscv32im_unknown_none_elf),
1971    ("riscv32ima-unknown-none-elf", riscv32ima_unknown_none_elf),
1972    ("riscv32imc-unknown-none-elf", riscv32imc_unknown_none_elf),
1973    ("riscv32imc-esp-espidf", riscv32imc_esp_espidf),
1974    ("riscv32imac-esp-espidf", riscv32imac_esp_espidf),
1975    ("riscv32imafc-esp-espidf", riscv32imafc_esp_espidf),
1976
1977    ("riscv32e-unknown-none-elf", riscv32e_unknown_none_elf),
1978    ("riscv32em-unknown-none-elf", riscv32em_unknown_none_elf),
1979    ("riscv32emc-unknown-none-elf", riscv32emc_unknown_none_elf),
1980
1981    ("riscv32imac-unknown-none-elf", riscv32imac_unknown_none_elf),
1982    ("riscv32imafc-unknown-none-elf", riscv32imafc_unknown_none_elf),
1983    ("riscv32imac-unknown-xous-elf", riscv32imac_unknown_xous_elf),
1984    ("riscv32gc-unknown-linux-gnu", riscv32gc_unknown_linux_gnu),
1985    ("riscv32gc-unknown-linux-musl", riscv32gc_unknown_linux_musl),
1986    ("riscv64imac-unknown-none-elf", riscv64imac_unknown_none_elf),
1987    ("riscv64gc-unknown-none-elf", riscv64gc_unknown_none_elf),
1988    ("riscv64gc-unknown-linux-gnu", riscv64gc_unknown_linux_gnu),
1989    ("riscv64gc-unknown-linux-musl", riscv64gc_unknown_linux_musl),
1990
1991    ("sparc-unknown-none-elf", sparc_unknown_none_elf),
1992
1993    ("loongarch32-unknown-none", loongarch32_unknown_none),
1994    ("loongarch32-unknown-none-softfloat", loongarch32_unknown_none_softfloat),
1995    ("loongarch64-unknown-none", loongarch64_unknown_none),
1996    ("loongarch64-unknown-none-softfloat", loongarch64_unknown_none_softfloat),
1997
1998    ("aarch64-unknown-none", aarch64_unknown_none),
1999    ("aarch64-unknown-none-softfloat", aarch64_unknown_none_softfloat),
2000    ("aarch64-unknown-nuttx", aarch64_unknown_nuttx),
2001
2002    ("x86_64-fortanix-unknown-sgx", x86_64_fortanix_unknown_sgx),
2003
2004    ("x86_64-unknown-uefi", x86_64_unknown_uefi),
2005    ("i686-unknown-uefi", i686_unknown_uefi),
2006    ("aarch64-unknown-uefi", aarch64_unknown_uefi),
2007
2008    ("nvptx64-nvidia-cuda", nvptx64_nvidia_cuda),
2009
2010    ("amdgcn-amd-amdhsa", amdgcn_amd_amdhsa),
2011
2012    ("xtensa-esp32-none-elf", xtensa_esp32_none_elf),
2013    ("xtensa-esp32-espidf", xtensa_esp32_espidf),
2014    ("xtensa-esp32s2-none-elf", xtensa_esp32s2_none_elf),
2015    ("xtensa-esp32s2-espidf", xtensa_esp32s2_espidf),
2016    ("xtensa-esp32s3-none-elf", xtensa_esp32s3_none_elf),
2017    ("xtensa-esp32s3-espidf", xtensa_esp32s3_espidf),
2018
2019    ("i686-wrs-vxworks", i686_wrs_vxworks),
2020    ("x86_64-wrs-vxworks", x86_64_wrs_vxworks),
2021    ("armv7-wrs-vxworks-eabihf", armv7_wrs_vxworks_eabihf),
2022    ("aarch64-wrs-vxworks", aarch64_wrs_vxworks),
2023    ("powerpc-wrs-vxworks", powerpc_wrs_vxworks),
2024    ("powerpc-wrs-vxworks-spe", powerpc_wrs_vxworks_spe),
2025    ("powerpc64-wrs-vxworks", powerpc64_wrs_vxworks),
2026    ("riscv32-wrs-vxworks", riscv32_wrs_vxworks),
2027    ("riscv64-wrs-vxworks", riscv64_wrs_vxworks),
2028
2029    ("aarch64-kmc-solid_asp3", aarch64_kmc_solid_asp3),
2030    ("armv7a-kmc-solid_asp3-eabi", armv7a_kmc_solid_asp3_eabi),
2031    ("armv7a-kmc-solid_asp3-eabihf", armv7a_kmc_solid_asp3_eabihf),
2032
2033    ("mipsel-sony-psp", mipsel_sony_psp),
2034    ("mipsel-sony-psx", mipsel_sony_psx),
2035    ("mipsel-unknown-none", mipsel_unknown_none),
2036    ("mips-mti-none-elf", mips_mti_none_elf),
2037    ("mipsel-mti-none-elf", mipsel_mti_none_elf),
2038    ("thumbv4t-none-eabi", thumbv4t_none_eabi),
2039    ("armv4t-none-eabi", armv4t_none_eabi),
2040    ("thumbv5te-none-eabi", thumbv5te_none_eabi),
2041    ("armv5te-none-eabi", armv5te_none_eabi),
2042
2043    ("aarch64_be-unknown-linux-gnu", aarch64_be_unknown_linux_gnu),
2044    ("aarch64-unknown-linux-gnu_ilp32", aarch64_unknown_linux_gnu_ilp32),
2045    ("aarch64_be-unknown-linux-gnu_ilp32", aarch64_be_unknown_linux_gnu_ilp32),
2046
2047    ("bpfeb-unknown-none", bpfeb_unknown_none),
2048    ("bpfel-unknown-none", bpfel_unknown_none),
2049
2050    ("armv6k-nintendo-3ds", armv6k_nintendo_3ds),
2051
2052    ("aarch64-nintendo-switch-freestanding", aarch64_nintendo_switch_freestanding),
2053
2054    ("armv7-sony-vita-newlibeabihf", armv7_sony_vita_newlibeabihf),
2055
2056    ("armv7-unknown-linux-uclibceabi", armv7_unknown_linux_uclibceabi),
2057    ("armv7-unknown-linux-uclibceabihf", armv7_unknown_linux_uclibceabihf),
2058
2059    ("x86_64-unknown-none", x86_64_unknown_none),
2060
2061    ("aarch64-unknown-teeos", aarch64_unknown_teeos),
2062
2063    ("mips64-openwrt-linux-musl", mips64_openwrt_linux_musl),
2064
2065    ("aarch64-unknown-nto-qnx700", aarch64_unknown_nto_qnx700),
2066    ("aarch64-unknown-nto-qnx710", aarch64_unknown_nto_qnx710),
2067    ("aarch64-unknown-nto-qnx710_iosock", aarch64_unknown_nto_qnx710_iosock),
2068    ("aarch64-unknown-nto-qnx800", aarch64_unknown_nto_qnx800),
2069    ("x86_64-pc-nto-qnx710", x86_64_pc_nto_qnx710),
2070    ("x86_64-pc-nto-qnx710_iosock", x86_64_pc_nto_qnx710_iosock),
2071    ("x86_64-pc-nto-qnx800", x86_64_pc_nto_qnx800),
2072    ("i686-pc-nto-qnx700", i686_pc_nto_qnx700),
2073
2074    ("aarch64-unknown-linux-ohos", aarch64_unknown_linux_ohos),
2075    ("armv7-unknown-linux-ohos", armv7_unknown_linux_ohos),
2076    ("loongarch64-unknown-linux-ohos", loongarch64_unknown_linux_ohos),
2077    ("x86_64-unknown-linux-ohos", x86_64_unknown_linux_ohos),
2078
2079    ("x86_64-unknown-linux-none", x86_64_unknown_linux_none),
2080
2081    ("thumbv6m-nuttx-eabi", thumbv6m_nuttx_eabi),
2082    ("thumbv7a-nuttx-eabi", thumbv7a_nuttx_eabi),
2083    ("thumbv7a-nuttx-eabihf", thumbv7a_nuttx_eabihf),
2084    ("thumbv7m-nuttx-eabi", thumbv7m_nuttx_eabi),
2085    ("thumbv7em-nuttx-eabi", thumbv7em_nuttx_eabi),
2086    ("thumbv7em-nuttx-eabihf", thumbv7em_nuttx_eabihf),
2087    ("thumbv8m.base-nuttx-eabi", thumbv8m_base_nuttx_eabi),
2088    ("thumbv8m.main-nuttx-eabi", thumbv8m_main_nuttx_eabi),
2089    ("thumbv8m.main-nuttx-eabihf", thumbv8m_main_nuttx_eabihf),
2090    ("riscv32imc-unknown-nuttx-elf", riscv32imc_unknown_nuttx_elf),
2091    ("riscv32imac-unknown-nuttx-elf", riscv32imac_unknown_nuttx_elf),
2092    ("riscv32imafc-unknown-nuttx-elf", riscv32imafc_unknown_nuttx_elf),
2093    ("riscv64imac-unknown-nuttx-elf", riscv64imac_unknown_nuttx_elf),
2094    ("riscv64gc-unknown-nuttx-elf", riscv64gc_unknown_nuttx_elf),
2095    ("x86_64-lynx-lynxos178", x86_64_lynx_lynxos178),
2096
2097    ("x86_64-pc-cygwin", x86_64_pc_cygwin),
2098}
2099
2100/// Cow-Vec-Str: Cow<'static, [Cow<'static, str>]>
2101macro_rules! cvs {
2102    () => {
2103        ::std::borrow::Cow::Borrowed(&[])
2104    };
2105    ($($x:expr),+ $(,)?) => {
2106        ::std::borrow::Cow::Borrowed(&[
2107            $(
2108                ::std::borrow::Cow::Borrowed($x),
2109            )*
2110        ])
2111    };
2112}
2113
2114pub(crate) use cvs;
2115
2116/// Warnings encountered when parsing the target `json`.
2117///
2118/// Includes fields that weren't recognized and fields that don't have the expected type.
2119#[derive(Debug, PartialEq)]
2120pub struct TargetWarnings {
2121    unused_fields: Vec<String>,
2122    incorrect_type: Vec<String>,
2123}
2124
2125impl TargetWarnings {
2126    pub fn empty() -> Self {
2127        Self { unused_fields: Vec::new(), incorrect_type: Vec::new() }
2128    }
2129
2130    pub fn warning_messages(&self) -> Vec<String> {
2131        let mut warnings = vec![];
2132        if !self.unused_fields.is_empty() {
2133            warnings.push(format!(
2134                "target json file contains unused fields: {}",
2135                self.unused_fields.join(", ")
2136            ));
2137        }
2138        if !self.incorrect_type.is_empty() {
2139            warnings.push(format!(
2140                "target json file contains fields whose value doesn't have the correct json type: {}",
2141                self.incorrect_type.join(", ")
2142            ));
2143        }
2144        warnings
2145    }
2146}
2147
2148/// For the [`Target::check_consistency`] function, determines whether the given target is a builtin or a JSON
2149/// target.
2150#[derive(Copy, Clone, Debug, PartialEq)]
2151enum TargetKind {
2152    Json,
2153    Builtin,
2154}
2155
2156/// Everything `rustc` knows about how to compile for a specific target.
2157///
2158/// Every field here must be specified, and has no default value.
2159#[derive(PartialEq, Clone, Debug)]
2160pub struct Target {
2161    /// Unversioned target tuple to pass to LLVM.
2162    ///
2163    /// Target tuples can optionally contain an OS version (notably Apple targets), which rustc
2164    /// cannot know without querying the environment.
2165    ///
2166    /// Use `rustc_codegen_ssa::back::versioned_llvm_target` if you need the full LLVM target.
2167    pub llvm_target: StaticCow<str>,
2168    /// Metadata about a target, for example the description or tier.
2169    /// Used for generating target documentation.
2170    pub metadata: TargetMetadata,
2171    /// Number of bits in a pointer. Influences the `target_pointer_width` `cfg` variable.
2172    pub pointer_width: u32,
2173    /// Architecture to use for ABI considerations. Valid options include: "x86",
2174    /// "x86_64", "arm", "aarch64", "mips", "powerpc", "powerpc64", and others.
2175    pub arch: StaticCow<str>,
2176    /// [Data layout](https://llvm.org/docs/LangRef.html#data-layout) to pass to LLVM.
2177    pub data_layout: StaticCow<str>,
2178    /// Optional settings with defaults.
2179    pub options: TargetOptions,
2180}
2181
2182/// Metadata about a target like the description or tier.
2183/// Part of #120745.
2184/// All fields are optional for now, but intended to be required in the future.
2185#[derive(Default, PartialEq, Clone, Debug)]
2186pub struct TargetMetadata {
2187    /// A short description of the target including platform requirements,
2188    /// for example "64-bit Linux (kernel 3.2+, glibc 2.17+)".
2189    pub description: Option<StaticCow<str>>,
2190    /// The tier of the target. 1, 2 or 3.
2191    pub tier: Option<u64>,
2192    /// Whether the Rust project ships host tools for a target.
2193    pub host_tools: Option<bool>,
2194    /// Whether a target has the `std` library. This is usually true for targets running
2195    /// on an operating system.
2196    pub std: Option<bool>,
2197}
2198
2199impl Target {
2200    pub fn parse_data_layout(&self) -> Result<TargetDataLayout, TargetDataLayoutErrors<'_>> {
2201        let mut dl = TargetDataLayout::parse_from_llvm_datalayout_string(
2202            &self.data_layout,
2203            self.options.default_address_space,
2204        )?;
2205
2206        // Perform consistency checks against the Target information.
2207        if dl.endian != self.endian {
2208            return Err(TargetDataLayoutErrors::InconsistentTargetArchitecture {
2209                dl: dl.endian.as_str(),
2210                target: self.endian.as_str(),
2211            });
2212        }
2213
2214        let target_pointer_width: u64 = self.pointer_width.into();
2215        let dl_pointer_size: u64 = dl.pointer_size().bits();
2216        if dl_pointer_size != target_pointer_width {
2217            return Err(TargetDataLayoutErrors::InconsistentTargetPointerWidth {
2218                pointer_size: dl_pointer_size,
2219                target: self.pointer_width,
2220            });
2221        }
2222
2223        dl.c_enum_min_size = Integer::from_size(Size::from_bits(
2224            self.c_enum_min_bits.unwrap_or(self.c_int_width as _),
2225        ))
2226        .map_err(|err| TargetDataLayoutErrors::InvalidBitsSize { err })?;
2227
2228        Ok(dl)
2229    }
2230}
2231
2232pub trait HasTargetSpec {
2233    fn target_spec(&self) -> &Target;
2234}
2235
2236impl HasTargetSpec for Target {
2237    #[inline]
2238    fn target_spec(&self) -> &Target {
2239        self
2240    }
2241}
2242
2243/// x86 (32-bit) abi options.
2244#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
2245pub struct X86Abi {
2246    /// On x86-32 targets, the regparm N causes the compiler to pass arguments
2247    /// in registers EAX, EDX, and ECX instead of on the stack.
2248    pub regparm: Option<u32>,
2249    /// Override the default ABI to return small structs in registers
2250    pub reg_struct_return: bool,
2251}
2252
2253pub trait HasX86AbiOpt {
2254    fn x86_abi_opt(&self) -> X86Abi;
2255}
2256
2257type StaticCow<T> = Cow<'static, T>;
2258
2259/// Optional aspects of a target specification.
2260///
2261/// This has an implementation of `Default`, see each field for what the default is. In general,
2262/// these try to take "minimal defaults" that don't assume anything about the runtime they run in.
2263///
2264/// `TargetOptions` as a separate structure is mostly an implementation detail of `Target`
2265/// construction, all its fields logically belong to `Target` and available from `Target`
2266/// through `Deref` impls.
2267#[derive(PartialEq, Clone, Debug)]
2268pub struct TargetOptions {
2269    /// Used as the `target_endian` `cfg` variable. Defaults to little endian.
2270    pub endian: Endian,
2271    /// Width of c_int type. Defaults to "32".
2272    pub c_int_width: u16,
2273    /// OS name to use for conditional compilation (`target_os`). Defaults to "none".
2274    /// "none" implies a bare metal target without `std` library.
2275    /// A couple of targets having `std` also use "unknown" as an `os` value,
2276    /// but they are exceptions.
2277    pub os: StaticCow<str>,
2278    /// Environment name to use for conditional compilation (`target_env`). Defaults to "".
2279    pub env: StaticCow<str>,
2280    /// ABI name to distinguish multiple ABIs on the same OS and architecture. For instance, `"eabi"`
2281    /// or `"eabihf"`. Defaults to "".
2282    /// This field is *not* forwarded directly to LLVM; its primary purpose is `cfg(target_abi)`.
2283    /// However, parts of the backend do check this field for specific values to enable special behavior.
2284    pub abi: StaticCow<str>,
2285    /// Vendor name to use for conditional compilation (`target_vendor`). Defaults to "unknown".
2286    pub vendor: StaticCow<str>,
2287
2288    /// Linker to invoke
2289    pub linker: Option<StaticCow<str>>,
2290    /// Default linker flavor used if `-C linker-flavor` or `-C linker` are not passed
2291    /// on the command line. Defaults to `LinkerFlavor::Gnu(Cc::Yes, Lld::No)`.
2292    pub linker_flavor: LinkerFlavor,
2293    linker_flavor_json: LinkerFlavorCli,
2294    lld_flavor_json: LldFlavor,
2295    linker_is_gnu_json: bool,
2296
2297    /// Objects to link before and after all other object code.
2298    pub pre_link_objects: CrtObjects,
2299    pub post_link_objects: CrtObjects,
2300    /// Same as `(pre|post)_link_objects`, but when self-contained linking mode is enabled.
2301    pub pre_link_objects_self_contained: CrtObjects,
2302    pub post_link_objects_self_contained: CrtObjects,
2303    /// Behavior for the self-contained linking mode: inferred for some targets, or explicitly
2304    /// enabled (in bulk, or with individual components).
2305    pub link_self_contained: LinkSelfContainedDefault,
2306
2307    /// Linker arguments that are passed *before* any user-defined libraries.
2308    pub pre_link_args: LinkArgs,
2309    pre_link_args_json: LinkArgsCli,
2310    /// Linker arguments that are unconditionally passed after any
2311    /// user-defined but before post-link objects. Standard platform
2312    /// libraries that should be always be linked to, usually go here.
2313    pub late_link_args: LinkArgs,
2314    late_link_args_json: LinkArgsCli,
2315    /// Linker arguments used in addition to `late_link_args` if at least one
2316    /// Rust dependency is dynamically linked.
2317    pub late_link_args_dynamic: LinkArgs,
2318    late_link_args_dynamic_json: LinkArgsCli,
2319    /// Linker arguments used in addition to `late_link_args` if all Rust
2320    /// dependencies are statically linked.
2321    pub late_link_args_static: LinkArgs,
2322    late_link_args_static_json: LinkArgsCli,
2323    /// Linker arguments that are unconditionally passed *after* any
2324    /// user-defined libraries.
2325    pub post_link_args: LinkArgs,
2326    post_link_args_json: LinkArgsCli,
2327
2328    /// Optional link script applied to `dylib` and `executable` crate types.
2329    /// This is a string containing the script, not a path. Can only be applied
2330    /// to linkers where linker flavor matches `LinkerFlavor::Gnu(..)`.
2331    pub link_script: Option<StaticCow<str>>,
2332    /// Environment variables to be set for the linker invocation.
2333    pub link_env: StaticCow<[(StaticCow<str>, StaticCow<str>)]>,
2334    /// Environment variables to be removed for the linker invocation.
2335    pub link_env_remove: StaticCow<[StaticCow<str>]>,
2336
2337    /// Extra arguments to pass to the external assembler (when used)
2338    pub asm_args: StaticCow<[StaticCow<str>]>,
2339
2340    /// Default CPU to pass to LLVM. Corresponds to `llc -mcpu=$cpu`. Defaults
2341    /// to "generic".
2342    pub cpu: StaticCow<str>,
2343    /// Whether a cpu needs to be explicitly set.
2344    /// Set to true if there is no default cpu. Defaults to false.
2345    pub need_explicit_cpu: bool,
2346    /// Default target features to pass to LLVM. These features overwrite
2347    /// `-Ctarget-cpu` but can be overwritten with `-Ctarget-features`.
2348    /// Corresponds to `llc -mattr=$features`.
2349    /// Note that these are LLVM feature names, not Rust feature names!
2350    ///
2351    /// Generally it is a bad idea to use negative target features because they often interact very
2352    /// poorly with how `-Ctarget-cpu` works. Instead, try to use a lower "base CPU" and enable the
2353    /// features you want to use.
2354    pub features: StaticCow<str>,
2355    /// Direct or use GOT indirect to reference external data symbols
2356    pub direct_access_external_data: Option<bool>,
2357    /// Whether dynamic linking is available on this target. Defaults to false.
2358    pub dynamic_linking: bool,
2359    /// Whether dynamic linking can export TLS globals. Defaults to true.
2360    pub dll_tls_export: bool,
2361    /// If dynamic linking is available, whether only cdylibs are supported.
2362    pub only_cdylib: bool,
2363    /// Whether executables are available on this target. Defaults to true.
2364    pub executables: bool,
2365    /// Relocation model to use in object file. Corresponds to `llc
2366    /// -relocation-model=$relocation_model`. Defaults to `Pic`.
2367    pub relocation_model: RelocModel,
2368    /// Code model to use. Corresponds to `llc -code-model=$code_model`.
2369    /// Defaults to `None` which means "inherited from the base LLVM target".
2370    pub code_model: Option<CodeModel>,
2371    /// TLS model to use. Options are "global-dynamic" (default), "local-dynamic", "initial-exec"
2372    /// and "local-exec". This is similar to the -ftls-model option in GCC/Clang.
2373    pub tls_model: TlsModel,
2374    /// Do not emit code that uses the "red zone", if the ABI has one. Defaults to false.
2375    pub disable_redzone: bool,
2376    /// Frame pointer mode for this target. Defaults to `MayOmit`.
2377    pub frame_pointer: FramePointer,
2378    /// Emit each function in its own section. Defaults to true.
2379    pub function_sections: bool,
2380    /// String to prepend to the name of every dynamic library. Defaults to "lib".
2381    pub dll_prefix: StaticCow<str>,
2382    /// String to append to the name of every dynamic library. Defaults to ".so".
2383    pub dll_suffix: StaticCow<str>,
2384    /// String to append to the name of every executable.
2385    pub exe_suffix: StaticCow<str>,
2386    /// String to prepend to the name of every static library. Defaults to "lib".
2387    pub staticlib_prefix: StaticCow<str>,
2388    /// String to append to the name of every static library. Defaults to ".a".
2389    pub staticlib_suffix: StaticCow<str>,
2390    /// Values of the `target_family` cfg set for this target.
2391    ///
2392    /// Common options are: "unix", "windows". Defaults to no families.
2393    ///
2394    /// See <https://doc.rust-lang.org/reference/conditional-compilation.html#target_family>.
2395    pub families: StaticCow<[StaticCow<str>]>,
2396    /// Whether the target toolchain's ABI supports returning small structs as an integer.
2397    pub abi_return_struct_as_int: bool,
2398    /// Whether the target toolchain is like AIX's. Linker options on AIX are special and it uses
2399    /// XCOFF as binary format. Defaults to false.
2400    pub is_like_aix: bool,
2401    /// Whether the target toolchain is like macOS's. Only useful for compiling against iOS/macOS,
2402    /// in particular running dsymutil and some other stuff like `-dead_strip`. Defaults to false.
2403    /// Also indicates whether to use Apple-specific ABI changes, such as extending function
2404    /// parameters to 32-bits.
2405    pub is_like_darwin: bool,
2406    /// Whether the target toolchain is like Solaris's.
2407    /// Only useful for compiling against Illumos/Solaris,
2408    /// as they have a different set of linker flags. Defaults to false.
2409    pub is_like_solaris: bool,
2410    /// Whether the target is like Windows.
2411    /// This is a combination of several more specific properties represented as a single flag:
2412    ///   - The target uses a Windows ABI,
2413    ///   - uses PE/COFF as a format for object code,
2414    ///   - uses Windows-style dllexport/dllimport for shared libraries,
2415    ///   - uses import libraries and .def files for symbol exports,
2416    ///   - executables support setting a subsystem.
2417    pub is_like_windows: bool,
2418    /// Whether the target is like MSVC.
2419    /// This is a combination of several more specific properties represented as a single flag:
2420    ///   - The target has all the properties from `is_like_windows`
2421    ///     (for in-tree targets "is_like_msvc ⇒ is_like_windows" is ensured by a unit test),
2422    ///   - has some MSVC-specific Windows ABI properties,
2423    ///   - uses a link.exe-like linker,
2424    ///   - uses CodeView/PDB for debuginfo and natvis for its visualization,
2425    ///   - uses SEH-based unwinding,
2426    ///   - supports control flow guard mechanism.
2427    pub is_like_msvc: bool,
2428    /// Whether a target toolchain is like WASM.
2429    pub is_like_wasm: bool,
2430    /// Whether a target toolchain is like Android, implying a Linux kernel and a Bionic libc
2431    pub is_like_android: bool,
2432    /// Target's binary file format. Defaults to BinaryFormat::Elf
2433    pub binary_format: BinaryFormat,
2434    /// Default supported version of DWARF on this platform.
2435    /// Useful because some platforms (osx, bsd) only want up to DWARF2.
2436    pub default_dwarf_version: u32,
2437    /// The MinGW toolchain has a known issue that prevents it from correctly
2438    /// handling COFF object files with more than 2<sup>15</sup> sections. Since each weak
2439    /// symbol needs its own COMDAT section, weak linkage implies a large
2440    /// number sections that easily exceeds the given limit for larger
2441    /// codebases. Consequently we want a way to disallow weak linkage on some
2442    /// platforms.
2443    pub allows_weak_linkage: bool,
2444    /// Whether the linker support rpaths or not. Defaults to false.
2445    pub has_rpath: bool,
2446    /// Whether to disable linking to the default libraries, typically corresponds
2447    /// to `-nodefaultlibs`. Defaults to true.
2448    pub no_default_libraries: bool,
2449    /// Dynamically linked executables can be compiled as position independent
2450    /// if the default relocation model of position independent code is not
2451    /// changed. This is a requirement to take advantage of ASLR, as otherwise
2452    /// the functions in the executable are not randomized and can be used
2453    /// during an exploit of a vulnerability in any code.
2454    pub position_independent_executables: bool,
2455    /// Executables that are both statically linked and position-independent are supported.
2456    pub static_position_independent_executables: bool,
2457    /// Determines if the target always requires using the PLT for indirect
2458    /// library calls or not. This controls the default value of the `-Z plt` flag.
2459    pub plt_by_default: bool,
2460    /// Either partial, full, or off. Full RELRO makes the dynamic linker
2461    /// resolve all symbols at startup and marks the GOT read-only before
2462    /// starting the program, preventing overwriting the GOT.
2463    pub relro_level: RelroLevel,
2464    /// Format that archives should be emitted in. This affects whether we use
2465    /// LLVM to assemble an archive or fall back to the system linker, and
2466    /// currently only "gnu" is used to fall into LLVM. Unknown strings cause
2467    /// the system linker to be used.
2468    pub archive_format: StaticCow<str>,
2469    /// Is asm!() allowed? Defaults to true.
2470    pub allow_asm: bool,
2471    /// Whether the runtime startup code requires the `main` function be passed
2472    /// `argc` and `argv` values.
2473    pub main_needs_argc_argv: bool,
2474
2475    /// Flag indicating whether #[thread_local] is available for this target.
2476    pub has_thread_local: bool,
2477    /// This is mainly for easy compatibility with emscripten.
2478    /// If we give emcc .o files that are actually .bc files it
2479    /// will 'just work'.
2480    pub obj_is_bitcode: bool,
2481    /// Content of the LLVM cmdline section associated with embedded bitcode.
2482    pub bitcode_llvm_cmdline: StaticCow<str>,
2483
2484    /// Don't use this field; instead use the `.min_atomic_width()` method.
2485    pub min_atomic_width: Option<u64>,
2486
2487    /// Don't use this field; instead use the `.max_atomic_width()` method.
2488    pub max_atomic_width: Option<u64>,
2489
2490    /// Whether the target supports atomic CAS operations natively
2491    pub atomic_cas: bool,
2492
2493    /// Panic strategy: "unwind" or "abort"
2494    pub panic_strategy: PanicStrategy,
2495
2496    /// Whether or not linking dylibs to a static CRT is allowed.
2497    pub crt_static_allows_dylibs: bool,
2498    /// Whether or not the CRT is statically linked by default.
2499    pub crt_static_default: bool,
2500    /// Whether or not crt-static is respected by the compiler (or is a no-op).
2501    pub crt_static_respected: bool,
2502
2503    /// The implementation of stack probes to use.
2504    pub stack_probes: StackProbeType,
2505
2506    /// The minimum alignment for global symbols.
2507    pub min_global_align: Option<Align>,
2508
2509    /// Default number of codegen units to use in debug mode
2510    pub default_codegen_units: Option<u64>,
2511
2512    /// Default codegen backend used for this target. Defaults to `None`.
2513    ///
2514    /// If `None`, then `CFG_DEFAULT_CODEGEN_BACKEND` environmental variable captured when
2515    /// compiling `rustc` will be used instead (or llvm if it is not set).
2516    ///
2517    /// N.B. when *using* the compiler, backend can always be overridden with `-Zcodegen-backend`.
2518    ///
2519    /// This was added by WaffleLapkin in #116793. The motivation is a rustc fork that requires a
2520    /// custom codegen backend for a particular target.
2521    pub default_codegen_backend: Option<StaticCow<str>>,
2522
2523    /// Whether to generate trap instructions in places where optimization would
2524    /// otherwise produce control flow that falls through into unrelated memory.
2525    pub trap_unreachable: bool,
2526
2527    /// This target requires everything to be compiled with LTO to emit a final
2528    /// executable, aka there is no native linker for this target.
2529    pub requires_lto: bool,
2530
2531    /// This target has no support for threads.
2532    pub singlethread: bool,
2533
2534    /// Whether library functions call lowering/optimization is disabled in LLVM
2535    /// for this target unconditionally.
2536    pub no_builtins: bool,
2537
2538    /// The default visibility for symbols in this target.
2539    ///
2540    /// This value typically shouldn't be accessed directly, but through the
2541    /// `rustc_session::Session::default_visibility` method, which allows `rustc` users to override
2542    /// this setting using cmdline flags.
2543    pub default_visibility: Option<SymbolVisibility>,
2544
2545    /// Whether a .debug_gdb_scripts section will be added to the output object file
2546    pub emit_debug_gdb_scripts: bool,
2547
2548    /// Whether or not to unconditionally `uwtable` attributes on functions,
2549    /// typically because the platform needs to unwind for things like stack
2550    /// unwinders.
2551    pub requires_uwtable: bool,
2552
2553    /// Whether or not to emit `uwtable` attributes on functions if `-C force-unwind-tables`
2554    /// is not specified and `uwtable` is not required on this target.
2555    pub default_uwtable: bool,
2556
2557    /// Whether or not SIMD types are passed by reference in the Rust ABI,
2558    /// typically required if a target can be compiled with a mixed set of
2559    /// target features. This is `true` by default, and `false` for targets like
2560    /// wasm32 where the whole program either has simd or not.
2561    pub simd_types_indirect: bool,
2562
2563    /// Pass a list of symbol which should be exported in the dylib to the linker.
2564    pub limit_rdylib_exports: bool,
2565
2566    /// If set, have the linker export exactly these symbols, instead of using
2567    /// the usual logic to figure this out from the crate itself.
2568    pub override_export_symbols: Option<StaticCow<[StaticCow<str>]>>,
2569
2570    /// Determines how or whether the MergeFunctions LLVM pass should run for
2571    /// this target. Either "disabled", "trampolines", or "aliases".
2572    /// The MergeFunctions pass is generally useful, but some targets may need
2573    /// to opt out. The default is "aliases".
2574    ///
2575    /// Workaround for: <https://github.com/rust-lang/rust/issues/57356>
2576    pub merge_functions: MergeFunctions,
2577
2578    /// Use platform dependent mcount function
2579    pub mcount: StaticCow<str>,
2580
2581    /// Use LLVM intrinsic for mcount function name
2582    pub llvm_mcount_intrinsic: Option<StaticCow<str>>,
2583
2584    /// LLVM ABI name, corresponds to the '-mabi' parameter available in multilib C compilers
2585    /// and the `-target-abi` flag in llc. In the LLVM API this is `MCOptions.ABIName`.
2586    pub llvm_abiname: StaticCow<str>,
2587
2588    /// Control the float ABI to use, for architectures that support it. The only architecture we
2589    /// currently use this for is ARM. Corresponds to the `-float-abi` flag in llc. In the LLVM API
2590    /// this is `FloatABIType`. (clang's `-mfloat-abi` is similar but more complicated since it
2591    /// can also affect the `soft-float` target feature.)
2592    ///
2593    /// If not provided, LLVM will infer the float ABI from the target triple (`llvm_target`).
2594    pub llvm_floatabi: Option<FloatAbi>,
2595
2596    /// Picks a specific ABI for this target. This is *not* just for "Rust" ABI functions,
2597    /// it can also affect "C" ABI functions; the point is that this flag is interpreted by
2598    /// rustc and not forwarded to LLVM.
2599    /// So far, this is only used on x86.
2600    pub rustc_abi: Option<RustcAbi>,
2601
2602    /// Whether or not RelaxElfRelocation flag will be passed to the linker
2603    pub relax_elf_relocations: bool,
2604
2605    /// Additional arguments to pass to LLVM, similar to the `-C llvm-args` codegen option.
2606    pub llvm_args: StaticCow<[StaticCow<str>]>,
2607
2608    /// Whether to use legacy .ctors initialization hooks rather than .init_array. Defaults
2609    /// to false (uses .init_array).
2610    pub use_ctors_section: bool,
2611
2612    /// Whether the linker is instructed to add a `GNU_EH_FRAME` ELF header
2613    /// used to locate unwinding information is passed
2614    /// (only has effect if the linker is `ld`-like).
2615    pub eh_frame_header: bool,
2616
2617    /// Is true if the target is an ARM architecture using thumb v1 which allows for
2618    /// thumb and arm interworking.
2619    pub has_thumb_interworking: bool,
2620
2621    /// Which kind of debuginfo is used by this target?
2622    pub debuginfo_kind: DebuginfoKind,
2623    /// How to handle split debug information, if at all. Specifying `None` has
2624    /// target-specific meaning.
2625    pub split_debuginfo: SplitDebuginfo,
2626    /// Which kinds of split debuginfo are supported by the target?
2627    pub supported_split_debuginfo: StaticCow<[SplitDebuginfo]>,
2628
2629    /// The sanitizers supported by this target
2630    ///
2631    /// Note that the support here is at a codegen level. If the machine code with sanitizer
2632    /// enabled can generated on this target, but the necessary supporting libraries are not
2633    /// distributed with the target, the sanitizer should still appear in this list for the target.
2634    pub supported_sanitizers: SanitizerSet,
2635
2636    /// Minimum number of bits in #[repr(C)] enum. Defaults to the size of c_int
2637    pub c_enum_min_bits: Option<u64>,
2638
2639    /// Whether or not the DWARF `.debug_aranges` section should be generated.
2640    pub generate_arange_section: bool,
2641
2642    /// Whether the target supports stack canary checks. `true` by default,
2643    /// since this is most common among tier 1 and tier 2 targets.
2644    pub supports_stack_protector: bool,
2645
2646    /// The name of entry function.
2647    /// Default value is "main"
2648    pub entry_name: StaticCow<str>,
2649
2650    /// The ABI of the entry function.
2651    /// Default value is `CanonAbi::C`
2652    pub entry_abi: CanonAbi,
2653
2654    /// Whether the target supports XRay instrumentation.
2655    pub supports_xray: bool,
2656
2657    /// The default address space for this target. When using LLVM as a backend, most targets simply
2658    /// use LLVM's default address space (0). Some other targets, such as CHERI targets, use a
2659    /// custom default address space (in this specific case, `200`).
2660    pub default_address_space: rustc_abi::AddressSpace,
2661
2662    /// Whether the targets supports -Z small-data-threshold
2663    small_data_threshold_support: SmallDataThresholdSupport,
2664}
2665
2666/// Add arguments for the given flavor and also for its "twin" flavors
2667/// that have a compatible command line interface.
2668fn add_link_args_iter(
2669    link_args: &mut LinkArgs,
2670    flavor: LinkerFlavor,
2671    args: impl Iterator<Item = StaticCow<str>> + Clone,
2672) {
2673    let mut insert = |flavor| link_args.entry(flavor).or_default().extend(args.clone());
2674    insert(flavor);
2675    match flavor {
2676        LinkerFlavor::Gnu(cc, lld) => {
2677            assert_eq!(lld, Lld::No);
2678            insert(LinkerFlavor::Gnu(cc, Lld::Yes));
2679        }
2680        LinkerFlavor::Darwin(cc, lld) => {
2681            assert_eq!(lld, Lld::No);
2682            insert(LinkerFlavor::Darwin(cc, Lld::Yes));
2683        }
2684        LinkerFlavor::Msvc(lld) => {
2685            assert_eq!(lld, Lld::No);
2686            insert(LinkerFlavor::Msvc(Lld::Yes));
2687        }
2688        LinkerFlavor::WasmLld(..)
2689        | LinkerFlavor::Unix(..)
2690        | LinkerFlavor::EmCc
2691        | LinkerFlavor::Bpf
2692        | LinkerFlavor::Llbc
2693        | LinkerFlavor::Ptx => {}
2694    }
2695}
2696
2697fn add_link_args(link_args: &mut LinkArgs, flavor: LinkerFlavor, args: &[&'static str]) {
2698    add_link_args_iter(link_args, flavor, args.iter().copied().map(Cow::Borrowed))
2699}
2700
2701impl TargetOptions {
2702    pub fn supports_comdat(&self) -> bool {
2703        // XCOFF and MachO don't support COMDAT.
2704        !self.is_like_aix && !self.is_like_darwin
2705    }
2706}
2707
2708impl TargetOptions {
2709    fn link_args(flavor: LinkerFlavor, args: &[&'static str]) -> LinkArgs {
2710        let mut link_args = LinkArgs::new();
2711        add_link_args(&mut link_args, flavor, args);
2712        link_args
2713    }
2714
2715    fn add_pre_link_args(&mut self, flavor: LinkerFlavor, args: &[&'static str]) {
2716        add_link_args(&mut self.pre_link_args, flavor, args);
2717    }
2718
2719    fn update_from_cli(&mut self) {
2720        self.linker_flavor = LinkerFlavor::from_cli_json(
2721            self.linker_flavor_json,
2722            self.lld_flavor_json,
2723            self.linker_is_gnu_json,
2724        );
2725        for (args, args_json) in [
2726            (&mut self.pre_link_args, &self.pre_link_args_json),
2727            (&mut self.late_link_args, &self.late_link_args_json),
2728            (&mut self.late_link_args_dynamic, &self.late_link_args_dynamic_json),
2729            (&mut self.late_link_args_static, &self.late_link_args_static_json),
2730            (&mut self.post_link_args, &self.post_link_args_json),
2731        ] {
2732            args.clear();
2733            for (flavor, args_json) in args_json {
2734                let linker_flavor = self.linker_flavor.with_cli_hints(*flavor);
2735                // Normalize to no lld to avoid asserts.
2736                let linker_flavor = match linker_flavor {
2737                    LinkerFlavor::Gnu(cc, _) => LinkerFlavor::Gnu(cc, Lld::No),
2738                    LinkerFlavor::Darwin(cc, _) => LinkerFlavor::Darwin(cc, Lld::No),
2739                    LinkerFlavor::Msvc(_) => LinkerFlavor::Msvc(Lld::No),
2740                    _ => linker_flavor,
2741                };
2742                if !args.contains_key(&linker_flavor) {
2743                    add_link_args_iter(args, linker_flavor, args_json.iter().cloned());
2744                }
2745            }
2746        }
2747    }
2748
2749    fn update_to_cli(&mut self) {
2750        self.linker_flavor_json = self.linker_flavor.to_cli_counterpart();
2751        self.lld_flavor_json = self.linker_flavor.lld_flavor();
2752        self.linker_is_gnu_json = self.linker_flavor.is_gnu();
2753        for (args, args_json) in [
2754            (&self.pre_link_args, &mut self.pre_link_args_json),
2755            (&self.late_link_args, &mut self.late_link_args_json),
2756            (&self.late_link_args_dynamic, &mut self.late_link_args_dynamic_json),
2757            (&self.late_link_args_static, &mut self.late_link_args_static_json),
2758            (&self.post_link_args, &mut self.post_link_args_json),
2759        ] {
2760            *args_json = args
2761                .iter()
2762                .map(|(flavor, args)| (flavor.to_cli_counterpart(), args.clone()))
2763                .collect();
2764        }
2765    }
2766}
2767
2768impl Default for TargetOptions {
2769    /// Creates a set of "sane defaults" for any target. This is still
2770    /// incomplete, and if used for compilation, will certainly not work.
2771    fn default() -> TargetOptions {
2772        TargetOptions {
2773            endian: Endian::Little,
2774            c_int_width: 32,
2775            os: "none".into(),
2776            env: "".into(),
2777            abi: "".into(),
2778            vendor: "unknown".into(),
2779            linker: option_env!("CFG_DEFAULT_LINKER").map(|s| s.into()),
2780            linker_flavor: LinkerFlavor::Gnu(Cc::Yes, Lld::No),
2781            linker_flavor_json: LinkerFlavorCli::Gcc,
2782            lld_flavor_json: LldFlavor::Ld,
2783            linker_is_gnu_json: true,
2784            link_script: None,
2785            asm_args: cvs![],
2786            cpu: "generic".into(),
2787            need_explicit_cpu: false,
2788            features: "".into(),
2789            direct_access_external_data: None,
2790            dynamic_linking: false,
2791            dll_tls_export: true,
2792            only_cdylib: false,
2793            executables: true,
2794            relocation_model: RelocModel::Pic,
2795            code_model: None,
2796            tls_model: TlsModel::GeneralDynamic,
2797            disable_redzone: false,
2798            frame_pointer: FramePointer::MayOmit,
2799            function_sections: true,
2800            dll_prefix: "lib".into(),
2801            dll_suffix: ".so".into(),
2802            exe_suffix: "".into(),
2803            staticlib_prefix: "lib".into(),
2804            staticlib_suffix: ".a".into(),
2805            families: cvs![],
2806            abi_return_struct_as_int: false,
2807            is_like_aix: false,
2808            is_like_darwin: false,
2809            is_like_solaris: false,
2810            is_like_windows: false,
2811            is_like_msvc: false,
2812            is_like_wasm: false,
2813            is_like_android: false,
2814            binary_format: BinaryFormat::Elf,
2815            default_dwarf_version: 4,
2816            allows_weak_linkage: true,
2817            has_rpath: false,
2818            no_default_libraries: true,
2819            position_independent_executables: false,
2820            static_position_independent_executables: false,
2821            plt_by_default: true,
2822            relro_level: RelroLevel::None,
2823            pre_link_objects: Default::default(),
2824            post_link_objects: Default::default(),
2825            pre_link_objects_self_contained: Default::default(),
2826            post_link_objects_self_contained: Default::default(),
2827            link_self_contained: LinkSelfContainedDefault::False,
2828            pre_link_args: LinkArgs::new(),
2829            pre_link_args_json: LinkArgsCli::new(),
2830            late_link_args: LinkArgs::new(),
2831            late_link_args_json: LinkArgsCli::new(),
2832            late_link_args_dynamic: LinkArgs::new(),
2833            late_link_args_dynamic_json: LinkArgsCli::new(),
2834            late_link_args_static: LinkArgs::new(),
2835            late_link_args_static_json: LinkArgsCli::new(),
2836            post_link_args: LinkArgs::new(),
2837            post_link_args_json: LinkArgsCli::new(),
2838            link_env: cvs![],
2839            link_env_remove: cvs![],
2840            archive_format: "gnu".into(),
2841            main_needs_argc_argv: true,
2842            allow_asm: true,
2843            has_thread_local: false,
2844            obj_is_bitcode: false,
2845            bitcode_llvm_cmdline: "".into(),
2846            min_atomic_width: None,
2847            max_atomic_width: None,
2848            atomic_cas: true,
2849            panic_strategy: PanicStrategy::Unwind,
2850            crt_static_allows_dylibs: false,
2851            crt_static_default: false,
2852            crt_static_respected: false,
2853            stack_probes: StackProbeType::None,
2854            min_global_align: None,
2855            default_codegen_units: None,
2856            default_codegen_backend: None,
2857            trap_unreachable: true,
2858            requires_lto: false,
2859            singlethread: false,
2860            no_builtins: false,
2861            default_visibility: None,
2862            emit_debug_gdb_scripts: true,
2863            requires_uwtable: false,
2864            default_uwtable: false,
2865            simd_types_indirect: true,
2866            limit_rdylib_exports: true,
2867            override_export_symbols: None,
2868            merge_functions: MergeFunctions::Aliases,
2869            mcount: "mcount".into(),
2870            llvm_mcount_intrinsic: None,
2871            llvm_abiname: "".into(),
2872            llvm_floatabi: None,
2873            rustc_abi: None,
2874            relax_elf_relocations: false,
2875            llvm_args: cvs![],
2876            use_ctors_section: false,
2877            eh_frame_header: true,
2878            has_thumb_interworking: false,
2879            debuginfo_kind: Default::default(),
2880            split_debuginfo: Default::default(),
2881            // `Off` is supported by default, but targets can remove this manually, e.g. Windows.
2882            supported_split_debuginfo: Cow::Borrowed(&[SplitDebuginfo::Off]),
2883            supported_sanitizers: SanitizerSet::empty(),
2884            c_enum_min_bits: None,
2885            generate_arange_section: true,
2886            supports_stack_protector: true,
2887            entry_name: "main".into(),
2888            entry_abi: CanonAbi::C,
2889            supports_xray: false,
2890            default_address_space: rustc_abi::AddressSpace::ZERO,
2891            small_data_threshold_support: SmallDataThresholdSupport::DefaultForArch,
2892        }
2893    }
2894}
2895
2896/// `TargetOptions` being a separate type is basically an implementation detail of `Target` that is
2897/// used for providing defaults. Perhaps there's a way to merge `TargetOptions` into `Target` so
2898/// this `Deref` implementation is no longer necessary.
2899impl Deref for Target {
2900    type Target = TargetOptions;
2901
2902    #[inline]
2903    fn deref(&self) -> &Self::Target {
2904        &self.options
2905    }
2906}
2907impl DerefMut for Target {
2908    #[inline]
2909    fn deref_mut(&mut self) -> &mut Self::Target {
2910        &mut self.options
2911    }
2912}
2913
2914impl Target {
2915    pub fn is_abi_supported(&self, abi: ExternAbi) -> bool {
2916        let abi_map = AbiMap::from_target(self);
2917        abi_map.canonize_abi(abi, false).is_mapped()
2918    }
2919
2920    /// Minimum integer size in bits that this target can perform atomic
2921    /// operations on.
2922    pub fn min_atomic_width(&self) -> u64 {
2923        self.min_atomic_width.unwrap_or(8)
2924    }
2925
2926    /// Maximum integer size in bits that this target can perform atomic
2927    /// operations on.
2928    pub fn max_atomic_width(&self) -> u64 {
2929        self.max_atomic_width.unwrap_or_else(|| self.pointer_width.into())
2930    }
2931
2932    /// Check some basic consistency of the current target. For JSON targets we are less strict;
2933    /// some of these checks are more guidelines than strict rules.
2934    fn check_consistency(&self, kind: TargetKind) -> Result<(), String> {
2935        macro_rules! check {
2936            ($b:expr, $($msg:tt)*) => {
2937                if !$b {
2938                    return Err(format!($($msg)*));
2939                }
2940            }
2941        }
2942        macro_rules! check_eq {
2943            ($left:expr, $right:expr, $($msg:tt)*) => {
2944                if ($left) != ($right) {
2945                    return Err(format!($($msg)*));
2946                }
2947            }
2948        }
2949        macro_rules! check_ne {
2950            ($left:expr, $right:expr, $($msg:tt)*) => {
2951                if ($left) == ($right) {
2952                    return Err(format!($($msg)*));
2953                }
2954            }
2955        }
2956        macro_rules! check_matches {
2957            ($left:expr, $right:pat, $($msg:tt)*) => {
2958                if !matches!($left, $right) {
2959                    return Err(format!($($msg)*));
2960                }
2961            }
2962        }
2963
2964        check_eq!(
2965            self.is_like_darwin,
2966            self.vendor == "apple",
2967            "`is_like_darwin` must be set if and only if `vendor` is `apple`"
2968        );
2969        check_eq!(
2970            self.is_like_solaris,
2971            self.os == "solaris" || self.os == "illumos",
2972            "`is_like_solaris` must be set if and only if `os` is `solaris` or `illumos`"
2973        );
2974        check_eq!(
2975            self.is_like_windows,
2976            self.os == "windows" || self.os == "uefi" || self.os == "cygwin",
2977            "`is_like_windows` must be set if and only if `os` is `windows`, `uefi` or `cygwin`"
2978        );
2979        check_eq!(
2980            self.is_like_wasm,
2981            self.arch == "wasm32" || self.arch == "wasm64",
2982            "`is_like_wasm` must be set if and only if `arch` is `wasm32` or `wasm64`"
2983        );
2984        if self.is_like_msvc {
2985            check!(self.is_like_windows, "if `is_like_msvc` is set, `is_like_windows` must be set");
2986        }
2987        if self.os == "emscripten" {
2988            check!(self.is_like_wasm, "the `emcscripten` os only makes sense on wasm-like targets");
2989        }
2990
2991        // Check that default linker flavor is compatible with some other key properties.
2992        check_eq!(
2993            self.is_like_darwin,
2994            matches!(self.linker_flavor, LinkerFlavor::Darwin(..)),
2995            "`linker_flavor` must be `darwin` if and only if `is_like_darwin` is set"
2996        );
2997        check_eq!(
2998            self.is_like_msvc,
2999            matches!(self.linker_flavor, LinkerFlavor::Msvc(..)),
3000            "`linker_flavor` must be `msvc` if and only if `is_like_msvc` is set"
3001        );
3002        check_eq!(
3003            self.is_like_wasm && self.os != "emscripten",
3004            matches!(self.linker_flavor, LinkerFlavor::WasmLld(..)),
3005            "`linker_flavor` must be `wasm-lld` if and only if `is_like_wasm` is set and the `os` is not `emscripten`",
3006        );
3007        check_eq!(
3008            self.os == "emscripten",
3009            matches!(self.linker_flavor, LinkerFlavor::EmCc),
3010            "`linker_flavor` must be `em-cc` if and only if `os` is `emscripten`"
3011        );
3012        check_eq!(
3013            self.arch == "bpf",
3014            matches!(self.linker_flavor, LinkerFlavor::Bpf),
3015            "`linker_flavor` must be `bpf` if and only if `arch` is `bpf`"
3016        );
3017        check_eq!(
3018            self.arch == "nvptx64",
3019            matches!(self.linker_flavor, LinkerFlavor::Ptx),
3020            "`linker_flavor` must be `ptc` if and only if `arch` is `nvptx64`"
3021        );
3022
3023        for args in [
3024            &self.pre_link_args,
3025            &self.late_link_args,
3026            &self.late_link_args_dynamic,
3027            &self.late_link_args_static,
3028            &self.post_link_args,
3029        ] {
3030            for (&flavor, flavor_args) in args {
3031                check!(
3032                    !flavor_args.is_empty() || self.arch == "avr",
3033                    "linker flavor args must not be empty"
3034                );
3035                // Check that flavors mentioned in link args are compatible with the default flavor.
3036                match self.linker_flavor {
3037                    LinkerFlavor::Gnu(..) => {
3038                        check_matches!(
3039                            flavor,
3040                            LinkerFlavor::Gnu(..),
3041                            "mixing GNU and non-GNU linker flavors"
3042                        );
3043                    }
3044                    LinkerFlavor::Darwin(..) => {
3045                        check_matches!(
3046                            flavor,
3047                            LinkerFlavor::Darwin(..),
3048                            "mixing Darwin and non-Darwin linker flavors"
3049                        )
3050                    }
3051                    LinkerFlavor::WasmLld(..) => {
3052                        check_matches!(
3053                            flavor,
3054                            LinkerFlavor::WasmLld(..),
3055                            "mixing wasm and non-wasm linker flavors"
3056                        )
3057                    }
3058                    LinkerFlavor::Unix(..) => {
3059                        check_matches!(
3060                            flavor,
3061                            LinkerFlavor::Unix(..),
3062                            "mixing unix and non-unix linker flavors"
3063                        );
3064                    }
3065                    LinkerFlavor::Msvc(..) => {
3066                        check_matches!(
3067                            flavor,
3068                            LinkerFlavor::Msvc(..),
3069                            "mixing MSVC and non-MSVC linker flavors"
3070                        );
3071                    }
3072                    LinkerFlavor::EmCc
3073                    | LinkerFlavor::Bpf
3074                    | LinkerFlavor::Ptx
3075                    | LinkerFlavor::Llbc => {
3076                        check_eq!(flavor, self.linker_flavor, "mixing different linker flavors")
3077                    }
3078                }
3079
3080                // Check that link args for cc and non-cc versions of flavors are consistent.
3081                let check_noncc = |noncc_flavor| -> Result<(), String> {
3082                    if let Some(noncc_args) = args.get(&noncc_flavor) {
3083                        for arg in flavor_args {
3084                            if let Some(suffix) = arg.strip_prefix("-Wl,") {
3085                                check!(
3086                                    noncc_args.iter().any(|a| a == suffix),
3087                                    " link args for cc and non-cc versions of flavors are not consistent"
3088                                );
3089                            }
3090                        }
3091                    }
3092                    Ok(())
3093                };
3094
3095                match self.linker_flavor {
3096                    LinkerFlavor::Gnu(Cc::Yes, lld) => check_noncc(LinkerFlavor::Gnu(Cc::No, lld))?,
3097                    LinkerFlavor::WasmLld(Cc::Yes) => check_noncc(LinkerFlavor::WasmLld(Cc::No))?,
3098                    LinkerFlavor::Unix(Cc::Yes) => check_noncc(LinkerFlavor::Unix(Cc::No))?,
3099                    _ => {}
3100                }
3101            }
3102
3103            // Check that link args for lld and non-lld versions of flavors are consistent.
3104            for cc in [Cc::No, Cc::Yes] {
3105                check_eq!(
3106                    args.get(&LinkerFlavor::Gnu(cc, Lld::No)),
3107                    args.get(&LinkerFlavor::Gnu(cc, Lld::Yes)),
3108                    "link args for lld and non-lld versions of flavors are not consistent",
3109                );
3110                check_eq!(
3111                    args.get(&LinkerFlavor::Darwin(cc, Lld::No)),
3112                    args.get(&LinkerFlavor::Darwin(cc, Lld::Yes)),
3113                    "link args for lld and non-lld versions of flavors are not consistent",
3114                );
3115            }
3116            check_eq!(
3117                args.get(&LinkerFlavor::Msvc(Lld::No)),
3118                args.get(&LinkerFlavor::Msvc(Lld::Yes)),
3119                "link args for lld and non-lld versions of flavors are not consistent",
3120            );
3121        }
3122
3123        if self.link_self_contained.is_disabled() {
3124            check!(
3125                self.pre_link_objects_self_contained.is_empty()
3126                    && self.post_link_objects_self_contained.is_empty(),
3127                "if `link_self_contained` is disabled, then `pre_link_objects_self_contained` and `post_link_objects_self_contained` must be empty",
3128            );
3129        }
3130
3131        // If your target really needs to deviate from the rules below,
3132        // except it and document the reasons.
3133        // Keep the default "unknown" vendor instead.
3134        check_ne!(self.vendor, "", "`vendor` cannot be empty");
3135        check_ne!(self.os, "", "`os` cannot be empty");
3136        if !self.can_use_os_unknown() {
3137            // Keep the default "none" for bare metal targets instead.
3138            check_ne!(
3139                self.os,
3140                "unknown",
3141                "`unknown` os can only be used on particular targets; use `none` for bare-metal targets"
3142            );
3143        }
3144
3145        // Check dynamic linking stuff.
3146        // We skip this for JSON targets since otherwise, our default values would fail this test.
3147        // These checks are not critical for correctness, but more like default guidelines.
3148        // FIXME (https://github.com/rust-lang/rust/issues/133459): do we want to change the JSON
3149        // target defaults so that they pass these checks?
3150        if kind == TargetKind::Builtin {
3151            // BPF: when targeting user space vms (like rbpf), those can load dynamic libraries.
3152            // hexagon: when targeting QuRT, that OS can load dynamic libraries.
3153            // wasm{32,64}: dynamic linking is inherent in the definition of the VM.
3154            if self.os == "none"
3155                && (self.arch != "bpf"
3156                    && self.arch != "hexagon"
3157                    && self.arch != "wasm32"
3158                    && self.arch != "wasm64")
3159            {
3160                check!(
3161                    !self.dynamic_linking,
3162                    "dynamic linking is not supported on this OS/architecture"
3163                );
3164            }
3165            if self.only_cdylib
3166                || self.crt_static_allows_dylibs
3167                || !self.late_link_args_dynamic.is_empty()
3168            {
3169                check!(
3170                    self.dynamic_linking,
3171                    "dynamic linking must be allowed when `only_cdylib` or `crt_static_allows_dylibs` or `late_link_args_dynamic` are set"
3172                );
3173            }
3174            // Apparently PIC was slow on wasm at some point, see comments in wasm_base.rs
3175            if self.dynamic_linking && !self.is_like_wasm {
3176                check_eq!(
3177                    self.relocation_model,
3178                    RelocModel::Pic,
3179                    "targets that support dynamic linking must use the `pic` relocation model"
3180                );
3181            }
3182            if self.position_independent_executables {
3183                check_eq!(
3184                    self.relocation_model,
3185                    RelocModel::Pic,
3186                    "targets that support position-independent executables must use the `pic` relocation model"
3187                );
3188            }
3189            // The UEFI targets do not support dynamic linking but still require PIC (#101377).
3190            if self.relocation_model == RelocModel::Pic && (self.os != "uefi") {
3191                check!(
3192                    self.dynamic_linking || self.position_independent_executables,
3193                    "when the relocation model is `pic`, the target must support dynamic linking or use position-independent executables. \
3194                Set the relocation model to `static` to avoid this requirement"
3195                );
3196            }
3197            if self.static_position_independent_executables {
3198                check!(
3199                    self.position_independent_executables,
3200                    "if `static_position_independent_executables` is set, then `position_independent_executables` must be set"
3201                );
3202            }
3203            if self.position_independent_executables {
3204                check!(
3205                    self.executables,
3206                    "if `position_independent_executables` is set then `executables` must be set"
3207                );
3208            }
3209        }
3210
3211        // Check crt static stuff
3212        if self.crt_static_default || self.crt_static_allows_dylibs {
3213            check!(
3214                self.crt_static_respected,
3215                "static CRT can be enabled but `crt_static_respected` is not set"
3216            );
3217        }
3218
3219        // Check that RISC-V targets always specify which ABI they use,
3220        // and that ARM targets specify their float ABI.
3221        match &*self.arch {
3222            "riscv32" => {
3223                check_matches!(
3224                    &*self.llvm_abiname,
3225                    "ilp32" | "ilp32f" | "ilp32d" | "ilp32e",
3226                    "invalid RISC-V ABI name: {}",
3227                    self.llvm_abiname,
3228                );
3229            }
3230            "riscv64" => {
3231                // Note that the `lp64e` is still unstable as it's not (yet) part of the ELF psABI.
3232                check_matches!(
3233                    &*self.llvm_abiname,
3234                    "lp64" | "lp64f" | "lp64d" | "lp64e",
3235                    "invalid RISC-V ABI name: {}",
3236                    self.llvm_abiname,
3237                );
3238            }
3239            "arm" => {
3240                check!(
3241                    self.llvm_floatabi.is_some(),
3242                    "ARM targets must set `llvm-floatabi` to `hard` or `soft`",
3243                )
3244            }
3245            _ => {}
3246        }
3247
3248        // Check consistency of Rust ABI declaration.
3249        if let Some(rust_abi) = self.rustc_abi {
3250            match rust_abi {
3251                RustcAbi::X86Sse2 => check_matches!(
3252                    &*self.arch,
3253                    "x86",
3254                    "`x86-sse2` ABI is only valid for x86-32 targets"
3255                ),
3256                RustcAbi::X86Softfloat => check_matches!(
3257                    &*self.arch,
3258                    "x86" | "x86_64",
3259                    "`x86-softfloat` ABI is only valid for x86 targets"
3260                ),
3261            }
3262        }
3263
3264        // Check that the given target-features string makes some basic sense.
3265        if !self.features.is_empty() {
3266            let mut features_enabled = FxHashSet::default();
3267            let mut features_disabled = FxHashSet::default();
3268            for feat in self.features.split(',') {
3269                if let Some(feat) = feat.strip_prefix("+") {
3270                    features_enabled.insert(feat);
3271                    if features_disabled.contains(feat) {
3272                        return Err(format!(
3273                            "target feature `{feat}` is both enabled and disabled"
3274                        ));
3275                    }
3276                } else if let Some(feat) = feat.strip_prefix("-") {
3277                    features_disabled.insert(feat);
3278                    if features_enabled.contains(feat) {
3279                        return Err(format!(
3280                            "target feature `{feat}` is both enabled and disabled"
3281                        ));
3282                    }
3283                } else {
3284                    return Err(format!(
3285                        "target feature `{feat}` is invalid, must start with `+` or `-`"
3286                    ));
3287                }
3288            }
3289            // Check that we don't mis-set any of the ABI-relevant features.
3290            let abi_feature_constraints = self.abi_required_features();
3291            for feat in abi_feature_constraints.required {
3292                // The feature might be enabled by default so we can't *require* it to show up.
3293                // But it must not be *disabled*.
3294                if features_disabled.contains(feat) {
3295                    return Err(format!(
3296                        "target feature `{feat}` is required by the ABI but gets disabled in target spec"
3297                    ));
3298                }
3299            }
3300            for feat in abi_feature_constraints.incompatible {
3301                // The feature might be disabled by default so we can't *require* it to show up.
3302                // But it must not be *enabled*.
3303                if features_enabled.contains(feat) {
3304                    return Err(format!(
3305                        "target feature `{feat}` is incompatible with the ABI but gets enabled in target spec"
3306                    ));
3307                }
3308            }
3309        }
3310
3311        Ok(())
3312    }
3313
3314    /// Test target self-consistency and JSON encoding/decoding roundtrip.
3315    #[cfg(test)]
3316    fn test_target(mut self) {
3317        let recycled_target = Target::from_json(self.to_json()).map(|(j, _)| j);
3318        self.update_to_cli();
3319        self.check_consistency(TargetKind::Builtin).unwrap();
3320        assert_eq!(recycled_target, Ok(self));
3321    }
3322
3323    // Add your target to the whitelist if it has `std` library
3324    // and you certainly want "unknown" for the OS name.
3325    fn can_use_os_unknown(&self) -> bool {
3326        self.llvm_target == "wasm32-unknown-unknown"
3327            || self.llvm_target == "wasm64-unknown-unknown"
3328            || (self.env == "sgx" && self.vendor == "fortanix")
3329    }
3330
3331    /// Load a built-in target
3332    pub fn expect_builtin(target_tuple: &TargetTuple) -> Target {
3333        match *target_tuple {
3334            TargetTuple::TargetTuple(ref target_tuple) => {
3335                load_builtin(target_tuple).expect("built-in target")
3336            }
3337            TargetTuple::TargetJson { .. } => {
3338                panic!("built-in targets doesn't support target-paths")
3339            }
3340        }
3341    }
3342
3343    /// Load all built-in targets
3344    pub fn builtins() -> impl Iterator<Item = Target> {
3345        load_all_builtins()
3346    }
3347
3348    /// Search for a JSON file specifying the given target tuple.
3349    ///
3350    /// If none is found in `$RUST_TARGET_PATH`, look for a file called `target.json` inside the
3351    /// sysroot under the target-tuple's `rustlib` directory. Note that it could also just be a
3352    /// bare filename already, so also check for that. If one of the hardcoded targets we know
3353    /// about, just return it directly.
3354    ///
3355    /// The error string could come from any of the APIs called, including filesystem access and
3356    /// JSON decoding.
3357    pub fn search(
3358        target_tuple: &TargetTuple,
3359        sysroot: &Path,
3360    ) -> Result<(Target, TargetWarnings), String> {
3361        use std::{env, fs};
3362
3363        fn load_file(path: &Path) -> Result<(Target, TargetWarnings), String> {
3364            let contents = fs::read_to_string(path).map_err(|e| e.to_string())?;
3365            let obj = serde_json::from_str(&contents).map_err(|e| e.to_string())?;
3366            Target::from_json(obj)
3367        }
3368
3369        match *target_tuple {
3370            TargetTuple::TargetTuple(ref target_tuple) => {
3371                // check if tuple is in list of built-in targets
3372                if let Some(t) = load_builtin(target_tuple) {
3373                    return Ok((t, TargetWarnings::empty()));
3374                }
3375
3376                // search for a file named `target_tuple`.json in RUST_TARGET_PATH
3377                let path = {
3378                    let mut target = target_tuple.to_string();
3379                    target.push_str(".json");
3380                    PathBuf::from(target)
3381                };
3382
3383                let target_path = env::var_os("RUST_TARGET_PATH").unwrap_or_default();
3384
3385                for dir in env::split_paths(&target_path) {
3386                    let p = dir.join(&path);
3387                    if p.is_file() {
3388                        return load_file(&p);
3389                    }
3390                }
3391
3392                // Additionally look in the sysroot under `lib/rustlib/<tuple>/target.json`
3393                // as a fallback.
3394                let rustlib_path = crate::relative_target_rustlib_path(sysroot, target_tuple);
3395                let p = PathBuf::from_iter([
3396                    Path::new(sysroot),
3397                    Path::new(&rustlib_path),
3398                    Path::new("target.json"),
3399                ]);
3400                if p.is_file() {
3401                    return load_file(&p);
3402                }
3403
3404                // Leave in a specialized error message for the removed target.
3405                // FIXME: If you see this and it's been a few months after this has been released,
3406                // you can probably remove it.
3407                if target_tuple == "i586-pc-windows-msvc" {
3408                    Err("the `i586-pc-windows-msvc` target has been removed. Use the `i686-pc-windows-msvc` target instead.\n\
3409                        Windows 10 (the minimum required OS version) requires a CPU baseline of at least i686 so you can safely switch".into())
3410                } else {
3411                    Err(format!("could not find specification for target {target_tuple:?}"))
3412                }
3413            }
3414            TargetTuple::TargetJson { ref contents, .. } => {
3415                let obj = serde_json::from_str(contents).map_err(|e| e.to_string())?;
3416                Target::from_json(obj)
3417            }
3418        }
3419    }
3420
3421    /// Return the target's small data threshold support, converting
3422    /// `DefaultForArch` into a concrete value.
3423    pub fn small_data_threshold_support(&self) -> SmallDataThresholdSupport {
3424        match &self.options.small_data_threshold_support {
3425            // Avoid having to duplicate the small data support in every
3426            // target file by supporting a default value for each
3427            // architecture.
3428            SmallDataThresholdSupport::DefaultForArch => match self.arch.as_ref() {
3429                "mips" | "mips64" | "mips32r6" => {
3430                    SmallDataThresholdSupport::LlvmArg("mips-ssection-threshold".into())
3431                }
3432                "hexagon" => {
3433                    SmallDataThresholdSupport::LlvmArg("hexagon-small-data-threshold".into())
3434                }
3435                "m68k" => SmallDataThresholdSupport::LlvmArg("m68k-ssection-threshold".into()),
3436                "riscv32" | "riscv64" => {
3437                    SmallDataThresholdSupport::LlvmModuleFlag("SmallDataLimit".into())
3438                }
3439                _ => SmallDataThresholdSupport::None,
3440            },
3441            s => s.clone(),
3442        }
3443    }
3444
3445    pub fn object_architecture(
3446        &self,
3447        unstable_target_features: &FxIndexSet<Symbol>,
3448    ) -> Option<(object::Architecture, Option<object::SubArchitecture>)> {
3449        use object::Architecture;
3450        Some(match self.arch.as_ref() {
3451            "arm" => (Architecture::Arm, None),
3452            "aarch64" => (
3453                if self.pointer_width == 32 {
3454                    Architecture::Aarch64_Ilp32
3455                } else {
3456                    Architecture::Aarch64
3457                },
3458                None,
3459            ),
3460            "x86" => (Architecture::I386, None),
3461            "s390x" => (Architecture::S390x, None),
3462            "mips" | "mips32r6" => (Architecture::Mips, None),
3463            "mips64" | "mips64r6" => (
3464                // While there are currently no builtin targets
3465                // using the N32 ABI, it is possible to specify
3466                // it using a custom target specification. N32
3467                // is an ILP32 ABI like the Aarch64_Ilp32
3468                // and X86_64_X32 cases above and below this one.
3469                if self.options.llvm_abiname.as_ref() == "n32" {
3470                    Architecture::Mips64_N32
3471                } else {
3472                    Architecture::Mips64
3473                },
3474                None,
3475            ),
3476            "x86_64" => (
3477                if self.pointer_width == 32 {
3478                    Architecture::X86_64_X32
3479                } else {
3480                    Architecture::X86_64
3481                },
3482                None,
3483            ),
3484            "powerpc" => (Architecture::PowerPc, None),
3485            "powerpc64" => (Architecture::PowerPc64, None),
3486            "riscv32" => (Architecture::Riscv32, None),
3487            "riscv64" => (Architecture::Riscv64, None),
3488            "sparc" => {
3489                if unstable_target_features.contains(&sym::v8plus) {
3490                    // Target uses V8+, aka EM_SPARC32PLUS, aka 64-bit V9 but in 32-bit mode
3491                    (Architecture::Sparc32Plus, None)
3492                } else {
3493                    // Target uses V7 or V8, aka EM_SPARC
3494                    (Architecture::Sparc, None)
3495                }
3496            }
3497            "sparc64" => (Architecture::Sparc64, None),
3498            "avr" => (Architecture::Avr, None),
3499            "msp430" => (Architecture::Msp430, None),
3500            "hexagon" => (Architecture::Hexagon, None),
3501            "bpf" => (Architecture::Bpf, None),
3502            "loongarch32" => (Architecture::LoongArch32, None),
3503            "loongarch64" => (Architecture::LoongArch64, None),
3504            "csky" => (Architecture::Csky, None),
3505            "arm64ec" => (Architecture::Aarch64, Some(object::SubArchitecture::Arm64EC)),
3506            // Unsupported architecture.
3507            _ => return None,
3508        })
3509    }
3510
3511    /// Returns whether this target is known to have unreliable alignment:
3512    /// native C code for the target fails to align some data to the degree
3513    /// required by the C standard. We can't *really* do anything about that
3514    /// since unsafe Rust code may assume alignment any time, but we can at least
3515    /// inhibit some optimizations, and we suppress the alignment checks that
3516    /// would detect this unsoundness.
3517    ///
3518    /// Every target that returns less than `Align::MAX` here is still has a soundness bug.
3519    pub fn max_reliable_alignment(&self) -> Align {
3520        // FIXME(#112480) MSVC on x86-32 is unsound and fails to properly align many types with
3521        // more-than-4-byte-alignment on the stack. This makes alignments larger than 4 generally
3522        // unreliable on 32bit Windows.
3523        if self.is_like_windows && self.arch == "x86" {
3524            Align::from_bytes(4).unwrap()
3525        } else {
3526            Align::MAX
3527        }
3528    }
3529}
3530
3531/// Either a target tuple string or a path to a JSON file.
3532#[derive(Clone, Debug)]
3533pub enum TargetTuple {
3534    TargetTuple(String),
3535    TargetJson {
3536        /// Warning: This field may only be used by rustdoc. Using it anywhere else will lead to
3537        /// inconsistencies as it is discarded during serialization.
3538        path_for_rustdoc: PathBuf,
3539        tuple: String,
3540        contents: String,
3541    },
3542}
3543
3544// Use a manual implementation to ignore the path field
3545impl PartialEq for TargetTuple {
3546    fn eq(&self, other: &Self) -> bool {
3547        match (self, other) {
3548            (Self::TargetTuple(l0), Self::TargetTuple(r0)) => l0 == r0,
3549            (
3550                Self::TargetJson { path_for_rustdoc: _, tuple: l_tuple, contents: l_contents },
3551                Self::TargetJson { path_for_rustdoc: _, tuple: r_tuple, contents: r_contents },
3552            ) => l_tuple == r_tuple && l_contents == r_contents,
3553            _ => false,
3554        }
3555    }
3556}
3557
3558// Use a manual implementation to ignore the path field
3559impl Hash for TargetTuple {
3560    fn hash<H: Hasher>(&self, state: &mut H) -> () {
3561        match self {
3562            TargetTuple::TargetTuple(tuple) => {
3563                0u8.hash(state);
3564                tuple.hash(state)
3565            }
3566            TargetTuple::TargetJson { path_for_rustdoc: _, tuple, contents } => {
3567                1u8.hash(state);
3568                tuple.hash(state);
3569                contents.hash(state)
3570            }
3571        }
3572    }
3573}
3574
3575// Use a manual implementation to prevent encoding the target json file path in the crate metadata
3576impl<S: Encoder> Encodable<S> for TargetTuple {
3577    fn encode(&self, s: &mut S) {
3578        match self {
3579            TargetTuple::TargetTuple(tuple) => {
3580                s.emit_u8(0);
3581                s.emit_str(tuple);
3582            }
3583            TargetTuple::TargetJson { path_for_rustdoc: _, tuple, contents } => {
3584                s.emit_u8(1);
3585                s.emit_str(tuple);
3586                s.emit_str(contents);
3587            }
3588        }
3589    }
3590}
3591
3592impl<D: Decoder> Decodable<D> for TargetTuple {
3593    fn decode(d: &mut D) -> Self {
3594        match d.read_u8() {
3595            0 => TargetTuple::TargetTuple(d.read_str().to_owned()),
3596            1 => TargetTuple::TargetJson {
3597                path_for_rustdoc: PathBuf::new(),
3598                tuple: d.read_str().to_owned(),
3599                contents: d.read_str().to_owned(),
3600            },
3601            _ => {
3602                panic!("invalid enum variant tag while decoding `TargetTuple`, expected 0..2");
3603            }
3604        }
3605    }
3606}
3607
3608impl TargetTuple {
3609    /// Creates a target tuple from the passed target tuple string.
3610    pub fn from_tuple(tuple: &str) -> Self {
3611        TargetTuple::TargetTuple(tuple.into())
3612    }
3613
3614    /// Creates a target tuple from the passed target path.
3615    pub fn from_path(path: &Path) -> Result<Self, io::Error> {
3616        let canonicalized_path = try_canonicalize(path)?;
3617        let contents = std::fs::read_to_string(&canonicalized_path).map_err(|err| {
3618            io::Error::new(
3619                io::ErrorKind::InvalidInput,
3620                format!("target path {canonicalized_path:?} is not a valid file: {err}"),
3621            )
3622        })?;
3623        let tuple = canonicalized_path
3624            .file_stem()
3625            .expect("target path must not be empty")
3626            .to_str()
3627            .expect("target path must be valid unicode")
3628            .to_owned();
3629        Ok(TargetTuple::TargetJson { path_for_rustdoc: canonicalized_path, tuple, contents })
3630    }
3631
3632    /// Returns a string tuple for this target.
3633    ///
3634    /// If this target is a path, the file name (without extension) is returned.
3635    pub fn tuple(&self) -> &str {
3636        match *self {
3637            TargetTuple::TargetTuple(ref tuple) | TargetTuple::TargetJson { ref tuple, .. } => {
3638                tuple
3639            }
3640        }
3641    }
3642
3643    /// Returns an extended string tuple for this target.
3644    ///
3645    /// If this target is a path, a hash of the path is appended to the tuple returned
3646    /// by `tuple()`.
3647    pub fn debug_tuple(&self) -> String {
3648        use std::hash::DefaultHasher;
3649
3650        match self {
3651            TargetTuple::TargetTuple(tuple) => tuple.to_owned(),
3652            TargetTuple::TargetJson { path_for_rustdoc: _, tuple, contents: content } => {
3653                let mut hasher = DefaultHasher::new();
3654                content.hash(&mut hasher);
3655                let hash = hasher.finish();
3656                format!("{tuple}-{hash}")
3657            }
3658        }
3659    }
3660}
3661
3662impl fmt::Display for TargetTuple {
3663    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3664        write!(f, "{}", self.debug_tuple())
3665    }
3666}