Skip to content

Commit 2f86230

Browse files
authored
Merge pull request #272 from bytecodealliance/ydnar/wasm-tools
internal/wasmtools: vendor wasm-tools with WebAssembly
2 parents 8aa7089 + a411917 commit 2f86230

File tree

21 files changed

+1077
-66
lines changed

21 files changed

+1077
-66
lines changed

.github/dependabot.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ updates:
2323
schedule:
2424
interval: weekly
2525
open-pull-requests-limit: 10
26+
27+
- package-ecosystem: cargo
28+
directory: "/internal/wasmtools"
29+
schedule:
30+
interval: weekly
31+
open-pull-requests-limit: 10

.github/workflows/test.yaml

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
paths-ignore:
99
- '.prettier*'
1010
- '.vscode/**'
11-
- '*.md'
11+
- '**/*.md'
1212
- 'docs/**'
1313
- 'LICENSE'
1414

@@ -56,11 +56,6 @@ jobs:
5656
with:
5757
go-version: ${{ matrix.go-version }}
5858

59-
- name: Set up wasm-tools
60-
uses: bytecodealliance/actions/wasm-tools/setup@v1
61-
with:
62-
version: ${{ env.wasm-tools-version }}
63-
6459
- name: Run Go tests
6560
run: go test -v ${{ env.go-modules }}
6661

@@ -100,11 +95,6 @@ jobs:
10095
with:
10196
tinygo-version: ${{ matrix.tinygo-version }}
10297

103-
- name: Set up wasm-tools
104-
uses: bytecodealliance/actions/wasm-tools/setup@v1
105-
with:
106-
version: ${{ env.wasm-tools-version }}
107-
10898
- name: Test with TinyGo
10999
run: tinygo test -v ${{ env.go-modules }}
110100

@@ -136,6 +126,7 @@ jobs:
136126
with:
137127
tinygo-version: ${{ matrix.tinygo-version }}
138128

129+
# TinyGo needs wasm-tools for -target=wasip2
139130
- name: Set up wasm-tools
140131
uses: bytecodealliance/actions/wasm-tools/setup@v1
141132
with:

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.DS_Store
22
/generated
3+
/internal/wasmtools/target
4+
/internal/wasmtools/wasm-tools.wasm
35
go.work.sum

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
44

55
## [Unreleased]
66

