Skip to main content

rustc_target/spec/targets/
wasm32_unknown_emscripten.rs

1use crate::spec::{
2    Arch, LinkArgs, LinkerFlavor, Os, PanicStrategy, RelocModel, Target, TargetMetadata,
3    TargetOptions, base, cvs,
4};
5
6pub(crate) fn target() -> Target {
7    // Reset flags for non-Em flavors back to empty to satisfy sanity checking tests.
8    let pre_link_args = LinkArgs::new();
9    let post_link_args = TargetOptions::link_args(LinkerFlavor::EmCc, &["-sABORTING_MALLOC=0"]);
10
11    let opts = TargetOptions {
12        os: Os::Emscripten,
13        linker_flavor: LinkerFlavor::EmCc,
14        // emcc emits two files - a .js file to instantiate the wasm and supply platform
15        // functionality, and a .wasm file.
16        exe_suffix: ".js".into(),
17        linker: None,
18        pre_link_args,
19        post_link_args,
20        relocation_model: RelocModel::Pic,
21        // crt_static should always be true for an executable and always false
22        // for a shared library. There is no easy way to indicate this and it
23        // doesn't seem to matter much so we set crt_static_allows_dylibs to
24        // true and leave crt_static as true when linking dynamic libraries.
25        // wasi also sets crt_static_allows_dylibs: true so this is at least
26        // aligned between wasm targets.
27        crt_static_respected: true,
28        crt_static_default: true,
29        crt_static_allows_dylibs: true,
30        main_needs_argc_argv: true,
31        // Use the wasm C-ABI entry name from the tool-conventions BasicCABI
32        // spec rather than a raw `main`, as referenced by emscripten's crt/libc.
33        // Required for entry paths like `-sPROXY_TO_PTHREAD`, whose
34        // `crt1_proxy_main` links against `__main_argc_argv`.
35        entry_name: "__main_argc_argv".into(),
36        panic_strategy: PanicStrategy::Unwind,
37        no_default_libraries: false,
38        families: cvs!["unix", "wasm"],
39        // Explicitly override the `base::wasm`'s `llvm_args` back to empty. The
40        // base is to force using the most standard exception-handling
41        // instructions, when enabled, but this target is intended to follow
42        // Emscripten, which is whatever LLVM defaults to.
43        llvm_args: cvs![],
44        ..base::wasm::options()
45    };
46    Target {
47        llvm_target: "wasm32-unknown-emscripten".into(),
48        metadata: TargetMetadata {
49            description: Some("WebAssembly via Emscripten".into()),
50            tier: Some(2),
51            host_tools: Some(false),
52            std: Some(true),
53        },
54        pointer_width: 32,
55        data_layout: "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-i128:128-f128:64-n32:64-S128-ni:1:10:20"
56            .into(),
57        arch: Arch::Wasm32,
58        options: opts,
59    }
60}