build_target/
env.rs

1use crate::utils::{build_env_opt, define_target_enum};
2use std::fmt;
3
4define_target_enum! {
5    // adapted from target/env.rs from platforms crate
6    /// Target enviroment that disambiguates the target platform by ABI / libc.
7    ///
8    /// # Note
9    /// This value is closely related to the fourth element of the platform target triple,
10    /// though it is not identical. For example, embedded ABIs such as `gnueabihf` will simply
11    /// define `target_env` as `"gnu"` (i.e. [`Env::Gnu`])
12    #[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
13    #[non_exhaustive]
14    pub enum Env {
15        /// The GNU C Library (glibc)
16        Gnu => "gnu",
17        /// Microsoft Visual C(++)
18        Msvc => "msvc",
19        /// Clean, efficient, standards-conformant libc implementation.
20        Musl => "musl",
21        /// Newlib environment
22        Newlib => "newlib",
23        /// NTO 7.0 environment
24        Nto70 => "nto70",
25        /// NTO 7.1 environment
26        Nto71 => "nto71",
27        /// NTO 7.1 iOSOCK environment
28        Nto71Iosock => "nto71_iosock",
29        /// NTO 8.0 environment
30        Nto80 => "nto80",
31        /// Open Harmony OS
32        OhOS => "ohos",
33        P1 => "p1",
34        P2 => "p2",
35        /// Relibc environment
36        Relibc => "relibc",
37        /// Intel Software Guard Extensions (SGX) Enclave
38        Sgx => "sgx",
39        /// C library for developing embedded Linux systems
40        UClibc => "uclibc"
41    }
42
43    as_str_doc = "String representing this environment which matches `#[cfg(target_env)]`.",
44    from_str_doc = "Tries to parse the given string as an [`Env`] falling back to [`Env::Other`] for unknown values.",
45}
46
47impl Env {
48    /// Gets the current target [`Env`].
49    #[must_use]
50    pub fn target() -> Option<Self> {
51        build_env_opt("CARGO_CFG_TARGET_ENV").map(Self::from_str)
52    }
53}
54
55impl fmt::Display for Env {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        f.write_str(self.as_str())
58    }
59}