use std::path::{Path, PathBuf};
use vj_codegen::aot::{AotBackend, AotKernelHandle};
pub use crate::aot_legacy::{build_aot_so, build_aot_so_default_obj};
use crate::run::{RunError, build_aot_object, build_aot_object_with};
use crate::sources::SourceSet;
use crate::timing_options::TimingOptions;
pub const EXPORT_PATTERNS: &[&str] = &["sim_*", "vj_*", "VJ_*"];
pub const RUNTIME_LIB_NAME: &str = "libvj_aot.a";
pub fn find_runtime_lib() -> Option<PathBuf> {
if let Some(p) = std::env::var_os("VJ_AOT_RT") {
return Some(PathBuf::from(p));
}
let exe = std::env::current_exe().ok()?;
let mut dir = exe.parent();
while let Some(d) = dir {
let candidate = d.join(RUNTIME_LIB_NAME);
if candidate.is_file() {
return Some(candidate);
}
if d.file_name().is_some_and(|n| n == "deps") {
dir = d.parent();
} else {
break;
}
}
None
}
pub fn build_aot_design(
sources: &SourceSet,
timing: TimingOptions,
runtime_lib: &Path,
out_so: &Path,
) -> Result<AotKernelHandle, RunError> {
let mut out_obj: PathBuf = out_so.to_path_buf();
out_obj.set_extension("o");
build_aot_object(sources, timing, &out_obj)?;
let linked = AotBackend::for_host()
.map_err(RunError::Codegen)?
.link_exporting(
std::slice::from_ref(&out_obj),
runtime_lib,
out_so,
Some(EXPORT_PATTERNS),
)
.map_err(RunError::Codegen);
let _ = std::fs::remove_file(&out_obj);
linked
}
pub fn build_aot_exe(
sources: &SourceSet,
timing: TimingOptions,
runtime_lib: &Path,
out_exe: &Path,
) -> Result<(), RunError> {
let mut out_obj: PathBuf = out_exe.to_path_buf();
out_obj.set_extension("o");
build_aot_object_with(sources, timing, true, &out_obj)?;
let linked = AotBackend::for_host()
.map_err(RunError::Codegen)?
.link_exe(std::slice::from_ref(&out_obj), runtime_lib, out_exe)
.map_err(RunError::Codegen);
let _ = std::fs::remove_file(&out_obj);
linked
}