Skip to content

Commit a545f17

Browse files
aykevldeadprogram
authored andcommitted
wasm: add support for GOOS=wasip1
This adds true GOOS=wasip1 support in addition to our existing -target=wasi support. The old support for WASI isn't removed, but should be treated as deprecated and will likely be removed eventually to reduce the test burden.
1 parent f4375d0 commit a545f17

40 files changed

+99
-38
lines changed

.github/workflows/linux.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ jobs:
153153
tar -C ~/lib -xf tinygo.linux-amd64.tar.gz
154154
ln -s ~/lib/tinygo/bin/tinygo ~/go/bin/tinygo
155155
- run: make tinygo-test-wasi-fast
156+
- run: make tinygo-test-wasip1-fast
156157
- run: make smoketest
157158
assert-test-linux:
158159
# Run all tests that can run on Linux, with LLVM assertions enabled to catch

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,12 @@ tinygo-bench-fast:
417417
# Same thing, except for wasi rather than the current platform.
418418
tinygo-test-wasi:
419419
$(TINYGO) test -target wasi $(TEST_PACKAGES_FAST) $(TEST_PACKAGES_SLOW) ./tests/runtime_wasi
420+
tinygo-test-wasip1:
421+
GOOS=wasip1 GOARCH=wasm $(TINYGO) test $(TEST_PACKAGES_FAST) $(TEST_PACKAGES_SLOW) ./tests/runtime_wasi
420422
tinygo-test-wasi-fast:
421423
$(TINYGO) test -target wasi $(TEST_PACKAGES_FAST) ./tests/runtime_wasi
424+
tinygo-test-wasip1-fast:
425+
GOOS=wasip1 GOARCH=wasm $(TINYGO) test $(TEST_PACKAGES_FAST) ./tests/runtime_wasi
422426
tinygo-bench-wasi:
423427
$(TINYGO) test -target wasi -bench . $(TEST_PACKAGES_FAST) $(TEST_PACKAGES_SLOW)
424428
tinygo-bench-wasi-fast:

builder/builder_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func TestClangAttributes(t *testing.T) {
6060
{GOOS: "darwin", GOARCH: "arm64"},
6161
{GOOS: "windows", GOARCH: "amd64"},
6262
{GOOS: "windows", GOARCH: "arm64"},
63+
{GOOS: "wasip1", GOARCH: "wasm"},
6364
} {
6465
name := "GOOS=" + options.GOOS + ",GOARCH=" + options.GOARCH
6566
if options.GOARCH == "arm" {

compileopts/target.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,15 @@ func LoadTarget(options *Options) (*TargetSpec, error) {
191191
default:
192192
return nil, fmt.Errorf("invalid GOARM=%s, must be 5, 6, or 7", options.GOARM)
193193
}
194+
case "wasm":
195+
llvmarch = "wasm32"
194196
default:
195197
llvmarch = options.GOARCH
196198
}
197199
llvmvendor := "unknown"
198200
llvmos := options.GOOS
199-
if llvmos == "darwin" {
201+
switch llvmos {
202+
case "darwin":
200203
// Use macosx* instead of darwin, otherwise darwin/arm64 will refer
201204
// to iOS!
202205
llvmos = "macosx10.12.0"
@@ -207,6 +210,8 @@ func LoadTarget(options *Options) (*TargetSpec, error) {
207210
llvmos = "macosx11.0.0"
208211
}
209212
llvmvendor = "apple"
213+
case "wasip1":
214+
llvmos = "wasi"
210215
}
211216
// Target triples (which actually have four components, but are called
212217
// triples for historical reasons) have the form:
@@ -277,6 +282,15 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) {
277282
case "arm64":
278283
spec.CPU = "generic"
279284
spec.Features = "+neon"
285+
case "wasm":
286+
spec.CPU = "generic"
287+
spec.Features = "+bulk-memory,+nontrapping-fptoint,+sign-ext"
288+
spec.BuildTags = append(spec.BuildTags, "tinygo.wasm")
289+
spec.CFlags = append(spec.CFlags,
290+
"-mbulk-memory",
291+
"-mnontrapping-fptoint",
292+
"-msign-ext",
293+
)
280294
}
281295
if goos == "darwin" {
282296
spec.Linker = "ld.lld"
@@ -320,6 +334,22 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) {
320334
"--no-insert-timestamp",
321335
"--no-dynamicbase",
322336
)
337+
} else if goos == "wasip1" {
338+
spec.GC = "" // use default GC
339+
spec.Scheduler = "asyncify"
340+
spec.Linker = "wasm-ld"
341+
spec.RTLib = "compiler-rt"
342+
spec.Libc = "wasi-libc"
343+
spec.DefaultStackSize = 1024 * 16 // 16kB
344+
spec.LDFlags = append(spec.LDFlags,
345+
"--stack-first",
346+
"--no-demangle",
347+
)
348+
spec.Emulator = "wasmtime --mapdir=/tmp::{tmpDir} {}"
349+
spec.ExtraFiles = append(spec.ExtraFiles,
350+
"src/runtime/asm_tinygowasm.S",
351+
"src/internal/task/task_asyncify_wasm.S",
352+
)
323353
} else {
324354
spec.LDFlags = append(spec.LDFlags, "-no-pie", "-Wl,--gc-sections") // WARNING: clang < 5.0 requires -nopie
325355
}

