2023-06-26 02:00:36 +00:00
|
|
|
use std::env;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use bindgen::CargoCallbacks;
|
2023-07-12 14:32:19 +00:00
|
|
|
use gobuild::BuildMode;
|
2023-06-27 03:17:49 +00:00
|
|
|
use std::path::Path;
|
2023-06-26 02:00:36 +00:00
|
|
|
|
|
|
|
fn main() {
|
2023-06-27 03:17:49 +00:00
|
|
|
// 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");
|
2023-07-12 14:32:19 +00:00
|
|
|
gobuild::Build::new().buildmode(BuildMode::CShared).file("main.go").compile("nebulaffi");
|
|
|
|
|
|
|
|
println!("Go compile success");
|
2023-06-26 02:00:36 +00:00
|
|
|
|
|
|
|
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
|
|
|
|
|
|
|
|
println!("cargo:rustc-link-search={}", out_path.display());
|
2023-07-12 14:32:19 +00:00
|
|
|
println!("cargo:rustc-link-lib=dylib=nebulaffi");
|
2023-06-26 02:00:36 +00:00
|
|
|
println!("cargo:rerun-if-changed=go.mod");
|
|
|
|
println!("cargo:rerun-if-changed=go.sum");
|
|
|
|
println!("cargo:rerun-if-changed=main.go");
|
|
|
|
|
2023-07-12 14:32:19 +00:00
|
|
|
println!("Generating bindings");
|
|
|
|
|
2023-06-26 02:00:36 +00:00
|
|
|
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!");
|
2023-07-12 14:32:19 +00:00
|
|
|
}
|