From 3d94a5891267279c344b074d045b7b099efe5284 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Thu, 26 Mar 2026 12:21:32 -0700 Subject: [PATCH 1/5] Add excludes to cc_action_type_set This makes it easier to compose action sets that are very similar to other ones but excluding a few things. This is used today for lto_backend, and in this change is also used for header parsing, which produces a warning when `-c` is passed. --- cc/toolchains/actions.bzl | 10 ++++++- cc/toolchains/actions/BUILD | 29 ++++++++++--------- cc/toolchains/args/compiler_input_flags/BUILD | 15 ++++++++-- ...compile_actions_without_header_parsing.txt | 1 + tests/rule_based_toolchain/actions/BUILD | 25 ++++++++++++++++ .../actions/actions_test.bzl | 12 ++++++++ 6 files changed, 76 insertions(+), 16 deletions(-) diff --git a/cc/toolchains/actions.bzl b/cc/toolchains/actions.bzl index 84dc83796..1158f7e9d 100644 --- a/cc/toolchains/actions.bzl +++ b/cc/toolchains/actions.bzl @@ -64,9 +64,13 @@ cc_action_type( def _cc_action_type_set_impl(ctx): if not ctx.attr.actions and not ctx.attr.allow_empty: fail("Each cc_action_type_set must contain at least one action type.") + actions = collect_action_types(ctx.attr.actions) + if ctx.attr.excludes: + excludes = {a: True for a in collect_action_types(ctx.attr.excludes).to_list()} + actions = depset([a for a in actions.to_list() if a not in excludes]) return [ActionTypeSetInfo( label = ctx.label, - actions = collect_action_types(ctx.attr.actions), + actions = actions, )] cc_action_type_set = rule( @@ -95,6 +99,10 @@ cc_action_type_set( mandatory = True, doc = "A list of cc_action_type or cc_action_type_set", ), + "excludes": attr.label_list( + providers = [ActionTypeSetInfo], + doc = "A list of cc_action_type or cc_action_type_set to exclude from the action set. Applied after accumulating all actions.", + ), "allow_empty": attr.bool(default = False), }, provides = [ActionTypeSetInfo], diff --git a/cc/toolchains/actions/BUILD b/cc/toolchains/actions/BUILD index 3f73f21ed..15d66ae08 100644 --- a/cc/toolchains/actions/BUILD +++ b/cc/toolchains/actions/BUILD @@ -253,31 +253,34 @@ cc_action_type_set( ], ) -# This matches `compile_actions` with `lto_backend` omitted, because -# `lto_backend` actions do not instantiate the full set of build variables. cc_action_type_set( - name = "source_compile_actions", + name = "compile_actions", actions = [ - ":compile_actions_without_header_parsing", - ":cpp_header_parsing", + ":cpp_compile_actions", + ":c_compile_actions", + ":assembly_actions", + ":objc_compile", ], ) cc_action_type_set( - name = "compile_actions_without_header_parsing", + name = "source_compile_actions", actions = [ - ":cpp_compile_without_header_parsing", - ":c_compile_actions", - ":assembly_actions", - ":objc_compile", + ":compile_actions", + ], + excludes = [ + # `lto_backend` actions do not instantiate the full set of build variables. + ":lto_backend", ], ) cc_action_type_set( - name = "compile_actions", + name = "compile_actions_without_header_parsing", actions = [ - ":source_compile_actions", - ":lto_backend", + ":compile_actions", + ], + excludes = [ + ":cpp_header_parsing", ], ) diff --git a/cc/toolchains/args/compiler_input_flags/BUILD b/cc/toolchains/args/compiler_input_flags/BUILD index 592f1e748..1cca4c929 100644 --- a/cc/toolchains/args/compiler_input_flags/BUILD +++ b/cc/toolchains/args/compiler_input_flags/BUILD @@ -3,14 +3,17 @@ load("//cc/toolchains:feature.bzl", "cc_feature") cc_feature( name = "feature", - args = [":flags"], + args = [ + ":flags", + ":header_parsing_args", + ], overrides = "//cc/toolchains/features/legacy:compiler_input_flags", visibility = ["//visibility:public"], ) cc_args( name = "flags", - actions = ["//cc/toolchains/actions:compile_actions"], + actions = ["//cc/toolchains/actions:compile_actions_without_header_parsing"], args = [ "-c", "{source_file}", @@ -18,3 +21,11 @@ cc_args( format = {"source_file": "//cc/toolchains/variables:source_file"}, requires_not_none = "//cc/toolchains/variables:source_file", ) + +cc_args( + name = "header_parsing_args", + actions = ["//cc/toolchains/actions:cpp_header_parsing"], + args = ["{source_file}"], + format = {"source_file": "//cc/toolchains/variables:source_file"}, + requires_not_none = "//cc/toolchains/variables:source_file", +) diff --git a/tests/rule_based_toolchain/action_type_sets/goldens/compile_actions_without_header_parsing.txt b/tests/rule_based_toolchain/action_type_sets/goldens/compile_actions_without_header_parsing.txt index 5e5c26f44..4037fe262 100644 --- a/tests/rule_based_toolchain/action_type_sets/goldens/compile_actions_without_header_parsing.txt +++ b/tests/rule_based_toolchain/action_type_sets/goldens/compile_actions_without_header_parsing.txt @@ -5,6 +5,7 @@ c++-module-compile c-compile clif-match linkstamp-compile +lto-backend objc++-compile objc-compile preprocess-assemble diff --git a/tests/rule_based_toolchain/actions/BUILD b/tests/rule_based_toolchain/actions/BUILD index 4e45f7ec7..20ca7c283 100644 --- a/tests/rule_based_toolchain/actions/BUILD +++ b/tests/rule_based_toolchain/actions/BUILD @@ -27,6 +27,31 @@ util.helper_target( visibility = ["//tests/rule_based_toolchain:__subpackages__"], ) +util.helper_target( + cc_action_type_set, + name = "all_compile_except_c", + actions = [ + ":c_compile", + ":cpp_compile", + ], + excludes = [ + ":c_compile", + ], + visibility = ["//tests/rule_based_toolchain:__subpackages__"], +) + +util.helper_target( + cc_action_type_set, + name = "all_compile_except_c_nested", + actions = [ + ":all_compile", + ], + excludes = [ + ":c_compile", + ], + visibility = ["//tests/rule_based_toolchain:__subpackages__"], +) + analysis_test_suite( name = "test_suite", targets = TARGETS, diff --git a/tests/rule_based_toolchain/actions/actions_test.bzl b/tests/rule_based_toolchain/actions/actions_test.bzl index 284ed9aea..1f677a6ac 100644 --- a/tests/rule_based_toolchain/actions/actions_test.bzl +++ b/tests/rule_based_toolchain/actions/actions_test.bzl @@ -32,12 +32,24 @@ def _test_action_types_impl(env, targets): targets.cpp_compile.label, ]) +def _test_action_type_set_excludes_impl(env, targets): + env.expect.that_target(targets.all_compile_except_c).provider(ActionTypeSetInfo) \ + .actions().contains_exactly([targets.cpp_compile.label]) + +def _test_action_type_set_excludes_nested_impl(env, targets): + env.expect.that_target(targets.all_compile_except_c_nested).provider(ActionTypeSetInfo) \ + .actions().contains_exactly([targets.cpp_compile.label]) + TARGETS = [ ":c_compile", ":cpp_compile", ":all_compile", + ":all_compile_except_c", + ":all_compile_except_c_nested", ] TESTS = { "actions_test": _test_action_types_impl, + "action_type_set_excludes_test": _test_action_type_set_excludes_impl, + "action_type_set_excludes_nested_test": _test_action_type_set_excludes_nested_impl, } From 2e8b05b6654a3cd44fa068af460ef3fbe9a33e76 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Tue, 26 May 2026 12:02:52 -0700 Subject: [PATCH 2/5] Remove other one --- cc/toolchains/actions/BUILD | 19 ++++++------------- .../action_type_sets/BUILD | 6 ------ .../cpp_compile_without_header_parsing.txt | 6 ------ 3 files changed, 6 insertions(+), 25 deletions(-) delete mode 100644 tests/rule_based_toolchain/action_type_sets/goldens/cpp_compile_without_header_parsing.txt diff --git a/cc/toolchains/actions/BUILD b/cc/toolchains/actions/BUILD index 15d66ae08..cbfd95043 100644 --- a/cc/toolchains/actions/BUILD +++ b/cc/toolchains/actions/BUILD @@ -232,24 +232,17 @@ cc_action_type_set( ], ) -cc_action_type_set( - name = "cpp_compile_without_header_parsing", - actions = [ - ":linkstamp_compile", - ":cpp_compile", - ":cpp_module_compile", - ":cpp_module_codegen", - ":clif_match", - ":objcpp_compile", - ], -) - cc_action_type_set( name = "cpp_compile_actions", actions = [ - ":cpp_compile_without_header_parsing", + ":clif_match", + ":cpp_compile", ":cpp_header_parsing", + ":cpp_module_codegen", + ":cpp_module_compile", + ":linkstamp_compile", ":lto_backend", + ":objcpp_compile", ], ) diff --git a/tests/rule_based_toolchain/action_type_sets/BUILD b/tests/rule_based_toolchain/action_type_sets/BUILD index 8cdde7d2f..c4053972a 100644 --- a/tests/rule_based_toolchain/action_type_sets/BUILD +++ b/tests/rule_based_toolchain/action_type_sets/BUILD @@ -21,12 +21,6 @@ action_type_set_diff_test( expected = "//tests/rule_based_toolchain/action_type_sets:goldens/c_compile_actions.txt", ) -action_type_set_diff_test( - name = "cpp_compile_without_header_parsing_test", - action_type_set = "//cc/toolchains/actions:cpp_compile_without_header_parsing", - expected = "//tests/rule_based_toolchain/action_type_sets:goldens/cpp_compile_without_header_parsing.txt", -) - action_type_set_diff_test( name = "cpp_compile_actions_test", action_type_set = "//cc/toolchains/actions:cpp_compile_actions", diff --git a/tests/rule_based_toolchain/action_type_sets/goldens/cpp_compile_without_header_parsing.txt b/tests/rule_based_toolchain/action_type_sets/goldens/cpp_compile_without_header_parsing.txt deleted file mode 100644 index ba8a18f45..000000000 --- a/tests/rule_based_toolchain/action_type_sets/goldens/cpp_compile_without_header_parsing.txt +++ /dev/null @@ -1,6 +0,0 @@ -c++-compile -c++-module-codegen -c++-module-compile -clif-match -linkstamp-compile -objc++-compile From ec027b1d4e73528a98337f20dfc0ab1d366443aa Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Tue, 26 May 2026 12:05:32 -0700 Subject: [PATCH 3/5] docs --- docs/toolchain_api.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/toolchain_api.md b/docs/toolchain_api.md index 1e0f5c798..f980055be 100755 --- a/docs/toolchain_api.md +++ b/docs/toolchain_api.md @@ -51,7 +51,7 @@ cc_action_type(
 load("@rules_cc//cc/toolchains/impl:documented_api.bzl", "cc_action_type_set")
 
