Skip to content

Commit

Permalink
llvm_toolchain: add compiler_rt for wasm32/wasi
Browse files Browse the repository at this point in the history
  • Loading branch information
rrbutani committed Aug 17, 2021
1 parent ae0d085 commit b69988e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
5 changes: 5 additions & 0 deletions toolchain/internal/configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
load(
"@com_grail_bazel_toolchain//toolchain/internal:extra_targets.bzl",
_cpu_names = "cpu_names",
_extra_target_setup = "extra_target_setup",
_overrides_for_target = "overrides_for_target",
_split_target_triple = "split_target_triple",
_target_triple_to_constraints = "target_triple_to_constraints",
Expand Down Expand Up @@ -296,6 +297,10 @@ def llvm_register_toolchains():
if not _download_llvm(rctx):
_download_llvm_preconfigured(rctx)

# Finally, do additional set up for the extra targets:
for target in rctx.attr.extra_targets:
_extra_target_setup(rctx, target)

def conditional_cc_toolchain(
name,
toolchain_config,
Expand Down
12 changes: 11 additions & 1 deletion toolchain/internal/extra_targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,17 @@ def sysroot_for_target(rctx, triple):

return None

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


# TODO: I think compiler_rt for wasi can be used on
# `wasm32-unknown-unknown` too.
if arch == "wasm32" and (os == "wasi" or os == "unknown" or os == "none"):
install_wasi_compiler_rt(rctx)
else:
print(
("`{}` support has not been added to bazel-toolchain; you may " +
"need to grab compiler_rt or do additional toolchain setup " +
"yourself!" + README).format(triple)
)
34 changes: 32 additions & 2 deletions toolchain/internal/extra_targets/wasi.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def get_wasi_sysroot(rctx):
)

print(
"\n\nIt worked! Feel free to make a PR adding `{}` as the WASI URL for LLVM {} with sha256 = `{}`.\n\n".format(
"\n\nIt worked! Feel free to make a PR adding `{}` as the WASI sysroot URL for LLVM {} with sha256 = `{}`.\n\n".format(
url,
llvm_major_version,
res.sha256
Expand Down Expand Up @@ -104,4 +104,34 @@ def wasi_compiler_rt_url(llvm_major_version):
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)

def install_wasi_compiler_rt(rctx):
pass
llvm_version = rctx.attr.llvm_version
llvm_major_version = int(llvm_version.split(".")[0])
common_download_params = {
"output": "lib/clang/{}/lib/wasi".format(llvm_version),
"stripPrefix": "lib/wasi",
"canonical_id": str(llvm_major_version),
}

if llvm_major_version in WASI_COMPILER_RT_LINKS:
url, sha = WASI_COMPILER_RT_LINKS[llvm_major_version]
rctx.download_and_extract(
url = url,
sha256 = sha,
**common_download_params
)
else:
url = wasi_compiler_rt_url(llvm_major_version)
print("We don't have a WASI compiler_rt URL for LLVM {}; we'll try to use `{}`..".format(llvm_major_version, url))

res = rctx.download_and_extract(
url = url,
**common_download_params
)

print(
"\n\nIt worked! Feel free to make a PR adding `{}` as the WASI compiler_rt URL for LLVM {} with sha256 = `{}`.\n\n".format(
url,
llvm_major_version,
res.sha256
)
)

0 comments on commit b69988e

Please sign in to comment.