Host toolchain minimal features

Test Plan: Tested with QEMU and the existing codebase.

X-Origin-Diff: phab/D713
GitOrigin-RevId: ecfc94ab2b4880447c628fc2e41b5ed6234f90d8
diff --git a/build/toolchain/cc_toolchain_config.bzl b/build/toolchain/cc_toolchain_config.bzl
index 11c7736..5aeb270 100644
--- a/build/toolchain/cc_toolchain_config.bzl
+++ b/build/toolchain/cc_toolchain_config.bzl
@@ -14,12 +14,126 @@
 #  See the License for the specific language governing permissions and
 #  limitations under the License.
 
-load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "tool", "tool_path")
+load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "feature", "flag_group", "flag_set", "tool", "tool_path")
+load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
+
+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,
+]
+
+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
+            ),
+        ],
+    )
+    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",
@@ -54,8 +168,10 @@
             path = "/bin/false",
         ),
     ]
+
     return cc_common.create_cc_toolchain_config_info(
         ctx = ctx,
+        features = [default_link_flags_feature, link_full_libc_feature, cpp_feature],
         cxx_builtin_include_directories = ctx.attr.host_includes,
         toolchain_identifier = "k8-toolchain",
         host_system_name = "local",
@@ -75,6 +191,8 @@
         "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/10/include/",