main_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ var supportedLinuxArches = map[string]string{
3434
"X86Linux": "linux/386",
3535
"ARMLinux": "linux/arm/6",
3636
"ARM64Linux": "linux/arm64",
37+
"WASIp1": "wasip1/wasm",
38+
}
39+
40+
func init() {
41+
major, _, _ := goenv.GetGorootVersion()
42+
if major < 21 {
43+
// Go 1.20 backwards compatibility.
44+
// Should be removed once we drop support for Go 1.20.
45+
delete(supportedLinuxArches, "WASIp1")
46+
}
3747
}
3848

3949
var sema = make(chan struct{}, runtime.NumCPU())
@@ -176,6 +186,8 @@ func runPlatTests(options compileopts.Options, tests []string, t *testing.T) {
176186
t.Fatal("failed to load target spec:", err)
177187
}
178188

189+
isWebAssembly := options.Target == "wasi" || options.Target == "wasm" || (options.Target == "" && options.GOARCH == "wasm")
190+
179191
for _, name := range tests {
180192
if options.GOOS == "linux" && (options.GOARCH == "arm" || options.GOARCH == "386") {
181193
switch name {
@@ -226,7 +238,7 @@ func runPlatTests(options compileopts.Options, tests []string, t *testing.T) {
226238
runTest("env.go", options, t, []string{"first", "second"}, []string{"ENV1=VALUE1", "ENV2=VALUE2"})
227239
})
228240
}
229-
if options.Target == "wasi" || options.Target == "wasm" {
241+
if isWebAssembly {
230242
t.Run("alias.go-scheduler-none", func(t *testing.T) {
231243
t.Parallel()
232244
options := compileopts.Options(options)
@@ -246,7 +258,7 @@ func runPlatTests(options compileopts.Options, tests []string, t *testing.T) {
246258
runTest("rand.go", options, t, nil, nil)
247259
})
248260
}
249-
if options.Target != "wasi" && options.Target != "wasm" {
261+
if !isWebAssembly {
250262
// The recover() builtin isn't supported yet on WebAssembly and Windows.
251263
t.Run("recover.go", func(t *testing.T) {
252264
t.Parallel()

src/os/dir_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build linux && !baremetal && !wasi
5+
//go:build linux && !baremetal && !wasi && !wasip1
66

77
package os
88

src/os/dir_wasi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// fairly similar: we use fdopendir, fdclosedir, and readdir from wasi-libc in
77
// a similar way that the darwin code uses functions from libc.
88

9-
//go:build wasi
9+
//go:build wasi || wasip1
1010

1111
package os
1212

src/os/env_unix_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build darwin || linux
5+
//go:build darwin || linux || wasip1
66

77
package os_test
88

src/os/exec_posix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build aix || darwin || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris || windows
5+
//go:build aix || darwin || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris || wasip1 || windows
66

77
package os
88

src/os/file_other.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build baremetal || (wasm && !wasi)
1+
//go:build baremetal || (wasm && !wasi && !wasip1)
22

33
package os
44

0 commit comments

Comments
 (0)