bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
23.18k stars 4.05k forks source link

Allow reading from constraints and config settings via providers #22101

Open matts1 opened 6 months ago

matts1 commented 6 months ago

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:

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

comius commented 5 months ago

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

gregestren commented 5 months ago

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.

matts1 commented 5 months ago

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.