Build a Wasm test platform in test_gen

This commit is contained in:
Brian Carroll 2021-12-10 17:29:45 +00:00
parent 686d646b09
commit 53d221481e
2 changed files with 79 additions and 0 deletions

View file

@ -0,0 +1,53 @@
use std::env;
use std::ffi::OsStr;
use std::path::Path;
use std::process::Command;
pub const PLATFORM_FILENAME: &str = "wasm_test_platform";
pub const DIRNAME_VAR: &str = "TEST_GEN_OUT";
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=src/helpers/{}.c", PLATFORM_FILENAME);
let out_dir = env::var("OUT_DIR").unwrap();
println!("cargo:rustc-env={}={}", DIRNAME_VAR, out_dir);
run_command(
Path::new("."),
"zig",
[
"build-obj",
"-target",
"wasm32-wasi",
"-lc",
&format!("src/helpers/{}.c", PLATFORM_FILENAME),
&format!("-femit-bin={}/{}.o", out_dir, PLATFORM_FILENAME),
],
);
}
fn run_command<S, I, P: AsRef<Path>>(path: P, command_str: &str, args: I)
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
let output_result = Command::new(OsStr::new(&command_str))
.current_dir(path)
.args(args)
.output();
match output_result {
Ok(output) => match output.status.success() {
true => (),
false => {
let error_str = match std::str::from_utf8(&output.stderr) {
Ok(stderr) => stderr.to_string(),
Err(_) => format!("Failed to run \"{}\"", command_str),
};
panic!("{} failed: {}", command_str, error_str);
}
},
Err(reason) => panic!("{} failed: {}", command_str, reason),
}
}

View file

@ -0,0 +1,26 @@
#include <stdio.h>
void *roc_alloc(size_t size, unsigned int alignment) { return malloc(size); }
void *roc_realloc(void *ptr, size_t new_size, size_t old_size,
unsigned int alignment)
{
return realloc(ptr, new_size);
}
void roc_dealloc(void *ptr, unsigned int alignment) { free(ptr); }
void roc_panic(void *ptr, unsigned int alignment)
{
char *msg = (char *)ptr;
fprintf(stderr,
"Application crashed with message\n\n %s\n\nShutting down\n", msg);
exit(0);
}
void *roc_memcpy(void *dest, const void *src, size_t n)
{
return memcpy(dest, src, n);
}
void *roc_memset(void *str, int c, size_t n) { return memset(str, c, n); }