diff --git a/Cargo.toml b/Cargo.toml index cf47d76..cc90a33 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,7 +33,7 @@ opt-level = 2 cc = "1" glob = "0.3" pkg-config = "0.3" -bindgen = "0.72" +bindgen = { version = "0.72", optional = true } [features] default = ["std", "parallel"] @@ -44,3 +44,4 @@ sse41 = [] # x64 SSE 4.1 (will crash on x86 CPUs without it) avx2 = [] # x64 AVX2 (will crash on x86 CPUs without it) system-dylib = [ ] # Use the system-installed dylib instead of compiling a static library from the vendor +generate-bindings = ["dep:bindgen"] # Generate ffi.rs based on the compile target \ No newline at end of file diff --git a/build.rs b/build.rs index 20797dc..7903281 100644 --- a/build.rs +++ b/build.rs @@ -2,31 +2,11 @@ use std::env; use std::path::PathBuf; fn main() { - if cfg!(feature = "system-dylib") { - let lib_name = "libwebp"; - let find_system_lib = pkg_config::Config::new().probe(lib_name).is_ok(); + #[cfg(feature = "system-dylib")] + system_dylib(); - if find_system_lib { - println!("cargo:rustc-link-lib={lib_name}"); - return; - } - } - - let bindings = bindgen::Builder::default() - .header("wrap.h") - .default_enum_style(bindgen::EnumVariation::Rust { - non_exhaustive: false, - }) - .trust_clang_mangling(false) - .impl_debug(true) - .allowlist_function("[wW][eE][bB].*") - .allowlist_var("[wW][eE][bB].*") - .allowlist_type("[wW][eE][bB].*") - .use_core() - .generate() - .expect("Unable to generate bindings"); - - bindings.write_to_file("src/ffi.rs").unwrap(); + #[cfg(feature = "generate-bindings")] + generate_bindings(); let manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR")); @@ -56,6 +36,38 @@ fn main() { cc.compile("webpsys"); } +#[cfg(feature = "system-dylib")] +fn system_dylib() { + let lib_name = "libwebp"; + let find_system_lib = pkg_config::Config::new().probe(lib_name).is_ok(); + + if find_system_lib { + println!("cargo:rustc-link-lib={lib_name}"); + return; + } +} + +#[cfg(feature = "generate-bindings")] +fn generate_bindings() { + let bindings = bindgen::Builder::default() + .header("wrap.h") + .default_enum_style(bindgen::EnumVariation::Rust { + non_exhaustive: false, + }) + .trust_clang_mangling(false) + .impl_debug(true) + .allowlist_function("[wW][eE][bB].*") + .allowlist_var("[wW][eE][bB].*") + .allowlist_type("[wW][eE][bB].*") + .use_core() + .generate() + .expect("Unable to generate bindings"); + + bindings + .write_to_file("src/ffi.rs") + .expect("Couldn't write bindings to ffi.rs"); +} + fn setup_build(build: &mut cc::Build, include_dir: &PathBuf) { build.include(include_dir); build.define("NDEBUG", Some("1"));