use std::env;
use std::fs;
use std::path::{Path, PathBuf};
const PLACEHOLDER_MARKER: &str = "__mold_placeholder";
const PLACEHOLDER_INDEX_HTML: &str = r#"<!doctype html>
<html><head><meta charset="utf-8"><title>mold</title></head>
<body>placeholder</body></html>
"#;
fn main() {
println!("cargo:rerun-if-env-changed=MOLD_WEB_DIST");
let crate_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR"));
let repo_dist_index = crate_dir
.join("..")
.join("..")
.join("web")
.join("dist")
.join("index.html");
println!("cargo:rerun-if-changed={}", repo_dist_index.display());
let resolved = resolve_web_dist(&crate_dir, &out_dir);
println!("cargo:rerun-if-changed={}", resolved.display());
println!("cargo:rustc-env=MOLD_EMBED_WEB_DIR={}", resolved.display());
}
fn resolve_web_dist(crate_dir: &Path, out_dir: &Path) -> PathBuf {
if let Ok(dist) = env::var("MOLD_WEB_DIST") {
let path = PathBuf::from(&dist);
if path.join("index.html").is_file() {
return path;
}
println!(
"cargo:warning=MOLD_WEB_DIST={} has no index.html; falling back to stub",
dist
);
}
let repo_dist = crate_dir.join("..").join("..").join("web").join("dist");
if repo_dist.join("index.html").is_file() {
return repo_dist.canonicalize().unwrap_or(repo_dist);
}
write_stub(out_dir)
}
fn write_stub(out_dir: &Path) -> PathBuf {
if env::var("PROFILE").as_deref() == Ok("release") {
println!(
"cargo:warning=mold-server: embedding placeholder web UI — web/dist is missing and \
MOLD_WEB_DIST is unset; run scripts/ensure-web-dist.sh first to embed the real SPA"
);
}
let stub = out_dir.join("web-stub");
fs::create_dir_all(&stub).expect("create web stub dir");
fs::write(stub.join("index.html"), PLACEHOLDER_INDEX_HTML).expect("write stub index.html");
fs::write(stub.join(PLACEHOLDER_MARKER), b"1").expect("write stub marker");
stub
}