Skip to content

Commit

Permalink
Add component examples built from example core modules
Browse files Browse the repository at this point in the history
Also, specify how to reproduce the wasi-component-adapter binaries.
  • Loading branch information
ereslibre committed Oct 2, 2023
1 parent 6a9a944 commit 08ef159
Show file tree
Hide file tree
Showing 17 changed files with 613 additions and 56 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ jobs:
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Install Wasm32-wasi target
run: rustup target add wasm32-wasi
- name: Install wasm-tools
run: cargo install --locked [email protected]
- name: Build Rust examples
working-directory: ./examples
run: make all
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ exclude = [
"examples/rust-basic",
"examples/rust-fetch",
"examples/rust-kv",
"examples/rust-pdf-create",
"examples/rust-params",
"examples/rust-wasi-nn",
"examples/rust-wasi-nn-preload",
Expand Down
60 changes: 27 additions & 33 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
.PHONY: all rust-* pdf-create
SUBDIRS = rust-basic rust-fetch rust-kv rust-wasi-nn rust-wasi-nn-preload rust-pdf-create
COMPONENTS = $(addprefix components/, $(SUBDIRS))
COMPONENT_TARGETS = components/rust-basic components/rust-kv components/rust-params

rust-basic:
cd rust-basic && \
cargo build --target wasm32-wasi --release && \
cp target/wasm32-wasi/release/rust-basic.wasm ./basic.wasm
all: $(SUBDIRS) rust-params $(COMPONENT_CURRENT_SUPPORTED_TARGETS) components/rust-params

rust-fetch:
cd rust-fetch && \
cargo build --target wasm32-wasi --release && \
cp target/wasm32-wasi/release/rust-fetch.wasm ./index.wasm
.PHONY: $(SUBDIRS) rust-params $(COMPONENT_CURRENT_SUPPORTED_TARGETS) components/rust-params

rust-kv:
cd rust-kv && \
cargo build --target wasm32-wasi --release && \
mv target/wasm32-wasi/release/rust-kv.wasm ./kv.wasm
$(SUBDIRS):
cd $@ && \
cargo build --target wasm32-wasi --release && \
cp target/wasm32-wasi/release/$@.wasm ./$@.wasm

rust-params:
cd rust-params && \
cargo build --target wasm32-wasi --release && \
mv target/wasm32-wasi/release/rust-params.wasm "./[id].wasm"

rust-wasi-nn:
cd rust-wasi-nn && \
cargo build --target wasm32-wasi --release && \
mv target/wasm32-wasi/release/rust-wasi-nn.wasm "./inference.wasm"

rust-wasi-nn-preload:
cd rust-wasi-nn-preload && \
cargo build --target wasm32-wasi --release && \
mv target/wasm32-wasi/release/rust-wasi-nn-preload.wasm "./inference.wasm"

rust-pdf-create:
cd rust-pdf-create && \
cargo build --target wasm32-wasi --release && \
mv target/wasm32-wasi/release/rust-pdf-create.wasm ./index.wasm

all: rust-basic rust-fetch rust-kv rust-params rust-wasi-nn
cd $@ && \
cargo build --target wasm32-wasi --release && \
cp target/wasm32-wasi/release/$@.wasm "./[id].wasm"

$(COMPONENTS):
mkdir -p $@
make $(@:components/%=%)
cp $(@:components/%=%)/*.toml $@/
wasm-tools component new $(@:components/%=%)/$(@:components/%=%).wasm \
--adapt wasi_snapshot_preview1=components/wasi-component-adapter/wasi_snapshot_preview1-command.wasm \
-o $@/$(@:components/%=%).wasm

components/rust-params:
mkdir -p $@
make rust-params
wasm-tools component new rust-params/[id].wasm \
--adapt wasi_snapshot_preview1=components/wasi-component-adapter/wasi_snapshot_preview1-command.wasm \
-o $@/[id].wasm
1 change: 1 addition & 0 deletions examples/components/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/rust-*
6 changes: 6 additions & 0 deletions examples/components/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Component model examples

This directory contains core module examples found in the parent
directory, adapted with the
[`wasi-component-adapter`](./wasi-component-adapter/README.md) so that they are
converted to WebAssembly components.
58 changes: 58 additions & 0 deletions examples/components/wasi-component-adapter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# WASI component adapter

The Bytecode Alliance provides a WASI component adapter that allows a
WASI WebAssembly module to be compiled to a WebAssembly component.

You can reproduce this WASI component adapter by running:

```shell-session
$ git clone [email protected]:bytecodealliance/wasmtime.git
$ cd wasmtime
$ cargo build -p wasi-preview1-component-adapter \
--target wasm32-unknown-unknown --release
```

You can find the WASI component adapter at
`target/wasm32-unknown-unknown/release/wasi_snapshot_preview1.wasm`.

This adapter is valid for a WASI reactor. If you want to build an
adapter for a WASI command, you can run:

```shell-session
$ git clone [email protected]:bytecodealliance/wasmtime.git
$ cd wasmtime
$ cargo build -p wasi-preview1-component-adapter \
--features command \
--no-default-features \
--target wasm32-unknown-unknown --release
```

## Converting a WebAssembly module to a WebAssembly component

In order to convert a WebAssemby module to a WebAssembly component,
you can achieve this goal by using `wasm-tools`, along with the WASI
component adapter you have just built.

You can link to the reactor WASI adapter like so:

```shell-session
$ wasm-tools component new my-wasm-module.wasm \
--adapt wasi_snapshot_preview1=wasi_snapshot_preview1-reactor.wasm \
-o my-wasm-component.wasm
```

Or you can link to the command WASI adapter like the following:

```shell-session
$ wasm-tools component new my-wasm-module.wasm \
--adapt wasi_snapshot_preview1=wasi_snapshot_preview1-command.wasm \
-o my-wasm-component.wasm
```

In both cases, you can check that the component is valid, and output
the WIT definition associated with it, like:

```shell-session
$ wasm-tools validate my-wasm-component.wasm --features component-model
$ wasm-tools component wit component.wasm
```
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions examples/rust-basic/Cargo.lock

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

File renamed without changes.
4 changes: 2 additions & 2 deletions examples/rust-fetch/Cargo.lock

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

Loading

0 comments on commit 08ef159

Please sign in to comment.