Skip to content

Commit 6f8700a

Browse files
committed
Deduplicate tools for cc_tool_map
Fixes #235
1 parent ba3ec91 commit 6f8700a

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

cc/toolchains/tool_map.bzl

+18-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def _cc_tool_map_impl(ctx):
3232
action_to_as = {}
3333
for i in range(len(action_sets)):
3434
action_set = action_sets[i]
35-
tool = tools[i]
35+
tool = tools[ctx.attr.tool_index_for_action[i]]
3636

3737
for action in action_set.actions.to_list():
3838
if action in action_to_as:
@@ -63,6 +63,10 @@ See //cc/toolchains/actions:BUILD for valid options.
6363
The tool may be a `cc_tool` or other executable rule.
6464
""",
6565
),
66+
"tool_index_for_action": attr.int_list(
67+
mandatory = True,
68+
doc = """The index of the tool in `tools` for the action in `actions`.""",
69+
)
6670
},
6771
provides = [ToolConfigInfo],
6872
)
@@ -100,21 +104,27 @@ def cc_tool_map(name, tools, **kwargs):
100104
)
101105
```
102106
103-
Note:
104-
Due to an implementation limitation, if you need to map the same tool to multiple actions,
105-
you will need to create an intermediate alias for the tool for each set of actions. See
106-
https://github.com/bazelbuild/rules_cc/issues/235 for more details.
107-
108107
Args:
109108
name: (str) The name of the target.
110109
tools: (Dict[Label, Label]) A mapping between
111110
`cc_action_type`/`cc_action_type_set` targets
112111
and the `cc_tool` or executable target that implements that action.
113112
**kwargs: [common attributes](https://bazel.build/reference/be/common-definitions#common-attributes) that should be applied to this rule.
114113
"""
114+
actions = []
115+
tool_index_for_action = []
116+
deduplicated_tools = {}
117+
for action, tool in tools.items():
118+
actions.append(action)
119+
label = native.package_relative_label(tool)
120+
if label not in deduplicated_tools:
121+
deduplicated_tools[label] = len(deduplicated_tools)
122+
tool_index_for_action.append(deduplicated_tools[label])
123+
115124
_cc_tool_map(
116125
name = name,
117-
actions = tools.keys(),
118-
tools = tools.values(),
126+
actions = actions,
127+
tools = deduplicated_tools.keys(),
128+
tool_index_for_action = tool_index_for_action,
119129
**kwargs
120130
)
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
load(
22
":tool_map_test.bzl",
3-
"duplicate_tool_test",
3+
"duplicate_action_test",
44
"valid_config_test",
55
)
66

7-
duplicate_tool_test(name = "duplicate_tool_test")
7+
duplicate_action_test(name = "duplicate_action_test")
88

99
valid_config_test(name = "valid_config_test")

tests/rule_based_toolchain/tool_map/tool_map_test.bzl

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ _C_COMPILE = "//cc/toolchains/actions:c_compile"
2323
_CPP_COMPILE = "//cc/toolchains/actions:cpp_compile"
2424
_ALL_CPP_COMPILE = "//cc/toolchains/actions:cpp_compile_actions"
2525
_STRIP = "//cc/toolchains/actions:strip"
26+
_LINK_DYNAMIC_LIBRARY = "//cc/toolchains/actions:cpp_link_executable"
2627
_BIN = "//tests/rule_based_toolchain/testdata:bin"
2728
_BIN_WRAPPER = "//tests/rule_based_toolchain/testdata:bin_wrapper"
2829

@@ -31,6 +32,7 @@ def valid_config_test(name):
3132
cc_tool_map(
3233
name = subject_name,
3334
tools = {
35+
_LINK_DYNAMIC_LIBRARY: _BIN,
3436
_C_COMPILE: _BIN_WRAPPER,
3537
_ALL_CPP_COMPILE: _BIN,
3638
},
@@ -42,6 +44,7 @@ def valid_config_test(name):
4244
targets = {
4345
"c_compile": _C_COMPILE,
4446
"cpp_compile": _CPP_COMPILE,
47+
"link_dynamic_library": _LINK_DYNAMIC_LIBRARY,
4548
"strip": _STRIP,
4649
"subject": subject_name,
4750
},
@@ -53,8 +56,9 @@ def _valid_config_test_impl(env, targets):
5356
configs.contains(targets.strip[ActionTypeInfo]).equals(False)
5457
configs.get(targets.c_compile[ActionTypeInfo]).exe().path().split("/").offset(-1, subjects.str).equals("bin_wrapper")
5558
configs.get(targets.cpp_compile[ActionTypeInfo]).exe().path().split("/").offset(-1, subjects.str).equals("bin")
59+
configs.get(targets.link_dynamic_library[ActionTypeInfo]).exe().path().split("/").offset(-1, subjects.str).equals("bin")
5660

57-
def duplicate_tool_test(name):
61+
def duplicate_action_test(name):
5862
subject_name = "_%s_subject" % name
5963
helper_target(
6064
cc_tool_map,

0 commit comments

Comments
 (0)