Skip to content

Commit bad29c0

Browse files
authored
Support WebAssembly target platforms wasm{32,64}-wasip1 (#466)
Note this doesn't differ much from wasm{32,64}-unknown-unknown targets, except for different sysroot setup. For whatever reason clang expects slightly differently located libs.
1 parent e831f94 commit bad29c0

File tree

9 files changed

+125
-5
lines changed

9 files changed

+125
-5
lines changed

platforms/BUILD.bazel

+16
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,19 @@ platform(
6161
"@platforms//cpu:wasm64",
6262
],
6363
)
64+
65+
platform(
66+
name = "wasip1-wasm32",
67+
constraint_values = [
68+
"@platforms//os:wasi",
69+
"@platforms//cpu:wasm32",
70+
],
71+
)
72+
73+
platform(
74+
name = "wasip1-wasm64",
75+
constraint_values = [
76+
"@platforms//os:wasi",
77+
"@platforms//cpu:wasm64",
78+
],
79+
)

tests/MODULE.bazel

+26-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ llvm.toolchain(
189189
)
190190
use_repo(llvm, "llvm_toolchain_linux_exec")
191191

192-
# Toolchain example for WebAssembly.
192+
# Toolchain example for WebAssembly wasm* targets.
193193
llvm.toolchain(
194194
name = "llvm_toolchain_wasm",
195195
libclang_rt = {
@@ -220,6 +220,31 @@ use_repo(llvm, "llvm_toolchain_wasm")
220220

221221
register_toolchains("@llvm_toolchain_wasm//:all")
222222

223+
# Toolchain example for WebAssembly wasm*-wasi* targets.
224+
llvm.toolchain(
225+
name = "llvm_toolchain_wasm_wasi",
226+
libclang_rt = {
227+
"@libclang_rt_wasm32//:libclang_rt.builtins-wasm32.a": "wasm32-unknown-wasip1/libclang_rt.builtins.a",
228+
},
229+
# WebAssembly tests use a separate (newer) version of LLVM to exercise
230+
# support for experimental features such as wasm64.
231+
llvm_versions = {
232+
# The most recent LLVM as of 2024-10-17
233+
"": "19.1.0",
234+
},
235+
stdlib = {
236+
"wasip1-wasm32": "libc",
237+
},
238+
)
239+
llvm.sysroot(
240+
name = "llvm_toolchain_wasm_wasi",
241+
label = "@wasi_sdk_sysroots//wasm32-wasip1",
242+
targets = ["wasip1-wasm32"],
243+
)
244+
use_repo(llvm, "llvm_toolchain_wasm_wasi")
245+
246+
register_toolchains("@llvm_toolchain_wasm_wasi//:all")
247+
223248
wasi_sdk_sysroots = use_repo_rule("//wasm:wasi_sdk.bzl", "wasi_sdk_sysroots")
224249

225250
wasi_sdk_sysroots(name = "wasi_sdk_sysroots")

tests/WORKSPACE

+28-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ llvm_toolchain(
156156
urls = {"": ["https://github.com/llvm/llvm-project/releases/download/llvmorg-17.0.6/clang+llvm-17.0.6-x86_64-linux-gnu-ubuntu-22.04.tar.xz"]},
157157
)
158158

159-
# Toolchain example for WebAssembly.
159+
# Toolchain example for WebAssembly wasm* targets.
160160
llvm_toolchain(
161161
name = "llvm_toolchain_wasm",
162162
libclang_rt = {
@@ -185,6 +185,33 @@ load(
185185

186186
llvm_register_toolchains_wasm()
187187

188+
# Toolchain example for WebAssembly wasm*-wasi* targets.
189+
llvm_toolchain(
190+
name = "llvm_toolchain_wasm_wasi",
191+
libclang_rt = {
192+
"@libclang_rt_wasm32//:libclang_rt.builtins-wasm32.a": "wasm32-unknown-wasip1/libclang_rt.builtins.a",
193+
},
194+
# WebAssembly tests use a separate (newer) version of LLVM to exercise
195+
# support for experimental features such as wasm64.
196+
llvm_versions = {
197+
# The most recent LLVM as of 2024-10-17
198+
"": "19.1.0",
199+
},
200+
stdlib = {
201+
"wasip1-wasm32": "libc",
202+
},
203+
sysroot = {
204+
"wasip1-wasm32": "@wasi_sdk_sysroots//wasm32-wasip1",
205+
},
206+
)
207+
208+
load(
209+
"@llvm_toolchain_wasm_wasi//:toolchains.bzl",
210+
llvm_register_toolchains_wasm_wasi = "llvm_register_toolchains",
211+
)
212+
213+
llvm_register_toolchains_wasm_wasi()
214+
188215
load("//wasm:wasi_sdk.bzl", "libclang_rt_wasm32", "wasi_sdk_sysroots")
189216

190217
libclang_rt_wasm32(name = "libclang_rt_wasm32")

tests/wasm/BUILD.bazel

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ build_test(
88
":wasm32_strlen",
99
":wasm32_strlen_nolibc",
1010
":wasm64_strlen_nolibc",
11+
":wasm32_wasip1_strlen",
1112
],
1213
)
1314

@@ -42,3 +43,16 @@ transition_binary_to_platform(
4243
bin = ":wasm_strlen_nolibc",
4344
platform = "@toolchains_llvm//platforms:wasm64",
4445
)
46+
47+
cc_binary(
48+
name = "wasm_strlen_wasi",
49+
srcs = ["wasm_strlen_wasi.c"],
50+
linkopts = ["-Wl,--no-entry"],
51+
tags = ["manual"],
52+
)
53+
54+
transition_binary_to_platform(
55+
name = "wasm32_wasip1_strlen",
56+
bin = ":wasm_strlen_wasi",
57+
platform = "@toolchains_llvm//platforms:wasip1-wasm32",
58+
)

tests/wasm/wasi_sdk.bzl

+6-1
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@ def _wasi_sdk_sysroots(ctx):
3030
name = repr(abi),
3131
))
3232
ctx.execute(["mv", "include/" + abi, "%s/include" % (abi,)])
33-
ctx.execute(["mv", "lib/" + abi, "%s/lib" % (abi,)])
3433
ctx.execute(["mv", "share/" + abi, "%s/share" % (abi,)])
3534

35+
# This is needed for wasm*-unknown-unknown targets
36+
ctx.execute(["cp", "-R", "lib/" + abi, "%s/lib" % (abi,)])
37+
38+
# This is needed for wasm*-wasip1 targets
39+
ctx.execute(["mv", "lib/" + abi, "%s/lib/%s" % (abi, abi)])
40+
3641
wasi_sdk_sysroots = repository_rule(_wasi_sdk_sysroots)
3742

3843
def _libclang_rt_wasm32(ctx):

tests/wasm/wasm_strlen_wasi.c

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
5+
uint32_t wasm_strlen(char *s) {
6+
return strlen(s);
7+
}
8+
9+
int main() {
10+
char *s = "Hello world!";
11+
printf("strlen(\"%s\") = %d\n", s, wasm_strlen(s));
12+
return 0;
13+
}

toolchain/cc_toolchain_config.bzl

+16
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,22 @@ def cc_toolchain_config(
105105
"unknown",
106106
"unknown",
107107
),
108+
"wasip1-wasm32": (
109+
"clang-wasm32",
110+
"wasm32",
111+
"unknown",
112+
"clang",
113+
"unknown",
114+
"unknown",
115+
),
116+
"wasip1-wasm64": (
117+
"clang-wasm64",
118+
"wasm64",
119+
"unknown",
120+
"clang",
121+
"unknown",
122+
"unknown",
123+
),
108124
}[target_os_arch_key]
109125

110126
# Unfiltered compiler flags; these are placed at the end of the command

toolchain/internal/common.bzl

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ SUPPORTED_TARGETS = [
1919
("darwin", "aarch64"),
2020
("none", "wasm32"),
2121
("none", "wasm64"),
22+
("wasip1", "wasm32"),
23+
("wasip1", "wasm64"),
2224
]
2325

2426
# Map of tool name to its symlinked name in the tools directory.
@@ -132,7 +134,7 @@ def os_from_rctx(rctx):
132134

133135
def os_bzl(os):
134136
# Return the OS string as used in bazel platform constraints.
135-
return {"darwin": "osx", "linux": "linux", "none": "none"}[os]
137+
return {"darwin": "osx", "linux": "linux", "none": "none", "wasip1": "wasi"}[os]
136138

137139
def arch(rctx):
138140
arch = rctx.attr.exec_arch

toolchain/internal/configure.bzl

+3-1
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ def _cc_toolchain_str(
324324
"linux-x86_64": "x86_64-unknown-linux-gnu",
325325
"wasm32": "wasm32-unknown-unknown",
326326
"wasm64": "wasm64-unknown-unknown",
327+
"wasip1-wasm32": "wasm32-wasip1",
328+
"wasip1-wasm64": "wasm64-wasip1",
327329
}[target_pair]
328330
cxx_builtin_include_directories = [
329331
toolchain_path_prefix + "include/c++/v1",
@@ -350,7 +352,7 @@ def _cc_toolchain_str(
350352
_join(sysroot_prefix, "/usr/include"),
351353
_join(sysroot_prefix, "/System/Library/Frameworks"),
352354
])
353-
elif target_os == "none":
355+
elif target_os == "none" or target_os == "wasip1":
354356
if sysroot_prefix:
355357
cxx_builtin_include_directories.extend([
356358
_join(sysroot_prefix, "/include"),

0 commit comments

Comments
 (0)