wasmer/build/emtests.rs

112 lines
3.5 KiB
Rust
Raw Normal View History

2018-12-10 03:21:28 +00:00
//! This file will run at build time to autogenerate the Emscripten tests
//! It will compile the files indicated in TESTS, to:executable and .wasm
//! - Compile using cc and get the output from it (expected output)
//! - Compile using emcc and get the .wasm from it (wasm)
//! - Generate the test that will compare the output of running the .wasm file
//! with wasmer with the expected output
use std::fs;
use std::path::PathBuf;
2018-12-11 01:06:55 +00:00
use std::process::Command;
2018-12-10 03:21:28 +00:00
static BANNER: &str = "// Rust test file autogenerated with cargo build (build/emtests.rs).
// Please do NOT modify it by hand, as it will be reseted on next build.\n";
2018-12-15 06:46:11 +00:00
const TESTS: [&str; 2] = ["emtests/puts.c", "emtests/printf.c"];
2018-12-10 03:21:28 +00:00
2018-12-11 01:06:55 +00:00
pub fn compile(file: &str) -> String {
let mut output_path = PathBuf::from(file);
output_path.set_extension("out");
let output_str = output_path.to_str().unwrap();
2018-12-15 06:46:11 +00:00
2018-12-11 01:06:55 +00:00
// Compile to .out
Command::new("cc")
2018-12-15 06:46:11 +00:00
.arg(file)
.arg("-o")
.arg(output_str)
.output()
.expect("failed to execute process");
2018-12-11 01:06:55 +00:00
// Get the result of .out
let output = Command::new(output_str)
2018-12-15 06:46:11 +00:00
.arg(output_str)
.output()
.expect("failed to execute process");
2018-12-11 01:06:55 +00:00
// Remove executable
fs::remove_file(output_str).unwrap();
let mut output_path = PathBuf::from(file);
output_path.set_extension("js");
let output_str = output_path.to_str().unwrap();
// Compile to wasm
2018-12-11 02:42:23 +00:00
let _wasm_compilation = Command::new("emcc")
2018-12-15 06:46:11 +00:00
.arg(file)
.arg("-s")
.arg("WASM=1")
.arg("-o")
.arg(output_str)
.output()
.expect("failed to execute process");
2018-12-11 01:06:55 +00:00
2018-12-11 02:42:23 +00:00
// panic!("{:?}", wasm_compilation);
// if output.stderr {
// panic!("{}", output.stderr);
// }
2018-12-11 01:06:55 +00:00
// Remove js file
fs::remove_file(output_str).unwrap();
let mut output_path = PathBuf::from(file);
output_path.set_extension("output");
2018-12-15 06:46:11 +00:00
let module_name = output_path
.file_stem()
.unwrap()
.to_str()
.unwrap()
.to_owned();
2018-12-11 01:06:55 +00:00
let output_str = output_path.to_str().unwrap();
// Write the output to file
fs::write(output_str, output.stdout).expect("Unable to write file");
2018-12-11 02:42:23 +00:00
let rust_test_filepath = format!(
concat!(env!("CARGO_MANIFEST_DIR"), "/src/emtests/{}.rs"),
module_name.as_str()
);
let contents = format!("#[test]
fn test_{module_name}() {{
assert_emscripten_output!(\"../../emtests/{module_name}.wasm\", \"{module_name}\", vec![], \"../../emtests/{module_name}.output\");
}}
", module_name=module_name);
fs::write(&rust_test_filepath, contents.as_bytes()).unwrap();
2018-12-11 01:06:55 +00:00
module_name
2018-12-15 06:46:11 +00:00
2018-12-11 01:06:55 +00:00
// panic!("OUTPUT: {:?}", output);
}
2018-12-10 03:21:28 +00:00
pub fn build() {
let rust_test_modpath = concat!(env!("CARGO_MANIFEST_DIR"), "/src/emtests/mod.rs");
let mut modules: Vec<String> = Vec::new();
// modules.reserve_exact(TESTS.len());
2018-12-11 01:06:55 +00:00
for test in TESTS.iter() {
let moudle_name = compile(test);
modules.push(format!("mod {};", moudle_name));
}
2018-12-10 03:21:28 +00:00
modules.insert(0, BANNER.to_string());
2018-12-11 02:42:23 +00:00
modules.insert(1, "// The _common module is not autogenerated, as it provides common macros for the emtests\n#[macro_use]\nmod _common;".to_string());
2018-12-10 03:21:28 +00:00
// We add an empty line
modules.push("".to_string());
let modfile: String = modules.join("\n");
let source = fs::read(&rust_test_modpath).unwrap();
// We only modify the mod file if has changed
if source != modfile.as_bytes() {
fs::write(&rust_test_modpath, modfile.as_bytes()).unwrap();
}
}