Skip to content

wasm: add support for GOOS=wasip1 #3861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
32 changes: 31 additions & 1 deletion compileopts/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 14 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/os/dir_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/os/dir_wasi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/os/env_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/os/exec_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/os/file_other.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build baremetal || (wasm && !wasi)
//go:build baremetal || (wasm && !wasi && !wasip1)

package os

Expand Down
2 changes: 1 addition & 1 deletion src/os/file_unix.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/os/getpagesize_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build windows || darwin || (linux && !baremetal)
//go:build windows || darwin || (linux && !baremetal) || wasip1

package os_test

Expand Down
2 changes: 1 addition & 1 deletion src/os/is_wasi_no_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !wasi
//go:build !wasi && !wasip1

package os_test

Expand Down
2 changes: 1 addition & 1 deletion src/os/is_wasi_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build wasi
//go:build wasi || wasip1

package os_test

Expand Down
3 changes: 1 addition & 2 deletions src/os/os_anyos_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build windows || darwin || (linux && !baremetal)
//go:build windows || darwin || (linux && !baremetal) || wasip1

package os_test

Expand All @@ -23,7 +23,6 @@ var dot = []string{
"os_test.go",
"types.go",
"stat_darwin.go",
"stat_linux.go",
}

func randomName() string {
Expand Down
2 changes: 1 addition & 1 deletion src/os/os_chmod_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/os/os_symlink_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/os/read_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/os/removeall_noat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/os/removeall_other.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/os/stat_linux.go → src/os/stat_linuxlike.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion src/os/stat_other.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/os/stat_unix.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/os/tempfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/os/types_unix.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/env.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build linux || darwin || windows
//go:build linux || darwin || windows || wasip1

package runtime

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/env_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build linux || darwin
//go:build linux || darwin || wasip1

package runtime

Expand Down
10 changes: 10 additions & 0 deletions src/runtime/os_wasip1.go
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion src/runtime/runtime_wasm_js.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build wasm && !wasi
//go:build wasm && !wasi && !wasip1

package runtime

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/runtime_wasm_js_scheduler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build wasm && !wasi && !scheduler.none
//go:build wasm && !wasi && !scheduler.none && !wasip1

package runtime

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/runtime_wasm_wasi.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build tinygo.wasm && wasi
//go:build tinygo.wasm && (wasi || wasip1)

package runtime

Expand Down
2 changes: 1 addition & 1 deletion src/syscall/errno_other.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !wasi && !darwin
//go:build !wasi && !wasip1 && !darwin

package syscall

Expand Down
2 changes: 1 addition & 1 deletion src/syscall/file_emulated.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/syscall/file_hosted.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/syscall/proc_emulated.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/syscall/proc_hosted.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/syscall/syscall_libc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build darwin || nintendoswitch || wasi
//go:build darwin || nintendoswitch || wasi || wasip1

package syscall

Expand Down
2 changes: 1 addition & 1 deletion src/syscall/syscall_libc_wasi.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build wasi
//go:build wasi || wasip1

package syscall

Expand Down
2 changes: 1 addition & 1 deletion src/testing/is_wasi_no_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/testing/is_wasi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down