From bce55df488cbcfb619219f6de340a9531c6669de Mon Sep 17 00:00:00 2001 From: ComplexSpaces Date: Thu, 16 Mar 2023 17:36:15 -0500 Subject: [PATCH] Support dynamic feature detection on iOS and derivative platforms --- cpufeatures/Cargo.toml | 2 +- cpufeatures/src/aarch64.rs | 42 +++++++------------------------------- 2 files changed, 8 insertions(+), 36 deletions(-) diff --git a/cpufeatures/Cargo.toml b/cpufeatures/Cargo.toml index d42f9dca..4022d0a1 100644 --- a/cpufeatures/Cargo.toml +++ b/cpufeatures/Cargo.toml @@ -14,7 +14,7 @@ categories = ["no-std"] edition = "2018" readme = "README.md" -[target.aarch64-apple-darwin.dependencies] +[target.'cfg(all(target_arch = "aarch64", target_vendor = "apple"))'.dependencies] libc = "0.2.95" [target.'cfg(all(target_arch = "aarch64", target_os = "linux"))'.dependencies] diff --git a/cpufeatures/src/aarch64.rs b/cpufeatures/src/aarch64.rs index 6efdde1a..2c9a044b 100644 --- a/cpufeatures/src/aarch64.rs +++ b/cpufeatures/src/aarch64.rs @@ -37,8 +37,8 @@ pub fn getauxval_hwcap() -> u64 { unsafe { libc::getauxval(libc::AT_HWCAP) } } -// MacOS runtime detection of target CPU features using `sysctlbyname`. -#[cfg(target_os = "macos")] +// Apple platform's runtime detection of target CPU features using `sysctlbyname`. +#[cfg(target_vendor = "apple")] #[macro_export] #[doc(hidden)] macro_rules! __detect_target_features { @@ -87,7 +87,7 @@ pub mod hwcaps { pub const SHA3: c_ulong = libc::HWCAP_SHA3 | libc::HWCAP_SHA512; } -// macOS `check!` macro. +// Apple OS (macOS, iOS, watchOS, and tvOS) `check!` macro. // // NOTE: several of these instructions (e.g. `aes`, `sha2`) can be assumed to // be present on all Apple ARM64 hardware. @@ -98,7 +98,7 @@ pub mod hwcaps { // // See discussion on this issue for more information: // -#[cfg(target_os = "macos")] +#[cfg(target_vendor = "apple")] #[macro_export] #[doc(hidden)] macro_rules! check { @@ -117,8 +117,8 @@ macro_rules! check { }; } -/// macOS helper function for calling `sysctlbyname`. -#[cfg(target_os = "macos")] +/// Apple helper function for calling `sysctlbyname`. +#[cfg(target_vendor = "apple")] pub unsafe fn sysctlbyname(name: &[u8]) -> bool { assert_eq!( name.last().cloned(), @@ -143,36 +143,8 @@ pub unsafe fn sysctlbyname(name: &[u8]) -> bool { value != 0 } -// iOS `check!` macro. -// -// Unfortunately iOS does not provide access to the `sysctl(3)` API which means -// we can only return static values for CPU features which can be assumed to -// be present on all Apple ARM64 hardware. -// -// See discussion on this issue for more information: -// -#[cfg(target_os = "ios")] -#[macro_export] -#[doc(hidden)] -macro_rules! check { - ("aes") => { - true - }; - ("sha2") => { - true - }; - ("sha3") => { - false - }; -} - // On other targets, runtime CPU feature detection is unavailable -#[cfg(not(any( - target_os = "ios", - target_os = "linux", - target_os = "android", - target_os = "macos" -)))] +#[cfg(not(any(target_vendor = "apple", target_os = "linux", target_os = "android",)))] #[macro_export] #[doc(hidden)] macro_rules! __detect_target_features {