Merge pull request #1199 from Skgland/do_not_gen_in_src

Generate into $OUT_DIR instead of src/
This commit is contained in:
Mark Thom
2022-01-11 23:01:59 -07:00
committed by GitHub
5 changed files with 45 additions and 21 deletions

View File

@@ -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())
@@ -83,4 +83,6 @@ fn main() {
.arg(static_atoms_path.as_os_str())
.spawn().unwrap()
.wait().unwrap();
println!("cargo:rerun-if-changed=src/");
}

View 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
}
}

View 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;
}
let mut file = match File::open(entry.path()) {
Ok(file) => file,
Err(_) => continue,
};
fn process_filepath(path: &std::path::Path) -> std::result::Result<syn::File, ()> {
let mut src = String::new();
let mut file = match File::open(path) {
Ok(file) => file,
Err(_) => return Err(()),
};
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();

View 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]

View 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;