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