From d9968f0adca617d8e75612936850ad468da58400 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sun, 8 Sep 2024 07:29:46 +0900 Subject: [PATCH] detect: Make macOS implementation available on other Apple targets --- .github/workflows/ci.yml | 5 ++- src/imp/atomic128/README.md | 4 +-- src/imp/atomic128/aarch64.rs | 12 +++++-- src/imp/atomic128/detect/aarch64_aa64reg.rs | 2 +- .../{aarch64_macos.rs => aarch64_apple.rs} | 24 +++++++++++--- src/imp/atomic128/detect/common.rs | 33 ++++++++++++++++--- 6 files changed, 65 insertions(+), 15 deletions(-) rename src/imp/atomic128/detect/{aarch64_macos.rs => aarch64_apple.rs} (88%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 933b26e1..9239c014 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -126,7 +126,10 @@ jobs: os: windows-latest - rust: nightly target: aarch64-apple-darwin - os: macos-14 # AArch64 + os: macos-latest # AArch64 + - rust: nightly + target: aarch64-apple-ios-macabi + os: macos-latest # AArch64 - rust: '1.59' # LLVM 13 target: aarch64-unknown-linux-gnu # - rust: '1.64' # LLVM 14 diff --git a/src/imp/atomic128/README.md b/src/imp/atomic128/README.md index 1d53e8ee..d93e343a 100644 --- a/src/imp/atomic128/README.md +++ b/src/imp/atomic128/README.md @@ -41,8 +41,8 @@ Here is the table of targets that support run-time CPU feature detection and the | aarch64 | android | getauxval | all | Enabled by default | | aarch64 | freebsd | elf_aux_info | lse, lse2 | Enabled by default | | aarch64 | netbsd | sysctl | all | Enabled by default | -| aarch64 | openbsd | sysctl | all | Enabled by default | -| aarch64 | macos | sysctl | all | Currently only used in tests because FEAT_LSE and FEAT_LSE2 are always available at compile-time. | +| aarch64 | openbsd | sysctlbyname | all | Enabled by default | +| aarch64 | macos/ios/tvos/watchos/visionos | sysctlbyname | all | Currently only used in tests because FEAT_LSE and FEAT_LSE2 are always available at compile-time. | | aarch64 | windows | IsProcessorFeaturePresent | lse | Enabled by default | | aarch64 | fuchsia | zx_system_get_features | lse | Enabled by default | | powerpc64 | linux | getauxval | all | Disabled by default | diff --git a/src/imp/atomic128/aarch64.rs b/src/imp/atomic128/aarch64.rs index 06db9c7f..2b2e5afd 100644 --- a/src/imp/atomic128/aarch64.rs +++ b/src/imp/atomic128/aarch64.rs @@ -141,9 +141,15 @@ mod detect_aa64reg; // mod detect_auxv; #[cfg(test)] #[cfg(not(portable_atomic_no_outline_atomics))] -#[cfg(target_os = "macos")] -#[path = "detect/aarch64_macos.rs"] -mod detect_macos; +#[cfg(any( + target_os = "macos", + target_os = "ios", + target_os = "tvos", + target_os = "watchos", + target_os = "visionos", +))] +#[path = "detect/aarch64_apple.rs"] +mod detect_apple; #[cfg(not(portable_atomic_no_asm))] use core::arch::asm; diff --git a/src/imp/atomic128/detect/aarch64_aa64reg.rs b/src/imp/atomic128/detect/aarch64_aa64reg.rs index 059cba51..758868c6 100644 --- a/src/imp/atomic128/detect/aarch64_aa64reg.rs +++ b/src/imp/atomic128/detect/aarch64_aa64reg.rs @@ -18,7 +18,7 @@ Supported platforms: https://github.com/torvalds/linux/commit/77c97b4ee21290f5f083173d957843b615abbff2 - FreeBSD 12.0+ (emulate mrs instruction) https://github.com/freebsd/freebsd-src/commit/398810619cb32abf349f8de23f29510b2ee0839b -- NetBSD 9.0+ (through sysctl) +- NetBSD 9.0+ (through sysctl/sysctlbyname) https://github.com/NetBSD/src/commit/0e9d25528729f7fea53e78275d1bc5039dfe8ffb - OpenBSD 7.1+ (through sysctl) https://github.com/openbsd/src/commit/d335af936b9d7dd9cf655cae1ce19560c45de6c8 diff --git a/src/imp/atomic128/detect/aarch64_macos.rs b/src/imp/atomic128/detect/aarch64_apple.rs similarity index 88% rename from src/imp/atomic128/detect/aarch64_macos.rs rename to src/imp/atomic128/detect/aarch64_apple.rs index fb334c9b..c51afbcf 100644 --- a/src/imp/atomic128/detect/aarch64_macos.rs +++ b/src/imp/atomic128/detect/aarch64_apple.rs @@ -1,7 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT /* -Run-time CPU feature detection on AArch64 macOS by using sysctl. +Run-time CPU feature detection on AArch64 Apple targets by using sysctlbyname. This module is currently only enabled on tests because AArch64 macOS always supports FEAT_LSE and FEAT_LSE2. https://github.com/llvm/llvm-project/blob/llvmorg-18.1.2/llvm/include/llvm/TargetParser/AArch64TargetParser.h#L728 @@ -12,9 +12,10 @@ M4 is Armv9.2 and it doesn't support FEAT_LSE128/FEAT_LRCPC3. Refs: https://developer.apple.com/documentation/kernel/1387446-sysctlbyname/determining_instruction_set_characteristics -Note that iOS doesn't support sysctl: +TODO: non-macOS targets doesn't always supports FEAT_LSE2, but sysctl on them on app stores is...? - https://developer.apple.com/forums/thread/9440 - https://nabla-c0d3.github.io/blog/2015/06/16/ios9-security-privacy +- https://github.com/rust-lang/stdarch/pull/1636 */ include!("common.rs"); @@ -136,7 +137,14 @@ mod tests { clippy::no_effect_underscore_binding )] const _: fn() = || { - use test_helper::{libc, sys}; + use test_helper::libc; + #[cfg(not(any( + target_os = "ios", + target_os = "tvos", + target_os = "watchos", + target_os = "visionos", + )))] + use test_helper::sys; let mut _sysctlbyname: unsafe extern "C" fn( *const ffi::c_char, *mut ffi::c_void, @@ -145,6 +153,14 @@ mod tests { ffi::c_size_t, ) -> ffi::c_int = ffi::sysctlbyname; _sysctlbyname = libc::sysctlbyname; - _sysctlbyname = sys::sysctlbyname; + #[cfg(not(any( + target_os = "ios", + target_os = "tvos", + target_os = "watchos", + target_os = "visionos", + )))] // TODO + { + _sysctlbyname = sys::sysctlbyname; + } }; } diff --git a/src/imp/atomic128/detect/common.rs b/src/imp/atomic128/detect/common.rs index 6f4a6e47..d765d64f 100644 --- a/src/imp/atomic128/detect/common.rs +++ b/src/imp/atomic128/detect/common.rs @@ -148,23 +148,48 @@ mod c_types { // Hexagon https://lists.llvm.org/pipermail/llvm-dev/attachments/20190916/21516a52/attachment-0001.pdf // AIX https://www.ibm.com/docs/en/xl-c-aix/13.1.2?topic=descriptions-qchars // z/OS https://www.ibm.com/docs/en/zos/2.5.0?topic=specifiers-character-types - // (macOS is currently the only Apple target that uses this module, and Windows currently doesn't use this module) - #[cfg(not(target_os = "macos"))] + // (Windows currently doesn't use this module) + #[cfg(not(any( + target_os = "macos", + target_os = "ios", + target_os = "tvos", + target_os = "watchos", + target_os = "visionos", + )))] pub(crate) type c_char = u8; // c_char is i8 on all Apple targets - #[cfg(target_os = "macos")] + #[cfg(any( + target_os = "macos", + target_os = "ios", + target_os = "tvos", + target_os = "watchos", + target_os = "visionos", + ))] pub(crate) type c_char = i8; // Static assertions for C type definitions. #[cfg(test)] const _: fn() = || { - use test_helper::{libc, sys}; + use test_helper::libc; + #[cfg(not(any( + target_os = "ios", + target_os = "tvos", + target_os = "watchos", + target_os = "visionos", + )))] + use test_helper::sys; let _: c_int = 0 as std::os::raw::c_int; let _: c_uint = 0 as std::os::raw::c_uint; let _: c_long = 0 as std::os::raw::c_long; let _: c_ulong = 0 as std::os::raw::c_ulong; let _: c_size_t = 0 as libc::size_t; // std::os::raw::c_size_t is unstable let _: c_char = 0 as std::os::raw::c_char; + #[cfg(not(any( + target_os = "ios", + target_os = "tvos", + target_os = "watchos", + target_os = "visionos", + )))] // TODO let _: c_char = 0 as sys::c_char; }; }