1""" 2This file contains helpers for defining build flags and options that are used to 3configure the Skia build. 4""" 5 6# https://github.com/bazelbuild/bazel-skylib/blob/main/rules/common_settings.bzl 7load("@bazel_skylib//rules:common_settings.bzl", "string_flag", skylib_bool_flag = "bool_flag") 8 9# Forked from https://github.com/bazelbuild/bazel-skylib/blob/main/rules/common_settings.bzl 10BuildSettingInfo = provider( 11 doc = "A singleton provider that contains the raw value of a multi-string build setting", 12 fields = ["values"], 13) 14 15def _multi_string_impl(ctx): 16 allowed_values = ctx.attr.values 17 values = ctx.build_setting_value 18 for v in values: 19 if v not in ctx.attr.values: 20 fail("Error setting " + str(ctx.label) + ": invalid value '" + v + "'. Allowed values are " + str(allowed_values)) 21 return BuildSettingInfo(values = values) 22 23multi_string_flag = rule( 24 implementation = _multi_string_impl, 25 build_setting = config.string(flag = True, allow_multiple = True), 26 attrs = { 27 "values": attr.string_list( 28 doc = "The list of allowed values for this setting. An error is raised if any other values are given.", 29 ), 30 }, 31 doc = "A string-typed build setting that can be set multiple times on the command line", 32) 33 34def string_flag_with_values(flag_name, values, default = "", multiple = False, name = ""): 35 """Create a string flag and corresponding config_settings. 36 37 string_flag_with_values is a Bazel Macro that defines a flag with the given name and a set 38 of valid values for that flag. For each value, a config_setting is defined with the name 39 of the value, associated with the created flag. 40 This is defined to make the BUILD.bazel file easier to read w/o the boilerplate of defining 41 a string_flag rule and n config_settings 42 https://docs.bazel.build/versions/main/skylark/macros.html 43 44 Args: 45 flag_name: string, the name of the flag to create and use for the config_settings 46 values: list of strings, the valid values for this flag to be set to. 47 default: string, whatever the default value should be if the flag is not set. Can be 48 empty string for both a string_flag and a multi_string flag. 49 multiple: boolean, True if the flag should be able to be set multiple times on the CLI. 50 name: string unused, https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#unnamed-macro 51 """ 52 if multiple: 53 multi_string_flag( 54 name = flag_name, 55 # We have to specify a default value, even if that value is empty string. 56 # https://docs.bazel.build/versions/main/skylark/config.html#instantiating-build-settings 57 build_setting_default = default, 58 # If empty string is the default, we need to make sure it is in the list 59 # of acceptable values. If the default is not empty string, we don't want 60 # to make empty string a valid value. Having duplicate values in the list 61 # does not cause any issues, so we can just add the default to achieve 62 # this affect. 63 values = values + [default], 64 ) 65 else: 66 string_flag( 67 name = flag_name, 68 # We have to specify a default value, even if that value is empty string. 69 # https://docs.bazel.build/versions/main/skylark/config.html#instantiating-build-settings 70 build_setting_default = default, 71 # If empty string is the default, we need to make sure it is in the list 72 # of acceptable values. If the default is not empty string, we don't want 73 # to make empty string a valid value. Having duplicate values in the list 74 # does not cause any issues, so we can just add the default to achieve 75 # this affect. 76 values = values + [default], 77 ) 78 79 # For each of the values given, we define a config_setting. This allows us to use 80 # select statements, on the given setting, e.g. referencing 81 # //bazel/common_config_settings:some_valid_value_for_a_flag 82 for v in values: 83 native.config_setting( 84 name = v, 85 flag_values = { 86 ":" + flag_name: v, 87 }, 88 ) 89 90def bool_flag(flag_name, default = True, name = ""): 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 flag_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 name: string unused, https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#unnamed-macro 103 """ 104 skylib_bool_flag(name = flag_name, build_setting_default = default) 105 106 native.config_setting( 107 name = flag_name + "_true", 108 flag_values = { 109 # The value must be a string, but it will be parsed to a boolean 110 # https://docs.bazel.build/versions/main/skylark/config.html#build-settings-and-select 111 ":" + flag_name: "True", 112 }, 113 ) 114 115 native.config_setting( 116 name = flag_name + "_false", 117 flag_values = { 118 ":" + flag_name: "False", 119 }, 120 ) 121