From 0ddaf4e1bef0e8382727642aadd0f95fe93a2603 Mon Sep 17 00:00:00 2001 From: Francis Nixon Date: Mon, 28 Oct 2024 17:59:00 -0400 Subject: [PATCH] build.rs: Use pkg-config to find header search paths for bindgen. --- Cargo.toml | 2 ++ libblkid-rs-sys/Cargo.toml | 6 ++++++ libblkid-rs-sys/build.rs | 29 ++++++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d3d8f8c..c74d777 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/libblkid-rs-sys/Cargo.toml b/libblkid-rs-sys/Cargo.toml index 51e5a10..20a2145 100644 --- a/libblkid-rs-sys/Cargo.toml +++ b/libblkid-rs-sys/Cargo.toml @@ -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 @@ -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 = [] \ No newline at end of file diff --git a/libblkid-rs-sys/build.rs b/libblkid-rs-sys/build.rs index b1997cc..f13063c 100644 --- a/libblkid-rs-sys/build.rs +++ b/libblkid-rs-sys/build.rs @@ -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()