Wasm: pre-link app-independent code before running tests (builtins, plaform & libc)

This commit is contained in:
Brian Carroll 2022-01-07 18:01:16 +00:00
parent d0e3c45986
commit 5de9581b62
3 changed files with 21 additions and 55 deletions

View File

@ -9,6 +9,9 @@ edition = "2018"
name = "test_gen"
path = "src/tests.rs"
[build-dependencies]
roc_builtins = { path = "../builtins" }
[dev-dependencies]
roc_gen_llvm = { path = "../gen_llvm" }
roc_gen_dev = { path = "../gen_dev" }

View File

@ -1,3 +1,4 @@
use roc_builtins::bitcode;
use std::env;
use std::ffi::OsStr;
use std::path::Path;
@ -5,8 +6,6 @@ use std::process::Command;
const PLATFORM_FILENAME: &str = "wasm_test_platform";
const OUT_DIR_VAR: &str = "TEST_GEN_OUT";
const LIBC_PATH_VAR: &str = "TEST_GEN_WASM_LIBC_PATH";
const COMPILER_RT_PATH_VAR: &str = "TEST_GEN_WASM_COMPILER_RT_PATH";
fn main() {
println!("cargo:rerun-if-changed=build.rs");
@ -20,8 +19,7 @@ fn build_wasm() {
println!("cargo:rustc-env={}={}", OUT_DIR_VAR, out_dir);
build_wasm_test_platform(&out_dir);
build_wasm_libc(&out_dir);
build_wasm_platform_and_builtins(&out_dir);
}
fn zig_executable() -> String {
@ -31,52 +29,25 @@ fn zig_executable() -> String {
}
}
fn build_wasm_test_platform(out_dir: &str) {
/// Create an all-in-one object file: platform + builtins + libc
fn build_wasm_platform_and_builtins(out_dir: &str) {
println!("cargo:rerun-if-changed=src/helpers/{}.c", PLATFORM_FILENAME);
run_command(
Path::new("."),
&zig_executable(),
[
"build-obj",
"-target",
"wasm32-wasi",
"-lc",
&format!("src/helpers/{}.c", PLATFORM_FILENAME),
&format!("-femit-bin={}/{}.o", out_dir, PLATFORM_FILENAME),
],
);
}
// See discussion with Luuk de Gram (Zig contributor)
// https://github.com/rtfeldman/roc/pull/2181#pullrequestreview-839608063
// This builds a library file that exports everything. It has no linker data but we don't need that.
let args = [
"build-lib",
"-target",
"wasm32-wasi",
"-lc",
"-dynamic", // -dynamic ensures libc code goes into the binary
bitcode::BUILTINS_WASM32_OBJ_PATH,
&format!("src/helpers/{}.c", PLATFORM_FILENAME),
&format!("-femit-bin={}/{}.o", out_dir, PLATFORM_FILENAME),
];
fn build_wasm_libc(out_dir: &str) {
let source_path = "src/helpers/dummy_libc_program.c";
println!("cargo:rerun-if-changed={}", source_path);
let cwd = Path::new(".");
let zig_cache_dir = format!("{}/zig-cache-wasm32", out_dir);
run_command(
cwd,
&zig_executable(),
[
"build-exe", // must be an executable or it won't compile libc
"-target",
"wasm32-wasi",
"-lc",
source_path,
"-femit-bin=/dev/null",
"--global-cache-dir",
&zig_cache_dir,
],
);
let libc_path = run_command(cwd, "find", [&zig_cache_dir, "-name", "libc.a"]);
let compiler_rt_path = run_command(cwd, "find", [&zig_cache_dir, "-name", "compiler_rt.o"]);
println!("cargo:rustc-env={}={}", LIBC_PATH_VAR, libc_path);
println!(
"cargo:rustc-env={}={}",
COMPILER_RT_PATH_VAR, compiler_rt_path
);
run_command(Path::new("."), &zig_executable(), args);
}
fn feature_is_enabled(feature_name: &str) -> bool {

View File

@ -8,7 +8,6 @@ use wasmer::{Memory, WasmPtr};
use crate::helpers::from_wasm32_memory::FromWasm32Memory;
use crate::helpers::wasm32_test_result::Wasm32TestResult;
use roc_builtins::bitcode;
use roc_can::builtins::builtin_defs_map;
use roc_collections::all::{MutMap, MutSet};
use roc_gen_wasm::{DEBUG_LOG_SETTINGS, MEMORY_NAME};
@ -16,8 +15,6 @@ use roc_gen_wasm::{DEBUG_LOG_SETTINGS, MEMORY_NAME};
// Should manually match build.rs
const PLATFORM_FILENAME: &str = "wasm_test_platform";
const OUT_DIR_VAR: &str = "TEST_GEN_OUT";
const LIBC_PATH_VAR: &str = "TEST_GEN_WASM_LIBC_PATH";
const COMPILER_RT_PATH_VAR: &str = "TEST_GEN_WASM_COMPILER_RT_PATH";
#[allow(unused_imports)]
use roc_mono::ir::PRETTY_PRINT_IR_SYMBOLS;
@ -175,8 +172,6 @@ fn run_linker(
let app_o_file = wasm_build_dir.join("app.o");
let test_out_dir = std::env::var(OUT_DIR_VAR).unwrap();
let test_platform_o = format!("{}/{}.o", test_out_dir, PLATFORM_FILENAME);
let libc_a_file = std::env::var(LIBC_PATH_VAR).unwrap();
let compiler_rt_o_file = std::env::var(COMPILER_RT_PATH_VAR).unwrap();
// write the module to a file so the linker can access it
std::fs::write(&app_o_file, &app_module_bytes).unwrap();
@ -185,10 +180,7 @@ fn run_linker(
"wasm-ld",
// input files
app_o_file.to_str().unwrap(),
bitcode::BUILTINS_WASM32_OBJ_PATH,
&test_platform_o,
&libc_a_file,
&compiler_rt_o_file,
// output
"-o",
final_wasm_file.to_str().unwrap(),