rustc_target: introduce Env

Improve type safety by using an enum rather than strings.
This commit is contained in:
Tamir Duberstein 2025-11-05 12:37:09 -05:00
parent 86c74a4d16
commit ddd7596400
No known key found for this signature in database
56 changed files with 205 additions and 160 deletions

View file

@ -49,7 +49,7 @@ use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
use rustc_session::Session;
use rustc_session::config::OutputFilenames;
use rustc_span::{Symbol, sym};
use rustc_target::spec::{Abi, Arch};
use rustc_target::spec::{Abi, Arch, Env};
pub use crate::config::*;
use crate::prelude::*;
@ -185,7 +185,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
// won't be available when using a LLVM-built sysroot.
let has_reliable_f16_f128 = !(sess.target.arch == Arch::X86_64
&& sess.target.os == "windows"
&& sess.target.env == "gnu"
&& sess.target.env == Env::Gnu
&& sess.target.abi != Abi::Llvm);
TargetConfig {

View file

@ -7,7 +7,7 @@
use rustc_codegen_ssa::common;
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, HasTypingEnv};
use rustc_middle::ty::{self, Instance, TypeVisitableExt};
use rustc_target::spec::Arch;
use rustc_target::spec::{Arch, Env};
use tracing::debug;
use crate::context::CodegenCx;
@ -145,7 +145,7 @@ pub(crate) fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'t
if cx.use_dll_storage_attrs
&& let Some(library) = tcx.native_library(instance_def_id)
&& library.kind.is_dllimport()
&& !matches!(tcx.sess.target.env.as_ref(), "gnu" | "uclibc")
&& !matches!(tcx.sess.target.env, Env::Gnu | Env::Uclibc)
{
llvm::set_dllimport_storage_class(llfn);
}

View file

