#![allow(clippy::expect_used, clippy::unwrap_used)]
fn main() {
#[cfg(feature = "embedded")]
embedded_runtime::prepare();
}
#[cfg(feature = "embedded")]
mod embedded_runtime {
use std::path::PathBuf;
fn is_precompile_bootstrap() -> bool {
std::env::var("ERYX_PRECOMPILE_BOOTSTRAP").is_ok()
}
fn find_cached_runtime() -> Option<PathBuf> {
let version = std::env::var("CARGO_PKG_VERSION").ok()?;
let cache_dir = if let Ok(xdg) = std::env::var("XDG_CACHE_HOME") {
PathBuf::from(xdg).join("eryx")
} else {
let home = std::env::var("HOME")
.or_else(|_| std::env::var("USERPROFILE"))
.ok()?;
PathBuf::from(home).join(".cache").join("eryx")
};
let prefix = format!("runtime-v{version}");
let entries = std::fs::read_dir(&cache_dir).ok()?;
for entry in entries.flatten() {
let name = entry.file_name();
let name_str = name.to_string_lossy();
if name_str.starts_with(&prefix) && name_str.ends_with(".cwasm") {
let path = entry.path();
eprintln!("Found cached runtime: {}", path.display());
return Some(path);
}
}
None
}
pub fn prepare() {
let out_dir = PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR not set"));
println!("cargo::rerun-if-env-changed=ERYX_RUNTIME_CWASM");
println!("cargo::rerun-if-changed=../eryx-runtime/runtime.cwasm");
let cwasm_path = std::env::var("ERYX_RUNTIME_CWASM")
.ok()
.map(PathBuf::from)
.filter(|p| p.exists());
let cwasm_path = cwasm_path.or_else(find_cached_runtime);
let cwasm_path = cwasm_path.or_else(|| {
Some(PathBuf::from("../eryx-runtime/runtime.cwasm")).filter(|p| p.exists())
});
match cwasm_path {
Some(path) => {
let dest = out_dir.join("runtime.cwasm");
std::fs::copy(&path, &dest).expect("Failed to copy runtime.cwasm");
}
None => {
if is_precompile_bootstrap() {
let dest = out_dir.join("runtime.cwasm");
std::fs::write(&dest, b"").expect("Failed to write placeholder runtime.cwasm");
return;
}
panic!(
"\n\
Pre-compiled runtime (runtime.cwasm) not found.\n\
\n\
The `embedded` feature requires a pre-compiled WASM runtime for your platform.\n\
\n\
Option 1 — Use eryx-precompile (recommended for crates.io users):\n\
\n\
$ cargo binstall eryx-precompile\n\
$ eryx-precompile setup\n\
\n\
Option 2 — Set an explicit path:\n\
\n\
$ export ERYX_RUNTIME_CWASM=/path/to/runtime.cwasm\n\
\n\
Option 3 — Build from the workspace (for eryx developers):\n\
\n\
$ mise run precompile-eryx-runtime\n\
"
);
}
}
}
}