1# Copyright (C) 2021 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""Build setting rule. 16 17The rule returns a BuildSettingInfo with the value of the build setting. 18More documentation on how to use build settings at 19https://docs.bazel.build/versions/master/skylark/config.html#user-defined-build-settings 20""" 21 22BuildSettingInfo = provider( 23 doc = "A singleton provider that contains the raw value of a build setting", 24 fields = { 25 "value": "The value of the build setting in the current configuration. " + 26 "This value may come from the command line or an upstream transition, " + 27 "or else it will be the build setting's default.", 28 }, 29) 30 31def _string_impl(ctx): 32 allowed_values = ctx.attr.values 33 value = ctx.build_setting_value 34 35 if len(allowed_values) == 0 or value in ctx.attr.values: 36 return BuildSettingInfo(value = value) 37 fail("Error setting " + str(ctx.label) + ": invalid value '" + value + "'. Allowed values are " + str(allowed_values)) 38 39string_flag = rule( 40 implementation = _string_impl, 41 build_setting = config.string(flag = True), 42 attrs = { 43 "values": attr.string_list( 44 doc = "The list of allowed values for this setting. An error is raised if any other value is given.", 45 ), 46 }, 47 doc = "A string-typed build setting that can be set on the command line", 48) 49 50def _impl(ctx): 51 return BuildSettingInfo(value = ctx.build_setting_value) 52 53string_list_flag = rule( 54 implementation = _impl, 55 build_setting = config.string_list(flag = True), 56 doc = "A string list-typed build setting that can be set on the command line", 57) 58