blob: 78f59419cfd50c2decc4046830813cacedbe18c4 [file] [log] [blame]
This file assembles a toolchain for Linux using the Clang Compiler and musl.
It currently makes use of musl and not glibc because the pre-compiled libraries from the latter
have absolute paths baked in and this makes linking difficult. The pre-compiled musl library
does not have this restriction and is much easier to statically link in as a result.
As inputs, it takes external URLs from which to download the clang binaries/libraries/includes
as well as the musl headers and pre-compiled binaries. Those files are downloaded and put
into one folder, with a little bit of re-arrangement so clang can find files (like the C runtime).
def _download_and_extract_deb(ctx, deb, sha256, prefix, output = ""):
"""Downloads a debian file and extracts the data into the provided output directory"""
download_info =
url = deb,
output = "",
sha256 = sha256,
# This uses the extracted llvm-ar that comes with clang.
ctx.execute(["bin/llvm-ar", "x", ""])
extract_info = ctx.extract(
archive = "data.tar.xz",
output = output,
stripPrefix = prefix,
# Clean up
def _build_cpp_toolchain_impl(ctx):
# Download the clang toolchain (the extraction can take a while)
download_info = ctx.download_and_extract(
url = ctx.attr.clang_url,
output = "",
stripPrefix = ctx.attr.clang_prefix,
sha256 = ctx.attr.clang_sha256,
# This puts the musl include files in ${PWD}/usr/include/x86_64-linux-musl
# and the runtime files and lib.a files in ${PWD}/usr/lib/x86_64-linux-musl
# kjlubick@ cannot figure out how to get clang to look in ${PWD}/usr/lib/x86_64-linux-musl
# for the crt1.o files, so we'll move them to ${PWD}/usr/lib/ where clang *is* looking.
for file in ["crt1.o", "crtn.o", "Scrt1.o", "crti.o", "rcrt1.o"]:
ctx.execute(["cp", "usr/lib/x86_64-linux-musl/" + file, "usr/lib/"])
# Create a BUILD.bazel file that makes all the files in this subfolder
# available for use in rules, i.e. in the toolchain declaration.
content = """
name = "all_files",
srcs = glob([
visibility = ["//visibility:public"]
executable = False,
build_cpp_toolchain = repository_rule(
implementation = _build_cpp_toolchain_impl,
attrs = {
"clang_url": attr.string(mandatory = True),
"clang_sha256": attr.string(mandatory = True),
"clang_prefix": attr.string(mandatory = True),
"musl_dev_url": attr.string(mandatory = True),
"musl_dev_sha256": attr.string(mandatory = True),
doc = "",