From 9b7db63e6f004b7c342fb56441bb68756d7ea2c2 Mon Sep 17 00:00:00 2001 From: Kevin Lubick Date: Fri, 1 Apr 2022 08:29:11 -0400 Subject: [PATCH] [bazel] Sketch out changes for Mac toolchain Change-Id: Idae84d8d9538012e5cfb75a1a477dbc72a4da5bc Bug: skia:13125 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/526264 Reviewed-by: Jorge Betancourt --- .bazelrc | 11 ++++-- WORKSPACE.bazel | 10 +++--- toolchain/BUILD.bazel | 36 ++++++++++--------- ...bzl => download_linux_amd64_toolchain.bzl} | 23 ++++++++---- toolchain/download_mac_m1_toolchain.bzl | 20 +++++++++++ toolchain/download_toolchains.bzl | 29 +++++++++++++++ ...g.bzl => linux_amd64_toolchain_config.bzl} | 6 ++-- 7 files changed, 101 insertions(+), 34 deletions(-) rename toolchain/{build_toolchain.bzl => download_linux_amd64_toolchain.bzl} (90%) create mode 100644 toolchain/download_mac_m1_toolchain.bzl create mode 100644 toolchain/download_toolchains.bzl rename toolchain/{clang_toolchain_config.bzl => linux_amd64_toolchain_config.bzl} (99%) diff --git a/.bazelrc b/.bazelrc index 3eedabc9ee..d9b296a86d 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,8 +1,13 @@ +# All of our clang builds should use the hermetic toolchain build:clang --crosstool_top=//toolchain:clang_suite -build:clang --compiler=clang -# Use the default Bazel C++ toolchain to build the tools used during the build. -build:clang --host_crosstool_top=@bazel_tools//tools/cpp:toolchain +# We don't have a great way to test the host system in order to use the right +# version of the toolchain. So we need to have the user specify what their host is. +build:clang_linux --config=clang +build:clang_linux --compiler=host_is_linux_amd64 + +build:clang_mac --config=clang +build:clang_mac --compiler=host_is_mac_M1 # ============================================================================= # Alias to build configurations below. This makes configuring things from diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index a84aec3b4c..cd4b19de46 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -1,7 +1,10 @@ workspace(name = "skia") +load("//toolchain:download_toolchains.bzl", "download_toolchains_for_skia") + +download_toolchains_for_skia("clang_linux_amd64", "clang_mac_m1") + load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -load("//toolchain:build_toolchain.bzl", "build_cpp_toolchain") # See https://github.com/emscripten-core/emsdk/tree/85d27a4a2a60d591613a305b14ae438c2bb3ce11/bazel#setup-instructions http_archive( @@ -24,11 +27,6 @@ load("@emsdk//:emscripten_deps.bzl", emsdk_emscripten_deps = "emscripten_deps") emsdk_emscripten_deps(emscripten_version = "3.1.4") -build_cpp_toolchain( - # Meant to run on amd64 linux and compile for amd64 linux. - name = "clang_linux_amd64", -) - http_archive( name = "bazel_skylib", sha256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d", diff --git a/toolchain/BUILD.bazel b/toolchain/BUILD.bazel index 0f04a5f583..dac835816a 100644 --- a/toolchain/BUILD.bazel +++ b/toolchain/BUILD.bazel @@ -1,23 +1,27 @@ load("@rules_cc//cc:defs.bzl", "cc_toolchain", "cc_toolchain_suite") -load(":clang_toolchain_config.bzl", "provide_clang_toolchain_config") +load(":linux_amd64_toolchain_config.bzl", "provide_linux_amd64_toolchain_config") package(default_visibility = ["//visibility:public"]) -# https://docs.bazel.build/versions/main/be/c-cpp.html#cc_toolchain_suite +# https://bazel.build/reference/be/c-cpp#cc_toolchain_suite cc_toolchain_suite( name = "clang_suite", toolchains = { # The key is target_cpu|compiler - "k8|clang": ":clang_toolchain", - "x86_64|clang": ":clang_toolchain", - "k8": ":clang_toolchain", + # compiler appears to be a string we can choose arbitrarily + # https://bazel.build/reference/command-line-reference?hl=en#flag--compiler + "k8|host_is_linux_amd64": ":linux_amd64_host", + "x86_64|host_is_linux_amd64": ":linux_amd64_host", + "k8": ":linux_amd64_host", + # TODO(jmbetancourt) + # "arm64|host_is_mac_M1": ":mac_M1_host", }, ) filegroup(name = "not_implemented") filegroup( - name = "all-toolchain-files", + name = "all_linux_amd64_files", srcs = [ "IWYU_mapping.imp", "ar_trampoline.sh", @@ -27,20 +31,20 @@ filegroup( ], ) -provide_clang_toolchain_config( - name = "clang_toolchain_config", +provide_linux_amd64_toolchain_config( + name = "linux_amd64_toolchain_config", ) +# https://bazel.build/reference/be/c-cpp#cc_toolchain cc_toolchain( - name = "clang_toolchain", - all_files = ":all-toolchain-files", - ar_files = ":all-toolchain-files", - compiler_files = ":all-toolchain-files", + name = "linux_amd64_host", + all_files = ":all_linux_amd64_files", + ar_files = ":all_linux_amd64_files", + compiler_files = ":all_linux_amd64_files", dwp_files = ":not_implemented", - linker_files = ":all-toolchain-files", + linker_files = ":all_linux_amd64_files", objcopy_files = ":not_implemented", strip_files = ":not_implemented", - supports_param_files = 0, - toolchain_config = ":clang_toolchain_config", - toolchain_identifier = "clang-toolchain", + supports_param_files = False, + toolchain_config = ":linux_amd64_toolchain_config", ) diff --git a/toolchain/build_toolchain.bzl b/toolchain/download_linux_amd64_toolchain.bzl similarity index 90% rename from toolchain/build_toolchain.bzl rename to toolchain/download_linux_amd64_toolchain.bzl index cf3381294a..1d8ac2f5bb 100644 --- a/toolchain/build_toolchain.bzl +++ b/toolchain/download_linux_amd64_toolchain.bzl @@ -1,7 +1,16 @@ """ -This file assembles a toolchain for Linux using the Clang Compiler glibc. +This file assembles a toolchain for an amd64 Linux host using the Clang Compiler and glibc. -It downloads the necessary header files and pre-compiled static/shared libraries to +It downloads the necessary headers, executables, and pre-compiled static/shared libraries to +the external subfolder of the Bazel cache (the same place third party deps are downloaded with +http_archive or similar functions in WORKSPACE.bazel). These will be able to be used via our +custom c++ toolchain configuration (see //toolchain/clang_toolchain_config.bzl) + +Most files are downloaded as .deb files from packages.debian.org (with us acting as the dependency +resolver) and extracted to + [outputRoot (aka Bazel cache)]/[outputUserRoot]/[outputBase]/external/clang_linux_amd64 + (See https://bazel.build/docs/output_directories#layout-diagram) +which will act as our sysroot. """ # From https://github.com/llvm/llvm-project/releases/download/llvmorg-13.0.0/clang+llvm-13.0.0-x86_64-linux-gnu-ubuntu-20.04.tar.xz.sha256 @@ -166,7 +175,7 @@ def _download_and_extract_deb(ctx, deb, sha256, prefix, output = ""): # Clean up ctx.delete("tmp") -def _build_cpp_toolchain_impl(ctx): +def _download_linux_amd64_toolchain_impl(ctx): # Workaround for Bazel not yet supporting .ar files # See https://skia-review.googlesource.com/c/buildbot/+/524764 # https://bazel.build/rules/lib/repository_ctx#download @@ -220,8 +229,10 @@ def _mirror(arr): return [arr[1]] return arr -build_cpp_toolchain = repository_rule( - implementation = _build_cpp_toolchain_impl, +# https://bazel.build/rules/repository_rules +download_linux_amd64_toolchain = repository_rule( + implementation = _download_linux_amd64_toolchain_impl, attrs = {}, - doc = "", + doc = "Downloads clang, and all supporting headers, executables, " + + "and shared libraries required to build Skia on a Linux amd64 host", ) diff --git a/toolchain/download_mac_m1_toolchain.bzl b/toolchain/download_mac_m1_toolchain.bzl new file mode 100644 index 0000000000..a1e291da09 --- /dev/null +++ b/toolchain/download_mac_m1_toolchain.bzl @@ -0,0 +1,20 @@ +""" +This file assembles a toolchain for a Mac M1 host using the Clang Compiler and glibc. + +It downloads the necessary headers, executables, and pre-compiled static/shared libraries to +the external subfolder of the Bazel cache (the same place third party deps are downloaded with +http_archive or similar functions in WORKSPACE.bazel). These will be able to be used via our +custom c++ toolchain configuration (see //toolchain/clang_toolchain_config.bzl) +""" + +def _download_mac_m1_toolchain(ctx): + # TODO(jmbetancourt) + pass + +# https://bazel.build/rules/repository_rules +download_mac_m1_toolchain = repository_rule( + implementation = _download_mac_m1_toolchain, + attrs = {}, + doc = "Downloads clang, and all supporting headers, executables, " + + "and shared libraries required to build Skia on a Mac M1 host", +) diff --git a/toolchain/download_toolchains.bzl b/toolchain/download_toolchains.bzl new file mode 100644 index 0000000000..2df5fed3e5 --- /dev/null +++ b/toolchain/download_toolchains.bzl @@ -0,0 +1,29 @@ +""" +This file exports the various toolchains for the hosts that we support building Skia on. + +Supported: + - Linux amd64 + +Planned: + - Mac M1 + - Windows amd64 + +""" + +load(":download_linux_amd64_toolchain.bzl", "download_linux_amd64_toolchain") +load(":download_mac_m1_toolchain.bzl", "download_mac_m1_toolchain") + +# This key in this dictionary (and thus the name passed into the rule) controls what the subfolder +# will be called in the external directory. It must match what we use in the appropriate +# toolchain_config.bzl file or it will not be able to locate the sysroot to build with. +name_toolchain = { + "clang_linux_amd64": download_linux_amd64_toolchain, + "clang_mac_m1": download_mac_m1_toolchain, +} + +def download_toolchains_for_skia(*args): + for toolchain_name in args: + if toolchain_name not in name_toolchain: + fail("unrecognized toolchain name " + toolchain_name) + download_toolchain = name_toolchain[toolchain_name] + download_toolchain(name = toolchain_name) diff --git a/toolchain/clang_toolchain_config.bzl b/toolchain/linux_amd64_toolchain_config.bzl similarity index 99% rename from toolchain/clang_toolchain_config.bzl rename to toolchain/linux_amd64_toolchain_config.bzl index d6f2c4c3af..96eac91556 100644 --- a/toolchain/clang_toolchain_config.bzl +++ b/toolchain/linux_amd64_toolchain_config.bzl @@ -26,7 +26,7 @@ load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") # The location of the created clang toolchain. EXTERNAL_TOOLCHAIN = "external/clang_linux_amd64" -def _clang_impl(ctx): +def _linux_amd64_toolchain_info(ctx): action_configs = _make_action_configs() features = [] features += _make_default_flags() @@ -59,10 +59,10 @@ def _clang_impl(ctx): toolchain_identifier = "clang-toolchain", ) -provide_clang_toolchain_config = rule( +provide_linux_amd64_toolchain_config = rule( attrs = {}, provides = [CcToolchainConfigInfo], - implementation = _clang_impl, + implementation = _linux_amd64_toolchain_info, ) def _make_action_configs():