use include_str! for the build in libraries

instead of "copy-paste-ing" the prolog source into rust string literals in the generated files
This commit is contained in:
Bennet Bleßmann
2024-07-23 00:01:29 +02:00
parent eaa95293e4
commit e7fd408548

View File

@@ -5,23 +5,18 @@ use instructions_template::generate_instructions_rs;
use static_string_indexing::index_static_strings; use static_string_indexing::index_static_strings;
use std::env; use std::env;
use std::fs;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
use std::path::PathBuf;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
fn find_prolog_files( fn find_prolog_files(path_prefix: &str, current_dir: &Path) -> Vec<(String, PathBuf)> {
libraries: &mut File, let mut libraries = vec![];
path_prefix: &str,
const_prefix: &str,
current_dir: &Path,
) -> Vec<(String, String)> {
let mut constants = vec![];
let entries = match current_dir.read_dir() { let entries = match current_dir.read_dir() {
Ok(entries) => entries, Ok(entries) => entries,
Err(_) => return constants, Err(_) => return libraries,
}; };
for entry in entries.filter_map(Result::ok).map(|e| e.path()) { 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() { if let Some(file_name) = entry.file_name() {
let file_name = file_name.to_str().unwrap(); let file_name = file_name.to_str().unwrap();
let new_path_prefix = format!("{path_prefix}{file_name}/"); let new_path_prefix = format!("{path_prefix}{file_name}/");
let new_const_prefix = format!("{const_prefix}_{}", file_name.to_uppercase()); let new_libs = find_prolog_files(&new_path_prefix, &entry);
let new_consts = libraries.extend(new_libs);
find_prolog_files(libraries, &new_path_prefix, &new_const_prefix, &entry);
constants.extend(new_consts);
} }
} else if entry.is_file() { } else if entry.is_file() {
let ext = std::ffi::OsStr::new("pl"); let ext = std::ffi::OsStr::new("pl");
if entry.extension() == Some(ext) { 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 name = entry.file_stem().unwrap().to_str().unwrap();
let lib_name = format!("{path_prefix}{name}"); let lib_name = format!("{path_prefix}{name}");
let const_name = format!("{const_prefix}_{}", name.to_uppercase());
writeln!(libraries, "const {const_name}: &str = {contain:?};").unwrap(); libraries.push((lib_name, entry));
constants.push((lib_name, const_name));
} }
} }
} }
constants libraries
} }
fn main() { fn main() {
@@ -67,32 +56,59 @@ fn main() {
let dest_path = Path::new(&out_dir).join("libraries.rs"); let dest_path = Path::new(&out_dir).join("libraries.rs");
let mut libraries = File::create(dest_path).unwrap(); let mut libraries = File::create(dest_path).unwrap();
let lib_path = Path::new("src/lib"); let lib_path = Path::new("src").join("lib");
writeln!( writeln!(
libraries, libraries,
"\ "\
use indexmap::IndexMap;\ use indexmap::IndexMap;
\
" "
) )
.unwrap(); .unwrap();
let constants = find_prolog_files(&mut libraries, "", "LIB", lib_path); let constants = find_prolog_files("", &lib_path);
writeln!( writeln!(
libraries, libraries,
"\ "\
std::thread_local!{{ std::thread_local!{{
static LIBRARIES: IndexMap<&'static str, &'static str> = {{ static LIBRARIES: IndexMap<&'static str, &'static str> = {{
let mut m = IndexMap::new();" let mut m = IndexMap::new();
\
"
) )
.unwrap(); .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!( writeln!(
libraries, libraries,
"\ " m.insert(\"{name}\", include_str!({path:?}));"
m.insert(\"{name}\",{constant});"
) )
.unwrap(); .unwrap();
} }