extern crate napi_build;
use std::{env, path::PathBuf, process::Command};
fn main() {
if env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("wasi") {
setup_wasi_rustc();
}
napi_build::setup();
}
fn setup_wasi_rustc() {
let rustc = env::var("RUSTC").expect("RUSTC must be set by Cargo");
let target = env::var("TARGET").expect("TARGET must be set by Cargo");
let output = Command::new(&rustc)
.args(["--print", "sysroot"])
.output()
.unwrap_or_else(|err| panic!("Could not run `{rustc} --print sysroot`: {err}"));
if !output.status.success() {
panic!(
"`{rustc} --print sysroot` failed: {}",
String::from_utf8_lossy(&output.stderr)
);
}
let sysroot = PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
let crt_reactor_path = sysroot
.join("lib")
.join("rustlib")
.join(&target)
.join("lib")
.join("self-contained")
.join("crt1-reactor.o");
if !crt_reactor_path.exists() {
panic!(
"crt1-reactor.o not found at {}. Without it, the wasm module hangs when it loads. Run `rustup target add {target}`.",
crt_reactor_path.display()
);
}
env::set_var("RUSTC", sysroot.join("bin").join("rustc"));
}