From 30a9a3a0894f19774f67279ed0224a17fa1a6f9b Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 10 Nov 2024 18:05:43 +0100 Subject: [PATCH 1/4] Properly disable PIC on WASM targets CC https://github.com/rust-lang/cc-rs/issues/1240 --- src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index f75b9511..3c165dc9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1994,11 +1994,15 @@ impl Build { cmd.push_cc_arg("-fdata-sections".into()); } // Disable generation of PIC on bare-metal for now: rust-lld doesn't support this yet + // + // `rustc` also defaults to disable PIC on WASM: + // if self.pic.unwrap_or( target.os != "windows" && target.os != "none" && target.os != "uefi" - && target.os != "wasi", + && target.arch != "wasm32" + && target.arch != "wasm64", ) { cmd.push_cc_arg("-fPIC".into()); // PLT only applies if code is compiled with PIC support, From e61f4f6c92af948859e988ccec0fb9441a01976e Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 10 Nov 2024 18:09:19 +0100 Subject: [PATCH 2/4] Disable exceptions on all WASM targets --- src/lib.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 3c165dc9..4a3dd5a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2013,10 +2013,17 @@ impl Build { cmd.push_cc_arg("-fno-plt".into()); } } - if target.os == "wasi" { + if target.arch == "wasm32" || target.arch == "wasm64" { // WASI does not support exceptions yet. // https://github.com/WebAssembly/exception-handling + // + // `rustc` also defaults to (currently) disable exceptions + // on all WASM targets: + // cmd.push_cc_arg("-fno-exceptions".into()); + } + + if target.os == "wasi" { // Link clang sysroot if let Ok(wasi_sysroot) = self.wasi_sysroot() { cmd.push_cc_arg( From 3fd85a8f3479e8190102c19fbce7f76a4ffa1d9a Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 10 Nov 2024 18:15:44 +0100 Subject: [PATCH 3/4] Default to compiling with Clang on all WASM targets --- src/lib.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4a3dd5a9..f8544f01 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2715,12 +2715,10 @@ impl Build { "{}-{}-{}-{}", target.full_arch, target.vendor, target.os, traditional ) - } else if target.os == "wasi" { - if self.cpp { - "clang++".to_string() - } else { - "clang".to_string() - } + } else if target.arch == "wasm32" || target.arch == "wasm64" { + // Compiling WASM is not currently supported by GCC, so + // let's default to Clang. + clang.to_string() } else if target.os == "vxworks" { if self.cpp { "wr-c++".to_string() From e473a886fd3bd544fe87299807927abf6bfb2080 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 10 Nov 2024 18:20:50 +0100 Subject: [PATCH 4/4] Find archiver on WASM64 as well --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index f8544f01..9ac535ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3103,7 +3103,7 @@ impl Build { name = format!("em{}", tool).into(); Some(self.cmd(&name)) } - } else if target.arch == "wasm32" { + } else if target.arch == "wasm32" || target.arch == "wasm64" { // Formally speaking one should be able to use this approach, // parsing -print-search-dirs output, to cover all clang targets, // including Android SDKs and other cross-compilation scenarios...