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
20use cfg_if::cfg_if;
21
22#[macro_use]
23mod macros;
24
25mod 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)]
30pub use self::arch::__is_feature_detected;
31
32pub(crate) use self::arch::Feature;
33
34mod bit;
35mod cache;
36
37cfg_if! {
38 if #[cfg(miri)] {
39 // When running under miri all target-features that are not enabled at
40 // compile-time are reported as disabled at run-time.
41 //
42 // For features for which `cfg(target_feature)` returns true,
43 // this run-time detection logic is never called.
44 #[path = "os/other.rs"]
45 mod os;
46 } else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
47 // On x86/x86_64 no OS specific functionality is required.
48 #[path = "os/x86.rs"]
49 mod os;
50 } else if #[cfg(all(any(target_os = "linux", target_os = "android"), feature = "libc"))] {
51 #[path = "os/linux/mod.rs"]
52 mod os;
53 } else if #[cfg(all(target_os = "freebsd", feature = "libc"))] {
54 #[cfg(target_arch = "aarch64")]
55 #[path = "os/aarch64.rs"]
56 mod aarch64;
57 #[path = "os/freebsd/mod.rs"]
58 mod os;
59 } else if #[cfg(all(target_os = "openbsd", target_arch = "aarch64", feature = "libc"))] {
60 #[allow(dead_code)] // we don't use code that calls the mrs instruction.
61 #[path = "os/aarch64.rs"]
62 mod aarch64;
63 #[path = "os/openbsd/aarch64.rs"]
64 mod os;
65 } else if #[cfg(all(target_os = "windows", target_arch = "aarch64"))] {
66 #[path = "os/windows/aarch64.rs"]
67 mod os;
68 } else {
69 #[path = "os/other.rs"]
70 mod os;
71 }
72}
73
74/// Performs run-time feature detection.
75#[inline]
76#[allow(dead_code)]
77fn check_for(x: Feature) -> bool {
78 cache::test(bit:x as u32)
79}
80
81/// Returns an `Iterator<Item=(&'static str, bool)>` where
82/// `Item.0` is the feature name, and `Item.1` is a `bool` which
83/// is `true` if the feature is supported by the host and `false` otherwise.
84#[unstable(feature = "stdsimd", issue = "27731")]
85pub fn features() -> impl Iterator<Item = (&'static str, bool)> {
86 cfg_if! {
87 if #[cfg(any(
88 target_arch = "x86",
89 target_arch = "x86_64",
90 target_arch = "arm",
91 target_arch = "aarch64",
92 target_arch = "riscv32",
93 target_arch = "riscv64",
94 target_arch = "powerpc",
95 target_arch = "powerpc64",
96 target_arch = "mips",
97 target_arch = "mips64",
98 ))] {
99 (0_u8..Feature::_last as u8).map(|discriminant: u8| {
100 #[allow(bindings_with_variant_name)] // RISC-V has Feature::f
101 let f: Feature = unsafe { core::mem::transmute(discriminant) };
102 let name: &'static str = f.to_str();
103 let enabled: bool = check_for(f);
104 (name, enabled)
105 })
106 } else {
107 None.into_iter()
108 }
109 }
110}
111