trifid/nebula-ffi/build.rs

42 lines
1.2 KiB
Rust

use std::env;
use std::path::PathBuf;
use bindgen::CargoCallbacks;
use gobuild::BuildMode;
use std::path::Path;
fn main() {
// Find compiler: if /usr/local/go/bin/go exists, use that
// otherwise, rely on PATH
let compiler = if Path::new("/usr/local/go/bin/go").exists() {
println!("Using /usr/local/go/bin/go");
"/usr/local/go/bin/go"
} else {
"go"
};
gobuild::Build::new().compiler(compiler).buildmode(BuildMode::CArchive).file("main.go").compile("nebulaffi");
println!("Go compile success");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
println!("cargo:rustc-link-search={}", out_path.display());
println!("cargo:rustc-link-lib=dylib=nebulaffi");
println!("cargo:rerun-if-changed=go.mod");
println!("cargo:rerun-if-changed=go.sum");
println!("cargo:rerun-if-changed=main.go");
println!("Generating bindings");
let bindings = bindgen::Builder::default()
.header(out_path.join("libnebulaffi.h").display().to_string())
.parse_callbacks(Box::new(CargoCallbacks))
.generate()
.expect("Error generating CFFI bindings");
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}