• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2THIS IS THE EXTERNAL-ONLY VERSION OF THIS FILE. G3 HAS ITS OWN.
3
4This file contains helpers for defining build flags and options that are used to
5configure the Skia build.
6"""
7
8# https://github.com/bazelbuild/bazel-skylib/blob/main/rules/common_settings.bzl
9load("@bazel_skylib//rules:common_settings.bzl", "string_flag", skylib_bool_flag = "bool_flag")
10load("@bazel_skylib//lib:selects.bzl", _selects = "selects")
11
12# Re-export other symbols from bazel_skylib for convenience
13selects = _selects
14
15# Forked from https://github.com/bazelbuild/bazel-skylib/blob/main/rules/common_settings.bzl
16BuildSettingInfo = provider(
17    doc = "A singleton provider that contains the raw value of a multi-string build setting",
18    fields = ["values"],
19)
20
21def _multi_string_impl(ctx):
22    allowed_values = ctx.attr.values
23    values = ctx.build_setting_value
24    for v in values:
25        if v not in ctx.attr.values:
26            fail("Error setting " + str(ctx.label) + ": invalid value '" + v + "'. Allowed values are " + str(allowed_values))
27    return BuildSettingInfo(values = values)
28
29multi_string_flag = rule(
30    implementation = _multi_string_impl,
31    # https://bazel.build/rules/lib/config#string_list
32    build_setting = config.string_list(flag = True, repeatable = True),
33    attrs = {
34        "values": attr.string_list(
35            doc = "The list of allowed values for this setting. An error is raised if any other values are given.",
36        ),
37    },
38    doc = "A string-typed build setting that can be set multiple times on the command line",
39)
40
41def string_flag_with_values(name, values, default = "", multiple = False):
42    """Create a string flag and corresponding config_settings.
43
44    string_flag_with_values is a Bazel Macro that defines a flag with the given name and a set
45    of valid values for that flag. For each value, a config_setting is defined with the name
46    of the value, associated with the created flag.
47    This is defined to make the BUILD.bazel file easier to read w/o the boilerplate of defining
48    a string_flag rule and n config_settings
49    https://docs.bazel.build/versions/main/skylark/macros.html
50
51    Args:
52        name: string, the name of the flag to create and use for the config_settings
53        values: list of strings, the valid values for this flag to be set to.
54        default: string, whatever the default value should be if the flag is not set. Can be
55            empty string for both a string_flag and a multi_string flag.
56        multiple: boolean, True if the flag should be able to be set multiple times on the CLI.
57    """
58    if multiple:
59        multi_string_flag(
60            name = name,
61            # We have to specify a default value, even if that value is empty string.
62            # https://docs.bazel.build/versions/main/skylark/config.html#instantiating-build-settings
63            build_setting_default = [default],
64            # We need to make sure empty string (the default) is in the list of acceptable values.
65            values = values + [""],
66        )
67    else:
68        string_flag(
69            name = name,
70            # We have to specify a default value, even if that value is empty string.
71            # https://docs.bazel.build/versions/main/skylark/config.html#instantiating-build-settings
72            build_setting_default = default,
73            # We need to make sure empty string (the default) is in the list of acceptable values.
74            values = values + [""],
75        )
76
77    # For each of the values given, we define a config_setting. This allows us to use
78    # select statements, on the given setting, e.g. referencing
79    # //bazel/common_config_settings:some_valid_value_for_a_flag
80    for v in values:
81        native.config_setting(
82            name = v,
83            flag_values = {
84                ":" + name: v,
85            },
86            visibility = ["//:__subpackages__"],
87        )
88
89def bool_flag(name, default, public = True):
90    """Create a boolean flag and corresponding config_settings.
91
92    bool_flag is a Bazel Macro that defines a boolean flag with the given name two config_settings,
93    one for True, one for False. Reminder that Bazel has special syntax for unsetting boolean flags,
94    but this does not work well with aliases.
95    https://docs.bazel.build/versions/main/skylark/config.html#using-build-settings-on-the-command-line
96    Thus it is best to define both an "enabled" alias and a "disabled" alias.
97
98    Args:
99        name: string, the name of the flag to create and use for the config_settings
100        default: boolean, if the flag should default to on or off.
101        public: boolean, if the flag should be usable from other packages or if it is meant to be
102            combined with some other constraint.
103    """
104
105    skylib_bool_flag(name = name, build_setting_default = default)
106    vis = ["//:__subpackages__"]
107    if not public:
108        vis = ["//visibility:private"]
109
110    native.config_setting(
111        name = name + "_true",
112        flag_values = {
113            # The value must be a string, but it will be parsed to a boolean
114            # https://docs.bazel.build/versions/main/skylark/config.html#build-settings-and-select
115            ":" + name: "True",
116        },
117        visibility = vis,
118    )
119
120    native.config_setting(
121        name = name + "_false",
122        flag_values = {
123            ":" + name: "False",
124        },
125        visibility = vis,
126    )
127