1//! [Flexible target specification.](https://github.com/rust-lang/rfcs/pull/131)2//!3//! Rust targets a wide variety of usecases, and in the interest of flexibility,4//! allows new target tuples to be defined in configuration files. Most users5//! will not need to care about these, but this is invaluable when porting Rust6//! to a new platform, and allows for an unprecedented level of control over how7//! the compiler works.8//!9//! # Using targets and target.json10//!11//! Invoking "rustc --target=${TUPLE}" will result in rustc initiating the [`Target::search`] by12//! - checking if "$TUPLE" is a complete path to a json (ending with ".json") and loading if so13//! - checking builtin targets for "${TUPLE}"14//! - checking directories in "${RUST_TARGET_PATH}" for "${TUPLE}.json"15//! - checking for "${RUSTC_SYSROOT}/lib/rustlib/${TUPLE}/target.json"16//!17//! Code will then be compiled using the first discovered target spec.18//!19//! # Defining a new target20//!21//! Targets are defined using a struct which additionally has serialization to and from [JSON].22//! The `Target` struct in this module loosely corresponds with the format the JSON takes.23//! We usually try to make the fields equivalent but we have given up on a 1:1 correspondence24//! between the JSON and the actual structure itself.25//!26//! Some fields are required in every target spec, and they should be embedded in Target directly.27//! Optional keys are in TargetOptions, but Target derefs to it, for no practical difference.28//! Most notable is the "data-layout" field which specifies Rust's notion of sizes and alignments29//! for several key types, such as f64, pointers, and so on.30//!31//! At one point we felt `-C` options should override the target's settings, like in C compilers,32//! but that was an essentially-unmarked route for making code incorrect and Rust unsound.33//! Confronted with programmers who prefer a compiler with a good UX instead of a lethal weapon,34//! we have almost-entirely recanted that notion, though we hope "target modifiers" will offer35//! a way to have a decent UX yet still extend the necessary compiler controls, without36//! requiring a new target spec for each and every single possible target micro-variant.37//!38//! [JSON]: https://json.org3940use core::result::Result;41use std::borrow::Cow;42use std::collections::BTreeMap;43use std::hash::{Hash, Hasher};44use std::ops::{Deref, DerefMut};45use std::path::{Path, PathBuf};46use std::str::FromStr;47use std::{fmt, io};4849use rustc_abi::{50 Align, CVariadicStatus, CanonAbi, Endian, ExternAbi, Integer, Size, TargetDataLayout,51 TargetDataLayoutError,52};53use rustc_data_structures::fx::{FxHashSet, FxIndexSet};54use rustc_error_messages::{DiagArgValue, IntoDiagArg, into_diag_arg_using_display};55use rustc_fs_util::try_canonicalize;56use rustc_macros::{BlobDecodable, Decodable, Encodable, StableHash};57use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};58use rustc_span::{Symbol, kw, sym};59use serde_json::Value;60use tracing::debug;6162use crate::json::{Json, ToJson};63use crate::spec::crt_objects::CrtObjects;6465pub mod crt_objects;6667mod abi_map;68mod base;69mod json;7071pub use abi_map::{AbiMap, AbiMapping};72pub use base::apple;73pub use base::avr::ef_avr_arch;74pub use json::json_schema;7576/// Linker is called through a C/C++ compiler.77#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]78pub enum Cc {79 Yes,80 No,81}8283/// Linker is LLD.84#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]85pub enum Lld {86 Yes,87 No,88}8990/// All linkers have some kinds of command line interfaces and rustc needs to know which commands91/// to use with each of them. So we cluster all such interfaces into a (somewhat arbitrary) number92/// of classes that we call "linker flavors".93///94/// Technically, it's not even necessary, we can nearly always infer the flavor from linker name95/// and target properties like `is_like_windows`/`is_like_darwin`/etc. However, the PRs originally96/// introducing `-Clinker-flavor` (#40018 and friends) were aiming to reduce this kind of inference97/// and provide something certain and explicitly specified instead, and that design goal is still98/// relevant now.99///100/// The second goal is to keep the number of flavors to the minimum if possible.101/// LLD somewhat forces our hand here because that linker is self-sufficient only if its executable102/// (`argv[0]`) is named in specific way, otherwise it doesn't work and requires a103/// `-flavor LLD_FLAVOR` argument to choose which logic to use. Our shipped `rust-lld` in104/// particular is not named in such specific way, so it needs the flavor option, so we make our105/// linker flavors sufficiently fine-grained to satisfy LLD without inferring its flavor from other106/// target properties, in accordance with the first design goal.107///108/// The first component of the flavor is tightly coupled with the compilation target,109/// while the `Cc` and `Lld` flags can vary within the same target.110#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]111pub enum LinkerFlavor {112 /// Unix-like linker with GNU extensions (both naked and compiler-wrapped forms).113 /// Besides similar "default" Linux/BSD linkers this also includes Windows/GNU linker,114 /// which is somewhat different because it doesn't produce ELFs.115 Gnu(Cc, Lld),116 /// Unix-like linker for Apple targets (both naked and compiler-wrapped forms).117 /// Extracted from the "umbrella" `Unix` flavor due to its corresponding LLD flavor.118 Darwin(Cc, Lld),119 /// Unix-like linker for Wasm targets (both naked and compiler-wrapped forms).120 /// Extracted from the "umbrella" `Unix` flavor due to its corresponding LLD flavor.121 /// Non-LLD version does not exist, so the lld flag is currently hardcoded here.122 WasmLld(Cc),123 /// Basic Unix-like linker for "any other Unix" targets (Solaris/illumos, L4Re, MSP430, etc),124 /// possibly with non-GNU extensions (both naked and compiler-wrapped forms).125 /// LLD doesn't support any of these.126 Unix(Cc),127 /// MSVC-style linker for Windows and UEFI, LLD supports it.128 Msvc(Lld),129 /// Emscripten Compiler Frontend, a wrapper around `WasmLld(Cc::Yes)` that has a different130 /// interface and produces some additional JavaScript output.131 EmCc,132 // Below: other linker-like tools with unique interfaces for exotic targets.133 /// Linker tool for BPF.134 Bpf,135 /// Linker tool for Nvidia PTX.136 Ptx,137 /// LLVM bitcode linker that can be used as a `self-contained` linker138 Llbc,139}140141/// Linker flavors available externally through command line (`-Clinker-flavor`)142/// or json target specifications.143/// This set has accumulated historically, and contains both (stable and unstable) legacy values, as144/// well as modern ones matching the internal linker flavors (`LinkerFlavor`).145#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]146pub enum LinkerFlavorCli {147 // Modern (unstable) flavors, with direct counterparts in `LinkerFlavor`.148 Gnu(Cc, Lld),149 Darwin(Cc, Lld),150 WasmLld(Cc),151 Unix(Cc),152 // Note: `Msvc(Lld::No)` is also a stable value.153 Msvc(Lld),154 EmCc,155 Bpf,156 Ptx,157 Llbc,158159 // Legacy stable values160 Gcc,161 Ld,162 Lld(LldFlavor),163 Em,164}165166impl LinkerFlavorCli {167 /// Returns whether this `-C linker-flavor` option is one of the unstable values.168 pub fn is_unstable(&self) -> bool {169 match self {170 LinkerFlavorCli::Gnu(..)171 | LinkerFlavorCli::Darwin(..)172 | LinkerFlavorCli::WasmLld(..)173 | LinkerFlavorCli::Unix(..)174 | LinkerFlavorCli::Msvc(Lld::Yes)175 | LinkerFlavorCli::EmCc176 | LinkerFlavorCli::Bpf177 | LinkerFlavorCli::Llbc178 | LinkerFlavorCli::Ptx => true,179 LinkerFlavorCli::Gcc180 | LinkerFlavorCli::Ld181 | LinkerFlavorCli::Lld(..)182 | LinkerFlavorCli::Msvc(Lld::No)183 | LinkerFlavorCli::Em => false,184 }185 }186}187188crate::target_spec_enum! {189 pub enum LldFlavor {190 Wasm = "wasm",191 Ld64 = "darwin",192 Ld = "gnu",193 Link = "link",194 }195196 parse_error_type = "LLD flavor";197}198199impl LinkerFlavor {200 /// At this point the target's reference linker flavor doesn't yet exist and we need to infer201 /// it. The inference always succeeds and gives some result, and we don't report any flavor202 /// incompatibility errors for json target specs. The CLI flavor is used as the main source203 /// of truth, other flags are used in case of ambiguities.204 fn from_cli_json(cli: LinkerFlavorCli, lld_flavor: LldFlavor, is_gnu: bool) -> LinkerFlavor {205 match cli {206 LinkerFlavorCli::Gnu(cc, lld) => LinkerFlavor::Gnu(cc, lld),207 LinkerFlavorCli::Darwin(cc, lld) => LinkerFlavor::Darwin(cc, lld),208 LinkerFlavorCli::WasmLld(cc) => LinkerFlavor::WasmLld(cc),209 LinkerFlavorCli::Unix(cc) => LinkerFlavor::Unix(cc),210 LinkerFlavorCli::Msvc(lld) => LinkerFlavor::Msvc(lld),211 LinkerFlavorCli::EmCc => LinkerFlavor::EmCc,212 LinkerFlavorCli::Bpf => LinkerFlavor::Bpf,213 LinkerFlavorCli::Llbc => LinkerFlavor::Llbc,214 LinkerFlavorCli::Ptx => LinkerFlavor::Ptx,215216 // Below: legacy stable values217 LinkerFlavorCli::Gcc => match lld_flavor {218 LldFlavor::Ld if is_gnu => LinkerFlavor::Gnu(Cc::Yes, Lld::No),219 LldFlavor::Ld64 => LinkerFlavor::Darwin(Cc::Yes, Lld::No),220 LldFlavor::Wasm => LinkerFlavor::WasmLld(Cc::Yes),221 LldFlavor::Ld | LldFlavor::Link => LinkerFlavor::Unix(Cc::Yes),222 },223 LinkerFlavorCli::Ld => match lld_flavor {224 LldFlavor::Ld if is_gnu => LinkerFlavor::Gnu(Cc::No, Lld::No),225 LldFlavor::Ld64 => LinkerFlavor::Darwin(Cc::No, Lld::No),226 LldFlavor::Ld | LldFlavor::Wasm | LldFlavor::Link => LinkerFlavor::Unix(Cc::No),227 },228 LinkerFlavorCli::Lld(LldFlavor::Ld) => LinkerFlavor::Gnu(Cc::No, Lld::Yes),229 LinkerFlavorCli::Lld(LldFlavor::Ld64) => LinkerFlavor::Darwin(Cc::No, Lld::Yes),230 LinkerFlavorCli::Lld(LldFlavor::Wasm) => LinkerFlavor::WasmLld(Cc::No),231 LinkerFlavorCli::Lld(LldFlavor::Link) => LinkerFlavor::Msvc(Lld::Yes),232 LinkerFlavorCli::Em => LinkerFlavor::EmCc,233 }234 }235236 /// Returns the corresponding backwards-compatible CLI flavor.237 fn to_cli(self) -> LinkerFlavorCli {238 match self {239 LinkerFlavor::Gnu(Cc::Yes, _)240 | LinkerFlavor::Darwin(Cc::Yes, _)241 | LinkerFlavor::WasmLld(Cc::Yes)242 | LinkerFlavor::Unix(Cc::Yes) => LinkerFlavorCli::Gcc,243 LinkerFlavor::Gnu(_, Lld::Yes) => LinkerFlavorCli::Lld(LldFlavor::Ld),244 LinkerFlavor::Darwin(_, Lld::Yes) => LinkerFlavorCli::Lld(LldFlavor::Ld64),245 LinkerFlavor::WasmLld(..) => LinkerFlavorCli::Lld(LldFlavor::Wasm),246 LinkerFlavor::Gnu(..) | LinkerFlavor::Darwin(..) | LinkerFlavor::Unix(..) => {247 LinkerFlavorCli::Ld248 }249 LinkerFlavor::Msvc(Lld::Yes) => LinkerFlavorCli::Lld(LldFlavor::Link),250 LinkerFlavor::Msvc(..) => LinkerFlavorCli::Msvc(Lld::No),251 LinkerFlavor::EmCc => LinkerFlavorCli::Em,252 LinkerFlavor::Bpf => LinkerFlavorCli::Bpf,253 LinkerFlavor::Llbc => LinkerFlavorCli::Llbc,254 LinkerFlavor::Ptx => LinkerFlavorCli::Ptx,255 }256 }257258 /// Returns the modern CLI flavor that is the counterpart of this flavor.259 fn to_cli_counterpart(self) -> LinkerFlavorCli {260 match self {261 LinkerFlavor::Gnu(cc, lld) => LinkerFlavorCli::Gnu(cc, lld),262 LinkerFlavor::Darwin(cc, lld) => LinkerFlavorCli::Darwin(cc, lld),263 LinkerFlavor::WasmLld(cc) => LinkerFlavorCli::WasmLld(cc),264 LinkerFlavor::Unix(cc) => LinkerFlavorCli::Unix(cc),265 LinkerFlavor::Msvc(lld) => LinkerFlavorCli::Msvc(lld),266 LinkerFlavor::EmCc => LinkerFlavorCli::EmCc,267 LinkerFlavor::Bpf => LinkerFlavorCli::Bpf,268 LinkerFlavor::Llbc => LinkerFlavorCli::Llbc,269 LinkerFlavor::Ptx => LinkerFlavorCli::Ptx,270 }271 }272273 fn infer_cli_hints(cli: LinkerFlavorCli) -> (Option<Cc>, Option<Lld>) {274 match cli {275 LinkerFlavorCli::Gnu(cc, lld) | LinkerFlavorCli::Darwin(cc, lld) => {276 (Some(cc), Some(lld))277 }278 LinkerFlavorCli::WasmLld(cc) => (Some(cc), Some(Lld::Yes)),279 LinkerFlavorCli::Unix(cc) => (Some(cc), None),280 LinkerFlavorCli::Msvc(lld) => (Some(Cc::No), Some(lld)),281 LinkerFlavorCli::EmCc => (Some(Cc::Yes), Some(Lld::Yes)),282 LinkerFlavorCli::Bpf | LinkerFlavorCli::Ptx => (None, None),283 LinkerFlavorCli::Llbc => (None, None),284285 // Below: legacy stable values286 LinkerFlavorCli::Gcc => (Some(Cc::Yes), None),287 LinkerFlavorCli::Ld => (Some(Cc::No), Some(Lld::No)),288 LinkerFlavorCli::Lld(_) => (Some(Cc::No), Some(Lld::Yes)),289 LinkerFlavorCli::Em => (Some(Cc::Yes), Some(Lld::Yes)),290 }291 }292293 fn infer_linker_hints(linker_stem: &str) -> Result<Self, (Option<Cc>, Option<Lld>)> {294 // Remove any version postfix.295 let stem = linker_stem296 .rsplit_once('-')297 .and_then(|(lhs, rhs)| rhs.chars().all(char::is_numeric).then_some(lhs))298 .unwrap_or(linker_stem);299300 if stem == "llvm-bitcode-linker" {301 Ok(Self::Llbc)302 } else if stem == "emcc" // GCC/Clang can have an optional target prefix.303 || stem == "gcc"304 || stem.ends_with("-gcc")305 || stem == "g++"306 || stem.ends_with("-g++")307 || stem == "clang"308 || stem.ends_with("-clang")309 || stem == "clang++"310 || stem.ends_with("-clang++")311 {312 Err((Some(Cc::Yes), Some(Lld::No)))313 } else if stem == "wasm-ld"314 || stem.ends_with("-wasm-ld")315 || stem == "ld.lld"316 || stem == "lld"317 || stem == "rust-lld"318 || stem == "lld-link"319 {320 Err((Some(Cc::No), Some(Lld::Yes)))321 } else if stem == "ld" || stem.ends_with("-ld") || stem == "link" {322 Err((Some(Cc::No), Some(Lld::No)))323 } else {324 Err((None, None))325 }326 }327328 fn with_hints(self, (cc_hint, lld_hint): (Option<Cc>, Option<Lld>)) -> LinkerFlavor {329 match self {330 LinkerFlavor::Gnu(cc, lld) => {331 LinkerFlavor::Gnu(cc_hint.unwrap_or(cc), lld_hint.unwrap_or(lld))332 }333 LinkerFlavor::Darwin(cc, lld) => {334 LinkerFlavor::Darwin(cc_hint.unwrap_or(cc), lld_hint.unwrap_or(lld))335 }336 LinkerFlavor::WasmLld(cc) => LinkerFlavor::WasmLld(cc_hint.unwrap_or(cc)),337 LinkerFlavor::Unix(cc) => LinkerFlavor::Unix(cc_hint.unwrap_or(cc)),338 LinkerFlavor::Msvc(lld) => LinkerFlavor::Msvc(lld_hint.unwrap_or(lld)),339 LinkerFlavor::EmCc | LinkerFlavor::Bpf | LinkerFlavor::Llbc | LinkerFlavor::Ptx => self,340 }341 }342343 pub fn with_cli_hints(self, cli: LinkerFlavorCli) -> LinkerFlavor {344 self.with_hints(LinkerFlavor::infer_cli_hints(cli))345 }346347 pub fn with_linker_hints(self, linker_stem: &str) -> LinkerFlavor {348 match LinkerFlavor::infer_linker_hints(linker_stem) {349 Ok(linker_flavor) => linker_flavor,350 Err(hints) => self.with_hints(hints),351 }352 }353354 pub fn check_compatibility(self, cli: LinkerFlavorCli) -> Option<String> {355 let compatible = |cli| {356 // The CLI flavor should be compatible with the target if:357 match (self, cli) {358 // 1. they are counterparts: they have the same principal flavor.359 (LinkerFlavor::Gnu(..), LinkerFlavorCli::Gnu(..))360 | (LinkerFlavor::Darwin(..), LinkerFlavorCli::Darwin(..))361 | (LinkerFlavor::WasmLld(..), LinkerFlavorCli::WasmLld(..))362 | (LinkerFlavor::Unix(..), LinkerFlavorCli::Unix(..))363 | (LinkerFlavor::Msvc(..), LinkerFlavorCli::Msvc(..))364 | (LinkerFlavor::EmCc, LinkerFlavorCli::EmCc)365 | (LinkerFlavor::Bpf, LinkerFlavorCli::Bpf)366 | (LinkerFlavor::Llbc, LinkerFlavorCli::Llbc)367 | (LinkerFlavor::Ptx, LinkerFlavorCli::Ptx) => return true,368 // 2. The linker flavor is independent of target and compatible369 (LinkerFlavor::Ptx, LinkerFlavorCli::Llbc) => return true,370 _ => {}371 }372373 // 3. or, the flavor is legacy and survives this roundtrip.374 cli == self.with_cli_hints(cli).to_cli()375 };376 (!compatible(cli)).then(|| {377 LinkerFlavorCli::all()378 .iter()379 .filter(|cli| compatible(**cli))380 .map(|cli| cli.desc())381 .intersperse(", ")382 .collect()383 })384 }385386 pub fn lld_flavor(self) -> LldFlavor {387 match self {388 LinkerFlavor::Gnu(..)389 | LinkerFlavor::Unix(..)390 | LinkerFlavor::EmCc391 | LinkerFlavor::Bpf392 | LinkerFlavor::Llbc393 | LinkerFlavor::Ptx => LldFlavor::Ld,394 LinkerFlavor::Darwin(..) => LldFlavor::Ld64,395 LinkerFlavor::WasmLld(..) => LldFlavor::Wasm,396 LinkerFlavor::Msvc(..) => LldFlavor::Link,397 }398 }399400 pub fn is_gnu(self) -> bool {401 matches!(self, LinkerFlavor::Gnu(..))402 }403404 /// Returns whether the flavor uses the `lld` linker.405 pub fn uses_lld(self) -> bool {406 // Exhaustive match in case new flavors are added in the future.407 match self {408 LinkerFlavor::Gnu(_, Lld::Yes)409 | LinkerFlavor::Darwin(_, Lld::Yes)410 | LinkerFlavor::WasmLld(..)411 | LinkerFlavor::EmCc412 | LinkerFlavor::Msvc(Lld::Yes) => true,413 LinkerFlavor::Gnu(..)414 | LinkerFlavor::Darwin(..)415 | LinkerFlavor::Msvc(_)416 | LinkerFlavor::Unix(_)417 | LinkerFlavor::Bpf418 | LinkerFlavor::Llbc419 | LinkerFlavor::Ptx => false,420 }421 }422423 /// Returns whether the flavor calls the linker via a C/C++ compiler.424 pub fn uses_cc(self) -> bool {425 // Exhaustive match in case new flavors are added in the future.426 match self {427 LinkerFlavor::Gnu(Cc::Yes, _)428 | LinkerFlavor::Darwin(Cc::Yes, _)429 | LinkerFlavor::WasmLld(Cc::Yes)430 | LinkerFlavor::Unix(Cc::Yes)431 | LinkerFlavor::EmCc => true,432 LinkerFlavor::Gnu(..)433 | LinkerFlavor::Darwin(..)434 | LinkerFlavor::WasmLld(_)435 | LinkerFlavor::Msvc(_)436 | LinkerFlavor::Unix(_)437 | LinkerFlavor::Bpf438 | LinkerFlavor::Llbc439 | LinkerFlavor::Ptx => false,440 }441 }442443 /// For flavors with an `Lld` component, ensure it's enabled. Otherwise, returns the given444 /// flavor unmodified.445 pub fn with_lld_enabled(self) -> LinkerFlavor {446 match self {447 LinkerFlavor::Gnu(cc, Lld::No) => LinkerFlavor::Gnu(cc, Lld::Yes),448 LinkerFlavor::Darwin(cc, Lld::No) => LinkerFlavor::Darwin(cc, Lld::Yes),449 LinkerFlavor::Msvc(Lld::No) => LinkerFlavor::Msvc(Lld::Yes),450 _ => self,451 }452 }453454 /// For flavors with an `Lld` component, ensure it's disabled. Otherwise, returns the given455 /// flavor unmodified.456 pub fn with_lld_disabled(self) -> LinkerFlavor {457 match self {458 LinkerFlavor::Gnu(cc, Lld::Yes) => LinkerFlavor::Gnu(cc, Lld::No),459 LinkerFlavor::Darwin(cc, Lld::Yes) => LinkerFlavor::Darwin(cc, Lld::No),460 LinkerFlavor::Msvc(Lld::Yes) => LinkerFlavor::Msvc(Lld::No),461 _ => self,462 }463 }464}465466macro_rules! linker_flavor_cli_impls {467 ($(($($flavor:tt)*) $string:literal)*) => (468 impl LinkerFlavorCli {469 const fn all() -> &'static [LinkerFlavorCli] {470 &[$($($flavor)*,)*]471 }472473 pub const fn one_of() -> &'static str {474 concat!("one of: ", $($string, " ",)*)475 }476477 pub fn desc(self) -> &'static str {478 match self {479 $($($flavor)* => $string,)*480 }481 }482 }483484 impl FromStr for LinkerFlavorCli {485 type Err = String;486487 fn from_str(s: &str) -> Result<LinkerFlavorCli, Self::Err> {488 Ok(match s {489 $($string => $($flavor)*,)*490 _ => return Err(format!("invalid linker flavor, allowed values: {}", Self::one_of())),491 })492 }493 }494 )495}496497linker_flavor_cli_impls! {498 (LinkerFlavorCli::Gnu(Cc::No, Lld::No)) "gnu"499 (LinkerFlavorCli::Gnu(Cc::No, Lld::Yes)) "gnu-lld"500 (LinkerFlavorCli::Gnu(Cc::Yes, Lld::No)) "gnu-cc"501 (LinkerFlavorCli::Gnu(Cc::Yes, Lld::Yes)) "gnu-lld-cc"502 (LinkerFlavorCli::Darwin(Cc::No, Lld::No)) "darwin"503 (LinkerFlavorCli::Darwin(Cc::No, Lld::Yes)) "darwin-lld"504 (LinkerFlavorCli::Darwin(Cc::Yes, Lld::No)) "darwin-cc"505 (LinkerFlavorCli::Darwin(Cc::Yes, Lld::Yes)) "darwin-lld-cc"506 (LinkerFlavorCli::WasmLld(Cc::No)) "wasm-lld"507 (LinkerFlavorCli::WasmLld(Cc::Yes)) "wasm-lld-cc"508 (LinkerFlavorCli::Unix(Cc::No)) "unix"509 (LinkerFlavorCli::Unix(Cc::Yes)) "unix-cc"510 (LinkerFlavorCli::Msvc(Lld::Yes)) "msvc-lld"511 (LinkerFlavorCli::Msvc(Lld::No)) "msvc"512 (LinkerFlavorCli::EmCc) "em-cc"513 (LinkerFlavorCli::Bpf) "bpf"514 (LinkerFlavorCli::Llbc) "llbc"515 (LinkerFlavorCli::Ptx) "ptx"516517 // Legacy stable flavors518 (LinkerFlavorCli::Gcc) "gcc"519 (LinkerFlavorCli::Ld) "ld"520 (LinkerFlavorCli::Lld(LldFlavor::Ld)) "ld.lld"521 (LinkerFlavorCli::Lld(LldFlavor::Ld64)) "ld64.lld"522 (LinkerFlavorCli::Lld(LldFlavor::Link)) "lld-link"523 (LinkerFlavorCli::Lld(LldFlavor::Wasm)) "wasm-ld"524 (LinkerFlavorCli::Em) "em"525}526527crate::json::serde_deserialize_from_str!(LinkerFlavorCli);528impl schemars::JsonSchema for LinkerFlavorCli {529 fn schema_name() -> std::borrow::Cow<'static, str> {530 "LinkerFlavor".into()531 }532 fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {533 let all: Vec<&'static str> =534 Self::all().iter().map(|flavor| flavor.desc()).collect::<Vec<_>>();535 schemars::json_schema! ({536 "type": "string",537 "enum": all538 })539 .into()540 }541}542543impl ToJson for LinkerFlavorCli {544 fn to_json(&self) -> Json {545 self.desc().to_json()546 }547}548549/// The different `-Clink-self-contained` options that can be specified in a target spec:550/// - enabling or disabling in bulk551/// - some target-specific pieces of inference to determine whether to use self-contained linking552/// if `-Clink-self-contained` is not specified explicitly (e.g. on musl/mingw)553/// - explicitly enabling some of the self-contained linking components, e.g. the linker component554/// to use `rust-lld`555#[derive(Clone, Copy, PartialEq, Debug)]556pub enum LinkSelfContainedDefault {557 /// The target spec explicitly enables self-contained linking.558 True,559560 /// The target spec explicitly disables self-contained linking.561 False,562563 /// The target spec requests that the self-contained mode is inferred, in the context of musl.564 InferredForMusl,565566 /// The target spec requests that the self-contained mode is inferred, in the context of mingw.567 InferredForMingw,568569 /// The target spec explicitly enables a list of self-contained linking components: e.g. for570 /// targets opting into a subset of components like the CLI's `-C link-self-contained=+linker`.571 WithComponents(LinkSelfContainedComponents),572}573574/// Parses a backwards-compatible `-Clink-self-contained` option string, without components.575impl FromStr for LinkSelfContainedDefault {576 type Err = String;577578 fn from_str(s: &str) -> Result<LinkSelfContainedDefault, Self::Err> {579 Ok(match s {580 "false" => LinkSelfContainedDefault::False,581 "true" | "wasm" => LinkSelfContainedDefault::True,582 "musl" => LinkSelfContainedDefault::InferredForMusl,583 "mingw" => LinkSelfContainedDefault::InferredForMingw,584 _ => {585 return Err(format!(586 "'{s}' is not a valid `-Clink-self-contained` default. \587 Use 'false', 'true', 'wasm', 'musl' or 'mingw'",588 ));589 }590 })591 }592}593594crate::json::serde_deserialize_from_str!(LinkSelfContainedDefault);595impl schemars::JsonSchema for LinkSelfContainedDefault {596 fn schema_name() -> std::borrow::Cow<'static, str> {597 "LinkSelfContainedDefault".into()598 }599 fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {600 schemars::json_schema! ({601 "type": "string",602 "enum": ["false", "true", "wasm", "musl", "mingw"]603 })604 .into()605 }606}607608impl ToJson for LinkSelfContainedDefault {609 fn to_json(&self) -> Json {610 match *self {611 LinkSelfContainedDefault::WithComponents(components) => {612 // Serialize the components in a json object's `components` field, to prepare for a613 // future where `crt-objects-fallback` is removed from the json specs and614 // incorporated as a field here.615 let mut map = BTreeMap::new();616 map.insert("components", components);617 map.to_json()618 }619620 // Stable backwards-compatible values621 LinkSelfContainedDefault::True => "true".to_json(),622 LinkSelfContainedDefault::False => "false".to_json(),623 LinkSelfContainedDefault::InferredForMusl => "musl".to_json(),624 LinkSelfContainedDefault::InferredForMingw => "mingw".to_json(),625 }626 }627}628629impl LinkSelfContainedDefault {630 /// Returns whether the target spec has self-contained linking explicitly disabled. Used to emit631 /// errors if the user then enables it on the CLI.632 pub fn is_disabled(self) -> bool {633 self == LinkSelfContainedDefault::False634 }635636 /// Returns the key to use when serializing the setting to json:637 /// - individual components in a `link-self-contained` object value638 /// - the other variants as a backwards-compatible `crt-objects-fallback` string639 fn json_key(self) -> &'static str {640 match self {641 LinkSelfContainedDefault::WithComponents(_) => "link-self-contained",642 _ => "crt-objects-fallback",643 }644 }645646 /// Creates a `LinkSelfContainedDefault` enabling the self-contained linker for target specs647 /// (the equivalent of `-Clink-self-contained=+linker` on the CLI).648 pub fn with_linker() -> LinkSelfContainedDefault {649 LinkSelfContainedDefault::WithComponents(LinkSelfContainedComponents::LINKER)650 }651}652653bitflags::bitflags! {654 #[derive(Clone, Copy, PartialEq, Eq, Default)]655 /// The `-C link-self-contained` components that can individually be enabled or disabled.656 pub struct LinkSelfContainedComponents: u8 {657 /// CRT objects (e.g. on `windows-gnu`, `musl`, `wasi` targets)658 const CRT_OBJECTS = 1 << 0;659 /// libc static library (e.g. on `musl`, `wasi` targets)660 const LIBC = 1 << 1;661 /// libgcc/libunwind (e.g. on `windows-gnu`, `fuchsia`, `fortanix`, `gnullvm` targets)662 const UNWIND = 1 << 2;663 /// Linker, dlltool, and their necessary libraries (e.g. on `windows-gnu` and for `rust-lld`)664 const LINKER = 1 << 3;665 /// Sanitizer runtime libraries666 const SANITIZERS = 1 << 4;667 /// Other MinGW libs and Windows import libs668 const MINGW = 1 << 5;669 }670}671rustc_data_structures::external_bitflags_debug! { LinkSelfContainedComponents }672673impl LinkSelfContainedComponents {674 /// Return the component's name.675 ///676 /// Returns `None` if the bitflags aren't a singular component (but a mix of multiple flags).677 pub fn as_str(self) -> Option<&'static str> {678 Some(match self {679 LinkSelfContainedComponents::CRT_OBJECTS => "crto",680 LinkSelfContainedComponents::LIBC => "libc",681 LinkSelfContainedComponents::UNWIND => "unwind",682 LinkSelfContainedComponents::LINKER => "linker",683 LinkSelfContainedComponents::SANITIZERS => "sanitizers",684 LinkSelfContainedComponents::MINGW => "mingw",685 _ => return None,686 })687 }688689 /// Returns an array of all the components.690 fn all_components() -> [LinkSelfContainedComponents; 6] {691 [692 LinkSelfContainedComponents::CRT_OBJECTS,693 LinkSelfContainedComponents::LIBC,694 LinkSelfContainedComponents::UNWIND,695 LinkSelfContainedComponents::LINKER,696 LinkSelfContainedComponents::SANITIZERS,697 LinkSelfContainedComponents::MINGW,698 ]699 }700701 /// Returns whether at least a component is enabled.702 pub fn are_any_components_enabled(self) -> bool {703 !self.is_empty()704 }705706 /// Returns whether `LinkSelfContainedComponents::LINKER` is enabled.707 pub fn is_linker_enabled(self) -> bool {708 self.contains(LinkSelfContainedComponents::LINKER)709 }710711 /// Returns whether `LinkSelfContainedComponents::CRT_OBJECTS` is enabled.712 pub fn is_crt_objects_enabled(self) -> bool {713 self.contains(LinkSelfContainedComponents::CRT_OBJECTS)714 }715}716717impl FromStr for LinkSelfContainedComponents {718 type Err = String;719720 /// Parses a single `-Clink-self-contained` well-known component, not a set of flags.721 fn from_str(s: &str) -> Result<Self, Self::Err> {722 Ok(match s {723 "crto" => LinkSelfContainedComponents::CRT_OBJECTS,724 "libc" => LinkSelfContainedComponents::LIBC,725 "unwind" => LinkSelfContainedComponents::UNWIND,726 "linker" => LinkSelfContainedComponents::LINKER,727 "sanitizers" => LinkSelfContainedComponents::SANITIZERS,728 "mingw" => LinkSelfContainedComponents::MINGW,729 _ => {730 return Err(format!(731 "'{s}' is not a valid link-self-contained component, expected 'crto', 'libc', 'unwind', 'linker', 'sanitizers', 'mingw'"732 ));733 }734 })735 }736}737738crate::json::serde_deserialize_from_str!(LinkSelfContainedComponents);739impl schemars::JsonSchema for LinkSelfContainedComponents {740 fn schema_name() -> std::borrow::Cow<'static, str> {741 "LinkSelfContainedComponents".into()742 }743 fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {744 let all =745 Self::all_components().iter().map(|component| component.as_str()).collect::<Vec<_>>();746 schemars::json_schema! ({747 "type": "string",748 "enum": all,749 })750 .into()751 }752}753754impl ToJson for LinkSelfContainedComponents {755 fn to_json(&self) -> Json {756 let components: Vec<_> = Self::all_components()757 .into_iter()758 .filter(|c| self.contains(*c))759 .map(|c| {760 // We can unwrap because we're iterating over all the known singular components,761 // not an actual set of flags where `as_str` can fail.762 c.as_str().unwrap().to_owned()763 })764 .collect();765766 components.to_json()767 }768}769770bitflags::bitflags! {771 /// The `-C linker-features` components that can individually be enabled or disabled.772 ///773 /// They are feature flags intended to be a more flexible mechanism than linker flavors, and774 /// also to prevent a combinatorial explosion of flavors whenever a new linker feature is775 /// required. These flags are "generic", in the sense that they can work on multiple targets on776 /// the CLI. Otherwise, one would have to select different linkers flavors for each target.777 ///778 /// Here are some examples of the advantages they offer:779 /// - default feature sets for principal flavors, or for specific targets.780 /// - flavor-specific features: for example, clang offers automatic cross-linking with781 /// `--target`, which gcc-style compilers don't support. The *flavor* is still a C/C++782 /// compiler, and we don't need to multiply the number of flavors for this use-case. Instead,783 /// we can have a single `+target` feature.784 /// - umbrella features: for example if clang accumulates more features in the future than just785 /// the `+target` above. That could be modeled as `+clang`.786 /// - niche features for resolving specific issues: for example, on Apple targets the linker787 /// flag implementing the `as-needed` native link modifier (#99424) is only possible on788 /// sufficiently recent linker versions.789 /// - still allows for discovery and automation, for example via feature detection. This can be790 /// useful in exotic environments/build systems.791 #[derive(Clone, Copy, PartialEq, Eq, Default)]792 pub struct LinkerFeatures: u8 {793 /// Invoke the linker via a C/C++ compiler (e.g. on most unix targets).794 const CC = 1 << 0;795 /// Use the lld linker, either the system lld or the self-contained linker `rust-lld`.796 const LLD = 1 << 1;797 }798}799rustc_data_structures::external_bitflags_debug! { LinkerFeatures }800801impl LinkerFeatures {802 /// Parses a single `-C linker-features` well-known feature, not a set of flags.803 pub fn from_str(s: &str) -> Option<LinkerFeatures> {804 Some(match s {805 "cc" => LinkerFeatures::CC,806 "lld" => LinkerFeatures::LLD,807 _ => return None,808 })809 }810811 /// Return the linker feature name, as would be passed on the CLI.812 ///813 /// Returns `None` if the bitflags aren't a singular component (but a mix of multiple flags).814 pub fn as_str(self) -> Option<&'static str> {815 Some(match self {816 LinkerFeatures::CC => "cc",817 LinkerFeatures::LLD => "lld",818 _ => return None,819 })820 }821822 /// Returns whether the `lld` linker feature is enabled.823 pub fn is_lld_enabled(self) -> bool {824 self.contains(LinkerFeatures::LLD)825 }826827 /// Returns whether the `cc` linker feature is enabled.828 pub fn is_cc_enabled(self) -> bool {829 self.contains(LinkerFeatures::CC)830 }831}832833crate::target_spec_enum! {834 #[derive(Encodable, BlobDecodable, StableHash)]835 pub enum PanicStrategy {836 Unwind = "unwind",837 Abort = "abort",838 ImmediateAbort = "immediate-abort",839 }840841 parse_error_type = "panic strategy";842}843844#[derive(Clone, Copy, Debug, PartialEq, Hash, Encodable, BlobDecodable, StableHash)]845pub enum OnBrokenPipe {846 Default,847 Kill,848 Error,849 Inherit,850}851852impl PanicStrategy {853 pub const fn desc_symbol(&self) -> Symbol {854 match *self {855 PanicStrategy::Unwind => sym::unwind,856 PanicStrategy::Abort => sym::abort,857 PanicStrategy::ImmediateAbort => sym::immediate_abort,858 }859 }860861 pub fn unwinds(self) -> bool {862 matches!(self, PanicStrategy::Unwind)863 }864}865866crate::target_spec_enum! {867 pub enum RelroLevel {868 Full = "full",869 Partial = "partial",870 Off = "off",871 None = "none",872 }873874 parse_error_type = "relro level";875}876877impl IntoDiagArg for PanicStrategy {878 fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {879 DiagArgValue::Str(Cow::Owned(self.desc().to_string()))880 }881}882883crate::target_spec_enum! {884 pub enum SymbolVisibility {885 Hidden = "hidden",886 Protected = "protected",887 Interposable = "interposable",888 }889890 parse_error_type = "symbol visibility";891}892893#[derive(Clone, Debug, PartialEq, Hash)]894pub enum SmallDataThresholdSupport {895 None,896 DefaultForArch,897 LlvmModuleFlag(StaticCow<str>),898 LlvmArg(StaticCow<str>),899}900901impl FromStr for SmallDataThresholdSupport {902 type Err = String;903904 fn from_str(s: &str) -> Result<Self, Self::Err> {905 if s == "none" {906 Ok(Self::None)907 } else if s == "default-for-arch" {908 Ok(Self::DefaultForArch)909 } else if let Some(flag) = s.strip_prefix("llvm-module-flag=") {910 Ok(Self::LlvmModuleFlag(flag.to_string().into()))911 } else if let Some(arg) = s.strip_prefix("llvm-arg=") {912 Ok(Self::LlvmArg(arg.to_string().into()))913 } else {914 Err(format!("'{s}' is not a valid value for small-data-threshold-support."))915 }916 }917}918919crate::json::serde_deserialize_from_str!(SmallDataThresholdSupport);920impl schemars::JsonSchema for SmallDataThresholdSupport {921 fn schema_name() -> std::borrow::Cow<'static, str> {922 "SmallDataThresholdSupport".into()923 }924 fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {925 schemars::json_schema! ({926 "type": "string",927 "pattern": r#"^none|default-for-arch|llvm-module-flag=.+|llvm-arg=.+$"#,928 })929 .into()930 }931}932933impl ToJson for SmallDataThresholdSupport {934 fn to_json(&self) -> Value {935 match self {936 Self::None => "none".to_json(),937 Self::DefaultForArch => "default-for-arch".to_json(),938 Self::LlvmModuleFlag(flag) => format!("llvm-module-flag={flag}").to_json(),939 Self::LlvmArg(arg) => format!("llvm-arg={arg}").to_json(),940 }941 }942}943944crate::target_spec_enum! {945 pub enum MergeFunctions {946 Disabled = "disabled",947 Trampolines = "trampolines",948 Aliases = "aliases",949 }950951 parse_error_type = "value for merge-functions";952}953954crate::target_spec_enum! {955 pub enum RelocModel {956 Static = "static",957 Pic = "pic",958 Pie = "pie",959 DynamicNoPic = "dynamic-no-pic",960 Ropi = "ropi",961 Rwpi = "rwpi",962 RopiRwpi = "ropi-rwpi",963 }964965 parse_error_type = "relocation model";966}967968impl RelocModel {969 pub const fn desc_symbol(&self) -> Symbol {970 match *self {971 RelocModel::Static => kw::Static,972 RelocModel::Pic => sym::pic,973 RelocModel::Pie => sym::pie,974 RelocModel::DynamicNoPic => sym::dynamic_no_pic,975 RelocModel::Ropi => sym::ropi,976 RelocModel::Rwpi => sym::rwpi,977 RelocModel::RopiRwpi => sym::ropi_rwpi,978 }979 }980}981982crate::target_spec_enum! {983 pub enum CodeModel {984 Tiny = "tiny",985 Small = "small",986 Kernel = "kernel",987 Medium = "medium",988 Large = "large",989 }990991 parse_error_type = "code model";992}993994crate::target_spec_enum! {995 /// The float ABI setting to be configured in the LLVM target machine.996 pub enum FloatAbi {997 Soft = "soft",998 Hard = "hard",999 }10001001 parse_error_type = "float abi";1002}10031004crate::target_spec_enum! {1005 /// The Rustc-specific variant of the ABI used for this target.1006 pub enum RustcAbi {1007 /// On x86-32 only: make use of SSE and SSE2 for ABI purposes.1008 X86Sse2 = "x86-sse2",1009 /// On x86-32/64, aarch64, and S390x: do not use any FPU or SIMD registers for the ABI.1010 Softfloat = "softfloat", "x86-softfloat",1011 }10121013 parse_error_type = "rustc abi";1014}10151016crate::target_spec_enum! {1017 pub enum TlsModel {1018 GeneralDynamic = "global-dynamic",1019 LocalDynamic = "local-dynamic",1020 InitialExec = "initial-exec",1021 LocalExec = "local-exec",1022 Emulated = "emulated",1023 }10241025 parse_error_type = "TLS model";1026}10271028crate::target_spec_enum! {1029 /// Everything is flattened to a single enum to make the json encoding/decoding less annoying.1030 pub enum LinkOutputKind {1031 /// Dynamically linked non position-independent executable.1032 DynamicNoPicExe = "dynamic-nopic-exe",1033 /// Dynamically linked position-independent executable.1034 DynamicPicExe = "dynamic-pic-exe",1035 /// Statically linked non position-independent executable.1036 StaticNoPicExe = "static-nopic-exe",1037 /// Statically linked position-independent executable.1038 StaticPicExe = "static-pic-exe",1039 /// Regular dynamic library ("dynamically linked").1040 DynamicDylib = "dynamic-dylib",1041 /// Dynamic library with bundled libc ("statically linked").1042 StaticDylib = "static-dylib",1043 /// WASI module with a lifetime past the _initialize entry point1044 WasiReactorExe = "wasi-reactor-exe",1045 }10461047 parse_error_type = "CRT object kind";1048}10491050impl LinkOutputKind {1051 pub fn can_link_dylib(self) -> bool {1052 match self {1053 LinkOutputKind::StaticNoPicExe | LinkOutputKind::StaticPicExe => false,1054 LinkOutputKind::DynamicNoPicExe1055 | LinkOutputKind::DynamicPicExe1056 | LinkOutputKind::DynamicDylib1057 | LinkOutputKind::StaticDylib1058 | LinkOutputKind::WasiReactorExe => true,1059 }1060 }1061}10621063pub type LinkArgs = BTreeMap<LinkerFlavor, Vec<StaticCow<str>>>;1064pub type LinkArgsCli = BTreeMap<LinkerFlavorCli, Vec<StaticCow<str>>>;10651066crate::target_spec_enum! {1067 /// Which kind of debuginfo does the target use?1068 ///1069 /// Useful in determining whether a target supports Split DWARF (a target with1070 /// `DebuginfoKind::Dwarf` and supporting `SplitDebuginfo::Unpacked` for example).1071 #[derive(Default)]1072 pub enum DebuginfoKind {1073 /// DWARF debuginfo (such as that used on `x86_64_unknown_linux_gnu`).1074 #[default]1075 Dwarf = "dwarf",1076 /// DWARF debuginfo in dSYM files (such as on Apple platforms).1077 DwarfDsym = "dwarf-dsym",1078 /// Program database files (such as on Windows).1079 Pdb = "pdb",1080 }10811082 parse_error_type = "debuginfo kind";1083}10841085crate::target_spec_enum! {1086 #[derive(Default, Encodable, Decodable)]1087 pub enum SplitDebuginfo {1088 /// Split debug-information is disabled, meaning that on supported platforms1089 /// you can find all debug information in the executable itself. This is1090 /// only supported for ELF effectively.1091 ///1092 /// * Windows - not supported1093 /// * macOS - don't run `dsymutil`1094 /// * ELF - `.debug_*` sections1095 #[default]1096 Off = "off",10971098 /// Split debug-information can be found in a "packed" location separate1099 /// from the final artifact. This is supported on all platforms.1100 ///1101 /// * Windows - `*.pdb`1102 /// * macOS - `*.dSYM` (run `dsymutil`)1103 /// * ELF - `*.dwp` (run `thorin`)1104 Packed = "packed",11051106 /// Split debug-information can be found in individual object files on the1107 /// filesystem. The main executable may point to the object files.1108 ///1109 /// * Windows - not supported1110 /// * macOS - supported, scattered object files1111 /// * ELF - supported, scattered `*.dwo` or `*.o` files (see `SplitDwarfKind`)1112 Unpacked = "unpacked",1113 }11141115 parse_error_type = "split debuginfo";1116}11171118into_diag_arg_using_display!(SplitDebuginfo);11191120#[derive(Clone, Debug, PartialEq, Eq, serde_derive::Deserialize, schemars::JsonSchema)]1121#[serde(tag = "kind")]1122#[serde(rename_all = "kebab-case")]1123pub enum StackProbeType {1124 /// Don't emit any stack probes.1125 None,1126 /// It is harmless to use this option even on targets that do not have backend support for1127 /// stack probes as the failure mode is the same as if no stack-probe option was specified in1128 /// the first place.1129 Inline,1130 /// Call `__rust_probestack` whenever stack needs to be probed.1131 Call,1132 /// Use inline option for LLVM versions later than specified in `min_llvm_version_for_inline`1133 /// and call `__rust_probestack` otherwise.1134 InlineOrCall {1135 #[serde(rename = "min-llvm-version-for-inline")]1136 min_llvm_version_for_inline: (u32, u32, u32),1137 },1138}11391140impl ToJson for StackProbeType {1141 fn to_json(&self) -> Json {1142 Json::Object(match self {1143 StackProbeType::None => {1144 [(String::from("kind"), "none".to_json())].into_iter().collect()1145 }1146 StackProbeType::Inline => {1147 [(String::from("kind"), "inline".to_json())].into_iter().collect()1148 }1149 StackProbeType::Call => {1150 [(String::from("kind"), "call".to_json())].into_iter().collect()1151 }1152 StackProbeType::InlineOrCall { min_llvm_version_for_inline: (maj, min, patch) } => [1153 (String::from("kind"), "inline-or-call".to_json()),1154 (1155 String::from("min-llvm-version-for-inline"),1156 Json::Array(vec![maj.to_json(), min.to_json(), patch.to_json()]),1157 ),1158 ]1159 .into_iter()1160 .collect(),1161 })1162 }1163}11641165#[derive(Default, Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, StableHash)]1166pub struct SanitizerSet(u16);1167bitflags::bitflags! {1168 impl SanitizerSet: u16 {1169 const ADDRESS = 1 << 0;1170 const LEAK = 1 << 1;1171 const MEMORY = 1 << 2;1172 const THREAD = 1 << 3;1173 const HWADDRESS = 1 << 4;1174 const CFI = 1 << 5;1175 const MEMTAG = 1 << 6;1176 const SHADOWCALLSTACK = 1 << 7;1177 const KCFI = 1 << 8;1178 const KERNELADDRESS = 1 << 9;1179 const KERNELHWADDRESS = 1 << 10;1180 const SAFESTACK = 1 << 11;1181 const DATAFLOW = 1 << 12;1182 const REALTIME = 1 << 13;1183 }1184}1185rustc_data_structures::external_bitflags_debug! { SanitizerSet }11861187impl SanitizerSet {1188 // Taken from LLVM's sanitizer compatibility logic:1189 // https://github.com/llvm/llvm-project/blob/release/18.x/clang/lib/Driver/SanitizerArgs.cpp#L5121190 const MUTUALLY_EXCLUSIVE: &'static [(SanitizerSet, SanitizerSet)] = &[1191 (SanitizerSet::ADDRESS, SanitizerSet::MEMORY),1192 (SanitizerSet::ADDRESS, SanitizerSet::THREAD),1193 (SanitizerSet::ADDRESS, SanitizerSet::HWADDRESS),1194 (SanitizerSet::ADDRESS, SanitizerSet::MEMTAG),1195 (SanitizerSet::ADDRESS, SanitizerSet::KERNELADDRESS),1196 (SanitizerSet::ADDRESS, SanitizerSet::KERNELHWADDRESS),1197 (SanitizerSet::ADDRESS, SanitizerSet::SAFESTACK),1198 (SanitizerSet::LEAK, SanitizerSet::MEMORY),1199 (SanitizerSet::LEAK, SanitizerSet::THREAD),1200 (SanitizerSet::LEAK, SanitizerSet::KERNELADDRESS),1201 (SanitizerSet::LEAK, SanitizerSet::KERNELHWADDRESS),1202 (SanitizerSet::LEAK, SanitizerSet::SAFESTACK),1203 (SanitizerSet::MEMORY, SanitizerSet::THREAD),1204 (SanitizerSet::MEMORY, SanitizerSet::HWADDRESS),1205 (SanitizerSet::MEMORY, SanitizerSet::KERNELADDRESS),1206 (SanitizerSet::MEMORY, SanitizerSet::KERNELHWADDRESS),1207 (SanitizerSet::MEMORY, SanitizerSet::SAFESTACK),1208 (SanitizerSet::THREAD, SanitizerSet::HWADDRESS),1209 (SanitizerSet::THREAD, SanitizerSet::KERNELADDRESS),1210 (SanitizerSet::THREAD, SanitizerSet::KERNELHWADDRESS),1211 (SanitizerSet::THREAD, SanitizerSet::SAFESTACK),1212 (SanitizerSet::HWADDRESS, SanitizerSet::MEMTAG),1213 (SanitizerSet::HWADDRESS, SanitizerSet::KERNELADDRESS),1214 (SanitizerSet::HWADDRESS, SanitizerSet::KERNELHWADDRESS),1215 (SanitizerSet::HWADDRESS, SanitizerSet::SAFESTACK),1216 (SanitizerSet::CFI, SanitizerSet::KCFI),1217 (SanitizerSet::MEMTAG, SanitizerSet::KERNELADDRESS),1218 (SanitizerSet::MEMTAG, SanitizerSet::KERNELHWADDRESS),1219 (SanitizerSet::KERNELADDRESS, SanitizerSet::KERNELHWADDRESS),1220 (SanitizerSet::KERNELADDRESS, SanitizerSet::SAFESTACK),1221 (SanitizerSet::KERNELHWADDRESS, SanitizerSet::SAFESTACK),1222 ];12231224 /// Return sanitizer's name1225 ///1226 /// Returns none if the flags is a set of sanitizers numbering not exactly one.1227 pub fn as_str(self) -> Option<&'static str> {1228 Some(match self {1229 SanitizerSet::ADDRESS => "address",1230 SanitizerSet::CFI => "cfi",1231 SanitizerSet::DATAFLOW => "dataflow",1232 SanitizerSet::KCFI => "kcfi",1233 SanitizerSet::KERNELADDRESS => "kernel-address",1234 SanitizerSet::KERNELHWADDRESS => "kernel-hwaddress",1235 SanitizerSet::LEAK => "leak",1236 SanitizerSet::MEMORY => "memory",1237 SanitizerSet::MEMTAG => "memtag",1238 SanitizerSet::SAFESTACK => "safestack",1239 SanitizerSet::SHADOWCALLSTACK => "shadow-call-stack",1240 SanitizerSet::THREAD => "thread",1241 SanitizerSet::HWADDRESS => "hwaddress",1242 SanitizerSet::REALTIME => "realtime",1243 _ => return None,1244 })1245 }12461247 pub fn mutually_exclusive(self) -> Option<(SanitizerSet, SanitizerSet)> {1248 Self::MUTUALLY_EXCLUSIVE1249 .into_iter()1250 .find(|&(a, b)| self.contains(*a) && self.contains(*b))1251 .copied()1252 }1253}12541255/// Formats a sanitizer set as a comma separated list of sanitizers' names.1256impl fmt::Display for SanitizerSet {1257 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {1258 let mut first = true;1259 for s in *self {1260 let name = s.as_str().unwrap_or_else(|| panic!("unrecognized sanitizer {s:?}"));1261 if !first {1262 f.write_str(", ")?;1263 }1264 f.write_str(name)?;1265 first = false;1266 }1267 Ok(())1268 }1269}12701271impl FromStr for SanitizerSet {1272 type Err = String;1273 fn from_str(s: &str) -> Result<Self, Self::Err> {1274 Ok(match s {1275 "address" => SanitizerSet::ADDRESS,1276 "cfi" => SanitizerSet::CFI,1277 "dataflow" => SanitizerSet::DATAFLOW,1278 "kcfi" => SanitizerSet::KCFI,1279 "kernel-address" => SanitizerSet::KERNELADDRESS,1280 "kernel-hwaddress" => SanitizerSet::KERNELHWADDRESS,1281 "leak" => SanitizerSet::LEAK,1282 "memory" => SanitizerSet::MEMORY,1283 "memtag" => SanitizerSet::MEMTAG,1284 "safestack" => SanitizerSet::SAFESTACK,1285 "shadow-call-stack" => SanitizerSet::SHADOWCALLSTACK,1286 "thread" => SanitizerSet::THREAD,1287 "hwaddress" => SanitizerSet::HWADDRESS,1288 "realtime" => SanitizerSet::REALTIME,1289 s => return Err(format!("unknown sanitizer {s}")),1290 })1291 }1292}12931294crate::json::serde_deserialize_from_str!(SanitizerSet);1295impl schemars::JsonSchema for SanitizerSet {1296 fn schema_name() -> std::borrow::Cow<'static, str> {1297 "SanitizerSet".into()1298 }1299 fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {1300 let all = Self::all().iter().map(|sanitizer| sanitizer.as_str()).collect::<Vec<_>>();1301 schemars::json_schema! ({1302 "type": "string",1303 "enum": all,1304 })1305 .into()1306 }1307}13081309impl ToJson for SanitizerSet {1310 fn to_json(&self) -> Json {1311 self.into_iter()1312 .map(|v| Some(v.as_str()?.to_json()))1313 .collect::<Option<Vec<_>>>()1314 .unwrap_or_default()1315 .to_json()1316 }1317}13181319crate::target_spec_enum! {1320 pub enum FramePointer {1321 /// Forces the machine code generator to always preserve the frame pointers.1322 Always = "always",1323 /// Forces the machine code generator to preserve the frame pointers except for the leaf1324 /// functions (i.e. those that don't call other functions).1325 NonLeaf = "non-leaf",1326 /// Allows the machine code generator to omit the frame pointers.1327 ///1328 /// This option does not guarantee that the frame pointers will be omitted.1329 MayOmit = "may-omit",1330 }13311332 parse_error_type = "frame pointer";1333}13341335impl FramePointer {1336 /// It is intended that the "force frame pointer" transition is "one way"1337 /// so this convenience assures such if used1338 #[inline]1339 pub fn ratchet(&mut self, rhs: FramePointer) -> FramePointer {1340 *self = match (*self, rhs) {1341 (FramePointer::Always, _) | (_, FramePointer::Always) => FramePointer::Always,1342 (FramePointer::NonLeaf, _) | (_, FramePointer::NonLeaf) => FramePointer::NonLeaf,1343 _ => FramePointer::MayOmit,1344 };1345 *self1346 }1347}13481349crate::target_spec_enum! {1350 /// Controls use of stack canaries.1351 #[derive(Encodable, BlobDecodable, StableHash)]1352 pub enum StackProtector {1353 /// Disable stack canary generation.1354 None = "none",13551356 /// On LLVM, mark all generated LLVM functions with the `ssp` attribute (see1357 /// llvm/docs/LangRef.rst). This triggers stack canary generation in1358 /// functions which contain an array of a byte-sized type with more than1359 /// eight elements.1360 Basic = "basic",13611362 /// On LLVM, mark all generated LLVM functions with the `sspstrong`1363 /// attribute (see llvm/docs/LangRef.rst). This triggers stack canary1364 /// generation in functions which either contain an array, or which take1365 /// the address of a local variable.1366 Strong = "strong",13671368 /// Generate stack canaries in all functions.1369 All = "all",1370 }13711372 parse_error_type = "stack protector";1373}13741375into_diag_arg_using_display!(StackProtector);13761377crate::target_spec_enum! {1378 pub enum BinaryFormat {1379 Coff = "coff",1380 Elf = "elf",1381 MachO = "mach-o",1382 Wasm = "wasm",1383 Xcoff = "xcoff",1384 }13851386 parse_error_type = "binary format";1387}13881389impl BinaryFormat {1390 /// Returns [`object::BinaryFormat`] for given `BinaryFormat`1391 pub fn to_object(&self) -> object::BinaryFormat {1392 match self {1393 Self::Coff => object::BinaryFormat::Coff,1394 Self::Elf => object::BinaryFormat::Elf,1395 Self::MachO => object::BinaryFormat::MachO,1396 Self::Wasm => object::BinaryFormat::Wasm,1397 Self::Xcoff => object::BinaryFormat::Xcoff,1398 }1399 }14001401 pub fn desc_symbol(&self) -> Symbol {1402 match self {1403 Self::Coff => sym::coff,1404 Self::Elf => sym::elf,1405 Self::MachO => sym::macho,1406 Self::Wasm => sym::wasm,1407 Self::Xcoff => sym::xcoff,1408 }1409 }1410}14111412impl ToJson for Align {1413 fn to_json(&self) -> Json {1414 self.bits().to_json()1415 }1416}14171418macro_rules! supported_targets {1419 ( $(($tuple:literal, $module:ident),)+ ) => {1420 mod targets {1421 $(pub(crate) mod $module;)+1422 }14231424 /// List of supported targets1425 pub static TARGETS: &[&str] = &[$($tuple),+];14261427 fn load_builtin(target: &str) -> Option<Target> {1428 let t = match target {1429 $( $tuple => targets::$module::target(), )+1430 _ => return None,1431 };1432 debug!("got builtin target: {:?}", t);1433 Some(t)1434 }14351436 fn load_all_builtins() -> impl Iterator<Item = Target> {1437 [1438 $( targets::$module::target, )+1439 ]1440 .into_iter()1441 .map(|f| f())1442 }14431444 #[cfg(test)]1445 mod tests {1446 // Cannot put this into a separate file without duplication, make an exception.1447 $(1448 #[test] // `#[test]`1449 fn $module() {1450 crate::spec::targets::$module::target().test_target()1451 }1452 )+1453 }1454 };1455}14561457supported_targets! {1458 ("x86_64-unknown-linux-gnu", x86_64_unknown_linux_gnu),1459 ("x86_64-unknown-linux-gnux32", x86_64_unknown_linux_gnux32),1460 ("i686-unknown-linux-gnu", i686_unknown_linux_gnu),1461 ("i586-unknown-linux-gnu", i586_unknown_linux_gnu),1462 ("loongarch64-unknown-linux-gnu", loongarch64_unknown_linux_gnu),1463 ("loongarch64-unknown-linux-musl", loongarch64_unknown_linux_musl),1464 ("m68k-unknown-linux-gnu", m68k_unknown_linux_gnu),1465 ("m68k-unknown-none-elf", m68k_unknown_none_elf),1466 ("csky-unknown-linux-gnuabiv2", csky_unknown_linux_gnuabiv2),1467 ("csky-unknown-linux-gnuabiv2hf", csky_unknown_linux_gnuabiv2hf),1468 ("mips-unknown-linux-gnu", mips_unknown_linux_gnu),1469 ("mips64-unknown-linux-gnuabi64", mips64_unknown_linux_gnuabi64),1470 ("mips64el-unknown-linux-gnuabi64", mips64el_unknown_linux_gnuabi64),1471 ("mipsisa32r6-unknown-linux-gnu", mipsisa32r6_unknown_linux_gnu),1472 ("mipsisa32r6el-unknown-linux-gnu", mipsisa32r6el_unknown_linux_gnu),1473 ("mipsisa64r6-unknown-linux-gnuabi64", mipsisa64r6_unknown_linux_gnuabi64),1474 ("mipsisa64r6el-unknown-linux-gnuabi64", mipsisa64r6el_unknown_linux_gnuabi64),1475 ("mipsel-unknown-linux-gnu", mipsel_unknown_linux_gnu),1476 ("powerpc-unknown-linux-gnu", powerpc_unknown_linux_gnu),1477 ("powerpc-unknown-linux-gnuspe", powerpc_unknown_linux_gnuspe),1478 ("powerpc-unknown-linux-musl", powerpc_unknown_linux_musl),1479 ("powerpc-unknown-linux-muslspe", powerpc_unknown_linux_muslspe),1480 ("powerpc64-ibm-aix", powerpc64_ibm_aix),1481 ("powerpc64-unknown-linux-gnu", powerpc64_unknown_linux_gnu),1482 ("powerpc64-unknown-linux-musl", powerpc64_unknown_linux_musl),1483 ("powerpc64le-unknown-linux-gnu", powerpc64le_unknown_linux_gnu),1484 ("powerpc64le-unknown-linux-musl", powerpc64le_unknown_linux_musl),1485 ("s390x-unknown-linux-gnu", s390x_unknown_linux_gnu),1486 ("s390x-unknown-none-softfloat", s390x_unknown_none_softfloat),1487 ("s390x-unknown-linux-musl", s390x_unknown_linux_musl),1488 ("sparc-unknown-linux-gnu", sparc_unknown_linux_gnu),1489 ("sparc64-unknown-linux-gnu", sparc64_unknown_linux_gnu),1490 ("arm-unknown-linux-gnueabi", arm_unknown_linux_gnueabi),1491 ("arm-unknown-linux-gnueabihf", arm_unknown_linux_gnueabihf),1492 ("armeb-unknown-linux-gnueabi", armeb_unknown_linux_gnueabi),1493 ("arm-unknown-linux-musleabi", arm_unknown_linux_musleabi),1494 ("arm-unknown-linux-musleabihf", arm_unknown_linux_musleabihf),1495 ("armv4t-unknown-linux-gnueabi", armv4t_unknown_linux_gnueabi),1496 ("armv5te-unknown-linux-gnueabi", armv5te_unknown_linux_gnueabi),1497 ("armv5te-unknown-linux-musleabi", armv5te_unknown_linux_musleabi),1498 ("armv5te-unknown-linux-uclibceabi", armv5te_unknown_linux_uclibceabi),1499 ("armv7-unknown-linux-gnueabi", armv7_unknown_linux_gnueabi),1500 ("armv7-unknown-linux-gnueabihf", armv7_unknown_linux_gnueabihf),1501 ("thumbv7neon-unknown-linux-gnueabihf", thumbv7neon_unknown_linux_gnueabihf),1502 ("thumbv7neon-unknown-linux-musleabihf", thumbv7neon_unknown_linux_musleabihf),1503 ("armv7-unknown-linux-musleabi", armv7_unknown_linux_musleabi),1504 ("armv7-unknown-linux-musleabihf", armv7_unknown_linux_musleabihf),1505 ("aarch64-unknown-linux-gnu", aarch64_unknown_linux_gnu),1506 ("aarch64-unknown-linux-musl", aarch64_unknown_linux_musl),1507 ("aarch64_be-unknown-linux-musl", aarch64_be_unknown_linux_musl),1508 ("x86_64-unknown-linux-musl", x86_64_unknown_linux_musl),1509 ("i686-unknown-linux-musl", i686_unknown_linux_musl),1510 ("i586-unknown-linux-musl", i586_unknown_linux_musl),1511 ("mips-unknown-linux-musl", mips_unknown_linux_musl),1512 ("mipsel-unknown-linux-musl", mipsel_unknown_linux_musl),1513 ("mips64-unknown-linux-muslabi64", mips64_unknown_linux_muslabi64),1514 ("mips64el-unknown-linux-muslabi64", mips64el_unknown_linux_muslabi64),1515 ("hexagon-unknown-linux-musl", hexagon_unknown_linux_musl),1516 ("hexagon-unknown-none-elf", hexagon_unknown_none_elf),1517 ("hexagon-unknown-qurt", hexagon_unknown_qurt),15181519 ("mips-unknown-linux-uclibc", mips_unknown_linux_uclibc),1520 ("mipsel-unknown-linux-uclibc", mipsel_unknown_linux_uclibc),15211522 ("i686-linux-android", i686_linux_android),1523 ("x86_64-linux-android", x86_64_linux_android),1524 ("arm-linux-androideabi", arm_linux_androideabi),1525 ("armv7-linux-androideabi", armv7_linux_androideabi),1526 ("thumbv7neon-linux-androideabi", thumbv7neon_linux_androideabi),1527 ("aarch64-linux-android", aarch64_linux_android),1528 ("riscv64-linux-android", riscv64_linux_android),15291530 ("aarch64-unknown-freebsd", aarch64_unknown_freebsd),1531 ("armv6-unknown-freebsd", armv6_unknown_freebsd),1532 ("armv7-unknown-freebsd", armv7_unknown_freebsd),1533 ("i686-unknown-freebsd", i686_unknown_freebsd),1534 ("powerpc-unknown-freebsd", powerpc_unknown_freebsd),1535 ("powerpc64-unknown-freebsd", powerpc64_unknown_freebsd),1536 ("powerpc64le-unknown-freebsd", powerpc64le_unknown_freebsd),1537 ("riscv64gc-unknown-freebsd", riscv64gc_unknown_freebsd),1538 ("x86_64-unknown-freebsd", x86_64_unknown_freebsd),15391540 ("x86_64-unknown-dragonfly", x86_64_unknown_dragonfly),15411542 ("aarch64-unknown-openbsd", aarch64_unknown_openbsd),1543 ("i686-unknown-openbsd", i686_unknown_openbsd),1544 ("powerpc-unknown-openbsd", powerpc_unknown_openbsd),1545 ("powerpc64-unknown-openbsd", powerpc64_unknown_openbsd),1546 ("riscv64gc-unknown-openbsd", riscv64gc_unknown_openbsd),1547 ("sparc64-unknown-openbsd", sparc64_unknown_openbsd),1548 ("x86_64-unknown-openbsd", x86_64_unknown_openbsd),15491550 ("aarch64-unknown-netbsd", aarch64_unknown_netbsd),1551 ("aarch64_be-unknown-netbsd", aarch64_be_unknown_netbsd),1552 ("armv6-unknown-netbsd-eabihf", armv6_unknown_netbsd_eabihf),1553 ("armv7-unknown-netbsd-eabihf", armv7_unknown_netbsd_eabihf),1554 ("i586-unknown-netbsd", i586_unknown_netbsd),1555 ("i686-unknown-netbsd", i686_unknown_netbsd),1556 ("mipsel-unknown-netbsd", mipsel_unknown_netbsd),1557 ("powerpc-unknown-netbsd", powerpc_unknown_netbsd),1558 ("riscv64gc-unknown-netbsd", riscv64gc_unknown_netbsd),1559 ("sparc64-unknown-netbsd", sparc64_unknown_netbsd),1560 ("x86_64-unknown-netbsd", x86_64_unknown_netbsd),15611562 ("i686-unknown-haiku", i686_unknown_haiku),1563 ("x86_64-unknown-haiku", x86_64_unknown_haiku),15641565 ("aarch64-unknown-helenos", aarch64_unknown_helenos),1566 ("i686-unknown-helenos", i686_unknown_helenos),1567 ("powerpc-unknown-helenos", powerpc_unknown_helenos),1568 ("sparc64-unknown-helenos", sparc64_unknown_helenos),1569 ("x86_64-unknown-helenos", x86_64_unknown_helenos),15701571 ("i686-unknown-hurd-gnu", i686_unknown_hurd_gnu),1572 ("x86_64-unknown-hurd-gnu", x86_64_unknown_hurd_gnu),15731574 ("aarch64-apple-darwin", aarch64_apple_darwin),1575 ("arm64e-apple-darwin", arm64e_apple_darwin),1576 ("x86_64-apple-darwin", x86_64_apple_darwin),1577 ("x86_64h-apple-darwin", x86_64h_apple_darwin),1578 ("i686-apple-darwin", i686_apple_darwin),15791580 ("aarch64-unknown-fuchsia", aarch64_unknown_fuchsia),1581 ("riscv64gc-unknown-fuchsia", riscv64gc_unknown_fuchsia),1582 ("x86_64-unknown-fuchsia", x86_64_unknown_fuchsia),15831584 ("avr-none", avr_none),15851586 ("x86_64-unknown-l4re-uclibc", x86_64_unknown_l4re_uclibc),15871588 ("aarch64-unknown-redox", aarch64_unknown_redox),1589 ("i586-unknown-redox", i586_unknown_redox),1590 ("riscv64gc-unknown-redox", riscv64gc_unknown_redox),1591 ("x86_64-unknown-redox", x86_64_unknown_redox),15921593 ("x86_64-unknown-managarm-mlibc", x86_64_unknown_managarm_mlibc),1594 ("aarch64-unknown-managarm-mlibc", aarch64_unknown_managarm_mlibc),1595 ("riscv64gc-unknown-managarm-mlibc", riscv64gc_unknown_managarm_mlibc),15961597 ("i386-apple-ios", i386_apple_ios),1598 ("x86_64-apple-ios", x86_64_apple_ios),1599 ("aarch64-apple-ios", aarch64_apple_ios),1600 ("arm64e-apple-ios", arm64e_apple_ios),1601 ("armv7s-apple-ios", armv7s_apple_ios),1602 ("x86_64-apple-ios-macabi", x86_64_apple_ios_macabi),1603 ("aarch64-apple-ios-macabi", aarch64_apple_ios_macabi),1604 ("aarch64-apple-ios-sim", aarch64_apple_ios_sim),16051606 ("aarch64-apple-tvos", aarch64_apple_tvos),1607 ("aarch64-apple-tvos-sim", aarch64_apple_tvos_sim),1608 ("arm64e-apple-tvos", arm64e_apple_tvos),1609 ("x86_64-apple-tvos", x86_64_apple_tvos),16101611 ("armv7k-apple-watchos", armv7k_apple_watchos),1612 ("arm64_32-apple-watchos", arm64_32_apple_watchos),1613 ("x86_64-apple-watchos-sim", x86_64_apple_watchos_sim),1614 ("aarch64-apple-watchos", aarch64_apple_watchos),1615 ("aarch64-apple-watchos-sim", aarch64_apple_watchos_sim),16161617 ("aarch64-apple-visionos", aarch64_apple_visionos),1618 ("aarch64-apple-visionos-sim", aarch64_apple_visionos_sim),16191620 ("armebv7r-none-eabi", armebv7r_none_eabi),1621 ("armebv7r-none-eabihf", armebv7r_none_eabihf),1622 ("armv7r-none-eabi", armv7r_none_eabi),1623 ("thumbv7r-none-eabi", thumbv7r_none_eabi),1624 ("armv7r-none-eabihf", armv7r_none_eabihf),1625 ("thumbv7r-none-eabihf", thumbv7r_none_eabihf),1626 ("armv8r-none-eabihf", armv8r_none_eabihf),1627 ("thumbv8r-none-eabihf", thumbv8r_none_eabihf),16281629 ("armv7-rtems-eabihf", armv7_rtems_eabihf),16301631 ("x86_64-pc-solaris", x86_64_pc_solaris),1632 ("sparcv9-sun-solaris", sparcv9_sun_solaris),16331634 ("x86_64-unknown-illumos", x86_64_unknown_illumos),1635 ("aarch64-unknown-illumos", aarch64_unknown_illumos),16361637 ("x86_64-pc-windows-gnu", x86_64_pc_windows_gnu),1638 ("x86_64-uwp-windows-gnu", x86_64_uwp_windows_gnu),1639 ("x86_64-win7-windows-gnu", x86_64_win7_windows_gnu),1640 ("i686-pc-windows-gnu", i686_pc_windows_gnu),1641 ("i686-uwp-windows-gnu", i686_uwp_windows_gnu),1642 ("i686-win7-windows-gnu", i686_win7_windows_gnu),16431644 ("aarch64-pc-windows-gnullvm", aarch64_pc_windows_gnullvm),1645 ("i686-pc-windows-gnullvm", i686_pc_windows_gnullvm),1646 ("x86_64-pc-windows-gnullvm", x86_64_pc_windows_gnullvm),16471648 ("aarch64-pc-windows-msvc", aarch64_pc_windows_msvc),1649 ("aarch64-uwp-windows-msvc", aarch64_uwp_windows_msvc),1650 ("arm64ec-pc-windows-msvc", arm64ec_pc_windows_msvc),1651 ("x86_64-pc-windows-msvc", x86_64_pc_windows_msvc),1652 ("x86_64-uwp-windows-msvc", x86_64_uwp_windows_msvc),1653 ("x86_64-win7-windows-msvc", x86_64_win7_windows_msvc),1654 ("i686-pc-windows-msvc", i686_pc_windows_msvc),1655 ("i686-uwp-windows-msvc", i686_uwp_windows_msvc),1656 ("i686-win7-windows-msvc", i686_win7_windows_msvc),1657 ("thumbv7a-pc-windows-msvc", thumbv7a_pc_windows_msvc),1658 ("thumbv7a-uwp-windows-msvc", thumbv7a_uwp_windows_msvc),16591660 ("wasm32-unknown-emscripten", wasm32_unknown_emscripten),1661 ("wasm32-unknown-unknown", wasm32_unknown_unknown),1662 ("wasm32v1-none", wasm32v1_none),1663 ("wasm32-wasip1", wasm32_wasip1),1664 ("wasm32-wasip2", wasm32_wasip2),1665 ("wasm32-wasip3", wasm32_wasip3),1666 ("wasm32-wasip1-threads", wasm32_wasip1_threads),1667 ("wasm32-wali-linux-musl", wasm32_wali_linux_musl),1668 ("wasm64-unknown-unknown", wasm64_unknown_unknown),16691670 ("thumbv6m-none-eabi", thumbv6m_none_eabi),1671 ("thumbv7m-none-eabi", thumbv7m_none_eabi),1672 ("thumbv7em-none-eabi", thumbv7em_none_eabi),1673 ("thumbv7em-none-eabihf", thumbv7em_none_eabihf),1674 ("thumbv8m.base-none-eabi", thumbv8m_base_none_eabi),1675 ("thumbv8m.main-none-eabi", thumbv8m_main_none_eabi),1676 ("thumbv8m.main-none-eabihf", thumbv8m_main_none_eabihf),16771678 ("armv7a-none-eabi", armv7a_none_eabi),1679 ("thumbv7a-none-eabi", thumbv7a_none_eabi),1680 ("armv7a-none-eabihf", armv7a_none_eabihf),1681 ("thumbv7a-none-eabihf", thumbv7a_none_eabihf),1682 ("armv7a-nuttx-eabi", armv7a_nuttx_eabi),1683 ("armv7a-nuttx-eabihf", armv7a_nuttx_eabihf),1684 ("armv7a-vex-v5", armv7a_vex_v5),16851686 ("msp430-none-elf", msp430_none_elf),16871688 ("aarch64_be-unknown-hermit", aarch64_be_unknown_hermit),1689 ("aarch64-unknown-hermit", aarch64_unknown_hermit),1690 ("riscv64gc-unknown-hermit", riscv64gc_unknown_hermit),1691 ("x86_64-unknown-hermit", x86_64_unknown_hermit),1692 ("x86_64-unknown-motor", x86_64_unknown_motor),16931694 ("x86_64-unikraft-linux-musl", x86_64_unikraft_linux_musl),16951696 ("armv7-unknown-trusty", armv7_unknown_trusty),1697 ("aarch64-unknown-trusty", aarch64_unknown_trusty),1698 ("x86_64-unknown-trusty", x86_64_unknown_trusty),16991700 ("riscv32i-unknown-none-elf", riscv32i_unknown_none_elf),1701 ("riscv32im-risc0-zkvm-elf", riscv32im_risc0_zkvm_elf),1702 ("riscv32im-unknown-none-elf", riscv32im_unknown_none_elf),1703 ("riscv32ima-unknown-none-elf", riscv32ima_unknown_none_elf),1704 ("riscv32imc-unknown-none-elf", riscv32imc_unknown_none_elf),1705 ("riscv32imc-esp-espidf", riscv32imc_esp_espidf),1706 ("riscv32imac-esp-espidf", riscv32imac_esp_espidf),1707 ("riscv32imafc-esp-espidf", riscv32imafc_esp_espidf),17081709 ("riscv32e-unknown-none-elf", riscv32e_unknown_none_elf),1710 ("riscv32em-unknown-none-elf", riscv32em_unknown_none_elf),1711 ("riscv32emc-unknown-none-elf", riscv32emc_unknown_none_elf),17121713 ("riscv32imac-unknown-none-elf", riscv32imac_unknown_none_elf),1714 ("riscv32imafc-unknown-none-elf", riscv32imafc_unknown_none_elf),1715 ("riscv32imac-unknown-xous-elf", riscv32imac_unknown_xous_elf),1716 ("riscv32gc-unknown-linux-gnu", riscv32gc_unknown_linux_gnu),1717 ("riscv32gc-unknown-linux-musl", riscv32gc_unknown_linux_musl),1718 ("riscv64im-unknown-none-elf", riscv64im_unknown_none_elf),1719 ("riscv64imac-unknown-none-elf", riscv64imac_unknown_none_elf),1720 ("riscv64gc-unknown-none-elf", riscv64gc_unknown_none_elf),1721 ("riscv64gc-unknown-linux-gnu", riscv64gc_unknown_linux_gnu),1722 ("riscv64gc-unknown-linux-musl", riscv64gc_unknown_linux_musl),1723 ("riscv64a23-unknown-linux-gnu", riscv64a23_unknown_linux_gnu),17241725 ("sparc-unknown-none-elf", sparc_unknown_none_elf),17261727 ("loongarch32-unknown-none", loongarch32_unknown_none),1728 ("loongarch32-unknown-none-softfloat", loongarch32_unknown_none_softfloat),1729 ("loongarch64-unknown-none", loongarch64_unknown_none),1730 ("loongarch64-unknown-none-softfloat", loongarch64_unknown_none_softfloat),17311732 ("aarch64-unknown-none", aarch64_unknown_none),1733 ("aarch64-unknown-none-softfloat", aarch64_unknown_none_softfloat),1734 ("aarch64_be-unknown-none-softfloat", aarch64_be_unknown_none_softfloat),1735 ("aarch64-unknown-nuttx", aarch64_unknown_nuttx),1736 ("aarch64v8r-unknown-none", aarch64v8r_unknown_none),1737 ("aarch64v8r-unknown-none-softfloat", aarch64v8r_unknown_none_softfloat),17381739 ("x86_64-fortanix-unknown-sgx", x86_64_fortanix_unknown_sgx),17401741 ("x86_64-unknown-uefi", x86_64_unknown_uefi),1742 ("i686-unknown-uefi", i686_unknown_uefi),1743 ("aarch64-unknown-uefi", aarch64_unknown_uefi),17441745 ("nvptx64-nvidia-cuda", nvptx64_nvidia_cuda),17461747 ("amdgcn-amd-amdhsa", amdgcn_amd_amdhsa),17481749 ("xtensa-esp32-none-elf", xtensa_esp32_none_elf),1750 ("xtensa-esp32-espidf", xtensa_esp32_espidf),1751 ("xtensa-esp32s2-none-elf", xtensa_esp32s2_none_elf),1752 ("xtensa-esp32s2-espidf", xtensa_esp32s2_espidf),1753 ("xtensa-esp32s3-none-elf", xtensa_esp32s3_none_elf),1754 ("xtensa-esp32s3-espidf", xtensa_esp32s3_espidf),17551756 ("i686-wrs-vxworks", i686_wrs_vxworks),1757 ("x86_64-wrs-vxworks", x86_64_wrs_vxworks),1758 ("armv7-wrs-vxworks-eabihf", armv7_wrs_vxworks_eabihf),1759 ("aarch64-wrs-vxworks", aarch64_wrs_vxworks),1760 ("powerpc-wrs-vxworks", powerpc_wrs_vxworks),1761 ("powerpc-wrs-vxworks-spe", powerpc_wrs_vxworks_spe),1762 ("powerpc64-wrs-vxworks", powerpc64_wrs_vxworks),1763 ("riscv32-wrs-vxworks", riscv32_wrs_vxworks),1764 ("riscv64-wrs-vxworks", riscv64_wrs_vxworks),17651766 ("aarch64-kmc-solid_asp3", aarch64_kmc_solid_asp3),1767 ("armv7a-kmc-solid_asp3-eabi", armv7a_kmc_solid_asp3_eabi),1768 ("armv7a-kmc-solid_asp3-eabihf", armv7a_kmc_solid_asp3_eabihf),17691770 ("mipsel-sony-psp", mipsel_sony_psp),1771 ("mipsel-sony-psx", mipsel_sony_psx),1772 ("mipsel-unknown-none", mipsel_unknown_none),1773 ("mips-mti-none-elf", mips_mti_none_elf),1774 ("mipsel-mti-none-elf", mipsel_mti_none_elf),17751776 ("armv4t-none-eabi", armv4t_none_eabi),1777 ("armv5te-none-eabi", armv5te_none_eabi),1778 ("armv6-none-eabi", armv6_none_eabi),1779 ("armv6-none-eabihf", armv6_none_eabihf),1780 ("thumbv4t-none-eabi", thumbv4t_none_eabi),1781 ("thumbv5te-none-eabi", thumbv5te_none_eabi),1782 ("thumbv6-none-eabi", thumbv6_none_eabi),17831784 ("aarch64_be-unknown-linux-gnu", aarch64_be_unknown_linux_gnu),1785 ("aarch64-unknown-linux-gnu_ilp32", aarch64_unknown_linux_gnu_ilp32),1786 ("aarch64_be-unknown-linux-gnu_ilp32", aarch64_be_unknown_linux_gnu_ilp32),17871788 ("bpfeb-unknown-none", bpfeb_unknown_none),1789 ("bpfel-unknown-none", bpfel_unknown_none),17901791 ("armv6k-nintendo-3ds", armv6k_nintendo_3ds),17921793 ("aarch64-nintendo-switch-freestanding", aarch64_nintendo_switch_freestanding),17941795 ("armv7-sony-vita-newlibeabihf", armv7_sony_vita_newlibeabihf),17961797 ("armv7-unknown-linux-uclibceabi", armv7_unknown_linux_uclibceabi),1798 ("armv7-unknown-linux-uclibceabihf", armv7_unknown_linux_uclibceabihf),17991800 ("x86_64-unknown-none", x86_64_unknown_none),18011802 ("aarch64-unknown-teeos", aarch64_unknown_teeos),18031804 ("mips64-openwrt-linux-musl", mips64_openwrt_linux_musl),18051806 ("aarch64-unknown-nto-qnx700", aarch64_unknown_nto_qnx700),1807 ("aarch64-unknown-nto-qnx710", aarch64_unknown_nto_qnx710),1808 ("aarch64-unknown-nto-qnx710_iosock", aarch64_unknown_nto_qnx710_iosock),1809 ("aarch64-unknown-nto-qnx800", aarch64_unknown_nto_qnx800),1810 ("x86_64-pc-nto-qnx710", x86_64_pc_nto_qnx710),1811 ("x86_64-pc-nto-qnx710_iosock", x86_64_pc_nto_qnx710_iosock),1812 ("x86_64-pc-nto-qnx800", x86_64_pc_nto_qnx800),1813 ("i686-pc-nto-qnx700", i686_pc_nto_qnx700),18141815 ("aarch64-unknown-linux-ohos", aarch64_unknown_linux_ohos),1816 ("armv7-unknown-linux-ohos", armv7_unknown_linux_ohos),1817 ("loongarch64-unknown-linux-ohos", loongarch64_unknown_linux_ohos),1818 ("x86_64-unknown-linux-ohos", x86_64_unknown_linux_ohos),18191820 ("x86_64-unknown-linux-none", x86_64_unknown_linux_none),18211822 ("thumbv6m-nuttx-eabi", thumbv6m_nuttx_eabi),1823 ("thumbv7a-nuttx-eabi", thumbv7a_nuttx_eabi),1824 ("thumbv7a-nuttx-eabihf", thumbv7a_nuttx_eabihf),1825 ("thumbv7m-nuttx-eabi", thumbv7m_nuttx_eabi),1826 ("thumbv7em-nuttx-eabi", thumbv7em_nuttx_eabi),1827 ("thumbv7em-nuttx-eabihf", thumbv7em_nuttx_eabihf),1828 ("thumbv8m.base-nuttx-eabi", thumbv8m_base_nuttx_eabi),1829 ("thumbv8m.main-nuttx-eabi", thumbv8m_main_nuttx_eabi),1830 ("thumbv8m.main-nuttx-eabihf", thumbv8m_main_nuttx_eabihf),1831 ("riscv32imc-unknown-nuttx-elf", riscv32imc_unknown_nuttx_elf),1832 ("riscv32imac-unknown-nuttx-elf", riscv32imac_unknown_nuttx_elf),1833 ("riscv32imafc-unknown-nuttx-elf", riscv32imafc_unknown_nuttx_elf),1834 ("riscv64imac-unknown-nuttx-elf", riscv64imac_unknown_nuttx_elf),1835 ("riscv64gc-unknown-nuttx-elf", riscv64gc_unknown_nuttx_elf),1836 ("x86_64-lynx-lynxos178", x86_64_lynx_lynxos178),18371838 ("x86_64-pc-cygwin", x86_64_pc_cygwin),18391840 ("x86_64-unknown-linux-gnuasan", x86_64_unknown_linux_gnuasan),1841 ("x86_64-unknown-linux-gnumsan", x86_64_unknown_linux_gnumsan),1842 ("x86_64-unknown-linux-gnutsan", x86_64_unknown_linux_gnutsan),1843}18441845/// Cow-Vec-Str: Cow<'static, [Cow<'static, str>]>1846macro_rules! cvs {1847 () => {1848 ::std::borrow::Cow::Borrowed(&[])1849 };1850 ($($x:expr),+ $(,)?) => {1851 ::std::borrow::Cow::Borrowed(&[1852 $(1853 ::std::borrow::Cow::Borrowed($x),1854 )*1855 ])1856 };1857}18581859pub(crate) use cvs;18601861/// Warnings encountered when parsing the target `json`.1862///1863/// Includes fields that weren't recognized and fields that don't have the expected type.1864#[derive(Debug, PartialEq)]1865pub struct TargetWarnings {1866 unused_fields: Vec<String>,1867}18681869impl TargetWarnings {1870 pub fn empty() -> Self {1871 Self { unused_fields: Vec::new() }1872 }18731874 pub fn warning_messages(&self) -> Vec<String> {1875 let mut warnings = vec![];1876 if !self.unused_fields.is_empty() {1877 warnings.push(format!(1878 "target json file contains unused fields: {}",1879 self.unused_fields.join(", ")1880 ));1881 }1882 warnings1883 }1884}18851886/// For the [`Target::check_consistency`] function, determines whether the given target is a builtin or a JSON1887/// target.1888#[derive(Copy, Clone, Debug, PartialEq)]1889enum TargetKind {1890 Json,1891 Builtin,1892}18931894crate::target_spec_enum! {1895 pub enum Arch {1896 AArch64 = "aarch64",1897 AmdGpu = "amdgpu",1898 Arm = "arm",1899 Arm64EC = "arm64ec",1900 Avr = "avr",1901 Bpf = "bpf",1902 CSky = "csky",1903 Hexagon = "hexagon",1904 LoongArch32 = "loongarch32",1905 LoongArch64 = "loongarch64",1906 M68k = "m68k",1907 Mips = "mips",1908 Mips32r6 = "mips32r6",1909 Mips64 = "mips64",1910 Mips64r6 = "mips64r6",1911 Msp430 = "msp430",1912 Nvptx64 = "nvptx64",1913 PowerPC = "powerpc",1914 PowerPC64 = "powerpc64",1915 RiscV32 = "riscv32",1916 RiscV64 = "riscv64",1917 S390x = "s390x",1918 Sparc = "sparc",1919 Sparc64 = "sparc64",1920 SpirV = "spirv",1921 Wasm32 = "wasm32",1922 Wasm64 = "wasm64",1923 X86 = "x86",1924 X86_64 = "x86_64",1925 Xtensa = "xtensa",1926 }1927 other_variant = Other;1928}19291930impl Arch {1931 pub fn desc_symbol(&self) -> Symbol {1932 match self {1933 Self::AArch64 => sym::aarch64,1934 Self::AmdGpu => sym::amdgpu,1935 Self::Arm => sym::arm,1936 Self::Arm64EC => sym::arm64ec,1937 Self::Avr => sym::avr,1938 Self::Bpf => sym::bpf,1939 Self::CSky => sym::csky,1940 Self::Hexagon => sym::hexagon,1941 Self::LoongArch32 => sym::loongarch32,1942 Self::LoongArch64 => sym::loongarch64,1943 Self::M68k => sym::m68k,1944 Self::Mips => sym::mips,1945 Self::Mips32r6 => sym::mips32r6,1946 Self::Mips64 => sym::mips64,1947 Self::Mips64r6 => sym::mips64r6,1948 Self::Msp430 => sym::msp430,1949 Self::Nvptx64 => sym::nvptx64,1950 Self::PowerPC => sym::powerpc,1951 Self::PowerPC64 => sym::powerpc64,1952 Self::RiscV32 => sym::riscv32,1953 Self::RiscV64 => sym::riscv64,1954 Self::S390x => sym::s390x,1955 Self::Sparc => sym::sparc,1956 Self::Sparc64 => sym::sparc64,1957 Self::SpirV => sym::spirv,1958 Self::Wasm32 => sym::wasm32,1959 Self::Wasm64 => sym::wasm64,1960 Self::X86 => sym::x86,1961 Self::X86_64 => sym::x86_64,1962 Self::Xtensa => sym::xtensa,1963 Self::Other(name) => rustc_span::Symbol::intern(name),1964 }1965 }19661967 /// Whether `#[rustc_scalable_vector]` is supported for a target architecture1968 pub fn supports_scalable_vectors(&self) -> bool {1969 use Arch::*;19701971 match self {1972 AArch64 | RiscV32 | RiscV64 => true,1973 AmdGpu | Arm | Arm64EC | Avr | Bpf | CSky | Hexagon | LoongArch32 | LoongArch641974 | M68k | Mips | Mips32r6 | Mips64 | Mips64r6 | Msp430 | Nvptx64 | PowerPC1975 | PowerPC64 | S390x | Sparc | Sparc64 | SpirV | Wasm32 | Wasm64 | X86 | X86_641976 | Xtensa | Other(_) => false,1977 }1978 }1979}19801981crate::target_spec_enum! {1982 pub enum Os {1983 Aix = "aix",1984 AmdHsa = "amdhsa",1985 Android = "android",1986 Cuda = "cuda",1987 Cygwin = "cygwin",1988 Dragonfly = "dragonfly",1989 Emscripten = "emscripten",1990 EspIdf = "espidf",1991 FreeBsd = "freebsd",1992 Fuchsia = "fuchsia",1993 Haiku = "haiku",1994 HelenOs = "helenos",1995 Hermit = "hermit",1996 Horizon = "horizon",1997 Hurd = "hurd",1998 Illumos = "illumos",1999 IOs = "ios",2000 L4Re = "l4re",
Findings
✓ No findings reported for this file.