From 98323eabbd8f6ead4c77de75489639e797ea0732 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Mon, 14 Jan 2019 18:44:01 -0600 Subject: [PATCH] FIX: Multiple packaging errors. * The include setting doesn't do what I thought it did. * Also when packaging the rules on what build.rs can do are a little more strict than was immediately apparent. --- .gitignore | 1 - Cargo.toml | 8 +++++++- bin/build_main.rs | 32 ++++++++++++++++++++++---------- src/build/stdlib.rs | 7 +++++++ 4 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 src/build/stdlib.rs diff --git a/.gitignore b/.gitignore index 17a81ca..058252d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,3 @@ target docsite/site/generated docsite/site/public rerast_rules/ -src/build/stdlib.rs diff --git a/Cargo.toml b/Cargo.toml index c2fd369..4ec4510 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,8 +9,14 @@ documentation = "https://docs.rs/crate/ucg" readme = "README.md" keywords = ["compiler", "config"] license = "Apache-2.0" -include = ["std/**/*.ucg", "std/*.ucg"] build = "bin/build_main.rs" +include = [ + "std/**/*", + "src/**/*", + "bin/build_main.rs", + "Cargo.toml", + "Cargo.lock" +] [dependencies] abortable_parser = "~0.2.2" diff --git a/bin/build_main.rs b/bin/build_main.rs index 5d046ff..a7924ea 100644 --- a/bin/build_main.rs +++ b/bin/build_main.rs @@ -1,33 +1,45 @@ extern crate walkdir; +use std::path::PathBuf; + use walkdir::WalkDir; fn generate_rust_module() -> String { let mut rust_lib = String::new(); - rust_lib.push_str("use std::collections::HashMap;\n"); - rust_lib.push_str("pub fn get_libs() -> HashMap {\n"); - rust_lib.push_str("\tlet mut stdlib = HashMap::new();\n"); + let out_dir = std::env::var("OUT_DIR").unwrap(); + // NOTE(jwall): Since the generated file will be included using the include! macro + // This has to be an expression or item. This means we need to enclose it with + // braces to force it to be a single expression instead of multiple. + rust_lib.push_str("{"); for entry in WalkDir::new("std").into_iter().filter_map(|e| e.ok()) { // Okay we want to add these as include bytes in a simulated // filesystem for our binary to use. let path = entry.into_path(); // We only include files that are not test files. - if path.is_file() && !path.ends_with("_test.ucg") { - let path_str = path.to_string_lossy(); + let path_str = path.to_string_lossy().to_string(); + if path.is_file() && !path_str.ends_with("_test.ucg") { + println!("Adding lib file: {}", path_str); + let out_path = PathBuf::from(format!("{}/{}", out_dir, path_str)); + // We have to copy the file into out out directory to ensure that we + // have a reliable way for the stdlib.rs module file to include them + // from. + std::fs::create_dir_all(out_path.parent().unwrap()).unwrap(); + std::fs::copy(&path_str, &out_path).unwrap(); let include = format!( - "\tstdlib.insert(\n\t\t\"{}\".to_string(),\n\t\tinclude_str!(\"../../{}\"));\n", - path_str, path_str + "\tstdlib.insert(\n\t\t\"{}\".to_string(),\n\t\tinclude_str!(\"{}/{}\"));\n", + path_str, out_dir, path_str ); rust_lib.push_str(&include); + rust_lib.push_str("\n"); } } - rust_lib.push_str("\tstdlib\n"); rust_lib.push_str("}"); - println!("{}", rust_lib); + println!("Finished Adding lib files"); rust_lib } fn main() { let contents = generate_rust_module(); - std::fs::write("src/build/stdlib.rs", contents.as_bytes()).unwrap(); + let out_dir = std::env::var("OUT_DIR").unwrap(); + std::fs::write(format!("{}/stdlib_generated.rs", out_dir), contents.as_bytes()).unwrap(); } diff --git a/src/build/stdlib.rs b/src/build/stdlib.rs new file mode 100644 index 0000000..63cac55 --- /dev/null +++ b/src/build/stdlib.rs @@ -0,0 +1,7 @@ +use std::collections::HashMap; + +pub fn get_libs() -> HashMap { + let mut stdlib = HashMap::new(); + include!(concat!(env!("OUT_DIR"), "/stdlib_generated.rs")); + stdlib +}