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
use crate::error::Reason;

mod list;

/// 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.41
pub use list::ALL_TARGETS as ALL;

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! {
    /// All of the operating systems known to rustc
    #[derive(Clone, Copy, PartialEq, Debug)]
    pub enum Os {
        haiku,
        openbsd,
        freebsd,
        redox,
        vxworks,
        uefi,
        emscripten,
        netbsd,
        fuchsia,
        cloudabi,
        wasi,
        solaris,
        cuda,
        dragonfly,
        l4re,
        android,
        macos,
        hermit,
        linux,
        windows,
        unknown,
        ios,
    }
}

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

target_enum! {
    /// All of the target environments known to rustc
    #[derive(Clone, Copy, PartialEq, Debug)]
    pub enum Env {
        uclibc,
        sgx,
        eabihf,
        relibc,
        gnu,
        musl,
        msvc,
        gnueabihf,
    }
}

target_enum! {
    /// All of the target vendors known to rustc
    #[derive(Clone, Copy, PartialEq, Debug)]
    pub enum Vendor {
        pc,
        unknown,
        uwp,
        nvidia,
        sun,
        fortanix,
        wrs,
        rumprun,
        apple,
    }
}

target_enum! {
    /// All of the CPU architectures known to rustc
    #[derive(Clone, Copy, PartialEq, Debug)]
    pub enum Arch {
        x86_64,
        wasm32,
        msp430,
        mips,
        powerpc,
        arm,
        mips64,
        sparc64,
        hexagon,
        riscv64,
        aarch64,
        powerpc64,
        riscv32,
        sparc,
        nvptx64,
        x86,
        s390x,
    }
}

target_enum! {
    /// All of the target families known to rustc
    #[derive(Clone, Copy, PartialEq, Debug)]
    pub enum Family {
        /// Everything that isn't windows, and has a family!
        unix,
        /// The lone wolf of target families.
        windows,
    }
}

/// Contains information regarding a particular target known to rustc
#[derive(Debug)]
pub struct TargetInfo {
    /// The target's unique identifier
    pub triple: &'static str,
    /// 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 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 family, if any. Used by the
    /// [target_family](https://doc.rust-lang.org/reference/conditional-compilation.html#target_family)
    /// predicate.
    pub family: Option<Family>,
    /// 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,
}

/// Attempts to find the `TargetInfo` for the specified target triple
///
/// ```
/// assert!(cfg_expr::targets::get_target_by_triple("x86_64-unknown-linux-musl").is_some());
/// ```
pub fn get_target_by_triple(triple: &str) -> Option<&'static TargetInfo> {
    ALL.binary_search_by(|ti| ti.triple.cmp(triple))
        .map(|i| &ALL[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.
///
/// ```
/// assert_eq!("1.41.0", cfg_expr::targets::rustc_version());
/// ```
pub fn rustc_version() -> &'static str {
    list::RUSTC_VERSION
}

#[cfg(test)]
mod test {
    // 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.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!(
            6,
            super::ALL
                .iter()
                .filter(|ti| ti.os == Some(super::Os::ios))
                .count()
        );
    }
}