extern crate core;
use std::{env, path::PathBuf, process::Command};
fn main() {
println!("cargo:rerun-if-changed=build.rs");
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let prebuilt_lib_dir = manifest_dir.join("libntrntesttube").join("artifacts");
let lib_name = "ntrntesttube";
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let header = if std::env::var("DOCS_RS").is_ok() {
manifest_dir
.join("libntrntesttube")
.join("artifacts")
.join("libntrntesttube.docrs.h")
} else {
out_dir.join(format!("lib{}.h", lib_name))
};
println!("cargo:rerun-if-changed=./libntrntesttube");
let lib_filename = if cfg!(target_os = "macos") {
format!("lib{}.{}", lib_name, "dylib")
} else if cfg!(target_os = "linux") {
format!("lib{}.{}", lib_name, "so")
} else if cfg!(target_os = "windows") {
format!("{}.{}", lib_name, "dll")
} else {
panic!("Unsupported architecture");
};
let lib_filename = lib_filename.as_str();
if env::var("PREBUILD_LIB") == Ok("1".to_string()) {
build_libntrntesttube(prebuilt_lib_dir.join(lib_filename));
}
let out_dir_lib_path = out_dir.join(lib_filename);
if std::fs::metadata(&out_dir_lib_path).is_err()
|| env::var("NEUTRON_TUBE_DEV") == Ok("1".to_string())
{
build_libntrntesttube(out_dir_lib_path);
}
if env::var("PROFILE").unwrap() == "debug" {
let target_dir = out_dir.join("..").join("..").join("..").join("deps");
for entry in std::fs::read_dir(out_dir.clone()).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.is_file() {
let file_name = path.file_name().unwrap().to_str().unwrap();
if file_name.starts_with("libntrntesttube") {
let target_path = target_dir.join(file_name);
std::fs::copy(path, target_path).unwrap();
}
}
}
}
println!(
"cargo:rustc-link-search=native={}",
out_dir.to_str().unwrap()
);
if std::env::var("DOCS_RS").is_err() {
println!("cargo:rustc-link-lib=dylib={}", lib_name);
}
let bindings = bindgen::Builder::default()
.header(header.to_str().unwrap())
.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!");
}
fn build_libntrntesttube(out: PathBuf) {
if std::env::var("DOCS_RS").is_ok() {
return;
}
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let tidy_status = Command::new("go")
.current_dir(manifest_dir.join("libntrntesttube"))
.arg("mod")
.arg("tidy")
.spawn()
.unwrap()
.wait()
.unwrap();
if !tidy_status.success() {
panic!("failed to run 'go mod tidy'");
}
let exit_status = Command::new("go")
.current_dir(manifest_dir.join("libntrntesttube"))
.arg("build")
.arg("-buildmode=c-shared")
.arg("-tags")
.arg("skip_ccv_msg_filter")
.arg("-ldflags")
.arg("-w")
.arg("-o")
.arg(out)
.arg("main.go")
.spawn()
.unwrap()
.wait()
.unwrap();
if !exit_status.success() {
panic!("failed to build go code");
}
}