|
| 1 | +#[cfg(not(feature = "system-dylib"))] |
1 | 2 | use std::env; |
2 | 3 | use std::path::PathBuf; |
3 | 4 |
|
4 | | -fn main() { |
5 | | - #[cfg(feature = "system-dylib")] |
6 | | - system_dylib(); |
| 5 | +#[cfg(feature = "system-dylib")] |
| 6 | +const PACKAGE_CONFIG_NAME: &str = "libwebp"; |
7 | 7 |
|
| 8 | +fn main() { |
8 | 9 | #[cfg(feature = "generate-bindings")] |
9 | 10 | generate_bindings(); |
10 | 11 |
|
| 12 | + #[cfg(all(not(feature = "generate-bindings"), feature = "system-dylib"))] |
| 13 | + pkg_config::Config::new() |
| 14 | + .probe(PACKAGE_CONFIG_NAME) |
| 15 | + .unwrap(); |
| 16 | + |
| 17 | + #[cfg(not(feature = "system-dylib"))] |
| 18 | + do_build(); |
| 19 | +} |
| 20 | + |
| 21 | +#[cfg(feature = "generate-bindings")] |
| 22 | +fn generate_bindings() { |
| 23 | + println!("cargo:rerun-if-changed=wrap.h"); |
| 24 | + let bindings = bindgen::Builder::default() |
| 25 | + .header("wrap.h") |
| 26 | + .default_enum_style(bindgen::EnumVariation::Rust { |
| 27 | + non_exhaustive: false, |
| 28 | + }) |
| 29 | + .trust_clang_mangling(false) |
| 30 | + .clang_args( |
| 31 | + include_paths() |
| 32 | + .into_iter() |
| 33 | + .map(|include_path| format!("-I{}", include_path.to_str().unwrap())), |
| 34 | + ) |
| 35 | + .impl_debug(true) |
| 36 | + .allowlist_function("[wW][eE][bB].*") |
| 37 | + .allowlist_var("[wW][eE][bB].*") |
| 38 | + .allowlist_type("[wW][eE][bB].*") |
| 39 | + .use_core() |
| 40 | + .generate() |
| 41 | + .expect("Unable to generate bindings"); |
| 42 | + |
| 43 | + bindings |
| 44 | + .write_to_file("src/ffi.rs") |
| 45 | + .expect("Couldn't write bindings to ffi.rs"); |
| 46 | +} |
| 47 | + |
| 48 | +#[cfg(all(feature = "system-dylib", feature = "generate-bindings"))] |
| 49 | +fn include_paths() -> Vec<PathBuf> { |
| 50 | + let system_libwebp = pkg_config::Config::new() |
| 51 | + .probe(PACKAGE_CONFIG_NAME) |
| 52 | + .unwrap(); |
| 53 | + system_libwebp.include_paths |
| 54 | +} |
| 55 | + |
| 56 | +#[cfg(all(not(feature = "system-dylib"), feature = "generate-bindings"))] |
| 57 | +fn include_paths() -> Vec<PathBuf> { |
| 58 | + vec![PathBuf::from("vendor/src")] |
| 59 | +} |
| 60 | + |
| 61 | +#[cfg(not(feature = "system-dylib"))] |
| 62 | +fn do_build() { |
11 | 63 | let manifest_dir = |
12 | 64 | PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR")); |
13 | 65 | let vendor = manifest_dir.join("vendor"); |
@@ -36,38 +88,7 @@ fn main() { |
36 | 88 | cc.compile("webpsys"); |
37 | 89 | } |
38 | 90 |
|
39 | | -#[cfg(feature = "system-dylib")] |
40 | | -fn system_dylib() { |
41 | | - let lib_name = "libwebp"; |
42 | | - let find_system_lib = pkg_config::Config::new().probe(lib_name).is_ok(); |
43 | | - |
44 | | - if find_system_lib { |
45 | | - println!("cargo:rustc-link-lib={lib_name}"); |
46 | | - return; |
47 | | - } |
48 | | -} |
49 | | - |
50 | | -#[cfg(feature = "generate-bindings")] |
51 | | -fn generate_bindings() { |
52 | | - let bindings = bindgen::Builder::default() |
53 | | - .header("wrap.h") |
54 | | - .default_enum_style(bindgen::EnumVariation::Rust { |
55 | | - non_exhaustive: false, |
56 | | - }) |
57 | | - .trust_clang_mangling(false) |
58 | | - .impl_debug(true) |
59 | | - .allowlist_function("[wW][eE][bB].*") |
60 | | - .allowlist_var("[wW][eE][bB].*") |
61 | | - .allowlist_type("[wW][eE][bB].*") |
62 | | - .use_core() |
63 | | - .generate() |
64 | | - .expect("Unable to generate bindings"); |
65 | | - |
66 | | - bindings |
67 | | - .write_to_file("src/ffi.rs") |
68 | | - .expect("Couldn't write bindings to ffi.rs"); |
69 | | -} |
70 | | - |
| 91 | +#[cfg(not(feature = "system-dylib"))] |
71 | 92 | fn setup_build(build: &mut cc::Build, include_dir: &PathBuf) { |
72 | 93 | build.include(include_dir); |
73 | 94 | build.define("NDEBUG", Some("1")); |
|
0 commit comments