1def declare_flag_choices(flag, choices):
2    """Declares a `config_setting` for each known choice for the provided flag.
3
4    The name of each config setting uses the name of the `config_setting` is:
5        [flag label name]_[choice]
6
7    This can be used with select_choice() to map `config_setting`s to values.
8
9    Args:
10      flag: The flag that guides the declared `config_setting`s.
11      pkg: The package that declare_flag_choices() was declared in.
12      choice_map: A mapping of distinct choices to
13    """
14    flag_name = flag.split(":")[1]
15    [
16        native.config_setting(
17            name = "{}_{}".format(flag_name, choice),
18            flag_values = {flag: choice},
19        )
20        for choice in choices
21    ]
22
23def flag_choice(flag, pkg, choice_map):
24    """Creates a `select()` based on choices declared by `declare_choices()`.
25
26    Args:
27      flag: The flag that guides the select.
28      pkg: The package that `declare_flag_choices()` was called in.
29      choice_map: A mapping of distinct choices to the final intended value.
30    """
31    return {
32        "{}:{}_{}".format(
33            pkg.split(":")[0],
34            flag.split(":")[1],
35            choice,
36        ): val
37        for choice, val in choice_map.items()
38    }
39