Skip to content

Commit

Permalink
- .h files from libheif was embedded in the crate sources.
Browse files Browse the repository at this point in the history
- Changed `build.rs` to use embedded `.h` files for the bindgen build stage instead of `.h` files from the installed libheif library.
  • Loading branch information
Cykooz committed Nov 14, 2024
1 parent 105ede3 commit 5dcadf4
Show file tree
Hide file tree
Showing 10 changed files with 3,431 additions and 53 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Change Log

## [Unreleased] - ReleaseDate

### Changes

- `.h` files from `libheif` was embedded in the crate sources.
- Changed `build.rs` to use embedded `.h` files for the bindgen build stage
instead of `.h` files from the installed libheif library.

Now you can link the crate with any version of `libheif`
that is backward compatible with the version supported by the crate.

## [3.0.1] - 2024-11-12

### Added
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ license = "MIT"
documentation = "https://docs.rs/crate/libheif-sys"
links = "heif"
build = "build.rs"
exclude = ["/data"]


[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ available.

## System dependencies

- `libheif-dev` >= 1.18.0 (any version if `use-bindgen` feature is enabled).
- `libheif-dev` >= 1.18.0.
- `clang` - to generate rust bindings for `libheif`.
[See bindgen requirements.](https://rust-lang.github.io/rust-bindgen/requirements.html)

Expand Down
63 changes: 14 additions & 49 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,23 @@ fn main() {
}

println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=wrapper.hpp");
println!("cargo:rerun-if-changed=include/wrapper.h");

// Tell cargo to tell rustc to link the heif library.
#[allow(unused_mut)]
#[allow(unused_variables)]
#[allow(unused_assignments)]
let mut include_dirs: Vec<String> = Vec::new();

#[cfg(not(target_os = "windows"))]
{
#[cfg(feature = "compile-libheif")]
compile_libheif();

include_dirs.extend(find_libheif());
find_libheif();
}

#[cfg(target_os = "windows")]
include_dirs.extend(install_libheif_by_vcpkg());
install_libheif_by_vcpkg();

#[cfg(feature = "use-bindgen")]
run_bindgen(&include_dirs);
run_bindgen();
}

#[allow(dead_code)]
Expand Down Expand Up @@ -136,55 +132,31 @@ fn compile_libheif() {
}

#[cfg(not(target_os = "windows"))]
fn find_libheif() -> Vec<String> {
fn find_libheif() {
let mut config = pkg_config::Config::new();
config.atleast_version(MIN_LIBHEIF_VERSION);
#[cfg(feature = "compile-libheif")]
config.statik(true);

match config.probe("libheif") {
Ok(library) => library
.include_paths
.iter()
.map(|dir| dir.to_string_lossy().to_string())
.collect(),
Err(err) => {
println!("cargo:warning={}", err);
std::process::exit(1);
}
if let Err(err) = config.probe("libheif") {
println!("cargo:warning={}", err);
std::process::exit(1);
}
}

#[cfg(target_os = "windows")]
fn install_libheif_by_vcpkg() -> Vec<String> {
fn install_libheif_by_vcpkg() {
let vcpkg_lib = vcpkg::Config::new()
.emit_includes(true)
.find_package("libheif");
let mut include_dirs = Vec::new();
match vcpkg_lib {
Ok(lib) => {
// https://users.rust-lang.org/t/bindgen-cant-find-included-file/62687
use walkdir::WalkDir;
for path in lib.include_paths {
for subdir in WalkDir::new(path)
.into_iter()
.filter_entry(|e| e.file_type().is_dir())
{
let dir = subdir.unwrap().path().to_string_lossy().to_string();
include_dirs.push(dir);
}
}
include_dirs
}
Err(err) => {
println!("cargo:warning={}", err);
std::process::exit(1);
}
if let Err(err) = vcpkg_lib {
println!("cargo:warning={}", err);
std::process::exit(1);
}
}

#[cfg(feature = "use-bindgen")]
fn run_bindgen(include_dirs: &[String]) {
fn run_bindgen() {
let mut base_builder = bindgen::Builder::default()
.generate_comments(true)
.formatter(bindgen::Formatter::Rustfmt)
Expand All @@ -199,13 +171,6 @@ fn run_bindgen(include_dirs: &[String]) {
"-fparse-all-comments",
"-fretain-comments-from-system-headers",
]);
if !include_dirs.is_empty() {
base_builder = base_builder.clang_args(
include_dirs
.iter()
.map(|dir| format!("--include-directory={}", dir)),
);
}

// Don't derive Copy and Clone for structures with pointers.
for struct_name in [
Expand All @@ -218,7 +183,7 @@ fn run_bindgen(include_dirs: &[String]) {
}

// The main module
let builder = base_builder.clone().header("wrapper.hpp");
let builder = base_builder.clone().header("include/wrapper.h");
// Finish the builder and generate the bindings.
let bindings = builder
.generate()
Expand Down
Loading

0 comments on commit 5dcadf4

Please sign in to comment.