Skip to content

Commit 1e13068

Browse files
committed
Add integration tests for building WebAssembly binaries.
1 parent 8800a0f commit 1e13068

File tree

6 files changed

+159
-5
lines changed

6 files changed

+159
-5
lines changed

tests/BUILD.bazel

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414

1515
load("@bazel_skylib//rules:build_test.bzl", "build_test")
1616
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
17-
load(":transitions.bzl", "dwp_file", "transition_library_to_platform")
17+
load(
18+
":transitions.bzl",
19+
"dwp_file",
20+
"transition_binary_to_platform",
21+
"transition_library_to_platform",
22+
)
1823

1924
cc_library(
2025
name = "stdlib",
@@ -200,3 +205,35 @@ cc_test(
200205
tags = ["manual"],
201206
deps = [":test_cxx_standard_lib_transitioned"],
202207
)
208+
209+
cc_binary(
210+
name = "wasm_strlen",
211+
srcs = ["wasm_strlen.c"],
212+
linkopts = ["-Wl,--no-entry"],
213+
tags = ["manual"],
214+
)
215+
216+
transition_binary_to_platform(
217+
name = "wasm32_strlen",
218+
bin = ":wasm_strlen",
219+
platform = "@toolchains_llvm//platforms:wasm32",
220+
)
221+
222+
cc_binary(
223+
name = "wasm_strlen_nolibc",
224+
srcs = ["wasm_strlen_nolibc.c"],
225+
linkopts = ["-Wl,--no-entry"],
226+
tags = ["manual"],
227+
)
228+
229+
transition_binary_to_platform(
230+
name = "wasm32_strlen_nolibc",
231+
bin = ":wasm_strlen_nolibc",
232+
platform = "@toolchains_llvm//platforms:wasm32",
233+
)
234+
235+
transition_binary_to_platform(
236+
name = "wasm64_strlen_nolibc",
237+
bin = ":wasm_strlen_nolibc",
238+
platform = "@toolchains_llvm//platforms:wasm64",
239+
)

tests/MODULE.bazel

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,38 @@ llvm.toolchain(
179179
exec_arch = "amd64",
180180
)
181181
use_repo(llvm, "llvm_toolchain_linux_exec")
182+
183+
# Toolchain example for WebAssembly.
184+
llvm.toolchain(
185+
name = "llvm_toolchain_wasm",
186+
llvm_versions = {
187+
# The most recent LLVM as of 2024-10-17
188+
"": "19.1.0",
189+
},
190+
stdlib = {
191+
"wasm32": "libc",
192+
"wasm64": "none",
193+
},
194+
libclang_rt = {
195+
"@libclang_rt_wasm32//:libclang_rt.builtins-wasm32.a": "wasm32-unknown-unknown/libclang_rt.builtins.a",
196+
},
197+
)
198+
llvm.sysroot(
199+
name = "llvm_toolchain_wasm",
200+
label = "@wasi_sdk_sysroots//wasm32-wasip2",
201+
targets = ["wasm32"],
202+
)
203+
llvm.sysroot(
204+
name = "llvm_toolchain_wasm",
205+
label = "@wasi_sdk_sysroots//empty",
206+
targets = ["wasm64"],
207+
)
208+
209+
use_repo(llvm, "llvm_toolchain_wasm")
210+
register_toolchains("@llvm_toolchain_wasm//:all")
211+
212+
wasi_sdk_sysroots = use_repo_rule("//:wasi_sdk.bzl", "wasi_sdk_sysroots")
213+
wasi_sdk_sysroots(name = "wasi_sdk_sysroots")
214+
215+
libclang_rt_wasm32 = use_repo_rule("//:wasi_sdk.bzl", "libclang_rt_wasm32")
216+
libclang_rt_wasm32(name = "libclang_rt_wasm32")

tests/transitions.bzl

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ dwp_file = rule(
9797
},
9898
)
9999

100-
def _transition_library_to_platform_transition_impl(_, attr):
100+
def _transition_to_platform_transition_impl(_, attr):
101101
return {"//command_line_option:platforms": str(attr.platform)}
102102

