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_select! {
41    target_arch = "aarch64" => {
42        mod aarch64;
43        pub(crate) use self::aarch64::detect_features;
44    }
45    target_arch = "arm" => {
46        mod arm;
47        pub(crate) use self::arm::detect_features;
48    }
49    any(target_arch = "riscv32", target_arch = "riscv64") => {
50        mod riscv;
51        pub(crate) use self::riscv::detect_features;
52    }
53    any(target_arch = "mips", target_arch = "mips64") => {
54        mod mips;
55        pub(crate) use self::mips::detect_features;
56    }
57    any(target_arch = "powerpc", target_arch = "powerpc64") => {
58        mod powerpc;
59        pub(crate) use self::powerpc::detect_features;
60    }
61    any(target_arch = "loongarch32", target_arch = "loongarch64") => {
62        mod loongarch;
63        pub(crate) use self::loongarch::detect_features;
64    }
65    target_arch = "s390x" => {
66        mod s390x;
67        pub(crate) use self::s390x::detect_features;
68    }
69    _ => {
70        use crate::detect::cache;
71        /// Performs run-time feature detection.
72        pub(crate) fn detect_features() -> cache::Initializer {
73            cache::Initializer::default()
74        }
75    }
76}