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.
Этот коммит содержится в:
Ayke van Laethem 2023-08-10 22:36:35 +02:00 коммит произвёл Ron Evans
родитель f4375d0452
коммит a545f17d2e
40 изменённых файлов: 99 добавлений и 38 удалений

1
.github/workflows/linux.yml предоставленный
Просмотреть файл

@ -153,6 +153,7 @@ jobs:
tar -C ~/lib -xf tinygo.linux-amd64.tar.gz
ln -s ~/lib/tinygo/bin/tinygo ~/go/bin/tinygo
- run: make tinygo-test-wasi-fast
- run: make tinygo-test-wasip1-fast
- run: make smoketest
assert-test-linux:
# Run all tests that can run on Linux, with LLVM assertions enabled to catch

Просмотреть файл

@ -417,8 +417,12 @@ tinygo-bench-fast:
# Same thing, except for wasi rather than the current platform.
tinygo-test-wasi:
$(TINYGO) test -target wasi $(TEST_PACKAGES_FAST) $(TEST_PACKAGES_SLOW) ./tests/runtime_wasi
tinygo-test-wasip1:
GOOS=wasip1 GOARCH=wasm $(TINYGO) test $(TEST_PACKAGES_FAST) $(TEST_PACKAGES_SLOW) ./tests/runtime_wasi
tinygo-test-wasi-fast:
$(TINYGO) test -target wasi $(TEST_PACKAGES_FAST) ./tests/runtime_wasi
tinygo-test-wasip1-fast:
GOOS=wasip1 GOARCH=wasm $(TINYGO) test $(TEST_PACKAGES_FAST) ./tests/runtime_wasi
tinygo-bench-wasi:
$(TINYGO) test -target wasi -bench . $(TEST_PACKAGES_FAST) $(TEST_PACKAGES_SLOW)
tinygo-bench-wasi-fast:

Просмотреть файл

@ -60,6 +60,7 @@ func TestClangAttributes(t *testing.T) {
{GOOS: "darwin", GOARCH: "arm64"},
{GOOS: "windows", GOARCH: "amd64"},
{GOOS: "windows", GOARCH: "arm64"},
{GOOS: "wasip1", GOARCH: "wasm"},
} {
name := "GOOS=" + options.GOOS + ",GOARCH=" + options.GOARCH
if options.GOARCH == "arm" {

Просмотреть файл

@ -191,12 +191,15 @@ func LoadTarget(options *Options) (*TargetSpec, error) {
default:
return nil, fmt.Errorf("invalid GOARM=%s, must be 5, 6, or 7", options.GOARM)
}
case "wasm":
llvmarch = "wasm32"
default:
llvmarch = options.GOARCH
}
llvmvendor := "unknown"
llvmos := options.GOOS
if llvmos == "darwin" {
switch llvmos {
case "darwin":
// Use macosx* instead of darwin, otherwise darwin/arm64 will refer
// to iOS!
llvmos = "macosx10.12.0"
@ -207,6 +210,8 @@ func LoadTarget(options *Options) (*TargetSpec, error) {
llvmos = "macosx11.0.0"
}
llvmvendor = "apple"
case "wasip1":
llvmos = "wasi"
}
// Target triples (which actually have four components, but are called
// triples for historical reasons) have the form:
@ -277,6 +282,15 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) {
case "arm64":
spec.CPU = "generic"
spec.Features = "+neon"
case "wasm":
spec.CPU = "generic"
spec.Features = "+bulk-memory,+nontrapping-fptoint,+sign-ext"
spec.BuildTags = append(spec.BuildTags, "tinygo.wasm")
spec.CFlags = append(spec.CFlags,
"-mbulk-memory",
"-mnontrapping-fptoint",
"-msign-ext",
)
}
if goos == "darwin" {
spec.Linker = "ld.lld"
@ -320,6 +334,22 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) {
"--no-insert-timestamp",
"--no-dynamicbase",
)
} else if goos == "wasip1" {
spec.GC = "" // use default GC
spec.Scheduler = "asyncify"
spec.Linker = "wasm-ld"
spec.RTLib = "compiler-rt"
spec.Libc = "wasi-libc"
spec.DefaultStackSize = 1024 * 16 // 16kB
spec.LDFlags = append(spec.LDFlags,
"--stack-first",
"--no-demangle",
)
spec.Emulator = "wasmtime --mapdir=/tmp::{tmpDir} {}"
spec.ExtraFiles = append(spec.ExtraFiles,
"src/runtime/asm_tinygowasm.S",
"src/internal/task/task_asyncify_wasm.S",
)
} else {
spec.LDFlags = append(spec.LDFlags, "-no-pie", "-Wl,--gc-sections") // WARNING: clang < 5.0 requires -nopie
}

