Debian Codegraph @

debian/rust-vendor/platform-info/src/lib_impl.rs

raw
1 // "plumbing" setup and connections for `lib.rs`
2
3 // spell-checker:ignore (jargon) armv
4
5 #![warn(unused_results)] // enable warnings for unused results
6
7 #[cfg(target_os = "windows")]
8 use std::path::Path;
9 #[cfg(target_os = "windows")]
10 use std::path::PathBuf;
11
12 //=== types
13
14 /// Standard thread-safe error type
15 pub type ThreadSafeStdError = dyn std::error::Error + Send + Sync;
16 /// Standard thread-safe error type (boxed to allow translation for any `std::error::Error` type)
17 pub type BoxedThreadSafeStdError = Box<ThreadSafeStdError>;
18
19 /// A slice of a path string
20 /// (akin to [`str`]; aka/equivalent to [`Path`]).
21 #[cfg(target_os = "windows")]
22 type PathStr = Path;
23 /// An owned, mutable path string
24 /// (akin to [`String`]; aka/equivalent to [`PathBuf`]).
25 #[cfg(target_os = "windows")]
26 type PathString = PathBuf;
27
28 //=== platform-specific functions
29
30 // map_processor
31 /// *Returns* processor type mapped from machine architecture string.
32 ///
33 /// Provides GNU coreutils-compatible processor type mappings from machine architecture strings.
34 /// This function normalizes architecture names to match the output of GNU coreutils' `uname -p`.
35 ///
36 /// # Difference from `machine()`
37 ///
38 /// While [`UNameAPI::machine()`] returns the raw architecture string from the OS (e.g., "arm64" on macOS),
39 /// `processor()` provides a normalized, cross-platform compatible representation:
40 ///
41 /// | Platform | `machine()` | `processor()` | Reason |
42 /// |----------|-----------|-------------|--------|
43 /// | macOS ARM | "arm64" | "arm" | GNU coreutils compatibility |
44 /// | Linux ARM64 | "aarch64" | "aarch64" | Preserve Linux convention |
45 /// | Windows x86 | "i386"-"i686" | "i686" | Normalize to common name |
46 ///
47 /// # Architecture Mappings
48 ///
49 /// * macOS uses "arm64" for ARM-based Macs → maps to "arm"
50 /// * Linux uses "aarch64" for ARM64 → passes through as "aarch64"
51 /// * Various i386/i486/i586/i686 variants → normalized to "i686"
52 /// * ARMv6/v7/v8 variants → mapped to "arm"
53 /// * Unknown architectures pass through unchanged (better than returning "unknown")
54 ///
55 /// ref: <https://github.com/uutils/coreutils/issues/8659>
56 pub(crate) fn map_processor(machine: &str) -> String {
57 match machine {
58 "aarch64" => "aarch64".to_string(),
59 "x86_64" | "amd64" => "x86_64".to_string(),
60 "i386" | "i486" | "i586" | "i686" => "i686".to_string(),
61 "arm64" | "armv6l" | "armv7l" | "armv8l" => "arm".to_string(),
62 _ => machine.to_string(),
63 }
64 }
65
66 //=== platform-specific const
67
68 // HOST_OS_NAME * ref: [`uname` info](https://en.wikipedia.org/wiki/Uname)
69 const HOST_OS_NAME: &str = if cfg!(all(
70 target_os = "linux",
71 any(target_env = "gnu", target_env = "")
72 )) {
73 "GNU/Linux"
74 } else if cfg!(all(
75 target_os = "linux",
76 not(any(target_env = "gnu", target_env = ""))
77 )) {
78 "Linux"
79 } else if cfg!(target_os = "android") {
80 "Android"
81 } else if cfg!(target_os = "windows") {
82 "MS/Windows" // prior art == `busybox`
83 } else if cfg!(target_os = "freebsd") {
84 "FreeBSD"
85 } else if cfg!(target_os = "netbsd") {
86 "NetBSD"
87 } else if cfg!(target_os = "openbsd") {
88 "OpenBSD"
89 } else if cfg!(target_vendor = "apple") {
90 "Darwin"
91 } else if cfg!(target_os = "fuchsia") {
92 "Fuchsia"
93 } else if cfg!(target_os = "redox") {
94 "Redox"
95 } else if cfg!(target_os = "illumos") {
96 "illumos"
97 } else if cfg!(target_os = "solaris") {
98 "Solaris"
99 } else if cfg!(target_os = "haiku") {
100 "Haiku"
101 } else if cfg!(target_os = "dragonfly") {
102 "DragonFly"
103 } else if cfg!(target_os = "aix") {
104 "AIX"
105 } else if cfg!(target_os = "cygwin") {
106 "Cygwin"
107 } else if cfg!(target_os = "wasi") {
108 "WASI"
109 } else if cfg!(target_os = "hurd") {
110 "GNU"
111 } else {
112 "unknown"
113 };
114
115 //=== platform-specific module code
116
117 #[cfg(all(unix, not(target_os = "wasi")))]
118 #[path = "platform/unix.rs"]
119 mod target;
120 #[cfg(windows)]
121 #[path = "platform/windows.rs"]
122 mod target;
123 #[cfg(target_os = "wasi")]
124 #[path = "platform/wasi.rs"]
125 mod target;
126 #[cfg(not(any(unix, windows, target_os = "wasi")))]
127 #[path = "platform/unknown.rs"]
128 mod target;
129
130 pub use target::*;
131
132 //=== Tests
133
134 #[test]
135 fn test_map_processor_mappings() {
136 // ARM variants
137 assert_eq!(map_processor("arm64"), "arm");
138 assert_eq!(map_processor("aarch64"), "aarch64");
139 assert_eq!(map_processor("armv6l"), "arm");
140 assert_eq!(map_processor("armv7l"), "arm");
141 assert_eq!(map_processor("armv8l"), "arm");
142
143 // x86 variants
144 assert_eq!(map_processor("x86_64"), "x86_64");
145 assert_eq!(map_processor("amd64"), "x86_64");
146 assert_eq!(map_processor("i386"), "i686");
147 assert_eq!(map_processor("i486"), "i686");
148 assert_eq!(map_processor("i586"), "i686");
149 assert_eq!(map_processor("i686"), "i686");
150
151 // Unknown/passthrough architectures
152 assert_eq!(map_processor("riscv32"), "riscv32");
153 assert_eq!(map_processor("riscv64"), "riscv64");
154 assert_eq!(map_processor("powerpc"), "powerpc");
155 assert_eq!(map_processor("powerpc64"), "powerpc64");
156 assert_eq!(map_processor("powerpc64le"), "powerpc64le");
157 assert_eq!(map_processor("s390x"), "s390x");
158 assert_eq!(map_processor("sparc64"), "sparc64");
159 assert_eq!(map_processor("loongarch64"), "loongarch64");
160 assert_eq!(map_processor("mips64"), "mips64");
161 assert_eq!(map_processor("unknown"), "unknown");
162 }