103-
_transition_library_to_platform_transition = transition(
104-
implementation = _transition_library_to_platform_transition_impl,
103+
_transition_to_platform_transition = transition(
104+
implementation = _transition_to_platform_transition_impl,
105105
inputs = [],
106106
outputs = ["//command_line_option:platforms"],
107107
)
@@ -114,7 +114,27 @@ def _transition_library_to_platform_impl(ctx):
114114
transition_library_to_platform = rule(
115115
implementation = _transition_library_to_platform_impl,
116116
attrs = {
117-
"lib": attr.label(mandatory = True, cfg = _transition_library_to_platform_transition),
117+
"lib": attr.label(mandatory = True, cfg = _transition_to_platform_transition),
118+
"platform": attr.label(mandatory = True),
119+
"_allowlist_function_transition": attr.label(
120+
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
121+
),
122+
},
123+
)
124+
125+
def _transition_binary_to_platform_impl(ctx):
126+
out = ctx.actions.declare_file(ctx.attr.name)
127+
ctx.actions.symlink(output = out, target_file = ctx.file.bin)
128+
return DefaultInfo(files = depset([out]))
129+
130+
transition_binary_to_platform = rule(
131+
implementation = _transition_binary_to_platform_impl,
132+
attrs = {
133+
"bin": attr.label(
134+
mandatory = True,
135+
allow_single_file = True,
136+
cfg = _transition_to_platform_transition,
137+
),
118138
"platform": attr.label(mandatory = True),
119139
"_allowlist_function_transition": attr.label(
120140
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",

tests/wasi_sdk.bzl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
_SYSROOT_BUILD = """
2+
filegroup(
3+
name = {name},
4+
srcs = glob(["include/**/*", "lib/**/*", "share/**/*"], allow_empty=True),
5+
visibility = ["//visibility:public"],
6+
)
7+
"""
8+
9+
_WASI_SDK_ABIS = [
10+
"wasm32-wasi",
11+
"wasm32-wasip1",
12+
"wasm32-wasip1-threads",
13+
"wasm32-wasip2",
14+
"wasm32-wasi-threads",
15+
]
16+
17+
def _wasi_sdk_sysroots(ctx):
18+
ctx.download_and_extract(
19+
integrity = "sha256-NRcvfSeZSFsVpGsdh/UKWF2RXsZiCA8AXZkVOlCIjwg=",
20+
stripPrefix = "wasi-sysroot-24.0",
21+
url = ["https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-24/wasi-sysroot-24.0.tar.gz"],
22+
)
23+
24+
ctx.file("empty/BUILD.bazel", _SYSROOT_BUILD.format(
25+
name = repr("empty"),
26+
))
27+
28+
for abi in _WASI_SDK_ABIS:
29+
ctx.file("%s/BUILD.bazel" % (abi,), _SYSROOT_BUILD.format(
30+
name = repr(abi),
31+
))
32+
ctx.execute(["mv", "include/" + abi, "%s/include" % (abi,)])
33+
ctx.execute(["mv", "lib/" + abi, "%s/lib" % (abi,)])
34+
ctx.execute(["mv", "share/" + abi, "%s/share" % (abi,)])
35+
36+
wasi_sdk_sysroots = repository_rule(_wasi_sdk_sysroots)
37+
38+
def _libclang_rt_wasm32(ctx):
39+
ctx.file("BUILD.bazel", """
40+
exports_files(glob(["*.a"]))
41+
""")
42+
43+
ctx.download_and_extract(
44+
integrity = "sha256-fjPA33WLkEabHePKFY4tCn9xk01YhFJbpqNy3gs7Dsc=",
45+
stripPrefix = "libclang_rt.builtins-wasm32-wasi-24.0",
46+
url = ["https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-24/libclang_rt.builtins-wasm32-wasi-24.0.tar.gz"],
47+
)
48+
49+
libclang_rt_wasm32 = repository_rule(_libclang_rt_wasm32)

tests/wasm_strlen.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <stdint.h>
2+
#include <string.h>
3+
4+
__attribute__((export_name("strlen")))
5+
uint32_t wasm_strlen(char *s) {
6+
return strlen(s);
7+
}

tests/wasm_strlen_nolibc.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
__attribute__((export_name("strlen")))
2+
unsigned long wasm_strlen(char *s) {
3+
unsigned long len = 0;
4+
for (; *s; len++) {}
5+
return len;
6+
}

0 commit comments

Comments
 (0)