Skip to content

Commit f3670fd

Browse files
authored
Fix Dynamic Linking (#44)
* Only do the cc build if `system-dylib` feature is disabled. * dynamic linking: Generate bindings using system headers * dylib: Link against libwebp `pkg_config` automaticially links against libwebp when we probe it. libwebp is already probed if `generate-bindings` (in `include_paths`), but we need to manually probe if `generate-bindings` is disabled.
1 parent 81fe59a commit f3670fd

2 files changed

Lines changed: 61 additions & 40 deletions

File tree

‎build.rs‎

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,65 @@
1+
#[cfg(not(feature = "system-dylib"))]
12
use std::env;
23
use std::path::PathBuf;
34

4-
fn main() {
5-
#[cfg(feature = "system-dylib")]
6-
system_dylib();
5+
#[cfg(feature = "system-dylib")]
6+
const PACKAGE_CONFIG_NAME: &str = "libwebp";
77

8+
fn main() {
89
#[cfg(feature = "generate-bindings")]
910
generate_bindings();
1011

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() {
1163
let manifest_dir =
1264
PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
1365
let vendor = manifest_dir.join("vendor");
@@ -36,38 +88,7 @@ fn main() {
3688
cc.compile("webpsys");
3789
}
3890

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"))]
7192
fn setup_build(build: &mut cc::Build, include_dir: &PathBuf) {
7293
build.include(include_dir);
7394
build.define("NDEBUG", Some("1"));

‎wrap.h‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include "vendor/src/webp/types.h"
2-
#include "vendor/src/webp/encode.h"
3-
#include "vendor/src/webp/decode.h"
4-
#include "vendor/src/webp/mux.h"
5-
#include "vendor/src/webp/demux.h"
1+
#include "webp/types.h"
2+
#include "webp/encode.h"
3+
#include "webp/decode.h"
4+
#include "webp/mux.h"
5+
#include "webp/demux.h"

0 commit comments

Comments
 (0)