Merge pull request #5333 from roc-lang/wasm-flow

Fix misc wasm compilation issues
This commit is contained in:
Brendan Hansknecht 2023-04-29 20:43:58 +00:00 committed by GitHub
commit e520eaddcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 70 deletions

View file

@ -362,21 +362,8 @@ pub fn build_zig_host_wasm32(
unimplemented!("Linking a shared library to wasm not yet implemented");
}
let zig_target = if matches!(opt_level, OptLevel::Development) {
"wasm32-wasi"
} else {
// For LLVM backend wasm we are emitting a .bc file anyway so this target is OK
"i386-linux-musl"
};
// NOTE currently just to get compiler warnings if the host code is invalid.
// the produced artifact is not used
//
// NOTE we're emitting LLVM IR here (again, it is not actually used)
//
// we'd like to compile with `-target wasm32-wasi` but that is blocked on
//
// https://github.com/ziglang/zig/issues/9414
let mut zig_cmd = zig();
zig_cmd
@ -397,7 +384,7 @@ pub fn build_zig_host_wasm32(
"--library",
"c",
"-target",
zig_target,
"wasm32-wasi",
// "-femit-llvm-ir=/home/folkertdev/roc/roc/crates/cli_testing_examples/benchmarks/platform/host.ll",
"-fPIC",
"--strip",
@ -1280,8 +1267,6 @@ fn link_wasm32(
input_paths: &[&str],
_link_type: LinkType,
) -> io::Result<(Child, PathBuf)> {
let wasi_libc_path = find_wasi_libc_path();
let child = zig()
// .env_clear()
// .env("PATH", &env_path)
@ -1289,8 +1274,9 @@ fn link_wasm32(
.args(input_paths)
.args([
// include wasi libc
// TOOD: This now compiles fine with `-lc`. That said, the output file doesn't work.
// using `-lc` is broken in zig 8 (and early 9) in combination with ReleaseSmall
wasi_libc_path.to_str().unwrap(),
find_wasi_libc_path().to_str().unwrap(),
&format!("-femit-bin={}", output_path.to_str().unwrap()),
"-target",
"wasm32-wasi-musl",

View file

@ -103,7 +103,7 @@ pub fn gen_from_mono_module<'a>(
let opt = code_gen_options.opt_level;
match code_gen_options.backend {
CodeGenBackend::Assembly => gen_from_mono_module_dev(
CodeGenBackend::Assembly | CodeGenBackend::Wasm => gen_from_mono_module_dev(
arena,
loaded,
target,
@ -113,16 +113,6 @@ pub fn gen_from_mono_module<'a>(
CodeGenBackend::Llvm(backend_mode) => {
gen_from_mono_module_llvm(arena, loaded, path, target, opt, backend_mode, debug)
}
CodeGenBackend::Wasm => {
// emit wasm via the llvm backend
let backend_mode = match code_gen_options.opt_level {
OptLevel::Development => LlvmBackendMode::BinaryDev,
OptLevel::Normal | OptLevel::Size | OptLevel::Optimize => LlvmBackendMode::Binary,
};
gen_from_mono_module_llvm(arena, loaded, path, target, opt, backend_mode, debug)
}
}
}
@ -966,9 +956,17 @@ fn build_loaded_file<'a>(
std::fs::write(&output_exe_path, &*roc_app_bytes).unwrap();
}
(LinkingStrategy::Legacy, _) => {
let extension = if matches!(operating_system, roc_target::OperatingSystem::Wasi) {
// Legacy linker is only by used llvm wasm backend, not dev.
// llvm wasm backend directly emits a bitcode file when targeting wasi, not a `.o` or `.wasm` file.
// If we set the extension wrong, zig will print a ton of warnings when linking.
"bc"
} else {
operating_system.object_file_ext()
};
let app_o_file = tempfile::Builder::new()
.prefix("roc_app")
.suffix(&format!(".{}", operating_system.object_file_ext()))
.suffix(&format!(".{}", extension))
.tempfile()
.map_err(|err| todo!("TODO Gracefully handle tempfile creation error {:?}", err))?;
let app_o_file = app_o_file.path();

View file

@ -29,7 +29,7 @@ impl OperatingSystem {
match self {
OperatingSystem::Windows => "obj",
OperatingSystem::Unix => "o",
OperatingSystem::Wasi => "o",
OperatingSystem::Wasi => "wasm",
}
}