2022-09-22 15:13:16 +02:00
|
|
|
use std::env;
|
|
|
|
|
use std::env::set_current_dir;
|
2022-09-28 15:45:26 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2022-09-22 15:13:16 +02:00
|
|
|
use std::process::Command;
|
|
|
|
|
|
2024-02-13 14:50:00 -04:00
|
|
|
extern crate cc;
|
|
|
|
|
|
2025-02-12 10:06:16 +01:00
|
|
|
fn submodules_init(project_dir: &Path) {
|
|
|
|
|
let mark_file_path = ".submodules-initialized";
|
|
|
|
|
|
|
|
|
|
// Check if the mark file exists
|
|
|
|
|
if !Path::new(mark_file_path).exists() {
|
|
|
|
|
// If mark file doesn't exist, initialize submodule
|
|
|
|
|
if Command::new("git")
|
|
|
|
|
.args(["submodule", "init"])
|
|
|
|
|
.status()
|
|
|
|
|
.expect("Failed to execute 'git submodule init'")
|
|
|
|
|
.success()
|
|
|
|
|
&& Command::new("git")
|
|
|
|
|
.args(["submodule", "update", "--recursive"])
|
|
|
|
|
.status()
|
|
|
|
|
.expect("Failed to execute 'git submodule update --recursive'")
|
|
|
|
|
.success()
|
|
|
|
|
{
|
|
|
|
|
// Now, inside nwaku folder, run 'make update' to get nwaku's vendors
|
|
|
|
|
let nwaku_path = project_dir.join("vendor");
|
|
|
|
|
set_current_dir(nwaku_path).expect("Moving to vendor dir");
|
|
|
|
|
|
|
|
|
|
if Command::new("make")
|
|
|
|
|
.args(["update"])
|
|
|
|
|
.status()
|
|
|
|
|
.expect("Failed to execute 'make update'")
|
|
|
|
|
.success()
|
|
|
|
|
{
|
|
|
|
|
std::fs::File::create(mark_file_path).expect("Failed to create mark file");
|
|
|
|
|
} else {
|
|
|
|
|
panic!("Failed to run 'make update' within nwaku folder.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set_current_dir(project_dir).expect("Going back to project dir");
|
|
|
|
|
|
|
|
|
|
println!("Git submodules initialized and updated successfully.");
|
|
|
|
|
} else {
|
|
|
|
|
panic!("Failed to initialize or update git submodules.");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
println!("Mark file '{mark_file_path}' exists. Skipping git submodule initialization.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-30 10:45:22 +02:00
|
|
|
|
2025-02-12 10:06:16 +01:00
|
|
|
fn build_nwaku_lib(project_dir: &Path) {
|
|
|
|
|
let nwaku_path = project_dir.join("vendor");
|
|
|
|
|
set_current_dir(nwaku_path).expect("Moving to vendor dir");
|
2022-11-30 10:45:22 +02:00
|
|
|
|
2024-02-08 11:47:02 -04:00
|
|
|
let mut cmd = Command::new("make");
|
2025-02-12 10:06:16 +01:00
|
|
|
cmd.arg("libwaku").arg("STATIC=1");
|
2022-11-30 10:45:22 +02:00
|
|
|
cmd.status()
|
2024-02-08 11:47:02 -04:00
|
|
|
.map_err(|e| println!("cargo:warning=make build failed due to: {e}"))
|
2022-09-22 15:13:16 +02:00
|
|
|
.unwrap();
|
2022-11-30 10:45:22 +02:00
|
|
|
|
2022-09-28 15:45:26 +02:00
|
|
|
set_current_dir(project_dir).expect("Going back to project dir");
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-08 11:47:02 -04:00
|
|
|
fn generate_bindgen_code(project_dir: &Path) {
|
2025-02-12 10:06:16 +01:00
|
|
|
let nwaku_path = project_dir.join("vendor");
|
|
|
|
|
let header_path = nwaku_path.join("library/libwaku.h");
|
2022-09-23 08:47:28 +02:00
|
|
|
|
2024-02-13 14:50:00 -04:00
|
|
|
cc::Build::new()
|
2024-02-26 11:13:30 -04:00
|
|
|
.object(
|
2025-02-12 10:06:16 +01:00
|
|
|
nwaku_path
|
2024-02-26 11:13:30 -04:00
|
|
|
.join("vendor/nim-libbacktrace/libbacktrace_wrapper.o")
|
|
|
|
|
.display()
|
|
|
|
|
.to_string(),
|
|
|
|
|
)
|
|
|
|
|
.compile("libbacktrace_wrapper");
|
2024-02-13 14:50:00 -04:00
|
|
|
|
2024-02-08 11:47:02 -04:00
|
|
|
println!("cargo:rerun-if-changed={}", header_path.display());
|
2024-02-26 11:13:30 -04:00
|
|
|
println!(
|
|
|
|
|
"cargo:rustc-link-search={}",
|
2025-02-12 10:06:16 +01:00
|
|
|
nwaku_path.join("build").display()
|
2024-02-26 11:13:30 -04:00
|
|
|
);
|
|
|
|
|
println!("cargo:rustc-link-lib=static=waku");
|
2024-11-28 10:35:41 +01:00
|
|
|
|
2024-02-26 11:13:30 -04:00
|
|
|
println!(
|
|
|
|
|
"cargo:rustc-link-search={}",
|
2025-02-12 10:06:16 +01:00
|
|
|
nwaku_path
|
2024-02-26 11:13:30 -04:00
|
|
|
.join("vendor/nim-nat-traversal/vendor/miniupnp/miniupnpc/build")
|
|
|
|
|
.display()
|
|
|
|
|
);
|
2024-02-13 14:50:00 -04:00
|
|
|
println!("cargo:rustc-link-lib=static=miniupnpc");
|
2024-11-28 10:35:41 +01:00
|
|
|
|
2024-02-26 11:13:30 -04:00
|
|
|
println!(
|
|
|
|
|
"cargo:rustc-link-search={}",
|
2025-02-12 10:06:16 +01:00
|
|
|
nwaku_path
|
2024-02-26 11:13:30 -04:00
|
|
|
.join("vendor/nim-nat-traversal/vendor/libnatpmp-upstream")
|
|
|
|
|
.display()
|
|
|
|
|
);
|
2024-02-13 14:50:00 -04:00
|
|
|
println!("cargo:rustc-link-lib=static=natpmp");
|
2024-11-28 10:35:41 +01:00
|
|
|
|
2024-02-13 14:50:00 -04:00
|
|
|
println!("cargo:rustc-link-lib=dl");
|
|
|
|
|
println!("cargo:rustc-link-lib=m");
|
2024-11-28 10:35:41 +01:00
|
|
|
|
2024-02-26 11:13:30 -04:00
|
|
|
println!(
|
|
|
|
|
"cargo:rustc-link-search=native={}",
|
2025-02-12 10:06:16 +01:00
|
|
|
nwaku_path
|
2024-02-26 11:13:30 -04:00
|
|
|
.join("vendor/nim-libbacktrace/install/usr/lib")
|
|
|
|
|
.display()
|
|
|
|
|
);
|
2024-02-13 14:50:00 -04:00
|
|
|
println!("cargo:rustc-link-lib=static=backtrace");
|
|
|
|
|
|
2024-11-28 10:35:41 +01:00
|
|
|
cc::Build::new()
|
|
|
|
|
.file("src/cmd.c") // Compile the C file
|
|
|
|
|
.compile("cmditems"); // Compile it as a library
|
|
|
|
|
println!("cargo:rustc-link-lib=static=cmditems");
|
|
|
|
|
|
2022-09-23 08:47:28 +02:00
|
|
|
// Generate waku bindings with bindgen
|
2022-09-22 15:13:16 +02:00
|
|
|
let bindings = bindgen::Builder::default()
|
|
|
|
|
// The input header we would like to generate
|
|
|
|
|
// bindings for.
|
2024-02-08 11:47:02 -04:00
|
|
|
.header(format!("{}", header_path.display()))
|
2022-09-22 15:13:16 +02:00
|
|
|
// 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 15:45:26 +02:00
|
|
|
|
2023-02-15 11:42:38 +01:00
|
|
|
#[cfg(not(doc))]
|
2022-09-28 15:45:26 +02:00
|
|
|
fn main() {
|
|
|
|
|
let project_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
|
|
|
|
|
|
2025-02-12 10:06:16 +01:00
|
|
|
submodules_init(&project_dir);
|
2024-02-08 11:47:02 -04:00
|
|
|
build_nwaku_lib(&project_dir);
|
|
|
|
|
generate_bindgen_code(&project_dir);
|
2022-09-28 15:45:26 +02:00
|
|
|
}
|