bazelbuild / bazelbuild/rules_cc

deriving makevar values from feature configuration

Open
#371 1 comment 0 reactions 0 assignees View on GitHub
P3 type: feature request
Dominant language
Starlark
Stars
247
Forks
196
PR merge metrics
No merged PRs in 30d

Description

Forgive me if this isn't actually possible, but I suspect it is, since `cc_flags_supplier` does a pretty similar thing. My request is for there to be some way of setting makevars (i.e. the contents of the `cc_toolchain`'s `TemplateVariableInfo`) based on the current `feature_configuration`. The way I'd probably see this working is something like a `makevar_sets` sibling to `flag_sets` and `env_sets` on `feature()` but that's not the only way.

The why:

We have a toolchain setup today that sets up some complicated interacting features. This is a bit of an illustration, for argument's sake:
```
stack_protector_features = feature_stack(
prefix = "stack_protector",
actions = all_compile_actions + all_link_actions,
features = [
dict(name = "strong", flags = ["-fstack-protector-strong"]),
dict(name = "basic", flags = ["-fstack-protector"]),
dict(name = "disabled", flags = ["-fno-stack-protector"]),
],
)
```
This produces four features:
- `stack_protector_choice`, the meta-feature
- `stack_protector_strong`
- `stack_protector_basic`
- `stack_protector_disabled`

One can then disable the choice feature to revert to upstream defaults, or otherwise the highest feature in the stack wins (strong, in this case), which is implemented via chaining the features/not_features of each item to the next one. This all works pretty well for us (and maybe you all might be interesting in us submitting this upstream? Though it's not actually that hard to write).

But now the issue we've created with complex feature relationships is it can be somewhat hard to reverse-engineer what high-level concepts were actually set on a specific target given just the enabled features list (we generate metadata on our targets). Given the above, we can write some code that infers the higher-level information from the exact set of features that were enabled (or infers it from the flags, which is easier in some cases) but what we really wanted was a more general solution to this, which let us do something like:

```
stack_protector_features = feature_stack(
prefix = "stack_protector",
....
features = [
dict(name = "strong", flags = ["-fstack-protector-strong"], makevars = {"STACK_PROTECTOR": "strong"),
dict(name = "basic", flags = ["-fstack-protector"], makevars = {"STACK_PROTECTOR": "basic"}),
...
],
)
```

This would take the guesswork out of our introspection. Thanks!

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.