#[cfg(feature = "rag")]
fn build_ort_compat() {
use std::process::Command;
if !cfg!(target_os = "linux") {
return;
}
let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR must be set by cargo");
let src = std::path::Path::new("compat/ort_compat.cpp");
if !src.exists() {
return;
}
let obj_path = std::path::Path::new(&out_dir).join("ort_compat.o");
let lib_path = std::path::Path::new(&out_dir).join("libort_compat.a");
let compiler = if cfg!(target_os = "linux") {
"g++"
} else {
"c++"
};
let compile_status = Command::new(compiler)
.args([
"-c",
"-fPIC",
"-std=c++17",
"-o",
obj_path.to_str().unwrap(),
src.to_str().unwrap(),
])
.status();
match compile_status {
Ok(s) if s.success() => {}
Ok(s) => {
eprintln!(
"cargo:warning=ort_compat.cpp compilation failed (exit {}); \
build may fail with undefined reference errors on older glibc systems",
s
);
return;
}
Err(e) => {
eprintln!(
"cargo:warning=g++ not found ({}); \
build may fail with undefined reference errors on older glibc systems",
e
);
return;
}
}
let ar_status = Command::new("ar")
.args([
"rcs",
lib_path.to_str().unwrap(),
obj_path.to_str().unwrap(),
])
.status();
if ar_status.map(|s| s.success()).unwrap_or(false) {
if cfg!(target_os = "linux") {
println!("cargo:rustc-link-arg=-Wl,--start-group");
println!("cargo:rustc-link-arg={}", lib_path.display());
println!("cargo:rustc-link-arg=-Wl,--end-group");
} else {
println!("cargo:rustc-link-search=native={}", out_dir);
println!("cargo:rustc-link-lib=static=ort_compat");
}
} else {
eprintln!("cargo:warning=failed to create ort_compat.a");
}
println!("cargo:rerun-if-changed=compat/ort_compat.cpp");
}
#[cfg(feature = "web")]
fn ensure_web_dist() {
use std::fs;
use std::path::Path;
let dist = Path::new("web/dist");
if !dist.exists() {
fs::create_dir_all(dist)
.expect("build.rs: failed to create web/dist/ placeholder directory");
let placeholder = r#"<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>code-graph</title></head>
<body>
<h1>Frontend not built</h1>
<p>Run <code>npm run build</code> inside the <code>web/</code> directory to generate the frontend assets.</p>
</body>
</html>
"#;
fs::write(dist.join("index.html"), placeholder)
.expect("build.rs: failed to write web/dist/index.html placeholder");
println!(
"cargo:warning=web/dist/ was missing — created a placeholder. \
Run `cd web && npm run build` for a real frontend."
);
}
println!("cargo:rerun-if-changed=web/dist");
}
fn main() {
#[cfg(feature = "web")]
ensure_web_dist();
#[cfg(feature = "rag")]
build_ort_compat();
}