Skip to content

Commit bce55df

Browse files
committed
Support dynamic feature detection on iOS and derivative platforms
1 parent bb022c8 commit bce55df

File tree

2 files changed

+8
-36
lines changed

2 files changed

+8
-36
lines changed

Diff for: cpufeatures/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ categories = ["no-std"]
1414
edition = "2018"
1515
readme = "README.md"
1616

17-
[target.aarch64-apple-darwin.dependencies]
17+
[target.'cfg(all(target_arch = "aarch64", target_vendor = "apple"))'.dependencies]
1818
libc = "0.2.95"
1919

2020
[target.'cfg(all(target_arch = "aarch64", target_os = "linux"))'.dependencies]

Diff for: cpufeatures/src/aarch64.rs

+7-35
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ pub fn getauxval_hwcap() -> u64 {
3737
unsafe { libc::getauxval(libc::AT_HWCAP) }
3838
}
3939

40-
// MacOS runtime detection of target CPU features using `sysctlbyname`.
41-
#[cfg(target_os = "macos")]
40+
// Apple platform's runtime detection of target CPU features using `sysctlbyname`.
41+
#[cfg(target_vendor = "apple")]
4242
#[macro_export]
4343
#[doc(hidden)]
4444
macro_rules! __detect_target_features {
@@ -87,7 +87,7 @@ pub mod hwcaps {
8787
pub const SHA3: c_ulong = libc::HWCAP_SHA3 | libc::HWCAP_SHA512;
8888
}
8989

90-
// macOS `check!` macro.
90+
// Apple OS (macOS, iOS, watchOS, and tvOS) `check!` macro.
9191
//
9292
// NOTE: several of these instructions (e.g. `aes`, `sha2`) can be assumed to
9393
// be present on all Apple ARM64 hardware.
@@ -98,7 +98,7 @@ pub mod hwcaps {
9898
//
9999
// See discussion on this issue for more information:
100100
// <https://github.com/RustCrypto/utils/issues/378>
101-
#[cfg(target_os = "macos")]
101+
#[cfg(target_vendor = "apple")]
102102
#[macro_export]
103103
#[doc(hidden)]
104104
macro_rules! check {
@@ -117,8 +117,8 @@ macro_rules! check {
117117
};
118118
}
119119

120-
/// macOS helper function for calling `sysctlbyname`.
121-
#[cfg(target_os = "macos")]
120+
/// Apple helper function for calling `sysctlbyname`.
121+
#[cfg(target_vendor = "apple")]
122122
pub unsafe fn sysctlbyname(name: &[u8]) -> bool {
123123
assert_eq!(
124124
name.last().cloned(),
@@ -143,36 +143,8 @@ pub unsafe fn sysctlbyname(name: &[u8]) -> bool {
143143
value != 0
144144
}
145145

146-
// iOS `check!` macro.
147-
//
148-
// Unfortunately iOS does not provide access to the `sysctl(3)` API which means
149-
// we can only return static values for CPU features which can be assumed to
150-
// be present on all Apple ARM64 hardware.
151-
//
152-
// See discussion on this issue for more information:
153-
// <https://github.com/RustCrypto/utils/issues/378>
154-
#[cfg(target_os = "ios")]
155-
#[macro_export]
156-
#[doc(hidden)]
157-
macro_rules! check {
158-
("aes") => {
159-
true
160-
};
161-
("sha2") => {
162-
true
163-
};
164-
("sha3") => {
165-
false
166-
};
167-
}
168-
169146
// On other targets, runtime CPU feature detection is unavailable
170-
#[cfg(not(any(
171-
target_os = "ios",
172-
target_os = "linux",
173-
target_os = "android",
174-
target_os = "macos"
175-
)))]
147+
#[cfg(not(any(target_vendor = "apple", target_os = "linux", target_os = "android",)))]
176148
#[macro_export]
177149
#[doc(hidden)]
178150
macro_rules! __detect_target_features {

0 commit comments

Comments
 (0)