1#![cfg_attr(bootstrap, feature(debug_closure_helpers))]
12#![expect(internal_features)]
13#![feature(iter_intersperse)]
14#![feature(rustc_attrs)]
15use std::path::{Path, PathBuf};
18
19pub mod asm;
20pub mod callconv;
21pub mod json;
22pub mod spec;
23pub mod target_features;
24
25#[cfg(test)]
26mod tests;
27
28use rustc_abi::HashStableContext;
29
30const RUST_LIB_DIR: &str = "rustlib";
34
35pub fn relative_target_rustlib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
40 let libdir = find_relative_libdir(sysroot);
41 Path::new(libdir.as_ref()).join(RUST_LIB_DIR).join(target_triple)
42}
43
44fn find_relative_libdir(sysroot: &Path) -> std::borrow::Cow<'static, str> {
46 #[cfg(target_pointer_width = "64")]
55 const PRIMARY_LIB_DIR: &str = "lib64";
56
57 #[cfg(target_pointer_width = "32")]
58 const PRIMARY_LIB_DIR: &str = "lib32";
59
60 const SECONDARY_LIB_DIR: &str = "lib";
61
62 match option_env!("CFG_LIBDIR_RELATIVE") {
63 None | Some("lib") => {
64 if sysroot.join(PRIMARY_LIB_DIR).join(RUST_LIB_DIR).exists() {
65 PRIMARY_LIB_DIR.into()
66 } else {
67 SECONDARY_LIB_DIR.into()
68 }
69 }
70 Some(libdir) => libdir.into(),
71 }
72}
73
74macro_rules! target_spec_enum {
75 (
76 $( #[$attr:meta] )*
77 pub enum $Name:ident {
78 $(
79 $( #[$variant_attr:meta] )*
80 $Variant:ident = $string:literal,
81 )*
82 }
83 parse_error_type = $parse_error_type:literal;
84 ) => {
85 $( #[$attr] )*
86 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
87 #[derive(schemars::JsonSchema)]
88 pub enum $Name {
89 $(
90 $( #[$variant_attr] )*
91 #[serde(rename = $string)] $Variant,
93 )*
94 }
95
96 impl FromStr for $Name {
97 type Err = String;
98
99 fn from_str(s: &str) -> Result<Self, Self::Err> {
100 Ok(match s {
101 $( $string => Self::$Variant, )*
102 _ => {
103 let all = [$( concat!("'", $string, "'") ),*].join(", ");
104 return Err(format!("invalid {}: '{s}'. allowed values: {all}", $parse_error_type));
105 }
106 })
107 }
108 }
109
110 impl $Name {
111 pub const ALL: &'static [$Name] = &[ $( $Name::$Variant, )* ];
112 pub fn desc(&self) -> &'static str {
113 match self {
114 $( Self::$Variant => $string, )*
115 }
116 }
117 }
118
119 crate::target_spec_enum!(@common_impls $Name);
120 };
121
122 (
123 $( #[$attr:meta] )*
124 pub enum $Name:ident {
125 $(
126 $( #[$variant_attr:meta] )*
127 $Variant:ident = $string:literal,
128 )*
129 }
130 $( #[$other_variant_attr:meta] )*
131 other_variant = $OtherVariant:ident;
132 ) => {
133 $( #[$attr] )*
134 #[derive(Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
135 pub enum $Name {
136 $(
137 $( #[$variant_attr:meta] )*
138 $Variant,
139 )*
140 $( #[$other_variant_attr] )*
149 $OtherVariant(crate::spec::StaticCow<str>),
150 }
151
152 impl schemars::JsonSchema for $Name {
153 fn schema_name() -> std::borrow::Cow<'static, str> {
154 std::borrow::Cow::Borrowed(stringify!($Name))
155 }
156
157 fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
158 schemars::json_schema!({
159 "type": "string"
160 })
161 }
162 }
163
164 impl FromStr for $Name {
165 type Err = core::convert::Infallible;
166
167 fn from_str(s: &str) -> Result<Self, Self::Err> {
168 Ok(match s {
169 $( $string => Self::$Variant, )*
170 _ => Self::$OtherVariant(s.to_owned().into()),
171 })
172 }
173 }
174
175 impl $Name {
176 pub fn desc(&self) -> &str {
177 match self {
178 $( Self::$Variant => $string, )*
179 Self::$OtherVariant(name) => name.as_ref(),
180 }
181 }
182 }
183
184 crate::target_spec_enum!(@common_impls $Name);
185 };
186
187 (@common_impls $Name:ident) => {
188 impl crate::json::ToJson for $Name {
189 fn to_json(&self) -> crate::json::Json {
190 self.desc().to_json()
191 }
192 }
193
194 crate::json::serde_deserialize_from_str!($Name);
195
196
197 impl std::fmt::Display for $Name {
198 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
199 f.write_str(self.desc())
200 }
201 }
202 };
203}
204use target_spec_enum;