• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2This file contains general helper macros that make our BUILD.bazel files easier to read.
3"""
4
5def select_multi(values_map, default, name = ""):
6    """select() but allowing multiple matches of the keys.
7
8    select_multi works around a restriction in native select() that prevents multiple
9    keys from being matched unless one is a strict subset of another. For some features,
10    we allow multiple of that component to be active. For example, with codecs, we let
11    the clients mix and match anywhere from 0 built in codecs to all of them.
12
13    select_multi takes a given map and turns it into several distinct select statements
14    that have the effect of using any values associated with any active keys.
15    For example, if the following parameters are passed in:
16        values_map = {
17            ":alpha": ["apple", "apricot"],
18            ":beta": ["banana"],
19            ":gamma": ["grapefruit"],
20        },
21        default = []
22    it will be unrolled into the following select statements
23        [] + select({
24            ":apple": ["apple", "apricot"],
25            "//conditions:default": [],
26        }) + select({
27            ":beta": ["banana"],
28            "//conditions:default": [],
29        }) + select({
30            ":gamma": ["grapefruit"],
31            "//conditions:default": [],
32        })
33
34    Args:
35        values_map: dictionary of labels to a list of labels, just like select()
36        default: list of labels, the value that should be used if any of the options do not match.
37            This is typically an empty list
38        name: string unused, https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#unnamed-macro
39
40    Returns:
41        A list of values that is filled in by the generated select statements.
42    """
43    if len(values_map) == 0:
44        return default
45    rv = []
46    for key, value in values_map.items():
47        rv += select({
48            key: value,
49            "//conditions:default": default,
50        })
51    return rv
52
53def generated_cc_atom(name, **kwargs):
54    """A self-annotating label for a generated cc_library for exactly one file.
55
56    Args:
57        name: string, the name of the cc_library
58        **kwargs: All other arguments are passed verbatim to cc_library
59    """
60    if len(kwargs.get("srcs", [])) > 1 or len(kwargs.get("hdrs", [])) > 1:
61        fail("Cannot have more than one src or hdr file in generated_cc_atom")
62    if len(kwargs.get("srcs", [])) > 0 and len(kwargs.get("hdrs", [])) > 0:
63        fail("Cannot set both srcs and hdrs in generated_cc_atom")
64    if len(kwargs.get("srcs", [])) == 0 and len(kwargs.get("hdrs", [])) == 0:
65        fail("Must set exactly one of srcs or hdrs in generated_cc_atom")
66    deps = kwargs.get("deps", [])
67    deps.append("//bazel:defines_from_flags")
68    kwargs["deps"] = deps
69    native.cc_library(
70        name = name,
71        **kwargs
72    )
73