extern crate bindgen;
use std::{io::Write, path::PathBuf};
#[cfg(target_os = "windows")]
const SOLCLIENT_GZ_PATH: &str = "solclient_Win_vs2015_7.25.0.10.tar.gz";
#[cfg(target_os = "macos")]
const SOLCLIENT_GZ_PATH: &str = "solclient_Darwin-universal2_opt_7.25.0.10.tar.gz";
#[cfg(target_os = "linux")]
const SOLCLIENT_GZ_PATH: &str = "solclient_Linux26-x86_64_opt_7.25.0.10.tar.gz";
fn main() {
let solclient_folder_name = "../solclient-7.25.0.10";
let solclient_folder_path = std::path::Path::new(solclient_folder_name);
let solclient_gz_url = format!(
"https://github.com/Yvictor/rsolace/releases/download/0.0.0/{}",
SOLCLIENT_GZ_PATH
);
let resp = reqwest::blocking::get(solclient_gz_url).unwrap();
let content = resp.bytes().unwrap();
let file_gz_name = format!("{}.tar.gz", solclient_folder_name);
let file_gz_path = std::path::Path::new(&file_gz_name);
if !file_gz_path.exists() {
let mut file_gz = std::fs::File::create(file_gz_path).unwrap();
file_gz.write_all(&content).unwrap();
file_gz.sync_data().unwrap();
}
let file_gz = std::fs::File::open(file_gz_path).unwrap();
let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(file_gz));
archive
.entries()
.unwrap()
.filter_map(|r| r.ok())
.map(|mut entry| -> std::io::Result<PathBuf> {
let strip_path = entry.path()?.iter().skip(1).collect::<std::path::PathBuf>();
let path = solclient_folder_path.join(strip_path);
entry.unpack(&path)?;
Ok(path)
})
.filter_map(|e| e.ok())
.for_each(|x| println!("> {}", x.display()));
println!(
"cargo:rustc-link-search=native={}/lib",
solclient_folder_name
);
println!(
"cargo:rustc-link-search=native=rsolace-sys/{}/lib",
solclient_folder_name
);
let os = std::env::consts::OS;
if os == "macos" {
println!("cargo:rustc-link-lib=dylib=gssapi_krb5");
}
println!("cargo:rustc-link-lib=static=ssl");
println!("cargo:rustc-link-lib=static=crypto");
println!("cargo:rustc-link-lib=static=solclient");
let include_path = solclient_folder_path.join("include");
let include_arg = format!("-I{}", include_path.to_str().unwrap());
println!(
"cargo:rerun-if-changed={}/include/solclient/solClient.h",
solclient_folder_name
);
let bindings = bindgen::Builder::default()
.header("wrapper.h")
.clang_arg("-v")
.clang_arg("-Isolclient-7.25.0.10/include")
.clang_arg(&include_arg)
.allowlist_function("^solClient_.*")
.allowlist_var("^SOLCLIENT_.*")
.size_t_is_usize(true)
.generate_comments(false)
.generate()
.expect("Unable to generate bindings");
let out_path = PathBuf::from("src");
bindings
.write_to_file(out_path.join("solace_bindings.rs"))
.expect("Couldn't write bindings!");
}