Skip to content

Commit

Permalink
Integrate cc_tool_map into rule-based toolchains
Browse files Browse the repository at this point in the history
BEGIN_PUBLIC

Integrate cc_tool_map into rule-based toolchains

Integrates cc_tool_map as a new attribute of cc_toolchain, completely replacing cc_action_type_config.

END_PUBLIC

PiperOrigin-RevId: 666365739
Change-Id: Iac74a31736dad66ac3dc75b4478ab4d4d2412181
  • Loading branch information
Googler authored and copybara-github committed Aug 22, 2024
1 parent 3a62fd3 commit f5eb3c0
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 387 deletions.
113 changes: 0 additions & 113 deletions cc/toolchains/action_type_config.bzl

This file was deleted.

23 changes: 1 addition & 22 deletions cc/toolchains/cc_toolchain_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -164,35 +164,14 @@ ToolConfigInfo = provider(
},
)

ActionTypeConfigInfo = provider(
doc = "Configuration of a Bazel action.",
# @unsorted-dict-items
fields = {
"label": "(Label) The label defining this provider. Place in error messages to simplify debugging",
"action_type": "(ActionTypeInfo) The type of the action",
"tools": "(Sequence[ToolInfo]) The tool applied to the action will be the first tool in the sequence with a feature set that matches the feature configuration",
"implies": "(depset[FeatureInfo]) Set of features implied by this action config",
"files": "(runfiles) The files required to run these actions",
},
)

ActionTypeConfigSetInfo = provider(
doc = "A set of action configs",
# @unsorted-dict-items
fields = {
"label": "(Label) The label defining this provider. Place in error messages to simplify debugging",
"configs": "(dict[ActionTypeInfo, ActionTypeConfigInfo]) A set of action configs",
},
)

ToolchainConfigInfo = provider(
doc = "The configuration for a toolchain",
# @unsorted-dict-items
fields = {
"label": "(Label) The label defining this provider. Place in error messages to simplify debugging",
"features": "(Sequence[FeatureInfo]) The features available for this toolchain",
"enabled_features": "(Sequence[FeatureInfo]) The features That are enabled by default for this toolchain",
"action_type_configs": "(dict[ActionTypeInfo, ActionTypeConfigInfo]) The configuration of action configs for the toolchain.",
"tool_map": "(ToolConfigInfo) A provider mapping toolchain action types to tools.",
"args": "(Sequence[ArgsInfo]) A list of arguments to be unconditionally applied to the toolchain.",
"files": "(dict[ActionTypeInfo, depset[File]]) Files required for the toolchain, keyed by the action type.",
},
Expand Down
20 changes: 0 additions & 20 deletions cc/toolchains/impl/collect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

load(
"//cc/toolchains:cc_toolchain_info.bzl",
"ActionTypeConfigSetInfo",
"ActionTypeSetInfo",
"ArgsListInfo",
"FeatureSetInfo",
Expand Down Expand Up @@ -151,22 +150,3 @@ def collect_args_lists(targets, label):
for k, v in by_action.items()
]),
)

def collect_action_type_config_sets(targets, label, fail = fail):
"""Collects several `cc_action_type_config` labels together.
Args:
targets: (List[Target]) A list of targets providing ActionTypeConfigSetInfo
label: The label to apply to the resulting config.
fail: (function) The fail function. Should only be used in tests.
Returns:
A combined ActionTypeConfigSetInfo representing a variety of action
types.
"""
configs = {}
for atcs in collect_provider(targets, ActionTypeConfigSetInfo):
for action_type, config in atcs.configs.items():
if action_type in configs:
fail("The action type %s is configured by both %s and %s. Each action type may only be configured once." % (action_type.label, config.label, configs[action_type].label))
configs[action_type] = config
return ActionTypeConfigSetInfo(label = label, configs = configs)
24 changes: 11 additions & 13 deletions cc/toolchains/impl/legacy_converter.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,16 @@ def convert_tool(tool):
with_features = [],
)

def _convert_action_type_config(atc):
implies = sorted([ft.name for ft in atc.implies.to_list()])

return legacy_action_config(
action_name = atc.action_type.name,
enabled = True,
tools = [convert_tool(tool) for tool in atc.tools],
implies = implies,
)
def _convert_tool_map(tool_map):
return [
legacy_action_config(
action_name = action_type.name,
enabled = True,
tools = [convert_tool(tool_map.configs[action_type])],
implies = [],
)
for action_type in tool_map.configs.keys()
]

