Skip to content

Commit

Permalink
Merge pull request #2294 from fermyon/spin-componentize-in-tree
Browse files Browse the repository at this point in the history
  • Loading branch information
rylev authored Feb 26, 2024
2 parents 5e5070e + 3adac40 commit 089daf7
Show file tree
Hide file tree
Showing 67 changed files with 4,772 additions and 104 deletions.
113 changes: 57 additions & 56 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ wasmtime = "18.0.1"
wasmtime-wasi = { version = "18.0.1", features = ["tokio"] }
wasmtime-wasi-http = "18.0.1"

spin-componentize = { git = "https://github.com/fermyon/spin-componentize", rev = "191789170abde10cd55590466c0660dd6c7d472a" }
spin-componentize = { path = "crates/componentize" }

[[bin]]
name = "spin"
Expand Down
30 changes: 30 additions & 0 deletions crates/componentize/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "spin-componentize"
version.workspace = true
authors.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true
rust-version.workspace = true

[dependencies]
anyhow = { workspace = true }
wasmparser = "0.200.0"
wasm-encoder = "0.200.0"
wit-component = "0.200.0"
wit-parser = "0.200.0"

[dev-dependencies]
wasmtime = { workspace = true }
wasmtime-wasi = { workspace = true }
tokio = { version = "1.36.0", features = ["macros", "rt", "fs"] }
async-trait = "0.1.77"
cap-std = "2.0.1"
rand = "0.8.5"
rand_chacha = "0.3.1"
rand_core = "0.6.4"
serde = { version = "1.0.197", features = ["derive"] }
tempfile = "3.10.0"
toml = "0.8.10"
serde_json = "1.0"
30 changes: 30 additions & 0 deletions crates/componentize/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# spin-componentize

This library converts a Spin module to a
[component](https://github.com/WebAssembly/component-model/).

Note that although the world specifies both `inbound-redis` and `inbound-http`
exports, `spin-componentize` will only export either or both according to what
the original module exported.

## Building

This crate requires a [Rust](https://rustup.rs/) installation v1.68 or later and a couple of Wasm targets:

```shell
rustup target add wasm32-wasi
rustup target add wasm32-unknown-unknown
```

Note that this is currently only a library and does not yet have a CLI interface, although that
would be easy to add if desired.

## Testing

To test whether the spin componentize process produces wasm components that can be used with wasmtime, we run "abi conformance" testing. These tests are run with a plain `cargo test` invocation.

## Wit and Adapters

spin-componentize and the abi conformance tests use component adapters built from wasmtime.

See the [adapters README](./adapters/README.md) for more information.
10 changes: 10 additions & 0 deletions crates/componentize/adapters/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Adapters

The componentize process uses adapters to adapt plain wasm modules to wasi preview 2 compatible wasm components. There are three adapters that are built and stored as wasm binaries in this repository:

* The upstream wasi preview1 adapters for both commands and reactors for use with newer versions of wit-bindgen (v0.5 and above).
* These are currently [the wasmtime 18.0.1 release](https://github.com/bytecodealliance/wasmtime/releases/tag/v18.0.1).
* A modified adapter that has knowledge of Spin APIs for use with v0.2 of wit-bindgen which has a different ABI than newer wit-bindgen based modules.
* This is currently built using commit [603fb3e](https://github.com/rylev/wasmtime/commit/603fb3e14fb0eb7468b832711fee5ff7e7ce7012) on the github.com/rylev/wasmtime fork of wasmtime.
* You can see a diff between the upstream wasmtime 18.0.1 compatible adapter and this custom adapter [here](https://github.com/bytecodealliance/wasmtime/compare/release-18.0.0...rylev:wasmtime:v18.0.1-spin).

Binary file not shown.
Binary file not shown.
Binary file not shown.
31 changes: 31 additions & 0 deletions crates/componentize/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::{
env, fs,
path::{Path, PathBuf},
};

fn main() {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let adapters_dir = Path::new("adapters");
std::fs::create_dir_all(out_dir.join("wasm32-unknown-unknown/release")).unwrap();

println!("cargo:rerun-if-changed=adapters/wasi_snapshot_preview1.spin.wasm");
fs::copy(
adapters_dir.join("wasi_snapshot_preview1.spin.wasm"),
out_dir.join("wasm32-unknown-unknown/release/wasi_snapshot_preview1_spin.wasm"),
)
.unwrap();

println!("cargo:rerun-if-changed=adapters/wasi_snapshot_preview1.reactor.wasm");
fs::copy(
adapters_dir.join("wasi_snapshot_preview1.reactor.wasm"),
out_dir.join("wasm32-unknown-unknown/release/wasi_snapshot_preview1_upstream.wasm"),
)
.unwrap();

println!("cargo:rerun-if-changed=adapters/wasi_snapshot_preview1.command.wasm");
fs::copy(
adapters_dir.join("wasi_snapshot_preview1.command.wasm"),
out_dir.join("wasm32-unknown-unknown/release/wasi_snapshot_preview1_command.wasm"),
)
.unwrap();
}
Loading

0 comments on commit 089daf7

Please sign in to comment.