@ -29,7 +29,7 @@ use rustc_span::source_map::Spanned;
use rustc_span::{DUMMY_SP, Span, Symbol};
use rustc_symbol_mangling::mangle_internal_symbol;
use rustc_target::spec::{
Abi, Arch, HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel,
Abi, Arch, Env, HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel,
};
use smallvec::SmallVec;
@ -336,7 +336,7 @@ pub(crate) unsafe fn create_module<'ll>(
// Control Flow Guard is currently only supported by MSVC and LLVM on Windows.
if sess.target.is_like_msvc
|| (sess.target.options.os == "windows"
&& sess.target.options.env == "gnu"
&& sess.target.options.env == Env::Gnu
&& sess.target.options.abi == Abi::Llvm)
{
match sess.opts.cg.control_flow_guard {
@ -710,7 +710,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
},
);
if self.tcx.sess.target.env == "sim" {
if self.tcx.sess.target.env == Env::Sim {
llvm::add_module_flag_u32(
self.llmod,
llvm::ModuleFlagMergeBehavior::Error,

View file

@ -15,7 +15,9 @@ use rustc_fs_util::path_to_c_string;
use rustc_middle::bug;
use rustc_session::Session;
use rustc_session::config::{PrintKind, PrintRequest};
use rustc_target::spec::{Abi, Arch, MergeFunctions, PanicStrategy, SmallDataThresholdSupport};
use rustc_target::spec::{
Abi, Arch, Env, MergeFunctions, PanicStrategy, SmallDataThresholdSupport,
};
use smallvec::{SmallVec, smallvec};
use crate::back::write::create_informational_target_machine;
@ -352,7 +354,7 @@ pub(crate) fn target_config(sess: &Session) -> TargetConfig {
fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
let target_arch = &sess.target.arch;
let target_os = sess.target.options.os.as_ref();
let target_env = sess.target.options.env.as_ref();
let target_env = &sess.target.options.env;
let target_abi = &sess.target.options.abi;
let target_pointer_width = sess.target.pointer_width;
let version = get_version();
@ -371,7 +373,7 @@ fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
// Selection failure <https://github.com/llvm/llvm-project/issues/50374> (fixed in llvm21)
(Arch::S390x, _) if lt_21_0_0 => false,
// MinGW ABI bugs <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115054>
(Arch::X86_64, "windows") if target_env == "gnu" && *target_abi != Abi::Llvm => false,
(Arch::X86_64, "windows") if *target_env == Env::Gnu && *target_abi != Abi::Llvm => false,
// Infinite recursion <https://github.com/llvm/llvm-project/issues/97981>
(Arch::CSky, _) => false,
(Arch::Hexagon, _) if lt_21_0_0 => false, // (fixed in llvm21)
@ -403,7 +405,7 @@ fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
// not fail if our compiler-builtins is linked. (fixed in llvm21)
(Arch::X86, _) if lt_21_0_0 => false,
// MinGW ABI bugs <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115054>
(Arch::X86_64, "windows") if target_env == "gnu" && *target_abi != Abi::Llvm => false,
(Arch::X86_64, "windows") if *target_env == Env::Gnu && *target_abi != Abi::Llvm => false,
// There are no known problems on other platforms, so the only requirement is that symbols
// are available. `compiler-builtins` provides all symbols required for core `f128`
// support, so this should work for everything else.
@ -424,7 +426,7 @@ fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
// (ld is 80-bit extended precision).
//
// musl does not implement the symbols required for f128 math at all.
_ if target_env == "musl" => false,
_ if *target_env == Env::Musl => false,
(Arch::X86_64, _) => false,
(_, "linux") if target_pointer_width == 64 => true,
_ => false,

View file

@ -6,7 +6,7 @@ use itertools::Itertools;
use rustc_middle::middle::exported_symbols::SymbolExportKind;
use rustc_session::Session;
pub(super) use rustc_target::spec::apple::OSVersion;
use rustc_target::spec::{Arch, Target};
use rustc_target::spec::{Arch, Env, Target};
use tracing::debug;
use crate::errors::{XcrunError, XcrunSdkPathWarning};
@ -17,35 +17,35 @@ mod tests;
/// The canonical name of the desired SDK for a given target.
pub(super) fn sdk_name(target: &Target) -> &'static str {
match (&*target.os, &*target.env) {
("macos", "") => "MacOSX",
("ios", "") => "iPhoneOS",
("ios", "sim") => "iPhoneSimulator",
match (&*target.os, &target.env) {
("macos", Env::Unspecified) => "MacOSX",
("ios", Env::Unspecified) => "iPhoneOS",
("ios", Env::Sim) => "iPhoneSimulator",
// Mac Catalyst uses the macOS SDK
("ios", "macabi") => "MacOSX",
("tvos", "") => "AppleTVOS",
("tvos", "sim") => "AppleTVSimulator",
("visionos", "") => "XROS",
("visionos", "sim") => "XRSimulator",
("watchos", "") => "WatchOS",
("watchos", "sim") => "WatchSimulator",
("ios", Env::MacAbi) => "MacOSX",
("tvos", Env::Unspecified) => "AppleTVOS",
("tvos", Env::Sim) => "AppleTVSimulator",
("visionos", Env::Unspecified) => "XROS",
("visionos", Env::Sim) => "XRSimulator",
("watchos", Env::Unspecified) => "WatchOS",
("watchos", Env::Sim) => "WatchSimulator",
(os, abi) => unreachable!("invalid os '{os}' / abi '{abi}' combination for Apple target"),
}
}
pub(super) fn macho_platform(target: &Target) -> u32 {
match (&*target.os, &*target.env) {
match (&*target.os, &target.env) {
("macos", _) => object::macho::PLATFORM_MACOS,
("ios", "macabi") => object::macho::PLATFORM_MACCATALYST,
("ios", "sim") => object::macho::PLATFORM_IOSSIMULATOR,
("ios", Env::MacAbi) => object::macho::PLATFORM_MACCATALYST,
("ios", Env::Sim) => object::macho::PLATFORM_IOSSIMULATOR,
("ios", _) => object::macho::PLATFORM_IOS,
("watchos", "sim") => object::macho::PLATFORM_WATCHOSSIMULATOR,
("watchos", Env::Sim) => object::macho::PLATFORM_WATCHOSSIMULATOR,
("watchos", _) => object::macho::PLATFORM_WATCHOS,
("tvos", "sim") => object::macho::PLATFORM_TVOSSIMULATOR,
("tvos", Env::Sim) => object::macho::PLATFORM_TVOSSIMULATOR,
("tvos", _) => object::macho::PLATFORM_TVOS,
("visionos", "sim") => object::macho::PLATFORM_XROSSIMULATOR,
("visionos", Env::Sim) => object::macho::PLATFORM_XROSSIMULATOR,
("visionos", _) => object::macho::PLATFORM_XROS,
_ => unreachable!("tried to get Mach-O platform for non-Apple target"),
(os, env) => unreachable!("invalid os '{os}' / env '{env}' combination for Apple target"),
}
}

View file

@ -46,9 +46,9 @@ use rustc_session::{Session, filesearch};
use rustc_span::Symbol;
use rustc_target::spec::crt_objects::CrtObjects;
use rustc_target::spec::{
Abi, BinaryFormat, Cc, LinkOutputKind, LinkSelfContainedComponents, LinkSelfContainedDefault,
LinkerFeatures, LinkerFlavor, LinkerFlavorCli, Lld, RelocModel, RelroLevel, SanitizerSet,
SplitDebuginfo,
Abi, BinaryFormat, Cc, Env, LinkOutputKind, LinkSelfContainedComponents,
LinkSelfContainedDefault, LinkerFeatures, LinkerFlavor, LinkerFlavorCli, Lld, RelocModel,
RelroLevel, SanitizerSet, SplitDebuginfo,
};
use tracing::{debug, info, warn};
@ -3071,7 +3071,7 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo
// `sess.target.arch` (`target_arch`) is not detailed enough.
let llvm_arch = sess.target.llvm_target.split_once('-').expect("LLVM target must have arch").0;
let target_os = &*sess.target.os;
let target_env = &*sess.target.env;
let target_env = &sess.target.env;
// The architecture name to forward to the linker.
//
@ -3123,12 +3123,12 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo
// > - xros-simulator
// > - driverkit
let platform_name = match (target_os, target_env) {
(os, "") => os,
("ios", "macabi") => "mac-catalyst",
("ios", "sim") => "ios-simulator",
("tvos", "sim") => "tvos-simulator",
("watchos", "sim") => "watchos-simulator",
("visionos", "sim") => "visionos-simulator",
(os, Env::Unspecified) => os,
("ios", Env::MacAbi) => "mac-catalyst",
("ios", Env::Sim) => "ios-simulator",
("tvos", Env::Sim) => "tvos-simulator",
("watchos", Env::Sim) => "watchos-simulator",
("visionos", Env::Sim) => "visionos-simulator",
_ => bug!("invalid OS/env combination for Apple target: {target_os}, {target_env}"),
};

View file

@ -7,7 +7,7 @@ use rustc_middle::ty::{self, Instance, TyCtxt};
use rustc_middle::{bug, mir, span_bug};
use rustc_session::cstore::{DllCallingConvention, DllImport};
use rustc_span::Span;
use rustc_target::spec::{Abi, Target};
use rustc_target::spec::{Abi, Env, Target};
use crate::traits::*;
@ -171,7 +171,7 @@ pub fn asm_const_to_str<'tcx>(
}
pub fn is_mingw_gnu_toolchain(target: &Target) -> bool {
target.os == "windows" && target.env == "gnu" && target.abi == Abi::Unspecified
target.os == "windows" && target.env == Env::Gnu && target.abi == Abi::Unspecified
}
pub fn i686_decorated_name(

View file

@ -15,7 +15,7 @@ use rustc_session::cstore::{DllCallingConvention, DllImport, ForeignModule, Nati
use rustc_session::search_paths::PathKind;
use rustc_span::Symbol;
use rustc_span::def_id::{DefId, LOCAL_CRATE};
use rustc_target::spec::{Abi, Arch, BinaryFormat, LinkSelfContainedComponents};
use rustc_target::spec::{Abi, Arch, BinaryFormat, Env, LinkSelfContainedComponents};
use crate::errors;
@ -79,7 +79,7 @@ pub fn walk_native_lib_search_dirs<R>(
// Mac Catalyst uses the macOS SDK, but to link to iOS-specific frameworks
// we must have the support library stubs in the library search path (#121430).
if let Some(sdk_root) = apple_sdk_root
&& sess.target.env == "macabi"
&& sess.target.env == Env::MacAbi
{
f(&sdk_root.join("System/iOSSupport/usr/lib"), false)?;
f(&sdk_root.join("System/iOSSupport/System/Library/Frameworks"), true)?;

View file

@ -242,7 +242,7 @@ pub(crate) fn default_configuration(sess: &Session) -> Cfg {
ins_sym!(sym::target_abi, sess.target.abi.desc_symbol());
ins_sym!(sym::target_arch, sess.target.arch.desc_symbol());
ins_str!(sym::target_endian, sess.target.endian.as_str());
ins_str!(sym::target_env, &sess.target.env);
ins_sym!(sym::target_env, sess.target.env.desc_symbol());
for family in sess.target.families.as_ref() {
ins_str!(sym::target_family, family);
@ -450,7 +450,7 @@ impl CheckCfg {
values_target_abi.insert(target.options.abi.desc_symbol());
values_target_arch.insert(target.arch.desc_symbol());
values_target_endian.insert(Symbol::intern(target.options.endian.as_str()));
values_target_env.insert(Symbol::intern(&target.options.env));
values_target_env.insert(target.options.env.desc_symbol());
values_target_family.extend(
target.options.families.iter().map(|family| Symbol::intern(family)),
);

View file

@ -4,7 +4,7 @@ use rustc_data_structures::fx::FxIndexSet;
use rustc_span::{Symbol, sym};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use crate::spec::{RelocModel, Target};
use crate::spec::{Env, RelocModel, Target};
def_reg_class! {
AArch64 AArch64InlineAsmRegClass {
@ -77,7 +77,7 @@ pub(crate) fn target_reserves_x18(target: &Target, target_features: &FxIndexSet<
// Note that +reserve-x18 is currently not set for the above targets.
target.os == "android"
|| target.os == "fuchsia"
|| target.env == "ohos"
|| target.env == Env::Ohos
|| target.is_like_darwin
|| target.is_like_windows
|| target_features.contains(&sym::reserve_x18)

View file

@ -1,7 +1,7 @@
use rustc_abi::TyAbiInterface;
use crate::callconv::{ArgAbi, FnAbi};
use crate::spec::HasTargetSpec;
use crate::spec::{Env, HasTargetSpec};
fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
if ret.layout.is_aggregate() {
@ -18,7 +18,7 @@ where
if arg.is_ignore() {
// powerpc-unknown-linux-{gnu,musl,uclibc} doesn't ignore ZSTs.
if cx.target_spec().os == "linux"
&& matches!(&*cx.target_spec().env, "gnu" | "musl" | "uclibc")
&& matches!(cx.target_spec().env, Env::Gnu | Env::Musl | Env::Uclibc)
&& arg.layout.is_zst()
{
arg.make_indirect_from_ignore();

View file

@ -5,7 +5,7 @@
use rustc_abi::{Endian, HasDataLayout, TyAbiInterface};
use crate::callconv::{Align, ArgAbi, FnAbi, Reg, RegKind, Uniform};
use crate::spec::HasTargetSpec;
use crate::spec::{Env, HasTargetSpec};
#[derive(Debug, Clone, Copy, PartialEq)]
enum ABI {
@ -106,7 +106,7 @@ where
Ty: TyAbiInterface<'a, C> + Copy,
C: HasDataLayout + HasTargetSpec,
{
let abi = if cx.target_spec().env == "musl" || cx.target_spec().os == "freebsd" {
let abi = if cx.target_spec().env == Env::Musl || cx.target_spec().os == "freebsd" {
ELFv2
} else if cx.target_spec().os == "aix" {
AIX

View file

@ -4,7 +4,7 @@
use rustc_abi::{BackendRepr, HasDataLayout, TyAbiInterface};
use crate::callconv::{ArgAbi, FnAbi, Reg, RegKind};
use crate::spec::HasTargetSpec;
use crate::spec::{Env, HasTargetSpec};
fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
let size = ret.layout.size;
@ -30,7 +30,7 @@ where
if arg.is_ignore() {
// s390x-unknown-linux-{gnu,musl,uclibc} doesn't ignore ZSTs.
if cx.target_spec().os == "linux"
&& matches!(&*cx.target_spec().env, "gnu" | "musl" | "uclibc")
&& matches!(cx.target_spec().env, Env::Gnu | Env::Musl | Env::Uclibc)
&& arg.layout.is_zst()
{
arg.make_indirect_from_ignore();

View file

@ -6,7 +6,7 @@ use rustc_abi::{
};
use crate::callconv::{ArgAbi, ArgAttribute, CastTarget, FnAbi, Uniform};
use crate::spec::HasTargetSpec;
use crate::spec::{Env, HasTargetSpec};
#[derive(Clone, Debug)]
struct Sdata {
@ -224,7 +224,7 @@ where
if arg.is_ignore() {
// sparc64-unknown-linux-{gnu,musl,uclibc} doesn't ignore ZSTs.
if cx.target_spec().os == "linux"
&& matches!(&*cx.target_spec().env, "gnu" | "musl" | "uclibc")
&& matches!(cx.target_spec().env, Env::Gnu | Env::Musl | Env::Uclibc)
&& arg.layout.is_zst()
{
arg.make_indirect_from_ignore();

View file

@ -4,7 +4,7 @@ use std::num::ParseIntError;
use std::str::FromStr;
use crate::spec::{
Abi, BinaryFormat, Cc, DebuginfoKind, FloatAbi, FramePointer, LinkerFlavor, Lld, RustcAbi,
Abi, BinaryFormat, Cc, DebuginfoKind, Env, FloatAbi, FramePointer, LinkerFlavor, Lld, RustcAbi,
SplitDebuginfo, StackProbeType, StaticCow, Target, TargetOptions, cvs,
};
@ -94,11 +94,11 @@ pub(crate) enum TargetEnv {
}
impl TargetEnv {
fn target_env(self) -> &'static str {
fn target_env(self) -> Env {
match self {
Self::Normal => "",
Self::MacCatalyst => "macabi",
Self::Simulator => "sim",
Self::Normal => Env::Unspecified,
Self::MacCatalyst => Env::MacAbi,
Self::Simulator => Env::Sim,
}
}
@ -121,7 +121,7 @@ pub(crate) fn base(
let mut opts = TargetOptions {
llvm_floatabi: Some(FloatAbi::Hard),
os: os.into(),
env: env.target_env().into(),
env: env.target_env(),
// NOTE: We originally set `cfg(target_abi = "macabi")` / `cfg(target_abi = "sim")`,
// before it was discovered that those are actually environments:
// https://github.com/rust-lang/rust/issues/133331
@ -319,18 +319,18 @@ impl OSVersion {
/// This matches what LLVM does, see in part:
/// <https://github.com/llvm/llvm-project/blob/llvmorg-21.1.3/llvm/lib/TargetParser/Triple.cpp#L2140-L2175>
pub fn minimum_deployment_target(target: &Target) -> Self {
let (major, minor, patch) = match (&*target.os, &target.arch, &*target.env) {
let (major, minor, patch) = match (&*target.os, &target.arch, &target.env) {
("macos", crate::spec::Arch::AArch64, _) => (11, 0, 0),
("ios", crate::spec::Arch::AArch64, "macabi") => (14, 0, 0),
("ios", crate::spec::Arch::AArch64, "sim") => (14, 0, 0),
("ios", crate::spec::Arch::AArch64, Env::MacAbi) => (14, 0, 0),
("ios", crate::spec::Arch::AArch64, Env::Sim) => (14, 0, 0),
("ios", _, _) if target.llvm_target.starts_with("arm64e") => (14, 0, 0),
// Mac Catalyst defaults to 13.1 in Clang.
("ios", _, "macabi") => (13, 1, 0),
("tvos", crate::spec::Arch::AArch64, "sim") => (14, 0, 0),
("watchos", crate::spec::Arch::AArch64, "sim") => (7, 0, 0),
("ios", _, Env::MacAbi) => (13, 1, 0),
("tvos", crate::spec::Arch::AArch64, Env::Sim) => (14, 0, 0),
("watchos", crate::spec::Arch::AArch64, Env::Sim) => (7, 0, 0),
// True Aarch64 on watchOS (instead of their Aarch64 Ilp32 called `arm64_32`) has been
// available since Xcode 14, but it's only actually used more recently in watchOS 26.
("watchos", crate::spec::Arch::AArch64, "")
("watchos", crate::spec::Arch::AArch64, Env::Unspecified)
if !target.llvm_target.starts_with("arm64_32") =>
{
(26, 0, 0)

View file

@ -1,10 +1,10 @@
use super::OSVersion;
use crate::spec::Abi;
use crate::spec::targets::{
aarch64_apple_darwin, aarch64_apple_ios_sim, aarch64_apple_visionos_sim,
aarch64_apple_watchos_sim, i686_apple_darwin, x86_64_apple_darwin, x86_64_apple_ios,
x86_64_apple_tvos, x86_64_apple_watchos_sim,
};
use crate::spec::{Abi, Env};
#[test]
fn simulator_targets_set_env() {
@ -19,7 +19,7 @@ fn simulator_targets_set_env() {
];
for target in &all_sim_targets {
assert_eq!(target.env, "sim");
assert_eq!(target.env, Env::Sim);
// Ensure backwards compat
assert_eq!(target.abi, Abi::Sim);
}

View file

@ -1,5 +1,5 @@
use crate::spec::{TargetOptions, base};
use crate::spec::{Env, TargetOptions, base};
pub(crate) fn opts() -> TargetOptions {
TargetOptions { env: "gnu".into(), ..base::hurd::opts() }
TargetOptions { env: Env::Gnu, ..base::hurd::opts() }
}

View file

@ -1,9 +1,9 @@
use crate::spec::{Cc, LinkerFlavor, PanicStrategy, RelocModel, TargetOptions, cvs};
use crate::spec::{Cc, Env, LinkerFlavor, PanicStrategy, RelocModel, TargetOptions, cvs};
pub(crate) fn opts() -> TargetOptions {
TargetOptions {
os: "l4re".into(),
env: "uclibc".into(),
env: Env::Uclibc,
linker_flavor: LinkerFlavor::Unix(Cc::No),
panic_strategy: PanicStrategy::Abort,
linker: Some("l4-bender".into()),

View file

@ -1,7 +1,7 @@
use crate::spec::{Cc, LinkerFlavor, Lld, TargetOptions, base};
use crate::spec::{Cc, Env, LinkerFlavor, Lld, TargetOptions, base};
pub(crate) fn opts() -> TargetOptions {
let mut base = TargetOptions { env: "gnu".into(), ..base::linux::opts() };
let mut base = TargetOptions { env: Env::Gnu, ..base::linux::opts() };
// When we're asked to use the `rust-lld` linker by default, set the appropriate lld-using
// linker flavor, and self-contained linker component.

View file

@ -1,8 +1,8 @@
use crate::spec::{LinkSelfContainedDefault, TargetOptions, base, crt_objects};
use crate::spec::{Env, LinkSelfContainedDefault, TargetOptions, base, crt_objects};
pub(crate) fn opts() -> TargetOptions {
TargetOptions {
env: "musl".into(),
env: Env::Musl,
pre_link_objects_self_contained: crt_objects::pre_musl_self_contained(),
post_link_objects_self_contained: crt_objects::post_musl_self_contained(),
link_self_contained: LinkSelfContainedDefault::InferredForMusl,

View file

@ -1,8 +1,8 @@
use crate::spec::{TargetOptions, TlsModel, base};
use crate::spec::{Env, TargetOptions, TlsModel, base};
pub(crate) fn opts() -> TargetOptions {
TargetOptions {
env: "ohos".into(),
env: Env::Ohos,
crt_static_default: false,
tls_model: TlsModel::Emulated,
has_thread_local: false,

View file

@ -1,5 +1,5 @@
use crate::spec::{TargetOptions, base};
use crate::spec::{Env, TargetOptions, base};
pub(crate) fn opts() -> TargetOptions {
TargetOptions { env: "uclibc".into(), ..base::linux::opts() }
TargetOptions { env: Env::Uclibc, ..base::linux::opts() }
}

View file

@ -2,8 +2,8 @@
//! aspects from their respective base targets
use crate::spec::{
Cc, LinkSelfContainedDefault, LinkerFlavor, PanicStrategy, RelocModel, TargetOptions, TlsModel,
add_link_args, crt_objects, cvs,
Cc, Env, LinkSelfContainedDefault, LinkerFlavor, PanicStrategy, RelocModel, TargetOptions,
TlsModel, add_link_args, crt_objects, cvs,
};
pub(crate) fn opts() -> TargetOptions {
@ -58,7 +58,7 @@ pub(crate) fn opts() -> TargetOptions {
is_like_wasm: true,
families: cvs!["wasm", "unix"],
os: "linux".into(),
env: "musl".into(),
env: Env::Musl,
// we allow dynamic linking, but only cdylibs. Basically we allow a
// final library artifact that exports some symbols (a wasm module) but

View file

@ -1,9 +1,9 @@
use crate::spec::{RelroLevel, TargetOptions, cvs};
use crate::spec::{Env, RelroLevel, TargetOptions, cvs};
pub(crate) fn opts() -> TargetOptions {
TargetOptions {
os: "managarm".into(),
env: "mlibc".into(),
env: Env::Mlibc,
dynamic_linking: true,
executables: true,
families: cvs!["unix"],

View file

@ -1,9 +1,9 @@
use crate::spec::{Cc, LinkerFlavor, Lld, RelroLevel, TargetOptions, cvs};
use crate::spec::{Cc, Env, LinkerFlavor, Lld, RelroLevel, TargetOptions, cvs};
pub(crate) fn opts() -> TargetOptions {
TargetOptions {
os: "redox".into(),
env: "relibc".into(),
env: Env::Relibc,
dynamic_linking: true,
families: cvs!["unix"],
has_rpath: true,

View file

@ -1,9 +1,9 @@
use crate::spec::{PanicStrategy, RelocModel, TargetOptions, cvs};
use crate::spec::{Env, PanicStrategy, RelocModel, TargetOptions, cvs};
pub(crate) fn opts() -> TargetOptions {
TargetOptions {
os: "linux".into(),
env: "musl".into(),
env: Env::Musl,
vendor: "unikraft".into(),
linker: Some("kraftld".into()),
relocation_model: RelocModel::Static,

View file

@ -1,9 +1,9 @@
use crate::spec::{TargetOptions, cvs};
use crate::spec::{Env, TargetOptions, cvs};
pub(crate) fn opts() -> TargetOptions {
TargetOptions {
os: "vxworks".into(),
env: "gnu".into(),
env: Env::Gnu,
vendor: "wrs".into(),
linker: Some("wr-c++".into()),
exe_suffix: ".vxe".into(),

View file

@ -1,8 +1,8 @@
use std::borrow::Cow;
use crate::spec::{
BinaryFormat, Cc, DebuginfoKind, LinkSelfContainedDefault, LinkerFlavor, Lld, SplitDebuginfo,
TargetOptions, add_link_args, crt_objects, cvs,
BinaryFormat, Cc, DebuginfoKind, Env, LinkSelfContainedDefault, LinkerFlavor, Lld,
SplitDebuginfo, TargetOptions, add_link_args, crt_objects, cvs,
};
pub(crate) fn opts() -> TargetOptions {
@ -78,7 +78,7 @@ pub(crate) fn opts() -> TargetOptions {
TargetOptions {
os: "windows".into(),
env: "gnu".into(),
env: Env::Gnu,
vendor: "pc".into(),
// FIXME(#13846) this should be enabled for windows
function_sections: false,

View file

@ -1,7 +1,8 @@
use std::borrow::Cow;
use crate::spec::{
Abi, BinaryFormat, Cc, DebuginfoKind, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions, cvs,
Abi, BinaryFormat, Cc, DebuginfoKind, Env, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions,
cvs,
};
pub(crate) fn opts() -> TargetOptions {
@ -21,7 +22,7 @@ pub(crate) fn opts() -> TargetOptions {
TargetOptions {
os: "windows".into(),
env: "gnu".into(),
env: Env::Gnu,
vendor: "pc".into(),
abi: Abi::Llvm,
linker: Some("clang".into()),

View file

@ -1,11 +1,11 @@
use crate::spec::{TargetOptions, base, cvs};
use crate::spec::{Env, TargetOptions, base, cvs};
pub(crate) fn opts() -> TargetOptions {
let base = base::msvc::opts();
TargetOptions {
os: "windows".into(),
env: "msvc".into(),
env: Env::Msvc,
vendor: "pc".into(),
dynamic_linking: true,
dll_prefix: "".into(),

View file

@ -5,7 +5,7 @@ use rustc_abi::{Align, AlignFromBytesError};
use super::crt_objects::CrtObjects;
use super::{
Abi, Arch, BinaryFormat, CodeModel, DebuginfoKind, FloatAbi, FramePointer, LinkArgsCli,
Abi, Arch, BinaryFormat, CodeModel, DebuginfoKind, Env, FloatAbi, FramePointer, LinkArgsCli,
LinkSelfContainedComponents, LinkSelfContainedDefault, LinkerFlavorCli, LldFlavor,
MergeFunctions, PanicStrategy, RelocModel, RelroLevel, RustcAbi, SanitizerSet,
SmallDataThresholdSupport, SplitDebuginfo, StackProbeType, StaticCow, SymbolVisibility, Target,
@ -506,7 +506,7 @@ struct TargetSpecJson {
c_int_width: Option<u16>,
c_enum_min_bits: Option<u64>,
os: Option<StaticCow<str>>,
env: Option<StaticCow<str>>,
env: Option<Env>,
abi: Option<Abi>,
vendor: Option<StaticCow<str>>,
linker: Option<StaticCow<str>>,

View file

@ -1926,6 +1926,38 @@ impl Arch {
}
}
crate::target_spec_enum! {
pub enum Env {
Gnu = "gnu",
MacAbi = "macabi",
Mlibc = "mlibc",
Msvc = "msvc",
Musl = "musl",
Newlib = "newlib",
Nto70 = "nto70",
Nto71 = "nto71",
Nto71IoSock = "nto71_iosock",
Nto80 = "nto80",
Ohos = "ohos",
Relibc = "relibc",
Sgx = "sgx",
Sim = "sim",
P1 = "p1",
P2 = "p2",
P3 = "p3",
Uclibc = "uclibc",
V5 = "v5",
Unspecified = "",
}
other_variant = Other;
}
impl Env {
pub fn desc_symbol(&self) -> Symbol {
Symbol::intern(self.desc())
}
}
crate::target_spec_enum! {
pub enum Abi {
Abi64 = "abi64",
@ -2081,8 +2113,8 @@ pub struct TargetOptions {
/// A couple of targets having `std` also use "unknown" as an `os` value,
/// but they are exceptions.
pub os: StaticCow<str>,
/// Environment name to use for conditional compilation (`target_env`). Defaults to "".
pub env: StaticCow<str>,
/// Environment name to use for conditional compilation (`target_env`). Defaults to [`Env::Unspecified`].
pub env: Env,
/// ABI name to distinguish multiple ABIs on the same OS and architecture. For instance, `"eabi"`
/// or `"eabihf"`. Defaults to [`Abi::Unspecified`].
/// This field is *not* forwarded directly to LLVM; its primary purpose is `cfg(target_abi)`.
@ -2591,7 +2623,7 @@ impl Default for TargetOptions {
endian: Endian::Little,
c_int_width: 32,
os: "none".into(),
env: "".into(),
env: Env::Unspecified,
abi: Abi::Unspecified,
vendor: "unknown".into(),
linker: option_env!("CFG_DEFAULT_LINKER").map(|s| s.into()),
@ -3148,7 +3180,7 @@ impl Target {
fn can_use_os_unknown(&self) -> bool {
self.llvm_target == "wasm32-unknown-unknown"
|| self.llvm_target == "wasm64-unknown-unknown"
|| (self.env == "sgx" && self.vendor == "fortanix")
|| (self.env == Env::Sgx && self.vendor == "fortanix")
}
/// Load a built-in target

View file

@ -1,11 +1,11 @@
use crate::spec::Target;
use crate::spec::base::nto_qnx;
use crate::spec::{Env, Target};
pub(crate) fn target() -> Target {
let mut target = nto_qnx::aarch64();
target.metadata.description = Some("ARM64 QNX Neutrino 7.0 RTOS".into());
target.options.pre_link_args =
nto_qnx::pre_link_args(nto_qnx::ApiVariant::Default, nto_qnx::Arch::Aarch64);
target.options.env = "nto70".into();
target.options.env = Env::Nto70;
target
}

View file

@ -1,5 +1,5 @@
use crate::spec::Target;
use crate::spec::base::nto_qnx;
use crate::spec::{Env, Target};
pub(crate) fn target() -> Target {
let mut target = nto_qnx::aarch64();
@ -7,6 +7,6 @@ pub(crate) fn target() -> Target {
Some("ARM64 QNX Neutrino 7.1 RTOS with io-pkt network stack".into());
target.options.pre_link_args =
nto_qnx::pre_link_args(nto_qnx::ApiVariant::Default, nto_qnx::Arch::Aarch64);
target.options.env = "nto71".into();
target.options.env = Env::Nto71;
target
}

View file

@ -1,5 +1,5 @@
use crate::spec::Target;
use crate::spec::base::nto_qnx;
use crate::spec::{Env, Target};
pub(crate) fn target() -> Target {
let mut target = nto_qnx::aarch64();
@ -7,6 +7,6 @@ pub(crate) fn target() -> Target {
Some("ARM64 QNX Neutrino 7.1 RTOS with io-sock network stack".into());
target.options.pre_link_args =
nto_qnx::pre_link_args(nto_qnx::ApiVariant::IoSock, nto_qnx::Arch::Aarch64);
target.options.env = "nto71_iosock".into();
target.options.env = Env::Nto71IoSock;
target
}

View file

@ -1,11 +1,11 @@
use crate::spec::Target;
use crate::spec::base::nto_qnx;
use crate::spec::{Env, Target};
pub(crate) fn target() -> Target {
let mut target = nto_qnx::aarch64();
target.metadata.description = Some("ARM64 QNX Neutrino 8.0 RTOS".into());
target.options.pre_link_args =
nto_qnx::pre_link_args(nto_qnx::ApiVariant::Default, nto_qnx::Arch::Aarch64);
target.options.env = "nto80".into();
target.options.env = Env::Nto80;
target
}

View file

@ -1,6 +1,6 @@
use crate::spec::{
Abi, Arch, Cc, FloatAbi, LinkerFlavor, Lld, RelocModel, Target, TargetMetadata, TargetOptions,
cvs,
Abi, Arch, Cc, Env, FloatAbi, LinkerFlavor, Lld, RelocModel, Target, TargetMetadata,
TargetOptions, cvs,
};
/// A base target for Nintendo 3DS devices using the devkitARM toolchain.
@ -26,7 +26,7 @@ pub(crate) fn target() -> Target {
options: TargetOptions {
os: "horizon".into(),
env: "newlib".into(),
env: Env::Newlib,
vendor: "nintendo".into(),
cpu: "mpcore".into(),
abi: Abi::EabiHf,

View file

@ -1,6 +1,6 @@
use crate::spec::{
Abi, Arch, Cc, FloatAbi, LinkerFlavor, Lld, PanicStrategy, RelocModel, Target, TargetMetadata,
TargetOptions, cvs,
Abi, Arch, Cc, Env, FloatAbi, LinkerFlavor, Lld, PanicStrategy, RelocModel, Target,
TargetMetadata, TargetOptions, cvs,
};
pub(crate) fn target() -> Target {
@ -32,7 +32,7 @@ pub(crate) fn target() -> Target {
c_enum_min_bits: Some(8),
eh_frame_header: false,
no_default_libraries: false,
env: "newlib".into(),
env: Env::Newlib,
..Default::default()
},
}

View file

@ -1,8 +1,8 @@
use rustc_abi::Endian;
use crate::spec::{
Abi, Arch, Cc, FloatAbi, LinkerFlavor, Lld, RelocModel, Target, TargetMetadata, TargetOptions,
cvs,
Abi, Arch, Cc, Env, FloatAbi, LinkerFlavor, Lld, RelocModel, Target, TargetMetadata,
TargetOptions, cvs,
};
/// A base target for PlayStation Vita devices using the VITASDK toolchain (using newlib).
@ -32,7 +32,7 @@ pub(crate) fn target() -> Target {
os: "vita".into(),
endian: Endian::Little,
c_int_width: 32,
env: "newlib".into(),
env: Env::Newlib,
vendor: "sony".into(),
abi: Abi::EabiHf,
llvm_floatabi: Some(FloatAbi::Hard),

View file

@ -1,6 +1,6 @@
use crate::spec::{
Abi, Arch, Cc, FloatAbi, LinkerFlavor, Lld, PanicStrategy, RelocModel, Target, TargetMetadata,
TargetOptions,
Abi, Arch, Cc, Env, FloatAbi, LinkerFlavor, Lld, PanicStrategy, RelocModel, Target,
TargetMetadata, TargetOptions,
};
const LINKER_SCRIPT: &str = include_str!("./armv7a_vex_v5_linker_script.ld");
@ -8,7 +8,7 @@ const LINKER_SCRIPT: &str = include_str!("./armv7a_vex_v5_linker_script.ld");
pub(crate) fn target() -> Target {
let opts = TargetOptions {
vendor: "vex".into(),
env: "v5".into(),
env: Env::V5,
os: "vexos".into(),
cpu: "cortex-a9".into(),
abi: Abi::EabiHf,

View file

@ -1,5 +1,5 @@
use crate::spec::base::nto_qnx;
use crate::spec::{Arch, RustcAbi, StackProbeType, Target, TargetOptions, base};
use crate::spec::{Arch, Env, RustcAbi, StackProbeType, Target, TargetOptions, base};
pub(crate) fn target() -> Target {
let mut meta = nto_qnx::meta();
@ -21,7 +21,7 @@ pub(crate) fn target() -> Target {
nto_qnx::ApiVariant::Default,
nto_qnx::Arch::I586,
),
env: "nto70".into(),
env: Env::Nto70,
vendor: "pc".into(),
stack_probes: StackProbeType::Inline,
..base::nto_qnx::opts()

View file

@ -1,4 +1,6 @@
use crate::spec::{Arch, PanicStrategy, RelocModel, Target, TargetMetadata, TargetOptions, cvs};
use crate::spec::{
Arch, Env, PanicStrategy, RelocModel, Target, TargetMetadata, TargetOptions, cvs,
};
pub(crate) fn target() -> Target {
Target {
@ -16,7 +18,7 @@ pub(crate) fn target() -> Target {
options: TargetOptions {
families: cvs!["unix"],
os: "espidf".into(),
env: "newlib".into(),
env: Env::Newlib,
vendor: "espressif".into(),
linker: Some("riscv32-esp-elf-gcc".into()),
cpu: "generic-rv32".into(),

View file

@ -1,4 +1,6 @@
use crate::spec::{Arch, PanicStrategy, RelocModel, Target, TargetMetadata, TargetOptions, cvs};
use crate::spec::{
Arch, Env, PanicStrategy, RelocModel, Target, TargetMetadata, TargetOptions, cvs,
};
pub(crate) fn target() -> Target {
Target {
@ -16,7 +18,7 @@ pub(crate) fn target() -> Target {
options: TargetOptions {
families: cvs!["unix"],
os: "espidf".into(),
env: "newlib".into(),
env: Env::Newlib,
vendor: "espressif".into(),
linker: Some("riscv32-esp-elf-gcc".into()),
cpu: "generic-rv32".into(),

View file

@ -1,4 +1,6 @@
use crate::spec::{Arch, PanicStrategy, RelocModel, Target, TargetMetadata, TargetOptions, cvs};
use crate::spec::{
Arch, Env, PanicStrategy, RelocModel, Target, TargetMetadata, TargetOptions, cvs,
};
pub(crate) fn target() -> Target {
Target {
@ -16,7 +18,7 @@ pub(crate) fn target() -> Target {
options: TargetOptions {
families: cvs!["unix"],
os: "espidf".into(),
env: "newlib".into(),
env: Env::Newlib,
vendor: "espressif".into(),
linker: Some("riscv32-esp-elf-gcc".into()),
cpu: "generic-rv32".into(),

View file

@ -11,14 +11,15 @@
//! introduced.
use crate::spec::{
Arch, Cc, LinkSelfContainedDefault, LinkerFlavor, Target, TargetMetadata, base, crt_objects,
Arch, Cc, Env, LinkSelfContainedDefault, LinkerFlavor, Target, TargetMetadata, base,
crt_objects,
};
pub(crate) fn target() -> Target {
let mut options = base::wasm::options();
options.os = "wasi".into();
options.env = "p1".into();
options.env = Env::P1;
options.add_pre_link_args(LinkerFlavor::WasmLld(Cc::Yes), &["--target=wasm32-wasip1"]);
options.pre_link_objects_self_contained = crt_objects::pre_wasi_self_contained();

View file

@ -8,14 +8,15 @@
//! Historically this target was known as `wasm32-wasi-preview1-threads`.
use crate::spec::{
Arch, Cc, LinkSelfContainedDefault, LinkerFlavor, Target, TargetMetadata, base, crt_objects,
Arch, Cc, Env, LinkSelfContainedDefault, LinkerFlavor, Target, TargetMetadata, base,
crt_objects,
};
pub(crate) fn target() -> Target {
let mut options = base::wasm::options();
options.os = "wasi".into();
options.env = "p1".into();
options.env = Env::P1;
options.add_pre_link_args(
LinkerFlavor::WasmLld(Cc::No),

View file

@ -17,14 +17,14 @@
//! <https://github.com/WebAssembly/component-model>.
use crate::spec::{
Arch, LinkSelfContainedDefault, RelocModel, Target, TargetMetadata, base, crt_objects,
Arch, Env, LinkSelfContainedDefault, RelocModel, Target, TargetMetadata, base, crt_objects,
};
pub(crate) fn target() -> Target {
let mut options = base::wasm::options();
options.os = "wasi".into();
options.env = "p2".into();
options.env = Env::P2;
options.linker = Some("wasm-component-ld".into());
options.pre_link_objects_self_contained = crt_objects::pre_wasi_self_contained();

View file

@ -8,13 +8,13 @@
//! all component-model-level imports anyway. Over time the imports of the
//! standard library will change to WASIp3.
use crate::spec::Target;
use crate::spec::{Env, Target};
pub(crate) fn target() -> Target {
// As of now WASIp3 is a lightly edited wasip2 target, so start with that
// and this may grow over time as more features are supported.
let mut target = super::wasm32_wasip2::target();
target.llvm_target = "wasm32-wasip3".into();
target.options.env = "p3".into();
target.options.env = Env::P3;
target
}

View file

@ -1,6 +1,8 @@
use std::borrow::Cow;
use crate::spec::{Abi, Arch, Cc, LinkerFlavor, Lld, Target, TargetMetadata, TargetOptions, cvs};
use crate::spec::{
Abi, Arch, Cc, Env, LinkerFlavor, Lld, Target, TargetMetadata, TargetOptions, cvs,
};
pub(crate) fn target() -> Target {
let pre_link_args = TargetOptions::link_args(
@ -56,7 +58,7 @@ pub(crate) fn target() -> Target {
];
let opts = TargetOptions {
os: "unknown".into(),
env: "sgx".into(),
env: Env::Sgx,
vendor: "fortanix".into(),
abi: Abi::Fortanix,
linker_flavor: LinkerFlavor::Gnu(Cc::No, Lld::Yes),

View file

@ -1,5 +1,5 @@
use crate::spec::Target;
use crate::spec::base::nto_qnx;
use crate::spec::{Env, Target};
pub(crate) fn target() -> Target {
let mut target = nto_qnx::x86_64();
@ -7,6 +7,6 @@ pub(crate) fn target() -> Target {
Some("x86 64-bit QNX Neutrino 7.1 RTOS with io-pkt network stack".into());
target.options.pre_link_args =
nto_qnx::pre_link_args(nto_qnx::ApiVariant::Default, nto_qnx::Arch::X86_64);
target.options.env = "nto71".into();
target.options.env = Env::Nto71;
target
}

View file

@ -1,5 +1,5 @@
use crate::spec::Target;
use crate::spec::base::nto_qnx;
use crate::spec::{Env, Target};
pub(crate) fn target() -> Target {
let mut target = nto_qnx::x86_64();
@ -7,6 +7,6 @@ pub(crate) fn target() -> Target {
Some("x86 64-bit QNX Neutrino 7.1 RTOS with io-sock network stack".into());
target.options.pre_link_args =
nto_qnx::pre_link_args(nto_qnx::ApiVariant::IoSock, nto_qnx::Arch::X86_64);
target.options.env = "nto71_iosock".into();
target.options.env = Env::Nto71IoSock;
target
}

View file

@ -1,11 +1,11 @@
use crate::spec::Target;
use crate::spec::base::nto_qnx;
use crate::spec::{Env, Target};
pub(crate) fn target() -> Target {
let mut target = nto_qnx::x86_64();
target.metadata.description = Some("x86 64-bit QNX Neutrino 8.0 RTOS".into());
target.options.pre_link_args =
nto_qnx::pre_link_args(nto_qnx::ApiVariant::Default, nto_qnx::Arch::X86_64);
target.options.env = "nto80".into();
target.options.env = Env::Nto80;
target
}

View file

@ -1,7 +1,7 @@
use rustc_abi::Endian;
use crate::spec::base::xtensa;
use crate::spec::{Arch, Target, TargetMetadata, TargetOptions, cvs};
use crate::spec::{Arch, Env, Target, TargetMetadata, TargetOptions, cvs};
pub(crate) fn target() -> Target {
Target {
@ -16,7 +16,7 @@ pub(crate) fn target() -> Target {
c_int_width: 32,
families: cvs!["unix"],
os: "espidf".into(),
env: "newlib".into(),
env: Env::Newlib,
vendor: "espressif".into(),
executables: true,

View file

@ -1,7 +1,7 @@
use rustc_abi::Endian;
use crate::spec::base::xtensa;
use crate::spec::{Arch, Target, TargetMetadata, TargetOptions, cvs};
use crate::spec::{Arch, Env, Target, TargetMetadata, TargetOptions, cvs};
pub(crate) fn target() -> Target {
Target {
@ -16,7 +16,7 @@ pub(crate) fn target() -> Target {
c_int_width: 32,
families: cvs!["unix"],
os: "espidf".into(),
env: "newlib".into(),
env: Env::Newlib,
vendor: "espressif".into(),
executables: true,

View file

@ -1,7 +1,7 @@
use rustc_abi::Endian;
use crate::spec::base::xtensa;
use crate::spec::{Arch, Target, TargetMetadata, TargetOptions, cvs};
use crate::spec::{Arch, Env, Target, TargetMetadata, TargetOptions, cvs};
pub(crate) fn target() -> Target {
Target {
@ -16,7 +16,7 @@ pub(crate) fn target() -> Target {
c_int_width: 32,
families: cvs!["unix"],
os: "espidf".into(),
env: "newlib".into(),
env: Env::Newlib,
vendor: "espressif".into(),
executables: true,

View file

@ -6,7 +6,7 @@ use rustc_abi::{Align, CanonAbi, Size, X86Call};
use rustc_middle::ty::Ty;
use rustc_span::Symbol;
use rustc_target::callconv::FnAbi;
use rustc_target::spec::Arch;
use rustc_target::spec::{Arch, Env};
use self::shims::windows::handle::{Handle, PseudoHandle};
use crate::shims::os_str::bytes_to_os_str;
@ -826,7 +826,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// It was originally specified as part of the Itanium C++ ABI:
// https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html#base-throw.
// MinGW implements _Unwind_RaiseException on top of SEH exceptions.
if this.tcx.sess.target.env != "gnu" {
if this.tcx.sess.target.env != Env::Gnu {
throw_unsup_format!(
"`_Unwind_RaiseException` is not supported on non-MinGW Windows",
);