use cmake::Config;
use std::{env, fs, path::PathBuf};
fn copy_dir(src: &PathBuf, dst: &PathBuf) {
fs::create_dir_all(dst).unwrap();
for entry in fs::read_dir(src).unwrap() {
let entry = entry.unwrap();
let dst_path = dst.join(entry.file_name());
if entry.file_type().unwrap().is_dir() {
copy_dir(&entry.path(), &dst_path);
} else {
fs::copy(entry.path(), &dst_path).unwrap();
}
}
}
fn main() {
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let epanet_out = out_path.join("EPANET");
copy_dir(&PathBuf::from("EPANET"), &epanet_out);
let dynamic_link = cfg!(feature = "dynamic-link");
let mut cmake_cfg = Config::new(&epanet_out);
cmake_cfg.define("CMAKE_BUILD_TYPE", "Release");
if !dynamic_link {
cmake_cfg.define("BUILD_SHARED_LIBS", "OFF");
if cfg!(target_os = "windows") {
cmake_cfg.cflag("/DDLLEXPORT=");
} else {
cmake_cfg.cflag("-DDLLEXPORT=");
}
}
if cfg!(target_family = "unix") && dynamic_link {
cmake_cfg.define("CMAKE_C_STANDARD_LIBRARIES", "-lm");
}
let dst = cmake_cfg.build();
println!("cargo:rustc-link-search=native={}/lib", dst.display());
println!("cargo:rustc-link-search=native={}/lib64", dst.display());
println!("cargo:rustc-link-search=native={}", dst.display());
if dynamic_link {
println!("cargo:rustc-link-lib=dylib=epanet2");
} else {
println!("cargo:rustc-link-lib=static=epanet2");
}
if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
println!("cargo:rustc-link-lib=dylib=m");
}
let mut builder = bindgen::Builder::default()
.header("wrapper.h")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()));
if !dynamic_link {
builder = builder.clang_arg("-DDLLEXPORT=");
}
let bindings = builder
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}