def convert_toolchain(toolchain):
"""Converts a rule-based toolchain into the legacy providers.
Expand All @@ -165,10 +166,7 @@ def convert_toolchain(toolchain):
mutually_exclusive = [],
external = False,
)))
action_configs = [
_convert_action_type_config(atc)
for atc in toolchain.action_type_configs.values()
]
action_configs = _convert_tool_map(toolchain.tool_map)

return struct(
features = [ft for ft in features if ft != None],
Expand Down
6 changes: 3 additions & 3 deletions cc/toolchains/impl/toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@bazel_skylib//rules/directory:providers.bzl", "DirectoryInfo")
load(
"//cc/toolchains:cc_toolchain_info.bzl",
"ActionTypeConfigSetInfo",
"ActionTypeSetInfo",
"ArgsListInfo",
"FeatureSetInfo",
"ToolConfigInfo",
"ToolchainConfigInfo",
)
load(":collect.bzl", "collect_action_types")
Expand Down Expand Up @@ -60,7 +60,7 @@ def _cc_toolchain_config_impl(ctx):
label = ctx.label,
known_features = ctx.attr.known_features + [ctx.attr._builtin_features],
enabled_features = ctx.attr.enabled_features,
action_type_configs = ctx.attr.action_type_configs,
tool_map = ctx.attr.tool_map,
args = ctx.attr.args,
)

Expand Down Expand Up @@ -108,7 +108,7 @@ cc_toolchain_config = rule(
# @unsorted-dict-items
attrs = {
# Attributes new to this rule.
"action_type_configs": attr.label_list(providers = [ActionTypeConfigSetInfo]),
"tool_map": attr.label(providers = [ToolConfigInfo], mandatory = True),
"args": attr.label_list(providers = [ArgsListInfo]),
"known_features": attr.label_list(providers = [FeatureSetInfo]),
"enabled_features": attr.label_list(providers = [FeatureSetInfo]),
Expand Down
39 changes: 18 additions & 21 deletions cc/toolchains/impl/toolchain_config_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
# limitations under the License.
"""Helper functions to create and validate a ToolchainConfigInfo."""

load("//cc/toolchains:cc_toolchain_info.bzl", "ToolchainConfigInfo")
load("//cc/toolchains:cc_toolchain_info.bzl", "ToolConfigInfo", "ToolchainConfigInfo")
load(":args_utils.bzl", "get_action_type")
load(":collect.bzl", "collect_action_type_config_sets", "collect_args_lists", "collect_features")
load(":collect.bzl", "collect_args_lists", "collect_features")

visibility([
"//cc/toolchains/...",
Expand Down Expand Up @@ -106,9 +106,6 @@ def _validate_args(self, known_features, fail):
fail,
)

def _validate_action_config(self, known_features, fail):
_validate_implies(self, known_features, fail = fail)

def _validate_feature(self, known_features, fail):
_validate_requires_any_of_feature_set(self, known_features, fail = fail)
for arg in self.args.args:
Expand All @@ -120,19 +117,17 @@ def _validate_toolchain(self, fail = fail):

for feature in self.features:
_validate_feature(feature, known_features, fail = fail)
for atc in self.action_type_configs.values():
_validate_action_config(atc, known_features, fail = fail)
for args in self.args:
_validate_args(args, known_features, fail = fail)

def _collect_files_for_action_type(atc, features, args):
transitive_files = [atc.files.files, get_action_type(args, atc.action_type).files]
def _collect_files_for_action_type(action_type, tool_map, features, args):
transitive_files = [tool_map[action_type].runfiles.files, get_action_type(args, action_type).files]
for ft in features:
transitive_files.append(get_action_type(ft.args, atc.action_type).files)
transitive_files.append(get_action_type(ft.args, action_type).files)

return depset(transitive = transitive_files)

def toolchain_config_info(label, known_features = [], enabled_features = [], args = [], action_type_configs = [], fail = fail):
def toolchain_config_info(label, known_features = [], enabled_features = [], args = [], tool_map = None, fail = fail):
"""Generates and validates a ToolchainConfigInfo from lists of labels.
Args:
Expand All @@ -141,8 +136,7 @@ def toolchain_config_info(label, known_features = [], enabled_features = [], arg
enabled_features: (List[Target]) A list of features that are enabled by
default. Every enabled feature is implicitly also a known feature.
args: (List[Target]) A list of targets providing ArgsListInfo
action_type_configs: (List[Target]) A list of targets providing
ActionTypeConfigSetInfo
tool_map: (Target) A target providing ToolMapInfo.
fail: A fail function. Use only during tests.
Returns:
A validated ToolchainConfigInfo
Expand All @@ -155,22 +149,25 @@ def toolchain_config_info(label, known_features = [], enabled_features = [], arg
features = collect_features(enabled_features + known_features).to_list()
enabled_features = collect_features(enabled_features).to_list()

if tool_map == None:
fail("tool_map is required")

# The `return` here is to support testing, since injecting `fail()` has a
# side-effect of allowing code to continue.
return None # buildifier: disable=unreachable

args = collect_args_lists(args, label = label)
action_type_configs = collect_action_type_config_sets(
action_type_configs,
label = label,
fail = fail,
).configs
tools = tool_map[ToolConfigInfo].configs
files = {
atc.action_type: _collect_files_for_action_type(atc, features, args)
for atc in action_type_configs.values()
action_type: _collect_files_for_action_type(action_type, tools, features, args)
for action_type in tools.keys()
}

toolchain_config = ToolchainConfigInfo(
label = label,
features = features,
enabled_features = enabled_features,
action_type_configs = action_type_configs,
tool_map = tool_map[ToolConfigInfo],
args = args.args,
files = files,
)
Expand Down
38 changes: 0 additions & 38 deletions tests/rule_based_toolchain/action_type_config/BUILD

This file was deleted.

Loading

0 comments on commit f5eb3c0

Please sign in to comment.