44 lines
1.3 KiB
Rust
44 lines
1.3 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");
|
|
compiler = "/usr/local/go/bin/go";
|
|
} else {
|
|
compiler = "go";
|
|
}
|
|
|
|
gobuild::Build::new().compiler(compiler).file("main.go").compile("nebulaffi");
|
|
gobuild::Build::new().buildmode(BuildMode::CShared).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!");
|
|
}
|