rustc_target/spec/targets/
wasm32_wasip1.rs

1//! The `wasm32-wasip1` enables compiling to WebAssembly using the first
2//! version of the WASI standard, called "preview1". This version of the
3//! standard was never formally specified and WASI has since evolved to a
4//! "preview2". This target in rustc uses the previous version of the proposal.
5//!
6//! This target uses the syscalls defined at
7//! <https://github.com/WebAssembly/WASI/tree/main/legacy/preview1>.
8//!
9//! Note that this target was historically called `wasm32-wasi` originally and
10//! was since renamed to `wasm32-wasip1` after the preview2 target was
11//! introduced.
12
13use crate::spec::{
14    Arch, Cc, Env, LinkSelfContainedDefault, LinkerFlavor, Os, Target, TargetMetadata, base,
15    crt_objects,
16};
17
18pub(crate) fn target() -> Target {
19    let mut options = base::wasm::options();
20
21    options.os = Os::Wasi;
22    options.env = Env::P1;
23    options.add_pre_link_args(LinkerFlavor::WasmLld(Cc::Yes), &["--target=wasm32-wasip1"]);
24
25    options.pre_link_objects_self_contained = crt_objects::pre_wasi_self_contained();
26    options.post_link_objects_self_contained = crt_objects::post_wasi_self_contained();
27
28    // FIXME: Figure out cases in which WASM needs to link with a native toolchain.
29    options.link_self_contained = LinkSelfContainedDefault::True;
30
31    // Right now this is a bit of a workaround but we're currently saying that
32    // the target by default has a static crt which we're taking as a signal
33    // for "use the bundled crt". If that's turned off then the system's crt
34    // will be used, but this means that default usage of this target doesn't
35    // need an external compiler but it's still interoperable with an external
36    // compiler if configured correctly.
37    options.crt_static_default = true;
38    options.crt_static_respected = true;
39
40    // Allow `+crt-static` to create a "cdylib" output which is just a wasm file
41    // without a main function.
42    options.crt_static_allows_dylibs = true;
43
44    // WASI's `sys::args::init` function ignores its arguments; instead,
45    // `args::args()` makes the WASI API calls itself.
46    options.main_needs_argc_argv = false;
47
48    // And, WASI mangles the name of "main" to distinguish between different
49    // signatures.
50    options.entry_name = "__main_void".into();
51
52    Target {
53        llvm_target: "wasm32-wasip1".into(),
54        metadata: TargetMetadata {
55            description: Some("WebAssembly with WASI".into()),
56            tier: Some(2),
57            host_tools: Some(false),
58            std: Some(true),
59        },
60        pointer_width: 32,
61        data_layout: "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-i128:128-n32:64-S128-ni:1:10:20".into(),
62        arch: Arch::Wasm32,
63        options,
64    }
65}