Skip to content

Commit eb723db

Browse files
committed
cpufeatures: gracefully degrade on targets without a detection backend
cpufeatures emitted a hard `compile_error!` on any target other than aarch64/loongarch64/x86/x86_64. This breaks otherwise-portable crates that depend on cpufeatures unconditionally but only invoke `cpufeatures::new!` under arch-gated cfgs (e.g. `#[cfg(target_arch = "x86_64")]`), so the detection macros are never actually expanded on other architectures (powerpc, powerpc64, etc.) — the crate simply fails to compile. Add a `fallback` module providing the `__unless_target_features!` and `__detect_target_features!` macros for those targets. Runtime detection is unavailable there, so feature checks rely solely on compile-time target features and otherwise report features as absent — mirroring the existing "other OS" fallback already used within the aarch64 backend.
1 parent 5c7e4f9 commit eb723db

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

cpufeatures/src/fallback.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//! Fallback for targets without a runtime CPU feature detection backend.
2+
//!
3+
//! On architectures cpufeatures has no detection support for (e.g.
4+
//! `powerpc`/`powerpc64`), runtime detection is unavailable, so feature checks
5+
//! fall back to compile-time target features only.
6+
7+
// Evaluate the given `$body` expression if any of the supplied target features
8+
// are not enabled at compile time. Otherwise returns true.
9+
#[macro_export]
10+
#[doc(hidden)]
11+
macro_rules! __unless_target_features {
12+
($($tf:tt),+ => $body:expr ) => {
13+
{
14+
#[cfg(not(all($(target_feature=$tf,)*)))]
15+
$body
16+
17+
#[cfg(all($(target_feature=$tf,)*))]
18+
true
19+
}
20+
};
21+
}
22+
23+
// Runtime CPU feature detection is unavailable on these targets.
24+
#[macro_export]
25+
#[doc(hidden)]
26+
macro_rules! __detect_target_features {
27+
($($tf:tt),+) => {
28+
false
29+
};
30+
}

cpufeatures/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ mod x86;
2222
#[cfg(miri)]
2323
mod miri;
2424

25+
#[cfg(not(miri))]
2526
#[cfg(not(any(
2627
target_arch = "aarch64",
2728
target_arch = "loongarch64",
2829
target_arch = "x86",
2930
target_arch = "x86_64"
3031
)))]
31-
compile_error!("This crate works only on `aarch64`, `loongarch64`, `x86`, and `x86-64` targets.");
32+
mod fallback;
3233

3334
/// Create module with CPU feature detection code.
3435
#[macro_export]

0 commit comments

Comments
 (0)