4d41304def
This can successfully build a C library: bazel build --config=clang //third_party:libpng This can build and run a statically-linked executable: bazel test --config=clang //:bazel_test For more verbose compile and linking output, add the `--features diagnostic` flag to a Bazel command (see _make_diagnostic_flags() in toolchain/clang_toolchain_config.bzl. Similarly, a `--features print_search_dirs` can be used to show where clang is looking for libraries etc to link against. These features are made available for easier debugging. Suggested review order: - Read https://docs.bazel.build/versions/4.2.1/tutorial/cc-toolchain-config.html if unfamiliar with setting up C++ toolchains in Bazel - .bazelrc and WORKSPACE.bazel that configure use and download of the toolchain (Clang 13, musl 1.2.2) - toolchain/build_toolchain.bzl which downloads and assembles the toolchain (w/o installing anything on the host machine) - toolchain/BUILD.bazel and toolchain/*trampoline.sh to see the setup of the toolchain rules. - toolchain/clang_toolchain_config.bzl to see the configuration of the toolchain. Pay special attention to the various command line flags that are set. - See that tools/bazel_test.cc has made a new home in experimental/bazel_test/bazel_test.cpp, with a companion BUILD.bazel. Note the addition of some function calls that test use of the C++ standard library. The number being used to test the PNG library is the latest and greatest that verifies we are compiling the one brought in via DEPS (and not a local one). - third_party/* to see how png (and its dependent zlib) have been built. Pay special attention to the musl_compat hack to fix static linking (any idea what the real cause is?) - //BUILD.bazel to see definition of the bazel_test executable. Change-Id: I7b0922d0d45cb9be8df2fd5fa5a1f48492654d5f Bug: skia:12541 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/461178 Reviewed-by: Ben Wagner <bungeman@google.com> Reviewed-by: Leandro Lovisolo <lovisolo@google.com>
91 lines
3.3 KiB
Python
91 lines
3.3 KiB
Python
"""
|
|
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"""
|
|
|
|
# https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download
|
|
download_info = ctx.download(
|
|
url = deb,
|
|
output = "deb.ar",
|
|
sha256 = sha256,
|
|
)
|
|
|
|
# https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#execute
|
|
# This uses the extracted llvm-ar that comes with clang.
|
|
ctx.execute(["bin/llvm-ar", "x", "deb.ar"])
|
|
|
|
# https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#extract
|
|
extract_info = ctx.extract(
|
|
archive = "data.tar.xz",
|
|
output = output,
|
|
stripPrefix = prefix,
|
|
)
|
|
|
|
# Clean up
|
|
ctx.delete("deb.ar")
|
|
ctx.delete("data.tar.xz")
|
|
ctx.delete("control.tar.xz")
|
|
|
|
def _build_cpp_toolchain_impl(ctx):
|
|
# Download the clang toolchain (the extraction can take a while)
|
|
# https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download
|
|
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
|
|
_download_and_extract_deb(
|
|
ctx,
|
|
ctx.attr.musl_dev_url,
|
|
ctx.attr.musl_dev_sha256,
|
|
".",
|
|
)
|
|
|
|
# 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.
|
|
# https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#file
|
|
ctx.file(
|
|
"BUILD.bazel",
|
|
content = """
|
|
filegroup(
|
|
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 = "",
|
|
)
|