rules_cc icon indicating copy to clipboard operation
rules_cc copied to clipboard

Cannot list the same tool twice in a `cc_tool_map`

Open armandomontanez opened this issue 1 year ago • 0 comments

When the same tool is listed twice for different actions in a cc_tool_map, an error occurs:

# ERROR: /private/var/tmp/_bazel_amontanez/e724b21efc8bc19866072fbc72ee5907/external/_main~_repo_rules~llvm_toolchain/BUILD:60:12: Label '@@_main~_repo_rules~llvm_toolchain//:clang' is duplicated in the 'tools' attribute of rule 'default_tools'

Workaround

To work around this limitation, create an alias pointing to the same tool as an intermediate:

cc_tool_map(
    name = "default_tools",
    tools = {
        "@rules_cc//cc/toolchains/actions:assembly_actions": ":asm",
        "@rules_cc//cc/toolchains/actions:c_compile": ":clang",
    },
)

alias(
    name = "asm",
    actual = ":clang",
)

cc_tool(
    name = "clang",
    # ...
)

Root cause

Because Bazel lacks a label_keyed_label_dict, cc_tool_map is implemented by unrolling the passed dictionary into two 1:1 label lists. Bazel emits an error when the same label is listed twice in a label_list, which is what is seen above. For cc_tool_map.tool, it's an error for the same dictionary key to be listed twice, but different keys may point to the same tool. Until Bazel supports a better mechanism for implementing cc_tool_map, please use the workaround above.

armandomontanez avatar Aug 23 '24 18:08 armandomontanez