use std::{
env,
path::{Path, PathBuf},
};
const FALLBACK_PREFIXES: &[&str] = &["/opt/local", "/opt/homebrew", "/usr/local"];
fn main() {
println!("cargo:rerun-if-changed=libguestfs-wrapper.h");
let libguestfs = pkg_config::Config::new().probe("libguestfs").expect(
"libguestfs development files were not found; install libguestfs-devel or equivalent",
);
let mut builder = bindgen::Builder::default().header("libguestfs-wrapper.h");
for include_path in &libguestfs.include_paths {
builder = builder.clang_arg(format!("-I{}", include_path.display()));
}
if libguestfs.include_paths.is_empty() {
if let Some(prefix) = FALLBACK_PREFIXES
.iter()
.find(|prefix| Path::new(prefix).join("include/guestfs.h").is_file())
{
builder = builder.clang_arg(format!("-I{prefix}/include"));
}
}
if libguestfs.link_paths.is_empty() {
if let Some(prefix) = FALLBACK_PREFIXES.iter().find(|prefix| {
Path::new(prefix)
.join("lib")
.join("libguestfs.dylib")
.is_file()
|| Path::new(prefix)
.join("lib")
.join("libguestfs.so")
.is_file()
}) {
println!("cargo:rustc-link-search=native={prefix}/lib");
}
}
let bindings = builder
.allowlist_type("guestfs_h")
.allowlist_type("guestfs_add_drive_opts_argv")
.allowlist_function(
"guestfs_(create|close|set_error_handler|last_error|last_errno|add_drive_opts_argv|launch|inspect_os|inspect_get_mountpoints|mount_ro|mount_options|mount_vfs|copy_out|exists|is_symlink|umount_all|shutdown)",
)
.allowlist_var("GUESTFS_ADD_DRIVE_OPTS_(READONLY|FORMAT)")
.layout_tests(false)
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("generate libguestfs bindings");
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR is set by Cargo"));
bindings
.write_to_file(out_dir.join("guestfs_bindings.rs"))
.expect("write generated libguestfs bindings");
}