From d2ee14efdb8b2555878c7a90dda443489cef780e Mon Sep 17 00:00:00 2001 From: eri Date: Thu, 12 Sep 2024 12:56:06 +0200 Subject: [PATCH] allow custom build scritps --- src/codegen/sys/build.rs | 52 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/src/codegen/sys/build.rs b/src/codegen/sys/build.rs index ce365d18f..0bcf481c8 100644 --- a/src/codegen/sys/build.rs +++ b/src/codegen/sys/build.rs @@ -36,9 +36,17 @@ fn generate_build_script(w: &mut dyn Write, env: &Env, split_build_rs: bool) -> general::start_comments(w, &env.config)?; writeln!(w)?; } + + let scripts = find_custom_build_scripts(env).unwrap_or_default(); + for script in &scripts { + writeln!(w, "mod {};", script)?; + } + if !scripts.is_empty() { + writeln!(w)?; + } + writeln!( w, - "{}", r#"#[cfg(not(docsrs))] use std::process;"# )?; @@ -61,11 +69,49 @@ fn main() { println!("cargo:warning={s}"); process::exit(1); } -} -"# + + "# + )?; + + for script in &scripts { + writeln!(w, "{}::main();", script)?; + } + + write!( + w, + " +}} +" ) } +fn find_custom_build_scripts(env: &Env) -> Result> { + let mut vec = Vec::::new(); + for entry in std::fs::read_dir(&env.config.auto_path.parent().unwrap())? { + let path = entry?.path(); + let Some(ext) = path.extension() else { + continue; + }; + if ext != "rs" { + continue; + } + let file_stem = path.file_stem().expect("No file name"); + let file_stem = file_stem + .to_str() + .expect("Can't convert file name to string") + .to_owned(); + + if !file_stem.starts_with("build_") { + continue; + } + + vec.push(file_stem); + } + vec.sort(); + + Ok(vec) +} + fn generate_build_version(w: &mut dyn Write, env: &Env) -> Result<()> { general::start_comments(w, &env.config)?; writeln!(w)?;