extern crate bindgen;
use std::{env, fs::canonicalize, path::PathBuf};
fn main() {
println!("cargo:rerun-if-changed=wrapper.h");
println!("cargo:rerun-if-changed=build.rs");
if cfg!(feature = "pls-generate") {
let gnutls_path = match env::consts::OS {
"linux" => "/usr/include",
"macos" => "/opt/homebrew/include",
"windows" => {
panic!("Generating bindings on Windows is broken, pls remove the pls-generate feature.");
}
_ => panic!("Unsupported OS"),
};
let bindings = bindgen::Builder::default()
.header("wrapper.h")
.clang_arg(format!("-I{}", gnutls_path))
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate()
.expect("Unable to generate bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
if cfg!(feature = "vendored") {
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
env::set_current_dir(out_path).unwrap();
repo_setup("https://github.com/libimobiledevice/libplist.git");
let dst = autotools::Config::new("libplist")
.without("cython", None)
.build();
println!(
"cargo:rustc-link-search=native={}",
dst.join("lib").display()
);
println!("cargo:rustc-link-lib=static=plist-2.0");
} else {
let override_path = PathBuf::from("./override").join(env::var("TARGET").unwrap());
if override_path.exists() {
println!(
"cargo:rustc-link-search={}",
canonicalize(&override_path).unwrap().display()
);
}
println!("cargo:rustc-link-search=/usr/local/lib");
println!("cargo:rustc-link-search=/usr/lib");
println!("cargo:rustc-link-search=/opt/homebrew/lib");
println!("cargo:rustc-link-search=/usr/local/opt/libimobiledevice/lib");
println!("cargo:rustc-link-search=/usr/local/opt/libusbmuxd/lib");
println!("cargo:rustc-link-search=/usr/local/opt/libimobiledevice-glue/lib");
}
}
fn repo_setup(url: &str) {
let mut cmd = std::process::Command::new("git");
cmd.arg("clone");
cmd.arg(url);
cmd.output().unwrap();
env::set_current_dir(url.split('/').last().unwrap().replace(".git", "")).unwrap();
env::set_var("NOCONFIGURE", "1");
let mut cmd = std::process::Command::new("./autogen.sh");
let _ = cmd.output();
env::remove_var("NOCONFIGURE");
env::set_current_dir("..").unwrap();
}