mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-02 08:11:12 +00:00
Build a Wasm test platform in test_gen
This commit is contained in:
parent
686d646b09
commit
53d221481e
2 changed files with 79 additions and 0 deletions
53
compiler/test_gen/build.rs
Normal file
53
compiler/test_gen/build.rs
Normal 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),
|
||||||
|
}
|
||||||
|
}
|
26
compiler/test_gen/src/helpers/wasm_test_platform.c
Normal file
26
compiler/test_gen/src/helpers/wasm_test_platform.c
Normal 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); }
|
Loading…
Add table
Add a link
Reference in a new issue