use std::env;
use std::fs;
use std::path::PathBuf;
use cmake::Config;
fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR environment variable not set"));
let dectalk_source_path =
PathBuf::from(&env::var("CARGO_MANIFEST_DIR").unwrap()).join("dectalk");
let dectalk_path = out_dir.join("dectalk");
dircpy::copy_dir(dectalk_source_path, &dectalk_path)
.expect("Failed to copy DECTalk to OUT_DIR");
let libdir_path: PathBuf;
if env::var("DOCS_RS").is_ok() {
println!("cargo:warning=DOCS_RS detected: Skipping native build");
libdir_path = out_dir.join("include");
fs::create_dir_all(libdir_path.join("dtk")).expect("Failed to create include directory");
fs::copy(
dectalk_path.join("src/dapi/src/api/ttsapi.h"),
libdir_path.join("dtk/ttsapi.h"),
)
.expect("Failed to copy ttsapi.h header");
fs::copy(
dectalk_path.join("src/dapi/src/osf/dtmmedefs.h"),
libdir_path.join("dtk/dtmmedefs.h"),
)
.expect("Failed to copy dtmmedefs.h header");
} else {
let dst = Config::new(&dectalk_path)
.cxxflag("-DCMAKE_INSTALL_PREFIX=$OUT_DIR")
.build();
println!("cargo:rustc-link-search=native={}/lib", dst.display());
println!("cargo:rustc-link-lib=dylib=dectalk");
libdir_path = dst
.join("include")
.canonicalize()
.expect("Can not canonicalize path");
}
let libdir_str = libdir_path.to_str().expect("Path is not a valid string");
let bindings = bindgen::Builder::default()
.header("wrapper.h")
.clang_arg(format!("-I{libdir_str}"))
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(out_dir.join("bindings.rs"))
.expect("Couldn't write bindings!");
}