Skip to content

Deduplicate tools for cc_tool_map #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions cc/toolchains/tool_map.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def _cc_tool_map_impl(ctx):
action_to_as = {}
for i in range(len(action_sets)):
action_set = action_sets[i]
tool = tools[i]
tool = tools[ctx.attr.tool_index_for_action[i]]

for action in action_set.actions.to_list():
if action in action_to_as:
Expand Down Expand Up @@ -63,6 +63,10 @@ See //cc/toolchains/actions:BUILD for valid options.
The tool may be a `cc_tool` or other executable rule.
""",
),
"tool_index_for_action": attr.int_list(
mandatory = True,
doc = """The index of the tool in `tools` for the action in `actions`.""",
),
},
provides = [ToolConfigInfo],
)
Expand Down Expand Up @@ -100,21 +104,27 @@ def cc_tool_map(name, tools, **kwargs):
)
```

Note:
Due to an implementation limitation, if you need to map the same tool to multiple actions,
you will need to create an intermediate alias for the tool for each set of actions. See
https://github.com/bazelbuild/rules_cc/issues/235 for more details.

Args:
name: (str) The name of the target.
tools: (Dict[Label, Label]) A mapping between
`cc_action_type`/`cc_action_type_set` targets
and the `cc_tool` or executable target that implements that action.
**kwargs: [common attributes](https://bazel.build/reference/be/common-definitions#common-attributes) that should be applied to this rule.
"""
actions = []
tool_index_for_action = []
deduplicated_tools = {}
for action, tool in tools.items():
actions.append(action)
label = native.package_relative_label(tool)
if label not in deduplicated_tools:
deduplicated_tools[label] = len(deduplicated_tools)
tool_index_for_action.append(deduplicated_tools[label])

_cc_tool_map(
name = name,
actions = tools.keys(),
tools = tools.values(),
actions = actions,
tools = deduplicated_tools.keys(),
tool_index_for_action = tool_index_for_action,
**kwargs
)
5 changes: 0 additions & 5 deletions docs/toolchain_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -657,11 +657,6 @@ cc_tool_map(
)
```

Note:
Due to an implementation limitation, if you need to map the same tool to multiple actions,
you will need to create an intermediate alias for the tool for each set of actions. See
https://github.com/bazelbuild/rules_cc/issues/235 for more details.


**PARAMETERS**

Expand Down
9 changes: 1 addition & 8 deletions examples/rule_based_toolchain/toolchain/tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ alias(
)

COMMON_TOOLS = {
"@rules_cc//cc/toolchains/actions:assembly_actions": ":asm",
"@rules_cc//cc/toolchains/actions:assembly_actions": ":clang",
"@rules_cc//cc/toolchains/actions:c_compile": ":clang",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions": ":clang++",
"@rules_cc//cc/toolchains/actions:link_actions": ":lld",
Expand All @@ -54,13 +54,6 @@ cc_tool_map(
visibility = ["//visibility:private"],
)

# TODO: https://github.com/bazelbuild/rules_cc/issues/235 - Workaround until
# Bazel has a more robust way to implement `cc_tool_map`.
alias(
name = "asm",
actual = ":clang",
)

cc_tool(
name = "clang",
src = select({
Expand Down
4 changes: 2 additions & 2 deletions tests/rule_based_toolchain/tool_map/BUILD
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
load(
":tool_map_test.bzl",
"duplicate_tool_test",
"duplicate_action_test",
"valid_config_test",
)

duplicate_tool_test(name = "duplicate_tool_test")
duplicate_action_test(name = "duplicate_action_test")

valid_config_test(name = "valid_config_test")
6 changes: 5 additions & 1 deletion tests/rule_based_toolchain/tool_map/tool_map_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ _C_COMPILE = "//cc/toolchains/actions:c_compile"
_CPP_COMPILE = "//cc/toolchains/actions:cpp_compile"
_ALL_CPP_COMPILE = "//cc/toolchains/actions:cpp_compile_actions"
_STRIP = "//cc/toolchains/actions:strip"
_LINK_DYNAMIC_LIBRARY = "//cc/toolchains/actions:cpp_link_executable"
_BIN = "//tests/rule_based_toolchain/testdata:bin"
_BIN_WRAPPER = "//tests/rule_based_toolchain/testdata:bin_wrapper"

Expand All @@ -31,6 +32,7 @@ def valid_config_test(name):
cc_tool_map(
name = subject_name,
tools = {
_LINK_DYNAMIC_LIBRARY: _BIN,
_C_COMPILE: _BIN_WRAPPER,
_ALL_CPP_COMPILE: _BIN,
},
Expand All @@ -42,6 +44,7 @@ def valid_config_test(name):
targets = {
"c_compile": _C_COMPILE,
"cpp_compile": _CPP_COMPILE,
"link_dynamic_library": _LINK_DYNAMIC_LIBRARY,
"strip": _STRIP,
"subject": subject_name,
},
Expand All @@ -53,8 +56,9 @@ def _valid_config_test_impl(env, targets):
configs.contains(targets.strip[ActionTypeInfo]).equals(False)
configs.get(targets.c_compile[ActionTypeInfo]).exe().path().split("/").offset(-1, subjects.str).equals("bin_wrapper")
configs.get(targets.cpp_compile[ActionTypeInfo]).exe().path().split("/").offset(-1, subjects.str).equals("bin")
configs.get(targets.link_dynamic_library[ActionTypeInfo]).exe().path().split("/").offset(-1, subjects.str).equals("bin")

def duplicate_tool_test(name):
def duplicate_action_test(name):
subject_name = "_%s_subject" % name
helper_target(
cc_tool_map,
Expand Down