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::{
20 LinkSelfContainedDefault, RelocModel, Target, TargetMetadata, base, crt_objects,
21};
22
23pub(crate) fn target() -> Target {
24 let mut options = base::wasm::options();
25
26 options.os = "wasi".into();
27 options.env = "p2".into();
28 options.linker = Some("wasm-component-ld".into());
29
30 options.pre_link_objects_self_contained = crt_objects::pre_wasi_self_contained();
31 options.post_link_objects_self_contained = crt_objects::post_wasi_self_contained();
32
33 // FIXME: Figure out cases in which WASM needs to link with a native toolchain.
34 options.link_self_contained = LinkSelfContainedDefault::True;
35
36 // Right now this is a bit of a workaround but we're currently saying that
37 // the target by default has a static crt which we're taking as a signal
38 // for "use the bundled crt". If that's turned off then the system's crt
39 // will be used, but this means that default usage of this target doesn't
40 // need an external compiler but it's still interoperable with an external
41 // compiler if configured correctly.
42 options.crt_static_default = true;
43 options.crt_static_respected = true;
44
45 // Allow `+crt-static` to create a "cdylib" output which is just a wasm file
46 // without a main function.
47 options.crt_static_allows_dylibs = true;
48
49 // WASI's `sys::args::init` function ignores its arguments; instead,
50 // `args::args()` makes the WASI API calls itself.
51 options.main_needs_argc_argv = false;
52
53 // And, WASI mangles the name of "main" to distinguish between different
54 // signatures.
55 options.entry_name = "__main_void".into();
56
57 // Default to PIC unlike base wasm. This makes precompiled objects such as
58 // the standard library more suitable to be used with shared libraries a la
59 // emscripten's dynamic linking convention.
60 options.relocation_model = RelocModel::Pic;
61
62 Target {
63 llvm_target: "wasm32-wasip2".into(),
64 metadata: TargetMetadata {
65 description: Some("WebAssembly".into()),
66 tier: Some(3),
67 host_tools: Some(false),
68 std: Some(true),
69 },
70 pointer_width: 32,
71 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(),
72 arch: "wasm32".into(),
73 options,
74 }
75}