1"""A wrapper that enables a `config_setting` matcher for label_flag flags."""
2
3load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
4load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain")
5
6def _match_label_flag_impl(ctx):
7    matches = str(ctx.attr.expected_value.label) == str(ctx.attr.flag.label)
8    return [
9        config_common.FeatureFlagInfo(value = str(matches)),
10        BuildSettingInfo(value = matches),
11    ]
12
13_match_label_flag = rule(
14    implementation = _match_label_flag_impl,
15    attrs = {
16        "expected_value": attr.label(
17            mandatory = True,
18            doc = "The expected flag value",
19        ),
20        "flag": attr.label(
21            mandatory = True,
22            doc = "The flag to extract a value from",
23        ),
24    },
25)
26
27def label_flag_matches(*, name, flag, value):
28    _match_label_flag(
29        name = name + "._impl",
30        expected_value = native.package_relative_label(value),
31        flag = flag,
32    )
33
34    native.config_setting(
35        name = name,
36        flag_values = {":{}".format(name + "._impl"): "True"},
37    )
38