Skip to content

Commit

Permalink
Add wasm32-wasi-musl target
Browse files Browse the repository at this point in the history
I would like to be able to compile wasm/wasi with zig + bazel. This adds
a toolchain to be able to do so. Wasm/wasi is a pretty rapidly changing
space, but I've choosen the most specific naming that I could (WASI
preview 1).

I've confirmed this works by running the compiled binary with wasmtime,
a popular wasm/wasi runtime.

```shell
bazel run --run_under=wasmtime //test/c:which_libc_wasip1_wasm
```

Signed-off-by: Tyler Rockwood <rockwood@redpanda.com>
  • Loading branch information
rockwotj committed Feb 19, 2024
1 parent 361dbd1 commit 38a5257
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ register_toolchains(
# arm64 toolchains for libc-aware platforms:
"@zig_sdk//libc_aware/toolchain:linux_arm64_gnu.2.28",
"@zig_sdk//libc_aware/toolchain:linux_arm64_musl",
# wasm/wasi toolchains
"@zig_sdk//toolchain:wasip1_wasm",
)
7 changes: 7 additions & 0 deletions test/c/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,10 @@ cc_binary(
("windows_arm64", "//platform:windows_arm64", []),
]
]

platform_binary(
name = "which_libc_wasip1_wasm",
src = "which_libc",
platform = "//platform:wasip1_wasm",
tags = [],
)
3 changes: 3 additions & 0 deletions test/c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#elif __linux__
#include <features.h>
#define OS "linux"
#elif __wasi__
#include <features.h>
#define OS "wasi"
#else
# error "Unknown compiler!"
#endif
Expand Down
2 changes: 2 additions & 0 deletions toolchain/platform/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def declare_platforms():
for bzlos, oss in _OS.items():
for os in oss:
declare_platform(gocpu, zigcpu, bzlos, os)
# We can support GOARCH=wasm32 after https://github.com/golang/go/issues/63131
declare_platform("wasm", "wasm32", "wasi", "wasip1")

def declare_libc_aware_platforms():
# create @zig_sdk//{os}_{arch}_platform entries with zig and go conventions
Expand Down
23 changes: 23 additions & 0 deletions toolchain/private/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def target_structs():
ret.append(_target_linux_musl(gocpu, zigcpu))
for glibc in _GLIBCS:
ret.append(_target_linux_gnu(gocpu, zigcpu, glibc))
ret.append(_target_wasm())
return ret

def _target_macos(gocpu, zigcpu):
Expand Down Expand Up @@ -199,3 +200,25 @@ def _target_linux_musl(gocpu, zigcpu):
ld_zig_subcmd = "ld.lld",
artifact_name_patterns = [],
)

def _target_wasm():
return struct(
gotarget = "wasip1_wasm",
zigtarget = "wasm32-wasi-musl",
includes = [
"libc/include/wasm-wasi-musl",
"libc/wasi",
] + _INCLUDE_TAIL,
linkopts = [],
dynamic_library_linkopts = [],
supports_dynamic_linker = True,
copts = [],
libc = "musl",
bazel_target_cpu = "wasm32",
constraint_values = [
"@platforms//os:wasi",
"@platforms//cpu:wasm32",
],
ld_zig_subcmd = "ld.lld",
artifact_name_patterns = [],
)
4 changes: 2 additions & 2 deletions toolchain/zig-wrapper.zig
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ fn getRunMode(self_exe: []const u8, self_base_noexe: []const u8) error{BadParent
var it = mem.split(u8, triple, "-");

const arch = it.next() orelse return error.BadParent;
if (mem.indexOf(u8, "aarch64,x86_64", arch) == null)
if (mem.indexOf(u8, "aarch64,x86_64,wasm32", arch) == null)
return error.BadParent;

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

// ABI triple is too much of a moving target
Expand Down

0 comments on commit 38a5257

Please sign in to comment.