1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use crate::error::{HasAtomicParseError, Reason};
use std::{borrow::Cow, ops::Deref};

mod builtins;

/// A list of all of the [builtin](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_target/spec/index.html#modules)
/// targets known to rustc, as of 1.54.0
pub use builtins::ALL_BUILTINS;

/// The unique identifier for a target.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Triple(pub Cow<'static, str>);

/// The "abi" field
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Abi(pub Cow<'static, str>);

/// The "architecture" field
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Arch(pub Cow<'static, str>);

/// The "vendor" field, which in practice is little more than an arbitrary modifier.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Vendor(pub Cow<'static, str>);

/// The "operating system" field, which sometimes implies an environment, and
/// sometimes isn't an actual operating system.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Os(pub Cow<'static, str>);

/// Individual target families, which describe a set of targets grouped in some logical manner,
/// typically by operating system. This includes values like `unix` and `windows`.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Family(pub Cow<'static, str>);

/// The "environment" field, which specifies an ABI environment on top of the
/// operating system. In many configurations, this field is omitted, and the
/// environment is implied by the operating system.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Env(pub Cow<'static, str>);

/// The panic strategy used on this target by default.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Panic(pub Cow<'static, str>);

macro_rules! field_impls {
    ($kind:ident) => {
        impl $kind {
            /// Constructs a new instance of this field.
            ///
            /// This method accepts both owned `String`s and `&'static str`s.
            #[inline]
            pub fn new(val: impl Into<Cow<'static, str>>) -> Self {
                Self(val.into())
            }

            /// Constructs a new instance of this field from a `&'static str`.
            #[inline]
            pub const fn new_const(val: &'static str) -> Self {
                Self(Cow::Borrowed(val))
            }

            /// Returns the string for the field.
            #[inline]
            pub fn as_str(&self) -> &str {
                &*self.0
            }
        }

        impl AsRef<str> for $kind {
            #[inline]
            fn as_ref(&self) -> &str {
                &*self.0
            }
        }

        impl std::fmt::Display for $kind {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.write_str(self.as_str())
            }
        }
    };
}

field_impls!(Triple);
field_impls!(Abi);
field_impls!(Arch);
field_impls!(Vendor);
field_impls!(Os);
field_impls!(Family);
field_impls!(Env);
field_impls!(Panic);

/// Integer size and pointers for which there's support for atomic functions.
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[non_exhaustive]
pub enum HasAtomic {
    /// The platform supports atomics for the given integer size in bits (e.g. `AtomicU8` if
    /// `HasAtomic::IntegerSize(8)`).
    IntegerSize(u16),

    /// The platform supports atomics for pointers (`AtomicPtr`).
    Pointer,
}

impl std::str::FromStr for HasAtomic {
    type Err = HasAtomicParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Ok(size) = s.parse::<u16>() {
            Ok(Self::IntegerSize(size))
        } else if s == "ptr" {
            Ok(HasAtomic::Pointer)
        } else {
            Err(HasAtomicParseError {
                input: s.to_owned(),
            })
        }
    }
}

impl std::fmt::Display for HasAtomic {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::IntegerSize(size) => write!(f, "{size}"),
            Self::Pointer => write!(f, "ptr"),
        }
    }
}

/// A set of families for a target.
///
/// Each target can be part of one or more families. This struct represents them.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Families(Cow<'static, [Family]>);

impl Families {
    /// Constructs a new instance.
    ///
    /// This method accepts both owned `String`s and `&'static str`s.
    ///
    /// If you have a `&'static [&'static str]`, prefer [`Self::new_const`].
    #[inline]
    pub fn new(val: impl IntoIterator<Item = Family>) -> Self {
        let mut fams: Vec<_> = val.into_iter().collect();
        fams.sort_unstable();
        Self(Cow::Owned(fams))
    }

