Skip to content

Commit 6a6107a

Browse files
antaaltNobodyXu
andauthored
Fix WASI compilation for C++ (#1083)
* 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]>
1 parent 76c377a commit 6a6107a

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/lib.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,14 @@ impl Build {
13181318
self.cargo_output
13191319
.print_metadata(&format_args!("cargo:rustc-link-lib={}", stdlib));
13201320
}
1321+
// Link c++ lib from WASI sysroot
1322+
if self.get_target()?.contains("wasi") {
1323+
let wasi_sysroot = self.wasi_sysroot()?;
1324+
self.cargo_output.print_metadata(&format_args!(
1325+
"cargo:rustc-flags=-L {}/lib/wasm32-wasi -lstatic=c++ -lstatic=c++abi",
1326+
wasi_sysroot
1327+
));
1328+
}
13211329
}
13221330

13231331
let cudart = match &self.cudart {
@@ -1917,6 +1925,14 @@ impl Build {
19171925
cmd.push_cc_arg("-fno-plt".into());
19181926
}
19191927
}
1928+
if target == "wasm32-wasip1" {
1929+
// WASI does not support exceptions yet.
1930+
// https://github.com/WebAssembly/exception-handling
1931+
cmd.push_cc_arg("-fno-exceptions".into());
1932+
// Link clang sysroot
1933+
let wasi_sysroot = self.wasi_sysroot()?;
1934+
cmd.push_cc_arg(format!("--sysroot={}", wasi_sysroot).into());
1935+
}
19201936
}
19211937
}
19221938

@@ -2859,7 +2875,11 @@ impl Build {
28592875
|| target == "wasm32-unknown-wasi"
28602876
|| target == "wasm32-unknown-unknown"
28612877
{
2862-
"clang".to_string()
2878+
if self.cpp {
2879+
"clang++".to_string()
2880+
} else {
2881+
"clang".to_string()
2882+
}
28632883
} else if target.contains("vxworks") {
28642884
if self.cpp {
28652885
"wr-c++".to_string()
@@ -3099,6 +3119,7 @@ impl Build {
30993119
| target.contains("openbsd")
31003120
| target.contains("aix")
31013121
| target.contains("linux-ohos")
3122+
| target.contains("-wasi")
31023123
{
31033124
Ok(Some("c++".to_string()))
31043125
} else if target.contains("android") {
@@ -3853,6 +3874,17 @@ impl Build {
38533874
}
38543875
}
38553876

3877+
fn wasi_sysroot(&self) -> Result<Arc<str>, Error> {
3878+
if let Some(wasi_sysroot_path) = self.getenv("WASI_SYSROOT") {
3879+
Ok(wasi_sysroot_path)
3880+
} else {
3881+
Err(Error::new(
3882+
ErrorKind::EnvVarNotFound,
3883+
"Environment variable WASI_SYSROOT not defined. Download sysroot from github & setup environment variable WASI_SYSROOT targetting the folder.",
3884+
))
3885+
}
3886+
}
3887+
38563888
fn cuda_file_count(&self) -> usize {
38573889
self.files
38583890
.iter()

0 commit comments

Comments
 (0)