about summary refs log tree commit diff
path: root/library/std_detect/src/detect/os/linux/mod.rs
diff options
context:
space:
mode:
authorJakub Beránek <berykubik@gmail.com>2025-07-04 09:26:12 +0200
committerJakub Beránek <berykubik@gmail.com>2025-07-22 20:17:06 +0200
commit5b2de8ab27e4a319e23817b61830a5cc6fd1745e (patch)
tree94d6ce5680690c678e56d907ec27145fb464eeed /library/std_detect/src/detect/os/linux/mod.rs
parent2e5367566819ca7878baa9600ae7a93eb0e37bbf (diff)
downloadrust-5b2de8ab27e4a319e23817b61830a5cc6fd1745e.tar.gz
rust-5b2de8ab27e4a319e23817b61830a5cc6fd1745e.zip
Move `std_detect` from `library/stdarch` to `library`
Diffstat (limited to 'library/std_detect/src/detect/os/linux/mod.rs')
-rw-r--r--library/std_detect/src/detect/os/linux/mod.rs67
1 files changed, 67 insertions, 0 deletions
diff --git a/library/std_detect/src/detect/os/linux/mod.rs b/library/std_detect/src/detect/os/linux/mod.rs
new file mode 100644
index 00000000000..9accd41717b
--- /dev/null
+++ b/library/std_detect/src/detect/os/linux/mod.rs
@@ -0,0 +1,67 @@
+//! Run-time feature detection on Linux
+//!
+#[cfg(feature = "std_detect_file_io")]
+use alloc::vec::Vec;
+
+mod auxvec;
+
+#[cfg(feature = "std_detect_file_io")]
+fn read_file(path: &str) -> Result<Vec<u8>, ()> {
+    let mut path = Vec::from(path.as_bytes());
+    path.push(0);
+
+    unsafe {
+        let file = libc::open(path.as_ptr() as *const libc::c_char, libc::O_RDONLY);
+        if file == -1 {
+            return Err(());
+        }
+
+        let mut data = Vec::new();
+        loop {
+            data.reserve(4096);
+            let spare = data.spare_capacity_mut();
+            match libc::read(file, spare.as_mut_ptr() as *mut _, spare.len()) {
+                -1 => {
+                    libc::close(file);
+                    return Err(());
+                }
+                0 => break,
+                n => data.set_len(data.len() + n as usize),
+            }
+        }
+
+        libc::close(file);
+        Ok(data)
+    }
+}
+
+cfg_if::cfg_if! {
+    if #[cfg(target_arch = "aarch64")] {
+        mod aarch64;
+        pub(crate) use self::aarch64::detect_features;
+    } else if #[cfg(target_arch = "arm")] {
+        mod arm;
+        pub(crate) use self::arm::detect_features;
+    } else if #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] {
+        mod riscv;
+        pub(crate) use self::riscv::detect_features;
+    } else if #[cfg(any(target_arch = "mips", target_arch = "mips64"))] {
+        mod mips;
+        pub(crate) use self::mips::detect_features;
+    } else if #[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] {
+        mod powerpc;
+        pub(crate) use self::powerpc::detect_features;
+    } else if #[cfg(any(target_arch = "loongarch32", target_arch = "loongarch64"))] {
+        mod loongarch;
+        pub(crate) use self::loongarch::detect_features;
+    } else if #[cfg(target_arch = "s390x")] {
+        mod s390x;
+        pub(crate) use self::s390x::detect_features;
+    } else {
+        use crate::detect::cache;
+        /// Performs run-time feature detection.
+        pub(crate) fn detect_features() -> cache::Initializer {
+            cache::Initializer::default()
+        }
+    }
+}