bazel icon indicating copy to clipboard operation
bazel copied to clipboard

Allow reading from constraints and config settings via providers

Open matts1 opened this issue 1 year ago • 3 comments

Description of the feature request:

I'd like config_setting and constraint_value rules to output a provider corresponding to whether they evaluate to true or false. This would allow us to read them directly in rules.

SelectMatchInfo = provider(
    fields = {"value": "(bool) Whether or not this counts as matching a select"}
)

As a follow-on from this, we could potentially have select read from the provider itself, thus allowing us to make any rule select'able. This would be extremely useful for a few particular rules:

  • bazel-skylib's bool_flag could be chained
  • bazel-skylib's selects.config_settings_group is currently implemented in a very hacky manner. This would allow us to clean that up

It would also allow us to implement arbitrarily complex resolution of config settings. For example, you could have a string_flag, and have a bool setting that checks if it starts with "linux-".

This would allow us to write:

bool_flag(
    name = "foo",
    build_setting_default = False,
)

alias(
    name = "bar",
    actual = select({
        ":foo": ":something",
        "//conditions:default": ":something_else",
    })
)

Which category does this issue belong to?

Core

What underlying problem are you trying to solve with this feature?

At the moment, if I write:

selects.config_settings_group(
   name = "foo_and_bar",
   match_all = [":foo", ":bar"]
)
def _my_rule_impl(ctx):
    ...

my_rule = rule(
   implementation = _my_rule_impl,
   attrs = {
        "_foo_and_bar": attr.label(default = ":foo_and_bar"),
        "_constraint": attr.label(default = "@platforms//os:linux"),
    }
)

Then in the rule implementation, I have no way of determining whether the config setting resolves to true or to false, and the same goes for the constraint value.

Which operating system are you running Bazel on?

linux

What is the output of bazel info release?

7.1.1

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse HEAD ?

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

matts1 avatar Apr 24 '24 00:04 matts1

Triaging to Configurability team, although I do find this suggestion interesting.

comius avatar May 23 '24 10:05 comius

Wouldn't https://github.com/bazelbuild/bazel/issues/19975 and https://docs.google.com/document/d/1p02Y9joQSgdXtiTA2mJ_UXHBiBDPPTqrO-oJcupLAu8/edit#heading=h.icwoewbtra8 address this?

i.e. make the existing ConfigMatchingProvider Starlark-accessible, which is already something we want to do.

gregestren avatar May 29 '24 23:05 gregestren

Ah, I wrote #19975 to address a slightly different problem, but it does appear that the solution to that also happens to solve this problem.

i.e. make the existing ConfigMatchingProvider Starlark-accessible, which is already something we want to do.

Without knowing implementation details, I'm not 100% sure, but it does sound like this would be sufficient.

matts1 avatar May 30 '24 00:05 matts1