Skip to content

Commit

Permalink
Fix WASI compilation for C++ (#1083)
Browse files Browse the repository at this point in the history
* Add support for WASI cpp build

* Look for WASI_SDK & link libc++

* Fixes & change wasi sdk to sysroot

* Fix fmt issues

* wasi sysroot now return a pathbuf

* Replaced PathBuf to Arc

* Ensure no-exceptions is set only for wasm32-wasi

* Update src/lib.rs

---------

Co-authored-by: Jiahao XU <[email protected]>
  • Loading branch information
antaalt and NobodyXu authored Jun 21, 2024
1 parent 76c377a commit 6a6107a
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,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 {
Expand Down Expand Up @@ -1917,6 +1925,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());
}
}
}

Expand Down Expand Up @@ -2859,7 +2875,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()
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -3853,6 +3874,17 @@ impl Build {
}
}

fn wasi_sysroot(&self) -> Result<Arc<str>, 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()
Expand Down

0 comments on commit 6a6107a

Please sign in to comment.