]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_target/src/spec/wasm32_unknown_unknown.rs
Merge commit '98e2b9f25b6db4b2680a3d388456d9f95cb28344' into clippyup
[rust.git] / compiler / rustc_target / src / spec / wasm32_unknown_unknown.rs
1 //! A "bare wasm" target representing a WebAssembly output that makes zero
2 //! assumptions about its environment.
3 //!
4 //! The `wasm32-unknown-unknown` target is intended to encapsulate use cases
5 //! that do not rely on any imported functionality. The binaries generated are
6 //! entirely self-contained by default when using the standard library. Although
7 //! the standard library is available, most of it returns an error immediately
8 //! (e.g. trying to create a TCP stream or something like that).
9 //!
10 //! This target is more or less managed by the Rust and WebAssembly Working
11 //! Group nowadays at <https://github.com/rustwasm>.
12
13 use super::wasm_base;
14 use super::{LinkerFlavor, LldFlavor, Target};
15 use crate::spec::abi::Abi;
16
17 pub fn target() -> Target {
18     let mut options = wasm_base::options();
19     options.os = "unknown".to_string();
20     options.linker_flavor = LinkerFlavor::Lld(LldFlavor::Wasm);
21
22     // This is a default for backwards-compatibility with the original
23     // definition of this target oh-so-long-ago. Once the "wasm" ABI is
24     // stable and the wasm-bindgen project has switched to using it then there's
25     // no need for this and it can be removed.
26     //
27     // Currently this is the reason that this target's ABI is mismatched with
28     // clang's ABI. This means that, in the limit, you can't merge C and Rust
29     // code on this target due to this ABI mismatch.
30     options.default_adjusted_cabi = Some(Abi::Wasm);
31
32     let clang_args = options.pre_link_args.entry(LinkerFlavor::Gcc).or_default();
33
34     // Make sure clang uses LLD as its linker and is configured appropriately
35     // otherwise
36     clang_args.push("--target=wasm32-unknown-unknown".to_string());
37
38     // For now this target just never has an entry symbol no matter the output
39     // type, so unconditionally pass this.
40     clang_args.push("-Wl,--no-entry".to_string());
41
42     // Rust really needs a way for users to specify exports and imports in
43     // the source code. --export-dynamic isn't the right tool for this job,
44     // however it does have the side effect of automatically exporting a lot
45     // of symbols, which approximates what people want when compiling for
46     // wasm32-unknown-unknown expect, so use it for now.
47     clang_args.push("-Wl,--export-dynamic".to_string());
48
49     // Add the flags to wasm-ld's args too.
50     let lld_args = options.pre_link_args.entry(LinkerFlavor::Lld(LldFlavor::Wasm)).or_default();
51     lld_args.push("--no-entry".to_string());
52     lld_args.push("--export-dynamic".to_string());
53
54     Target {
55         llvm_target: "wasm32-unknown-unknown".to_string(),
56         pointer_width: 32,
57         data_layout: "e-m:e-p:32:32-i64:64-n32:64-S128".to_string(),
58         arch: "wasm32".to_string(),
59         options,
60     }
61 }