use std::{fs, io::Write as _, path::PathBuf};
static SWIFT_RUNTIME: &str = "SWIFT_RUNTIME";
fn main() {
let swift_runtime = match std::env::var(SWIFT_RUNTIME) {
Ok(val) => val,
Err(_) => {
println!("Environment variable {} not found", SWIFT_RUNTIME);
"/usr/lib/swift".to_string()
}
};
println!("cargo:rerun-if-env-changed={}", SWIFT_RUNTIME);
println!("cargo:rustc-link-search=native={}", swift_runtime);
println!("cargo:rustc-link-lib=dylib=swiftCore");
}
fn build() {
let librs_path = PathBuf::from("src").join("lib.rs");
if librs_path.exists() {
fs::remove_file(&librs_path).unwrap();
}
let bindings = bindgen::Builder::default()
.clang_arg("-x")
.clang_arg("c++")
.clang_arg("-std=c++14")
.header("wrapper.h")
.raw_line("#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals, improper_ctypes)]")
.blocklist_item("template")
.blocklist_item("_Pred")
.blocklist_item("_Tp")
.blocklist_type("template")
.opaque_type("_Tp")
.opaque_type("std::.+")
.enable_cxx_namespaces()
.clang_arg("-Ifake")
.clang_arg("-Iswift/include")
.clang_arg("-Iswift/stdlib/public/SwiftShims/")
.raw_line("pub type _Tp = ();")
.raw_line("pub type _Pred = ();")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");
let code = bindings.to_string().replace("extern \"swift\" {", "extern \"C\" {");
let mut file = fs::OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(librs_path).unwrap();
file.write_all(code.as_bytes()).unwrap();
}