diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:41:41 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:41:41 +0000 |
commit | 10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87 (patch) | |
tree | bdffd5d80c26cf4a7a518281a204be1ace85b4c1 /vendor/os_info/src/bitness.rs | |
parent | Releasing progress-linux version 1.70.0+dfsg1-9~progress7.99u1. (diff) | |
download | rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.tar.xz rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.zip |
Merging upstream version 1.70.0+dfsg2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/os_info/src/bitness.rs')
-rw-r--r-- | vendor/os_info/src/bitness.rs | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/vendor/os_info/src/bitness.rs b/vendor/os_info/src/bitness.rs new file mode 100644 index 000000000..dba7e1c83 --- /dev/null +++ b/vendor/os_info/src/bitness.rs @@ -0,0 +1,124 @@ +// spell-checker:ignore getconf + +use std::fmt::{self, Display, Formatter}; +#[cfg(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "illumos", + target_os = "linux", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd" +))] +use std::process::{Command, Output}; + +/// Operating system architecture in terms of how many bits compose the basic values it can deal with. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[non_exhaustive] +pub enum Bitness { + /// Unknown bitness (unable to determine). + Unknown, + /// 32-bit. + X32, + /// 64-bit. + X64, +} + +impl Display for Bitness { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + match *self { + Bitness::Unknown => write!(f, "unknown bitness"), + Bitness::X32 => write!(f, "32-bit"), + Bitness::X64 => write!(f, "64-bit"), + } + } +} + +#[cfg(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "linux", + target_os = "macos", +))] +pub fn get() -> Bitness { + match &Command::new("getconf").arg("LONG_BIT").output() { + Ok(Output { stdout, .. }) if stdout == b"32\n" => Bitness::X32, + Ok(Output { stdout, .. }) if stdout == b"64\n" => Bitness::X64, + _ => Bitness::Unknown, + } +} + +#[cfg(target_os = "netbsd")] +pub fn get() -> Bitness { + match &Command::new("sysctl") + .arg("-n") + .arg("hw.machine_arch") + .output() + { + Ok(Output { stdout, .. }) if stdout == b"amd64\n" => Bitness::X64, + Ok(Output { stdout, .. }) if stdout == b"x86_64\n" => Bitness::X64, + Ok(Output { stdout, .. }) if stdout == b"i386\n" => Bitness::X32, + Ok(Output { stdout, .. }) if stdout == b"aarch64\n" => Bitness::X64, + Ok(Output { stdout, .. }) if stdout == b"earmv7hf\n" => Bitness::X32, + Ok(Output { stdout, .. }) if stdout == b"sparc64\n" => Bitness::X64, + _ => Bitness::Unknown, + } +} + +#[cfg(target_os = "openbsd")] +pub fn get() -> Bitness { + match &Command::new("sysctl").arg("-n").arg("hw.machine").output() { + Ok(Output { stdout, .. }) if stdout == b"amd64\n" => Bitness::X64, + Ok(Output { stdout, .. }) if stdout == b"x86_64\n" => Bitness::X64, + Ok(Output { stdout, .. }) if stdout == b"i386\n" => Bitness::X32, + Ok(Output { stdout, .. }) if stdout == b"aarch64\n" => Bitness::X64, + Ok(Output { stdout, .. }) if stdout == b"earmv7hf\n" => Bitness::X32, + Ok(Output { stdout, .. }) if stdout == b"sparc64\n" => Bitness::X64, + _ => Bitness::Unknown, + } +} + +#[cfg(target_os = "illumos")] +pub fn get() -> Bitness { + match &Command::new("isainfo").arg("-b").output() { + Ok(Output { stdout, .. }) if stdout == b"64\n" => Bitness::X64, + Ok(Output { stdout, .. }) if stdout == b"32\n" => Bitness::X32, + _ => Bitness::Unknown, + } +} + +#[cfg(all( + test, + any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "linux", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd" + ) +))] +mod tests { + use super::*; + use pretty_assertions::assert_ne; + + #[test] + fn get_bitness() { + let b = get(); + assert_ne!(b, Bitness::Unknown); + } + + #[test] + fn display() { + let data = [ + (Bitness::Unknown, "unknown bitness"), + (Bitness::X32, "32-bit"), + (Bitness::X64, "64-bit"), + ]; + + for (bitness, expected) in &data { + assert_eq!(&bitness.to_string(), expected); + } + } +} |