From a14808554e926227b9d4e0d2015857f842e40447 Mon Sep 17 00:00:00 2001 From: Xavier Bonaventura Date: Thu, 13 Nov 2025 10:07:22 +0100 Subject: [PATCH] Add support to inject cc_feature into legacy toolchains This adds a parameter to unix cc_toolchain_config and windows cc_toolchain_config to be able to inject a cc_feature. This allows a user to inject a feature (for example for custom warnings) without having to reimplement the full toolchain. Resolves #517 --- .../toolchain/unix_cc_toolchain_config.bzl | 4 ++++ .../toolchain/windows_cc_toolchain_config.bzl | 7 ++++++- cc/toolchains/feature_injection.bzl | 20 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 cc/toolchains/feature_injection.bzl diff --git a/cc/private/toolchain/unix_cc_toolchain_config.bzl b/cc/private/toolchain/unix_cc_toolchain_config.bzl index f91fa3f1..ea3f5575 100644 --- a/cc/private/toolchain/unix_cc_toolchain_config.bzl +++ b/cc/private/toolchain/unix_cc_toolchain_config.bzl @@ -31,6 +31,7 @@ load( ) load("@rules_cc//cc/common:cc_common.bzl", "cc_common") load("@rules_cc//cc/toolchains:cc_toolchain_config_info.bzl", "CcToolchainConfigInfo") +load("@rules_cc//cc/toolchains:feature_injection.bzl", "FeatureInfo", "convert_feature") def _target_os_version(ctx): platform_type = ctx.fragments.apple.single_arch_platform.platform_type @@ -1950,6 +1951,8 @@ def _impl(ctx): if symbol_check: features.append(symbol_check) + features.extend([convert_feature(extra_feature[FeatureInfo]) for extra_feature in ctx.attr.extra_features]) + return cc_common.create_cc_toolchain_config_info( ctx = ctx, features = features, @@ -1985,6 +1988,7 @@ cc_toolchain_config = rule( "cxx_builtin_include_directories": attr.string_list(), "cxx_flags": attr.string_list(), "dbg_compile_flags": attr.string_list(), + "extra_features": attr.label_list(providers = [FeatureInfo], default = []), "extra_flags_per_feature": attr.string_list_dict(), "fastbuild_compile_flags": attr.string_list(), "host_system_name": attr.string(mandatory = True), diff --git a/cc/private/toolchain/windows_cc_toolchain_config.bzl b/cc/private/toolchain/windows_cc_toolchain_config.bzl index 5fa087e5..a5df2035 100644 --- a/cc/private/toolchain/windows_cc_toolchain_config.bzl +++ b/cc/private/toolchain/windows_cc_toolchain_config.bzl @@ -31,6 +31,7 @@ load( ) load("@rules_cc//cc/common:cc_common.bzl", "cc_common") load("@rules_cc//cc/toolchains:cc_toolchain_config_info.bzl", "CcToolchainConfigInfo") +load("@rules_cc//cc/toolchains:feature_injection.bzl", "FeatureInfo", "convert_feature") all_compile_actions = [ ACTION_NAMES.c_compile, @@ -1703,9 +1704,12 @@ def _impl(ctx): ], enabled = True, ) + features.extend([cpp_modules_feature, cpp_module_modmap_file_feature, cpp20_module_compile_flags_feature]) + features.extend([convert_feature(extra_feature[FeatureInfo]) for extra_feature in ctx.attr.extra_features]) + return cc_common.create_cc_toolchain_config_info( ctx = ctx, - features = features + [cpp_modules_feature, cpp_module_modmap_file_feature, cpp20_module_compile_flags_feature], + features = features, action_configs = action_configs, artifact_name_patterns = artifact_name_patterns, cxx_builtin_include_directories = ctx.attr.cxx_builtin_include_directories, @@ -1736,6 +1740,7 @@ cc_toolchain_config = rule( "dbg_mode_debug_flag": attr.string(default = ""), "default_compile_flags": attr.string_list(default = []), "default_link_flags": attr.string_list(default = []), + "extra_features": attr.label_list(providers = [FeatureInfo], default = []), "fastbuild_mode_debug_flag": attr.string(default = ""), "host_system_name": attr.string(), "link_flags": attr.string_list(), diff --git a/cc/toolchains/feature_injection.bzl b/cc/toolchains/feature_injection.bzl new file mode 100644 index 00000000..44ca300e --- /dev/null +++ b/cc/toolchains/feature_injection.bzl @@ -0,0 +1,20 @@ +# Copyright 2026 The Bazel Authors. All rights reserved. +# +# 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. +"""Provides necessary tools to inject rules-based features to legacy toolchains""" + +load("@rules_cc//cc/toolchains:cc_toolchain_info.bzl", _FeatureInfo = "FeatureInfo") +load("@rules_cc//cc/toolchains/impl:legacy_converter.bzl", _convert_feature = "convert_feature") + +convert_feature = _convert_feature +FeatureInfo = _FeatureInfo