Debian Codegraph @

bazel/flags.bzl

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