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