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 16 17These rules return a BuildSettingInfo with the value of the build setting. 18For label-typed settings, use the native label_flag and label_setting rules. 19 20More documentation on how to use build settings at 21https://bazel.build/extending/config#user-defined-build-settings 22""" 23 24BuildSettingInfo = provider( 25 doc = "A singleton provider that contains the raw value of a build setting", 26 fields = { 27 "value": "The value of the build setting in the current configuration. " + 28 "This value may come from the command line or an upstream transition, " + 29 "or else it will be the build setting's default.", 30 }, 31) 32 33_MAKE_VARIABLE_ATTR = attr.string( 34 doc = "If set, the build setting's value will be available as a Make variable with this " + 35 "name in the attributes of rules that list this build setting in their 'toolchains' " + 36 "attribute.", 37) 38 39def _is_valid_make_variable_char(c): 40 # Restrict make variable names for consistency with predefined ones. There are no enforced 41 # restrictions on make variable names, but when they contain e.g. spaces or braces, they 42 # aren't expanded by e.g. cc_binary. 43 return c == "_" or c.isdigit() or (c.isalpha() and c.isupper()) 44 45def _get_template_variable_info(ctx): 46 make_variable = getattr(ctx.attr, "make_variable", None) 47 if not make_variable: 48 return [] 49 50 if not all([_is_valid_make_variable_char(c) for c in make_variable.elems()]): 51 fail("Error setting " + _no_at_str(ctx.label) + ": invalid make variable name '" + make_variable + "'. Make variable names may only contain uppercase letters, digits, and underscores.") 52 53 return [ 54 platform_common.TemplateVariableInfo({ 55 make_variable: str(ctx.build_setting_value), 56 }), 57 ] 58 59def _impl(ctx): 60 return [ 61 BuildSettingInfo(value = ctx.build_setting_value), 62 ] + _get_template_variable_info(ctx) 63 64int_flag = rule( 65 implementation = _impl, 66 build_setting = config.int(flag = True), 67 attrs = { 68 "make_variable": _MAKE_VARIABLE_ATTR, 69 }, 70 doc = "An int-typed build setting that can be set on the command line", 71) 72 73int_setting = rule( 74 implementation = _impl, 75 build_setting = config.int(), 76 attrs = { 77 "make_variable": _MAKE_VARIABLE_ATTR, 78 }, 79 doc = "An int-typed build setting that cannot be set on the command line", 80) 81 82bool_flag = rule( 83 implementation = _impl, 84 build_setting = config.bool(flag = True), 85 doc = "A bool-typed build setting that can be set on the command line", 86) 87 88bool_setting = rule( 89 implementation = _impl, 90 build_setting = config.bool(), 91 doc = "A bool-typed build setting that cannot be set on the command line", 92) 93 94string_list_flag = rule( 95 implementation = _impl, 96 build_setting = config.string_list(flag = True), 97 doc = "A string list-typed build setting that can be set on the command line", 98) 99 100string_list_setting = rule( 101 implementation = _impl, 102 build_setting = config.string_list(), 103 doc = "A string list-typed build setting that cannot be set on the command line", 104) 105 106def _no_at_str(label): 107 """Strips any leading '@'s for labels in the main repo, so that the error string is more friendly.""" 108 s = str(label) 109 if s.startswith("@@//"): 110 return s[2:] 111 if s.startswith("@//"): 112 return s[1:] 113 return s 114 115def _string_impl(ctx): 116 allowed_values = ctx.attr.values 117 value = ctx.build_setting_value 118 if len(allowed_values) == 0 or value in ctx.attr.values: 119 return [BuildSettingInfo(value = value)] + _get_template_variable_info(ctx) 120 else: 121 fail("Error setting " + _no_at_str(ctx.label) + ": invalid value '" + value + "'. Allowed values are " + str(allowed_values)) 122 123string_flag = rule( 124 implementation = _string_impl, 125 build_setting = config.string(flag = True), 126 attrs = { 127 "values": attr.string_list( 128 doc = "The list of allowed values for this setting. An error is raised if any other value is given.", 129 ), 130 "make_variable": _MAKE_VARIABLE_ATTR, 131 }, 132 doc = "A string-typed build setting that can be set on the command line", 133) 134 135string_setting = rule( 136 implementation = _string_impl, 137 build_setting = config.string(), 138 attrs = { 139 "values": attr.string_list( 140 doc = "The list of allowed values for this setting. An error is raised if any other value is given.", 141 ), 142 "make_variable": _MAKE_VARIABLE_ATTR, 143 }, 144 doc = "A string-typed build setting that cannot be set on the command line", 145) 146