1 | //! This module implements run-time feature detection. |
2 | //! |
3 | //! The `is_{arch}_feature_detected!("feature-name")` macros take the name of a |
4 | //! feature as a string-literal, and return a boolean indicating whether the |
5 | //! feature is enabled at run-time or not. |
6 | //! |
7 | //! These macros do two things: |
8 | //! * map the string-literal into an integer stored as a `Feature` enum, |
9 | //! * call a `os::check_for(x: Feature)` function that returns `true` if the |
10 | //! feature is enabled. |
11 | //! |
12 | //! The `Feature` enums are also implemented in the `arch/{target_arch}.rs` |
13 | //! modules. |
14 | //! |
15 | //! The `check_for` functions are, in general, Operating System dependent. Most |
16 | //! architectures do not allow user-space programs to query the feature bits |
17 | //! due to security concerns (x86 is the big exception). These functions are |
18 | //! implemented in the `os/{target_os}.rs` modules. |
19 | |
20 | use cfg_if::cfg_if; |
21 | |
22 | #[macro_use ] |
23 | mod macros; |
24 | |
25 | mod arch; |
26 | |
27 | // This module needs to be public because the `is_{arch}_feature_detected!` |
28 | // macros expand calls to items within it in user crates. |
29 | #[doc (hidden)] |
30 | #[unstable (feature = "stdarch_internal" , issue = "none" )] |
31 | pub use self::arch::__is_feature_detected; |
32 | |
33 | pub(crate) use self::arch::Feature; |
34 | |
35 | mod bit; |
36 | mod cache; |
37 | |
38 | cfg_if! { |
39 | if #[cfg(miri)] { |
40 | // When running under miri all target-features that are not enabled at |
41 | // compile-time are reported as disabled at run-time. |
42 | // |
43 | // For features for which `cfg(target_feature)` returns true, |
44 | // this run-time detection logic is never called. |
45 | #[path = "os/other.rs" ] |
46 | mod os; |
47 | } else if #[cfg(any(target_arch = "x86" , target_arch = "x86_64" ))] { |
48 | // On x86/x86_64 no OS specific functionality is required. |
49 | #[path = "os/x86.rs" ] |
50 | mod os; |
51 | } else if #[cfg(all(any(target_os = "linux" , target_os = "android" ), feature = "libc" ))] { |
52 | #[path = "os/linux/mod.rs" ] |
53 | mod os; |
54 | } else if #[cfg(all(target_os = "freebsd" , feature = "libc" ))] { |
55 | #[cfg(target_arch = "aarch64" )] |
56 | #[path = "os/aarch64.rs" ] |
57 | mod aarch64; |
58 | #[path = "os/freebsd/mod.rs" ] |
59 | mod os; |
60 | } else if #[cfg(all(target_os = "openbsd" , target_arch = "aarch64" , feature = "libc" ))] { |
61 | #[allow(dead_code)] // we don't use code that calls the mrs instruction. |
62 | #[path = "os/aarch64.rs" ] |
63 | mod aarch64; |
64 | #[path = "os/openbsd/aarch64.rs" ] |
65 | mod os; |
66 | } else if #[cfg(all(target_os = "windows" , any(target_arch = "aarch64" , target_arch = "arm64ec" )))] { |
67 | #[path = "os/windows/aarch64.rs" ] |
68 | mod os; |
69 | } else if #[cfg(all(target_os = "macos" , target_arch = "aarch64" , feature = "libc" ))] { |
70 | #[path = "os/macos/aarch64.rs" ] |
71 | mod os; |
72 | } else { |
73 | #[path = "os/other.rs" ] |
74 | mod os; |
75 | } |
76 | } |
77 | |
78 | /// Performs run-time feature detection. |
79 | #[inline ] |
80 | #[allow (dead_code)] |
81 | fn check_for(x: Feature) -> bool { |
82 | cache::test(bit:x as u32) |
83 | } |
84 | |
85 | /// Returns an `Iterator<Item=(&'static str, bool)>` where |
86 | /// `Item.0` is the feature name, and `Item.1` is a `bool` which |
87 | /// is `true` if the feature is supported by the host and `false` otherwise. |
88 | #[unstable (feature = "stdarch_internal" , issue = "none" )] |
89 | pub fn features() -> impl Iterator<Item = (&'static str, bool)> { |
90 | cfg_if! { |
91 | if #[cfg(any( |
92 | target_arch = "x86" , |
93 | target_arch = "x86_64" , |
94 | target_arch = "arm" , |
95 | target_arch = "aarch64" , |
96 | target_arch = "arm64ec" , |
97 | target_arch = "riscv32" , |
98 | target_arch = "riscv64" , |
99 | target_arch = "powerpc" , |
100 | target_arch = "powerpc64" , |
101 | target_arch = "mips" , |
102 | target_arch = "mips64" , |
103 | target_arch = "loongarch64" , |
104 | ))] { |
105 | (0_u8..Feature::_last as u8).map(|discriminant: u8| { |
106 | #[allow(bindings_with_variant_name)] // RISC-V has Feature::f |
107 | let f: Feature = unsafe { core::mem::transmute(discriminant) }; |
108 | let name: &'static str = f.to_str(); |
109 | let enabled: bool = check_for(f); |
110 | (name, enabled) |
111 | }) |
112 | } else { |
113 | None.into_iter() |
114 | } |
115 | } |
116 | } |
117 | |