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