use std::{env, fs, path::PathBuf};
fn main() {
const LIB_NAME: &str = "timsdata";
let lib_so_name = format!("lib{}.so", LIB_NAME);
let crate_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let lib_path = if let Ok(override_dir) = env::var("TIMSDATA_LIB_DIR") {
PathBuf::from(override_dir)
} else {
crate_dir.join("lib")
};
println!("cargo:rustc-link-search=native={}", lib_path.display());
println!("cargo:rustc-link-lib=dylib={}", LIB_NAME);
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN");
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let target_dir = out_dir
.ancestors()
.nth(3)
.expect("OUT_DIR does not have enough ancestors")
.to_path_buf();
let source = lib_path.join(&lib_so_name);
let destination = target_dir.join(&lib_so_name);
fs::create_dir_all(&target_dir).unwrap();
fs::copy(&source, &destination).unwrap_or_else(|e| {
panic!("Failed to copy {:?} to {:?}: {}", source, destination, e)
});
println!("cargo:warning=Target dir: {}", target_dir.display());
println!("cargo:warning=Source dir: {}", source.display());
println!("cargo:rerun-if-changed={}", source.display());
println!("cargo:rerun-if-env-changed=TIMSDATA_LIB_DIR");
}