Skip to content

Commit

Permalink
Target wasm32-wasi by default.
Browse files Browse the repository at this point in the history
This commit makes `cargo component` target `wasm32-wasi` by default, using a
local preview1 adapter to adapt to a preview2 version of WASI.

Closes bytecodealliance#48.
  • Loading branch information
peterhuene committed Mar 2, 2023
1 parent c38fd44 commit f2571c7
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Install Rust
run: rustup update stable --no-self-update && rustup default stable && rustup target add wasm32-unknown-unknown
run: rustup update stable --no-self-update && rustup default stable && rustup target add wasm32-wasi && rustup target add wasm32-unknown-unknown
shell: bash
- name: Install Protobuf Compiler
uses: arduino/setup-protoc@v1
Expand All @@ -33,7 +33,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Install Rust
run: rustup update stable --no-self-update && rustup default stable && rustup target add wasm32-unknown-unknown
run: rustup update stable --no-self-update && rustup default stable && rustup target add wasm32-wasi && rustup target add wasm32-unknown-unknown
- name: Install Protobuf Compiler
uses: arduino/setup-protoc@v1
with:
Expand Down
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,25 @@ and tooling developers can use to test their implementations.

## WASI Support

The current WASI preview does not yet support the WebAssembly component model.
Currently `cargo component` targets `wasm32-wasi` by default.

For that reason, `cargo component` currently targets `wasm32-unknown-unknown`
by default.
As this target is for a _preview1_ release of WASI, the WebAssembly module
produced by the Rust compiler must be adapted to the _preview2_ version of WASI
supported by the component model.

Once a WASI preview is available that [supports the component model][1], `cargo component`
will target WASI by default at that time.
The adaptation is automatically performed when `wasm32-wasi` is targeted.

[1]: https://github.com/WebAssembly/meetings/blob/main/wasi/2022/presentations/2022-06-30-gohman-wasi-preview2.pdf
To prevent this, override the target to `wasm32-unknown-unknown` using the
`--target` option when building. This, however, will disable WASI support.

Use the _preview2_ version of [`wasi-common`][2] in your host to run components
produced by `cargo component`.

When the Rust compiler supports a [_preview2_ version of the WASI target][1],
support in `cargo component` for adapting a _preview1_ module will be removed.

[1]: https://github.com/rust-lang/compiler-team/issues/594
[2]: https://github.com/bytecodealliance/preview2-prototyping/tree/main/wasi-common

## Installation

Expand Down
2 changes: 1 addition & 1 deletion example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ To build the component, run the following command:
cargo component build
```

The component should now exist at `target/wasm32-unknown-unknown/debug/service.wasm`.
The component should now exist at `target/wasm32-wasi/debug/service.wasm`.

The resulting component will have the following imports:

Expand Down
4 changes: 2 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ impl CompileOptions {
let spec = Packages::from_flags(self.workspace, self.exclude, self.packages)?;

if self.targets.is_empty() {
target::install_wasm32_unknown_unknown()?;
self.targets.push("wasm32-unknown-unknown".to_string());
target::install_wasm32_wasi()?;
self.targets.push("wasm32-wasi".to_string());
}

log::debug!("using targets {:#?}", self.targets);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub struct BuildCommand {
#[clap(long = "no-default-features")]
pub no_default_features: bool,

/// Build for the target triple (defaults to `wasm32-unknown-unknown`)
/// Build for the target triple (defaults to `wasm32-wasi`)
#[clap(long = "target", value_name = "TRIPLE")]
pub targets: Vec<String>,

Expand Down
2 changes: 1 addition & 1 deletion src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub struct CheckCommand {
#[clap(long = "no-default-features")]
pub no_default_features: bool,

/// Check for the target triple (defaults to `wasm32-unknown-unknown`)
/// Check for the target triple (defaults to `wasm32-wasi`)
#[clap(long = "target", value_name = "TRIPLE")]
pub targets: Vec<String>,

Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,13 @@ fn create_component(config: &Config, target_path: &Path) -> Result<()> {
format!("component {path}", path = target_path.display()),
)?;

let encoder = ComponentEncoder::default().module(&module)?.validate(true);
let encoder = ComponentEncoder::default()
.adapter(
"wasi_snapshot_preview1",
include_bytes!("../adapters/wasi_snapshot_preview1.wasm"),
)?
.module(&module)?
.validate(true);

let mut producers = wasm_metadata::Producers::empty();
producers.add(
Expand Down
12 changes: 6 additions & 6 deletions src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ use std::{
process::{Command, Stdio},
};

pub fn install_wasm32_unknown_unknown() -> Result<()> {
pub fn install_wasm32_wasi() -> Result<()> {
let sysroot = get_sysroot()?;
if sysroot.join("lib/rustlib/wasm32-unknown-unknown").exists() {
if sysroot.join("lib/rustlib/wasm32-wasi").exists() {
return Ok(());
}

if env::var_os("RUSTUP_TOOLCHAIN").is_none() {
bail!(
"failed to find the `wasm32-unknown-unknown` target \
"failed to find the `wasm32-wasi` target \
and `rustup` is not available. If you're using rustup \
make sure that it's correctly installed; if not, make sure to \
install the `wasm32-unknown-unknown` target before using this command"
install the `wasm32-wasi` target before using this command"
);
}

let output = Command::new("rustup")
.arg("target")
.arg("add")
.arg("wasm32-unknown-unknown")
.arg("wasm32-wasi")
.stderr(Stdio::inherit())
.stdout(Stdio::inherit())
.output()?;

if !output.status.success() {
bail!("failed to install the `wasm32-unknown-unknown` target");
bail!("failed to install the `wasm32-wasi` target");
}

Ok(())
Expand Down
20 changes: 20 additions & 0 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,23 @@ fn it_adds_a_producers_field() -> Result<()> {

Ok(())
}

#[test]
fn it_builds_wasm32_unknown_unknown() -> Result<()> {
let project = Project::new("foo")?;
project
.cargo_component("build --target wasm32-unknown-unknown")
.assert()
.stderr(contains("Finished dev [unoptimized + debuginfo] target(s)"))
.success();

validate_component(
&project
.build_dir()
.join("wasm32-unknown-unknown")
.join("debug")
.join("foo.wasm"),
)?;

Ok(())
}
4 changes: 2 additions & 2 deletions tests/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ impl Project {

pub fn debug_wasm(&self, name: &str) -> PathBuf {
self.build_dir()
.join("wasm32-unknown-unknown")
.join("wasm32-wasi")
.join("debug")
.join(format!("{name}.wasm"))
}

pub fn release_wasm(&self, name: &str) -> PathBuf {
self.build_dir()
.join("wasm32-unknown-unknown")
.join("wasm32-wasi")
.join("release")
.join(format!("{name}.wasm"))
}
Expand Down

0 comments on commit f2571c7

Please sign in to comment.