-cc_action_type_set(name, actions, allow_empty)
+cc_action_type_set(name, actions, allow_empty, excludes)
 
Represents a set of actions. @@ -80,6 +80,7 @@ cc_action_type_set( | name | A unique name for this target. | Name | required | | | actions | A list of cc_action_type or cc_action_type_set | List of labels | required | | | allow_empty | - | Boolean | optional | `False` | +| excludes | A list of cc_action_type or cc_action_type_set to exclude from the action set. Applied after accumulating all actions. | List of labels | optional | `[]` | From c9477cbc39076ec9b027eee4166c17969cbf3c46 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Tue, 26 May 2026 13:30:17 -0700 Subject: [PATCH 4/5] only single actions --- cc/toolchains/actions.bzl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cc/toolchains/actions.bzl b/cc/toolchains/actions.bzl index 1158f7e9d..9a8928e5b 100644 --- a/cc/toolchains/actions.bzl +++ b/cc/toolchains/actions.bzl @@ -66,7 +66,7 @@ def _cc_action_type_set_impl(ctx): fail("Each cc_action_type_set must contain at least one action type.") actions = collect_action_types(ctx.attr.actions) if ctx.attr.excludes: - excludes = {a: True for a in collect_action_types(ctx.attr.excludes).to_list()} + excludes = {a[ActionTypeInfo]: True for a in ctx.attr.excludes} actions = depset([a for a in actions.to_list() if a not in excludes]) return [ActionTypeSetInfo( label = ctx.label, @@ -100,8 +100,8 @@ cc_action_type_set( doc = "A list of cc_action_type or cc_action_type_set", ), "excludes": attr.label_list( - providers = [ActionTypeSetInfo], - doc = "A list of cc_action_type or cc_action_type_set to exclude from the action set. Applied after accumulating all actions.", + providers = [ActionTypeInfo], + doc = "A list of cc_action_type to exclude from the action set. Applied after accumulating all actions.", ), "allow_empty": attr.bool(default = False), }, From 1697d426fbb19e8d42afd808dd15aabd945eb140 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Tue, 26 May 2026 13:33:01 -0700 Subject: [PATCH 5/5] docs --- docs/toolchain_api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/toolchain_api.md b/docs/toolchain_api.md index f980055be..85866aaea 100755 --- a/docs/toolchain_api.md +++ b/docs/toolchain_api.md @@ -80,7 +80,7 @@ cc_action_type_set( | name | A unique name for this target. | Name | required | | | actions | A list of cc_action_type or cc_action_type_set | List of labels | required | | | allow_empty | - | Boolean | optional | `False` | -| excludes | A list of cc_action_type or cc_action_type_set to exclude from the action set. Applied after accumulating all actions. | List of labels | optional | `[]` | +| excludes | A list of cc_action_type to exclude from the action set. Applied after accumulating all actions. | List of labels | optional | `[]` |