diff --git a/build.rs b/build.rs index 4ae3184..e07ad6f 100644 --- a/build.rs +++ b/build.rs @@ -5,12 +5,19 @@ fn main() { enable_libc(); } -// This adds various simd cfgs if this compiler supports it. +// This adds various simd cfgs if this compiler and target support it. // // This can be disabled with RUSTFLAGS="--cfg memchr_disable_auto_simd", but // this is generally only intended for testing. +// +// On targets which don't feature SSE2, this is disabled, as LLVM wouln't know +// how to work with SSE2 operands. Enabling SSE4.2 and AVX on SSE2-only targets +// is not a problem. In that case, the fastest option will be chosen at +// runtime. fn enable_simd_optimizations() { - if is_env_set("CARGO_CFG_MEMCHR_DISABLE_AUTO_SIMD") { + if is_env_set("CARGO_CFG_MEMCHR_DISABLE_AUTO_SIMD") + || !target_has_feature("sse2") + { return; } println!("cargo:rustc-cfg=memchr_runtime_simd"); @@ -59,3 +66,9 @@ fn is_feature_set(name: &str) -> bool { fn is_env_set(name: &str) -> bool { env::var_os(name).is_some() } + +fn target_has_feature(feature: &str) -> bool { + env::var("CARGO_CFG_TARGET_FEATURE") + .map(|features| features.contains(feature)) + .unwrap_or(false) +}