2024-03-05 14:00:04 -08:00
|
|
|
use std::env;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2024-03-06 13:03:40 -08:00
|
|
|
fn main() {
|
2024-03-05 14:00:04 -08:00
|
|
|
let callee_root = env::var("CALLEE_PATH").expect("Callee path variable must be available");
|
|
|
|
let lean_root = env::var("LEAN_ROOT").expect("Lean root must exist");
|
2024-03-06 13:03:40 -08:00
|
|
|
let output_path = PathBuf::from(env::var("OUT_DIR").unwrap());
|
2024-03-05 15:57:48 -08:00
|
|
|
let input = format!("{lean_root}/include/lean/lean.h");
|
2024-03-05 14:00:04 -08:00
|
|
|
println!("cargo:rustc-link-search={callee_root}");
|
|
|
|
println!("cargo:rustc-link-search={lean_root}/lib/lean");
|
|
|
|
|
|
|
|
let bindings = bindgen::Builder::default()
|
2024-03-05 15:57:48 -08:00
|
|
|
.header(&input)
|
2024-03-05 14:00:04 -08:00
|
|
|
// Tell cargo to invalidate the built crate whenever any of the
|
|
|
|
// included header files changed.
|
|
|
|
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
|
2024-03-05 15:57:48 -08:00
|
|
|
// The following is specific to `lean.h`: There are a lot of static
|
|
|
|
// function
|
|
|
|
.wrap_static_fns(true)
|
|
|
|
// but some functions use `_Atomic(int)` types that bindgen cannot handle,
|
|
|
|
// so they are blocklisted. The types which use `_Atomic` are treated as
|
|
|
|
// opaque.
|
|
|
|
.blocklist_function("lean_get_rc_mt_addr")
|
|
|
|
.opaque_type("lean_thunk_object")
|
|
|
|
.opaque_type("lean_task_object")
|
2024-03-05 14:00:04 -08:00
|
|
|
.generate()
|
|
|
|
.expect("Unable to generate bindings");
|
|
|
|
|
|
|
|
bindings
|
2024-03-06 13:03:40 -08:00
|
|
|
.write_to_file(output_path.join("lean.rs"))
|
2024-03-05 14:00:04 -08:00
|
|
|
.expect("Couldn't write bindings!");
|
2024-03-05 15:57:48 -08:00
|
|
|
|
2024-03-06 13:03:40 -08:00
|
|
|
let extern_src_path = env::temp_dir().join("bindgen").join("extern.c");
|
|
|
|
let extern_obj_path = env::temp_dir().join("bindgen").join("extern.o");
|
|
|
|
|
2024-03-05 15:57:48 -08:00
|
|
|
// This is the path to the static library file.
|
|
|
|
let lib_path = output_path.join("libextern.a");
|
|
|
|
|
|
|
|
// Compile the generated wrappers into an object file.
|
|
|
|
let clang_output = std::process::Command::new("clang")
|
|
|
|
.arg("-O")
|
|
|
|
.arg("-c")
|
|
|
|
.arg("-o")
|
2024-03-05 16:39:33 -08:00
|
|
|
.arg(&extern_obj_path)
|
|
|
|
.arg(&extern_src_path)
|
2024-03-05 15:57:48 -08:00
|
|
|
.arg("-include")
|
|
|
|
.arg(&input)
|
2024-03-06 13:03:40 -08:00
|
|
|
.arg("-I")
|
|
|
|
.arg(format!("{lean_root}/include"))
|
2024-03-05 15:57:48 -08:00
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
if !clang_output.status.success() {
|
|
|
|
panic!(
|
|
|
|
"Could not compile object file:\n{}",
|
|
|
|
String::from_utf8_lossy(&clang_output.stderr)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn the object file into a static library
|
|
|
|
#[cfg(not(target_os = "windows"))]
|
|
|
|
let lib_output = std::process::Command::new("ar")
|
|
|
|
.arg("rcs")
|
|
|
|
.arg(output_path.join(&lib_path))
|
2024-03-05 16:39:33 -08:00
|
|
|
.arg(&extern_obj_path)
|
2024-03-05 15:57:48 -08:00
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
let lib_output = std::process::Command::new("LIB")
|
|
|
|
.arg(obj_path)
|
|
|
|
.arg(format!(
|
|
|
|
"/OUT:{}",
|
2024-03-06 13:03:40 -08:00
|
|
|
output_path.join("libextern.lib").display()
|
2024-03-05 15:57:48 -08:00
|
|
|
))
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
if !lib_output.status.success() {
|
|
|
|
panic!(
|
|
|
|
"Could not emit library file:\n{}",
|
|
|
|
String::from_utf8_lossy(&lib_output.stderr)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Statically link against the generated lib
|
|
|
|
println!("cargo:rustc-link-search={}", output_path.display());
|
|
|
|
println!("cargo:rustc-link-lib=static=extern");
|
2024-03-05 14:00:04 -08:00
|
|
|
}
|