Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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
60 changes: 36 additions & 24 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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"));
Expand Down
Loading