2022-09-22 13:13:16 +00:00
|
|
|
use std::env;
|
|
|
|
use std::env::set_current_dir;
|
2022-09-28 13:45:26 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2022-09-22 13:13:16 +00:00
|
|
|
use std::process::Command;
|
|
|
|
|
2022-09-28 13:45:26 +00:00
|
|
|
fn get_go_bin() -> String {
|
|
|
|
if cfg!(target_family = "unix") {
|
|
|
|
let output = String::from_utf8(
|
|
|
|
Command::new("/usr/bin/which")
|
|
|
|
.arg("go")
|
|
|
|
.output()
|
|
|
|
.map_err(|e| println!("cargo:warning=Couldn't find `which` command: {}", e))
|
|
|
|
.expect("`which` command not found")
|
|
|
|
.stdout,
|
|
|
|
)
|
|
|
|
.expect("which output couldnt be parsed");
|
|
|
|
|
|
|
|
if output.is_empty() {
|
|
|
|
println!("cargo:warning=Couldn't find go binary installed, please ensure that it is installed and/or withing the system paths");
|
|
|
|
panic!("Couldn't find `go` binary installed");
|
|
|
|
}
|
|
|
|
output.trim().to_string()
|
|
|
|
} else if cfg!(target_family = "windows") {
|
|
|
|
"go".into()
|
|
|
|
} else {
|
|
|
|
panic!("OS not supported!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_go_waku_lib(go_bin: &str, project_dir: &Path) {
|
2022-09-23 06:47:28 +00:00
|
|
|
// Build go-waku static lib
|
|
|
|
// build command taken from waku make file:
|
|
|
|
// https://github.com/status-im/go-waku/blob/eafbc4c01f94f3096c3201fb1e44f17f907b3068/Makefile#L115
|
2022-11-28 16:57:20 +00:00
|
|
|
let out_dir: PathBuf = env::var_os("OUT_DIR").unwrap().into();
|
2022-09-28 13:45:26 +00:00
|
|
|
let vendor_path = project_dir.join("vendor");
|
|
|
|
set_current_dir(vendor_path).expect("Moving to vendor dir");
|
2022-11-30 08:45:22 +00:00
|
|
|
|
|
|
|
let mut cmd = Command::new(go_bin);
|
|
|
|
cmd.env("CGO_ENABLED", "1")
|
2022-09-22 13:13:16 +00:00
|
|
|
.arg("build")
|
|
|
|
.arg("-buildmode=c-archive")
|
|
|
|
.arg("-o")
|
2022-11-28 16:57:20 +00:00
|
|
|
.arg(out_dir.join("libgowaku.a"))
|
2022-11-30 08:45:22 +00:00
|
|
|
.arg("./library");
|
|
|
|
|
|
|
|
// Setting `GOCACHE=/tmp/` for crates.io job that builds documentation
|
|
|
|
// when a crate is being published or updated.
|
|
|
|
if std::env::var("DOCS_RS").is_ok() {
|
|
|
|
cmd.env("GOCACHE", "/tmp/");
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.status()
|
2022-09-28 13:45:26 +00:00
|
|
|
.map_err(|e| println!("cargo:warning=go build failed due to: {}", e))
|
2022-09-22 13:13:16 +00:00
|
|
|
.unwrap();
|
2022-11-30 08:45:22 +00:00
|
|
|
|
2022-09-28 13:45:26 +00:00
|
|
|
set_current_dir(project_dir).expect("Going back to project dir");
|
|
|
|
}
|
|
|
|
|
2022-11-28 16:57:20 +00:00
|
|
|
fn generate_bindgen_code() {
|
|
|
|
let lib_dir: PathBuf = env::var_os("OUT_DIR").unwrap().into();
|
|
|
|
|
|
|
|
// let lib_dir = project_dir.join("vendor/build/lib");
|
2022-09-22 13:13:16 +00:00
|
|
|
|
|
|
|
println!("cargo:rustc-link-search={}", lib_dir.display());
|
|
|
|
println!("cargo:rustc-link-lib=static=gowaku");
|
|
|
|
println!("cargo:rerun-if-changed=libgowaku.h");
|
2022-09-23 06:47:28 +00:00
|
|
|
|
|
|
|
// Generate waku bindings with bindgen
|
2022-09-22 13:13:16 +00:00
|
|
|
let bindings = bindgen::Builder::default()
|
|
|
|
// The input header we would like to generate
|
|
|
|
// bindings for.
|
|
|
|
.header(format!("{}/{}", lib_dir.display(), "libgowaku.h"))
|
|
|
|
// Tell cargo to invalidate the built crate whenever any of the
|
|
|
|
// included header files changed.
|
|
|
|
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
|
|
|
|
// Finish the builder and generate the bindings.
|
|
|
|
.generate()
|
|
|
|
// Unwrap the Result and panic on failure.
|
|
|
|
.expect("Unable to generate bindings");
|
|
|
|
|
|
|
|
// Write the bindings to the $OUT_DIR/bindings.rs file.
|
|
|
|
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
|
|
|
|
bindings
|
|
|
|
.write_to_file(out_path.join("bindings.rs"))
|
|
|
|
.expect("Couldn't write bindings!");
|
|
|
|
}
|
2022-09-28 13:45:26 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let go_bin = get_go_bin();
|
|
|
|
|
|
|
|
let project_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
|
|
|
|
|
|
|
|
build_go_waku_lib(&go_bin, &project_dir);
|
2022-11-28 16:57:20 +00:00
|
|
|
generate_bindgen_code();
|
2022-09-28 13:45:26 +00:00
|
|
|
}
|