Skip to content

Commit

Permalink
Update to latest TinyGo and Rust
Browse files Browse the repository at this point in the history
Signed-off-by: Anuraag Agrawal <[email protected]>
  • Loading branch information
anuraaga committed Jan 21, 2025
1 parent c6e819e commit 703aad2
Show file tree
Hide file tree
Showing 20 changed files with 34 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ on:

env:
EMSDK_VERSION: "3.1.40"
TINYGO_VERSION: "0.33.0"
TINYGO_VERSION: "0.35.0"
ZIG_VERSION: "0.11.0"

concurrency:
Expand Down Expand Up @@ -80,7 +80,7 @@ jobs:
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: wasm32-unknown-unknown, wasm32-wasi
targets: wasm32-unknown-unknown, wasm32-wasip1

- name: Build TinyGo examples
run: make build.examples.tinygo
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defaults:

env: # Update this prior to requiring a higher minor version in go.mod
GO_VERSION: "1.23"
TINYGO_VERSION: "0.33.0"
TINYGO_VERSION: "0.35.0"
ZIG_VERSION: "0.11.0"
BINARYEN_VERSION: "116"
STDLIB_TESTS: "internal/integration_test/stdlibs"
Expand Down
28 changes: 19 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,24 @@ build.examples.as:
build.examples.zig: examples/allocation/zig/testdata/greet.wasm imports/wasi_snapshot_preview1/example/testdata/zig/cat.wasm imports/wasi_snapshot_preview1/testdata/zig/wasi.wasm
@cd internal/testing/dwarftestdata/testdata/zig; zig build; mv zig-out/*/main.wasm ./ # Need DWARF custom sections.

tinygo_sources := examples/basic/testdata/add.go examples/allocation/tinygo/testdata/greet.go examples/cli/testdata/cli.go imports/wasi_snapshot_preview1/example/testdata/tinygo/cat.go imports/wasi_snapshot_preview1/testdata/tinygo/wasi.go cmd/wazero/testdata/cat/cat.go
.PHONY: build.examples.tinygo
build.examples.tinygo: $(tinygo_sources)
tinygo_reactor_sources_reactor := examples/basic/testdata/add.go examples/allocation/tinygo/testdata/greet.go
.PHONY: build.examples.tinygo_reactor
build.examples.tinygo_reactor: $(tinygo_sources_reactor)
@for f in $^; do \
tinygo build -o $$(echo $$f | sed -e 's/\.go/\.wasm/') -scheduler=none --no-debug --target=wasip1 -buildmode=c-shared $$f; \
done

tinygo_sources_clis := examples/cli/testdata/cli.go imports/wasi_snapshot_preview1/example/testdata/tinygo/cat.go imports/wasi_snapshot_preview1/testdata/tinygo/wasi.go cmd/wazero/testdata/cat/cat.go
.PHONY: build.examples.tinygo_clis
build.examples.tinygo_clis: $(tinygo_sources_clis)
@for f in $^; do \
tinygo build -o $$(echo $$f | sed -e 's/\.go/\.wasm/') -scheduler=none --no-debug --target=wasi $$f; \
tinygo build -o $$(echo $$f | sed -e 's/\.go/\.wasm/') -scheduler=none --no-debug --target=wasip1 $$f; \
done
@mv cmd/wazero/testdata/cat/cat.wasm cmd/wazero/testdata/cat/cat-tinygo.wasm

.PHONY: build.examples.tinygo
build.examples.tinygo: build.examples.tinygo_reactor build.examples.tinygo_clis

# We use zig to build C as it is easy to install and embeds a copy of zig-cc.
# Note: Don't use "-Oz" as that breaks our wasi sock example.
c_sources := imports/wasi_snapshot_preview1/example/testdata/zig-cc/cat.c imports/wasi_snapshot_preview1/testdata/zig-cc/wasi.c internal/testing/dwarftestdata/testdata/zig-cc/main.c
Expand Down Expand Up @@ -80,21 +90,21 @@ build.examples.emscripten: $(emscripten_sources)
done

%/greet.wasm : cargo_target := wasm32-unknown-unknown
%/cat.wasm : cargo_target := wasm32-wasi
%/wasi.wasm : cargo_target := wasm32-wasi
%/cat.wasm : cargo_target := wasm32-wasip1
%/wasi.wasm : cargo_target := wasm32-wasip1

.PHONY: build.examples.rust
build.examples.rust: examples/allocation/rust/testdata/greet.wasm imports/wasi_snapshot_preview1/example/testdata/cargo-wasi/cat.wasm imports/wasi_snapshot_preview1/testdata/cargo-wasi/wasi.wasm internal/testing/dwarftestdata/testdata/rust/main.wasm.xz

# Normally, we build release because it is smaller. Testing dwarf requires the debug build.
internal/testing/dwarftestdata/testdata/rust/main.wasm.xz:
cd $(@D) && cargo wasi build
mv $(@D)/target/wasm32-wasi/debug/main.wasm $(@D)
cd $(@D) && cargo build --target wasm32-wasip1
mv $(@D)/target/wasm32-wasip1/debug/main.wasm $(@D)
cd $(@D) && xz -k -f ./main.wasm # Rust's DWARF section is huge, so compress it.

# Builds rust using cargo normally, or cargo-wasi.
%.wasm: %.rs
@(cd $(@D); cargo $(if $(findstring wasi,$(cargo_target)),wasi build,build --target $(cargo_target)) --release)
@(cd $(@D); cargo build --target $(cargo_target) --release)
@mv $(@D)/target/$(cargo_target)/release/$(@F) $(@D)

spectest_base_dir := internal/integration_test/spectest
Expand Down
Binary file modified cmd/wazero/testdata/cat/cat-tinygo.wasm
100755 → 100644
Binary file not shown.
Binary file modified examples/allocation/rust/testdata/greet.wasm
Binary file not shown.
6 changes: 5 additions & 1 deletion examples/allocation/tinygo/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
## TinyGo allocation example

This example shows how to pass strings in and out of a Wasm function defined
in TinyGo, built with `tinygo build -o greet.wasm -scheduler=none -target=wasi greet.go`
in TinyGo, built with

```bash
(cd testdata; tinygo build -scheduler=none -target=wasip1 -buildmode=c-shared -o greet.wasm greet.go)
```

```bash
$ go run greet.go wazero
Expand Down
2 changes: 1 addition & 1 deletion examples/allocation/tinygo/greet.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {

// Instantiate a WebAssembly module that imports the "log" function defined
// in "env" and exports "memory" and functions we'll use in this example.
mod, err := r.Instantiate(ctx, greetWasm)
mod, err := r.InstantiateWithConfig(ctx, greetWasm, wazero.NewModuleConfig().WithStartFunctions("_initialize"))
if err != nil {
log.Panicln(err)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/allocation/tinygo/testdata/greet.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func greeting(name string) string {
// _greet is a WebAssembly export that accepts a string pointer (linear memory
// offset) and calls greet.
//
//export greet
//go:wasmexport greet
func _greet(ptr, size uint32) {
name := ptrToString(ptr, size)
greet(name)
Expand All @@ -50,7 +50,7 @@ func _greet(ptr, size uint32) {
// Note: This uses a uint64 instead of two result values for compatibility with
// WebAssembly 1.0.
//
//export greeting
//go:wasmexport greeting
func _greeting(ptr, size uint32) (ptrSize uint64) {
name := ptrToString(ptr, size)
g := greeting(name)
Expand Down
Binary file modified examples/allocation/tinygo/testdata/greet.wasm
100755 → 100644
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/basic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ typically a `%.wasm` file.
the minimal command to build a `%.wasm` binary.

```bash
cd testdata; tinygo build -o add.wasm -target=wasi add.go
(cd testdata; tinygo build -buildmode=c-shared -target=wasip1 -o add.wasm add.go)
```

### Notes
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {

// Instantiate the guest Wasm into the same runtime. It exports the `add`
// function, implemented in WebAssembly.
mod, err := r.Instantiate(ctx, addWasm)
mod, err := r.InstantiateWithConfig(ctx, addWasm, wazero.NewModuleConfig().WithStartFunctions("_initialize"))
if err != nil {
log.Panicf("failed to instantiate module: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/testdata/add.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

//export add
//go:wasmexport add
func add(x, y uint32) uint32 {
return x + y
}
Expand Down
Binary file modified examples/basic/testdata/add.wasm
100755 → 100644
Binary file not shown.
Binary file modified examples/cli/testdata/cli.wasm
100755 → 100644
Binary file not shown.
Binary file modified imports/wasi_snapshot_preview1/example/testdata/cargo-wasi/cat.wasm
100644 → 100755
Binary file not shown.
Binary file modified imports/wasi_snapshot_preview1/example/testdata/tinygo/cat.wasm
100755 → 100644
Binary file not shown.
Binary file modified imports/wasi_snapshot_preview1/testdata/cargo-wasi/wasi.wasm
100644 → 100755
Binary file not shown.
Binary file modified imports/wasi_snapshot_preview1/testdata/tinygo/wasi.wasm
100755 → 100644
Binary file not shown.
2 changes: 1 addition & 1 deletion internal/integration_test/stdlibs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ build.tinygo:
@mkdir -p $(tinygo_bin)
for value in $(tinygo_tests); do\
echo Building $${value}... ;\
tinygo test -target wasi -c -o $(tinygo_bin)/$$(echo $$value | sed -e 's/\//_/g').test $${value} 2>&1 >/dev/null ;\
tinygo test -target wasip1 -c -o $(tinygo_bin)/$$(echo $$value | sed -e 's/\//_/g').test $${value} 2>&1 >/dev/null ;\
done

.PHONY: build.gowasip1
Expand Down
Binary file modified internal/testing/dwarftestdata/testdata/rust/main.wasm.xz
100644 → 100755
Binary file not shown.

0 comments on commit 703aad2

Please sign in to comment.