Skip to content

Commit 2ce914b

Browse files
authored
Merge pull request #165 from rockwotj/main
Add wasm32-wasi-musl target
2 parents c33a2e5 + c0ce086 commit 2ce914b

File tree

10 files changed

+91
-5
lines changed

10 files changed

+91
-5
lines changed

MODULE.bazel

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use_repo(
2626
go_deps,
2727
"com_github_bazelbuild_buildtools",
2828
"com_github_stretchr_testify",
29+
"com_github_tetratelabs_wazero",
2930
)
3031

3132
toolchains = use_extension("//toolchain:ext.bzl", "toolchains")
@@ -46,4 +47,6 @@ register_toolchains(
4647
# arm64 toolchains for libc-aware platforms:
4748
"@zig_sdk//libc_aware/toolchain:linux_arm64_gnu.2.28",
4849
"@zig_sdk//libc_aware/toolchain:linux_arm64_musl",
50+
# wasm/wasi toolchains
51+
"@zig_sdk//toolchain:wasip1_wasm",
4952
)

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ register_toolchains(
183183
"@zig_sdk//toolchain:darwin_arm64",
184184
"@zig_sdk//toolchain:windows_amd64",
185185
"@zig_sdk//toolchain:windows_arm64",
186+
"@zig_sdk//toolchain:wasip1_wasm",
186187
)
187188
```
188189

@@ -291,7 +292,9 @@ Go, the following Go-style toolchain aliases are created:
291292
|---------------- | -------- |
292293
|`x86_64` | `amd64` |
293294
|`aarch64` | `arm64` |
295+
|`wasm32` | `wasm` |
294296
|`macos` | `darwin` |
297+
|`wasi` | `wasip1` |
295298

296299
For example, the toolchain `linux_amd64_gnu.2.28` is aliased to
297300
`x86_64-linux-gnu.2.28`. To find out which toolchains can be registered or

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.19
55
require (
66
github.com/bazelbuild/buildtools v0.0.0-20230510134650-37bd1811516d
77
github.com/stretchr/testify v1.8.2
8+
github.com/tetratelabs/wazero v1.6.0
89
)
910

1011
require (

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
4444
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
4545
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
4646
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
47+
github.com/tetratelabs/wazero v1.6.0 h1:z0H1iikCdP8t+q341xqepY4EWvHEw8Es7tlqiVzlP3g=
48+
github.com/tetratelabs/wazero v1.6.0/go.mod h1:0U0G41+ochRKoPKCJlh0jMg1CHkyfK8kDqiirMmKY8A=
4749
go.starlark.net v0.0.0-20210223155950-e043a3d3c984/go.mod h1:t3mmBBPzAVvK0L0n1drDmrQsJ8FoIx4INCqVMTr/Zo0=
4850
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
4951
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=

test/c/BUILD

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
load("@hermetic_cc_toolchain//rules:platform.bzl", "platform_binary")
22
load("@rules_go//go:def.bzl", "go_library", "go_test")
3+
load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS")
34

45
cc_binary(
56
name = "which_libc",
@@ -41,52 +42,67 @@ _WINDOWS_AMD64 = [
4142
env = {
4243
"WANT": want,
4344
"BINARY": "$(rlocationpath which_libc_{})".format(name),
45+
"EXECUTOR": executor,
4446
},
4547
target_compatible_with = compatible_with,
4648
),
4749
)
48-
for name, platform, compatible_with, want, tags in [
50+
for name, platform, compatible_with, want, tags, executor in [
4951
(
5052
"linux_amd64_musl",
5153
"//libc_aware/platform:linux_amd64_musl",
5254
_LINUX_AMD64,
5355
"^linux non-glibc",
5456
[],
57+
"NATIVE",
5558
),
5659
(
5760
"linux_amd64_gnu.2.28",
5861
"//libc_aware/platform:linux_amd64_gnu.2.28",
5962
_LINUX_AMD64,
6063
"^linux glibc_2.28",
6164
[],
65+
"NATIVE",
6266
),
6367
(
6468
"linux_amd64",
6569
"//platform:linux_amd64",
6670
_LINUX_AMD64,
6771
"^linux glibc_2.28",
6872
[],
73+
"NATIVE",
6974
),
7075
(
7176
"windows_amd64",
7277
"//platform:windows_amd64",
7378
_WINDOWS_AMD64,
7479
"^windows ",
7580
[],
81+
"NATIVE",
7682
),
7783
(
7884
"darwin_amd64",
7985
"//platform:darwin_amd64",
8086
_DARWIN_AMD64,
8187
"^macos non-glibc",
8288
["darwin_c"],
89+
"NATIVE",
8390
),
8491
(
8592
"darwin_arm64",
8693
"//platform:darwin_arm64",
8794
_DARWIN_ARM64,
8895
"^macos non-glibc",
8996
["darwin_c"],
97+
"NATIVE",
98+
),
99+
(
100+
"wasip1_wasm32",
101+
"//platform:wasip1_wasm",
102+
HOST_CONSTRAINTS,
103+
"^wasi non-glibc",
104+
[],
105+
"WASI",
90106
),
91107
]
92108
]
@@ -96,6 +112,8 @@ go_library(
96112
srcs = ["c_test.go"],
97113
deps = [
98114
"@com_github_stretchr_testify//assert",
115+
"@com_github_tetratelabs_wazero//:wazero",
116+
"@com_github_tetratelabs_wazero//imports/wasi_snapshot_preview1",
99117
"@rules_go//go/runfiles",
100118
],
101119
)

test/c/c_test.go

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package c_test
22

33
import (
4+
"bytes"
5+
"context"
6+
"fmt"
47
"os"
58
"os/exec"
69
"testing"
710

811
"github.com/bazelbuild/rules_go/go/runfiles"
912
"github.com/stretchr/testify/assert"
13+
"github.com/tetratelabs/wazero"
14+
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
1015
)
1116

1217
func TestYadda(t *testing.T) {
@@ -16,11 +21,36 @@ func TestYadda(t *testing.T) {
1621
if err != nil {
1722
t.Fatalf("unable to locate guest binary: %v", err)
1823
}
19-
20-
got, err := exec.Command(binary).CombinedOutput()
24+
var got []byte
25+
switch os.Getenv("EXECUTOR") {
26+
case "NATIVE":
27+
got, err = exec.Command(binary).CombinedOutput()
28+
case "WASI":
29+
got, err = runWasi(binary)
30+
default:
31+
err = fmt.Errorf("unknown executor: %q", os.Getenv("EXECUTOR"))
32+
}
2133
if err != nil {
2234
t.Fatalf("run %q: %v", binary, err)
2335
}
2436

2537
assert.Regexp(t, string(want), string(got))
2638
}
39+
40+
func runWasi(binary string) ([]byte, error) {
41+
ctx := context.Background()
42+
r := wazero.NewRuntime(ctx)
43+
defer r.Close(ctx)
44+
buf := &bytes.Buffer{}
45+
config := wazero.NewModuleConfig().WithStdout(buf).WithStderr(buf).WithArgs("wasi")
46+
wasi_snapshot_preview1.MustInstantiate(ctx, r)
47+
bin, err := os.ReadFile(binary)
48+
if err != nil {
49+
return nil, fmt.Errorf("unable to read guest binary: %v", err)
50+
}
51+
_, err = r.InstantiateWithConfig(ctx, bin, config)
52+
if err != nil {
53+
return nil, fmt.Errorf("unable to create instantiate module: %v", err)
54+
}
55+
return buf.Bytes(), nil
56+
}

test/c/main.c

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#elif __linux__
1111
#include <features.h>
1212
#define OS "linux"
13+
#elif __wasi__
14+
#include <features.h>
15+
#define OS "wasi"
1316
#else
1417
# error "Unknown compiler!"
1518
#endif

toolchain/platform/defs.bzl

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ def declare_platforms():
1414
for os in oss:
1515
declare_platform(gocpu, zigcpu, bzlos, os)
1616

17+
# We can support GOARCH=wasm32 after https://github.com/golang/go/issues/63131
18+
declare_platform("wasm", "wasm32", "wasi", "wasip1")
19+
1720
def declare_libc_aware_platforms():
1821
# create @zig_sdk//{os}_{arch}_platform entries with zig and go conventions
1922
# with libc specified

toolchain/private/defs.bzl

+23
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def target_structs():
4848
ret.append(_target_linux_musl(gocpu, zigcpu))
4949
for glibc in _GLIBCS:
5050
ret.append(_target_linux_gnu(gocpu, zigcpu, glibc))
51+
ret.append(_target_wasm())
5152
return ret
5253

5354
def _target_macos(gocpu, zigcpu):
@@ -199,3 +200,25 @@ def _target_linux_musl(gocpu, zigcpu):
199200
ld_zig_subcmd = "ld.lld",
200201
artifact_name_patterns = [],
201202
)
203+
204+
def _target_wasm():
205+
return struct(
206+
gotarget = "wasip1_wasm",
207+
zigtarget = "wasm32-wasi-musl",
208+
includes = [
209+
"libc/include/wasm-wasi-musl",
210+
"libc/wasi",
211+
] + _INCLUDE_TAIL,
212+
linkopts = [],
213+
dynamic_library_linkopts = [],
214+
supports_dynamic_linker = False,
215+
copts = [],
216+
libc = "musl",
217+
bazel_target_cpu = "wasm32",
218+
constraint_values = [
219+
"@platforms//os:wasi",
220+
"@platforms//cpu:wasm32",
221+
],
222+
ld_zig_subcmd = "wasm-ld",
223+
artifact_name_patterns = [],
224+
)

toolchain/zig-wrapper.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,11 @@ fn getRunMode(self_exe: []const u8, self_base_noexe: []const u8) error{BadParent
279279
var it = mem.split(u8, triple, "-");
280280

281281
const arch = it.next() orelse return error.BadParent;
282-
if (mem.indexOf(u8, "aarch64,x86_64", arch) == null)
282+
if (mem.indexOf(u8, "aarch64,x86_64,wasm32", arch) == null)
283283
return error.BadParent;
284284

285285
const got_os = it.next() orelse return error.BadParent;
286-
if (mem.indexOf(u8, "linux,macos,windows", got_os) == null)
286+
if (mem.indexOf(u8, "linux,macos,windows,wasi", got_os) == null)
287287
return error.BadParent;
288288

289289
// ABI triple is too much of a moving target

0 commit comments

Comments
 (0)