Skip to content

Commit

Permalink
internal/wasmtools: initial commit by @Mossaka
Browse files Browse the repository at this point in the history
all: replace submodule with Cargo-managed wasm-tools CLI

* add wasm-tools as git submodule
* update Makefile to compile wit/wasm-tools.wasm
* use wazero to execute wasm-tools.wasm
* Makefile: add a build command
* Commit the wasm-tools.wasm file
* Makefile: replace path with a variable for the name of the target
* wit, Makefile: move wasmtools.go to internal/wasmtools
* Remove wasmtools submodule

Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]>

internal/wasmtools: optimize for size

Adding lto, opt-level=z and other options to the cargo.toml reduces the wasm-tools.wasm binary size from 7.8 MBi to 3.5 MBi

Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]>

wit/testdata_test: implement code review comment

Signed-off-by: Jiaxiao Zhou <[email protected]>

internal/wasmtools, wit: change the type of fsMap in Executor.Run to map[fs.FS]string

Signed-off-by: Jiaxiao Zhou <[email protected]>

Makefile: change dir to '.'

Signed-off-by: Jiaxiao Zhou <[email protected]>

internal/wasmtools, wit: enhance API, disable TinyGo tests, and improve naming

* Rename to WasmTools to Instance
* Add build tags to disable wasmtools when run under TinyGo
* wasmtools_tinygo: New returns a new error
* Rename Executor to Runner

Signed-off-by: Jiaxiao Zhou <[email protected]>

wit/testdata_test: change t.Skip to t.Log since Skip is not supported in TinyGo

Signed-off-by: Jiaxiao Zhou <[email protected]>

internal/wasmtools: add comments to improve readability for the Instance API

Signed-off-by: Jiaxiao Zhou <[email protected]>

CHANGELOG: Update with wasm-tools integration details

Signed-off-by: Jiaxiao Zhou <[email protected]>

internal/wasmtools, Makefile: add gzip compression for wasm-tools.wasm and update Makefile

This adds gzip compression for the wasm-tools.wasm to further reduce the size to 1.2M from 3.5M.
Added the wasm file to the .gitignore and thus the remote repo will only have the gzip file.
Updated Makefile to zip and unzip the Wasm module.

Signed-off-by: Jiaxiao Zhou <[email protected]>

.github: Add set-up wasm-tools.wasm to the test.yaml and release.yaml

Signed-off-by: Jiaxiao Zhou <[email protected]>
  • Loading branch information
Mossaka authored and ydnar committed Dec 17, 2024
1 parent a0aa3b9 commit 557b289
Show file tree
Hide file tree
Showing 16 changed files with 987 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jobs:
with:
go-version-file: go.mod

- name: Set up wams-tools.wasm
run: make internal/wasmtools/wasm-tools.wasm

- name: Run Go tests
run: go test ${{ env.go-modules }}

Expand Down
13 changes: 12 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ jobs:
with:
go-version-file: go.mod

- name: Set up wams-tools.wasm
run: make internal/wasmtools/wasm-tools.wasm

- name: Vet Go code
run: go vet ${{ env.go-modules }} ./tests/...
run: |
make build
go vet ${{ env.go-modules }} ./tests/...
# Test with Go
test-go:
Expand All @@ -61,6 +66,9 @@ jobs:
with:
version: ${{ env.wasm-tools-version }}

- name: Set up wams-tools.wasm
run: make internal/wasmtools/wasm-tools.wasm

- name: Run Go tests
run: go test -v ${{ env.go-modules }}

Expand Down Expand Up @@ -147,6 +155,9 @@ jobs:
with:
version: ${{ env.wasm-tools-version }}

- name: Set up wams-tools.wasm
run: make internal/wasmtools/wasm-tools.wasm

- name: Set up Wasmtime
uses: bytecodealliance/actions/wasmtime/setup@v1
with:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- `internal/wasmtools` now loads compiled `wasm-tools` Wasm module from the `wasm-tools` crate and executes it using wazero. This allows `wit-bindgen-go` to run on any platform without needing to install `wasm-tools` natively.

## [v0.5.0] — 2024-12-14

### Changed
Expand Down
18 changes: 16 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ wit_files = $(sort $(shell find testdata -name '*.wit' ! -name '*.golden.*'))
json: $(wit_files)

.PHONY: $(wit_files)
$(wit_files):
wasm-tools component wit -j --all-features $@ > $@.json
$(wit_files): internal/wasmtools/wasm-tools.wasm
wasmtime --dir . internal/wasmtools/wasm-tools.wasm component wit -j --all-features $@ > $@.json

# golden recompiles the .golden.wit test files.
.PHONY: golden
Expand All @@ -21,12 +21,26 @@ generated: clean json
.PHONY: clean
clean:
rm -rf ./generated/*
rm -f internal/wasmtools/wasm-tools.wasm.gz

# tests/generated writes generated Go code to the tests directory
.PHONY: tests/generated
tests/generated: json
go generate ./tests

# build builds the cmd/wit-bindgen-go binary
.PHONY: build
build: internal/wasmtools/wasm-tools.wasm
go build -o wit-bindgen-go ./cmd/wit-bindgen-go

internal/wasmtools/wasm-tools.wasm: internal/wasmtools/wasm-tools.wasm.gz
gzip -dc $< > $@

internal/wasmtools/wasm-tools.wasm.gz:
cd internal/wasmtools && \
cargo build --target wasm32-wasip1 --release -p wasm-tools
gzip -c internal/wasmtools/target/wasm32-wasip1/release/wasm-tools.wasm > $@

# test runs Go and TinyGo tests
GOTESTARGS :=
GOTESTMODULES := ./... ./cm/...
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/klauspost/compress v1.17.11 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/tetratelabs/wazero v1.8.2 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tetratelabs/wazero v1.8.2 h1:yIgLR/b2bN31bjxwXHD8a3d+BogigR952csSDdLYEv4=
github.com/tetratelabs/wazero v1.8.2/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs=
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/urfave/cli/v3 v3.0.0-beta1 h1:6DTaaUarcM0wX7qj5Hcvs+5Dm3dyUTBbEwIWAjcw9Zg=
Expand Down
2 changes: 2 additions & 0 deletions internal/wasmtools/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
wasm-tools.wasm
Loading

0 comments on commit 557b289

Please sign in to comment.