7+
### Added
8+
9+
- [`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`.
10+
711
### Changed
812

913
- Dropped support for TinyGo v0.32.0.

Makefile

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ wit_files = $(sort $(shell find testdata -name '*.wit' ! -name '*.golden.*'))
55
json: $(wit_files)
66

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

1111
# golden recompiles the .golden.wit test files.
@@ -21,12 +21,27 @@ generated: clean json
2121
.PHONY: clean
2222
clean:
2323
rm -rf ./generated/*
24+
rm -f internal/wasmtools/wasm-tools.wasm
25+
rm -f internal/wasmtools/wasm-tools.wasm.gz
2426

2527
# tests/generated writes generated Go code to the tests directory
2628
.PHONY: tests/generated
2729
tests/generated: json
2830
go generate ./tests
2931

32+
# wasm-tools builds the internal/wasmtools/wasm-tools.wasm.gz artifact
33+
.PHONY: wasm-tools
34+
wasm-tools: internal/wasmtools/target/wasm32-wasip1/release/wasm-tools.wasm
35+
gzip -c $< > internal/wasmtools/wasm-tools.wasm.gz
36+
37+
internal/wasmtools/target/wasm32-wasip1/release/wasm-tools.wasm: internal/wasmtools/Cargo.*
38+
cd internal/wasmtools && \
39+
cargo build --target wasm32-wasip1 --release -p wasm-tools
40+
41+
# internal/wasmtools/wasm-tools.wasm decompresses wasm-tools.wasm.gz for other make targets
42+
internal/wasmtools/wasm-tools.wasm: internal/wasmtools/wasm-tools.wasm.gz
43+
gzip -dc internal/wasmtools/wasm-tools.wasm.gz > $@
44+
3045
# test runs Go and TinyGo tests
3146
GOTESTARGS :=
3247
GOTESTMODULES := ./... ./cm/...

cmd/wit-bindgen-go/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
"go.bytecodealliance.org/cmd/wit-bindgen-go/cmd/generate"
1111
"go.bytecodealliance.org/cmd/wit-bindgen-go/cmd/wit"
12-
"go.bytecodealliance.org/internal/witcli"
12+
"go.bytecodealliance.org/internal/module"
1313
)
1414

1515
func main() {
@@ -64,7 +64,7 @@ var version = &cli.Command{
6464
Name: "version",
6565
Usage: "print the version",
6666
Action: func(ctx context.Context, cmd *cli.Command) error {
67-
fmt.Fprintf(cmd.Writer, "%s version %s\n", cmd.Root().Name, witcli.Version())
67+
fmt.Fprintf(cmd.Writer, "%s version %s\n", cmd.Root().Name, module.Version())
6868
return nil
6969
},
7070
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
github.com/klauspost/compress v1.17.11 // indirect
1818
github.com/opencontainers/go-digest v1.0.0 // indirect
1919
github.com/sirupsen/logrus v1.9.3 // indirect
20+
github.com/tetratelabs/wazero v1.8.2 // indirect
2021
github.com/ulikunitz/xz v0.5.12 // indirect
2122
golang.org/x/sync v0.10.0 // indirect
2223
golang.org/x/sys v0.28.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
2929
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
3030
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
3131
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
32+
github.com/tetratelabs/wazero v1.8.2 h1:yIgLR/b2bN31bjxwXHD8a3d+BogigR952csSDdLYEv4=
33+
github.com/tetratelabs/wazero v1.8.2/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs=
3234
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
3335
github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
3436
github.com/urfave/cli/v3 v3.0.0-beta1 h1:6DTaaUarcM0wX7qj5Hcvs+5Dm3dyUTBbEwIWAjcw9Zg=

internal/cmd/wasm-tools/main.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io/fs"
7+
"os"
8+
9+
"go.bytecodealliance.org/internal/wasmtools"
10+
)
11+
12+
func main() {
13+
ctx := context.Background()
14+
wasmTools, err := wasmtools.New(ctx)
15+
if err != nil {
16+
exit(err)
17+
}
18+
19+
fsMap := map[string]fs.FS{
20+
"./": os.DirFS("./"),
21+
}
22+
23+
var args []string
24+
if len(os.Args) != 0 {
25+
args = os.Args[1:]
26+
}
27+
28+
err = wasmTools.Run(ctx, os.Stdin, os.Stdout, os.Stderr, fsMap, args...)
29+
if err != nil {
30+
exit(err)
31+
}
32+
}
33+
34+
func exit(err error) {
35+
fmt.Fprintf(os.Stderr, "wasm-tools: %v\n", err)
36+
os.Exit(1)
37+
}

internal/witcli/version.go renamed to internal/module/module.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
1-
package witcli
1+
package module
22

33
import (
44
"runtime/debug"
55
"sync"
66
)
77

8-
// Version returns the version string of this module.
8+
// Path returns the path of the main module.
9+
func Path() string {
10+
build := buildInfo()
11+
if build == nil {
12+
return "(none)"
13+
}
14+
return build.Main.Path
15+
}
16+
17+
// Version returns the version string of the main module.
918
func Version() string {
1019
return versionString()
1120
}
1221

22+
var buildInfo = sync.OnceValue(func() *debug.BuildInfo {
23+
build, _ := debug.ReadBuildInfo()
24+
return build
25+
})
26+
1327
var versionString = sync.OnceValue(func() string {
14-
build, ok := debug.ReadBuildInfo()
15-
if !ok {
28+
build := buildInfo()
29+
if build == nil {
1630
return "(none)"
1731
}
1832
version := build.Main.Version

0 commit comments

Comments
 (0)