@@ -1459,12 +1459,20 @@ impl Build {
14591459 . print_metadata ( & format_args ! ( "cargo:rustc-link-lib={}" , stdlib. display( ) ) ) ;
14601460 }
14611461 // 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 ( ) ;
14641473 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( ) ,
14681476 ) ) ;
14691477 }
14701478 }
@@ -2179,8 +2187,25 @@ impl Build {
21792187 if self . cpp && self . cpp_set_stdlib . is_none ( ) {
21802188 cmd. push_cc_arg ( "-stdlib=libc++" . into ( ) ) ;
21812189 }
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 ( ) ) ;
21822208 }
2183-
21842209 // Pass `--target` with the LLVM target to configure Clang for cross-compiling.
21852210 //
21862211 // This is **required** for cross-compilation, as it's the only flag that
@@ -3953,6 +3978,17 @@ impl Build {
39533978 version
39543979 }
39553980
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+
39563992 fn wasi_sysroot ( & self ) -> Result < Arc < OsStr > , Error > {
39573993 if let Some ( wasi_sysroot_path) = self . getenv ( "WASI_SYSROOT" ) {
39583994 Ok ( wasi_sysroot_path)
0 commit comments