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