Switch to a preconfigured Clang for k8 and armv7

This one reuses more of Bazel's builtin features to remain up to date
more easily, and it's easier to expand support for more platforms. This
also takes care of upgrading to a newer Clang.

This will require updating to raspios bullseye on the Raspberry Pis.
This also renames armhf-debian to armv7 to match the broader Bazel
ecosystem's name for this architecture.

Change-Id: I3e2a4f4efea43e76314ca34a9293c7b4b45edd2c
Signed-off-by: Brian Silverman <bsilver16834@gmail.com>
diff --git a/third_party/bazel-toolchain/toolchain/internal/common.bzl b/third_party/bazel-toolchain/toolchain/internal/common.bzl
index a0e73c9..96e0f68 100644
--- a/third_party/bazel-toolchain/toolchain/internal/common.bzl
+++ b/third_party/bazel-toolchain/toolchain/internal/common.bzl
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-SUPPORTED_TARGETS = [("linux", "x86_64"), ("linux", "aarch64"), ("darwin", "x86_64")]
+SUPPORTED_TARGETS = [("linux", "x86_64"), ("linux", "aarch64"), ("linux", "armv7"), ("darwin", "x86_64")]
 
 host_tool_features = struct(
     SUPPORTS_ARG_FILE = "supports_arg_file",
diff --git a/third_party/bazel-toolchain/toolchain/internal/configure.bzl b/third_party/bazel-toolchain/toolchain/internal/configure.bzl
index 3aba341..6a183fc 100644
--- a/third_party/bazel-toolchain/toolchain/internal/configure.bzl
+++ b/third_party/bazel-toolchain/toolchain/internal/configure.bzl
@@ -37,10 +37,15 @@
         return ""
     return ("\n" + 12 * " ").join(["\"%s\"," % d for d in dirs])
 
+def _is_absolute(path):
+    return path[0] == "/" and (len(path) == 1 or path[1] != "/")
+
 def llvm_config_impl(rctx):
     _check_os_arch_keys(rctx.attr.toolchain_roots)
+    _check_os_arch_keys(rctx.attr.target_toolchain_roots)
     _check_os_arch_keys(rctx.attr.sysroot)
     _check_os_arch_keys(rctx.attr.cxx_builtin_include_directories)
+    _check_os_arch_keys(rctx.attr.standard_libraries)
 
     os = _os(rctx)
     if os == "windows":
@@ -61,18 +66,27 @@
 
     # Check if the toolchain root is an absolute path.
     use_absolute_paths = rctx.attr.absolute_paths
-    if toolchain_root[0] == "/" and (len(toolchain_root) == 1 or toolchain_root[1] != "/"):
+    for target_toolchain_root in rctx.attr.target_toolchain_roots.values():
+        if _is_absolute(toolchain_root) != _is_absolute(target_toolchain_root):
+            fail("Host and target toolchain roots must both be absolute or not")
+    if _is_absolute(toolchain_root):
         use_absolute_paths = True
 
+    target_llvm_repo_paths = {}
     if use_absolute_paths:
         llvm_repo_label = Label(toolchain_root + ":BUILD.bazel")  # Exact target does not matter.
         llvm_repo_path = _canonical_dir_path(str(rctx.path(llvm_repo_label).dirname))
+        for a_key in rctx.attr.target_toolchain_roots:
+            target_llvm_repo_label = Label(rctx.attr.target_toolchain_roots[a_key] + ":BUILD.bazel")
+            target_llvm_repo_paths[a_key] = _canonical_dir_path(str(rctx.path(target_llvm_repo_label).dirname))
         config_repo_path = _canonical_dir_path(str(rctx.path("")))
         toolchain_path_prefix = llvm_repo_path
         tools_path_prefix = llvm_repo_path
         wrapper_bin_prefix = config_repo_path
     else:
         llvm_repo_path = _pkg_path_from_label(Label(toolchain_root + ":BUILD.bazel"))
+        for a_key in rctx.attr.target_toolchain_roots:
+            target_llvm_repo_paths[a_key] = _pkg_path_from_label(Label(rctx.attr.target_toolchain_roots[a_key] + ":BUILD.bazel"))
         config_repo_path = "external/%s/" % rctx.name
 
         # tools can only be defined in a subdirectory of config_repo_path,
@@ -100,13 +114,24 @@
         os = os,
         arch = arch,
         toolchain_root = toolchain_root,
+        additional_target_compatible_with_dict = rctx.attr.additional_target_compatible_with,
+        target_toolchain_roots_dict = rctx.attr.target_toolchain_roots,
         toolchain_path_prefix = toolchain_path_prefix,
+        target_toolchain_path_prefixes_dict = target_llvm_repo_paths,
         tools_path_prefix = tools_path_prefix,
         wrapper_bin_prefix = wrapper_bin_prefix,
         additional_include_dirs_dict = rctx.attr.cxx_builtin_include_directories,
         sysroot_dict = rctx.attr.sysroot,
         default_sysroot_path = default_sysroot_path,
         llvm_version = rctx.attr.llvm_version,
+        standard_libraries_dict = rctx.attr.standard_libraries,
+        static_libstdcxx = rctx.attr.static_libstdcxx,
+        conlyopts_dict = rctx.attr.conlyopts,
+        cxxopts_dict = rctx.attr.cxxopts,
+        copts_dict = rctx.attr.copts,
+        opt_copts_dict = rctx.attr.opt_copts,
+        dbg_copts_dict = rctx.attr.dbg_copts,
+        linkopts_dict = rctx.attr.linkopts,
     )
     host_tools_info = dict([
         pair
@@ -237,7 +262,8 @@
 
     extra_files_str = ", \":llvm\", \":wrapper-files\""
 
-    additional_include_dirs = toolchain_info.additional_include_dirs_dict.get(_os_arch_pair(target_os, target_arch))
+    key = _os_arch_pair(target_os, target_arch)
+    additional_include_dirs = toolchain_info.additional_include_dirs_dict.get(key)
     additional_include_dirs_str = "[]"
     if additional_include_dirs:
         additional_include_dirs_str = "[{}]".format(
@@ -250,6 +276,25 @@
     # them into `dict`s.
     host_tools_info = json.decode(json.encode(host_tools_info))
 
+    standard_library = toolchain_info.standard_libraries_dict.get(key, "")
+    conlyopts = toolchain_info.conlyopts_dict.get(key, [])
+    cxxopts = toolchain_info.cxxopts_dict.get(key, [])
+    copts = toolchain_info.copts_dict.get(key, [])
+    opt_copts = toolchain_info.opt_copts_dict.get(key, [])
+    dbg_copts = toolchain_info.dbg_copts_dict.get(key, [])
+    linkopts = toolchain_info.linkopts_dict.get(key, [])
+    target_toolchain_root = toolchain_info.toolchain_root
+    if key in toolchain_info.target_toolchain_roots_dict:
+        target_toolchain_root = toolchain_info.target_toolchain_roots_dict[key]
+    elif "" in toolchain_info.target_toolchain_roots_dict:
+        target_toolchain_root = toolchain_info.target_toolchain_roots_dict[""]
+    target_toolchain_path_prefix = toolchain_info.toolchain_path_prefix
+    if key in toolchain_info.target_toolchain_path_prefixes_dict:
+        target_toolchain_path_prefix = toolchain_info.target_toolchain_path_prefixes_dict[key]
+    elif "" in toolchain_info.target_toolchain_roots_dict:
+        target_toolchain_path_prefix = toolchain_info.target_toolchain_path_prefixes_dict[""]
+    additional_target_compatible_with = toolchain_info.additional_target_compatible_with_dict.get(key, [])
+
     template = """
 # CC toolchain for cc-clang-{suffix}.
 
@@ -260,12 +305,21 @@
     target_arch = "{target_arch}",
     target_os = "{target_os}",
     toolchain_path_prefix = "{toolchain_path_prefix}",
+    target_toolchain_path_prefix = "{target_toolchain_path_prefix}",
     tools_path_prefix = "{tools_path_prefix}",
     wrapper_bin_prefix = "{wrapper_bin_prefix}",
     sysroot_path = "{sysroot_path}",
     additional_include_dirs = {additional_include_dirs_str},
     llvm_version = "{llvm_version}",
     host_tools_info = {host_tools_info},
+    standard_library = "{standard_library}",
+    static_libstdcxx = {static_libstdcxx},
+    conlyopts = {conlyopts},
+    cxxopts = {cxxopts},
+    copts = {copts},
+    opt_copts = {opt_copts},
+    dbg_copts = {dbg_copts},
+    linkopts = {linkopts},
 )
 
 toolchain(
@@ -277,7 +331,7 @@
     target_compatible_with = [
         "@platforms//cpu:{target_arch}",
         "@platforms//os:{target_os_bzl}",
-    ],
+    ] + {additional_target_compatible_with},
     toolchain = ":cc-clang-{suffix}",
     toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
 )
@@ -307,7 +361,7 @@
     name = "compiler-components-{suffix}",
     srcs = [
         "{toolchain_root}:clang",
-        "{toolchain_root}:include",
+        "{target_toolchain_root}:include",
         ":sysroot-components-{suffix}",
     ],
 )
@@ -318,7 +372,7 @@
         "{toolchain_root}:clang",
         "{toolchain_root}:ld",
         "{toolchain_root}:ar",
-        "{toolchain_root}:lib",
+        "{target_toolchain_root}:lib",
         ":sysroot-components-{suffix}",
     ],
 )
@@ -363,8 +417,11 @@
         host_arch = host_arch,
         target_os_bzl = target_os_bzl,
         host_os_bzl = host_os_bzl,
+        additional_target_compatible_with = additional_target_compatible_with,
         toolchain_root = toolchain_info.toolchain_root,
         toolchain_path_prefix = toolchain_info.toolchain_path_prefix,
+        target_toolchain_root = target_toolchain_root,
+        target_toolchain_path_prefix = target_toolchain_path_prefix,
         tools_path_prefix = toolchain_info.tools_path_prefix,
         wrapper_bin_prefix = toolchain_info.wrapper_bin_prefix,
         additional_include_dirs_str = additional_include_dirs_str,
@@ -373,4 +430,12 @@
         llvm_version = toolchain_info.llvm_version,
         extra_files_str = extra_files_str,
         host_tools_info = host_tools_info,
+        standard_library = standard_library,
+        static_libstdcxx = toolchain_info.static_libstdcxx,
+        conlyopts = conlyopts,
+        cxxopts = cxxopts,
+        copts = copts,
+        opt_copts = opt_copts,
+        dbg_copts = dbg_copts,
+        linkopts = linkopts,
     )
diff --git a/third_party/bazel-toolchain/toolchain/internal/llvm_distributions.bzl b/third_party/bazel-toolchain/toolchain/internal/llvm_distributions.bzl
index 8d3441b..4ba4601 100644
--- a/third_party/bazel-toolchain/toolchain/internal/llvm_distributions.bzl
+++ b/third_party/bazel-toolchain/toolchain/internal/llvm_distributions.bzl
@@ -174,6 +174,7 @@
     "clang+llvm-13.0.0-amd64-unknown-freebsd13.tar.xz": "c4f15e156afaa530eb47ba13c46800275102af535ed48e395aed4c1decc1eaa1",
     "clang+llvm-13.0.0-i386-unknown-freebsd12.tar.xz": "4d14b19c082438a5ceed61e538e5a0298018b1773e8ba2e990f3fbe33492f48f",
     "clang+llvm-13.0.0-i386-unknown-freebsd13.tar.xz": "f8e105c6ac2fd517ae5ed8ef9b9bab4b015fe89a06c90c3dd5d5c7933dca2276",
+    "clang+llvm-13.0.0-armv7a-linux-gnueabihf.tar.xz": "e17aacd843cc7c3c3c27a0d2293af8b3e9ad76408f7c178a92a182f18e7734e5",
     "clang+llvm-13.0.0-powerpc64le-linux-rhel-7.9.tar.xz": "cfade83f6da572a8ab0e4796d1f657967b342e98202c26e76c857879fb2fa2d2",
     "clang+llvm-13.0.0-powerpc64le-linux-ubuntu-18.04.tar.xz": "5d79e9e2919866a91431589355f6d07f35d439458ff12cb8f36093fb314a7028",
     "clang+llvm-13.0.0-x86_64-apple-darwin.tar.xz": "d051234eca1db1f5e4bc08c64937c879c7098900f7a0370f3ceb7544816a8b09",