Skip to content

Commit 017f21c

Browse files
committed
llvm_toolchain: add compiler_rt for wasm32/wasi
1 parent 2d414f2 commit 017f21c

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

toolchain/internal/configure.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
load(
1616
"@com_grail_bazel_toolchain//toolchain/internal:extra_targets.bzl",
1717
_cpu_names = "cpu_names",
18+
_extra_target_setup = "extra_target_setup",
1819
_overrides_for_target = "overrides_for_target",
1920
_split_target_triple = "split_target_triple",
2021
_target_triple_to_constraints = "target_triple_to_constraints",
@@ -296,6 +297,10 @@ def llvm_register_toolchains():
296297
if not _download_llvm(rctx):
297298
_download_llvm_preconfigured(rctx)
298299

300+
# Finally, do additional set up for the extra targets:
301+
for target in rctx.attr.extra_targets:
302+
_extra_target_setup(rctx, target)
303+
299304
def conditional_cc_toolchain(
300305
name,
301306
toolchain_config,

toolchain/internal/extra_targets.bzl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,17 @@ def sysroot_for_target(rctx, triple):
391391

392392
return None
393393

394+
# Runs *after* the toolchain has been fetched and extracted.
394395
def extra_target_setup(rctx, triple):
395396
arch, _vendor, os, _env = split_target_triple(triple)
396397

397-
398+
# TODO: I think compiler_rt for wasi can be used on
399+
# `wasm32-unknown-unknown` too.
400+
if arch == "wasm32" and (os == "wasi" or os == "unknown" or os == "none"):
401+
install_wasi_compiler_rt(rctx)
402+
else:
403+
print(
404+
("`{}` support has not been added to bazel-toolchain; you may " +
405+
"need to grab compiler_rt or do additional toolchain setup " +
406+
"yourself!" + README).format(triple)
407+
)

toolchain/internal/extra_targets/wasi.bzl

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def get_wasi_sysroot(rctx):
5454
)
5555

5656
print(
57-
"\n\nIt worked! Feel free to make a PR adding `{}` as the WASI URL for LLVM {} with sha256 = `{}`.\n\n".format(
57+
"\n\nIt worked! Feel free to make a PR adding `{}` as the WASI sysroot URL for LLVM {} with sha256 = `{}`.\n\n".format(
5858
url,
5959
llvm_major_version,
6060
res.sha256
@@ -104,4 +104,34 @@ def wasi_compiler_rt_url(llvm_major_version):
104104
return "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-{v}/libclang_rt.builtins-wasm32-wasi-{v}.0.tar.gz".format(v = llvm_major_version)
105105

106106
def install_wasi_compiler_rt(rctx):
107-
pass
107+
llvm_version = rctx.attr.llvm_version
108+
llvm_major_version = int(llvm_version.split(".")[0])
109+
common_download_params = {
110+
"output": "lib/clang/{}/lib/wasi".format(llvm_version),
111+
"stripPrefix": "lib/wasi",
112+
"canonical_id": str(llvm_major_version),
113+
}
114+
115+
if llvm_major_version in WASI_COMPILER_RT_LINKS:
116+
url, sha = WASI_COMPILER_RT_LINKS[llvm_major_version]
117+
rctx.download_and_extract(
118+
url = url,
119+
sha256 = sha,
120+
**common_download_params
121+
)
122+
else:
123+
url = wasi_compiler_rt_url(llvm_major_version)
124+
print("We don't have a WASI compiler_rt URL for LLVM {}; we'll try to use `{}`..".format(llvm_major_version, url))
125+
126+
res = rctx.download_and_extract(
127+
url = url,
128+
**common_download_params
129+
)
130+
131+
print(
132+
"\n\nIt worked! Feel free to make a PR adding `{}` as the WASI compiler_rt URL for LLVM {} with sha256 = `{}`.\n\n".format(
133+
url,
134+
llvm_major_version,
135+
res.sha256
136+
)
137+
)

0 commit comments

Comments
 (0)