1# https://github.com/bazelbuild/bazel-skylib/blob/main/rules/common_settings.bzl 2load("@bazel_skylib//rules:common_settings.bzl", "string_flag") 3 4# Forked from https://github.com/bazelbuild/bazel-skylib/blob/main/rules/common_settings.bzl 5BuildSettingInfo = provider(fields = ["values"]) 6 7def _multi_string_impl(ctx): 8 allowed_values = ctx.attr.values 9 values = ctx.build_setting_value 10 for v in values: 11 if v not in ctx.attr.values: 12 fail("Error setting " + str(ctx.label) + ": invalid value '" + v + "'. Allowed values are " + str(allowed_values)) 13 return BuildSettingInfo(values = values) 14 15multi_string_flag = rule( 16 implementation = _multi_string_impl, 17 build_setting = config.string(flag = True, allow_multiple = True), 18 attrs = { 19 "values": attr.string_list( 20 doc = "The list of allowed values for this setting. An error is raised if any other values are given.", 21 ), 22 }, 23 doc = "A string-typed build setting that can be set multiple times on the command line", 24) 25 26# string_flag_with_values is a Bazel Macro that defines a flag with the given name and a set 27# of valid values for that flag. For each value, a config_setting is defined with the name 28# of the value, associated with the created flag. 29# This is defined to make the BUILD.bazel file easier to read w/o the boilerplate of defining 30# a string_flag rule and n config_settings 31# https://docs.bazel.build/versions/main/skylark/macros.html 32def string_flag_with_values(flag_name, values, default = "", multiple = False): 33 if multiple: 34 multi_string_flag( 35 name = flag_name, 36 # We have to specify a default value, even if that value is empty string. 37 # https://docs.bazel.build/versions/main/skylark/config.html#instantiating-build-settings 38 build_setting_default = default, 39 # If empty string is the default, we need to make sure it is in the list 40 # of acceptable values. If the default is not empty string, we don't want 41 # to make empty string a valid value. Having duplicate values in the list 42 # does not cause any issues, so we can just add the default to achieve 43 # this affect. 44 values = values + [default], 45 ) 46 else: 47 string_flag( 48 name = flag_name, 49 # We have to specify a default value, even if that value is empty string. 50 # https://docs.bazel.build/versions/main/skylark/config.html#instantiating-build-settings 51 build_setting_default = default, 52 # If empty string is the default, we need to make sure it is in the list 53 # of acceptable values. If the default is not empty string, we don't want 54 # to make empty string a valid value. Having duplicate values in the list 55 # does not cause any issues, so we can just add the default to achieve 56 # this affect. 57 values = values + [default], 58 ) 59 60 # For each of the values given, we define a config_setting. This allows us to use 61 # select statements, on the given setting, e.g. referencing 62 # //bazel/common_config_settings:some_valid_value_for_a_flag 63 for v in values: 64 native.config_setting( 65 name = v, 66 flag_values = { 67 ":" + flag_name: v, 68 }, 69 ) 70