rustc_target/spec/targets/wasm32_wasip2.rs
1//! The `wasm32-wasip2` target is the next evolution of the
2//! wasm32-wasip1 target. While the wasi specification is still under
3//! active development, the preview 2 iteration is considered an "island
4//! of stability" that should allow users to rely on it indefinitely.
5//!
6//! The `wasi` target is a proposal to define a standardized set of WebAssembly
7//! component imports that allow it to interoperate with the host system in a
8//! standardized way. This set of imports is intended to empower WebAssembly
9//! binaries with host capabilities such as filesystem access, network access, etc.
10//!
11//! Wasi Preview 2 relies on the WebAssembly component model which is an extension of
12//! the core WebAssembly specification which allows interoperability between WebAssembly
13//! modules (known as "components") through high-level, shared-nothing APIs instead of the
14//! low-level, shared-everything linear memory model of the core WebAssembly specification.
15//!
16//! You can see more about wasi at <https://wasi.dev> and the component model at
17//! <https://github.com/WebAssembly/component-model>.
18
19use crate::spec::{LinkSelfContainedDefault, RelocModel, Target, base, crt_objects};
20
21pub(crate) fn target() -> Target {
22 let mut options = base::wasm::options();
23
24 options.os = "wasi".into();
25 options.env = "p2".into();
26 options.linker = Some("wasm-component-ld".into());
27
28 options.pre_link_objects_self_contained = crt_objects::pre_wasi_self_contained();
29 options.post_link_objects_self_contained = crt_objects::post_wasi_self_contained();
30
31 // FIXME: Figure out cases in which WASM needs to link with a native toolchain.
32 options.link_self_contained = LinkSelfContainedDefault::True;
33
34 // Right now this is a bit of a workaround but we're currently saying that
35 // the target by default has a static crt which we're taking as a signal
36 // for "use the bundled crt". If that's turned off then the system's crt
37 // will be used, but this means that default usage of this target doesn't
38 // need an external compiler but it's still interoperable with an external
39 // compiler if configured correctly.
40 options.crt_static_default = true;
41 options.crt_static_respected = true;
42
43 // Allow `+crt-static` to create a "cdylib" output which is just a wasm file
44 // without a main function.
45 options.crt_static_allows_dylibs = true;
46
47 // WASI's `sys::args::init` function ignores its arguments; instead,
48 // `args::args()` makes the WASI API calls itself.
49 options.main_needs_argc_argv = false;
50
51 // And, WASI mangles the name of "main" to distinguish between different
52 // signatures.
53 options.entry_name = "__main_void".into();
54
55 // Default to PIC unlike base wasm. This makes precompiled objects such as
56 // the standard library more suitable to be used with shared libraries a la
57 // emscripten's dynamic linking convention.
58 options.relocation_model = RelocModel::Pic;
59
60 Target {
61 llvm_target: "wasm32-wasip2".into(),
62 metadata: crate::spec::TargetMetadata {
63 description: Some("WebAssembly".into()),
64 tier: Some(3),
65 host_tools: Some(false),
66 std: Some(true),
67 },
68 pointer_width: 32,
69 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(),
70 arch: "wasm32".into(),
71 options,
72 }
73}