From e7fd408548a9fbbc19586a6b6cdb0020082cac75 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bennet=20Ble=C3=9Fmann?= Date: Tue, 23 Jul 2024 00:01:29 +0200 Subject: [PATCH] use include_str! for the build in libraries instead of "copy-paste-ing" the prolog source into rust string literals in the generated files --- build/main.rs | 68 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/build/main.rs b/build/main.rs index b7a66f03..1f026de3 100644 --- a/build/main.rs +++ b/build/main.rs @@ -5,23 +5,18 @@ use instructions_template::generate_instructions_rs; use static_string_indexing::index_static_strings; use std::env; -use std::fs; use std::fs::File; use std::io::Write; use std::path::Path; +use std::path::PathBuf; use std::process::{Command, Stdio}; -fn find_prolog_files( - libraries: &mut File, - path_prefix: &str, - const_prefix: &str, - current_dir: &Path, -) -> Vec<(String, String)> { - let mut constants = vec![]; +fn find_prolog_files(path_prefix: &str, current_dir: &Path) -> Vec<(String, PathBuf)> { + let mut libraries = vec![]; let entries = match current_dir.read_dir() { Ok(entries) => entries, - Err(_) => return constants, + Err(_) => return libraries, }; for entry in entries.filter_map(Result::ok).map(|e| e.path()) { @@ -29,27 +24,21 @@ fn find_prolog_files( if let Some(file_name) = entry.file_name() { let file_name = file_name.to_str().unwrap(); let new_path_prefix = format!("{path_prefix}{file_name}/"); - let new_const_prefix = format!("{const_prefix}_{}", file_name.to_uppercase()); - let new_consts = - find_prolog_files(libraries, &new_path_prefix, &new_const_prefix, &entry); - constants.extend(new_consts); + let new_libs = find_prolog_files(&new_path_prefix, &entry); + libraries.extend(new_libs); } } else if entry.is_file() { let ext = std::ffi::OsStr::new("pl"); if entry.extension() == Some(ext) { - let contain = String::from_utf8(fs::read(&entry).unwrap()).unwrap(); let name = entry.file_stem().unwrap().to_str().unwrap(); let lib_name = format!("{path_prefix}{name}"); - let const_name = format!("{const_prefix}_{}", name.to_uppercase()); - writeln!(libraries, "const {const_name}: &str = {contain:?};").unwrap(); - - constants.push((lib_name, const_name)); + libraries.push((lib_name, entry)); } } } - constants + libraries } fn main() { @@ -67,32 +56,59 @@ fn main() { let dest_path = Path::new(&out_dir).join("libraries.rs"); let mut libraries = File::create(dest_path).unwrap(); - let lib_path = Path::new("src/lib"); + let lib_path = Path::new("src").join("lib"); writeln!( libraries, "\ -use indexmap::IndexMap;\ +use indexmap::IndexMap; +\ " ) .unwrap(); - let constants = find_prolog_files(&mut libraries, "", "LIB", lib_path); + let constants = find_prolog_files("", &lib_path); writeln!( libraries, "\ std::thread_local!{{ static LIBRARIES: IndexMap<&'static str, &'static str> = {{ - let mut m = IndexMap::new();" + let mut m = IndexMap::new(); +\ + " ) .unwrap(); - for (name, constant) in constants { + let out_dir = std::env::var("OUT_DIR").unwrap(); + let out_dir_path: &Path = out_dir.as_ref(); + let manifest_dir = &std::env::var("CARGO_MANIFEST_DIR").unwrap(); + let manifest_dir_path: &Path = manifest_dir.as_ref(); + + let prefix: PathBuf = if let Ok(diff) = out_dir_path.strip_prefix(manifest_dir_path) { + let mut path = PathBuf::from("."); + for comp in diff.components() { + match comp { + std::path::Component::Normal(_) => path.push(".."), + std::path::Component::CurDir => (), + std::path::Component::Prefix(_) + | std::path::Component::RootDir + | std::path::Component::ParentDir => { + path = manifest_dir_path.to_path_buf(); + break; + } + } + } + path + } else { + manifest_dir_path.to_path_buf() + }; + + for (name, lib_path) in constants { + let path: PathBuf = prefix.join(lib_path); writeln!( libraries, - "\ - m.insert(\"{name}\",{constant});" + " m.insert(\"{name}\", include_str!({path:?}));" ) .unwrap(); } -- 2.54.0