bazelbuild / bazelbuild/rules_cc

[toolchains] Add helper for extracting action names from `cc_action_type` rules

Open
#292 1 comment 0 reactions 1 assignee Claimed by @armandomontanez View on GitHub
category: toolchains P3
Dominant language
Starlark
Stars
247
Forks
196
PR merge metrics
No merged PRs in 30d

Description

Many rules use [`cc_common.get_tool_for_action()`](https://bazel.build/rules/lib/toplevel/cc_common#get_tool_for_action) to create custom rules that employ binaries from the current C/C++ toolchain. The status-quo today is to use the [string constants exposed in Starlark files](https://github.com/bazelbuild/rules_cc/blob/main/cc/action_names.bzl). Since these are now represented as rules, it's plausible to express the action types by referencing the source-of-truth `cc_action_type` rule. Today this is not possible because the providers for toolchain rules are considered private and not exposed to prevent projects from relying on implementation details of the toolchain rules.

## Proposed solution
The simplest path forward is to add a `action_name_from_action_type(target)` helper that takes a target and extracts the action name from the action type. This will work as a bridge so user can do things like:
```py

def _foo_rule_impl(ctx):
# ...
tool_path = cc_common.get_tool_for_action(
feature_configuration = action_name_from_action_type(ctx.attr._objcopy),
action_name = action_name,
)
# Run tool...

foo_rule = rule(
implementation = _foo_rule_impl,
attrs = {
"_objcopy": attr.label(
default = "@rules_cc//cc/toolchains/actions:objcopy_embed_data",
),
},
toolchains = use_cpp_toolchain(),
fragments = ["cpp"],
)

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.