Skip to content

Commit

Permalink
Rollup merge of rust-lang#98358 - hoodmane:emscripten-dynlib, r=petro…
Browse files Browse the repository at this point in the history
…chenkov

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`
  • Loading branch information
matthiaskrgr committed Jul 26, 2022
2 parents c11207e + b0da01f commit b66240a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 32 additions & 1 deletion compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,38 @@ impl<'a> Linker for EmLinker<'a> {
&mut self.cmd
}

fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
fn set_output_kind(&mut self, output_kind: LinkOutputKind, _out_filename: &Path) {
match output_kind {
LinkOutputKind::DynamicNoPicExe | LinkOutputKind::DynamicPicExe => {
// "-sMAIN_MODULE=1" breaks
// https://github.com/rust-lang/rust/issues/92738
self.cmd.arg("-sMAIN_MODULE=2");
warn!(
"Building dynamic executable with -sMAIN_MODULE=2. \
Dead code elimination may break things. \
See https://emscripten.org/docs/compiling/Dynamic-Linking.html?highlight=main_module#code-size \
"
);
}
LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => {
// -sSIDE_MODULE=1 breaks
// https://github.com/rust-lang/rust/issues/92738
// In any case, -sSIDE_MODULE=2 is better because Rust is good at
// calculating exports.
self.cmd.arg("-sSIDE_MODULE=2");
// Without -sWASM_BIGINT there are issues with dynamic Rust libraries. There
// are no plans to fix this in Emscripten AFAIK. See
// https://github.com/emscripten-core/emscripten/pull/16693 This could also
// be fixed with panic=abort.
self.cmd.arg("-sWASM_BIGINT");
}
// -fno-pie is the default on Emscripten.
LinkOutputKind::StaticNoPicExe | LinkOutputKind::StaticPicExe => {}
LinkOutputKind::WasiReactorExe => {
unreachable!();
}
}
}

fn include_path(&mut self, path: &Path) {
self.cmd.arg("-L").arg(path);
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_target/src/spec/wasm32_unknown_emscripten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub fn target() -> Target {
pre_link_args,
post_link_args,
relocation_model: RelocModel::Pic,
crt_static_respected: true,
crt_static_default: true,
panic_strategy: PanicStrategy::Unwind,
no_default_libraries: false,
families: cvs!["unix", "wasm"],
Expand Down

0 comments on commit b66240a

Please sign in to comment.