Skip to content

Commit 93d3b18

Browse files
authored
Rollup merge of rust-lang#98358 - hoodmane:emscripten-dynlib, r=petrochenkov
Implement set_output_kind for Emscripten linker This is on top of rust-lang#98149. See also the earlier discussion on rust-lang#98303. With this PR, a crate that specifies that it is a cdylib will compile to a working Emscripten dynamic library without adding any extra cargo, rustc, or linker flags and without fiddling with environment variables. `@sbc100` r? `@petrochenkov`
2 parents 8c1cc82 + 9da4fea commit 93d3b18

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,38 @@ impl<'a> Linker for EmLinker<'a> {
10401040
&mut self.cmd
10411041
}
10421042

1043-
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
1043+
fn set_output_kind(&mut self, output_kind: LinkOutputKind, _out_filename: &Path) {
1044+
match output_kind {
1045+
LinkOutputKind::DynamicNoPicExe | LinkOutputKind::DynamicPicExe => {
1046+
// "-sMAIN_MODULE=1" breaks
1047+
// https://github.com/rust-lang/rust/issues/92738
1048+
self.cmd.arg("-sMAIN_MODULE=2");
1049+
warn!(
1050+
"Building dynamic executable with -sMAIN_MODULE=2. \
1051+
Dead code elimination may break things. \
1052+
See https://emscripten.org/docs/compiling/Dynamic-Linking.html?highlight=main_module#code-size \
1053+
"
1054+
);
1055+
}
1056+
LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => {
1057+
// -sSIDE_MODULE=1 breaks
1058+
// https://github.com/rust-lang/rust/issues/92738
1059+
// In any case, -sSIDE_MODULE=2 is better because Rust is good at
1060+
// calculating exports.
1061+
self.cmd.arg("-sSIDE_MODULE=2");
1062+
// Without -sWASM_BIGINT there are issues with dynamic Rust libraries. There
1063+
// are no plans to fix this in Emscripten AFAIK. See
1064+
// https://github.com/emscripten-core/emscripten/pull/16693 This could also
1065+
// be fixed with panic=abort.
1066+
self.cmd.arg("-sWASM_BIGINT");
1067+
}
1068+
// -fno-pie is the default on Emscripten.
1069+
LinkOutputKind::StaticNoPicExe | LinkOutputKind::StaticPicExe => {}
1070+
LinkOutputKind::WasiReactorExe => {
1071+
unreachable!();
1072+
}
1073+
}
1074+
}
10441075

10451076
fn include_path(&mut self, path: &Path) {
10461077
self.cmd.arg("-L").arg(path);

0 commit comments

Comments
 (0)