| 1 |
// Copyright (c) 2017 The Rust Project Developers |
| 2 |
// Copyright (c) 2018-2020 The Rust Secure Code Working Group |
| 3 |
// Licensed under the MIT License. |
| 4 |
// Original license: |
| 5 |
// https://github.com/rustsec/rustsec/blob/2a080f173ad9d8ac7fa260f0a3a6aebf0000de06/platforms/LICENSE-MIT |
| 6 |
// |
| 7 |
// Permission is hereby granted, free of charge, to any |
| 8 |
// person obtaining a copy of this software and associated |
| 9 |
// documentation files (the "Software"), to deal in the |
| 10 |
// Software without restriction, including without |
| 11 |
// limitation the rights to use, copy, modify, merge, |
| 12 |
// publish, distribute, sublicense, and/or sell copies of |
| 13 |
// the Software, and to permit persons to whom the Software |
| 14 |
// is furnished to do so, subject to the following |
| 15 |
// conditions: |
| 16 |
// |
| 17 |
// The above copyright notice and this permission notice |
| 18 |
// shall be included in all copies or substantial portions |
| 19 |
// of the Software. |
| 20 |
// |
| 21 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF |
| 22 |
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 23 |
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 24 |
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 25 |
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 26 |
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 27 |
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR |
| 28 |
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 29 |
// DEALINGS IN THE SOFTWARE. |
| 30 |
|
| 31 |
//! Detect and expose `target_arch` as a constant |
| 32 |
|
| 33 |
#[cfg(target_arch = "aarch64")] |
| 34 |
/// `target_arch` when building this crate: `aarch64` |
| 35 |
pub const ARCH: &str = "aarch64"; |
| 36 |
|
| 37 |
#[cfg(target_arch = "arm")] |
| 38 |
/// `target_arch` when building this crate: `arm` |
| 39 |
pub const ARCH: &str = "arm"; |
| 40 |
|
| 41 |
#[cfg(target_arch = "x86")] |
| 42 |
/// `target_arch` when building this crate: `x86` |
| 43 |
pub const ARCH: &str = "x86"; |
| 44 |
|
| 45 |
#[cfg(target_arch = "x86_64")] |
| 46 |
/// `target_arch` when building this crate: `x86_64` |
| 47 |
pub const ARCH: &str = "x86_64"; |
| 48 |
|
| 49 |
#[cfg(target_arch = "powerpc64")] |
| 50 |
/// `target_arch` when building this crate: `powerpc64` |
| 51 |
pub const ARCH: &str = "powerpc64"; |
| 52 |
|
| 53 |
#[cfg(target_arch = "riscv64")] |
| 54 |
/// `target_arch` when building this crate: `riscv64` |
| 55 |
pub const ARCH: &str = "riscv64"; |
| 56 |
|
| 57 |
#[cfg(target_arch = "mips")] |
| 58 |
/// `target_arch` when building this crate: `mips` |
| 59 |
pub const ARCH: &str = "mips"; |
| 60 |
|
| 61 |
#[cfg(target_arch = "loongarch64")] |
| 62 |
/// `target_arch` when building this crate: `loongarch64` |
| 63 |
pub const ARCH: &str = "loongarch64"; |
| 64 |
|
| 65 |
#[cfg(target_arch = "s390x")] |
| 66 |
/// `target_arch` when building this crate: `s390x` |
| 67 |
pub const ARCH: &str = "s390x"; |
| 68 |
|
| 69 |
#[cfg(not(any( |
| 70 |
target_arch = "aarch64", |
| 71 |
target_arch = "arm", |
| 72 |
target_arch = "x86", |
| 73 |
target_arch = "x86_64", |
| 74 |
target_arch = "powerpc64", |
| 75 |
target_arch = "riscv64", |
| 76 |
target_arch = "mips", |
| 77 |
target_arch = "loongarch64", |
| 78 |
target_arch = "s390x", |
| 79 |
)))] |
| 80 |
/// `target_arch` when building this crate: unknown! |
| 81 |
pub const ARCH: &str = "Unknown"; |
| 82 |
|
| 83 |
#[cfg(any(target_os = "macos", target_os = "windows"))] |
| 84 |
/// Returns Darwin kernel version for MacOS, or NT Kernel version for Windows |
| 85 |
pub fn get_os_version() -> String { |
| 86 |
whatsys::kernel_version().unwrap_or_else(|| "Unknown".to_owned()) |
| 87 |
} |
| 88 |
|
| 89 |
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))] |
| 90 |
/// Returns "Unknown" for platforms other than Linux, MacOS or Windows |
| 91 |
pub fn get_os_version() -> String { |
| 92 |
"Unknown".to_owned() |
| 93 |
} |
| 94 |
|
| 95 |
#[cfg(target_os = "linux")] |
| 96 |
/// Returns Linux kernel version, in the format of <Major>.<Minor> e.g. 5.8 |
| 97 |
pub fn get_os_version() -> String { |
| 98 |
parse_linux_os_string(whatsys::kernel_version().unwrap_or_else(|| "Unknown".to_owned())) |
| 99 |
} |
| 100 |
|
| 101 |
#[cfg(target_os = "windows")] |
| 102 |
/// Returns the Windows build number, e.g. 22000 |
| 103 |
pub fn get_windows_build_number() -> Option<i64> { |
| 104 |
match whatsys::windows_build_number() { |
| 105 |
// Cast to i64 to work with QuantityMetric type |
| 106 |
Some(i) => Some(i as i64), |
| 107 |
_ => None, |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
#[cfg(not(target_os = "windows"))] |
| 112 |
/// Returns None, for non-Windows operating systems |
| 113 |
pub fn get_windows_build_number() -> Option<i64> { |
| 114 |
None |
| 115 |
} |
| 116 |
|
| 117 |
#[cfg(target_os = "linux")] |
| 118 |
fn parse_linux_os_string(os_str: String) -> String { |
| 119 |
os_str.split('.').take(2).collect::<Vec<&str>>().join(".") |
| 120 |
} |
| 121 |
|
| 122 |
#[test] |
| 123 |
#[cfg(target_os = "linux")] |
| 124 |
fn parse_fixed_linux_os_string() { |
| 125 |
let alpine_os_string = "4.12.0-rc6-g48ec1f0-dirty".to_owned(); |
| 126 |
assert_eq!(parse_linux_os_string(alpine_os_string), "4.12"); |
| 127 |
let centos_os_string = "3.10.0-514.16.1.el7.x86_64".to_owned(); |
| 128 |
assert_eq!(parse_linux_os_string(centos_os_string), "3.10"); |
| 129 |
let ubuntu_os_string = "5.8.0-44-generic".to_owned(); |
| 130 |
assert_eq!(parse_linux_os_string(ubuntu_os_string), "5.8"); |
| 131 |
} |