Skip to content

Commit bda1c9f

Browse files
authored
Support WebAssembly target platforms wasm{32,64}-unknown-unknown (#405)
1 parent a80f2b4 commit bda1c9f

20 files changed

+325
-17
lines changed

platforms/BUILD.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,19 @@ platform(
4545
"@platforms//cpu:aarch64",
4646
],
4747
)
48+
49+
platform(
50+
name = "wasm32",
51+
constraint_values = [
52+
"@platforms//os:none",
53+
"@platforms//cpu:wasm32",
54+
],
55+
)
56+
57+
platform(
58+
name = "wasm64",
59+
constraint_values = [
60+
"@platforms//os:none",
61+
"@platforms//cpu:wasm64",
62+
],
63+
)

tests/MODULE.bazel

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,40 @@ 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+
# WebAssembly tests use a separate (newer) version of LLVM to exercise
187+
# support for experimental features such as wasm64.
188+
llvm_versions = {
189+
# The most recent LLVM as of 2024-10-17
190+
"": "19.1.0",
191+
},
192+
stdlib = {
193+
"wasm32": "libc",
194+
"wasm64": "none",
195+
},
196+
libclang_rt = {
197+
"@libclang_rt_wasm32//:libclang_rt.builtins-wasm32.a": "wasm32-unknown-unknown/libclang_rt.builtins.a",
198+
},
199+
)
200+
llvm.sysroot(
201+
name = "llvm_toolchain_wasm",
202+
label = "@wasi_sdk_sysroots//wasm32-wasip2",
203+
targets = ["wasm32"],
204+
)
205+
llvm.sysroot(
206+
name = "llvm_toolchain_wasm",
207+
label = "@wasi_sdk_sysroots//empty",
208+
targets = ["wasm64"],
209+
)
210+
211+
use_repo(llvm, "llvm_toolchain_wasm")
212+
register_toolchains("@llvm_toolchain_wasm//:all")
213+
214+
wasi_sdk_sysroots = use_repo_rule("//wasm:wasi_sdk.bzl", "wasi_sdk_sysroots")
215+
wasi_sdk_sysroots(name = "wasi_sdk_sysroots")
216+
217+
libclang_rt_wasm32 = use_repo_rule("//wasm:wasi_sdk.bzl", "libclang_rt_wasm32")
218+
libclang_rt_wasm32(name = "libclang_rt_wasm32")

tests/WORKSPACE

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,41 @@ 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.
160+
llvm_toolchain(
161+
name = "llvm_toolchain_wasm",
162+
libclang_rt = {
163+
"@libclang_rt_wasm32//:libclang_rt.builtins-wasm32.a": "wasm32-unknown-unknown/libclang_rt.builtins.a",
164+
},
165+
# WebAssembly tests use a separate (newer) version of LLVM to exercise
166+
# support for experimental features such as wasm64.
167+
llvm_versions = {
168+
# The most recent LLVM as of 2024-10-17
169+
"": "19.1.0",
170+
},
171+
stdlib = {
172+
"wasm32": "libc",
173+
"wasm64": "none",
174+
},
175+
sysroot = {
176+
"wasm32": "@wasi_sdk_sysroots//wasm32-wasip2",
177+
"wasm64": "@wasi_sdk_sysroots//empty",
178+
},
179+
)
180+
181+
load(
182+
"@llvm_toolchain_wasm//:toolchains.bzl",
183+
llvm_register_toolchains_wasm = "llvm_register_toolchains",
184+
)
185+
186+
llvm_register_toolchains_wasm()
187+
188+
load("//wasm:wasi_sdk.bzl", "libclang_rt_wasm32", "wasi_sdk_sysroots")
189+
190+
libclang_rt_wasm32(name = "libclang_rt_wasm32")
191+
192+
wasi_sdk_sysroots(name = "wasi_sdk_sysroots")
193+
159194
## Test dependencies.
160195

161196
# Well known repos; present here only for testing.

tests/scripts/debian_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ set -exuo pipefail
3030
# Common setup
3131
export DEBIAN_FRONTEND=noninteractive
3232
apt-get -qq update
33-
apt-get -qq -y install curl libtinfo5 zlib1g-dev >/dev/null
33+
apt-get -qq -y install curl libtinfo5 libxml2 zlib1g-dev >/dev/null
3434
# The above command gives some verbose output that can not be silenced easily.
3535
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=288778
3636

