1# Copyright 2019 The Bazel Authors. All rights reserved. 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"""Common build setting rules 16These rules return a BuildSettingInfo with the value of the build setting. 17For label-typed settings, use the native label_flag and label_setting rules. 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 _impl(ctx): 32 return BuildSettingInfo(value = ctx.build_setting_value) 33 34int_flag = rule( 35 implementation = _impl, 36 build_setting = config.int(flag = True), 37 doc = "An int-typed build setting that can be set on the command line", 38) 39 40int_setting = rule( 41 implementation = _impl, 42 build_setting = config.int(), 43 doc = "An int-typed build setting that cannot be set on the command line", 44) 45 46bool_flag = rule( 47 implementation = _impl, 48 build_setting = config.bool(flag = True), 49 doc = "A bool-typed build setting that can be set on the command line", 50) 51 52bool_setting = rule( 53 implementation = _impl, 54 build_setting = config.bool(), 55 doc = "A bool-typed build setting that cannot be set on the command line", 56) 57 58string_list_flag = rule( 59 implementation = _impl, 60 build_setting = config.string_list(flag = True), 61 doc = "A string list-typed build setting that can be set on the command line", 62) 63 64string_list_setting = rule( 65 implementation = _impl, 66 build_setting = config.string_list(), 67 doc = "A string list-typed build setting that cannot be set on the command line", 68) 69 70def _string_impl(ctx): 71 allowed_values = ctx.attr.values 72 value = ctx.build_setting_value 73 if len(allowed_values) == 0 or value in ctx.attr.values: 74 return BuildSettingInfo(value = value) 75 else: 76 fail("Error setting " + str(ctx.label) + ": invalid value '" + value + "'. Allowed values are " + str(allowed_values)) 77 78string_flag = rule( 79 implementation = _string_impl, 80 build_setting = config.string(flag = True), 81 attrs = { 82 "values": attr.string_list( 83 doc = "The list of allowed values for this setting. An error is raised if any other value is given.", 84 ), 85 }, 86 doc = "A string-typed build setting that can be set on the command line", 87) 88 89string_setting = rule( 90 implementation = _string_impl, 91 build_setting = config.string(), 92 attrs = { 93 "values": attr.string_list( 94 doc = "The list of allowed values for this setting. An error is raised if any other value is given.", 95 ), 96 }, 97 doc = "A string-typed build setting that cannot be set on the command line", 98) 99