    /// Constructs a new instance of this field from a static slice of `&'static str`.
    ///
    /// `val` must be in sorted order: this constructor cannot check for that due to
    /// limitations in current versions of Rust.
    #[inline]
    pub const fn new_const(val: &'static [Family]) -> Self {
        // TODO: Check that val is sorted.
        Self(Cow::Borrowed(val))
    }

    /// Returns true if this list of families contains a given family.
    #[inline]
    pub fn contains(&self, val: &Family) -> bool {
        self.0.contains(val)
    }
}

impl Deref for Families {
    type Target = [Family];
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl AsRef<[Family]> for Families {
    #[inline]
    fn as_ref(&self) -> &[Family] {
        &self.0
    }
}

impl std::fmt::Display for Families {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{{")?;
        let len = self.0.len();
        for (idx, family) in self.0.iter().enumerate() {
            write!(f, "{family}")?;
            if idx + 1 < len {
                write!(f, ", ")?;
            }
        }
        write!(f, "}}")
    }
}

/// A set of [`HasAtomic`] instances a target.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct HasAtomics(Cow<'static, [HasAtomic]>);

impl HasAtomics {
    /// Constructs a new instance.
    ///
    /// If you have a `&'static [HasAtomic]`, prefer [`Self::new_const`].
    #[inline]
    pub fn new(val: impl IntoIterator<Item = HasAtomic>) -> Self {
        let mut has_atomics: Vec<_> = val.into_iter().collect();
        has_atomics.sort_unstable();
        Self(Cow::Owned(has_atomics))
    }

    /// Constructs a new instance of this struct from a static slice of [`HasAtomic`].
    ///
    /// `val` must be in sorted order: this constructor cannot check for that due to
    /// limitations in current versions of Rust.
    #[inline]
    pub const fn new_const(val: &'static [HasAtomic]) -> Self {
        // TODO: Check that val is sorted.
        Self(Cow::Borrowed(val))
    }

