pub mod wrapper;
pub mod func_ids;
pub mod fill_table;
use std::collections::BTreeMap;
use std::path::Path;
use crate::context::CodegenContext;
pub fn generate(ctx: &CodegenContext, out_dir: &Path) {
std::fs::create_dir_all(out_dir).expect("Failed to create C++ output directory");
remove_stale_wrappers(out_dir);
let mut by_class: BTreeMap<(String, String), Vec<&crate::context::FuncEntry>> = BTreeMap::new();
for entry in &ctx.func_table {
by_class
.entry((entry.module_name.clone(), entry.class_name.clone()))
.or_default()
.push(entry);
}
for ((module, class), entries) in &by_class {
let code = wrapper::generate_wrapper_file(entries, ctx);
let filename = format!("RustealFunc_{}_{}.cpp", module, class);
std::fs::write(out_dir.join(&filename), code)
.unwrap_or_else(|e| panic!("Failed to write {filename}: {e}"));
}
let ids_code = func_ids::generate_cpp_func_ids(&ctx.func_table);
std::fs::write(out_dir.join("RustealFuncIds.h"), ids_code)
.expect("Failed to write RustealFuncIds.h");
let fill_code = fill_table::generate_fill_table(&ctx.func_table, &by_class);
std::fs::write(out_dir.join("RustealFillFuncTable.cpp"), fill_code)
.expect("Failed to write RustealFillFuncTable.cpp");
}
fn remove_stale_wrappers(out_dir: &Path) {
let Ok(entries) = std::fs::read_dir(out_dir) else {
return;
};
for entry in entries.flatten() {
let name = entry.file_name();
let name = name.to_string_lossy();
if name.starts_with("RustealFunc_") && name.ends_with(".cpp") {
std::fs::remove_file(entry.path())
.unwrap_or_else(|e| panic!("Failed to remove stale {name}: {e}"));
}
}
}