1 //! [Flexible target specification.](https://github.com/rust-lang/rfcs/pull/131)
3 //! Rust targets a wide variety of usecases, and in the interest of flexibility,
4 //! allows new target triples 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.
9 //! # Using custom targets
11 //! A target triple, as passed via `rustc --target=TRIPLE`, 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 `TRIPLE` is not one of the built-in
15 //! targets, rustc will check if a file named `TRIPLE` 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 `TRIPLE.json`. The first one found will
19 //! be loaded. If no file is found in any of those directories, a fatal error
22 //! Projects defining their own targets should use
23 //! `--target=path/to/my-awesome-platform.json` instead of adding to
24 //! `RUST_TARGET_PATH`.
26 //! # Defining a new target
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.
37 use crate::abi::call::Conv;
38 use crate::abi::{Endian, Integer, Size, TargetDataLayout, TargetDataLayoutErrors};
39 use crate::json::{Json, ToJson};
40 use crate::spec::abi::{lookup as lookup_abi, Abi};
41 use crate::spec::crt_objects::{CrtObjects, LinkSelfContainedDefault};
42 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
43 use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
44 use rustc_span::symbol::{sym, Symbol};
45 use serde_json::Value;
47 use std::collections::BTreeMap;
48 use std::hash::{Hash, Hasher};
49 use std::ops::{Deref, DerefMut};
50 use std::path::{Path, PathBuf};
51 use std::str::FromStr;
54 use rustc_macros::HashStable_Generic;
74 mod linux_uclibc_base;
87 mod windows_gnullvm_base;
88 mod windows_msvc_base;
89 mod windows_uwp_gnu_base;
90 mod windows_uwp_msvc_base;
92 /// Linker is called through a C/C++ compiler.
93 #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
100 #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
106 /// All linkers have some kinds of command line interfaces and rustc needs to know which commands
107 /// to use with each of them. So we cluster all such interfaces into a (somewhat arbitrary) number
108 /// of classes that we call "linker flavors".
110 /// Technically, it's not even necessary, we can nearly always infer the flavor from linker name
111 /// and target properties like `is_like_windows`/`is_like_osx`/etc. However, the PRs originally
112 /// introducing `-Clinker-flavor` (#40018 and friends) were aiming to reduce this kind of inference
113 /// and provide something certain and explicitly specified instead, and that design goal is still
116 /// The second goal is to keep the number of flavors to the minimum if possible.
117 /// LLD somewhat forces our hand here because that linker is self-sufficient only if its executable
118 /// (`argv[0]`) is named in specific way, otherwise it doesn't work and requires a
119 /// `-flavor LLD_FLAVOR` argument to choose which logic to use. Our shipped `rust-lld` in
120 /// particular is not named in such specific way, so it needs the flavor option, so we make our
121 /// linker flavors sufficiently fine-grained to satisfy LLD without inferring its flavor from other
122 /// target properties, in accordance with the first design goal.
124 /// The first component of the flavor is tightly coupled with the compilation target,
125 /// while the `Cc` and `Lld` flags can vary withing the same target.
126 #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
127 pub enum LinkerFlavor {
128 /// Unix-like linker with GNU extensions (both naked and compiler-wrapped forms).
129 /// Besides similar "default" Linux/BSD linkers this also includes Windows/GNU linker,
130 /// which is somewhat different because it doesn't produce ELFs.
132 /// Unix-like linker for Apple targets (both naked and compiler-wrapped forms).
133 /// Extracted from the "umbrella" `Unix` flavor due to its corresponding LLD flavor.
135 /// Unix-like linker for Wasm targets (both naked and compiler-wrapped forms).
136 /// Extracted from the "umbrella" `Unix` flavor due to its corresponding LLD flavor.
137 /// Non-LLD version does not exist, so the lld flag is currently hardcoded here.
139 /// Basic Unix-like linker for "any other Unix" targets (Solaris/illumos, L4Re, MSP430, etc),
140 /// possibly with non-GNU extensions (both naked and compiler-wrapped forms).
141 /// LLD doesn't support any of these.
143 /// MSVC-style linker for Windows and UEFI, LLD supports it.
145 /// Emscripten Compiler Frontend, a wrapper around `WasmLld(Cc::Yes)` that has a different
146 /// interface and produces some additional JavaScript output.
148 // Below: other linker-like tools with unique interfaces for exotic targets.
149 /// Linker tool for BPF.
151 /// Linker tool for Nvidia PTX.
155 /// Linker flavors available externally through command line (`-Clinker-flavor`)
156 /// or json target specifications.
157 /// FIXME: This set has accumulated historically, bring it more in line with the internal
158 /// linker flavors (`LinkerFlavor`).
159 #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
160 pub enum LinkerFlavorCli {
170 #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
179 pub fn as_str(&self) -> &'static str {
181 LldFlavor::Wasm => "wasm",
182 LldFlavor::Ld64 => "darwin",
183 LldFlavor::Ld => "gnu",
184 LldFlavor::Link => "link",
188 fn from_str(s: &str) -> Option<Self> {
190 "darwin" => LldFlavor::Ld64,
191 "gnu" => LldFlavor::Ld,
192 "link" => LldFlavor::Link,
193 "wasm" => LldFlavor::Wasm,
199 impl ToJson for LldFlavor {
200 fn to_json(&self) -> Json {
201 self.as_str().to_json()
206 pub fn from_cli(cli: LinkerFlavorCli, target: &TargetOptions) -> LinkerFlavor {
207 Self::from_cli_impl(cli, target.linker_flavor.lld_flavor(), target.linker_flavor.is_gnu())
210 /// The passed CLI flavor is preferred over other args coming from the default target spec,
211 /// so this function can produce a flavor that is incompatible with the current target.
212 /// FIXME: Produce errors when `-Clinker-flavor` is set to something incompatible
213 /// with the current target.
214 fn from_cli_impl(cli: LinkerFlavorCli, lld_flavor: LldFlavor, is_gnu: bool) -> LinkerFlavor {
216 LinkerFlavorCli::Gcc => match lld_flavor {
217 LldFlavor::Ld if is_gnu => LinkerFlavor::Gnu(Cc::Yes, Lld::No),
218 LldFlavor::Ld64 => LinkerFlavor::Darwin(Cc::Yes, Lld::No),
219 LldFlavor::Wasm => LinkerFlavor::WasmLld(Cc::Yes),
220 LldFlavor::Ld | LldFlavor::Link => LinkerFlavor::Unix(Cc::Yes),
222 LinkerFlavorCli::Ld => match lld_flavor {
223 LldFlavor::Ld if is_gnu => LinkerFlavor::Gnu(Cc::No, Lld::No),
224 LldFlavor::Ld64 => LinkerFlavor::Darwin(Cc::No, Lld::No),
225 LldFlavor::Ld | LldFlavor::Wasm | LldFlavor::Link => LinkerFlavor::Unix(Cc::No),
227 LinkerFlavorCli::Lld(LldFlavor::Ld) => LinkerFlavor::Gnu(Cc::No, Lld::Yes),
228 LinkerFlavorCli::Lld(LldFlavor::Ld64) => LinkerFlavor::Darwin(Cc::No, Lld::Yes),
229 LinkerFlavorCli::Lld(LldFlavor::Wasm) => LinkerFlavor::WasmLld(Cc::No),
230 LinkerFlavorCli::Lld(LldFlavor::Link) => LinkerFlavor::Msvc(Lld::Yes),
231 LinkerFlavorCli::Msvc => LinkerFlavor::Msvc(Lld::No),
232 LinkerFlavorCli::Em => LinkerFlavor::EmCc,
233 LinkerFlavorCli::BpfLinker => LinkerFlavor::Bpf,
234 LinkerFlavorCli::PtxLinker => LinkerFlavor::Ptx,
238 fn to_cli(self) -> LinkerFlavorCli {
240 LinkerFlavor::Gnu(Cc::Yes, _)
241 | LinkerFlavor::Darwin(Cc::Yes, _)
242 | LinkerFlavor::WasmLld(Cc::Yes)
243 | LinkerFlavor::Unix(Cc::Yes) => LinkerFlavorCli::Gcc,
244 LinkerFlavor::Gnu(_, Lld::Yes) => LinkerFlavorCli::Lld(LldFlavor::Ld),
245 LinkerFlavor::Darwin(_, Lld::Yes) => LinkerFlavorCli::Lld(LldFlavor::Ld64),
246 LinkerFlavor::WasmLld(..) => LinkerFlavorCli::Lld(LldFlavor::Wasm),
247 LinkerFlavor::Gnu(..) | LinkerFlavor::Darwin(..) | LinkerFlavor::Unix(..) => {
250 LinkerFlavor::Msvc(Lld::Yes) => LinkerFlavorCli::Lld(LldFlavor::Link),
251 LinkerFlavor::Msvc(..) => LinkerFlavorCli::Msvc,
252 LinkerFlavor::EmCc => LinkerFlavorCli::Em,
253 LinkerFlavor::Bpf => LinkerFlavorCli::BpfLinker,
254 LinkerFlavor::Ptx => LinkerFlavorCli::PtxLinker,
258 pub fn lld_flavor(self) -> LldFlavor {
260 LinkerFlavor::Gnu(..)
261 | LinkerFlavor::Unix(..)
264 | LinkerFlavor::Ptx => LldFlavor::Ld,
265 LinkerFlavor::Darwin(..) => LldFlavor::Ld64,
266 LinkerFlavor::WasmLld(..) => LldFlavor::Wasm,
267 LinkerFlavor::Msvc(..) => LldFlavor::Link,
271 pub fn is_gnu(self) -> bool {
272 matches!(self, LinkerFlavor::Gnu(..))
276 macro_rules! linker_flavor_cli_impls {
277 ($(($($flavor:tt)*) $string:literal)*) => (
278 impl LinkerFlavorCli {
279 pub const fn one_of() -> &'static str {
280 concat!("one of: ", $($string, " ",)*)
283 pub fn from_str(s: &str) -> Option<LinkerFlavorCli> {
285 $($string => $($flavor)*,)*
290 pub fn desc(&self) -> &str {
292 $($($flavor)* => $string,)*
299 linker_flavor_cli_impls! {
300 (LinkerFlavorCli::Gcc) "gcc"
301 (LinkerFlavorCli::Ld) "ld"
302 (LinkerFlavorCli::Lld(LldFlavor::Ld)) "ld.lld"
303 (LinkerFlavorCli::Lld(LldFlavor::Ld64)) "ld64.lld"
304 (LinkerFlavorCli::Lld(LldFlavor::Link)) "lld-link"
305 (LinkerFlavorCli::Lld(LldFlavor::Wasm)) "wasm-ld"
306 (LinkerFlavorCli::Msvc) "msvc"
307 (LinkerFlavorCli::Em) "em"
308 (LinkerFlavorCli::BpfLinker) "bpf-linker"
309 (LinkerFlavorCli::PtxLinker) "ptx-linker"
312 impl ToJson for LinkerFlavorCli {
313 fn to_json(&self) -> Json {
314 self.desc().to_json()
318 #[derive(Clone, Copy, Debug, PartialEq, Hash, Encodable, Decodable, HashStable_Generic)]
319 pub enum PanicStrategy {
325 pub fn desc(&self) -> &str {
327 PanicStrategy::Unwind => "unwind",
328 PanicStrategy::Abort => "abort",
332 pub const fn desc_symbol(&self) -> Symbol {
334 PanicStrategy::Unwind => sym::unwind,
335 PanicStrategy::Abort => sym::abort,
339 pub const fn all() -> [Symbol; 2] {
340 [Self::Abort.desc_symbol(), Self::Unwind.desc_symbol()]
344 impl ToJson for PanicStrategy {
345 fn to_json(&self) -> Json {
347 PanicStrategy::Abort => "abort".to_json(),
348 PanicStrategy::Unwind => "unwind".to_json(),
353 #[derive(Clone, Copy, Debug, PartialEq, Hash)]
354 pub enum RelroLevel {
362 pub fn desc(&self) -> &str {
364 RelroLevel::Full => "full",
365 RelroLevel::Partial => "partial",
366 RelroLevel::Off => "off",
367 RelroLevel::None => "none",
372 impl FromStr for RelroLevel {
375 fn from_str(s: &str) -> Result<RelroLevel, ()> {
377 "full" => Ok(RelroLevel::Full),
378 "partial" => Ok(RelroLevel::Partial),
379 "off" => Ok(RelroLevel::Off),
380 "none" => Ok(RelroLevel::None),
386 impl ToJson for RelroLevel {
387 fn to_json(&self) -> Json {
389 RelroLevel::Full => "full".to_json(),
390 RelroLevel::Partial => "partial".to_json(),
391 RelroLevel::Off => "off".to_json(),
392 RelroLevel::None => "None".to_json(),
397 #[derive(Clone, Copy, Debug, PartialEq, Hash)]
398 pub enum MergeFunctions {
404 impl MergeFunctions {
405 pub fn desc(&self) -> &str {
407 MergeFunctions::Disabled => "disabled",
408 MergeFunctions::Trampolines => "trampolines",
409 MergeFunctions::Aliases => "aliases",
414 impl FromStr for MergeFunctions {
417 fn from_str(s: &str) -> Result<MergeFunctions, ()> {
419 "disabled" => Ok(MergeFunctions::Disabled),
420 "trampolines" => Ok(MergeFunctions::Trampolines),
421 "aliases" => Ok(MergeFunctions::Aliases),
427 impl ToJson for MergeFunctions {
428 fn to_json(&self) -> Json {
430 MergeFunctions::Disabled => "disabled".to_json(),
431 MergeFunctions::Trampolines => "trampolines".to_json(),
432 MergeFunctions::Aliases => "aliases".to_json(),
437 #[derive(Clone, Copy, PartialEq, Hash, Debug)]
438 pub enum RelocModel {
448 impl FromStr for RelocModel {
451 fn from_str(s: &str) -> Result<RelocModel, ()> {
453 "static" => RelocModel::Static,
454 "pic" => RelocModel::Pic,
455 "pie" => RelocModel::Pie,
456 "dynamic-no-pic" => RelocModel::DynamicNoPic,
457 "ropi" => RelocModel::Ropi,
458 "rwpi" => RelocModel::Rwpi,
459 "ropi-rwpi" => RelocModel::RopiRwpi,
465 impl ToJson for RelocModel {
466 fn to_json(&self) -> Json {
468 RelocModel::Static => "static",
469 RelocModel::Pic => "pic",
470 RelocModel::Pie => "pie",
471 RelocModel::DynamicNoPic => "dynamic-no-pic",
472 RelocModel::Ropi => "ropi",
473 RelocModel::Rwpi => "rwpi",
474 RelocModel::RopiRwpi => "ropi-rwpi",
480 #[derive(Clone, Copy, PartialEq, Hash, Debug)]
489 impl FromStr for CodeModel {
492 fn from_str(s: &str) -> Result<CodeModel, ()> {
494 "tiny" => CodeModel::Tiny,
495 "small" => CodeModel::Small,
496 "kernel" => CodeModel::Kernel,
497 "medium" => CodeModel::Medium,
498 "large" => CodeModel::Large,
504 impl ToJson for CodeModel {
505 fn to_json(&self) -> Json {
507 CodeModel::Tiny => "tiny",
508 CodeModel::Small => "small",
509 CodeModel::Kernel => "kernel",
510 CodeModel::Medium => "medium",
511 CodeModel::Large => "large",
517 #[derive(Clone, Copy, PartialEq, Hash, Debug)]
525 impl FromStr for TlsModel {
528 fn from_str(s: &str) -> Result<TlsModel, ()> {
530 // Note the difference "general" vs "global" difference. The model name is "general",
531 // but the user-facing option name is "global" for consistency with other compilers.
532 "global-dynamic" => TlsModel::GeneralDynamic,
533 "local-dynamic" => TlsModel::LocalDynamic,
534 "initial-exec" => TlsModel::InitialExec,
535 "local-exec" => TlsModel::LocalExec,
541 impl ToJson for TlsModel {
542 fn to_json(&self) -> Json {
544 TlsModel::GeneralDynamic => "global-dynamic",
545 TlsModel::LocalDynamic => "local-dynamic",
546 TlsModel::InitialExec => "initial-exec",
547 TlsModel::LocalExec => "local-exec",
553 /// Everything is flattened to a single enum to make the json encoding/decoding less annoying.
554 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
555 pub enum LinkOutputKind {
556 /// Dynamically linked non position-independent executable.
558 /// Dynamically linked position-independent executable.
560 /// Statically linked non position-independent executable.
562 /// Statically linked position-independent executable.
564 /// Regular dynamic library ("dynamically linked").
566 /// Dynamic library with bundled libc ("statically linked").
568 /// WASI module with a lifetime past the _initialize entry point
572 impl LinkOutputKind {
573 fn as_str(&self) -> &'static str {
575 LinkOutputKind::DynamicNoPicExe => "dynamic-nopic-exe",
576 LinkOutputKind::DynamicPicExe => "dynamic-pic-exe",
577 LinkOutputKind::StaticNoPicExe => "static-nopic-exe",
578 LinkOutputKind::StaticPicExe => "static-pic-exe",
579 LinkOutputKind::DynamicDylib => "dynamic-dylib",
580 LinkOutputKind::StaticDylib => "static-dylib",
581 LinkOutputKind::WasiReactorExe => "wasi-reactor-exe",
585 pub(super) fn from_str(s: &str) -> Option<LinkOutputKind> {
587 "dynamic-nopic-exe" => LinkOutputKind::DynamicNoPicExe,
588 "dynamic-pic-exe" => LinkOutputKind::DynamicPicExe,
589 "static-nopic-exe" => LinkOutputKind::StaticNoPicExe,
590 "static-pic-exe" => LinkOutputKind::StaticPicExe,
591 "dynamic-dylib" => LinkOutputKind::DynamicDylib,
592 "static-dylib" => LinkOutputKind::StaticDylib,
593 "wasi-reactor-exe" => LinkOutputKind::WasiReactorExe,
599 impl fmt::Display for LinkOutputKind {
600 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
601 f.write_str(self.as_str())
605 pub type LinkArgs = BTreeMap<LinkerFlavor, Vec<StaticCow<str>>>;
606 pub type LinkArgsCli = BTreeMap<LinkerFlavorCli, Vec<StaticCow<str>>>;
608 /// Which kind of debuginfo does the target use?
610 /// Useful in determining whether a target supports Split DWARF (a target with
611 /// `DebuginfoKind::Dwarf` and supporting `SplitDebuginfo::Unpacked` for example).
612 #[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
613 pub enum DebuginfoKind {
614 /// DWARF debuginfo (such as that used on `x86_64_unknown_linux_gnu`).
617 /// DWARF debuginfo in dSYM files (such as on Apple platforms).
619 /// Program database files (such as on Windows).
624 fn as_str(&self) -> &'static str {
626 DebuginfoKind::Dwarf => "dwarf",
627 DebuginfoKind::DwarfDsym => "dwarf-dsym",
628 DebuginfoKind::Pdb => "pdb",
633 impl FromStr for DebuginfoKind {
636 fn from_str(s: &str) -> Result<Self, ()> {
638 "dwarf" => DebuginfoKind::Dwarf,
639 "dwarf-dsym" => DebuginfoKind::DwarfDsym,
640 "pdb" => DebuginfoKind::Pdb,
646 impl ToJson for DebuginfoKind {
647 fn to_json(&self) -> Json {
648 self.as_str().to_json()
652 impl fmt::Display for DebuginfoKind {
653 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
654 f.write_str(self.as_str())
658 #[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
659 pub enum SplitDebuginfo {
660 /// Split debug-information is disabled, meaning that on supported platforms
661 /// you can find all debug information in the executable itself. This is
662 /// only supported for ELF effectively.
664 /// * Windows - not supported
665 /// * macOS - don't run `dsymutil`
666 /// * ELF - `.debug_*` sections
670 /// Split debug-information can be found in a "packed" location separate
671 /// from the final artifact. This is supported on all platforms.
673 /// * Windows - `*.pdb`
674 /// * macOS - `*.dSYM` (run `dsymutil`)
675 /// * ELF - `*.dwp` (run `thorin`)
678 /// Split debug-information can be found in individual object files on the
679 /// filesystem. The main executable may point to the object files.
681 /// * Windows - not supported
682 /// * macOS - supported, scattered object files
683 /// * ELF - supported, scattered `*.dwo` or `*.o` files (see `SplitDwarfKind`)
687 impl SplitDebuginfo {
688 fn as_str(&self) -> &'static str {
690 SplitDebuginfo::Off => "off",
691 SplitDebuginfo::Packed => "packed",
692 SplitDebuginfo::Unpacked => "unpacked",
697 impl FromStr for SplitDebuginfo {
700 fn from_str(s: &str) -> Result<Self, ()> {
702 "off" => SplitDebuginfo::Off,
703 "unpacked" => SplitDebuginfo::Unpacked,
704 "packed" => SplitDebuginfo::Packed,
710 impl ToJson for SplitDebuginfo {
711 fn to_json(&self) -> Json {
712 self.as_str().to_json()
716 impl fmt::Display for SplitDebuginfo {
717 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
718 f.write_str(self.as_str())
722 #[derive(Clone, Debug, PartialEq, Eq)]
723 pub enum StackProbeType {
724 /// Don't emit any stack probes.
726 /// It is harmless to use this option even on targets that do not have backend support for
727 /// stack probes as the failure mode is the same as if no stack-probe option was specified in
730 /// Call `__rust_probestack` whenever stack needs to be probed.
732 /// Use inline option for LLVM versions later than specified in `min_llvm_version_for_inline`
733 /// and call `__rust_probestack` otherwise.
734 InlineOrCall { min_llvm_version_for_inline: (u32, u32, u32) },
737 impl StackProbeType {
738 // LLVM X86 targets (ix86 and x86_64) can use inline-asm stack probes starting with LLVM 16.
739 // Notable past issues were rust#83139 (fixed in 14) and rust#84667 (fixed in 16).
740 const X86: Self = Self::InlineOrCall { min_llvm_version_for_inline: (16, 0, 0) };
742 fn from_json(json: &Json) -> Result<Self, String> {
743 let object = json.as_object().ok_or_else(|| "expected a JSON object")?;
746 .and_then(|o| o.as_str())
747 .ok_or_else(|| "expected `kind` to be a string")?;
749 "none" => Ok(StackProbeType::None),
750 "inline" => Ok(StackProbeType::Inline),
751 "call" => Ok(StackProbeType::Call),
752 "inline-or-call" => {
753 let min_version = object
754 .get("min-llvm-version-for-inline")
755 .and_then(|o| o.as_array())
756 .ok_or_else(|| "expected `min-llvm-version-for-inline` to be an array")?;
757 let mut iter = min_version.into_iter().map(|v| {
758 let int = v.as_u64().ok_or_else(
759 || "expected `min-llvm-version-for-inline` values to be integers",
762 .map_err(|_| "`min-llvm-version-for-inline` values don't convert to u32")
764 let min_llvm_version_for_inline = (
765 iter.next().unwrap_or(Ok(11))?,
766 iter.next().unwrap_or(Ok(0))?,
767 iter.next().unwrap_or(Ok(0))?,
769 Ok(StackProbeType::InlineOrCall { min_llvm_version_for_inline })
771 _ => Err(String::from(
772 "`kind` expected to be one of `none`, `inline`, `call` or `inline-or-call`",
778 impl ToJson for StackProbeType {
779 fn to_json(&self) -> Json {
780 Json::Object(match self {
781 StackProbeType::None => {
782 [(String::from("kind"), "none".to_json())].into_iter().collect()
784 StackProbeType::Inline => {
785 [(String::from("kind"), "inline".to_json())].into_iter().collect()
787 StackProbeType::Call => {
788 [(String::from("kind"), "call".to_json())].into_iter().collect()
790 StackProbeType::InlineOrCall { min_llvm_version_for_inline: (maj, min, patch) } => [
791 (String::from("kind"), "inline-or-call".to_json()),
793 String::from("min-llvm-version-for-inline"),
794 Json::Array(vec![maj.to_json(), min.to_json(), patch.to_json()]),
803 bitflags::bitflags! {
804 #[derive(Default, Encodable, Decodable)]
805 pub struct SanitizerSet: u16 {
806 const ADDRESS = 1 << 0;
808 const MEMORY = 1 << 2;
809 const THREAD = 1 << 3;
810 const HWADDRESS = 1 << 4;
812 const MEMTAG = 1 << 6;
813 const SHADOWCALLSTACK = 1 << 7;
819 /// Return sanitizer's name
821 /// Returns none if the flags is a set of sanitizers numbering not exactly one.
822 pub fn as_str(self) -> Option<&'static str> {
824 SanitizerSet::ADDRESS => "address",
825 SanitizerSet::CFI => "cfi",
826 SanitizerSet::KCFI => "kcfi",
827 SanitizerSet::LEAK => "leak",
828 SanitizerSet::MEMORY => "memory",
829 SanitizerSet::MEMTAG => "memtag",
830 SanitizerSet::SHADOWCALLSTACK => "shadow-call-stack",
831 SanitizerSet::THREAD => "thread",
832 SanitizerSet::HWADDRESS => "hwaddress",
838 /// Formats a sanitizer set as a comma separated list of sanitizers' names.
839 impl fmt::Display for SanitizerSet {
840 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
841 let mut first = true;
843 let name = s.as_str().unwrap_or_else(|| panic!("unrecognized sanitizer {s:?}"));
854 impl IntoIterator for SanitizerSet {
855 type Item = SanitizerSet;
856 type IntoIter = std::vec::IntoIter<SanitizerSet>;
858 fn into_iter(self) -> Self::IntoIter {
860 SanitizerSet::ADDRESS,
864 SanitizerSet::MEMORY,
865 SanitizerSet::MEMTAG,
866 SanitizerSet::SHADOWCALLSTACK,
867 SanitizerSet::THREAD,
868 SanitizerSet::HWADDRESS,
872 .filter(|&s| self.contains(s))
878 impl<CTX> HashStable<CTX> for SanitizerSet {
879 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
880 self.bits().hash_stable(ctx, hasher);
884 impl ToJson for SanitizerSet {
885 fn to_json(&self) -> Json {
887 .map(|v| Some(v.as_str()?.to_json()))
888 .collect::<Option<Vec<_>>>()
894 #[derive(Clone, Copy, PartialEq, Hash, Debug)]
895 pub enum FramePointer {
896 /// Forces the machine code generator to always preserve the frame pointers.
898 /// Forces the machine code generator to preserve the frame pointers except for the leaf
899 /// functions (i.e. those that don't call other functions).
901 /// Allows the machine code generator to omit the frame pointers.
903 /// This option does not guarantee that the frame pointers will be omitted.
907 impl FromStr for FramePointer {
909 fn from_str(s: &str) -> Result<Self, ()> {
911 "always" => Self::Always,
912 "non-leaf" => Self::NonLeaf,
913 "may-omit" => Self::MayOmit,
919 impl ToJson for FramePointer {
920 fn to_json(&self) -> Json {
922 Self::Always => "always",
923 Self::NonLeaf => "non-leaf",
924 Self::MayOmit => "may-omit",
930 /// Controls use of stack canaries.
931 #[derive(Clone, Copy, Debug, PartialEq, Hash, Eq)]
932 pub enum StackProtector {
933 /// Disable stack canary generation.
936 /// On LLVM, mark all generated LLVM functions with the `ssp` attribute (see
937 /// llvm/docs/LangRef.rst). This triggers stack canary generation in
938 /// functions which contain an array of a byte-sized type with more than
942 /// On LLVM, mark all generated LLVM functions with the `sspstrong`
943 /// attribute (see llvm/docs/LangRef.rst). This triggers stack canary
944 /// generation in functions which either contain an array, or which take
945 /// the address of a local variable.
948 /// Generate stack canaries in all functions.
952 impl StackProtector {
953 fn as_str(&self) -> &'static str {
955 StackProtector::None => "none",
956 StackProtector::Basic => "basic",
957 StackProtector::Strong => "strong",
958 StackProtector::All => "all",
963 impl FromStr for StackProtector {
966 fn from_str(s: &str) -> Result<StackProtector, ()> {
968 "none" => StackProtector::None,
969 "basic" => StackProtector::Basic,
970 "strong" => StackProtector::Strong,
971 "all" => StackProtector::All,
977 impl fmt::Display for StackProtector {
978 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
979 f.write_str(self.as_str())
983 macro_rules! supported_targets {
984 ( $(($triple:literal, $module:ident ),)+ ) => {
987 /// List of supported targets
988 pub const TARGETS: &[&str] = &[$($triple),+];
990 fn load_builtin(target: &str) -> Option<Target> {
991 let mut t = match target {
992 $( $triple => $module::target(), )+
996 debug!("got builtin target: {:?}", t);
1004 // Cannot put this into a separate file without duplication, make an exception.
1006 #[test] // `#[test]`
1008 tests_impl::test_target(super::$module::target());
1015 supported_targets! {
1016 ("x86_64-unknown-linux-gnu", x86_64_unknown_linux_gnu),
1017 ("x86_64-unknown-linux-gnux32", x86_64_unknown_linux_gnux32),
1018 ("i686-unknown-linux-gnu", i686_unknown_linux_gnu),
1019 ("i586-unknown-linux-gnu", i586_unknown_linux_gnu),
1020 ("m68k-unknown-linux-gnu", m68k_unknown_linux_gnu),
1021 ("mips-unknown-linux-gnu", mips_unknown_linux_gnu),
1022 ("mips64-unknown-linux-gnuabi64", mips64_unknown_linux_gnuabi64),
1023 ("mips64el-unknown-linux-gnuabi64", mips64el_unknown_linux_gnuabi64),
1024 ("mipsisa32r6-unknown-linux-gnu", mipsisa32r6_unknown_linux_gnu),
1025 ("mipsisa32r6el-unknown-linux-gnu", mipsisa32r6el_unknown_linux_gnu),
1026 ("mipsisa64r6-unknown-linux-gnuabi64", mipsisa64r6_unknown_linux_gnuabi64),
1027 ("mipsisa64r6el-unknown-linux-gnuabi64", mipsisa64r6el_unknown_linux_gnuabi64),
1028 ("mipsel-unknown-linux-gnu", mipsel_unknown_linux_gnu),
1029 ("powerpc-unknown-linux-gnu", powerpc_unknown_linux_gnu),
1030 ("powerpc-unknown-linux-gnuspe", powerpc_unknown_linux_gnuspe),
1031 ("powerpc-unknown-linux-musl", powerpc_unknown_linux_musl),
1032 ("powerpc64-ibm-aix", powerpc64_ibm_aix),
1033 ("powerpc64-unknown-linux-gnu", powerpc64_unknown_linux_gnu),
1034 ("powerpc64-unknown-linux-musl", powerpc64_unknown_linux_musl),
1035 ("powerpc64le-unknown-linux-gnu", powerpc64le_unknown_linux_gnu),
1036 ("powerpc64le-unknown-linux-musl", powerpc64le_unknown_linux_musl),
1037 ("s390x-unknown-linux-gnu", s390x_unknown_linux_gnu),
1038 ("s390x-unknown-linux-musl", s390x_unknown_linux_musl),
1039 ("sparc-unknown-linux-gnu", sparc_unknown_linux_gnu),
1040 ("sparc64-unknown-linux-gnu", sparc64_unknown_linux_gnu),
1041 ("arm-unknown-linux-gnueabi", arm_unknown_linux_gnueabi),
1042 ("arm-unknown-linux-gnueabihf", arm_unknown_linux_gnueabihf),
1043 ("armeb-unknown-linux-gnueabi", armeb_unknown_linux_gnueabi),
1044 ("arm-unknown-linux-musleabi", arm_unknown_linux_musleabi),
1045 ("arm-unknown-linux-musleabihf", arm_unknown_linux_musleabihf),
1046 ("armv4t-unknown-linux-gnueabi", armv4t_unknown_linux_gnueabi),
1047 ("armv5te-unknown-linux-gnueabi", armv5te_unknown_linux_gnueabi),
1048 ("armv5te-unknown-linux-musleabi", armv5te_unknown_linux_musleabi),
1049 ("armv5te-unknown-linux-uclibceabi", armv5te_unknown_linux_uclibceabi),
1050 ("armv7-unknown-linux-gnueabi", armv7_unknown_linux_gnueabi),
1051 ("armv7-unknown-linux-gnueabihf", armv7_unknown_linux_gnueabihf),
1052 ("thumbv7neon-unknown-linux-gnueabihf", thumbv7neon_unknown_linux_gnueabihf),
1053 ("thumbv7neon-unknown-linux-musleabihf", thumbv7neon_unknown_linux_musleabihf),
1054 ("armv7-unknown-linux-musleabi", armv7_unknown_linux_musleabi),
1055 ("armv7-unknown-linux-musleabihf", armv7_unknown_linux_musleabihf),
1056 ("aarch64-unknown-linux-gnu", aarch64_unknown_linux_gnu),
1057 ("aarch64-unknown-linux-musl", aarch64_unknown_linux_musl),
1058 ("x86_64-unknown-linux-musl", x86_64_unknown_linux_musl),
1059 ("i686-unknown-linux-musl", i686_unknown_linux_musl),
1060 ("i586-unknown-linux-musl", i586_unknown_linux_musl),
1061 ("mips-unknown-linux-musl", mips_unknown_linux_musl),
1062 ("mipsel-unknown-linux-musl", mipsel_unknown_linux_musl),
1063 ("mips64-unknown-linux-muslabi64", mips64_unknown_linux_muslabi64),
1064 ("mips64el-unknown-linux-muslabi64", mips64el_unknown_linux_muslabi64),
1065 ("hexagon-unknown-linux-musl", hexagon_unknown_linux_musl),
1067 ("mips-unknown-linux-uclibc", mips_unknown_linux_uclibc),
1068 ("mipsel-unknown-linux-uclibc", mipsel_unknown_linux_uclibc),
1070 ("i686-linux-android", i686_linux_android),
1071 ("x86_64-linux-android", x86_64_linux_android),
1072 ("arm-linux-androideabi", arm_linux_androideabi),
1073 ("armv7-linux-androideabi", armv7_linux_androideabi),
1074 ("thumbv7neon-linux-androideabi", thumbv7neon_linux_androideabi),
1075 ("aarch64-linux-android", aarch64_linux_android),
1077 ("aarch64-unknown-freebsd", aarch64_unknown_freebsd),
1078 ("armv6-unknown-freebsd", armv6_unknown_freebsd),
1079 ("armv7-unknown-freebsd", armv7_unknown_freebsd),
1080 ("i686-unknown-freebsd", i686_unknown_freebsd),
1081 ("powerpc-unknown-freebsd", powerpc_unknown_freebsd),
1082 ("powerpc64-unknown-freebsd", powerpc64_unknown_freebsd),
1083 ("powerpc64le-unknown-freebsd", powerpc64le_unknown_freebsd),
1084 ("riscv64gc-unknown-freebsd", riscv64gc_unknown_freebsd),
1085 ("x86_64-unknown-freebsd", x86_64_unknown_freebsd),
1087 ("x86_64-unknown-dragonfly", x86_64_unknown_dragonfly),
1089 ("aarch64-unknown-openbsd", aarch64_unknown_openbsd),
1090 ("i686-unknown-openbsd", i686_unknown_openbsd),
1091 ("powerpc-unknown-openbsd", powerpc_unknown_openbsd),
1092 ("powerpc64-unknown-openbsd", powerpc64_unknown_openbsd),
1093 ("riscv64gc-unknown-openbsd", riscv64gc_unknown_openbsd),
1094 ("sparc64-unknown-openbsd", sparc64_unknown_openbsd),
1095 ("x86_64-unknown-openbsd", x86_64_unknown_openbsd),
1097 ("aarch64-unknown-netbsd", aarch64_unknown_netbsd),
1098 ("armv6-unknown-netbsd-eabihf", armv6_unknown_netbsd_eabihf),
1099 ("armv7-unknown-netbsd-eabihf", armv7_unknown_netbsd_eabihf),
1100 ("i686-unknown-netbsd", i686_unknown_netbsd),
1101 ("powerpc-unknown-netbsd", powerpc_unknown_netbsd),
1102 ("sparc64-unknown-netbsd", sparc64_unknown_netbsd),
1103 ("x86_64-unknown-netbsd", x86_64_unknown_netbsd),
1105 ("i686-unknown-haiku", i686_unknown_haiku),
1106 ("x86_64-unknown-haiku", x86_64_unknown_haiku),
1108 ("aarch64-apple-darwin", aarch64_apple_darwin),
1109 ("x86_64-apple-darwin", x86_64_apple_darwin),
1110 ("i686-apple-darwin", i686_apple_darwin),
1112 ("aarch64-fuchsia", aarch64_fuchsia),
1113 ("x86_64-fuchsia", x86_64_fuchsia),
1115 ("avr-unknown-gnu-atmega328", avr_unknown_gnu_atmega328),
1117 ("x86_64-unknown-l4re-uclibc", x86_64_unknown_l4re_uclibc),
1119 ("aarch64-unknown-redox", aarch64_unknown_redox),
1120 ("x86_64-unknown-redox", x86_64_unknown_redox),
1122 ("i386-apple-ios", i386_apple_ios),
1123 ("x86_64-apple-ios", x86_64_apple_ios),
1124 ("aarch64-apple-ios", aarch64_apple_ios),
1125 ("armv7-apple-ios", armv7_apple_ios),
1126 ("armv7s-apple-ios", armv7s_apple_ios),
1127 ("x86_64-apple-ios-macabi", x86_64_apple_ios_macabi),
1128 ("aarch64-apple-ios-macabi", aarch64_apple_ios_macabi),
1129 ("aarch64-apple-ios-sim", aarch64_apple_ios_sim),
1130 ("aarch64-apple-tvos", aarch64_apple_tvos),
1131 ("x86_64-apple-tvos", x86_64_apple_tvos),
1133 ("armv7k-apple-watchos", armv7k_apple_watchos),
1134 ("arm64_32-apple-watchos", arm64_32_apple_watchos),
1135 ("x86_64-apple-watchos-sim", x86_64_apple_watchos_sim),
1136 ("aarch64-apple-watchos-sim", aarch64_apple_watchos_sim),
1138 ("armebv7r-none-eabi", armebv7r_none_eabi),
1139 ("armebv7r-none-eabihf", armebv7r_none_eabihf),
1140 ("armv7r-none-eabi", armv7r_none_eabi),
1141 ("armv7r-none-eabihf", armv7r_none_eabihf),
1143 ("x86_64-pc-solaris", x86_64_pc_solaris),
1144 ("x86_64-sun-solaris", x86_64_sun_solaris),
1145 ("sparcv9-sun-solaris", sparcv9_sun_solaris),
1147 ("x86_64-unknown-illumos", x86_64_unknown_illumos),
1149 ("x86_64-pc-windows-gnu", x86_64_pc_windows_gnu),
1150 ("i686-pc-windows-gnu", i686_pc_windows_gnu),
1151 ("i686-uwp-windows-gnu", i686_uwp_windows_gnu),
1152 ("x86_64-uwp-windows-gnu", x86_64_uwp_windows_gnu),
1154 ("aarch64-pc-windows-gnullvm", aarch64_pc_windows_gnullvm),
1155 ("x86_64-pc-windows-gnullvm", x86_64_pc_windows_gnullvm),
1157 ("aarch64-pc-windows-msvc", aarch64_pc_windows_msvc),
1158 ("aarch64-uwp-windows-msvc", aarch64_uwp_windows_msvc),
1159 ("x86_64-pc-windows-msvc", x86_64_pc_windows_msvc),
1160 ("x86_64-uwp-windows-msvc", x86_64_uwp_windows_msvc),
1161 ("i686-pc-windows-msvc", i686_pc_windows_msvc),
1162 ("i686-uwp-windows-msvc", i686_uwp_windows_msvc),
1163 ("i586-pc-windows-msvc", i586_pc_windows_msvc),
1164 ("thumbv7a-pc-windows-msvc", thumbv7a_pc_windows_msvc),
1165 ("thumbv7a-uwp-windows-msvc", thumbv7a_uwp_windows_msvc),
1167 ("asmjs-unknown-emscripten", asmjs_unknown_emscripten),
1168 ("wasm32-unknown-emscripten", wasm32_unknown_emscripten),
1169 ("wasm32-unknown-unknown", wasm32_unknown_unknown),
1170 ("wasm32-wasi", wasm32_wasi),
1171 ("wasm64-unknown-unknown", wasm64_unknown_unknown),
1173 ("thumbv6m-none-eabi", thumbv6m_none_eabi),
1174 ("thumbv7m-none-eabi", thumbv7m_none_eabi),
1175 ("thumbv7em-none-eabi", thumbv7em_none_eabi),
1176 ("thumbv7em-none-eabihf", thumbv7em_none_eabihf),
1177 ("thumbv8m.base-none-eabi", thumbv8m_base_none_eabi),
1178 ("thumbv8m.main-none-eabi", thumbv8m_main_none_eabi),
1179 ("thumbv8m.main-none-eabihf", thumbv8m_main_none_eabihf),
1181 ("armv7a-none-eabi", armv7a_none_eabi),
1182 ("armv7a-none-eabihf", armv7a_none_eabihf),
1184 ("msp430-none-elf", msp430_none_elf),
1186 ("aarch64-unknown-hermit", aarch64_unknown_hermit),
1187 ("x86_64-unknown-hermit", x86_64_unknown_hermit),
1189 ("riscv32i-unknown-none-elf", riscv32i_unknown_none_elf),
1190 ("riscv32im-unknown-none-elf", riscv32im_unknown_none_elf),
1191 ("riscv32imc-unknown-none-elf", riscv32imc_unknown_none_elf),
1192 ("riscv32imc-esp-espidf", riscv32imc_esp_espidf),
1193 ("riscv32imac-unknown-none-elf", riscv32imac_unknown_none_elf),
1194 ("riscv32imac-unknown-xous-elf", riscv32imac_unknown_xous_elf),
1195 ("riscv32gc-unknown-linux-gnu", riscv32gc_unknown_linux_gnu),
1196 ("riscv32gc-unknown-linux-musl", riscv32gc_unknown_linux_musl),
1197 ("riscv64imac-unknown-none-elf", riscv64imac_unknown_none_elf),
1198 ("riscv64gc-unknown-none-elf", riscv64gc_unknown_none_elf),
1199 ("riscv64gc-unknown-linux-gnu", riscv64gc_unknown_linux_gnu),
1200 ("riscv64gc-unknown-linux-musl", riscv64gc_unknown_linux_musl),
1202 ("aarch64-unknown-none", aarch64_unknown_none),
1203 ("aarch64-unknown-none-softfloat", aarch64_unknown_none_softfloat),
1205 ("x86_64-fortanix-unknown-sgx", x86_64_fortanix_unknown_sgx),
1207 ("x86_64-unknown-uefi", x86_64_unknown_uefi),
1208 ("i686-unknown-uefi", i686_unknown_uefi),
1209 ("aarch64-unknown-uefi", aarch64_unknown_uefi),
1211 ("nvptx64-nvidia-cuda", nvptx64_nvidia_cuda),
1213 ("i686-wrs-vxworks", i686_wrs_vxworks),
1214 ("x86_64-wrs-vxworks", x86_64_wrs_vxworks),
1215 ("armv7-wrs-vxworks-eabihf", armv7_wrs_vxworks_eabihf),
1216 ("aarch64-wrs-vxworks", aarch64_wrs_vxworks),
1217 ("powerpc-wrs-vxworks", powerpc_wrs_vxworks),
1218 ("powerpc-wrs-vxworks-spe", powerpc_wrs_vxworks_spe),
1219 ("powerpc64-wrs-vxworks", powerpc64_wrs_vxworks),
1221 ("aarch64-kmc-solid_asp3", aarch64_kmc_solid_asp3),
1222 ("armv7a-kmc-solid_asp3-eabi", armv7a_kmc_solid_asp3_eabi),
1223 ("armv7a-kmc-solid_asp3-eabihf", armv7a_kmc_solid_asp3_eabihf),
1225 ("mipsel-sony-psp", mipsel_sony_psp),
1226 ("mipsel-sony-psx", mipsel_sony_psx),
1227 ("mipsel-unknown-none", mipsel_unknown_none),
1228 ("thumbv4t-none-eabi", thumbv4t_none_eabi),
1229 ("armv4t-none-eabi", armv4t_none_eabi),
1230 ("thumbv5te-none-eabi", thumbv5te_none_eabi),
1231 ("armv5te-none-eabi", armv5te_none_eabi),
1233 ("aarch64_be-unknown-linux-gnu", aarch64_be_unknown_linux_gnu),
1234 ("aarch64-unknown-linux-gnu_ilp32", aarch64_unknown_linux_gnu_ilp32),
1235 ("aarch64_be-unknown-linux-gnu_ilp32", aarch64_be_unknown_linux_gnu_ilp32),
1237 ("bpfeb-unknown-none", bpfeb_unknown_none),
1238 ("bpfel-unknown-none", bpfel_unknown_none),
1240 ("armv6k-nintendo-3ds", armv6k_nintendo_3ds),
1242 ("aarch64-nintendo-switch-freestanding", aarch64_nintendo_switch_freestanding),
1244 ("armv7-sony-vita-newlibeabihf", armv7_sony_vita_newlibeabihf),
1246 ("armv7-unknown-linux-uclibceabi", armv7_unknown_linux_uclibceabi),
1247 ("armv7-unknown-linux-uclibceabihf", armv7_unknown_linux_uclibceabihf),
1249 ("x86_64-unknown-none", x86_64_unknown_none),
1251 ("mips64-openwrt-linux-musl", mips64_openwrt_linux_musl),
1253 ("aarch64-unknown-nto-qnx710", aarch64_unknown_nto_qnx_710),
1254 ("x86_64-pc-nto-qnx710", x86_64_pc_nto_qnx710),
1257 /// Cow-Vec-Str: Cow<'static, [Cow<'static, str>]>
1260 ::std::borrow::Cow::Borrowed(&[])
1262 ($($x:expr),+ $(,)?) => {
1263 ::std::borrow::Cow::Borrowed(&[
1265 ::std::borrow::Cow::Borrowed($x),
1273 /// Warnings encountered when parsing the target `json`.
1275 /// Includes fields that weren't recognized and fields that don't have the expected type.
1276 #[derive(Debug, PartialEq)]
1277 pub struct TargetWarnings {
1278 unused_fields: Vec<String>,
1279 incorrect_type: Vec<String>,
1282 impl TargetWarnings {
1283 pub fn empty() -> Self {
1284 Self { unused_fields: Vec::new(), incorrect_type: Vec::new() }
1287 pub fn warning_messages(&self) -> Vec<String> {
1288 let mut warnings = vec![];
1289 if !self.unused_fields.is_empty() {
1290 warnings.push(format!(
1291 "target json file contains unused fields: {}",
1292 self.unused_fields.join(", ")
1295 if !self.incorrect_type.is_empty() {
1296 warnings.push(format!(
1297 "target json file contains fields whose value doesn't have the correct json type: {}",
1298 self.incorrect_type.join(", ")
1305 /// Everything `rustc` knows about how to compile for a specific target.
1307 /// Every field here must be specified, and has no default value.
1308 #[derive(PartialEq, Clone, Debug)]
1310 /// Target triple to pass to LLVM.
1311 pub llvm_target: StaticCow<str>,
1312 /// Number of bits in a pointer. Influences the `target_pointer_width` `cfg` variable.
1313 pub pointer_width: u32,
1314 /// Architecture to use for ABI considerations. Valid options include: "x86",
1315 /// "x86_64", "arm", "aarch64", "mips", "powerpc", "powerpc64", and others.
1316 pub arch: StaticCow<str>,
1317 /// [Data layout](https://llvm.org/docs/LangRef.html#data-layout) to pass to LLVM.
1318 pub data_layout: StaticCow<str>,
1319 /// Optional settings with defaults.
1320 pub options: TargetOptions,
1324 pub fn parse_data_layout(&self) -> Result<TargetDataLayout, TargetDataLayoutErrors<'_>> {
1325 let mut dl = TargetDataLayout::parse_from_llvm_datalayout_string(&self.data_layout)?;
1327 // Perform consistency checks against the Target information.
1328 if dl.endian != self.endian {
1329 return Err(TargetDataLayoutErrors::InconsistentTargetArchitecture {
1330 dl: dl.endian.as_str(),
1331 target: self.endian.as_str(),
1335 let target_pointer_width: u64 = self.pointer_width.into();
1336 if dl.pointer_size.bits() != target_pointer_width {
1337 return Err(TargetDataLayoutErrors::InconsistentTargetPointerWidth {
1338 pointer_size: dl.pointer_size.bits(),
1339 target: self.pointer_width,
1343 dl.c_enum_min_size = match Integer::from_size(Size::from_bits(self.c_enum_min_bits)) {
1345 Err(err) => return Err(TargetDataLayoutErrors::InvalidBitsSize { err }),
1352 pub trait HasTargetSpec {
1353 fn target_spec(&self) -> &Target;
1356 impl HasTargetSpec for Target {
1358 fn target_spec(&self) -> &Target {
1363 type StaticCow<T> = Cow<'static, T>;
1365 /// Optional aspects of a target specification.
1367 /// This has an implementation of `Default`, see each field for what the default is. In general,
1368 /// these try to take "minimal defaults" that don't assume anything about the runtime they run in.
1370 /// `TargetOptions` as a separate structure is mostly an implementation detail of `Target`
1371 /// construction, all its fields logically belong to `Target` and available from `Target`
1372 /// through `Deref` impls.
1373 #[derive(PartialEq, Clone, Debug)]
1374 pub struct TargetOptions {
1375 /// Whether the target is built-in or loaded from a custom target specification.
1376 pub is_builtin: bool,
1378 /// Used as the `target_endian` `cfg` variable. Defaults to little endian.
1380 /// Width of c_int type. Defaults to "32".
1381 pub c_int_width: StaticCow<str>,
1382 /// OS name to use for conditional compilation (`target_os`). Defaults to "none".
1383 /// "none" implies a bare metal target without `std` library.
1384 /// A couple of targets having `std` also use "unknown" as an `os` value,
1385 /// but they are exceptions.
1386 pub os: StaticCow<str>,
1387 /// Environment name to use for conditional compilation (`target_env`). Defaults to "".
1388 pub env: StaticCow<str>,
1389 /// ABI name to distinguish multiple ABIs on the same OS and architecture. For instance, `"eabi"`
1390 /// or `"eabihf"`. Defaults to "".
1391 pub abi: StaticCow<str>,
1392 /// Vendor name to use for conditional compilation (`target_vendor`). Defaults to "unknown".
1393 pub vendor: StaticCow<str>,
1395 /// Linker to invoke
1396 pub linker: Option<StaticCow<str>>,
1397 /// Default linker flavor used if `-C linker-flavor` or `-C linker` are not passed
1398 /// on the command line. Defaults to `LinkerFlavor::Gnu(Cc::Yes, Lld::No)`.
1399 pub linker_flavor: LinkerFlavor,
1400 linker_flavor_json: LinkerFlavorCli,
1401 lld_flavor_json: LldFlavor,
1402 linker_is_gnu_json: bool,
1404 /// Objects to link before and after all other object code.
1405 pub pre_link_objects: CrtObjects,
1406 pub post_link_objects: CrtObjects,
1407 /// Same as `(pre|post)_link_objects`, but when self-contained linking mode is enabled.
1408 pub pre_link_objects_self_contained: CrtObjects,
1409 pub post_link_objects_self_contained: CrtObjects,
1410 pub link_self_contained: LinkSelfContainedDefault,
1412 /// Linker arguments that are passed *before* any user-defined libraries.
1413 pub pre_link_args: LinkArgs,
1414 pre_link_args_json: LinkArgsCli,
1415 /// Linker arguments that are unconditionally passed after any
1416 /// user-defined but before post-link objects. Standard platform
1417 /// libraries that should be always be linked to, usually go here.
1418 pub late_link_args: LinkArgs,
1419 late_link_args_json: LinkArgsCli,
1420 /// Linker arguments used in addition to `late_link_args` if at least one
1421 /// Rust dependency is dynamically linked.
1422 pub late_link_args_dynamic: LinkArgs,
1423 late_link_args_dynamic_json: LinkArgsCli,
1424 /// Linker arguments used in addition to `late_link_args` if all Rust
1425 /// dependencies are statically linked.
1426 pub late_link_args_static: LinkArgs,
1427 late_link_args_static_json: LinkArgsCli,
1428 /// Linker arguments that are unconditionally passed *after* any
1429 /// user-defined libraries.
1430 pub post_link_args: LinkArgs,
1431 post_link_args_json: LinkArgsCli,
1433 /// Optional link script applied to `dylib` and `executable` crate types.
1434 /// This is a string containing the script, not a path. Can only be applied
1435 /// to linkers where linker flavor matches `LinkerFlavor::Gnu(..)`.
1436 pub link_script: Option<StaticCow<str>>,
1437 /// Environment variables to be set for the linker invocation.
1438 pub link_env: StaticCow<[(StaticCow<str>, StaticCow<str>)]>,
1439 /// Environment variables to be removed for the linker invocation.
1440 pub link_env_remove: StaticCow<[StaticCow<str>]>,
1442 /// Extra arguments to pass to the external assembler (when used)
1443 pub asm_args: StaticCow<[StaticCow<str>]>,
1445 /// Default CPU to pass to LLVM. Corresponds to `llc -mcpu=$cpu`. Defaults
1447 pub cpu: StaticCow<str>,
1448 /// Default target features to pass to LLVM. These features will *always* be
1449 /// passed, and cannot be disabled even via `-C`. Corresponds to `llc
1450 /// -mattr=$features`.
1451 pub features: StaticCow<str>,
1452 /// Whether dynamic linking is available on this target. Defaults to false.
1453 pub dynamic_linking: bool,
1454 /// If dynamic linking is available, whether only cdylibs are supported.
1455 pub only_cdylib: bool,
1456 /// Whether executables are available on this target. Defaults to true.
1457 pub executables: bool,
1458 /// Relocation model to use in object file. Corresponds to `llc
1459 /// -relocation-model=$relocation_model`. Defaults to `Pic`.
1460 pub relocation_model: RelocModel,
1461 /// Code model to use. Corresponds to `llc -code-model=$code_model`.
1462 /// Defaults to `None` which means "inherited from the base LLVM target".
1463 pub code_model: Option<CodeModel>,
1464 /// TLS model to use. Options are "global-dynamic" (default), "local-dynamic", "initial-exec"
1465 /// and "local-exec". This is similar to the -ftls-model option in GCC/Clang.
1466 pub tls_model: TlsModel,
1467 /// Do not emit code that uses the "red zone", if the ABI has one. Defaults to false.
1468 pub disable_redzone: bool,
1469 /// Frame pointer mode for this target. Defaults to `MayOmit`.
1470 pub frame_pointer: FramePointer,
1471 /// Emit each function in its own section. Defaults to true.
1472 pub function_sections: bool,
1473 /// String to prepend to the name of every dynamic library. Defaults to "lib".
1474 pub dll_prefix: StaticCow<str>,
1475 /// String to append to the name of every dynamic library. Defaults to ".so".
1476 pub dll_suffix: StaticCow<str>,
1477 /// String to append to the name of every executable.
1478 pub exe_suffix: StaticCow<str>,
1479 /// String to prepend to the name of every static library. Defaults to "lib".
1480 pub staticlib_prefix: StaticCow<str>,
1481 /// String to append to the name of every static library. Defaults to ".a".
1482 pub staticlib_suffix: StaticCow<str>,
1483 /// Values of the `target_family` cfg set for this target.
1485 /// Common options are: "unix", "windows". Defaults to no families.
1487 /// See <https://doc.rust-lang.org/reference/conditional-compilation.html#target_family>.
1488 pub families: StaticCow<[StaticCow<str>]>,
1489 /// Whether the target toolchain's ABI supports returning small structs as an integer.
1490 pub abi_return_struct_as_int: bool,
1491 /// Whether the target toolchain is like AIX's. Linker options on AIX are special and it uses
1492 /// XCOFF as binary format. Defaults to false.
1493 pub is_like_aix: bool,
1494 /// Whether the target toolchain is like macOS's. Only useful for compiling against iOS/macOS,
1495 /// in particular running dsymutil and some other stuff like `-dead_strip`. Defaults to false.
1496 /// Also indiates whether to use Apple-specific ABI changes, such as extending function
1497 /// parameters to 32-bits.
1498 pub is_like_osx: bool,
1499 /// Whether the target toolchain is like Solaris's.
1500 /// Only useful for compiling against Illumos/Solaris,
1501 /// as they have a different set of linker flags. Defaults to false.
1502 pub is_like_solaris: bool,
1503 /// Whether the target is like Windows.
1504 /// This is a combination of several more specific properties represented as a single flag:
1505 /// - The target uses a Windows ABI,
1506 /// - uses PE/COFF as a format for object code,
1507 /// - uses Windows-style dllexport/dllimport for shared libraries,
1508 /// - uses import libraries and .def files for symbol exports,
1509 /// - executables support setting a subsystem.
1510 pub is_like_windows: bool,
1511 /// Whether the target is like MSVC.
1512 /// This is a combination of several more specific properties represented as a single flag:
1513 /// - The target has all the properties from `is_like_windows`
1514 /// (for in-tree targets "is_like_msvc ⇒ is_like_windows" is ensured by a unit test),
1515 /// - has some MSVC-specific Windows ABI properties,
1516 /// - uses a link.exe-like linker,
1517 /// - uses CodeView/PDB for debuginfo and natvis for its visualization,
1518 /// - uses SEH-based unwinding,
1519 /// - supports control flow guard mechanism.
1520 pub is_like_msvc: bool,
1521 /// Whether a target toolchain is like WASM.
1522 pub is_like_wasm: bool,
1523 /// Whether a target toolchain is like Android, implying a Linux kernel and a Bionic libc
1524 pub is_like_android: bool,
1525 /// Default supported version of DWARF on this platform.
1526 /// Useful because some platforms (osx, bsd) only want up to DWARF2.
1527 pub default_dwarf_version: u32,
1528 /// The MinGW toolchain has a known issue that prevents it from correctly
1529 /// handling COFF object files with more than 2<sup>15</sup> sections. Since each weak
1530 /// symbol needs its own COMDAT section, weak linkage implies a large
1531 /// number sections that easily exceeds the given limit for larger
1532 /// codebases. Consequently we want a way to disallow weak linkage on some
1534 pub allows_weak_linkage: bool,
1535 /// Whether the linker support rpaths or not. Defaults to false.
1536 pub has_rpath: bool,
1537 /// Whether to disable linking to the default libraries, typically corresponds
1538 /// to `-nodefaultlibs`. Defaults to true.
1539 pub no_default_libraries: bool,
1540 /// Dynamically linked executables can be compiled as position independent
1541 /// if the default relocation model of position independent code is not
1542 /// changed. This is a requirement to take advantage of ASLR, as otherwise
1543 /// the functions in the executable are not randomized and can be used
1544 /// during an exploit of a vulnerability in any code.
1545 pub position_independent_executables: bool,
1546 /// Executables that are both statically linked and position-independent are supported.
1547 pub static_position_independent_executables: bool,
1548 /// Determines if the target always requires using the PLT for indirect
1549 /// library calls or not. This controls the default value of the `-Z plt` flag.
1550 pub needs_plt: bool,
1551 /// Either partial, full, or off. Full RELRO makes the dynamic linker
1552 /// resolve all symbols at startup and marks the GOT read-only before
1553 /// starting the program, preventing overwriting the GOT.
1554 pub relro_level: RelroLevel,
1555 /// Format that archives should be emitted in. This affects whether we use
1556 /// LLVM to assemble an archive or fall back to the system linker, and
1557 /// currently only "gnu" is used to fall into LLVM. Unknown strings cause
1558 /// the system linker to be used.
1559 pub archive_format: StaticCow<str>,
1560 /// Is asm!() allowed? Defaults to true.
1561 pub allow_asm: bool,
1562 /// Whether the runtime startup code requires the `main` function be passed
1563 /// `argc` and `argv` values.
1564 pub main_needs_argc_argv: bool,
1566 /// Flag indicating whether #[thread_local] is available for this target.
1567 pub has_thread_local: bool,
1568 /// This is mainly for easy compatibility with emscripten.
1569 /// If we give emcc .o files that are actually .bc files it
1570 /// will 'just work'.
1571 pub obj_is_bitcode: bool,
1572 /// Whether the target requires that emitted object code includes bitcode.
1573 pub forces_embed_bitcode: bool,
1574 /// Content of the LLVM cmdline section associated with embedded bitcode.
1575 pub bitcode_llvm_cmdline: StaticCow<str>,
1577 /// Don't use this field; instead use the `.min_atomic_width()` method.
1578 pub min_atomic_width: Option<u64>,
1580 /// Don't use this field; instead use the `.max_atomic_width()` method.
1581 pub max_atomic_width: Option<u64>,
1583 /// Whether the target supports atomic CAS operations natively
1584 pub atomic_cas: bool,
1586 /// Panic strategy: "unwind" or "abort"
1587 pub panic_strategy: PanicStrategy,
1589 /// Whether or not linking dylibs to a static CRT is allowed.
1590 pub crt_static_allows_dylibs: bool,
1591 /// Whether or not the CRT is statically linked by default.
1592 pub crt_static_default: bool,
1593 /// Whether or not crt-static is respected by the compiler (or is a no-op).
1594 pub crt_static_respected: bool,
1596 /// The implementation of stack probes to use.
1597 pub stack_probes: StackProbeType,
1599 /// The minimum alignment for global symbols.
1600 pub min_global_align: Option<u64>,
1602 /// Default number of codegen units to use in debug mode
1603 pub default_codegen_units: Option<u64>,
1605 /// Whether to generate trap instructions in places where optimization would
1606 /// otherwise produce control flow that falls through into unrelated memory.
1607 pub trap_unreachable: bool,
1609 /// This target requires everything to be compiled with LTO to emit a final
1610 /// executable, aka there is no native linker for this target.
1611 pub requires_lto: bool,
1613 /// This target has no support for threads.
1614 pub singlethread: bool,
1616 /// Whether library functions call lowering/optimization is disabled in LLVM
1617 /// for this target unconditionally.
1618 pub no_builtins: bool,
1620 /// The default visibility for symbols in this target should be "hidden"
1621 /// rather than "default"
1622 pub default_hidden_visibility: bool,
1624 /// Whether a .debug_gdb_scripts section will be added to the output object file
1625 pub emit_debug_gdb_scripts: bool,
1627 /// Whether or not to unconditionally `uwtable` attributes on functions,
1628 /// typically because the platform needs to unwind for things like stack
1630 pub requires_uwtable: bool,
1632 /// Whether or not to emit `uwtable` attributes on functions if `-C force-unwind-tables`
1633 /// is not specified and `uwtable` is not required on this target.
1634 pub default_uwtable: bool,
1636 /// Whether or not SIMD types are passed by reference in the Rust ABI,
1637 /// typically required if a target can be compiled with a mixed set of
1638 /// target features. This is `true` by default, and `false` for targets like
1639 /// wasm32 where the whole program either has simd or not.
1640 pub simd_types_indirect: bool,
1642 /// Pass a list of symbol which should be exported in the dylib to the linker.
1643 pub limit_rdylib_exports: bool,
1645 /// If set, have the linker export exactly these symbols, instead of using
1646 /// the usual logic to figure this out from the crate itself.
1647 pub override_export_symbols: Option<StaticCow<[StaticCow<str>]>>,
1649 /// Determines how or whether the MergeFunctions LLVM pass should run for
1650 /// this target. Either "disabled", "trampolines", or "aliases".
1651 /// The MergeFunctions pass is generally useful, but some targets may need
1652 /// to opt out. The default is "aliases".
1654 /// Workaround for: <https://github.com/rust-lang/rust/issues/57356>
1655 pub merge_functions: MergeFunctions,
1657 /// Use platform dependent mcount function
1658 pub mcount: StaticCow<str>,
1660 /// LLVM ABI name, corresponds to the '-mabi' parameter available in multilib C compilers
1661 pub llvm_abiname: StaticCow<str>,
1663 /// Whether or not RelaxElfRelocation flag will be passed to the linker
1664 pub relax_elf_relocations: bool,
1666 /// Additional arguments to pass to LLVM, similar to the `-C llvm-args` codegen option.
1667 pub llvm_args: StaticCow<[StaticCow<str>]>,
1669 /// Whether to use legacy .ctors initialization hooks rather than .init_array. Defaults
1670 /// to false (uses .init_array).
1671 pub use_ctors_section: bool,
1673 /// Whether the linker is instructed to add a `GNU_EH_FRAME` ELF header
1674 /// used to locate unwinding information is passed
1675 /// (only has effect if the linker is `ld`-like).
1676 pub eh_frame_header: bool,
1678 /// Is true if the target is an ARM architecture using thumb v1 which allows for
1679 /// thumb and arm interworking.
1680 pub has_thumb_interworking: bool,
1682 /// Which kind of debuginfo is used by this target?
1683 pub debuginfo_kind: DebuginfoKind,
1684 /// How to handle split debug information, if at all. Specifying `None` has
1685 /// target-specific meaning.
1686 pub split_debuginfo: SplitDebuginfo,
1687 /// Which kinds of split debuginfo are supported by the target?
1688 pub supported_split_debuginfo: StaticCow<[SplitDebuginfo]>,
1690 /// The sanitizers supported by this target
1692 /// Note that the support here is at a codegen level. If the machine code with sanitizer
1693 /// enabled can generated on this target, but the necessary supporting libraries are not
1694 /// distributed with the target, the sanitizer should still appear in this list for the target.
1695 pub supported_sanitizers: SanitizerSet,
1697 /// If present it's a default value to use for adjusting the C ABI.
1698 pub default_adjusted_cabi: Option<Abi>,
1700 /// Minimum number of bits in #[repr(C)] enum. Defaults to 32.
1701 pub c_enum_min_bits: u64,
1703 /// Whether or not the DWARF `.debug_aranges` section should be generated.
1704 pub generate_arange_section: bool,
1706 /// Whether the target supports stack canary checks. `true` by default,
1707 /// since this is most common among tier 1 and tier 2 targets.
1708 pub supports_stack_protector: bool,
1710 /// The name of entry function.
1711 /// Default value is "main"
1712 pub entry_name: StaticCow<str>,
1714 /// The ABI of entry function.
1715 /// Default value is `Conv::C`, i.e. C call convention
1716 pub entry_abi: Conv,
1719 /// Add arguments for the given flavor and also for its "twin" flavors
1720 /// that have a compatible command line interface.
1721 fn add_link_args_iter(
1722 link_args: &mut LinkArgs,
1723 flavor: LinkerFlavor,
1724 args: impl Iterator<Item = StaticCow<str>> + Clone,
1726 let mut insert = |flavor| link_args.entry(flavor).or_default().extend(args.clone());
1729 LinkerFlavor::Gnu(cc, lld) => {
1730 assert_eq!(lld, Lld::No);
1731 insert(LinkerFlavor::Gnu(cc, Lld::Yes));
1733 LinkerFlavor::Darwin(cc, lld) => {
1734 assert_eq!(lld, Lld::No);
1735 insert(LinkerFlavor::Darwin(cc, Lld::Yes));
1737 LinkerFlavor::Msvc(lld) => {
1738 assert_eq!(lld, Lld::No);
1739 insert(LinkerFlavor::Msvc(Lld::Yes));
1741 LinkerFlavor::WasmLld(..)
1742 | LinkerFlavor::Unix(..)
1743 | LinkerFlavor::EmCc
1745 | LinkerFlavor::Ptx => {}
1749 fn add_link_args(link_args: &mut LinkArgs, flavor: LinkerFlavor, args: &[&'static str]) {
1750 add_link_args_iter(link_args, flavor, args.iter().copied().map(Cow::Borrowed))
1753 impl TargetOptions {
1754 fn link_args(flavor: LinkerFlavor, args: &[&'static str]) -> LinkArgs {
1755 let mut link_args = LinkArgs::new();
1756 add_link_args(&mut link_args, flavor, args);
1760 fn add_pre_link_args(&mut self, flavor: LinkerFlavor, args: &[&'static str]) {
1761 add_link_args(&mut self.pre_link_args, flavor, args);
1764 fn add_post_link_args(&mut self, flavor: LinkerFlavor, args: &[&'static str]) {
1765 add_link_args(&mut self.post_link_args, flavor, args);
1768 fn update_from_cli(&mut self) {
1769 self.linker_flavor = LinkerFlavor::from_cli_impl(
1770 self.linker_flavor_json,
1771 self.lld_flavor_json,
1772 self.linker_is_gnu_json,
1774 for (args, args_json) in [
1775 (&mut self.pre_link_args, &self.pre_link_args_json),
1776 (&mut self.late_link_args, &self.late_link_args_json),
1777 (&mut self.late_link_args_dynamic, &self.late_link_args_dynamic_json),
1778 (&mut self.late_link_args_static, &self.late_link_args_static_json),
1779 (&mut self.post_link_args, &self.post_link_args_json),
1782 for (flavor, args_json) in args_json {
1783 // Cannot use `from_cli` due to borrow checker.
1784 let linker_flavor = LinkerFlavor::from_cli_impl(
1786 self.lld_flavor_json,
1787 self.linker_is_gnu_json,
1789 // Normalize to no lld to avoid asserts.
1790 let linker_flavor = match linker_flavor {
1791 LinkerFlavor::Gnu(cc, _) => LinkerFlavor::Gnu(cc, Lld::No),
1792 LinkerFlavor::Darwin(cc, _) => LinkerFlavor::Darwin(cc, Lld::No),
1793 LinkerFlavor::Msvc(_) => LinkerFlavor::Msvc(Lld::No),
1796 if !args.contains_key(&linker_flavor) {
1797 add_link_args_iter(args, linker_flavor, args_json.iter().cloned());
1803 fn update_to_cli(&mut self) {
1804 self.linker_flavor_json = self.linker_flavor.to_cli();
1805 self.lld_flavor_json = self.linker_flavor.lld_flavor();
1806 self.linker_is_gnu_json = self.linker_flavor.is_gnu();
1807 for (args, args_json) in [
1808 (&self.pre_link_args, &mut self.pre_link_args_json),
1809 (&self.late_link_args, &mut self.late_link_args_json),
1810 (&self.late_link_args_dynamic, &mut self.late_link_args_dynamic_json),
1811 (&self.late_link_args_static, &mut self.late_link_args_static_json),
1812 (&self.post_link_args, &mut self.post_link_args_json),
1815 args.iter().map(|(flavor, args)| (flavor.to_cli(), args.clone())).collect();
1820 impl Default for TargetOptions {
1821 /// Creates a set of "sane defaults" for any target. This is still
1822 /// incomplete, and if used for compilation, will certainly not work.
1823 fn default() -> TargetOptions {
1826 endian: Endian::Little,
1827 c_int_width: "32".into(),
1831 vendor: "unknown".into(),
1832 linker: option_env!("CFG_DEFAULT_LINKER").map(|s| s.into()),
1833 linker_flavor: LinkerFlavor::Gnu(Cc::Yes, Lld::No),
1834 linker_flavor_json: LinkerFlavorCli::Gcc,
1835 lld_flavor_json: LldFlavor::Ld,
1836 linker_is_gnu_json: true,
1839 cpu: "generic".into(),
1840 features: "".into(),
1841 dynamic_linking: false,
1844 relocation_model: RelocModel::Pic,
1846 tls_model: TlsModel::GeneralDynamic,
1847 disable_redzone: false,
1848 frame_pointer: FramePointer::MayOmit,
1849 function_sections: true,
1850 dll_prefix: "lib".into(),
1851 dll_suffix: ".so".into(),
1852 exe_suffix: "".into(),
1853 staticlib_prefix: "lib".into(),
1854 staticlib_suffix: ".a".into(),
1856 abi_return_struct_as_int: false,
1859 is_like_solaris: false,
1860 is_like_windows: false,
1861 is_like_msvc: false,
1862 is_like_wasm: false,
1863 is_like_android: false,
1864 default_dwarf_version: 4,
1865 allows_weak_linkage: true,
1867 no_default_libraries: true,
1868 position_independent_executables: false,
1869 static_position_independent_executables: false,
1871 relro_level: RelroLevel::None,
1872 pre_link_objects: Default::default(),
1873 post_link_objects: Default::default(),
1874 pre_link_objects_self_contained: Default::default(),
1875 post_link_objects_self_contained: Default::default(),
1876 link_self_contained: LinkSelfContainedDefault::False,
1877 pre_link_args: LinkArgs::new(),
1878 pre_link_args_json: LinkArgsCli::new(),
1879 late_link_args: LinkArgs::new(),
1880 late_link_args_json: LinkArgsCli::new(),
1881 late_link_args_dynamic: LinkArgs::new(),
1882 late_link_args_dynamic_json: LinkArgsCli::new(),
1883 late_link_args_static: LinkArgs::new(),
1884 late_link_args_static_json: LinkArgsCli::new(),
1885 post_link_args: LinkArgs::new(),
1886 post_link_args_json: LinkArgsCli::new(),
1888 link_env_remove: cvs![],
1889 archive_format: "gnu".into(),
1890 main_needs_argc_argv: true,
1892 has_thread_local: false,
1893 obj_is_bitcode: false,
1894 forces_embed_bitcode: false,
1895 bitcode_llvm_cmdline: "".into(),
1896 min_atomic_width: None,
1897 max_atomic_width: None,
1899 panic_strategy: PanicStrategy::Unwind,
1900 crt_static_allows_dylibs: false,
1901 crt_static_default: false,
1902 crt_static_respected: false,
1903 stack_probes: StackProbeType::None,
1904 min_global_align: None,
1905 default_codegen_units: None,
1906 trap_unreachable: true,
1907 requires_lto: false,
1908 singlethread: false,
1910 default_hidden_visibility: false,
1911 emit_debug_gdb_scripts: true,
1912 requires_uwtable: false,
1913 default_uwtable: false,
1914 simd_types_indirect: true,
1915 limit_rdylib_exports: true,
1916 override_export_symbols: None,
1917 merge_functions: MergeFunctions::Aliases,
1918 mcount: "mcount".into(),
1919 llvm_abiname: "".into(),
1920 relax_elf_relocations: false,
1922 use_ctors_section: false,
1923 eh_frame_header: true,
1924 has_thumb_interworking: false,
1925 debuginfo_kind: Default::default(),
1926 split_debuginfo: Default::default(),
1927 // `Off` is supported by default, but targets can remove this manually, e.g. Windows.
1928 supported_split_debuginfo: Cow::Borrowed(&[SplitDebuginfo::Off]),
1929 supported_sanitizers: SanitizerSet::empty(),
1930 default_adjusted_cabi: None,
1931 c_enum_min_bits: 32,
1932 generate_arange_section: true,
1933 supports_stack_protector: true,
1934 entry_name: "main".into(),
1940 /// `TargetOptions` being a separate type is basically an implementation detail of `Target` that is
1941 /// used for providing defaults. Perhaps there's a way to merge `TargetOptions` into `Target` so
1942 /// this `Deref` implementation is no longer necessary.
1943 impl Deref for Target {
1944 type Target = TargetOptions;
1947 fn deref(&self) -> &Self::Target {
1951 impl DerefMut for Target {
1953 fn deref_mut(&mut self) -> &mut Self::Target {
1959 /// Given a function ABI, turn it into the correct ABI for this target.
1960 pub fn adjust_abi(&self, abi: Abi) -> Abi {
1962 Abi::C { .. } => self.default_adjusted_cabi.unwrap_or(abi),
1963 Abi::System { unwind } if self.is_like_windows && self.arch == "x86" => {
1964 Abi::Stdcall { unwind }
1966 Abi::System { unwind } => Abi::C { unwind },
1967 Abi::EfiApi if self.arch == "arm" => Abi::Aapcs { unwind: false },
1968 Abi::EfiApi if self.arch == "x86_64" => Abi::Win64 { unwind: false },
1969 Abi::EfiApi => Abi::C { unwind: false },
1971 // See commentary in `is_abi_supported`.
1972 Abi::Stdcall { .. } | Abi::Thiscall { .. } if self.arch == "x86" => abi,
1973 Abi::Stdcall { unwind } | Abi::Thiscall { unwind } => Abi::C { unwind },
1974 Abi::Fastcall { .. } if self.arch == "x86" => abi,
1975 Abi::Vectorcall { .. } if ["x86", "x86_64"].contains(&&self.arch[..]) => abi,
1976 Abi::Fastcall { unwind } | Abi::Vectorcall { unwind } => Abi::C { unwind },
1982 /// Returns a None if the UNSUPPORTED_CALLING_CONVENTIONS lint should be emitted
1983 pub fn is_abi_supported(&self, abi: Abi) -> Option<bool> {
1996 ["arm", "aarch64", "riscv32", "riscv64", "x86", "x86_64"].contains(&&self.arch[..])
1998 X86Interrupt => ["x86", "x86_64"].contains(&&self.arch[..]),
1999 Aapcs { .. } => "arm" == self.arch,
2000 CCmseNonSecureCall => ["arm", "aarch64"].contains(&&self.arch[..]),
2001 Win64 { .. } | SysV64 { .. } => self.arch == "x86_64",
2002 PtxKernel => self.arch == "nvptx64",
2003 Msp430Interrupt => self.arch == "msp430",
2004 AmdGpuKernel => self.arch == "amdgcn",
2005 AvrInterrupt | AvrNonBlockingInterrupt => self.arch == "avr",
2006 Wasm => ["wasm32", "wasm64"].contains(&&self.arch[..]),
2007 Thiscall { .. } => self.arch == "x86",
2008 // On windows these fall-back to platform native calling convention (C) when the
2009 // architecture is not supported.
2011 // This is I believe a historical accident that has occurred as part of Microsoft
2012 // striving to allow most of the code to "just" compile when support for 64-bit x86
2013 // was added and then later again, when support for ARM architectures was added.
2015 // This is well documented across MSDN. Support for this in Rust has been added in
2016 // #54576. This makes much more sense in context of Microsoft's C++ than it does in
2017 // Rust, but there isn't much leeway remaining here to change it back at the time this
2018 // comment has been written.
2020 // Following are the relevant excerpts from the MSDN documentation.
2022 // > The __vectorcall calling convention is only supported in native code on x86 and
2023 // x64 processors that include Streaming SIMD Extensions 2 (SSE2) and above.
2025 // > On ARM machines, __vectorcall is accepted and ignored by the compiler.
2027 // -- https://docs.microsoft.com/en-us/cpp/cpp/vectorcall?view=msvc-160
2029 // > On ARM and x64 processors, __stdcall is accepted and ignored by the compiler;
2031 // -- https://docs.microsoft.com/en-us/cpp/cpp/stdcall?view=msvc-160
2033 // > In most cases, keywords or compiler switches that specify an unsupported
2034 // > convention on a particular platform are ignored, and the platform default
2035 // > convention is used.
2037 // -- https://docs.microsoft.com/en-us/cpp/cpp/argument-passing-and-naming-conventions
2038 Stdcall { .. } | Fastcall { .. } | Vectorcall { .. } if self.is_like_windows => true,
2039 // Outside of Windows we want to only support these calling conventions for the
2040 // architectures for which these calling conventions are actually well defined.
2041 Stdcall { .. } | Fastcall { .. } if self.arch == "x86" => true,
2042 Vectorcall { .. } if ["x86", "x86_64"].contains(&&self.arch[..]) => true,
2043 // Return a `None` for other cases so that we know to emit a future compat lint.
2044 Stdcall { .. } | Fastcall { .. } | Vectorcall { .. } => return None,
2048 /// Minimum integer size in bits that this target can perform atomic
2050 pub fn min_atomic_width(&self) -> u64 {
2051 self.min_atomic_width.unwrap_or(8)
2054 /// Maximum integer size in bits that this target can perform atomic
2056 pub fn max_atomic_width(&self) -> u64 {
2057 self.max_atomic_width.unwrap_or_else(|| self.pointer_width.into())
2060 /// Loads a target descriptor from a JSON object.
2061 pub fn from_json(obj: Json) -> Result<(Target, TargetWarnings), String> {
2062 // While ugly, this code must remain this way to retain
2063 // compatibility with existing JSON fields and the internal
2064 // expected naming of the Target and TargetOptions structs.
2065 // To ensure compatibility is retained, the built-in targets
2066 // are round-tripped through this code to catch cases where
2067 // the JSON parser is not updated to match the structs.
2069 let mut obj = match obj {
2070 Value::Object(obj) => obj,
2071 _ => return Err("Expected JSON object for target")?,
2074 let mut get_req_field = |name: &str| {
2076 .and_then(|j| j.as_str().map(str::to_string))
2077 .ok_or_else(|| format!("Field {name} in target specification is required"))
2080 let mut base = Target {
2081 llvm_target: get_req_field("llvm-target")?.into(),
2082 pointer_width: get_req_field("target-pointer-width")?
2084 .map_err(|_| "target-pointer-width must be an integer".to_string())?,
2085 data_layout: get_req_field("data-layout")?.into(),
2086 arch: get_req_field("arch")?.into(),
2087 options: Default::default(),
2090 let mut incorrect_type = vec![];
2093 ($key_name:ident) => ( {
2094 let name = (stringify!($key_name)).replace("_", "-");
2095 if let Some(s) = obj.remove(&name).and_then(|s| s.as_str().map(str::to_string).map(Cow::from)) {
2099 ($key_name:ident = $json_name:expr) => ( {
2100 let name = $json_name;
2101 if let Some(s) = obj.remove(name).and_then(|s| s.as_str().map(str::to_string).map(Cow::from)) {
2105 ($key_name:ident, bool) => ( {
2106 let name = (stringify!($key_name)).replace("_", "-");
2107 if let Some(s) = obj.remove(&name).and_then(|b| b.as_bool()) {
2111 ($key_name:ident = $json_name:expr, bool) => ( {
2112 let name = $json_name;
2113 if let Some(s) = obj.remove(name).and_then(|b| b.as_bool()) {
2117 ($key_name:ident, u64) => ( {
2118 let name = (stringify!($key_name)).replace("_", "-");
2119 if let Some(s) = obj.remove(&name).and_then(|j| Json::as_u64(&j)) {
2123 ($key_name:ident, u32) => ( {
2124 let name = (stringify!($key_name)).replace("_", "-");
2125 if let Some(s) = obj.remove(&name).and_then(|b| b.as_u64()) {
2127 return Err("Not a valid DWARF version number".into());
2129 base.$key_name = s as u32;
2132 ($key_name:ident, Option<u64>) => ( {
2133 let name = (stringify!($key_name)).replace("_", "-");
2134 if let Some(s) = obj.remove(&name).and_then(|b| b.as_u64()) {
2135 base.$key_name = Some(s);
2138 ($key_name:ident, MergeFunctions) => ( {
2139 let name = (stringify!($key_name)).replace("_", "-");
2140 obj.remove(&name).and_then(|o| o.as_str().and_then(|s| {
2141 match s.parse::<MergeFunctions>() {
2142 Ok(mergefunc) => base.$key_name = mergefunc,
2143 _ => return Some(Err(format!("'{}' is not a valid value for \
2144 merge-functions. Use 'disabled', \
2145 'trampolines', or 'aliases'.",
2149 })).unwrap_or(Ok(()))
2151 ($key_name:ident, RelocModel) => ( {
2152 let name = (stringify!($key_name)).replace("_", "-");
2153 obj.remove(&name).and_then(|o| o.as_str().and_then(|s| {
2154 match s.parse::<RelocModel>() {
2155 Ok(relocation_model) => base.$key_name = relocation_model,
2156 _ => return Some(Err(format!("'{}' is not a valid relocation model. \
2157 Run `rustc --print relocation-models` to \
2158 see the list of supported values.", s))),
2161 })).unwrap_or(Ok(()))
2163 ($key_name:ident, CodeModel) => ( {
2164 let name = (stringify!($key_name)).replace("_", "-");
2165 obj.remove(&name).and_then(|o| o.as_str().and_then(|s| {
2166 match s.parse::<CodeModel>() {
2167 Ok(code_model) => base.$key_name = Some(code_model),
2168 _ => return Some(Err(format!("'{}' is not a valid code model. \
2169 Run `rustc --print code-models` to \
2170 see the list of supported values.", s))),
2173 })).unwrap_or(Ok(()))
2175 ($key_name:ident, TlsModel) => ( {
2176 let name = (stringify!($key_name)).replace("_", "-");
2177 obj.remove(&name).and_then(|o| o.as_str().and_then(|s| {
2178 match s.parse::<TlsModel>() {
2179 Ok(tls_model) => base.$key_name = tls_model,
2180 _ => return Some(Err(format!("'{}' is not a valid TLS model. \
2181 Run `rustc --print tls-models` to \
2182 see the list of supported values.", s))),
2185 })).unwrap_or(Ok(()))
2187 ($key_name:ident, PanicStrategy) => ( {
2188 let name = (stringify!($key_name)).replace("_", "-");
2189 obj.remove(&name).and_then(|o| o.as_str().and_then(|s| {
2191 "unwind" => base.$key_name = PanicStrategy::Unwind,
2192 "abort" => base.$key_name = PanicStrategy::Abort,
2193 _ => return Some(Err(format!("'{}' is not a valid value for \
2194 panic-strategy. Use 'unwind' or 'abort'.",
2198 })).unwrap_or(Ok(()))
2200 ($key_name:ident, RelroLevel) => ( {
2201 let name = (stringify!($key_name)).replace("_", "-");
2202 obj.remove(&name).and_then(|o| o.as_str().and_then(|s| {
2203 match s.parse::<RelroLevel>() {
2204 Ok(level) => base.$key_name = level,
2205 _ => return Some(Err(format!("'{}' is not a valid value for \
2206 relro-level. Use 'full', 'partial, or 'off'.",
2210 })).unwrap_or(Ok(()))
2212 ($key_name:ident, DebuginfoKind) => ( {
2213 let name = (stringify!($key_name)).replace("_", "-");
2214 obj.remove(&name).and_then(|o| o.as_str().and_then(|s| {
2215 match s.parse::<DebuginfoKind>() {
2216 Ok(level) => base.$key_name = level,
2217 _ => return Some(Err(
2218 format!("'{s}' is not a valid value for debuginfo-kind. Use 'dwarf', \
2219 'dwarf-dsym' or 'pdb'.")
2223 })).unwrap_or(Ok(()))
2225 ($key_name:ident, SplitDebuginfo) => ( {
2226 let name = (stringify!($key_name)).replace("_", "-");
2227 obj.remove(&name).and_then(|o| o.as_str().and_then(|s| {
2228 match s.parse::<SplitDebuginfo>() {
2229 Ok(level) => base.$key_name = level,
2230 _ => return Some(Err(format!("'{}' is not a valid value for \
2231 split-debuginfo. Use 'off' or 'dsymutil'.",
2235 })).unwrap_or(Ok(()))
2237 ($key_name:ident, list) => ( {
2238 let name = (stringify!($key_name)).replace("_", "-");
2239 if let Some(j) = obj.remove(&name) {
2240 if let Some(v) = j.as_array() {
2241 base.$key_name = v.iter()
2242 .map(|a| a.as_str().unwrap().to_string().into())
2245 incorrect_type.push(name)
2249 ($key_name:ident, opt_list) => ( {
2250 let name = (stringify!($key_name)).replace("_", "-");
2251 if let Some(j) = obj.remove(&name) {
2252 if let Some(v) = j.as_array() {
2253 base.$key_name = Some(v.iter()
2254 .map(|a| a.as_str().unwrap().to_string().into())
2257 incorrect_type.push(name)
2261 ($key_name:ident, falliable_list) => ( {
2262 let name = (stringify!($key_name)).replace("_", "-");
2263 obj.remove(&name).and_then(|j| {
2264 if let Some(v) = j.as_array() {
2265 match v.iter().map(|a| FromStr::from_str(a.as_str().unwrap())).collect() {
2266 Ok(l) => { base.$key_name = l },
2267 // FIXME: `falliable_list` can't re-use the `key!` macro for list
2268 // elements and the error messages from that macro, so it has a bad
2269 // generic message instead
2270 Err(_) => return Some(Err(
2271 format!("`{:?}` is not a valid value for `{}`", j, name)
2275 incorrect_type.push(name)
2278 }).unwrap_or(Ok(()))
2280 ($key_name:ident, optional) => ( {
2281 let name = (stringify!($key_name)).replace("_", "-");
2282 if let Some(o) = obj.remove(&name) {
2285 .map(|s| s.to_string().into());
2288 ($key_name:ident = $json_name:expr, LldFlavor) => ( {
2289 let name = $json_name;
2290 obj.remove(name).and_then(|o| o.as_str().and_then(|s| {
2291 if let Some(flavor) = LldFlavor::from_str(&s) {
2292 base.$key_name = flavor;
2294 return Some(Err(format!(
2295 "'{}' is not a valid value for lld-flavor. \
2296 Use 'darwin', 'gnu', 'link' or 'wasm'.",
2300 })).unwrap_or(Ok(()))
2302 ($key_name:ident = $json_name:expr, LinkerFlavor) => ( {
2303 let name = $json_name;
2304 obj.remove(name).and_then(|o| o.as_str().and_then(|s| {
2305 match LinkerFlavorCli::from_str(s) {
2306 Some(linker_flavor) => base.$key_name = linker_flavor,
2307 _ => return Some(Err(format!("'{}' is not a valid value for linker-flavor. \
2308 Use {}", s, LinkerFlavorCli::one_of()))),
2311 })).unwrap_or(Ok(()))
2313 ($key_name:ident, StackProbeType) => ( {
2314 let name = (stringify!($key_name)).replace("_", "-");
2315 obj.remove(&name).and_then(|o| match StackProbeType::from_json(&o) {
2321 format!("`{:?}` is not a valid value for `{}`: {}", o, name, s)
2323 }).unwrap_or(Ok(()))
2325 ($key_name:ident, SanitizerSet) => ( {
2326 let name = (stringify!($key_name)).replace("_", "-");
2327 if let Some(o) = obj.remove(&name) {
2328 if let Some(a) = o.as_array() {
2330 base.$key_name |= match s.as_str() {
2331 Some("address") => SanitizerSet::ADDRESS,
2332 Some("cfi") => SanitizerSet::CFI,
2333 Some("kcfi") => SanitizerSet::KCFI,
2334 Some("leak") => SanitizerSet::LEAK,
2335 Some("memory") => SanitizerSet::MEMORY,
2336 Some("memtag") => SanitizerSet::MEMTAG,
2337 Some("shadow-call-stack") => SanitizerSet::SHADOWCALLSTACK,
2338 Some("thread") => SanitizerSet::THREAD,
2339 Some("hwaddress") => SanitizerSet::HWADDRESS,
2340 Some(s) => return Err(format!("unknown sanitizer {}", s)),
2341 _ => return Err(format!("not a string: {:?}", s)),
2345 incorrect_type.push(name)
2348 Ok::<(), String>(())
2351 ($key_name:ident = $json_name:expr, link_self_contained) => ( {
2352 let name = $json_name;
2353 obj.remove(name).and_then(|o| o.as_str().and_then(|s| {
2354 match s.parse::<LinkSelfContainedDefault>() {
2355 Ok(lsc_default) => base.$key_name = lsc_default,
2356 _ => return Some(Err(format!("'{}' is not a valid `-Clink-self-contained` default. \
2357 Use 'false', 'true', 'musl' or 'mingw'", s))),
2360 })).unwrap_or(Ok(()))
2362 ($key_name:ident = $json_name:expr, link_objects) => ( {
2363 let name = $json_name;
2364 if let Some(val) = obj.remove(name) {
2365 let obj = val.as_object().ok_or_else(|| format!("{}: expected a \
2366 JSON object with fields per CRT object kind.", name))?;
2367 let mut args = CrtObjects::new();
2369 let kind = LinkOutputKind::from_str(&k).ok_or_else(|| {
2370 format!("{}: '{}' is not a valid value for CRT object kind. \
2371 Use '(dynamic,static)-(nopic,pic)-exe' or \
2372 '(dynamic,static)-dylib' or 'wasi-reactor-exe'", name, k)
2375 let v = v.as_array().ok_or_else(||
2376 format!("{}.{}: expected a JSON array", name, k)
2377 )?.iter().enumerate()
2379 let s = s.as_str().ok_or_else(||
2380 format!("{}.{}[{}]: expected a JSON string", name, k, i))?;
2381 Ok(s.to_string().into())
2383 .collect::<Result<Vec<_>, String>>()?;
2385 args.insert(kind, v);
2387 base.$key_name = args;
2390 ($key_name:ident = $json_name:expr, link_args) => ( {
2391 let name = $json_name;
2392 if let Some(val) = obj.remove(name) {
2393 let obj = val.as_object().ok_or_else(|| format!("{}: expected a \
2394 JSON object with fields per linker-flavor.", name))?;
2395 let mut args = LinkArgsCli::new();
2397 let flavor = LinkerFlavorCli::from_str(&k).ok_or_else(|| {
2398 format!("{}: '{}' is not a valid value for linker-flavor. \
2399 Use 'em', 'gcc', 'ld' or 'msvc'", name, k)
2402 let v = v.as_array().ok_or_else(||
2403 format!("{}.{}: expected a JSON array", name, k)
2404 )?.iter().enumerate()
2406 let s = s.as_str().ok_or_else(||
2407 format!("{}.{}[{}]: expected a JSON string", name, k, i))?;
2408 Ok(s.to_string().into())
2410 .collect::<Result<Vec<_>, String>>()?;
2412 args.insert(flavor, v);
2414 base.$key_name = args;
2417 ($key_name:ident, env) => ( {
2418 let name = (stringify!($key_name)).replace("_", "-");
2419 if let Some(o) = obj.remove(&name) {
2420 if let Some(a) = o.as_array() {
2422 if let Some(s) = o.as_str() {
2423 let p = s.split('=').collect::<Vec<_>>();
2425 let k = p[0].to_string();
2426 let v = p[1].to_string();
2427 base.$key_name.to_mut().push((k.into(), v.into()));
2432 incorrect_type.push(name)
2436 ($key_name:ident, Option<Abi>) => ( {
2437 let name = (stringify!($key_name)).replace("_", "-");
2438 obj.remove(&name).and_then(|o| o.as_str().and_then(|s| {
2439 match lookup_abi(s) {
2440 Some(abi) => base.$key_name = Some(abi),
2441 _ => return Some(Err(format!("'{}' is not a valid value for abi", s))),
2444 })).unwrap_or(Ok(()))
2446 ($key_name:ident, TargetFamilies) => ( {
2447 if let Some(value) = obj.remove("target-family") {
2448 if let Some(v) = value.as_array() {
2449 base.$key_name = v.iter()
2450 .map(|a| a.as_str().unwrap().to_string().into())
2452 } else if let Some(v) = value.as_str() {
2453 base.$key_name = vec![v.to_string().into()].into();
2457 ($key_name:ident, Conv) => ( {
2458 let name = (stringify!($key_name)).replace("_", "-");
2459 obj.remove(&name).and_then(|o| o.as_str().and_then(|s| {
2460 match Conv::from_str(s) {
2465 Err(e) => Some(Err(e))
2467 })).unwrap_or(Ok(()))
2471 if let Some(j) = obj.remove("target-endian") {
2472 if let Some(s) = j.as_str() {
2473 base.endian = s.parse()?;
2475 incorrect_type.push("target-endian".into())
2479 if let Some(fp) = obj.remove("frame-pointer") {
2480 if let Some(s) = fp.as_str() {
2481 base.frame_pointer = s
2483 .map_err(|()| format!("'{s}' is not a valid value for frame-pointer"))?;
2485 incorrect_type.push("frame-pointer".into())
2489 key!(is_builtin, bool);
2490 key!(c_int_width = "target-c-int-width");
2495 key!(linker, optional);
2496 key!(linker_flavor_json = "linker-flavor", LinkerFlavor)?;
2497 key!(lld_flavor_json = "lld-flavor", LldFlavor)?;
2498 key!(linker_is_gnu_json = "linker-is-gnu", bool);
2499 key!(pre_link_objects = "pre-link-objects", link_objects);
2500 key!(post_link_objects = "post-link-objects", link_objects);
2501 key!(pre_link_objects_self_contained = "pre-link-objects-fallback", link_objects);
2502 key!(post_link_objects_self_contained = "post-link-objects-fallback", link_objects);
2503 key!(link_self_contained = "crt-objects-fallback", link_self_contained)?;
2504 key!(pre_link_args_json = "pre-link-args", link_args);
2505 key!(late_link_args_json = "late-link-args", link_args);
2506 key!(late_link_args_dynamic_json = "late-link-args-dynamic", link_args);
2507 key!(late_link_args_static_json = "late-link-args-static", link_args);
2508 key!(post_link_args_json = "post-link-args", link_args);
2509 key!(link_script, optional);
2510 key!(link_env, env);
2511 key!(link_env_remove, list);
2512 key!(asm_args, list);
2515 key!(dynamic_linking, bool);
2516 key!(only_cdylib, bool);
2517 key!(executables, bool);
2518 key!(relocation_model, RelocModel)?;
2519 key!(code_model, CodeModel)?;
2520 key!(tls_model, TlsModel)?;
2521 key!(disable_redzone, bool);
2522 key!(function_sections, bool);
2526 key!(staticlib_prefix);
2527 key!(staticlib_suffix);
2528 key!(families, TargetFamilies);
2529 key!(abi_return_struct_as_int, bool);
2530 key!(is_like_aix, bool);
2531 key!(is_like_osx, bool);
2532 key!(is_like_solaris, bool);
2533 key!(is_like_windows, bool);
2534 key!(is_like_msvc, bool);
2535 key!(is_like_wasm, bool);
2536 key!(is_like_android, bool);
2537 key!(default_dwarf_version, u32);
2538 key!(allows_weak_linkage, bool);
2539 key!(has_rpath, bool);
2540 key!(no_default_libraries, bool);
2541 key!(position_independent_executables, bool);
2542 key!(static_position_independent_executables, bool);
2543 key!(needs_plt, bool);
2544 key!(relro_level, RelroLevel)?;
2545 key!(archive_format);
2546 key!(allow_asm, bool);
2547 key!(main_needs_argc_argv, bool);
2548 key!(has_thread_local, bool);
2549 key!(obj_is_bitcode, bool);
2550 key!(forces_embed_bitcode, bool);
2551 key!(bitcode_llvm_cmdline);
2552 key!(max_atomic_width, Option<u64>);
2553 key!(min_atomic_width, Option<u64>);
2554 key!(atomic_cas, bool);
2555 key!(panic_strategy, PanicStrategy)?;
2556 key!(crt_static_allows_dylibs, bool);
2557 key!(crt_static_default, bool);
2558 key!(crt_static_respected, bool);
2559 key!(stack_probes, StackProbeType)?;
2560 key!(min_global_align, Option<u64>);
2561 key!(default_codegen_units, Option<u64>);
2562 key!(trap_unreachable, bool);
2563 key!(requires_lto, bool);
2564 key!(singlethread, bool);
2565 key!(no_builtins, bool);
2566 key!(default_hidden_visibility, bool);
2567 key!(emit_debug_gdb_scripts, bool);
2568 key!(requires_uwtable, bool);
2569 key!(default_uwtable, bool);
2570 key!(simd_types_indirect, bool);
2571 key!(limit_rdylib_exports, bool);
2572 key!(override_export_symbols, opt_list);
2573 key!(merge_functions, MergeFunctions)?;
2574 key!(mcount = "target-mcount");
2576 key!(relax_elf_relocations, bool);
2577 key!(llvm_args, list);
2578 key!(use_ctors_section, bool);
2579 key!(eh_frame_header, bool);
2580 key!(has_thumb_interworking, bool);
2581 key!(debuginfo_kind, DebuginfoKind)?;
2582 key!(split_debuginfo, SplitDebuginfo)?;
2583 key!(supported_split_debuginfo, falliable_list)?;
2584 key!(supported_sanitizers, SanitizerSet)?;
2585 key!(default_adjusted_cabi, Option<Abi>)?;
2586 key!(c_enum_min_bits, u64);
2587 key!(generate_arange_section, bool);
2588 key!(supports_stack_protector, bool);
2590 key!(entry_abi, Conv)?;
2592 if base.is_builtin {
2593 // This can cause unfortunate ICEs later down the line.
2594 return Err("may not set is_builtin for targets not built-in".into());
2596 base.update_from_cli();
2598 // Each field should have been read using `Json::remove` so any keys remaining are unused.
2599 let remaining_keys = obj.keys();
2602 TargetWarnings { unused_fields: remaining_keys.cloned().collect(), incorrect_type },
2606 /// Load a built-in target
2607 pub fn expect_builtin(target_triple: &TargetTriple) -> Target {
2608 match *target_triple {
2609 TargetTriple::TargetTriple(ref target_triple) => {
2610 load_builtin(target_triple).expect("built-in target")
2612 TargetTriple::TargetJson { .. } => {
2613 panic!("built-in targets doesn't support target-paths")
2618 /// Search for a JSON file specifying the given target triple.
2620 /// If none is found in `$RUST_TARGET_PATH`, look for a file called `target.json` inside the
2621 /// sysroot under the target-triple's `rustlib` directory. Note that it could also just be a
2622 /// bare filename already, so also check for that. If one of the hardcoded targets we know
2623 /// about, just return it directly.
2625 /// The error string could come from any of the APIs called, including filesystem access and
2628 target_triple: &TargetTriple,
2630 ) -> Result<(Target, TargetWarnings), String> {
2634 fn load_file(path: &Path) -> Result<(Target, TargetWarnings), String> {
2635 let contents = fs::read_to_string(path).map_err(|e| e.to_string())?;
2636 let obj = serde_json::from_str(&contents).map_err(|e| e.to_string())?;
2637 Target::from_json(obj)
2640 match *target_triple {
2641 TargetTriple::TargetTriple(ref target_triple) => {
2642 // check if triple is in list of built-in targets
2643 if let Some(t) = load_builtin(target_triple) {
2644 return Ok((t, TargetWarnings::empty()));
2647 // search for a file named `target_triple`.json in RUST_TARGET_PATH
2649 let mut target = target_triple.to_string();
2650 target.push_str(".json");
2651 PathBuf::from(target)
2654 let target_path = env::var_os("RUST_TARGET_PATH").unwrap_or_default();
2656 for dir in env::split_paths(&target_path) {
2657 let p = dir.join(&path);
2659 return load_file(&p);
2663 // Additionally look in the sysroot under `lib/rustlib/<triple>/target.json`
2665 let rustlib_path = crate::target_rustlib_path(sysroot, target_triple);
2666 let p = PathBuf::from_iter([
2668 Path::new(&rustlib_path),
2669 Path::new("target.json"),
2672 return load_file(&p);
2675 Err(format!("Could not find specification for target {target_triple:?}"))
2677 TargetTriple::TargetJson { ref contents, .. } => {
2678 let obj = serde_json::from_str(contents).map_err(|e| e.to_string())?;
2679 Target::from_json(obj)
2685 impl ToJson for Target {
2686 fn to_json(&self) -> Json {
2687 let mut d = serde_json::Map::new();
2688 let default: TargetOptions = Default::default();
2689 let mut target = self.clone();
2690 target.update_to_cli();
2692 macro_rules! target_val {
2694 let name = (stringify!($attr)).replace("_", "-");
2695 d.insert(name, target.$attr.to_json());
2699 macro_rules! target_option_val {
2701 let name = (stringify!($attr)).replace("_", "-");
2702 if default.$attr != target.$attr {
2703 d.insert(name, target.$attr.to_json());
2706 ($attr:ident, $json_name:expr) => {{
2707 let name = $json_name;
2708 if default.$attr != target.$attr {
2709 d.insert(name.into(), target.$attr.to_json());
2712 (link_args - $attr:ident, $json_name:expr) => {{
2713 let name = $json_name;
2714 if default.$attr != target.$attr {
2718 .map(|(k, v)| (k.desc().to_string(), v.clone()))
2719 .collect::<BTreeMap<_, _>>();
2720 d.insert(name.to_string(), obj.to_json());
2723 (env - $attr:ident) => {{
2724 let name = (stringify!($attr)).replace("_", "-");
2725 if default.$attr != target.$attr {
2729 .map(|&(ref k, ref v)| format!("{k}={v}"))
2730 .collect::<Vec<_>>();
2731 d.insert(name, obj.to_json());
2736 target_val!(llvm_target);
2737 d.insert("target-pointer-width".to_string(), self.pointer_width.to_string().to_json());
2739 target_val!(data_layout);
2741 target_option_val!(is_builtin);
2742 target_option_val!(endian, "target-endian");
2743 target_option_val!(c_int_width, "target-c-int-width");
2744 target_option_val!(os);
2745 target_option_val!(env);
2746 target_option_val!(abi);
2747 target_option_val!(vendor);
2748 target_option_val!(linker);
2749 target_option_val!(linker_flavor_json, "linker-flavor");
2750 target_option_val!(lld_flavor_json, "lld-flavor");
2751 target_option_val!(linker_is_gnu_json, "linker-is-gnu");
2752 target_option_val!(pre_link_objects);
2753 target_option_val!(post_link_objects);
2754 target_option_val!(pre_link_objects_self_contained, "pre-link-objects-fallback");
2755 target_option_val!(post_link_objects_self_contained, "post-link-objects-fallback");
2756 target_option_val!(link_self_contained, "crt-objects-fallback");
2757 target_option_val!(link_args - pre_link_args_json, "pre-link-args");
2758 target_option_val!(link_args - late_link_args_json, "late-link-args");
2759 target_option_val!(link_args - late_link_args_dynamic_json, "late-link-args-dynamic");
2760 target_option_val!(link_args - late_link_args_static_json, "late-link-args-static");
2761 target_option_val!(link_args - post_link_args_json, "post-link-args");
2762 target_option_val!(link_script);
2763 target_option_val!(env - link_env);
2764 target_option_val!(link_env_remove);
2765 target_option_val!(asm_args);
2766 target_option_val!(cpu);
2767 target_option_val!(features);
2768 target_option_val!(dynamic_linking);
2769 target_option_val!(only_cdylib);
2770 target_option_val!(executables);
2771 target_option_val!(relocation_model);
2772 target_option_val!(code_model);
2773 target_option_val!(tls_model);
2774 target_option_val!(disable_redzone);
2775 target_option_val!(frame_pointer);
2776 target_option_val!(function_sections);
2777 target_option_val!(dll_prefix);
2778 target_option_val!(dll_suffix);
2779 target_option_val!(exe_suffix);
2780 target_option_val!(staticlib_prefix);
2781 target_option_val!(staticlib_suffix);
2782 target_option_val!(families, "target-family");
2783 target_option_val!(abi_return_struct_as_int);
2784 target_option_val!(is_like_aix);
2785 target_option_val!(is_like_osx);
2786 target_option_val!(is_like_solaris);
2787 target_option_val!(is_like_windows);
2788 target_option_val!(is_like_msvc);
2789 target_option_val!(is_like_wasm);
2790 target_option_val!(is_like_android);
2791 target_option_val!(default_dwarf_version);
2792 target_option_val!(allows_weak_linkage);
2793 target_option_val!(has_rpath);
2794 target_option_val!(no_default_libraries);
2795 target_option_val!(position_independent_executables);
2796 target_option_val!(static_position_independent_executables);
2797 target_option_val!(needs_plt);
2798 target_option_val!(relro_level);
2799 target_option_val!(archive_format);
2800 target_option_val!(allow_asm);
2801 target_option_val!(main_needs_argc_argv);
2802 target_option_val!(has_thread_local);
2803 target_option_val!(obj_is_bitcode);
2804 target_option_val!(forces_embed_bitcode);
2805 target_option_val!(bitcode_llvm_cmdline);
2806 target_option_val!(min_atomic_width);
2807 target_option_val!(max_atomic_width);
2808 target_option_val!(atomic_cas);
2809 target_option_val!(panic_strategy);
2810 target_option_val!(crt_static_allows_dylibs);
2811 target_option_val!(crt_static_default);
2812 target_option_val!(crt_static_respected);
2813 target_option_val!(stack_probes);
2814 target_option_val!(min_global_align);
2815 target_option_val!(default_codegen_units);
2816 target_option_val!(trap_unreachable);
2817 target_option_val!(requires_lto);
2818 target_option_val!(singlethread);
2819 target_option_val!(no_builtins);
2820 target_option_val!(default_hidden_visibility);
2821 target_option_val!(emit_debug_gdb_scripts);
2822 target_option_val!(requires_uwtable);
2823 target_option_val!(default_uwtable);
2824 target_option_val!(simd_types_indirect);
2825 target_option_val!(limit_rdylib_exports);
2826 target_option_val!(override_export_symbols);
2827 target_option_val!(merge_functions);
2828 target_option_val!(mcount, "target-mcount");
2829 target_option_val!(llvm_abiname);
2830 target_option_val!(relax_elf_relocations);
2831 target_option_val!(llvm_args);
2832 target_option_val!(use_ctors_section);
2833 target_option_val!(eh_frame_header);
2834 target_option_val!(has_thumb_interworking);
2835 target_option_val!(debuginfo_kind);
2836 target_option_val!(split_debuginfo);
2837 target_option_val!(supported_split_debuginfo);
2838 target_option_val!(supported_sanitizers);
2839 target_option_val!(c_enum_min_bits);
2840 target_option_val!(generate_arange_section);
2841 target_option_val!(supports_stack_protector);
2842 target_option_val!(entry_name);
2843 target_option_val!(entry_abi);
2845 if let Some(abi) = self.default_adjusted_cabi {
2846 d.insert("default-adjusted-cabi".into(), Abi::name(abi).to_json());
2853 /// Either a target triple string or a path to a JSON file.
2854 #[derive(Clone, Debug)]
2855 pub enum TargetTriple {
2856 TargetTriple(String),
2858 /// Warning: This field may only be used by rustdoc. Using it anywhere else will lead to
2859 /// inconsistencies as it is discarded during serialization.
2860 path_for_rustdoc: PathBuf,
2866 // Use a manual implementation to ignore the path field
2867 impl PartialEq for TargetTriple {
2868 fn eq(&self, other: &Self) -> bool {
2869 match (self, other) {
2870 (Self::TargetTriple(l0), Self::TargetTriple(r0)) => l0 == r0,
2872 Self::TargetJson { path_for_rustdoc: _, triple: l_triple, contents: l_contents },
2873 Self::TargetJson { path_for_rustdoc: _, triple: r_triple, contents: r_contents },
2874 ) => l_triple == r_triple && l_contents == r_contents,
2880 // Use a manual implementation to ignore the path field
2881 impl Hash for TargetTriple {
2882 fn hash<H: Hasher>(&self, state: &mut H) -> () {
2884 TargetTriple::TargetTriple(triple) => {
2888 TargetTriple::TargetJson { path_for_rustdoc: _, triple, contents } => {
2891 contents.hash(state)
2897 // Use a manual implementation to prevent encoding the target json file path in the crate metadata
2898 impl<S: Encoder> Encodable<S> for TargetTriple {
2899 fn encode(&self, s: &mut S) {
2901 TargetTriple::TargetTriple(triple) => s.emit_enum_variant(0, |s| s.emit_str(triple)),
2902 TargetTriple::TargetJson { path_for_rustdoc: _, triple, contents } => s
2903 .emit_enum_variant(1, |s| {
2905 s.emit_str(contents)
2911 impl<D: Decoder> Decodable<D> for TargetTriple {
2912 fn decode(d: &mut D) -> Self {
2913 match d.read_usize() {
2914 0 => TargetTriple::TargetTriple(d.read_str().to_owned()),
2915 1 => TargetTriple::TargetJson {
2916 path_for_rustdoc: PathBuf::new(),
2917 triple: d.read_str().to_owned(),
2918 contents: d.read_str().to_owned(),
2921 panic!("invalid enum variant tag while decoding `TargetTriple`, expected 0..2");
2928 /// Creates a target triple from the passed target triple string.
2929 pub fn from_triple(triple: &str) -> Self {
2930 TargetTriple::TargetTriple(triple.into())
2933 /// Creates a target triple from the passed target path.
2934 pub fn from_path(path: &Path) -> Result<Self, io::Error> {
2935 let canonicalized_path = path.canonicalize()?;
2936 let contents = std::fs::read_to_string(&canonicalized_path).map_err(|err| {
2938 io::ErrorKind::InvalidInput,
2939 format!("target path {canonicalized_path:?} is not a valid file: {err}"),
2942 let triple = canonicalized_path
2944 .expect("target path must not be empty")
2946 .expect("target path must be valid unicode")
2948 Ok(TargetTriple::TargetJson { path_for_rustdoc: canonicalized_path, triple, contents })
2951 /// Returns a string triple for this target.
2953 /// If this target is a path, the file name (without extension) is returned.
2954 pub fn triple(&self) -> &str {
2956 TargetTriple::TargetTriple(ref triple)
2957 | TargetTriple::TargetJson { ref triple, .. } => triple,
2961 /// Returns an extended string triple for this target.
2963 /// If this target is a path, a hash of the path is appended to the triple returned
2965 pub fn debug_triple(&self) -> String {
2966 use std::collections::hash_map::DefaultHasher;
2969 TargetTriple::TargetTriple(triple) => triple.to_owned(),
2970 TargetTriple::TargetJson { path_for_rustdoc: _, triple, contents: content } => {
2971 let mut hasher = DefaultHasher::new();
2972 content.hash(&mut hasher);
2973 let hash = hasher.finish();
2974 format!("{triple}-{hash}")
2980 impl fmt::Display for TargetTriple {
2981 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2982 write!(f, "{}", self.debug_triple())