std_detect/detect/mod.rs
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#[macro_use]
21mod macros;
22
23mod arch;
24
25// This module needs to be public because the `is_{arch}_feature_detected!`
26// macros expand calls to items within it in user crates.
27#[unstable(feature = "stdarch_internal", issue = "none")]
28pub use self::arch::__is_feature_detected;
29pub(crate) use self::arch::Feature;
30
31mod bit;
32mod cache;
33
34cfg_select! {
35 miri => {
36 // When running under miri all target-features that are not enabled at
37 // compile-time are reported as disabled at run-time.
38 //
39 // For features for which `cfg(target_feature)` returns true,
40 // this run-time detection logic is never called.
41 #[path = "os/other.rs"]
42 mod os;
43 }
44 any(target_arch = "x86", target_arch = "x86_64") => {
45 // On x86/x86_64 no OS specific functionality is required.
46 #[path = "os/x86.rs"]
47 mod os;
48 }
49 any(target_os = "linux", target_os = "android") => {
50 #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
51 #[path = "os/riscv.rs"]
52 mod riscv;
53 #[path = "os/linux/mod.rs"]
54 mod os;
55 }
56 target_os = "freebsd" => {
57 #[cfg(target_arch = "aarch64")]
58 #[path = "os/aarch64.rs"]
59 mod aarch64;
60 #[path = "os/freebsd/mod.rs"]
61 mod os;
62 }
63 target_os = "openbsd" => {
64 #[allow(dead_code)] // we don't use code that calls the mrs instruction.
65 #[cfg(target_arch = "aarch64")]
66 #[path = "os/aarch64.rs"]
67 mod aarch64;
68 #[path = "os/openbsd/mod.rs"]
69 mod os;
70 }
71 all(target_os = "windows", any(target_arch = "aarch64", target_arch = "arm64ec")) => {
72 #[path = "os/windows/aarch64.rs"]
73 mod os;
74 }
75 all(target_vendor = "apple", target_arch = "aarch64") => {
76 #[path = "os/darwin/aarch64.rs"]
77 mod os;
78 }
79 _ => {
80 #[path = "os/other.rs"]
81 mod os;
82 }
83}
84
85/// Performs run-time feature detection.
86#[inline]
87#[allow(dead_code)]
88fn check_for(x: Feature) -> bool {
89 cache::test(x as u32)
90}
91
92/// Returns an `Iterator<Item=(&'static str, bool)>` where
93/// `Item.0` is the feature name, and `Item.1` is a `bool` which
94/// is `true` if the feature is supported by the host and `false` otherwise.
95#[unstable(feature = "stdarch_internal", issue = "none")]
96pub fn features() -> impl Iterator<Item = (&'static str, bool)> {
97 cfg_select! {
98 any(
99 target_arch = "x86",
100 target_arch = "x86_64",
101 target_arch = "arm",
102 target_arch = "aarch64",
103 target_arch = "arm64ec",
104 target_arch = "riscv32",
105 target_arch = "riscv64",
106 target_arch = "powerpc",
107 target_arch = "powerpc64",
108 target_arch = "mips",
109 target_arch = "mips64",
110 target_arch = "loongarch32",
111 target_arch = "loongarch64",
112 target_arch = "s390x",
113 ) => Feature::ALL.iter().copied().map(|feat| {
114 let name: &'static str = feat.to_str();
115 let enabled: bool = check_for(feat);
116 (name, enabled)
117 }),
118 _ => None.into_iter(),
119 }
120}