#![expect(internal_features)]
#[cfg(test)]
use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use eko::path::{Path, PathBuf};
pub mod asm;
pub mod callconv;
pub mod json;
pub mod spec;
pub mod target_features;
#[cfg(test)]
mod tests;
const RUST_LIB_DIR: &str = "rustlib";
pub fn relative_target_rustlib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
let libdir = find_relative_libdir(sysroot);
Path::new(libdir.as_ref()).join(RUST_LIB_DIR).join(target_triple)
}
fn find_relative_libdir(sysroot: &Path) -> alloc::borrow::Cow<'static, str> {
#[cfg(target_pointer_width = "64")]
const PRIMARY_LIB_DIR: &str = "lib64";
#[cfg(target_pointer_width = "32")]
const PRIMARY_LIB_DIR: &str = "lib32";
const SECONDARY_LIB_DIR: &str = "lib";
match option_env!("CFG_LIBDIR_RELATIVE") {
None | Some("lib") => {
if sysroot.join(PRIMARY_LIB_DIR).join(RUST_LIB_DIR).exists() {
PRIMARY_LIB_DIR.into()
} else {
SECONDARY_LIB_DIR.into()
}
}
Some(libdir) => libdir.into(),
}
}
macro_rules! target_spec_enum {
(
$( #[$attr:meta] )*
pub enum $Name:ident {
$(
$( #[$variant_attr:meta] )*
$Variant:ident = $string:literal $(,$alias:literal)* ,
)*
}
parse_error_type = $parse_error_type:literal;
) => {
$( #[$attr] )*
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
pub enum $Name {
$(
$( #[$variant_attr] )*
$Variant,
)*
}
impl FromStr for $Name {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
$(
$string => Self::$Variant,
$($alias => Self::$Variant,)*
)*
_ => {
let all = [$( concat!("'", $string, "'") ),*].join(", ");
return Err(format!("invalid {}: '{s}'. allowed values: {all}", $parse_error_type));
}
})
}
}
impl $Name {
pub const ALL: &'static [$Name] = &[ $( $Name::$Variant, )* ];
pub fn desc(&self) -> &'static str {
match self {
$( Self::$Variant => $string, )*
}
}
}
crate::rustc_target::target_spec_enum!(@common_impls $Name);
};
(
$( #[$attr:meta] )*
pub enum $Name:ident {
$(
$( #[$variant_attr:meta] )*
$Variant:ident = $string:literal $(,$alias:literal)* ,
)*
}
$( #[$other_variant_attr:meta] )*
other_variant = $OtherVariant:ident;
) => {
$( #[$attr] )*
#[derive(Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
pub enum $Name {
$(
$( #[$variant_attr:meta] )*
$Variant,
)*
$( #[$other_variant_attr] )*
$OtherVariant(crate::rustc_target::spec::StaticCow<str>),
}
impl FromStr for $Name {
type Err = core::convert::Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
$(
$string => Self::$Variant,
$($alias => Self::$Variant,)*
)*
_ => Self::$OtherVariant(s.to_owned().into()),
})
}
}
impl $Name {
pub fn desc(&self) -> &str {
match self {
$( Self::$Variant => $string, )*
Self::$OtherVariant(name) => name.as_ref(),
}
}
}
crate::rustc_target::target_spec_enum!(@common_impls $Name);
};
(@common_impls $Name:ident) => {
impl crate::rustc_target::json::ToJson for $Name {
fn to_json(&self) -> crate::rustc_target::json::Json {
self.desc().to_json()
}
}
crate::rustc_target::json::serde_deserialize_from_str!($Name);
impl core::fmt::Display for $Name {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.desc())
}
}
};
}
use target_spec_enum;