@@ -1459,12 +1459,20 @@ impl Build {
1459
1459
. print_metadata ( & format_args ! ( "cargo:rustc-link-lib={}" , stdlib. display( ) ) ) ;
1460
1460
}
1461
1461
// Link c++ lib from WASI sysroot
1462
- if target. os == "wasi" {
1463
- if let Ok ( wasi_sysroot) = self . wasi_sysroot ( ) {
1462
+ if target. arch == "wasm32" {
1463
+ if target. os == "wasi" {
1464
+ if let Ok ( wasi_sysroot) = self . wasi_sysroot ( ) {
1465
+ self . cargo_output . print_metadata ( & format_args ! (
1466
+ "cargo:rustc-flags=-L {}/lib/{} -lstatic=c++ -lstatic=c++abi" ,
1467
+ Path :: new( & wasi_sysroot) . display( ) ,
1468
+ self . get_raw_target( ) ?
1469
+ ) ) ;
1470
+ }
1471
+ } else if target. os == "linux" {
1472
+ let musl_sysroot = self . wasm_musl_sysroot ( ) . unwrap ( ) ;
1464
1473
self . cargo_output . print_metadata ( & format_args ! (
1465
- "cargo:rustc-flags=-L {}/lib/{} -lstatic=c++ -lstatic=c++abi" ,
1466
- Path :: new( & wasi_sysroot) . display( ) ,
1467
- self . get_raw_target( ) ?
1474
+ "cargo:rustc-flags=-L {}/lib -lstatic=c++ -lstatic=c++abi" ,
1475
+ Path :: new( & musl_sysroot) . display( ) ,
1468
1476
) ) ;
1469
1477
}
1470
1478
}
@@ -2179,8 +2187,25 @@ impl Build {
2179
2187
if self . cpp && self . cpp_set_stdlib . is_none ( ) {
2180
2188
cmd. push_cc_arg ( "-stdlib=libc++" . into ( ) ) ;
2181
2189
}
2190
+ } else if target. arch == "wasm32" && target. os == "linux" {
2191
+ for x in & [
2192
+ "atomics" ,
2193
+ "bulk-memory" ,
2194
+ "mutable-globals" ,
2195
+ "sign-ext" ,
2196
+ "exception-handling" ,
2197
+ ] {
2198
+ cmd. push_cc_arg ( format ! ( "-m{x}" ) . into ( ) ) ;
2199
+ }
2200
+ for x in & [ "wasm-exceptions" , "declspec" ] {
2201
+ cmd. push_cc_arg ( format ! ( "-f{x}" ) . into ( ) ) ;
2202
+ }
2203
+ let musl_sysroot = self . wasm_musl_sysroot ( ) . unwrap ( ) ;
2204
+ cmd. push_cc_arg (
2205
+ format ! ( "--sysroot={}" , Path :: new( & musl_sysroot) . display( ) ) . into ( ) ,
2206
+ ) ;
2207
+ cmd. push_cc_arg ( "-pthread" . into ( ) ) ;
2182
2208
}
2183
-
2184
2209
// Pass `--target` with the LLVM target to configure Clang for cross-compiling.
2185
2210
//
2186
2211
// This is **required** for cross-compilation, as it's the only flag that
@@ -3953,6 +3978,17 @@ impl Build {
3953
3978
version
3954
3979
}
3955
3980
3981
+ fn wasm_musl_sysroot ( & self ) -> Result < Arc < OsStr > , Error > {
3982
+ if let Some ( musl_sysroot_path) = self . getenv ( "WASM_MUSL_SYSROOT" ) {
3983
+ Ok ( musl_sysroot_path)
3984
+ } else {
3985
+ Err ( Error :: new (
3986
+ ErrorKind :: EnvVarNotFound ,
3987
+ "Environment variable WASM_MUSL_SYSROOT not defined for wasm32. Download sysroot from GitHub & setup environment variable MUSL_SYSROOT targeting the folder." ,
3988
+ ) )
3989
+ }
3990
+ }
3991
+
3956
3992
fn wasi_sysroot ( & self ) -> Result < Arc < OsStr > , Error > {
3957
3993
if let Some ( wasi_sysroot_path) = self . getenv ( "WASI_SYSROOT" ) {
3958
3994
Ok ( wasi_sysroot_path)
0 commit comments