Skip to content

Commit b66240a

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 c11207e + b0da01f commit b66240a

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,38 @@ impl<'a> Linker for EmLinker<'a> {
10491049
&mut self.cmd
10501050
}
10511051

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

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

compiler/rustc_target/src/spec/wasm32_unknown_emscripten.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub fn target() -> Target {
1919
pre_link_args,
2020
post_link_args,
2121
relocation_model: RelocModel::Pic,
22+
crt_static_respected: true,
23+
crt_static_default: true,
2224
panic_strategy: PanicStrategy::Unwind,
2325
no_default_libraries: false,
2426
families: cvs!["unix", "wasm"],

0 commit comments

Comments
 (0)