From 1353a1675d3d511624fbce2786c720913ef36113 Mon Sep 17 00:00:00 2001 From: SabrinaJewson Date: Mon, 9 Sep 2024 16:14:56 +0100 Subject: [PATCH 1/2] Autodetect CPU target features from Cargo --- Cargo.toml | 2 +- README.md | 15 ++++++--------- build.rs | 19 +++++++++++-------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7812258..1eae208 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ cc = "1" glob = "0.3" [features] -default = ["std", "parallel", "neon"] +default = ["std", "parallel"] std = [] parallel = ["cc/parallel"] neon = [] # ARM NEON SIMD (will crash on ARM CPUs without it) diff --git a/README.md b/README.md index c57ee46..8c00a01 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ libwebp is built with the [`cc`](//lib.rs/cc) crate. It needs a C compiler, but `cmake` is not used. -Set `TARGET_CPU` env var to `native` or your desired CPU architecture to optimize the C code for it. +Set `RUSTFLAGS="-Ctarget-cpu=native"` or your desired CPU architecture to optimize the C code for it. ## Usage @@ -15,19 +15,16 @@ Add the following to the `Cargo.toml` in your project: libwebp-sys = "0.9" ``` -or to require newer CPUs with SIMD support: - -```toml -[dependencies] -libwebp-sys = { version = "0.9", features = ["avx2", "sse41", "neon"] } -``` - or to require `no_std` support: ```toml -libwebp-sys = { version = "0.9", default-features = false, features = ["parallel", "neon"] } +libwebp-sys = { version = "0.9", default-features = false, features = ["parallel"] } ``` +The `neon`, `sse41` and `avx2` feature flags can be set to force support for Neon, SSE 4.1 and AVX2 +respectively, but this is usually unnecessary as it can be set through +`-Ctarget-feature` (e.g. `RUSTFLAGS="-Ctarget-feature=avx2"`) as well. + ## Examples ### Encode diff --git a/build.rs b/build.rs index 111d8e1..89ba885 100644 --- a/build.rs +++ b/build.rs @@ -48,25 +48,28 @@ fn setup_build(build: &mut cc::Build, include_dir: &PathBuf) { build.flag("-D_CRT_SECURE_NO_WARNINGS"); } - if let Ok(target_cpu) = env::var("TARGET_CPU") { - build.flag_if_supported(&format!("-march={}", target_cpu)); - } + let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH"); + let target_features = env::var("CARGO_CFG_TARGET_FEATURE").expect("CARGO_CFG_TARGET_FEATURE"); + let has_feature = |f: &str| target_features.split(',').any(|feature| feature == f); + + let target_cpu = env::var("TARGET_CPU").ok(); + let target_cpu = target_cpu.as_deref().unwrap_or(&*target_arch); + build.flag_if_supported(format!("-march={}", target_cpu)); - let target = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH"); - match target.as_str() { + match target_arch.as_str() { "x86_64" | "i686" => { build.define("WEBP_HAVE_SSE2", Some("1")); - if cfg!(feature = "sse41") { + if cfg!(feature = "sse41") || has_feature("sse4.1") { build.define("WEBP_HAVE_SSE41", Some("1")); build.flag_if_supported("-msse4.1"); } - if cfg!(feature = "avx2") { + if cfg!(feature = "avx2") || has_feature("avx2") { build.define("WEBP_HAVE_AVX2", Some("1")); build.flag_if_supported("-mavx2"); } } "aarch64" => { - if cfg!(feature = "neon") { + if cfg!(feature = "neon") || has_feature("neon") { build.define("WEBP_HAVE_NEON", Some("1")); } From ad069684ad153351d44f67d3cf17a64a2835acd5 Mon Sep 17 00:00:00 2001 From: SabrinaJewson Date: Tue, 10 Sep 2024 09:52:31 +0100 Subject: [PATCH 2/2] =?UTF-8?q?Don=E2=80=99t=20swallow=20errors=20in=20bui?= =?UTF-8?q?ld.rs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/build.rs b/build.rs index 89ba885..9d6b676 100644 --- a/build.rs +++ b/build.rs @@ -28,10 +28,8 @@ fn main() { cc.file(manifest_dir.join(f)); } - for f in glob::glob("vendor/sharpyuv/**/*.c") - .expect("glob vender/src failed") - .flatten() - { + for f in glob::glob("vendor/sharpyuv/**/*.c").expect("glob vendor/src failed") { + let f = f.expect("glob iteration vendor/src failed"); sharpyuv_build.file(manifest_dir.join(f)); } sharpyuv_build.compile("sharpyuv");