Skip to content

Commit a7d58d3

Browse files
authored
feat(build): add support for using wasm threads (#49)
1 parent e0b2fa8 commit a7d58d3

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

build.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ fn main() {
1717
let compile_target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH");
1818
let compile_target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS");
1919
let compile_target_env = env::var("CARGO_CFG_TARGET_ENV").expect("CARGO_CFG_TARGET_ENV");
20+
let compile_target_feature = env::var("CARGO_CFG_TARGET_FEATURE");
2021
// Except for Emscripten target (which emulates POSIX environment), compile to Wasm via WASI SDK
2122
// which is currently the only standalone provider of stdlib for compilation of C/C++ libraries.
2223
if compile_target_arch.starts_with("wasm") && compile_target_os != "emscripten" {
@@ -26,7 +27,13 @@ fn main() {
2627
"WASI SDK not found at {wasi_sdk}"
2728
);
2829
build.compiler(format!("{wasi_sdk}/bin/clang++"));
29-
println!("cargo:rustc-link-search={wasi_sdk}/share/wasi-sysroot/lib/wasm32-wasi");
30+
let wasi_sysroot_lib = match compile_target_feature {
31+
Ok(compile_target_feature) if compile_target_feature.contains("atomics") => {
32+
"wasm32-wasi-threads"
33+
}
34+
_ => "wasm32-wasi",
35+
};
36+
println!("cargo:rustc-link-search={wasi_sdk}/share/wasi-sysroot/lib/{wasi_sysroot_lib}");
3037
// Wasm exceptions are new and not yet supported by WASI SDK.
3138
build.flag("-fno-exceptions");
3239
// WASI SDK only has libc++ available.

deps/wasi_to_unknown.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ int32_t __imported_wasi_snapshot_preview1_fd_write(int32_t,
4343
__builtin_unreachable();
4444
}
4545

46+
int32_t __imported_wasi_snapshot_preview1_sched_yield() {
47+
return 0;
48+
}
49+
4650
_Noreturn void __imported_wasi_snapshot_preview1_proc_exit(int32_t) {
4751
__builtin_unreachable();
4852
}

0 commit comments

Comments
 (0)