std_detect/detect/os/linux/
mod.rs

1//! Run-time feature detection on Linux
2//!
3#[cfg(feature = "std_detect_file_io")]
4use alloc::vec::Vec;
5
6mod auxvec;
7
8#[cfg(feature = "std_detect_file_io")]
9fn read_file(orig_path: &str) -> Result<Vec<u8>, alloc::string::String> {
10    use alloc::format;
11
12    let mut path = Vec::from(orig_path.as_bytes());
13    path.push(0);
14
15    unsafe {
16        let file = libc::open(path.as_ptr() as *const libc::c_char, libc::O_RDONLY);
17        if file == -1 {
18            return Err(format!("Cannot open file at {orig_path}"));
19        }
20
21        let mut data = Vec::new();
22        loop {
23            data.reserve(4096);
24            let spare = data.spare_capacity_mut();
25            match libc::read(file, spare.as_mut_ptr() as *mut _, spare.len()) {
26                -1 => {
27                    libc::close(file);
28                    return Err(format!("Error while reading from file at {orig_path}"));
29                }
30                0 => break,
31                n => data.set_len(data.len() + n as usize),
32            }
33        }
34
35        libc::close(file);
36        Ok(data)
37    }
38}
39
40cfg_if::cfg_if! {
41    if #[cfg(target_arch = "aarch64")] {
42        mod aarch64;
43        pub(crate) use self::aarch64::detect_features;
44    } else if #[cfg(target_arch = "arm")] {
45        mod arm;
46        pub(crate) use self::arm::detect_features;
47    } else if #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] {
48        mod riscv;
49        pub(crate) use self::riscv::detect_features;
50    } else if #[cfg(any(target_arch = "mips", target_arch = "mips64"))] {
51        mod mips;
52        pub(crate) use self::mips::detect_features;
53    } else if #[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] {
54        mod powerpc;
55        pub(crate) use self::powerpc::detect_features;
56    } else if #[cfg(any(target_arch = "loongarch32", target_arch = "loongarch64"))] {
57        mod loongarch;
58        pub(crate) use self::loongarch::detect_features;
59    } else if #[cfg(target_arch = "s390x")] {
60        mod s390x;
61        pub(crate) use self::s390x::detect_features;
62    } else {
63        use crate::detect::cache;
64        /// Performs run-time feature detection.
65        pub(crate) fn detect_features() -> cache::Initializer {
66            cache::Initializer::default()
67        }
68    }
69}