From: Skgland Date: Mon, 10 Jan 2022 18:59:21 +0000 (+0100) Subject: don't genrate code in src/ X-Git-Tag: v0.9.0^2~74^2~1 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=a5de329712324e296dcc2ba83f2da87a5ee27975;p=scryer-prolog.git don't genrate code in src/ [Outputs of Build Script](https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script) states that only files in $OUT_DIR may be modified --- diff --git a/build.rs b/build.rs index 6c315769..3571199e 100644 --- a/build.rs +++ b/build.rs @@ -56,7 +56,7 @@ fn main() { find_prolog_files(&mut libraries, "", &lib_path); libraries.write_all(b"\n m\n };\n}\n").unwrap(); - let instructions_path = Path::new("src/instructions.rs"); + let instructions_path = Path::new(&out_dir).join("instructions.rs"); let mut instructions_file = File::create(&instructions_path).unwrap(); let quoted_output = generate_instructions_rs(); @@ -70,10 +70,10 @@ fn main() { .spawn().unwrap() .wait().unwrap(); - let static_atoms_path = Path::new("src/static_atoms.rs"); + let static_atoms_path = Path::new(&out_dir).join("static_atoms.rs"); let mut static_atoms_file = File::create(&static_atoms_path).unwrap(); - let quoted_output = index_static_strings(); + let quoted_output = index_static_strings(&instructions_path); static_atoms_file .write_all(quoted_output.to_string().as_bytes()) diff --git a/crates/instructions-template/src/lib.rs b/crates/instructions-template/src/lib.rs index a20be302..1a7e02a4 100644 --- a/crates/instructions-template/src/lib.rs +++ b/crates/instructions-template/src/lib.rs @@ -3037,11 +3037,13 @@ pub fn generate_instructions_rs() -> TokenStream { } #[macro_export] - macro_rules! instr { + macro_rules! _instr { #( #instr_macro_arms );* } + + pub use _instr as instr; // https://github.com/rust-lang/rust/pull/52234#issuecomment-976702997 } } diff --git a/crates/static-string-indexing/src/lib.rs b/crates/static-string-indexing/src/lib.rs index 22b87cc0..390c5405 100644 --- a/crates/static-string-indexing/src/lib.rs +++ b/crates/static-string-indexing/src/lib.rs @@ -84,7 +84,7 @@ impl<'ast> Visit<'ast> for StaticStrVisitor { } } -pub fn index_static_strings() -> TokenStream { +pub fn index_static_strings(instruction_rs_path: &std::path::Path) -> TokenStream { use quote::*; use std::ffi::OsStr; @@ -103,20 +103,14 @@ pub fn index_static_strings() -> TokenStream { let mut visitor = StaticStrVisitor::new(); - for entry in WalkDir::new("src/").into_iter().filter_entry(filter_rust_files) { - let entry = entry.unwrap(); - - if entry.path().is_dir() { - continue; - } + fn process_filepath(path: &std::path::Path) -> std::result::Result { + let mut src = String::new(); - let mut file = match File::open(entry.path()) { + let mut file = match File::open(path) { Ok(file) => file, - Err(_) => continue, + Err(_) => return Err(()), }; - let mut src = String::new(); - match file.read_to_string(&mut src) { Ok(_) => {} Err(e) => { @@ -127,14 +121,36 @@ pub fn index_static_strings() -> TokenStream { let syntax = match syn::parse_file(&src) { Ok(s) => s, Err(e) => { - panic!("parse error: {} in file {:?}", e, entry.path()); + panic!("parse error: {} in file {:?}", e, path); } }; + Ok(syntax) + } + + for entry in WalkDir::new("src/") + .into_iter() + .filter_entry(filter_rust_files) + { + let entry = entry.unwrap(); + + if entry.path().is_dir() { + continue; + } + + let syntax = match process_filepath(entry.path()) { + Ok(syntax) => syntax, + Err(_) => continue, + }; visitor.visit_file(&syntax); } - let indices = (0 .. visitor.static_strs.len()).map(|i| i << 3); + match process_filepath(instruction_rs_path) { + Ok(syntax) => visitor.visit_file(&syntax), + Err(_) => {} + } + + let indices = (0..visitor.static_strs.len()).map(|i| i << 3); let indices_iter = indices.clone(); let static_strs_len = visitor.static_strs.len(); diff --git a/src/atom_table.rs b/src/atom_table.rs index 71771cc2..b925273a 100644 --- a/src/atom_table.rs +++ b/src/atom_table.rs @@ -21,7 +21,7 @@ pub struct Atom { const_assert!(mem::size_of::() == 8); -include!("./static_atoms.rs"); +include!(concat!(env!("OUT_DIR"), "/static_atoms.rs")); impl<'a> From<&'a Atom> for Atom { #[inline] diff --git a/src/lib.rs b/src/lib.rs index a4aff98c..6cb89830 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,7 +21,9 @@ mod heap_iter; pub mod heap_print; mod indexing; #[macro_use] -pub mod instructions; +pub mod instructions { + include!(concat!(env!("OUT_DIR"), "/instructions.rs")); +} mod iterators; pub mod machine; mod raw_block; @@ -29,3 +31,5 @@ pub mod read; mod targets; pub mod types; pub mod write; + +use instructions::instr;