tests/scripts/linux_sysroot_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ set -exuo pipefail
3030
# Common setup
3131
export DEBIAN_FRONTEND=noninteractive
3232
apt-get -qq update
33-
apt-get -qq -y install curl libtinfo5 zlib1g-dev >/dev/null
33+
apt-get -qq -y install curl libtinfo5 libxml2 zlib1g-dev >/dev/null
3434
# The above command gives some verbose output that can not be silenced easily.
3535
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=288778
3636

tests/scripts/run_docker_exec_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ docker build --platform=linux/amd64 --pull --tag=bazel-docker-sandbox - <<-EOF
4040
FROM ${base_image}
4141
ENV DEBIAN_FRONTEND=noninteractive
4242
RUN apt-get -qq update && \
43-
apt-get -qq -y install libtinfo5 zlib1g-dev libxml2
43+
apt-get -qq -y install libtinfo5 libxml2 zlib1g-dev libxml2
4444
EOF
4545

4646
build_args=(

tests/scripts/run_tests.sh

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@
1616
set -euo pipefail
1717

1818
toolchain_name=""
19+
disable_wasm_tests=""
1920

20-
while getopts "t:h" opt; do
21+
while getopts "t:hW" opt; do
2122
case "${opt}" in
2223
"t") toolchain_name="${OPTARG}" ;;
2324
"h")
2425
echo "Usage:"
2526
echo "-t - Toolchain name to use for testing; default is llvm_toolchain"
2627
exit 2
2728
;;
29+
"W")
30+
disable_wasm_tests="yes"
31+
;;
2832
*)
2933
echo "invalid option: -${OPTARG}"
3034
exit 1
@@ -62,3 +66,24 @@ fi
6266
# Note that the following flags are currently known to cause issues in migration tests:
6367
# --incompatible_disallow_struct_provider_syntax # https://github.com/bazelbuild/bazel/issues/7347
6468
# --incompatible_no_rule_outputs_param # from rules_rust
69+
70+
# WebAssembly tests use a separate (newer) version of LLVM to exercise support
71+
# for experimental features such as wasm64, which can cause the CI environment
72+
# to run out of disk space.
73+
#
74+
# Mitigate this by expunging the workspace before trying to build Wasm targets.
75+
if [[ -z ${toolchain_name} && -z ${disable_wasm_tests} ]]; then
76+
# Redefine `test_args` without `--linkopt=-Wl,-v`, which breaks `wasm-ld`.
77+
#
78+
# https://github.com/llvm/llvm-project/issues/112836
79+
test_args=(
80+
"--copt=-v"
81+
"--linkopt=-Wl,-t"
82+
)
83+
wasm_targets=(
84+
"//wasm:all"
85+
)
86+
"${bazel}" clean --expunge
87+
"${bazel}" ${TEST_MIGRATION:+"--strict"} --bazelrc=/dev/null test \
88+
"${common_test_args[@]}" "${test_args[@]}" "${wasm_targets[@]}"
89+
fi

tests/scripts/ubuntu_20_04_test.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@ set -exuo pipefail
3030
# Common setup
3131
export DEBIAN_FRONTEND=noninteractive
3232
apt-get -qq update
33-
apt-get -qq -y install apt-utils curl libtinfo5 pkg-config zlib1g-dev >/dev/null
33+
apt-get -qq -y install apt-utils curl libtinfo5 libxml2 pkg-config zlib1g-dev >/dev/null
3434
# The above command gives some verbose output that can not be silenced easily.
3535
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=288778
3636
37+
# The WebAssembly tests use an LLVM version that is too new for the GNU libc
38+
# distributed in Ubuntu 20.04.
39+
disable_wasm_tests='-W'
40+
3741
# Run tests
3842
cd /src
39-
tests/scripts/run_tests.sh
43+
tests/scripts/run_tests.sh \${disable_wasm_tests}
4044
"""
4145
done

tests/scripts/ubuntu_22_04_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ set -exuo pipefail
3030
# Common setup
3131
export DEBIAN_FRONTEND=noninteractive
3232
apt-get -qq update
33-
apt-get -qq -y install curl libtinfo5 zlib1g-dev >/dev/null
33+
apt-get -qq -y install curl libtinfo5 libxml2 zlib1g-dev >/dev/null
3434
# The above command gives some verbose output that can not be silenced easily.
3535
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=288778
3636

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",

0 commit comments

Comments
 (0)