use std::{env, path::PathBuf, process::Command};
fn ensure_rust_ar() {
if Command::new("rust-ar").arg("--version").output().is_ok() {
return;
}
let _ = Command::new("cargo").args(["install", "cargo-binutils"]).status();
if Command::new("rust-ar").arg("--version").output().is_ok() {
return;
}
panic!(
"`rust-ar` was not found. Ensure the `llvm-tools` component is present (listed in \
rust-toolchain.toml) and try installing proxies via `cargo install cargo-binutils`."
);
}
fn main() {
let target = env::var("TARGET").unwrap_or_else(|_| "wasm32-wasip1".to_string());
if !target.starts_with("wasm32") {
println!("cargo:rerun-if-changed=stubs/heap_base.rs");
return;
}
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
println!("cargo:rerun-if-env-changed=TARGET");
println!("cargo:rerun-if-env-changed=RUSTUP_TOOLCHAIN");
println!("cargo:rerun-if-env-changed=RUSTFLAGS");
println!("cargo:rerun-if-changed={}", manifest_dir.join("stubs/heap_base.rs").display());
let out_obj = out_dir.join("miden_alloc_heap_base.o");
let out_a = out_dir.join("libmiden_alloc_intrinsics.a");
ensure_rust_ar();
let status = Command::new("rustc")
.arg("--crate-name")
.arg("miden_alloc_heap_base_stub")
.arg("--edition=2021")
.arg("--crate-type=rlib")
.arg("--target")
.arg(&target)
.arg("-C")
.arg("opt-level=1")
.arg("-C")
.arg("panic=abort")
.arg("-C")
.arg("codegen-units=1")
.arg("-C")
.arg("debuginfo=0")
.arg("-Z")
.arg("merge-functions=disabled")
.arg("-C")
.arg("target-feature=+bulk-memory,+wide-arithmetic")
.arg("--emit=obj")
.arg("-o")
.arg(&out_obj)
.arg(manifest_dir.join("stubs/heap_base.rs"))
.status()
.expect("failed to spawn rustc for heap_base stub object");
if !status.success() {
panic!("failed to compile heap_base stub object: {status}");
}
let status = Command::new("rust-ar")
.arg("crs")
.arg(&out_a)
.arg(&out_obj)
.status()
.expect("failed to spawn ar for alloc stubs");
if !status.success() {
panic!("failed to archive alloc stubs: {status}");
}
println!("cargo:rustc-link-search=native={}", out_dir.display());
println!("cargo:rustc-link-lib=static=miden_alloc_intrinsics");
}