use crate::platform::caps::{Arch, Caps};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum OverrideError {
AlreadyInitialized,
Unsupported,
}
impl core::fmt::Display for OverrideError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::AlreadyInitialized => f.write_str("detection cache already initialized"),
Self::Unsupported => f.write_str("override unsupported on this target"),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Detected {
pub caps: Caps,
pub arch: Arch,
}
impl Detected {
#[inline]
#[must_use]
pub const fn portable() -> Self {
Self {
caps: Caps::NONE,
arch: Arch::Other,
}
}
}
#[inline]
#[must_use]
pub fn get() -> Detected {
#[cfg(miri)]
{
return Detected::portable();
}
#[cfg(not(miri))]
{
#[allow(unused_mut)]
let mut det = {
#[cfg(feature = "std")]
{
*STD_CACHE.get_or_init(detect_with_override)
}
#[cfg(all(not(feature = "std"), target_has_atomic = "64"))]
{
atomic_cache::get_or_init(detect_with_override)
}
#[cfg(all(not(feature = "std"), not(target_has_atomic = "64")))]
{
detect_with_override()
}
};
debug_assert!(
crate::platform::target_matrix::manifest_has_arch(det.arch),
"detected arch policy drifted from .config/target-matrix.json"
);
det
}
}
#[inline]
#[must_use]
pub fn caps() -> Caps {
#[cfg(feature = "portable-only")]
{
Caps::NONE
}
#[cfg(not(feature = "portable-only"))]
{
get().caps
}
}
#[inline]
#[must_use]
pub fn arch() -> Arch {
get().arch
}
include!("detect/compile_time.rs");
include!("detect/cache_override.rs");
#[cold]
#[must_use]
pub fn detect_uncached() -> Detected {
#[cfg(target_arch = "x86_64")]
{
detect_x86_64()
}
#[cfg(target_arch = "x86")]
{
detect_x86()
}
#[cfg(target_arch = "aarch64")]
{
detect_aarch64()
}
#[cfg(target_arch = "riscv64")]
{
detect_riscv64()
}
#[cfg(target_arch = "riscv32")]
{
detect_riscv32()
}
#[cfg(target_arch = "s390x")]
{
detect_s390x()
}
#[cfg(target_arch = "powerpc64")]
{
detect_power()
}
#[cfg(target_arch = "wasm32")]
{
detect_wasm32()
}
#[cfg(target_arch = "wasm64")]
{
detect_wasm64()
}
#[cfg(not(any(
target_arch = "x86_64",
target_arch = "x86",
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "riscv32",
target_arch = "s390x",
target_arch = "powerpc64",
target_arch = "wasm32",
target_arch = "wasm64"
)))]
{
Detected::portable()
}
}
include!("detect/arch/x86.rs");
include!("detect/arch/aarch64.rs");
include!("detect/arch/riscv.rs");
include!("detect/arch/s390x.rs");
include!("detect/arch/power.rs");
include!("detect/arch/wasm.rs");
include!("detect/tests.rs");