use std::path::Path;
const PLACEHOLDER_MARKER: &str = "<!-- pitchfork-web-ui-placeholder -->";
fn main() {
println!("cargo:rerun-if-changed=ui/dist");
ensure_web_ui();
link_without_pie();
}
fn ensure_web_ui() {
let dist = Path::new("ui/dist");
let index = dist.join("index.html");
let placeholder = std::fs::read_to_string(&index)
.map(|html| html.contains(PLACEHOLDER_MARKER))
.unwrap_or(false);
if index.is_file() && !placeholder {
return;
}
if std::env::var("PROFILE").as_deref() == Ok("release") {
panic!(
"the web UI has not been built: ui/dist/index.html is {}. \
Run `mise run build:ui` first; release builds embed it.",
if placeholder {
"a placeholder"
} else {
"missing"
}
);
}
if !placeholder {
let html = format!(
"<!doctype html><html><head><meta charset=\"utf-8\"><title>pitchfork</title></head>\
<body>{PLACEHOLDER_MARKER}<p>This pitchfork build has no web UI. \
Run <code>mise run build:ui</code> and rebuild to include it.</p></body></html>\n"
);
std::fs::create_dir_all(dist)
.and_then(|()| std::fs::write(&index, html))
.expect("failed to write the placeholder ui/dist/index.html");
}
if let Err(err) = std::fs::File::options()
.write(true)
.open(&index)
.and_then(|file| file.set_modified(std::time::UNIX_EPOCH))
{
println!(
"cargo:warning=could not back-date the placeholder ui/dist/index.html ({err}); \
delete ui/dist before running `mise run build:ui`"
);
}
println!(
"cargo:warning=ui/dist has no built web UI; embedding a placeholder page. \
Run `mise run build:ui` to include the real one."
);
}
fn link_without_pie() {
println!("cargo:rerun-if-env-changed=PITCHFORK_NO_PIE");
let linux_gnu = std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("linux")
&& std::env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("gnu");
if linux_gnu && std::env::var("PITCHFORK_NO_PIE").as_deref() == Ok("1") {
println!("cargo:rustc-link-arg-bins=-no-pie");
}
}