Skip to content

Migrate from cargo component to the new wasm32-wasip2 target. #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 1 addition & 20 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,7 @@ publish = false
[lib]
crate-type = ["cdylib"]

[package.metadata.component]
package = "component:hello-wasi-http"
# This tells cargo-component to use a special adapter, which doesn't depend on
# `get-environment` or other things not present in the proxy world.
proxy = true

[package.metadata.component.dependencies]

[package.metadata.component.target]

[package.metadata.component.target.dependencies]
"wasi:http" = { path = "wit/deps/http" }
"wasi:clocks" = { path = "wit/deps/clocks" }
"wasi:io" = { path = "wit/deps/io" }
"wasi:random" = { path = "wit/deps/random" }
"wasi:cli" = { path = "wit/deps/cli" }
"wasi:filesystem" = { path = "wit/deps/filesystem" }
"wasi:sockets" = { path = "wit/deps/sockets" }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
wit-bindgen-rt = { version = "0.35.0", features = ["bitflags"] }
wasi = "0.13.3"
61 changes: 10 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ So without further ado...

## Let's go!

First, [install `cargo-component`](https://github.com/bytecodealliance/cargo-component#requirements) (version 0.13.2 or later). `cargo-component` is a tool for building Wasm components implemented in Rust. For more
information on building Wasm components from different languages, check [here]!
First, [install Rust](https://www.rust-lang.org/learn/get-started) (version 1.82 or later).
For more information on building Wasm components from different languages, check [here]!

[here]: https://component-model.bytecodealliance.org/language-support.html

With that, build the Wasm component from source in this repository:
```sh
$ cargo component build
Compiling hello-wasi-http v0.0.0 (/home/wasm/hello-wasi-http)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 2.01s
Creating component target/wasm32-wasip1/debug/hello_wasi_http.wasm
$ RUSTFLAGS="-Clink-arg=--wasi-adapter=proxy" cargo build --target=wasm32-wasip2
Compiling wit-bindgen-rt v0.35.0
Compiling bitflags v2.6.0
Compiling hello-wasi-http v0.0.0 (/home/dev/hello-wasi-http)
Finished `dev` profile [unoptimized + debuginfo] target(s)
```

This builds a Wasm component at `target/wasm32-wasip1/debug/hello_wasi_http.wasm`.
This builds a Wasm component at `target/wasm32-wasip2/debug/hello_wasi_http.wasm`.

To run it, we'll need at least Wasmtime `18.0`. Installation instructions are on [wasmtime.dev]. For example, on Linux or macOS, use the command below:

Expand All @@ -37,7 +38,7 @@ $ curl https://wasmtime.dev/install.sh -sSf | bash

Then, run in your terminal:
```sh
$ cargo component serve
$ wasmtime serve target/wasm32-wasip2/debug/hello_wasi_http.wasm
```
This starts up an HTTP server that, by default, listens on `0.0.0.0:8080`.

Expand All @@ -49,7 +50,7 @@ Hello, wasi:http/proxy world!

## Optimizing!

The above uses a `debug` build. To make a component that runs faster, build with `cargo component build --release`.
The above uses a `debug` build. To make a component that runs faster, run `cargo` with the `--release` flag.

It's also worth making sure you have a release build of Wasmtime; if you installed it from the instructions above
with wasmtime.dev, you're good.
Expand Down Expand Up @@ -83,46 +84,4 @@ If you're interested in tutorials for any of these options, please reach out and
[proxy]: https://github.com/WebAssembly/wasi-http/blob/main/wit/proxy.wit
[wasi-cloud-core]: https://github.com/WebAssembly/wasi-cloud-core

## Creating this repo

Here are my notes on how I created this repository, in case you're interested in recreating it.

To create a new project, run:

```sh
$ cargo component new --proxy --lib hello-wasi-http
Created binary (application) `hello-wasi-http` package
Updated manifest of package `hello-wasi-http`
Generated source file `src/main.rs`
$ cd hello-wasi-http
```

Copy the `wit` directory from your version of Wasmtime, to ensure that we're using the same version of the API that
Wasmtime is built with (e.g., for Wasmtime 18.0.0: https://github.com/bytecodealliance/wasmtime/tree/release-18.0.0).

Then, I manually trimmed the filesystem and sockets dependencies out.

In the future, we'll have wit dependencies stored in a registry, which will make this all much easier.

I derived `src/lib.rs` from Wasmtime's `crates/test-programs/src/bin/api_proxy.rs` contents on the `main` branch,
adapted it to work with cargo component, in particular by adding:

```rust
cargo_component_bindings::generate!();
```

Then, I renamed the `T` type to `Component`, which the bindings expect.

Finally, add dependencies:
```
$ cargo component add --target --path wit/deps/clocks wasi:clocks
$ cargo component add --target --path wit/deps/io wasi:io
$ cargo component add --target --path wit/deps/random wasi:random
$ cargo component add --target --path wit/deps/cli wasi:cli
$ cargo component add --target --path wit/deps/logging wasi:logging
```

These don't all actually get used in this tutorial, but they're currently needed because of some of the interfaces we
copied in from the Wasmtime tree reference them.

> TODO: I should also make a `api_proxy_streaming.rs` version to show streaming.
Loading