    /// Returns true if this list of families contains a given family.
    #[inline]
    pub fn contains(&self, val: HasAtomic) -> bool {
        self.0.contains(&val)
    }
}

impl Deref for HasAtomics {
    type Target = [HasAtomic];
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl AsRef<[HasAtomic]> for HasAtomics {
    #[inline]
    fn as_ref(&self) -> &[HasAtomic] {
        &self.0
    }
}

impl std::fmt::Display for HasAtomics {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{{")?;
        let len = self.0.len();
        for (idx, has_atomic) in self.0.iter().enumerate() {
            write!(f, "{has_atomic}")?;
            if idx + 1 < len {
                write!(f, ", ")?;
            }
        }
        write!(f, "}}")
    }
}

macro_rules! target_enum {
    (
        $(#[$outer:meta])*
        pub enum $kind:ident {
            $(
                $(#[$inner:ident $($args:tt)*])*
                $name:ident $(= $value:expr)?,
            )+
        }
    ) => {
        $(#[$outer])*
        #[allow(non_camel_case_types)]
        pub enum $kind {
            $(
                $(#[$inner $($args)*])*
                $name $(= $value)?,
            )+
        }

        impl_from_str! {
            $kind {
                $(
                    $(#[$inner $($args)*])*
                    $name $(= $value)?,
                )+
            }
        }
    };
}

macro_rules! impl_from_str {
    (
        $kind:ident {
            $(
                $(#[$attr:ident $($args:tt)*])*
                $name:ident $(= $value:expr)?,
            )+
        }
    ) => {
        impl std::str::FromStr for $kind {
            type Err = Reason;
            fn from_str(s: &str) -> Result<Self, Self::Err> {
                match s {
                    $(stringify!($name) => Ok(Self::$name),)+
                    _ => Err(Reason::Unexpected(&[$(stringify!($name),)+])),
                }
            }
        }
    };
}

target_enum! {
    /// The endian types known to rustc
    #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
    pub enum Endian {
        big,
        little,
    }
}

/// Contains information regarding a particular target known to rustc
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct TargetInfo {
    /// The target's unique identifier
    pub triple: Triple,
    /// The target's operating system, if any. Used by the
    /// [target_os](https://doc.rust-lang.org/reference/conditional-compilation.html#target_os)
    /// predicate.
    pub os: Option<Os>,
    /// The target's ABI, if any. Used by the
    /// [target_abi](https://github.com/rust-lang/rust/issues/80970) predicate.
    pub abi: Option<Abi>,
    /// The target's CPU architecture. Used by the
    /// [target_arch](https://doc.rust-lang.org/reference/conditional-compilation.html#target_arch)
    /// predicate.
    pub arch: Arch,
    /// The target's ABI/libc used, if any. Used by the
    /// [target_env](https://doc.rust-lang.org/reference/conditional-compilation.html#target_env)
    /// predicate.
    pub env: Option<Env>,
    /// The target's vendor, if any. Used by the
    /// [target_vendor](https://doc.rust-lang.org/reference/conditional-compilation.html#target_vendor)
    /// predicate.
    pub vendor: Option<Vendor>,
    /// The target's families, if any. Used by the
    /// [target_family](https://doc.rust-lang.org/reference/conditional-compilation.html#target_family)
    /// predicate.
    pub families: Families,
    /// The size of the target's pointer type. Used by the
    /// [target_pointer_width](https://doc.rust-lang.org/reference/conditional-compilation.html#target_pointer_width)
    /// predicate.
    pub pointer_width: u8,
    /// The target's endianness. Used by the
    /// [target_endian](https://doc.rust-lang.org/reference/conditional-compilation.html#target_endian)
    /// predicate.
    pub endian: Endian,
    /// The target's support for atomics. Used by the has_target_atomics predicate.
    pub has_atomics: HasAtomics,
    /// The panic strategy used on this target by default. Used by the
    /// [panic](https://doc.rust-lang.org/beta/reference/conditional-compilation.html#panic) predicate.
    pub panic: Panic,
}

/// Attempts to find the `TargetInfo` for the specified target triple
///
/// ```
/// assert!(cfg_expr::targets::get_builtin_target_by_triple("x86_64-unknown-linux-musl").is_some());
/// ```
pub fn get_builtin_target_by_triple(triple: &str) -> Option<&'static TargetInfo> {
    ALL_BUILTINS
        .binary_search_by(|ti| ti.triple.as_ref().cmp(triple))
        .map(|i| &ALL_BUILTINS[i])
        .ok()
}

/// Retrieves the version of rustc for which the built-in targets were
/// retrieved from.
///
/// Targets may be added and removed between different rustc versions.
pub fn rustc_version() -> &'static str {
    builtins::RUSTC_VERSION
}

#[cfg(test)]
mod test {
    use crate::targets::get_builtin_target_by_triple;
    use std::collections::{BTreeSet, HashSet};

    // rustc's target-list is currently sorted lexicographically
    // by the target-triple, so ensure that stays the case
    #[test]
    fn targets_are_sorted() {
        for window in super::ALL_BUILTINS.windows(2) {
            assert!(window[0].triple < window[1].triple);
        }
    }

    // Ensure our workaround for https://github.com/rust-lang/rust/issues/36156
    // still functions
    #[test]
    fn has_ios() {
        assert_eq!(
            8,
            super::ALL_BUILTINS
                .iter()
                .filter(|ti| ti.os == Some(super::Os::ios))
                .count()
        );
    }

    // Ensure that TargetInfo can be used as keys for btree and hash-based data structures.
    #[test]
    fn set_map_key() {
        let target_info =
            get_builtin_target_by_triple("x86_64-unknown-linux-gnu").expect("known target");

        let mut btree_set = BTreeSet::new();
        btree_set.insert(target_info);

        let mut hash_set = HashSet::new();
        hash_set.insert(target_info);
    }

    #[test]
    fn family_comp() {
        let a = super::Families::new([super::Family::unix, super::Family::wasm]);
        let b = super::Families::new([super::Family::wasm, super::Family::unix]);

        assert_eq!(a, b);
    }
}