Skip to content

Commit

Permalink
Merge pull request #272 from bytecodealliance/ydnar/wasm-tools
Browse files Browse the repository at this point in the history
internal/wasmtools: vendor wasm-tools with WebAssembly
  • Loading branch information
ydnar authored Dec 24, 2024
2 parents 8aa7089 + a411917 commit 2f86230
Show file tree
Hide file tree
Showing 21 changed files with 1,077 additions and 66 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ updates:
schedule:
interval: weekly
open-pull-requests-limit: 10

- package-ecosystem: cargo
directory: "/internal/wasmtools"
schedule:
interval: weekly
open-pull-requests-limit: 10
13 changes: 2 additions & 11 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
paths-ignore:
- '.prettier*'
- '.vscode/**'
- '*.md'
- '**/*.md'
- 'docs/**'
- 'LICENSE'

Expand Down Expand Up @@ -56,11 +56,6 @@ jobs:
with:
go-version: ${{ matrix.go-version }}

- name: Set up wasm-tools
uses: bytecodealliance/actions/wasm-tools/setup@v1
with:
version: ${{ env.wasm-tools-version }}

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

Expand Down Expand Up @@ -100,11 +95,6 @@ jobs:
with:
tinygo-version: ${{ matrix.tinygo-version }}

- name: Set up wasm-tools
uses: bytecodealliance/actions/wasm-tools/setup@v1
with:
version: ${{ env.wasm-tools-version }}

- name: Test with TinyGo
run: tinygo test -v ${{ env.go-modules }}

Expand Down Expand Up @@ -136,6 +126,7 @@ jobs:
with:
tinygo-version: ${{ matrix.tinygo-version }}

# TinyGo needs wasm-tools for -target=wasip2
- name: Set up wasm-tools
uses: bytecodealliance/actions/wasm-tools/setup@v1
with:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store
/generated
/internal/wasmtools/target
/internal/wasmtools/wasm-tools.wasm
go.work.sum
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

## [Unreleased]

### Added

- [`wasm-tools`](https://crates.io/crates/wasm-tools) is now vendored as a WebAssembly module, executed using [Wazero](https://wazero.io/). This allows package `wit` and `wit-bindgen-go` to run on any supported platform without needing to separately install `wasm-tools`.

### Changed

- Dropped support for TinyGo v0.32.0.
Expand Down
17 changes: 16 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ wit_files = $(sort $(shell find testdata -name '*.wit' ! -name '*.golden.*'))
json: $(wit_files)

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

# golden recompiles the .golden.wit test files.
Expand All @@ -21,12 +21,27 @@ generated: clean json
.PHONY: clean
clean:
rm -rf ./generated/*
rm -f internal/wasmtools/wasm-tools.wasm
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

# wasm-tools builds the internal/wasmtools/wasm-tools.wasm.gz artifact
.PHONY: wasm-tools
wasm-tools: internal/wasmtools/target/wasm32-wasip1/release/wasm-tools.wasm
gzip -c $< > internal/wasmtools/wasm-tools.wasm.gz

internal/wasmtools/target/wasm32-wasip1/release/wasm-tools.wasm: internal/wasmtools/Cargo.*
cd internal/wasmtools && \
cargo build --target wasm32-wasip1 --release -p wasm-tools

# internal/wasmtools/wasm-tools.wasm decompresses wasm-tools.wasm.gz for other make targets
internal/wasmtools/wasm-tools.wasm: internal/wasmtools/wasm-tools.wasm.gz
gzip -dc internal/wasmtools/wasm-tools.wasm.gz > $@

# test runs Go and TinyGo tests
GOTESTARGS :=
GOTESTMODULES := ./... ./cm/...
Expand Down
4 changes: 2 additions & 2 deletions cmd/wit-bindgen-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"go.bytecodealliance.org/cmd/wit-bindgen-go/cmd/generate"
"go.bytecodealliance.org/cmd/wit-bindgen-go/cmd/wit"
"go.bytecodealliance.org/internal/witcli"
"go.bytecodealliance.org/internal/module"
)

func main() {
Expand Down Expand Up @@ -64,7 +64,7 @@ var version = &cli.Command{
Name: "version",
Usage: "print the version",
Action: func(ctx context.Context, cmd *cli.Command) error {
fmt.Fprintf(cmd.Writer, "%s version %s\n", cmd.Root().Name, witcli.Version())
fmt.Fprintf(cmd.Writer, "%s version %s\n", cmd.Root().Name, module.Version())
return nil
},
}
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
37 changes: 37 additions & 0 deletions internal/cmd/wasm-tools/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"context"
"fmt"
"io/fs"
"os"

"go.bytecodealliance.org/internal/wasmtools"
)

func main() {
ctx := context.Background()
wasmTools, err := wasmtools.New(ctx)
if err != nil {
exit(err)
}

fsMap := map[string]fs.FS{
"./": os.DirFS("./"),
}

var args []string
if len(os.Args) != 0 {
args = os.Args[1:]
}

err = wasmTools.Run(ctx, os.Stdin, os.Stdout, os.Stderr, fsMap, args...)
if err != nil {
exit(err)
}
}

func exit(err error) {
fmt.Fprintf(os.Stderr, "wasm-tools: %v\n", err)
os.Exit(1)
}
22 changes: 18 additions & 4 deletions internal/witcli/version.go → internal/module/module.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
package witcli
package module

import (
"runtime/debug"
"sync"
)

// Version returns the version string of this module.
// Path returns the path of the main module.
func Path() string {
build := buildInfo()
if build == nil {
return "(none)"
}
return build.Main.Path
}

// Version returns the version string of the main module.
func Version() string {
return versionString()
}

var buildInfo = sync.OnceValue(func() *debug.BuildInfo {
build, _ := debug.ReadBuildInfo()
return build
})

var versionString = sync.OnceValue(func() string {
build, ok := debug.ReadBuildInfo()
if !ok {
build := buildInfo()
if build == nil {
return "(none)"
}
version := build.Main.Version
Expand Down
Loading

0 comments on commit 2f86230

Please sign in to comment.