Просмотреть файл

@ -34,6 +34,16 @@ var supportedLinuxArches = map[string]string{
"X86Linux": "linux/386",
"ARMLinux": "linux/arm/6",
"ARM64Linux": "linux/arm64",
"WASIp1": "wasip1/wasm",
}
func init() {
major, _, _ := goenv.GetGorootVersion()
if major < 21 {
// Go 1.20 backwards compatibility.
// Should be removed once we drop support for Go 1.20.
delete(supportedLinuxArches, "WASIp1")
}
}
var sema = make(chan struct{}, runtime.NumCPU())
@ -176,6 +186,8 @@ func runPlatTests(options compileopts.Options, tests []string, t *testing.T) {
t.Fatal("failed to load target spec:", err)
}
isWebAssembly := options.Target == "wasi" || options.Target == "wasm" || (options.Target == "" && options.GOARCH == "wasm")
for _, name := range tests {
if options.GOOS == "linux" && (options.GOARCH == "arm" || options.GOARCH == "386") {
switch name {
@ -226,7 +238,7 @@ func runPlatTests(options compileopts.Options, tests []string, t *testing.T) {
runTest("env.go", options, t, []string{"first", "second"}, []string{"ENV1=VALUE1", "ENV2=VALUE2"})
})
}
if options.Target == "wasi" || options.Target == "wasm" {
if isWebAssembly {
t.Run("alias.go-scheduler-none", func(t *testing.T) {
t.Parallel()
options := compileopts.Options(options)
@ -246,7 +258,7 @@ func runPlatTests(options compileopts.Options, tests []string, t *testing.T) {
runTest("rand.go", options, t, nil, nil)
})
}
if options.Target != "wasi" && options.Target != "wasm" {
if !isWebAssembly {
// The recover() builtin isn't supported yet on WebAssembly and Windows.
t.Run("recover.go", func(t *testing.T) {
t.Parallel()

Просмотреть файл

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build linux && !baremetal && !wasi
//go:build linux && !baremetal && !wasi && !wasip1
package os

Просмотреть файл

@ -6,7 +6,7 @@
// fairly similar: we use fdopendir, fdclosedir, and readdir from wasi-libc in
// a similar way that the darwin code uses functions from libc.
//go:build wasi
//go:build wasi || wasip1
package os

Просмотреть файл

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build darwin || linux
//go:build darwin || linux || wasip1
package os_test

Просмотреть файл

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build aix || darwin || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris || windows
//go:build aix || darwin || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris || wasip1 || windows
package os

Просмотреть файл

@ -1,4 +1,4 @@
//go:build baremetal || (wasm && !wasi)
//go:build baremetal || (wasm && !wasi && !wasip1)
package os

Просмотреть файл

@ -1,4 +1,4 @@
//go:build darwin || (linux && !baremetal)
//go:build darwin || (linux && !baremetal) || wasip1
// target wasi sets GOOS=linux and thus the +linux build tag,
// even though it doesn't show up in "tinygo info target -wasi"

Просмотреть файл

@ -1,4 +1,4 @@
//go:build windows || darwin || (linux && !baremetal)
//go:build windows || darwin || (linux && !baremetal) || wasip1
package os_test

Просмотреть файл

@ -1,4 +1,4 @@
//go:build !wasi
//go:build !wasi && !wasip1
package os_test

Просмотреть файл

@ -1,4 +1,4 @@
//go:build wasi
//go:build wasi || wasip1
package os_test

Просмотреть файл

@ -1,4 +1,4 @@
//go:build windows || darwin || (linux && !baremetal)
//go:build windows || darwin || (linux && !baremetal) || wasip1
package os_test
@ -23,7 +23,6 @@ var dot = []string{
"os_test.go",
"types.go",
"stat_darwin.go",
"stat_linux.go",
}
func randomName() string {

Просмотреть файл

@ -1,4 +1,4 @@
//go:build !baremetal && !js && !wasi
//go:build !baremetal && !js && !wasi && !wasip1
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style

Просмотреть файл

@ -1,4 +1,4 @@
//go:build !windows && !baremetal && !js && !wasi
//go:build !windows && !baremetal && !js && !wasi && !wasip1
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style

Просмотреть файл

@ -1,4 +1,4 @@
//go:build !baremetal && !js && !wasi
//go:build !baremetal && !js && !wasi && !wasip1
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style

Просмотреть файл

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build !baremetal && !js && !wasi
//go:build !baremetal && !js && !wasi && !wasip1
package os

Просмотреть файл

@ -1,4 +1,4 @@
//go:build baremetal || js || wasi
//go:build baremetal || js || wasi || wasip1
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style

Просмотреть файл

@ -1,9 +1,13 @@
//go:build linux && !baremetal
//go:build (linux && !baremetal) || wasip1
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Note: this file is used for both Linux and WASI.
// Eventually it might be better to spit it up, and make the syscall constants
// match the typical WASI constants instead of the Linux-equivalents used here.
package os
import (

Просмотреть файл

@ -1,4 +1,4 @@
//go:build baremetal || (wasm && !wasi)
//go:build baremetal || (wasm && !wasi && !wasip1)
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style

Просмотреть файл

@ -1,4 +1,4 @@
//go:build darwin || (linux && !baremetal)
//go:build darwin || (linux && !baremetal) || wasip1
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style

Просмотреть файл

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build !baremetal && !js && !wasi
//go:build !baremetal && !js && !wasi && !wasip1
package os_test

Просмотреть файл

@ -1,4 +1,4 @@
//go:build darwin || (linux && !baremetal)
//go:build darwin || (linux && !baremetal) || wasip1
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style

Просмотреть файл

@ -1,4 +1,4 @@
//go:build linux || darwin || windows
//go:build linux || darwin || windows || wasip1
package runtime

Просмотреть файл

@ -1,4 +1,4 @@
//go:build linux || darwin
//go:build linux || darwin || wasip1
package runtime

10
src/runtime/os_wasip1.go Обычный файл
Просмотреть файл

@ -0,0 +1,10 @@
//go:build wasip1
package runtime
// The actual GOOS=wasip1, as newly added in the Go 1.21 toolchain.
// Previously we supported -target=wasi, but that was essentially faked by using
// linux/arm instead because that was the closest thing that was already
// supported in the Go standard library.
const GOOS = "wasip1"

Просмотреть файл

@ -1,4 +1,4 @@
//go:build wasm && !wasi
//go:build wasm && !wasi && !wasip1
package runtime

Просмотреть файл

@ -1,4 +1,4 @@
//go:build wasm && !wasi && !scheduler.none
//go:build wasm && !wasi && !scheduler.none && !wasip1
package runtime

Просмотреть файл

@ -1,4 +1,4 @@
//go:build tinygo.wasm && wasi
//go:build tinygo.wasm && (wasi || wasip1)
package runtime

Просмотреть файл

@ -1,4 +1,4 @@
//go:build !wasi && !darwin
//go:build !wasi && !wasip1 && !darwin
package syscall

Просмотреть файл

@ -1,4 +1,4 @@
//go:build baremetal || wasm
//go:build baremetal || (wasm && !wasip1)
// This file emulates some file-related functions that are only available
// under a real operating system.

Просмотреть файл

@ -1,4 +1,4 @@
//go:build !baremetal && !wasm
//go:build !(baremetal || (wasm && !wasip1))
// This file assumes there is a libc available that runs on a real operating
// system.

Просмотреть файл

@ -1,4 +1,4 @@
//go:build baremetal || wasi || wasm
//go:build baremetal || tinygo.wasm
// This file emulates some process-related functions that are only available
// under a real operating system.

Просмотреть файл

@ -1,4 +1,4 @@
//go:build !baremetal && !wasi && !wasm
//go:build !baremetal && !tinygo.wasm
// This file assumes there is a libc available that runs on a real operating
// system.

Просмотреть файл

@ -1,4 +1,4 @@
//go:build darwin || nintendoswitch || wasi
//go:build darwin || nintendoswitch || wasi || wasip1
package syscall

Просмотреть файл

@ -1,4 +1,4 @@
//go:build wasi
//go:build wasi || wasip1
package syscall

Просмотреть файл

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
//go:build !wasi
//go:build !wasi && !wasip1
package testing_test

Просмотреть файл

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
//go:build wasi
//go:build wasi || wasip1
package testing_test