treewide: remove host_cc_toolchain, add static flags to linux platforms
With rules_rust now being able to build with our toolchain and sysroot,
we can finally get rid of the host_cc_toolchain.
Change-Id: Ic63bc34ff94d152a9c2d8a01a6036255da5e14cc
Reviewed-on: https://review.monogon.dev/c/monogon/+/4397
Tested-by: Jenkins CI
Reviewed-by: Jan Schär <jan@monogon.tech>
diff --git a/build/platforms/BUILD.bazel b/build/platforms/BUILD.bazel
index 028bb9a..c5c9838 100644
--- a/build/platforms/BUILD.bazel
+++ b/build/platforms/BUILD.bazel
@@ -5,6 +5,10 @@
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
+ flags = [
+ "--@toolchain_cc_mngn//buildmode:static",
+ "--@io_bazel_rules_go//go/config:static",
+ ],
visibility = ["//visibility:public"],
)
@@ -14,6 +18,10 @@
"@platforms//os:linux",
"@platforms//cpu:aarch64",
],
+ flags = [
+ "--@toolchain_cc_mngn//buildmode:static",
+ "--@io_bazel_rules_go//go/config:static",
+ ],
visibility = ["//visibility:public"],
)
diff --git a/build/toolchain/BUILD.bazel b/build/toolchain/BUILD.bazel
index a1d2288..e69de29 100644
--- a/build/toolchain/BUILD.bazel
+++ b/build/toolchain/BUILD.bazel
@@ -1,72 +0,0 @@
-load("@rules_cc//cc:defs.bzl", "cc_toolchain")
-load(":cc_toolchain_config.bzl", "host_cc_toolchain_config")
-
-# Toolchain definitions.
-#
-# We currently define two toolchains:
-#
-# - //build/toolchain:host_cc_toolchain , which points to our hermetic sandbox
-# sysroot default compiler toolchain. It is mainly used to target the execution platform
-# inside the sandbox (i.e. build tooling).
-# - //build/toolchain/musl-host-gcc:musl_host_toolchain , which combines the sandbox sysroot
-# gcc compiler with a sysroot tarball that targets the Metropolis node
-# runtime. This can be used to build C libraries/tools running within the
-# Metropolis node image or on the (unknown) host operating system outside the sandbox.
-
-# This file defines //build/toolchain:host_cc_toolchain.
-#
-# This is a C++ toolchain that uses GCC from the sandbox sysroot at hardcoded paths. We
-# can get away with this, as currently the entire build is performed in a hermetic
-# sandbox sysroot (see: //third_party/sandboxroot). We define this toolchain so
-# that we have full control over all configuration of it, which we need as we
-# are building some fairly odd C binaries (notably, a qboot bootloader for
-# testing).
-#
-# The host_cc toolchain suite is enabled for all cc_* targets whose
-# platform isn't matching a more specific toolchain.
-#
-# This, and :cc_toolchain_config.bzl is based on the following tutorial:
-# https://docs.bazel.build/versions/master/tutorial/cc-toolchain-config.html
-
-package(default_visibility = ["//visibility:public"])
-
-filegroup(name = "empty")
-
-cc_toolchain(
- name = "host_cc_k8_toolchain",
- all_files = ":empty",
- ar_files = ":empty",
- as_files = ":empty",
- compiler_files = ":empty",
- dwp_files = ":empty",
- linker_files = ":empty",
- objcopy_files = ":empty",
- strip_files = ":empty",
- supports_param_files = 0,
- toolchain_config = ":host_cc_k8_toolchain_config",
- toolchain_identifier = "host-k8-toolchain",
-)
-
-host_cc_toolchain_config(name = "host_cc_k8_toolchain_config")
-
-config_setting(
- name = "is_dynamic",
- flag_values = {"@toolchain_cc_mngn//buildmode:static": "False"},
-)
-
-toolchain(
- name = "host_cc_toolchain",
- exec_compatible_with = [
- "@platforms//os:linux",
- "@platforms//cpu:x86_64",
- ],
- target_compatible_with = [
- "@platforms//os:linux",
- "@platforms//cpu:x86_64",
- ],
- target_settings = [
- ":is_dynamic",
- ],
- toolchain = ":host_cc_k8_toolchain",
- toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
-)
diff --git a/build/toolchain/cc_toolchain_config.bzl b/build/toolchain/cc_toolchain_config.bzl
deleted file mode 100644
index 33925c6..0000000
--- a/build/toolchain/cc_toolchain_config.bzl
+++ /dev/null
@@ -1,248 +0,0 @@
-# Copyright 2020 The Monogon Project Authors.
-#
-# SPDX-License-Identifier: Apache-2.0
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
-load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "feature", "flag_group", "flag_set", "tool_path")
-load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
-
-all_compile_actions = [
- ACTION_NAMES.c_compile,
- ACTION_NAMES.cpp_compile,
- ACTION_NAMES.linkstamp_compile,
- ACTION_NAMES.assemble,
- ACTION_NAMES.preprocess_assemble,
- ACTION_NAMES.cpp_header_parsing,
- ACTION_NAMES.cpp_module_compile,
- ACTION_NAMES.cpp_module_codegen,
- ACTION_NAMES.clif_match,
- ACTION_NAMES.lto_backend,
-]
-
-all_cpp_compile_actions = [
- ACTION_NAMES.cpp_compile,
- ACTION_NAMES.linkstamp_compile,
- ACTION_NAMES.cpp_header_parsing,
- ACTION_NAMES.cpp_module_compile,
- ACTION_NAMES.cpp_module_codegen,
- ACTION_NAMES.clif_match,
-]
-
-preprocessor_compile_actions = [
- ACTION_NAMES.c_compile,
- ACTION_NAMES.cpp_compile,
- ACTION_NAMES.linkstamp_compile,
- ACTION_NAMES.preprocess_assemble,
- ACTION_NAMES.cpp_header_parsing,
- ACTION_NAMES.cpp_module_compile,
- ACTION_NAMES.clif_match,
-]
-
-codegen_compile_actions = [
- ACTION_NAMES.c_compile,
- ACTION_NAMES.cpp_compile,
- ACTION_NAMES.linkstamp_compile,
- ACTION_NAMES.assemble,
- ACTION_NAMES.preprocess_assemble,
- ACTION_NAMES.cpp_module_codegen,
- ACTION_NAMES.lto_backend,
-]
-
-executable_link_actions = [
- ACTION_NAMES.cpp_link_executable,
- ACTION_NAMES.lto_index_for_executable,
-]
-
-all_link_actions = [
- ACTION_NAMES.cpp_link_executable,
- ACTION_NAMES.cpp_link_dynamic_library,
- ACTION_NAMES.cpp_link_nodeps_dynamic_library,
-]
-
-lto_index_actions = [
- ACTION_NAMES.lto_index_for_executable,
- ACTION_NAMES.lto_index_for_dynamic_library,
- ACTION_NAMES.lto_index_for_nodeps_dynamic_library,
-]
-
-# This defines a minimal, barely parametrized toolchain configuration rule that
-# uses the host GCC with some possible overrides.
-
-def _host_cc_toolchain_impl(ctx):
- cpp_feature = feature(
- name = "cpp",
- enabled = ctx.attr.has_cpp,
- flag_sets = [
- flag_set(
- actions = all_cpp_compile_actions + [ACTION_NAMES.lto_backend],
- flag_groups = ([
- flag_group(
- flags = ["-std=c++17"],
- ),
- ]),
- ),
- flag_set(
- actions = all_link_actions + lto_index_actions,
- flag_groups = ([
- flag_group(
- flags = [
- "-lstdc++",
- ],
- ),
- ]),
- ),
- ],
- )
- link_full_libc_feature = feature(
- name = "link_full_libc",
- enabled = True,
- flag_sets = [
- flag_set(
- actions = all_link_actions + lto_index_actions,
- flag_groups = ([
- flag_group(
- flags = [
- "-lm",
- "-lutil",
- "-lpthread",
- ],
- ),
- ] if ctx.attr.is_glibc else []), # musl just works
- ),
- ],
- )
- pie_feature = feature(
- name = "pie",
- enabled = False,
- flag_sets = [
- flag_set(
- actions = executable_link_actions,
- flag_groups = ([
- flag_group(
- flags = [
- "-static-pie",
- ],
- ),
- ]),
- ),
- flag_set(
- actions = all_compile_actions,
- flag_groups = ([
- flag_group(
- flags = [
- "-fPIE",
- ],
- ),
- ]),
- ),
- ],
- )
- default_link_flags_feature = feature(
- name = "default_link_flags",
- enabled = True,
- flag_sets = [
- flag_set(
- actions = all_link_actions + lto_index_actions,
- flag_groups = ([
- flag_group(
- flags = [
- "-Wl,-z,relro,-z,now",
- "-pass-exit-codes",
- ],
- ),
- ]),
- ),
- ],
- )
- tool_paths = [
- tool_path(
- name = "gcc",
- path = ctx.attr.gcc,
- ),
- tool_path(
- name = "ld",
- path = "/usr/bin/ld",
- ),
- tool_path(
- name = "ar",
- path = "/usr/bin/ar",
- ),
- tool_path(
- name = "cpp",
- path = "/bin/false",
- ),
- tool_path(
- name = "gcov",
- path = "/bin/false",
- ),
- tool_path(
- name = "nm",
- path = "/bin/false",
- ),
- tool_path(
- name = "objdump",
- path = "/bin/false",
- ),
- tool_path(
- name = "strip",
- path = "/bin/false",
- ),
- tool_path(
- name = "objcopy",
- path = "/usr/bin/objcopy",
- ),
- ]
-
- return cc_common.create_cc_toolchain_config_info(
- ctx = ctx,
- features = [
- default_link_flags_feature,
- link_full_libc_feature,
- cpp_feature,
- pie_feature,
- ],
- cxx_builtin_include_directories = ctx.attr.host_includes,
- toolchain_identifier = "k8-toolchain",
- host_system_name = "local",
- target_system_name = "local",
- target_cpu = "k8",
- target_libc = "unknown",
- compiler = "gcc",
- abi_version = "unknown",
- abi_libc_version = "unknown",
- tool_paths = tool_paths,
- builtin_sysroot = ctx.attr.sysroot,
- )
-
-host_cc_toolchain_config = rule(
- implementation = _host_cc_toolchain_impl,
- attrs = {
- "gcc": attr.string(
- default = "/usr/bin/gcc",
- ),
- "has_cpp": attr.bool(default = True),
- "is_glibc": attr.bool(default = True),
- "host_includes": attr.string_list(
- default = [
- "/usr/lib/gcc/x86_64-redhat-linux/14/include/",
- "/usr/include",
- ],
- ),
- "sysroot": attr.string(
- default = "",
- ),
- },
- provides = [CcToolchainConfigInfo],
-)