]> Repositorios git - scryer-prolog.git/commitdiff
don't genrate code in src/
authorSkgland <[email protected]>
Mon, 10 Jan 2022 18:59:21 +0000 (19:59 +0100)
committerSkgland <[email protected]>
Mon, 10 Jan 2022 20:19:18 +0000 (21:19 +0100)
[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

build.rs
crates/instructions-template/src/lib.rs
crates/static-string-indexing/src/lib.rs
src/atom_table.rs
src/lib.rs

index 6c31576926ad19176e53d0e26e7cff4216618479..3571199e417ac996148f6098b920f86fcff61c54 100644 (file)
--- 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())
index a20be30203e072be7faac6fa373aca0f61f62e10..1a7e02a4ffa917b6903fc92e172188a84df38b60 100644 (file)
@@ -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
     }
 }
 
index 22b87cc013748ec05eb13867530c8fb500a33ee2..390c5405f92db1a85b5f2dc9c386e54b73dd4ea8 100644 (file)
@@ -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<syn::File, ()> {
+        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();
index 71771cc2cc08c03ad1c3df1202a7acd8c6abf9fc..b925273aea93ebbaf70c6cc4c3eace07a16bdec3 100644 (file)
@@ -21,7 +21,7 @@ pub struct Atom {
 
 const_assert!(mem::size_of::<Atom>() == 8);
 
-include!("./static_atoms.rs");
+include!(concat!(env!("OUT_DIR"), "/static_atoms.rs"));
 
 impl<'a> From<&'a Atom> for Atom {
     #[inline]
index a4aff98c8bf557be6cf567b14e7c99568a1d2c11..6cb89830aa6deaade03cc5e3a50264a3d5b396ca 100644 (file)
@@ -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;