Skip to content

Commit

Permalink
build.rs: Use pkg-config to find header search paths for bindgen.
Browse files Browse the repository at this point in the history
  • Loading branch information
Francis Nixon committed Nov 14, 2024
1 parent d07416b commit 0ddaf4e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ uuid = "1.0.0"
[features]
default = []
deprecated = []
pkg-config = ["libblkid-rs-sys/pkg-config"]
static = ["pkg-config", "libblkid-rs-sys/static"]

[lints.rust]
warnings = { level = "deny" }
Expand Down
6 changes: 6 additions & 0 deletions libblkid-rs-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ keywords = ["storage"]

[build-dependencies]
cc = "1.0.45"
pkg-config = "0.3.31"
cfg-if = "1.0.0"

[build-dependencies.bindgen]
default-features = false
Expand All @@ -30,3 +32,7 @@ nonstandard_style = { level = "deny", priority = 4 }
[lints.clippy]
all = { level = "deny" }
cargo = { level = "deny" , priority = 1}

[features]
static = ["pkg-config"]
pkg-config = []
29 changes: 26 additions & 3 deletions libblkid-rs-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
use bindgen::Builder;

use cfg_if::cfg_if;
use std::{env, path::PathBuf};

fn main() {
println!("cargo:rustc-link-lib=blkid");
cfg_if! {
if #[cfg(feature = "pkg-config")] {
fn setup_bindings_builder() -> Builder {
let mut pkg_config = pkg_config::Config::new();
let pkg_config = pkg_config.atleast_version("2.33.2");
#[cfg(feature = "static")]
{
pkg_config.statik(true);
}
let libblkid = pkg_config.probe("blkid").expect("Failed to find libblkid?");

Builder::default().clang_args(libblkid
.include_paths
.iter()
.map(|include|format!("-I{}", include.display())))
}
}else {
fn setup_bindings_builder() -> Builder {
println!("cargo:rustc-link-lib=blkid");
Builder::default()
}
}
}

let bindings = Builder::default()
fn main() {
let bindings = setup_bindings_builder()
.header("header.h")
.size_t_is_usize(true)
.generate()
Expand Down

0 comments on commit 0ddaf4e

Please sign in to comment.