diff --git a/src/lib.rs b/src/lib.rs index 9b2816fcf..97ef3a707 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1313,6 +1313,14 @@ impl Build { self.cargo_output .print_metadata(&format_args!("cargo:rustc-link-lib={}", stdlib)); } + // Link c++ lib from WASI sysroot + if self.get_target()?.contains("wasi") { + let wasi_sysroot = self.wasi_sysroot()?; + self.cargo_output.print_metadata(&format_args!( + "cargo:rustc-flags=-L {}/lib/wasm32-wasi -lstatic=c++ -lstatic=c++abi", + wasi_sysroot + )); + } } let cudart = match &self.cudart { @@ -1912,6 +1920,14 @@ impl Build { cmd.push_cc_arg("-fno-plt".into()); } } + if target == "wasm32-wasip1" { + // WASI does not support exceptions yet. + // https://github.com/WebAssembly/exception-handling + cmd.push_cc_arg("-fno-exceptions".into()); + // Link clang sysroot + let wasi_sysroot = self.wasi_sysroot()?; + cmd.push_cc_arg(format!("--sysroot={}", wasi_sysroot).into()); + } } } @@ -2861,7 +2877,11 @@ impl Build { || target == "wasm32-unknown-wasi" || target == "wasm32-unknown-unknown" { - "clang".to_string() + if self.cpp { + "clang++".to_string() + } else { + "clang".to_string() + } } else if target.contains("vxworks") { if self.cpp { "wr-c++".to_string() @@ -3099,6 +3119,7 @@ impl Build { | target.contains("openbsd") | target.contains("aix") | target.contains("linux-ohos") + | target.contains("-wasi") { Ok(Some("c++".to_string())) } else if target.contains("android") { @@ -3852,6 +3873,17 @@ impl Build { } } + fn wasi_sysroot(&self) -> Result, Error> { + if let Some(wasi_sysroot_path) = self.getenv("WASI_SYSROOT") { + Ok(wasi_sysroot_path) + } else { + Err(Error::new( + ErrorKind::EnvVarNotFound, + "Environment variable WASI_SYSROOT not defined. Download sysroot from github & setup environment variable WASI_SYSROOT targetting the folder.", + )) + } + } + fn cuda_file_count(&self) -> usize { self.files .iter()