mirror of
https://github.com/logos-messaging/logos-delivery-rust-bindings.git
synced 2026-07-30 06:53:29 +00:00
The shim existed to satisfy Nim's cmdCount/cmdLine globals, which the static library referenced when it pulled in the command-line machinery. Nothing in liblogosdelivery.a (nor any archive on the link line) references either symbol now, and a clean rebuild of the workspace and the basic example links without it. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
200 lines
7.2 KiB
Rust
200 lines
7.2 KiB
Rust
use std::env;
|
|
use std::env::set_current_dir;
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::Command;
|
|
|
|
extern crate cc;
|
|
|
|
fn submodules_init(vendor_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 the vendor folder, run 'make update' to get its vendors.
|
|
set_current_dir(vendor_dir).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 vendor folder.");
|
|
}
|
|
|
|
set_current_dir(env::var("CARGO_MANIFEST_DIR").unwrap())
|
|
.expect("Going back to manifest 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.");
|
|
}
|
|
}
|
|
|
|
fn build_nwaku_lib(vendor_dir: &Path) {
|
|
set_current_dir(vendor_dir).expect("Moving to vendor dir");
|
|
|
|
let mut cmd = Command::new("make");
|
|
cmd.arg("liblogosdelivery").arg("STATIC=1");
|
|
let status = cmd
|
|
.status()
|
|
.unwrap_or_else(|e| panic!("Failed to run 'make liblogosdelivery': {e}"));
|
|
|
|
// The exit status has to be checked, not just the spawn: a failed make would
|
|
// otherwise pass silently and the crate would link a stale library from a
|
|
// previous build.
|
|
if !status.success() {
|
|
panic!("'make liblogosdelivery STATIC=1' failed with {status}");
|
|
}
|
|
|
|
// The generated ffi.rs declares `#[link(name = "logosdelivery")]` with no
|
|
// kind, so a stray liblogosdelivery.dylib in build/ (left by a non-STATIC
|
|
// `make liblogosdelivery`) would be linked dynamically over the static .a we
|
|
// want. Remove it so the static archive is the only candidate.
|
|
for stale in ["build/liblogosdelivery.dylib", "build/liblogosdelivery.dylib.dSYM"] {
|
|
let path = vendor_dir.join(stale);
|
|
if path.exists() {
|
|
let _ = std::fs::remove_dir_all(&path).or_else(|_| std::fs::remove_file(&path));
|
|
}
|
|
}
|
|
|
|
set_current_dir(env::var("CARGO_MANIFEST_DIR").unwrap())
|
|
.expect("Going back to manifest dir");
|
|
}
|
|
|
|
/// Resolves a package directory under the vendor's `nimbledeps/pkgs2`, whose
|
|
/// names carry a version and hash (e.g. `nat_traversal-0.0.1-1a376d3e...`) that
|
|
/// change whenever `nimble.lock` moves.
|
|
fn nimble_pkg_dir(nwaku_path: &Path, pkg_name: &str) -> PathBuf {
|
|
let pkgs_dir = nwaku_path.join("nimbledeps/pkgs2");
|
|
let prefix = format!("{pkg_name}-");
|
|
std::fs::read_dir(&pkgs_dir)
|
|
.unwrap_or_else(|e| panic!("Cannot read {}: {e}", pkgs_dir.display()))
|
|
.filter_map(|entry| entry.ok())
|
|
.map(|entry| entry.path())
|
|
.find(|path| {
|
|
path.file_name()
|
|
.and_then(|name| name.to_str())
|
|
.is_some_and(|name| name.starts_with(&prefix))
|
|
})
|
|
.unwrap_or_else(|| {
|
|
panic!(
|
|
"No '{pkg_name}' package under {}. Was 'make liblogosdelivery' run?",
|
|
pkgs_dir.display()
|
|
)
|
|
})
|
|
}
|
|
|
|
/// Finds the vendor's `librln_<version>.a` and returns the name to link it by
|
|
/// (the file stem without the `lib` prefix).
|
|
fn find_librln(nwaku_path: &Path) -> String {
|
|
std::fs::read_dir(nwaku_path)
|
|
.unwrap_or_else(|e| panic!("Cannot read {}: {e}", nwaku_path.display()))
|
|
.filter_map(|entry| entry.ok())
|
|
.filter_map(|entry| entry.file_name().into_string().ok())
|
|
.find_map(|name| {
|
|
name.strip_prefix("lib")
|
|
.and_then(|name| name.strip_suffix(".a"))
|
|
.filter(|name| name.starts_with("rln"))
|
|
.map(str::to_owned)
|
|
})
|
|
.unwrap_or_else(|| {
|
|
panic!(
|
|
"No 'librln_*.a' in {}. Was 'make librln' run?",
|
|
nwaku_path.display()
|
|
)
|
|
})
|
|
}
|
|
|
|
fn emit_link_flags(nwaku_path: &Path) {
|
|
// The FFI surface is generated by the vendor's genBindings() rather than
|
|
// bindgen, so the sources it emits are what invalidates this crate.
|
|
println!(
|
|
"cargo:rerun-if-changed={}",
|
|
nwaku_path.join("library/rust_bindings/src").display()
|
|
);
|
|
// The Nim sources the library is built from have to invalidate it too, or
|
|
// editing them leaves the linked archive stale and the tests silently run
|
|
// against the previous build.
|
|
println!(
|
|
"cargo:rerun-if-changed={}",
|
|
nwaku_path.join("library").display()
|
|
);
|
|
println!(
|
|
"cargo:rerun-if-changed={}",
|
|
nwaku_path.join("logos_delivery").display()
|
|
);
|
|
println!(
|
|
"cargo:rustc-link-search={}",
|
|
nwaku_path.join("build").display()
|
|
);
|
|
println!("cargo:rustc-link-lib=static=logosdelivery");
|
|
|
|
// nat_traversal moved from a git submodule under vendor/ to a nimble
|
|
// dependency, and builds its NAT archives inside its own package dir.
|
|
let nat_traversal = nimble_pkg_dir(nwaku_path, "nat_traversal");
|
|
|
|
println!(
|
|
"cargo:rustc-link-search={}",
|
|
nat_traversal
|
|
.join("vendor/miniupnp/miniupnpc/build")
|
|
.display()
|
|
);
|
|
println!("cargo:rustc-link-lib=static=miniupnpc");
|
|
|
|
println!(
|
|
"cargo:rustc-link-search={}",
|
|
nat_traversal.join("vendor/libnatpmp-upstream").display()
|
|
);
|
|
println!("cargo:rustc-link-lib=static=natpmp");
|
|
|
|
println!("cargo:rustc-link-lib=dl");
|
|
println!("cargo:rustc-link-lib=m");
|
|
|
|
// boringssl (pulled in by libp2p) is C++, so its runtime has to be linked.
|
|
if cfg!(target_os = "macos") {
|
|
println!("cargo:rustc-link-lib=c++");
|
|
} else {
|
|
println!("cargo:rustc-link-lib=stdc++");
|
|
}
|
|
|
|
// The vendor's Makefile fetches librln into its root, naming it after the
|
|
// RLN version it pins, so the archive is discovered rather than hardcoded.
|
|
let librln = find_librln(nwaku_path);
|
|
println!("cargo:rustc-link-search={}", nwaku_path.display());
|
|
println!("cargo:rustc-link-lib=static={librln}");
|
|
|
|
// libbacktrace is not linked: the vendor builds with -d:disable_libbacktrace.
|
|
}
|
|
|
|
#[cfg(not(doc))]
|
|
fn main() {
|
|
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
|
|
// The vendor submodule lives at the repo root, one level up from this crate.
|
|
let vendor_dir = manifest_dir
|
|
.parent()
|
|
.expect("crate has a parent directory")
|
|
.join("vendor");
|
|
|
|
submodules_init(&vendor_dir);
|
|
build_nwaku_lib(&vendor_dir);
|
|
emit_link_flags(&vendor_dir);
|
|
}
|