2019-01-13 16:56:09 -06:00
|
|
|
extern crate walkdir;
|
|
|
|
|
2019-01-14 18:44:01 -06:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2019-01-13 16:56:09 -06:00
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
|
|
|
fn generate_rust_module() -> String {
|
|
|
|
let mut rust_lib = String::new();
|
2019-01-14 18:44:01 -06:00
|
|
|
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("{");
|
2019-01-13 16:56:09 -06:00
|
|
|
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.
|
2019-01-14 18:44:01 -06:00
|
|
|
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();
|
2019-01-13 20:33:38 -06:00
|
|
|
let include = format!(
|
2019-01-14 18:44:01 -06:00
|
|
|
"\tstdlib.insert(\n\t\t\"{}\".to_string(),\n\t\tinclude_str!(\"{}/{}\"));\n",
|
|
|
|
path_str, out_dir, path_str
|
2019-01-13 20:33:38 -06:00
|
|
|
);
|
|
|
|
rust_lib.push_str(&include);
|
2019-01-14 18:44:01 -06:00
|
|
|
rust_lib.push_str("\n");
|
2019-01-13 16:56:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
rust_lib.push_str("}");
|
2019-01-14 18:44:01 -06:00
|
|
|
println!("Finished Adding lib files");
|
2019-01-13 16:56:09 -06:00
|
|
|
rust_lib
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let contents = generate_rust_module();
|
2019-01-14 18:44:01 -06:00
|
|
|
let out_dir = std::env::var("OUT_DIR").unwrap();
|
2019-01-16 19:28:59 -06:00
|
|
|
std::fs::write(
|
|
|
|
format!("{}/stdlib_generated.rs", out_dir),
|
|
|
|
contents.as_bytes(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-01-13 20:33:38 -06:00
|
|
|
}
|