Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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 | package com.google.devtools.build.lib.packages; |
| 16 | |
Greg Estren | 4ccabd3 | 2017-03-14 17:12:45 +0000 | [diff] [blame] | 17 | import static com.google.common.collect.Sets.newEnumSet; |
| 18 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.common.annotations.VisibleForTesting; |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 20 | import com.google.common.base.Function; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 21 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 22 | import com.google.common.base.Predicate; |
| 23 | import com.google.common.base.Predicates; |
Greg Estren | 9d83784 | 2016-12-01 21:36:59 +0000 | [diff] [blame] | 24 | import com.google.common.base.Verify; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableList; |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 26 | import com.google.common.collect.ImmutableMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import com.google.common.collect.ImmutableSet; |
| 28 | import com.google.common.collect.Iterables; |
djasper | 1b86912 | 2019-03-04 10:41:50 -0800 | [diff] [blame] | 29 | import com.google.common.collect.Lists; |
nharmata | b81776f | 2019-02-25 15:52:33 -0800 | [diff] [blame] | 30 | import com.google.common.collect.Maps; |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 31 | import com.google.common.collect.Ordering; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 32 | import com.google.common.collect.Sets; |
Liam Miller-Cushon | bbae4d9 | 2016-05-03 18:23:03 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.analysis.TransitiveInfoProvider; |
gregce | 7fa23ea | 2018-01-18 12:46:04 -0800 | [diff] [blame] | 34 | import com.google.devtools.build.lib.analysis.config.transitions.NoTransition; |
John Cater | 0a9e1ed | 2019-03-27 11:02:01 -0700 | [diff] [blame] | 35 | import com.google.devtools.build.lib.analysis.config.transitions.TransitionFactory; |
janakr | a56a6ad | 2018-02-02 15:52:22 -0800 | [diff] [blame] | 36 | import com.google.devtools.build.lib.cmdline.Label; |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 37 | import com.google.devtools.build.lib.events.EventHandler; |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 38 | import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassNamePredicate; |
Googler | c5fcc86 | 2019-09-06 16:17:47 -0700 | [diff] [blame] | 39 | import com.google.devtools.build.lib.packages.Type.ConversionException; |
| 40 | import com.google.devtools.build.lib.packages.Type.LabelClass; |
mjhalupka | 7b398f9 | 2018-03-08 12:08:25 -0800 | [diff] [blame] | 41 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
mjhalupka | cfa0bb7 | 2018-03-12 12:43:15 -0700 | [diff] [blame] | 42 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | import com.google.devtools.build.lib.util.FileType; |
| 44 | import com.google.devtools.build.lib.util.FileTypeSet; |
| 45 | import com.google.devtools.build.lib.util.StringUtil; |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 46 | import java.util.ArrayList; |
Carmi Grushko | e80cccd | 2015-11-11 19:12:31 +0000 | [diff] [blame] | 47 | import java.util.Arrays; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 48 | import java.util.Collection; |
| 49 | import java.util.EnumSet; |
| 50 | import java.util.HashMap; |
Dmitry Lomov | f188dc2 | 2016-07-19 09:00:55 +0000 | [diff] [blame] | 51 | import java.util.LinkedHashMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 52 | import java.util.List; |
| 53 | import java.util.Map; |
janakr | 9ba46f8 | 2018-03-13 13:07:52 -0700 | [diff] [blame] | 54 | import java.util.Objects; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 55 | import java.util.Set; |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 56 | import java.util.concurrent.atomic.AtomicReference; |
vladmos | 9c787fa | 2017-07-04 11:45:22 -0400 | [diff] [blame] | 57 | import javax.annotation.Nullable; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 58 | import javax.annotation.concurrent.Immutable; |
adonovan | b74f160 | 2020-11-02 17:33:49 -0800 | [diff] [blame] | 59 | import net.starlark.java.eval.Dict; |
adonovan | 450c7ad | 2020-09-14 13:00:21 -0700 | [diff] [blame] | 60 | import net.starlark.java.eval.EvalException; |
| 61 | import net.starlark.java.eval.Starlark; |
| 62 | import net.starlark.java.eval.StarlarkValue; |
adonovan | 267bdaa | 2020-11-04 11:32:24 -0800 | [diff] [blame] | 63 | import net.starlark.java.eval.Structure; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 64 | |
| 65 | /** |
mjhalupka | cfa0bb7 | 2018-03-12 12:43:15 -0700 | [diff] [blame] | 66 | * Metadata of a rule attribute. Contains the attribute name and type, and an default value to be |
| 67 | * used if none is provided in a rule declaration in a BUILD file. Attributes are immutable, and may |
| 68 | * be shared by more than one rule (for example, <code>foo_binary</code> and <code>foo_library |
| 69 | * </code> may share many attributes in common). |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 70 | */ |
| 71 | @Immutable |
| 72 | public final class Attribute implements Comparable<Attribute> { |
| 73 | |
Googler | 72f3a10 | 2017-12-01 16:28:28 -0800 | [diff] [blame] | 74 | public static final RuleClassNamePredicate ANY_RULE = RuleClassNamePredicate.unspecified(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 75 | |
janakr | f4523e7 | 2019-06-05 14:11:06 -0700 | [diff] [blame] | 76 | private static final RuleClassNamePredicate NO_RULE = RuleClassNamePredicate.only(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 77 | |
mjhalupka | cfa0bb7 | 2018-03-12 12:43:15 -0700 | [diff] [blame] | 78 | /** Wraps the information necessary to construct an Aspect. */ |
| 79 | @VisibleForSerialization |
| 80 | abstract static class RuleAspect<C extends AspectClass> { |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 81 | protected final C aspectClass; |
| 82 | protected final Function<Rule, AspectParameters> parametersExtractor; |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 83 | |
janakr | f4523e7 | 2019-06-05 14:11:06 -0700 | [diff] [blame] | 84 | private RuleAspect(C aspectClass, Function<Rule, AspectParameters> parametersExtractor) { |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 85 | this.aspectClass = aspectClass; |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 86 | this.parametersExtractor = parametersExtractor; |
| 87 | } |
| 88 | |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 89 | public String getName() { |
| 90 | return this.aspectClass.getName(); |
| 91 | } |
| 92 | |
| 93 | public ImmutableSet<String> getRequiredParameters() { |
| 94 | return ImmutableSet.<String>of(); |
| 95 | } |
| 96 | |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 97 | public abstract Aspect getAspect(Rule rule); |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 98 | |
| 99 | public C getAspectClass() { |
| 100 | return aspectClass; |
| 101 | } |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 104 | private static class NativeRuleAspect extends RuleAspect<NativeAspectClass> { |
janakr | f4523e7 | 2019-06-05 14:11:06 -0700 | [diff] [blame] | 105 | NativeRuleAspect( |
| 106 | NativeAspectClass aspectClass, Function<Rule, AspectParameters> parametersExtractor) { |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 107 | super(aspectClass, parametersExtractor); |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 108 | } |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 109 | |
| 110 | @Override |
| 111 | public Aspect getAspect(Rule rule) { |
Googler | 459f130 | 2017-04-05 19:40:35 +0000 | [diff] [blame] | 112 | AspectParameters params = parametersExtractor.apply(rule); |
| 113 | return params == null ? null : Aspect.forNative(aspectClass, params); |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
janakr | f4523e7 | 2019-06-05 14:11:06 -0700 | [diff] [blame] | 117 | @VisibleForSerialization |
mjhalupka | cfa0bb7 | 2018-03-12 12:43:15 -0700 | [diff] [blame] | 118 | @AutoCodec |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 119 | static class StarlarkRuleAspect extends RuleAspect<StarlarkAspectClass> { |
gregce | d281df7 | 2020-05-11 12:27:06 -0700 | [diff] [blame] | 120 | private final StarlarkDefinedAspect aspect; |
Dmitry Lomov | 777845c | 2016-04-06 15:24:36 +0000 | [diff] [blame] | 121 | |
janakr | f4523e7 | 2019-06-05 14:11:06 -0700 | [diff] [blame] | 122 | @VisibleForSerialization |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 123 | StarlarkRuleAspect(StarlarkDefinedAspect aspect) { |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 124 | super(aspect.getAspectClass(), aspect.getDefaultParametersExtractor()); |
| 125 | this.aspect = aspect; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public ImmutableSet<String> getRequiredParameters() { |
| 130 | return aspect.getParamAttributes(); |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public Aspect getAspect(Rule rule) { |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 135 | AspectParameters parameters = parametersExtractor.apply(rule); |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 136 | return Aspect.forStarlark(aspectClass, aspect.getDefinition(parameters), parameters); |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Michajlo Matijkiw | ccd5fb1 | 2016-07-26 20:58:42 +0000 | [diff] [blame] | 140 | /** A RuleAspect that just wraps a pre-existing Aspect that doesn't vary with the Rule. */ |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 141 | private static class PredefinedRuleAspect extends RuleAspect<AspectClass> { |
Kevin Bierhoff | 6f1d608 | 2017-03-24 21:17:13 +0000 | [diff] [blame] | 142 | private final Aspect aspect; |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 143 | |
janakr | f4523e7 | 2019-06-05 14:11:06 -0700 | [diff] [blame] | 144 | PredefinedRuleAspect(Aspect aspect) { |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 145 | super(aspect.getAspectClass(), null); |
| 146 | this.aspect = aspect; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public Aspect getAspect(Rule rule) { |
| 151 | return aspect; |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
janakr | f4523e7 | 2019-06-05 14:11:06 -0700 | [diff] [blame] | 155 | private enum PropertyFlag { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 156 | MANDATORY, |
| 157 | EXECUTABLE, |
| 158 | UNDOCUMENTED, |
| 159 | TAGGABLE, |
| 160 | |
| 161 | /** |
| 162 | * Whether the list attribute is order-independent and can be sorted. |
| 163 | */ |
| 164 | ORDER_INDEPENDENT, |
| 165 | |
| 166 | /** |
| 167 | * Whether the allowedRuleClassesForLabels or allowedFileTypesForLabels are |
| 168 | * set to custom values. If so, and the attribute is called "deps", the |
| 169 | * legacy deps checking is skipped, and the new stricter checks are used |
| 170 | * instead. For non-"deps" attributes, this allows skipping the check if it |
| 171 | * would pass anyway, as the default setting allows any rule classes and |
| 172 | * file types. |
| 173 | */ |
| 174 | STRICT_LABEL_CHECKING, |
| 175 | |
| 176 | /** |
| 177 | * Set for things that would cause the a compile or lint-like action to |
| 178 | * be executed when the input changes. Used by compile_one_dependency. |
| 179 | * Set for attributes like hdrs and srcs on cc_ rules or srcs on java_ |
| 180 | * or py_rules. Generally not set on data/resource attributes. |
| 181 | */ |
| 182 | DIRECT_COMPILE_TIME_INPUT, |
| 183 | |
| 184 | /** |
| 185 | * Whether the value of the list type attribute must not be an empty list. |
| 186 | */ |
| 187 | NON_EMPTY, |
| 188 | |
| 189 | /** |
| 190 | * Verifies that the referenced rule produces a single artifact. Note that this check happens |
| 191 | * on a per label basis, i.e. the check happens separately for every label in a label list. |
| 192 | */ |
| 193 | SINGLE_ARTIFACT, |
| 194 | |
| 195 | /** |
| 196 | * Whether we perform silent ruleclass filtering of the dependencies of the label type |
| 197 | * attribute according to their rule classes. I.e. elements of the list which don't match the |
| 198 | * allowedRuleClasses predicate or not rules will be filtered out without throwing any errors. |
| 199 | * This flag is introduced to handle plugins, do not use it in other cases. |
| 200 | */ |
| 201 | SILENT_RULECLASS_FILTER, |
| 202 | |
| 203 | // TODO(bazel-team): This is a hack introduced because of the bad design of the original rules. |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 204 | // Depot cleanup would be too expensive, but don't migrate this to Starlark. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 205 | /** |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 206 | * Whether to perform analysis time filetype check on this label-type attribute or not. If the |
| 207 | * flag is set, we skip the check that applies the allowedFileTypes filter to generated files. |
| 208 | * Do not use this if avoidable. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 209 | */ |
| 210 | SKIP_ANALYSIS_TIME_FILETYPE_CHECK, |
| 211 | |
| 212 | /** |
| 213 | * Whether the value of the attribute should come from a given set of values. |
| 214 | */ |
| 215 | CHECK_ALLOWED_VALUES, |
| 216 | |
| 217 | /** |
| 218 | * Whether this attribute is opted out of "configurability", i.e. the ability to determine |
| 219 | * its value based on properties of the build configuration. |
| 220 | */ |
| 221 | NONCONFIGURABLE, |
Ulf Adams | 0b63897 | 2015-09-08 13:25:35 +0000 | [diff] [blame] | 222 | |
| 223 | /** |
jcater | 0de1097 | 2020-04-07 12:15:05 -0700 | [diff] [blame] | 224 | * Whether we should skip dependency validation checks done by {@link |
| 225 | * com.google.devtools.build.lib.analysis.RuleContext.PrerequisiteValidator} (for visibility, |
| 226 | * etc.). |
Ulf Adams | 0b63897 | 2015-09-08 13:25:35 +0000 | [diff] [blame] | 227 | */ |
Greg Estren | 875c7a7 | 2015-09-24 19:57:54 +0000 | [diff] [blame] | 228 | SKIP_PREREQ_VALIDATOR_CHECKS, |
Greg Estren | 4a10251 | 2015-10-28 20:49:22 +0000 | [diff] [blame] | 229 | |
| 230 | /** |
Greg Estren | 9d83784 | 2016-12-01 21:36:59 +0000 | [diff] [blame] | 231 | * Whether we should check constraints on this attribute even if default enforcement policy |
| 232 | * would skip it. See |
| 233 | * {@link com.google.devtools.build.lib.analysis.constraints.ConstraintSemantics} for more on |
| 234 | * constraints. |
Greg Estren | 4a10251 | 2015-10-28 20:49:22 +0000 | [diff] [blame] | 235 | */ |
Greg Estren | 9d83784 | 2016-12-01 21:36:59 +0000 | [diff] [blame] | 236 | CHECK_CONSTRAINTS_OVERRIDE, |
| 237 | |
| 238 | /** |
| 239 | * Whether we should skip constraints checking on this attribute even if default enforcement |
| 240 | * policy would check it. |
| 241 | */ |
| 242 | SKIP_CONSTRAINTS_OVERRIDE, |
Irina Iancu | 2f23599 | 2017-01-10 16:03:29 +0000 | [diff] [blame] | 243 | |
| 244 | /** |
| 245 | * Whether we should use output_licenses to check the licences on this attribute. |
| 246 | */ |
| 247 | OUTPUT_LICENSES, |
Googler | c2200fd | 2018-09-14 17:35:59 -0700 | [diff] [blame] | 248 | |
| 249 | /** |
cparsons | f626639 | 2018-10-18 15:42:52 -0700 | [diff] [blame] | 250 | * Has a Starlark-defined configuration transition. Transitions for analysis testing are tracked |
| 251 | * separately: see {@link #HAS_ANALYSIS_TEST_TRANSITION}. |
Googler | c2200fd | 2018-09-14 17:35:59 -0700 | [diff] [blame] | 252 | */ |
cparsons | f626639 | 2018-10-18 15:42:52 -0700 | [diff] [blame] | 253 | HAS_STARLARK_DEFINED_TRANSITION, |
| 254 | |
| 255 | /** |
| 256 | * Has a Starlark-defined configuration transition designed specifically for rules which |
| 257 | * run analysis tests. |
| 258 | */ |
| 259 | HAS_ANALYSIS_TEST_TRANSITION, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | // TODO(bazel-team): modify this interface to extend Predicate and have an extra error |
| 263 | // message function like AllowedValues does |
| 264 | /** |
| 265 | * A predicate-like class that determines whether an edge between two rules is valid or not. |
| 266 | */ |
| 267 | public interface ValidityPredicate { |
| 268 | /** |
| 269 | * This method should return null if the edge is valid, or a suitable error message |
| 270 | * if it is not. Note that warnings are not supported. |
| 271 | */ |
| 272 | String checkValid(Rule from, Rule to); |
| 273 | } |
| 274 | |
janakr | f4523e7 | 2019-06-05 14:11:06 -0700 | [diff] [blame] | 275 | @AutoCodec public static final ValidityPredicate ANY_EDGE = (from, to) -> null; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 276 | |
mjhalupka | cfa0bb7 | 2018-03-12 12:43:15 -0700 | [diff] [blame] | 277 | /** A predicate class to check if the value of the attribute comes from a predefined set. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 278 | public static class AllowedValueSet implements PredicateWithMessage<Object> { |
| 279 | |
| 280 | private final Set<Object> allowedValues; |
| 281 | |
brandjon | af77a30 | 2019-01-22 13:28:57 -0800 | [diff] [blame] | 282 | public AllowedValueSet(Object... values) { |
Carmi Grushko | e80cccd | 2015-11-11 19:12:31 +0000 | [diff] [blame] | 283 | this(Arrays.asList(values)); |
| 284 | } |
| 285 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 286 | public AllowedValueSet(Iterable<?> values) { |
| 287 | Preconditions.checkNotNull(values); |
| 288 | Preconditions.checkArgument(!Iterables.isEmpty(values)); |
adonovan | 3ed7ed5 | 2020-09-30 12:03:28 -0700 | [diff] [blame] | 289 | for (Object v : values) { |
| 290 | Starlark.checkValid(v); |
| 291 | } |
brandjon | ed24961 | 2017-07-11 22:06:22 +0200 | [diff] [blame] | 292 | allowedValues = ImmutableSet.copyOf(values); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public boolean apply(Object input) { |
| 297 | return allowedValues.contains(input); |
| 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public String getErrorReason(Object value) { |
| 302 | return String.format("has to be one of %s instead of '%s'", |
| 303 | StringUtil.joinEnglishList(allowedValues, "or", "'"), value); |
| 304 | } |
| 305 | |
| 306 | @VisibleForTesting |
| 307 | public Collection<Object> getAllowedValues() { |
| 308 | return allowedValues; |
| 309 | } |
| 310 | } |
| 311 | |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 312 | public ImmutableMap<String, ImmutableSet<String>> getRequiredAspectParameters() { |
| 313 | ImmutableMap.Builder<String, ImmutableSet<String>> paramBuilder = ImmutableMap.builder(); |
| 314 | for (RuleAspect<?> aspect : aspects) { |
| 315 | paramBuilder.put(aspect.getName(), aspect.getRequiredParameters()); |
| 316 | } |
| 317 | return paramBuilder.build(); |
| 318 | } |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 319 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 320 | /** |
| 321 | * Creates a new attribute builder. |
| 322 | * |
| 323 | * @param name attribute name |
| 324 | * @param type attribute type |
| 325 | * @return attribute builder |
| 326 | * |
| 327 | * @param <TYPE> attribute type class |
| 328 | */ |
| 329 | public static <TYPE> Attribute.Builder<TYPE> attr(String name, Type<TYPE> type) { |
| 330 | return new Builder<>(name, type); |
| 331 | } |
| 332 | |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 333 | /** A factory to generate {@link Attribute} instances. */ |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 334 | public static class ImmutableAttributeFactory { |
| 335 | private final Type<?> type; |
Klaus Aehlig | 04952fc | 2019-03-01 11:36:16 -0800 | [diff] [blame] | 336 | private final String doc; |
John Cater | 2c0dece | 2019-04-02 09:18:18 -0700 | [diff] [blame] | 337 | private final TransitionFactory<AttributeTransitionData> transitionFactory; |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 338 | private final RuleClassNamePredicate allowedRuleClassesForLabels; |
| 339 | private final RuleClassNamePredicate allowedRuleClassesForLabelsWarning; |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 340 | private final FileTypeSet allowedFileTypesForLabels; |
| 341 | private final ValidityPredicate validityPredicate; |
| 342 | private final Object value; |
| 343 | private final AttributeValueSource valueSource; |
| 344 | private final boolean valueSet; |
| 345 | private final Predicate<AttributeMap> condition; |
| 346 | private final ImmutableSet<PropertyFlag> propertyFlags; |
| 347 | private final PredicateWithMessage<Object> allowedValues; |
| 348 | private final RequiredProviders requiredProviders; |
| 349 | private final ImmutableList<RuleAspect<?>> aspects; |
| 350 | |
janakr | f4523e7 | 2019-06-05 14:11:06 -0700 | [diff] [blame] | 351 | private ImmutableAttributeFactory( |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 352 | Type<?> type, |
Klaus Aehlig | 04952fc | 2019-03-01 11:36:16 -0800 | [diff] [blame] | 353 | String doc, |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 354 | ImmutableSet<PropertyFlag> propertyFlags, |
| 355 | Object value, |
John Cater | 2c0dece | 2019-04-02 09:18:18 -0700 | [diff] [blame] | 356 | TransitionFactory<AttributeTransitionData> transitionFactory, |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 357 | RuleClassNamePredicate allowedRuleClassesForLabels, |
| 358 | RuleClassNamePredicate allowedRuleClassesForLabelsWarning, |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 359 | FileTypeSet allowedFileTypesForLabels, |
| 360 | ValidityPredicate validityPredicate, |
| 361 | AttributeValueSource valueSource, |
| 362 | boolean valueSet, |
| 363 | Predicate<AttributeMap> condition, |
| 364 | PredicateWithMessage<Object> allowedValues, |
| 365 | RequiredProviders requiredProviders, |
| 366 | ImmutableList<RuleAspect<?>> aspects) { |
| 367 | this.type = type; |
Klaus Aehlig | 04952fc | 2019-03-01 11:36:16 -0800 | [diff] [blame] | 368 | this.doc = doc; |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 369 | this.transitionFactory = transitionFactory; |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 370 | this.allowedRuleClassesForLabels = allowedRuleClassesForLabels; |
| 371 | this.allowedRuleClassesForLabelsWarning = allowedRuleClassesForLabelsWarning; |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 372 | this.allowedFileTypesForLabels = allowedFileTypesForLabels; |
| 373 | this.validityPredicate = validityPredicate; |
| 374 | this.value = value; |
| 375 | this.valueSource = valueSource; |
| 376 | this.valueSet = valueSet; |
| 377 | this.condition = condition; |
| 378 | this.propertyFlags = propertyFlags; |
| 379 | this.allowedValues = allowedValues; |
| 380 | this.requiredProviders = requiredProviders; |
| 381 | this.aspects = aspects; |
| 382 | } |
| 383 | |
| 384 | public AttributeValueSource getValueSource() { |
| 385 | return valueSource; |
| 386 | } |
| 387 | |
| 388 | public boolean isValueSet() { |
| 389 | return valueSet; |
| 390 | } |
| 391 | |
| 392 | public Attribute build(String name) { |
| 393 | Preconditions.checkState(!name.isEmpty(), "name has not been set"); |
| 394 | if (valueSource == AttributeValueSource.LATE_BOUND) { |
| 395 | Preconditions.checkState(isLateBound(name)); |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 396 | Preconditions.checkState(!transitionFactory.isSplit()); |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 397 | } |
| 398 | // TODO(bazel-team): Set the default to be no file type, then remove this check, and also |
| 399 | // remove all allowedFileTypes() calls without parameters. |
| 400 | |
| 401 | // do not modify this.allowedFileTypesForLabels, instead create a copy. |
| 402 | FileTypeSet allowedFileTypesForLabels = this.allowedFileTypesForLabels; |
| 403 | if (type.getLabelClass() == LabelClass.DEPENDENCY) { |
| 404 | if (isPrivateAttribute(name) && allowedFileTypesForLabels == null) { |
| 405 | allowedFileTypesForLabels = FileTypeSet.ANY_FILE; |
| 406 | } |
| 407 | Preconditions.checkNotNull( |
| 408 | allowedFileTypesForLabels, "allowedFileTypesForLabels not set for %s", name); |
| 409 | } else if (type.getLabelClass() == LabelClass.OUTPUT) { |
| 410 | // TODO(bazel-team): Set the default to no file type and make explicit calls instead. |
| 411 | if (allowedFileTypesForLabels == null) { |
| 412 | allowedFileTypesForLabels = FileTypeSet.ANY_FILE; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | return new Attribute( |
| 417 | name, |
Klaus Aehlig | 04952fc | 2019-03-01 11:36:16 -0800 | [diff] [blame] | 418 | doc, |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 419 | type, |
| 420 | propertyFlags, |
| 421 | value, |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 422 | transitionFactory, |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 423 | allowedRuleClassesForLabels, |
| 424 | allowedRuleClassesForLabelsWarning, |
| 425 | allowedFileTypesForLabels, |
| 426 | validityPredicate, |
| 427 | condition, |
| 428 | allowedValues, |
| 429 | requiredProviders, |
| 430 | aspects); |
| 431 | } |
| 432 | } |
| 433 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 434 | /** |
| 435 | * A fluent builder for the {@code Attribute} instances. |
| 436 | * |
| 437 | * <p>All methods could be called only once per builder. The attribute |
| 438 | * already undocumented based on its name cannot be marked as undocumented. |
| 439 | */ |
| 440 | public static class Builder <TYPE> { |
Kevin Bierhoff | 6f1d608 | 2017-03-24 21:17:13 +0000 | [diff] [blame] | 441 | private final String name; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 442 | private final Type<TYPE> type; |
John Cater | 2c0dece | 2019-04-02 09:18:18 -0700 | [diff] [blame] | 443 | private TransitionFactory<AttributeTransitionData> transitionFactory = |
| 444 | NoTransition.createFactory(); |
Googler | 72f3a10 | 2017-12-01 16:28:28 -0800 | [diff] [blame] | 445 | private RuleClassNamePredicate allowedRuleClassesForLabels = ANY_RULE; |
| 446 | private RuleClassNamePredicate allowedRuleClassesForLabelsWarning = NO_RULE; |
Ulf Adams | 788fd1a | 2015-03-12 13:54:09 +0000 | [diff] [blame] | 447 | private FileTypeSet allowedFileTypesForLabels; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 448 | private ValidityPredicate validityPredicate = ANY_EDGE; |
| 449 | private Object value; |
Klaus Aehlig | 04952fc | 2019-03-01 11:36:16 -0800 | [diff] [blame] | 450 | private String doc; |
Florian Weikert | 438ff16 | 2016-05-12 08:49:12 +0000 | [diff] [blame] | 451 | private AttributeValueSource valueSource = AttributeValueSource.DIRECT; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 452 | private boolean valueSet; |
| 453 | private Predicate<AttributeMap> condition; |
| 454 | private Set<PropertyFlag> propertyFlags = EnumSet.noneOf(PropertyFlag.class); |
| 455 | private PredicateWithMessage<Object> allowedValues = null; |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 456 | private RequiredProviders.Builder requiredProvidersBuilder = |
| 457 | RequiredProviders.acceptAnyBuilder(); |
Dmitry Lomov | f188dc2 | 2016-07-19 09:00:55 +0000 | [diff] [blame] | 458 | private HashMap<String, RuleAspect<?>> aspects = new LinkedHashMap<>(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 459 | |
| 460 | /** |
| 461 | * Creates an attribute builder with given name and type. This attribute is optional, uses |
| 462 | * target configuration and has a default value the same as its type default value. This |
| 463 | * attribute will be marked as undocumented if its name starts with the dollar sign ({@code $}) |
| 464 | * or colon ({@code :}). |
| 465 | * |
| 466 | * @param name attribute name |
| 467 | * @param type attribute type |
| 468 | */ |
| 469 | public Builder(String name, Type<TYPE> type) { |
| 470 | this.name = Preconditions.checkNotNull(name); |
| 471 | this.type = Preconditions.checkNotNull(type); |
| 472 | if (isImplicit(name) || isLateBound(name)) { |
| 473 | setPropertyFlag(PropertyFlag.UNDOCUMENTED, "undocumented"); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | private Builder<TYPE> setPropertyFlag(PropertyFlag flag, String propertyName) { |
Ulf Adams | 07dba94 | 2015-03-05 14:47:37 +0000 | [diff] [blame] | 478 | Preconditions.checkState( |
| 479 | !propertyFlags.contains(flag), "%s flag is already set", propertyName); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 480 | propertyFlags.add(flag); |
| 481 | return this; |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Sets the property flag of the corresponding name if exists, otherwise throws an Exception. |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 486 | * Only meant to use from Starlark, do not use from Java. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 487 | */ |
| 488 | public Builder<TYPE> setPropertyFlag(String propertyName) { |
| 489 | PropertyFlag flag = null; |
| 490 | try { |
| 491 | flag = PropertyFlag.valueOf(propertyName); |
| 492 | } catch (IllegalArgumentException e) { |
| 493 | throw new IllegalArgumentException("unknown attribute flag " + propertyName); |
| 494 | } |
| 495 | setPropertyFlag(flag, propertyName); |
| 496 | return this; |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Makes the built attribute mandatory. |
| 501 | */ |
| 502 | public Builder<TYPE> mandatory() { |
| 503 | return setPropertyFlag(PropertyFlag.MANDATORY, "mandatory"); |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Makes the built attribute non empty, meaning the attribute cannot have an empty list value. |
| 508 | * Only applicable for list type attributes. |
| 509 | */ |
| 510 | public Builder<TYPE> nonEmpty() { |
Ulf Adams | 07dba94 | 2015-03-05 14:47:37 +0000 | [diff] [blame] | 511 | Preconditions.checkNotNull(type.getListElementType(), "attribute '%s' must be a list", name); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 512 | return setPropertyFlag(PropertyFlag.NON_EMPTY, "non_empty"); |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Makes the built attribute producing a single artifact. |
| 517 | */ |
| 518 | public Builder<TYPE> singleArtifact() { |
Michael Staib | a751f92 | 2017-02-14 15:50:04 +0000 | [diff] [blame] | 519 | Preconditions.checkState(type.getLabelClass() == LabelClass.DEPENDENCY, |
Ulf Adams | 07dba94 | 2015-03-05 14:47:37 +0000 | [diff] [blame] | 520 | "attribute '%s' must be a label-valued type", name); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 521 | return setPropertyFlag(PropertyFlag.SINGLE_ARTIFACT, "single_artifact"); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Forces silent ruleclass filtering on the label type attribute. |
| 526 | * This flag is introduced to handle plugins, do not use it in other cases. |
| 527 | */ |
| 528 | public Builder<TYPE> silentRuleClassFilter() { |
Michael Staib | a751f92 | 2017-02-14 15:50:04 +0000 | [diff] [blame] | 529 | Preconditions.checkState(type.getLabelClass() == LabelClass.DEPENDENCY, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 530 | "must be a label-valued type"); |
| 531 | return setPropertyFlag(PropertyFlag.SILENT_RULECLASS_FILTER, "silent_ruleclass_filter"); |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Skip analysis time filetype check. Don't use it if avoidable. |
| 536 | */ |
| 537 | public Builder<TYPE> skipAnalysisTimeFileTypeCheck() { |
Michael Staib | a751f92 | 2017-02-14 15:50:04 +0000 | [diff] [blame] | 538 | Preconditions.checkState(type.getLabelClass() == LabelClass.DEPENDENCY, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 539 | "must be a label-valued type"); |
| 540 | return setPropertyFlag(PropertyFlag.SKIP_ANALYSIS_TIME_FILETYPE_CHECK, |
| 541 | "skip_analysis_time_filetype_check"); |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Mark the built attribute as order-independent. |
| 546 | */ |
| 547 | public Builder<TYPE> orderIndependent() { |
Ulf Adams | 07dba94 | 2015-03-05 14:47:37 +0000 | [diff] [blame] | 548 | Preconditions.checkNotNull(type.getListElementType(), "attribute '%s' must be a list", name); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 549 | return setPropertyFlag(PropertyFlag.ORDER_INDEPENDENT, "order-independent"); |
| 550 | } |
| 551 | |
| 552 | /** |
Irina Iancu | 2f23599 | 2017-01-10 16:03:29 +0000 | [diff] [blame] | 553 | * Mark the built attribute as to use output_licenses for license checking. |
| 554 | */ |
| 555 | public Builder<TYPE> useOutputLicenses() { |
| 556 | Preconditions.checkState(BuildType.isLabelType(type), "must be a label type"); |
| 557 | return setPropertyFlag(PropertyFlag.OUTPUT_LICENSES, "output_license"); |
| 558 | } |
| 559 | |
| 560 | /** |
cparsons | f626639 | 2018-10-18 15:42:52 -0700 | [diff] [blame] | 561 | * Indicate the attribute uses uses a starlark-defined (non-analysis-test) configuration |
| 562 | * transition. Transitions for analysis testing are tracked separately: see |
| 563 | * {@link #hasAnalysisTestTransition()}. |
Googler | c2200fd | 2018-09-14 17:35:59 -0700 | [diff] [blame] | 564 | */ |
cparsons | f626639 | 2018-10-18 15:42:52 -0700 | [diff] [blame] | 565 | public Builder<TYPE> hasStarlarkDefinedTransition() { |
| 566 | return setPropertyFlag(PropertyFlag.HAS_STARLARK_DEFINED_TRANSITION, |
| 567 | "starlark-defined split transition"); |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Indicate the attribute uses uses a starlark-defined analysis-test configuration transition. |
| 572 | * Such a configuration transition may only be applied on rules with {@code analysis_test=true}. |
| 573 | */ |
| 574 | public Builder<TYPE> hasAnalysisTestTransition() { |
| 575 | return setPropertyFlag(PropertyFlag.HAS_ANALYSIS_TEST_TRANSITION, |
| 576 | "analysis-test split transition"); |
Googler | c2200fd | 2018-09-14 17:35:59 -0700 | [diff] [blame] | 577 | } |
| 578 | |
John Cater | 0a9e1ed | 2019-03-27 11:02:01 -0700 | [diff] [blame] | 579 | /** Defines the configuration transition for this attribute. */ |
John Cater | 2c0dece | 2019-04-02 09:18:18 -0700 | [diff] [blame] | 580 | public Builder<TYPE> cfg(TransitionFactory<AttributeTransitionData> transitionFactory) { |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 581 | Preconditions.checkNotNull(transitionFactory); |
John Cater | 0a9e1ed | 2019-03-27 11:02:01 -0700 | [diff] [blame] | 582 | Preconditions.checkState( |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 583 | NoTransition.isInstance(this.transitionFactory), |
| 584 | "the configuration transition is already set"); |
| 585 | this.transitionFactory = transitionFactory; |
Chris Parsons | 2e62c0d | 2016-04-19 22:13:59 +0000 | [diff] [blame] | 586 | return this; |
| 587 | } |
| 588 | |
| 589 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 590 | * Requires the attribute target to be executable; only for label or label |
| 591 | * list attributes. Defaults to {@code false}. |
| 592 | */ |
| 593 | public Builder<TYPE> exec() { |
| 594 | return setPropertyFlag(PropertyFlag.EXECUTABLE, "executable"); |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Indicates that the attribute (like srcs or hdrs) should be used as an input when calculating |
| 599 | * compile_one_dependency. |
| 600 | */ |
| 601 | public Builder<TYPE> direct_compile_time_input() { |
| 602 | return setPropertyFlag(PropertyFlag.DIRECT_COMPILE_TIME_INPUT, |
| 603 | "direct_compile_time_input"); |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Makes the built attribute undocumented. |
| 608 | * |
| 609 | * @param reason explanation why the attribute is undocumented. This is not |
| 610 | * used but required for documentation |
| 611 | */ |
| 612 | public Builder<TYPE> undocumented(String reason) { |
| 613 | return setPropertyFlag(PropertyFlag.UNDOCUMENTED, "undocumented"); |
| 614 | } |
| 615 | |
| 616 | /** |
Klaus Aehlig | 04952fc | 2019-03-01 11:36:16 -0800 | [diff] [blame] | 617 | * Set the doc string for the attribute. |
| 618 | * |
| 619 | * @param doc The doc string for this attribute. |
| 620 | */ |
| 621 | public Builder<TYPE> setDoc(String doc) { |
| 622 | this.doc = doc; |
| 623 | return this; |
| 624 | } |
| 625 | |
| 626 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 627 | * Sets the attribute default value. The type of the default value must |
| 628 | * match the type parameter. (e.g. list=[], integer=0, string="", |
| 629 | * label=null). The {@code defaultValue} must be immutable. |
| 630 | * |
| 631 | * <p>If defaultValue is of type Label and is a target, that target will |
| 632 | * become an implicit dependency of the Rule; we will load the target |
| 633 | * (and its dependencies) if it encounters the Rule and build the target |
| 634 | * if needs to apply the Rule. |
| 635 | */ |
| 636 | public Builder<TYPE> value(TYPE defaultValue) { |
| 637 | Preconditions.checkState(!valueSet, "the default value is already set"); |
| 638 | value = defaultValue; |
| 639 | valueSet = true; |
| 640 | return this; |
| 641 | } |
| 642 | |
| 643 | /** |
gregce | ad752cc | 2020-04-10 10:32:59 -0700 | [diff] [blame] | 644 | * See value(TYPE) above. This method is only meant for Starlark usage. |
Florian Weikert | cb3d799 | 2016-09-06 14:54:22 +0000 | [diff] [blame] | 645 | * |
vladmos | 9c787fa | 2017-07-04 11:45:22 -0400 | [diff] [blame] | 646 | * <p>The parameter {@code context} is relevant iff the default value is a Label string. In this |
| 647 | * case, {@code context} must point to the parent Label in order to be able to convert the |
| 648 | * default value string to a proper Label. |
| 649 | * |
| 650 | * @param parameterName The name of the attribute to use in error messages |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 651 | */ |
vladmos | 9c787fa | 2017-07-04 11:45:22 -0400 | [diff] [blame] | 652 | public Builder<TYPE> defaultValue( |
| 653 | Object defaultValue, Object context, @Nullable String parameterName) |
Florian Weikert | cb3d799 | 2016-09-06 14:54:22 +0000 | [diff] [blame] | 654 | throws ConversionException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 655 | Preconditions.checkState(!valueSet, "the default value is already set"); |
vladmos | 9c787fa | 2017-07-04 11:45:22 -0400 | [diff] [blame] | 656 | value = |
| 657 | type.convert( |
| 658 | defaultValue, |
| 659 | ((parameterName == null) ? "" : String.format("parameter '%s' of ", parameterName)) |
| 660 | + String.format("attribute '%s'", name), |
| 661 | context); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 662 | valueSet = true; |
| 663 | return this; |
| 664 | } |
| 665 | |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 666 | /** See value(TYPE) above. This method is only meant for Starlark usage. */ |
Florian Weikert | cb3d799 | 2016-09-06 14:54:22 +0000 | [diff] [blame] | 667 | public Builder<TYPE> defaultValue(Object defaultValue) throws ConversionException { |
vladmos | 9c787fa | 2017-07-04 11:45:22 -0400 | [diff] [blame] | 668 | return defaultValue(defaultValue, null, null); |
Florian Weikert | cb3d799 | 2016-09-06 14:54:22 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 671 | /** |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 672 | * Sets the attribute default value to a computed default value - use this when the default |
| 673 | * value is a function of other attributes of the Rule. The type of the computed default value |
| 674 | * for a mandatory attribute must match the type parameter: (e.g. list=[], integer=0, string="", |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 675 | * label=null). The {@code defaultValue} implementation must be immutable. |
| 676 | * |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 677 | * <p>If the computed default returns a Label that is a target, that target will become an |
| 678 | * implicit dependency of this Rule; we will load the target (and its dependencies) if it |
| 679 | * encounters the Rule and build the target if needs to apply the Rule. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 680 | */ |
| 681 | public Builder<TYPE> value(ComputedDefault defaultValue) { |
| 682 | Preconditions.checkState(!valueSet, "the default value is already set"); |
| 683 | value = defaultValue; |
Florian Weikert | 438ff16 | 2016-05-12 08:49:12 +0000 | [diff] [blame] | 684 | valueSource = AttributeValueSource.COMPUTED_DEFAULT; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 685 | valueSet = true; |
| 686 | return this; |
| 687 | } |
| 688 | |
| 689 | /** |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 690 | * Sets the attribute default value to a Starlark computed default template. Like a native |
| 691 | * Computed Default, this allows a Starlark-defined Rule Class to specify that the default value |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 692 | * of an attribute is a function of other attributes of the Rule. |
| 693 | * |
| 694 | * <p>During the loading phase, the computed default template will be specialized for each rule |
| 695 | * it applies to. Those rules' attribute values will not be references to {@link |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 696 | * StarlarkComputedDefaultTemplate}s, but instead will be references to {@link |
| 697 | * StarlarkComputedDefault}s. |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 698 | * |
| 699 | * <p>If the computed default returns a Label that is a target, that target will become an |
| 700 | * implicit dependency of this Rule; we will load the target (and its dependencies) if it |
| 701 | * encounters the Rule and build the target if needs to apply the Rule. |
| 702 | */ |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 703 | public Builder<TYPE> value(StarlarkComputedDefaultTemplate starlarkComputedDefaultTemplate) { |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 704 | Preconditions.checkState(!valueSet, "the default value is already set"); |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 705 | value = starlarkComputedDefaultTemplate; |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 706 | valueSource = AttributeValueSource.COMPUTED_DEFAULT; |
| 707 | valueSet = true; |
| 708 | return this; |
| 709 | } |
| 710 | |
| 711 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 712 | * Sets the attribute default value to be late-bound, i.e., it is derived from the build |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 713 | * configuration and/or the rule's configured attributes. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 714 | */ |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 715 | public Builder<TYPE> value(LateBoundDefault<?, ? extends TYPE> defaultValue) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 716 | Preconditions.checkState(!valueSet, "the default value is already set"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 717 | value = defaultValue; |
Florian Weikert | 438ff16 | 2016-05-12 08:49:12 +0000 | [diff] [blame] | 718 | valueSource = AttributeValueSource.LATE_BOUND; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 719 | valueSet = true; |
| 720 | return this; |
| 721 | } |
| 722 | |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 723 | /** Returns where the value of this attribute comes from. Useful only for Starlark. */ |
Florian Weikert | 438ff16 | 2016-05-12 08:49:12 +0000 | [diff] [blame] | 724 | public AttributeValueSource getValueSource() { |
| 725 | return valueSource; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /** |
| 729 | * Sets a condition predicate. The default value of the attribute only applies if the condition |
| 730 | * evaluates to true. If the value is explicitly provided, then this condition is ignored. |
| 731 | * |
| 732 | * <p>The condition is only evaluated if the attribute is not explicitly set, and after all |
| 733 | * explicit attributes have been set. It can generally not access default values of other |
| 734 | * attributes. |
| 735 | */ |
| 736 | public Builder<TYPE> condition(Predicate<AttributeMap> condition) { |
| 737 | Preconditions.checkState(this.condition == null, "the condition is already set"); |
| 738 | this.condition = condition; |
| 739 | return this; |
| 740 | } |
| 741 | |
| 742 | /** |
| 743 | * Switches on the capability of an attribute to be published to the rule's |
| 744 | * tag set. |
| 745 | */ |
| 746 | public Builder<TYPE> taggable() { |
| 747 | return setPropertyFlag(PropertyFlag.TAGGABLE, "taggable"); |
| 748 | } |
| 749 | |
| 750 | /** |
jcater | 0de1097 | 2020-04-07 12:15:05 -0700 | [diff] [blame] | 751 | * Disables dependency checks done by {@link |
| 752 | * com.google.devtools.build.lib.analysis.RuleContext.PrerequisiteValidator}. |
Ulf Adams | 0b63897 | 2015-09-08 13:25:35 +0000 | [diff] [blame] | 753 | */ |
Greg Estren | 875c7a7 | 2015-09-24 19:57:54 +0000 | [diff] [blame] | 754 | public Builder<TYPE> skipPrereqValidatorCheck() { |
| 755 | return setPropertyFlag(PropertyFlag.SKIP_PREREQ_VALIDATOR_CHECKS, |
| 756 | "skip_prereq_validator_checks"); |
Ulf Adams | 0b63897 | 2015-09-08 13:25:35 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /** |
Greg Estren | 9d83784 | 2016-12-01 21:36:59 +0000 | [diff] [blame] | 760 | * Enforces constraint checking on this attribute even if default enforcement policy would skip |
| 761 | * it. If default policy checks the attribute, this is a no-op. |
| 762 | * |
| 763 | * <p>Most attributes are enforced by default, so in the common case this call is unnecessary. |
Greg Estren | 4a10251 | 2015-10-28 20:49:22 +0000 | [diff] [blame] | 764 | * |
jcater | 802551e | 2020-04-15 09:59:58 -0700 | [diff] [blame] | 765 | * <p>See {@link com.google.devtools.build.lib.analysis.constraints.ConstraintSemantics} for |
| 766 | * enforcement policy details. |
Greg Estren | 4a10251 | 2015-10-28 20:49:22 +0000 | [diff] [blame] | 767 | */ |
| 768 | public Builder<TYPE> checkConstraints() { |
Greg Estren | 9d83784 | 2016-12-01 21:36:59 +0000 | [diff] [blame] | 769 | Verify.verify(!propertyFlags.contains(PropertyFlag.SKIP_CONSTRAINTS_OVERRIDE), |
| 770 | "constraint checking is already overridden to be skipped"); |
| 771 | return setPropertyFlag(PropertyFlag.CHECK_CONSTRAINTS_OVERRIDE, "check_constraints"); |
| 772 | } |
| 773 | |
| 774 | /** |
| 775 | * Skips constraint checking on this attribute even if default enforcement policy would check |
| 776 | * it. If default policy skips the attribute, this is a no-op. |
| 777 | * |
jcater | 802551e | 2020-04-15 09:59:58 -0700 | [diff] [blame] | 778 | * <p>See {@link com.google.devtools.build.lib.analysis.constraints.ConstraintSemantics} for |
| 779 | * enforcement policy details. |
Greg Estren | 9d83784 | 2016-12-01 21:36:59 +0000 | [diff] [blame] | 780 | */ |
| 781 | public Builder<TYPE> dontCheckConstraints() { |
| 782 | Verify.verify(!propertyFlags.contains(PropertyFlag.CHECK_CONSTRAINTS_OVERRIDE), |
| 783 | "constraint checking is already overridden to be checked"); |
| 784 | return setPropertyFlag(PropertyFlag.SKIP_CONSTRAINTS_OVERRIDE, "dont_check_constraints"); |
Greg Estren | 4a10251 | 2015-10-28 20:49:22 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /** |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 788 | * If this is a label or label-list attribute, then this sets the allowed rule types for the |
| 789 | * labels occurring in the attribute. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 790 | * |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 791 | * <p>If the attribute contains Labels of any other rule type, then if they're in {@link |
| 792 | * #allowedRuleClassesForLabelsWarning}, the build continues with a warning. Else if they |
| 793 | * fulfill {@link #mandatoryNativeProvidersList}, the build continues without error. Else the |
| 794 | * build fails during analysis. |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 795 | * |
| 796 | * <p>If neither this nor {@link #allowedRuleClassesForLabelsWarning} is set, only rules that |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 797 | * fulfill {@link #mandatoryNativeProvidersList} build without error. |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 798 | * |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 799 | * <p>This only works on a per-target basis, not on a per-file basis; with other words, it works |
| 800 | * for 'deps' attributes, but not 'srcs' attributes. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 801 | */ |
| 802 | public Builder<TYPE> allowedRuleClasses(Iterable<String> allowedRuleClasses) { |
| 803 | return allowedRuleClasses( |
Googler | 72f3a10 | 2017-12-01 16:28:28 -0800 | [diff] [blame] | 804 | RuleClassNamePredicate.only(allowedRuleClasses)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | /** |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 808 | * If this is a label or label-list attribute, then this sets the allowed rule types for the |
| 809 | * labels occurring in the attribute. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 810 | * |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 811 | * <p>If the attribute contains Labels of any other rule type, then if they're in {@link |
| 812 | * #allowedRuleClassesForLabelsWarning}, the build continues with a warning. Else if they |
| 813 | * fulfill {@link #mandatoryNativeProvidersList}, the build continues without error. Else the |
| 814 | * build fails during analysis. |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 815 | * |
| 816 | * <p>If neither this nor {@link #allowedRuleClassesForLabelsWarning} is set, only rules that |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 817 | * fulfill {@link #mandatoryNativeProvidersList} build without error. |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 818 | * |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 819 | * <p>This only works on a per-target basis, not on a per-file basis; with other words, it works |
| 820 | * for 'deps' attributes, but not 'srcs' attributes. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 821 | */ |
Googler | 72f3a10 | 2017-12-01 16:28:28 -0800 | [diff] [blame] | 822 | public Builder<TYPE> allowedRuleClasses(RuleClassNamePredicate allowedRuleClasses) { |
Michael Staib | a751f92 | 2017-02-14 15:50:04 +0000 | [diff] [blame] | 823 | Preconditions.checkState(type.getLabelClass() == LabelClass.DEPENDENCY, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 824 | "must be a label-valued type"); |
| 825 | propertyFlags.add(PropertyFlag.STRICT_LABEL_CHECKING); |
| 826 | allowedRuleClassesForLabels = allowedRuleClasses; |
| 827 | return this; |
| 828 | } |
| 829 | |
| 830 | /** |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 831 | * If this is a label or label-list attribute, then this sets the allowed rule types for the |
| 832 | * labels occurring in the attribute. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 833 | * |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 834 | * <p>If the attribute contains Labels of any other rule type, then if they're in {@link |
| 835 | * #allowedRuleClassesForLabelsWarning}, the build continues with a warning. Else if they |
| 836 | * fulfill {@link #mandatoryNativeProvidersList}, the build continues without error. Else the |
| 837 | * build fails during analysis. |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 838 | * |
| 839 | * <p>If neither this nor {@link #allowedRuleClassesForLabelsWarning} is set, only rules that |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 840 | * fulfill {@link #mandatoryNativeProvidersList} build without error. |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 841 | * |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 842 | * <p>This only works on a per-target basis, not on a per-file basis; with other words, it works |
| 843 | * for 'deps' attributes, but not 'srcs' attributes. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 844 | */ |
| 845 | public Builder<TYPE> allowedRuleClasses(String... allowedRuleClasses) { |
| 846 | return allowedRuleClasses(ImmutableSet.copyOf(allowedRuleClasses)); |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * If this is a label or label-list attribute, then this sets the allowed |
| 851 | * file types for file labels occurring in the attribute. If the attribute |
| 852 | * contains labels that correspond to files of any other type, then an error |
| 853 | * is produced during the analysis phase. |
| 854 | * |
| 855 | * <p>This only works on a per-target basis, not on a per-file basis; with |
| 856 | * other words, it works for 'deps' attributes, but not 'srcs' attributes. |
| 857 | */ |
| 858 | public Builder<TYPE> allowedFileTypes(FileTypeSet allowedFileTypes) { |
Michael Staib | a751f92 | 2017-02-14 15:50:04 +0000 | [diff] [blame] | 859 | Preconditions.checkState(type.getLabelClass() == LabelClass.DEPENDENCY, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 860 | "must be a label-valued type"); |
| 861 | propertyFlags.add(PropertyFlag.STRICT_LABEL_CHECKING); |
Ulf Adams | 788fd1a | 2015-03-12 13:54:09 +0000 | [diff] [blame] | 862 | allowedFileTypesForLabels = Preconditions.checkNotNull(allowedFileTypes); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 863 | return this; |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * Allow all files for legacy compatibility. All uses of this method should be audited and then |
| 868 | * removed. In some cases, it's correct to allow any file, but mostly the set of files should be |
| 869 | * restricted to a reasonable set. |
| 870 | */ |
| 871 | public Builder<TYPE> legacyAllowAnyFileType() { |
| 872 | return allowedFileTypes(FileTypeSet.ANY_FILE); |
| 873 | } |
| 874 | |
| 875 | /** |
| 876 | * If this is a label or label-list attribute, then this sets the allowed |
| 877 | * file types for file labels occurring in the attribute. If the attribute |
| 878 | * contains labels that correspond to files of any other type, then an error |
| 879 | * is produced during the analysis phase. |
| 880 | * |
| 881 | * <p>This only works on a per-target basis, not on a per-file basis; with |
| 882 | * other words, it works for 'deps' attributes, but not 'srcs' attributes. |
| 883 | */ |
| 884 | public Builder<TYPE> allowedFileTypes(FileType... allowedFileTypes) { |
| 885 | return allowedFileTypes(FileTypeSet.of(allowedFileTypes)); |
| 886 | } |
| 887 | |
| 888 | /** |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 889 | * If this is a label or label-list attribute, then this sets the allowed rule types with |
| 890 | * warning for the labels occurring in the attribute. This must be a disjoint set from |
| 891 | * {@link #allowedRuleClasses}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 892 | * |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 893 | * <p>If the attribute contains Labels of any other rule type (other than this or those set in |
| 894 | * allowedRuleClasses()) and they fulfill {@link #getMandatoryNativeProvidersList()}}, the build |
| 895 | * continues without error. Else the build fails during analysis. |
| 896 | * |
| 897 | * <p>If neither this nor {@link #allowedRuleClassesForLabels} is set, only rules that |
| 898 | * fulfill {@link #getMandatoryNativeProvidersList()} build without error. |
| 899 | * |
| 900 | * <p>This only works on a per-target basis, not on a per-file basis; with other words, it |
| 901 | * works for 'deps' attributes, but not 'srcs' attributes. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 902 | */ |
| 903 | public Builder<TYPE> allowedRuleClassesWithWarning(Collection<String> allowedRuleClasses) { |
| 904 | return allowedRuleClassesWithWarning( |
Googler | 72f3a10 | 2017-12-01 16:28:28 -0800 | [diff] [blame] | 905 | RuleClassNamePredicate.only(allowedRuleClasses)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | /** |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 909 | * If this is a label or label-list attribute, then this sets the allowed rule types with |
| 910 | * warning for the labels occurring in the attribute. This must be a disjoint set from |
| 911 | * {@link #allowedRuleClasses}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 912 | * |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 913 | * <p>If the attribute contains Labels of any other rule type (other than this or those set in |
| 914 | * allowedRuleClasses()) and they fulfill {@link #getMandatoryNativeProvidersList()}}, the build |
| 915 | * continues without error. Else the build fails during analysis. |
| 916 | * |
| 917 | * <p>If neither this nor {@link #allowedRuleClassesForLabels} is set, only rules that |
| 918 | * fulfill {@link #getMandatoryNativeProvidersList()} build without error. |
| 919 | * |
| 920 | * <p>This only works on a per-target basis, not on a per-file basis; with other words, it |
| 921 | * works for 'deps' attributes, but not 'srcs' attributes. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 922 | */ |
Googler | 72f3a10 | 2017-12-01 16:28:28 -0800 | [diff] [blame] | 923 | public Builder<TYPE> allowedRuleClassesWithWarning(RuleClassNamePredicate allowedRuleClasses) { |
Michael Staib | a751f92 | 2017-02-14 15:50:04 +0000 | [diff] [blame] | 924 | Preconditions.checkState(type.getLabelClass() == LabelClass.DEPENDENCY, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 925 | "must be a label-valued type"); |
| 926 | propertyFlags.add(PropertyFlag.STRICT_LABEL_CHECKING); |
| 927 | allowedRuleClassesForLabelsWarning = allowedRuleClasses; |
| 928 | return this; |
| 929 | } |
| 930 | |
| 931 | /** |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 932 | * If this is a label or label-list attribute, then this sets the allowed rule types with |
dslomov | de965ac | 2017-07-31 21:07:51 +0200 | [diff] [blame] | 933 | * warning for the labels occurring in the attribute. This must be a disjoint set from {@link |
| 934 | * #allowedRuleClasses}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 935 | * |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 936 | * <p>If the attribute contains Labels of any other rule type (other than this or those set in |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 937 | * allowedRuleClasses()) and they fulfill {@link #getRequiredProviders()}}, the build continues |
| 938 | * without error. Else the build fails during analysis. |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 939 | * |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 940 | * <p>If neither this nor {@link #allowedRuleClassesForLabels} is set, only rules that fulfill |
| 941 | * {@link #getRequiredProviders()} build without error. |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 942 | * |
dslomov | de965ac | 2017-07-31 21:07:51 +0200 | [diff] [blame] | 943 | * <p>This only works on a per-target basis, not on a per-file basis; with other words, it works |
| 944 | * for 'deps' attributes, but not 'srcs' attributes. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 945 | */ |
| 946 | public Builder<TYPE> allowedRuleClassesWithWarning(String... allowedRuleClasses) { |
| 947 | return allowedRuleClassesWithWarning(ImmutableSet.copyOf(allowedRuleClasses)); |
| 948 | } |
| 949 | |
| 950 | /** |
Googler | 861ed2c | 2016-09-07 19:13:17 +0000 | [diff] [blame] | 951 | * Sets a list of lists of mandatory native providers. Every configured target occurring in this |
| 952 | * label type attribute has to provide all the providers from one of those lists, otherwise an |
| 953 | * error is produced during the analysis phase. |
Liam Miller-Cushon | bbae4d9 | 2016-05-03 18:23:03 +0000 | [diff] [blame] | 954 | */ |
Googler | 861ed2c | 2016-09-07 19:13:17 +0000 | [diff] [blame] | 955 | public final Builder<TYPE> mandatoryNativeProvidersList( |
| 956 | Iterable<? extends Iterable<Class<? extends TransitiveInfoProvider>>> providersList) { |
Michael Staib | a751f92 | 2017-02-14 15:50:04 +0000 | [diff] [blame] | 957 | Preconditions.checkState(type.getLabelClass() == LabelClass.DEPENDENCY, |
Liam Miller-Cushon | bbae4d9 | 2016-05-03 18:23:03 +0000 | [diff] [blame] | 958 | "must be a label-valued type"); |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 959 | |
Googler | 861ed2c | 2016-09-07 19:13:17 +0000 | [diff] [blame] | 960 | for (Iterable<Class<? extends TransitiveInfoProvider>> providers : providersList) { |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 961 | this.requiredProvidersBuilder.addNativeSet(ImmutableSet.copyOf(providers)); |
Googler | 861ed2c | 2016-09-07 19:13:17 +0000 | [diff] [blame] | 962 | } |
Googler | 861ed2c | 2016-09-07 19:13:17 +0000 | [diff] [blame] | 963 | return this; |
| 964 | } |
| 965 | |
| 966 | public Builder<TYPE> mandatoryNativeProviders( |
| 967 | Iterable<Class<? extends TransitiveInfoProvider>> providers) { |
| 968 | if (providers.iterator().hasNext()) { |
| 969 | mandatoryNativeProvidersList(ImmutableList.of(providers)); |
| 970 | } |
Liam Miller-Cushon | bbae4d9 | 2016-05-03 18:23:03 +0000 | [diff] [blame] | 971 | return this; |
| 972 | } |
| 973 | |
| 974 | /** |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 975 | * Sets a list of sets of mandatory Starlark providers. Every configured target occurring in |
| 976 | * this label type attribute has to provide all the providers from one of those sets, or be one |
| 977 | * of {@link #allowedRuleClasses}, otherwise an error is produced during the analysis phase. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 978 | */ |
Dmitry Lomov | fd2bdc3 | 2016-10-07 08:52:10 +0000 | [diff] [blame] | 979 | public Builder<TYPE> mandatoryProvidersList( |
gregce | 74d84d4 | 2020-04-17 10:02:03 -0700 | [diff] [blame] | 980 | Iterable<? extends Iterable<StarlarkProviderIdentifier>> providersList) { |
Michael Staib | a751f92 | 2017-02-14 15:50:04 +0000 | [diff] [blame] | 981 | Preconditions.checkState(type.getLabelClass() == LabelClass.DEPENDENCY, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 982 | "must be a label-valued type"); |
gregce | 74d84d4 | 2020-04-17 10:02:03 -0700 | [diff] [blame] | 983 | for (Iterable<StarlarkProviderIdentifier> providers : providersList) { |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 984 | this.requiredProvidersBuilder.addStarlarkSet(ImmutableSet.copyOf(providers)); |
Yun Peng | 5c34e96 | 2016-02-22 15:13:19 +0000 | [diff] [blame] | 985 | } |
Yun Peng | 5c34e96 | 2016-02-22 15:13:19 +0000 | [diff] [blame] | 986 | return this; |
| 987 | } |
| 988 | |
Dmitry Lomov | fd2bdc3 | 2016-10-07 08:52:10 +0000 | [diff] [blame] | 989 | public Builder<TYPE> legacyMandatoryProviders(String... ids) { |
| 990 | return mandatoryProviders( |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 991 | Iterables.transform( |
| 992 | Arrays.asList(ids), |
| 993 | s -> { |
| 994 | Preconditions.checkNotNull(s); |
gregce | 74d84d4 | 2020-04-17 10:02:03 -0700 | [diff] [blame] | 995 | return StarlarkProviderIdentifier.forLegacy(s); |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 996 | })); |
Dmitry Lomov | fd2bdc3 | 2016-10-07 08:52:10 +0000 | [diff] [blame] | 997 | } |
| 998 | |
gregce | 74d84d4 | 2020-04-17 10:02:03 -0700 | [diff] [blame] | 999 | public Builder<TYPE> mandatoryProviders(Iterable<StarlarkProviderIdentifier> providers) { |
Yun Peng | efd7ca1 | 2016-03-03 13:14:38 +0000 | [diff] [blame] | 1000 | if (providers.iterator().hasNext()) { |
| 1001 | mandatoryProvidersList(ImmutableList.of(providers)); |
| 1002 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1003 | return this; |
| 1004 | } |
| 1005 | |
gregce | 74d84d4 | 2020-04-17 10:02:03 -0700 | [diff] [blame] | 1006 | public Builder<TYPE> mandatoryProviders(StarlarkProviderIdentifier... providers) { |
dslomov | 77baa4c | 2017-07-10 17:15:27 +0200 | [diff] [blame] | 1007 | mandatoryProviders(Arrays.asList(providers)); |
| 1008 | return this; |
| 1009 | } |
| 1010 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1011 | /** |
Dmitry Lomov | 5a8f1c0 | 2015-11-26 10:49:16 +0000 | [diff] [blame] | 1012 | * Asserts that a particular parameterized aspect probably needs to be computed for all direct |
| 1013 | * dependencies through this attribute. |
| 1014 | * |
Googler | 459f130 | 2017-04-05 19:40:35 +0000 | [diff] [blame] | 1015 | * @param evaluator function that extracts aspect parameters from rule. If it returns null, |
| 1016 | * then the aspect will not be attached. |
Dmitry Lomov | 5a8f1c0 | 2015-11-26 10:49:16 +0000 | [diff] [blame] | 1017 | */ |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 1018 | public Builder<TYPE> aspect( |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 1019 | NativeAspectClass aspect, Function<Rule, AspectParameters> evaluator) { |
Dmitry Lomov | f188dc2 | 2016-07-19 09:00:55 +0000 | [diff] [blame] | 1020 | NativeRuleAspect nativeRuleAspect = new NativeRuleAspect(aspect, evaluator); |
| 1021 | RuleAspect<?> oldAspect = this.aspects.put(nativeRuleAspect.getName(), nativeRuleAspect); |
| 1022 | if (oldAspect != null) { |
| 1023 | throw new AssertionError( |
| 1024 | String.format("Aspect %s has already been added", oldAspect.getName())); |
| 1025 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1026 | return this; |
| 1027 | } |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 1028 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1029 | /** |
Dmitry Lomov | 5a8f1c0 | 2015-11-26 10:49:16 +0000 | [diff] [blame] | 1030 | * Asserts that a particular parameterized aspect probably needs to be computed for all direct |
| 1031 | * dependencies through this attribute. |
| 1032 | */ |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 1033 | public Builder<TYPE> aspect(NativeAspectClass aspect) { |
janakr | 123d784 | 2018-06-19 10:30:15 -0700 | [diff] [blame] | 1034 | return this.aspect(aspect, EMPTY_FUNCTION); |
Dmitry Lomov | 5a8f1c0 | 2015-11-26 10:49:16 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
janakr | 123d784 | 2018-06-19 10:30:15 -0700 | [diff] [blame] | 1037 | @AutoCodec @AutoCodec.VisibleForSerialization |
| 1038 | static final Function<Rule, AspectParameters> EMPTY_FUNCTION = input -> AspectParameters.EMPTY; |
| 1039 | |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 1040 | public Builder<TYPE> aspect(StarlarkDefinedAspect starlarkAspect) throws EvalException { |
| 1041 | StarlarkRuleAspect starlarkRuleAspect = new StarlarkRuleAspect(starlarkAspect); |
| 1042 | RuleAspect<?> oldAspect = this.aspects.put(starlarkAspect.getName(), starlarkRuleAspect); |
Dmitry Lomov | f188dc2 | 2016-07-19 09:00:55 +0000 | [diff] [blame] | 1043 | if (oldAspect != null) { |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 1044 | throw Starlark.errorf("aspect %s added more than once", starlarkAspect.getName()); |
Dmitry Lomov | f188dc2 | 2016-07-19 09:00:55 +0000 | [diff] [blame] | 1045 | } |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 1046 | return this; |
| 1047 | } |
| 1048 | |
Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 1049 | /** |
| 1050 | * Should only be used for deserialization. |
| 1051 | */ |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 1052 | public Builder<TYPE> aspect(final Aspect aspect) { |
Dmitry Lomov | f188dc2 | 2016-07-19 09:00:55 +0000 | [diff] [blame] | 1053 | PredefinedRuleAspect predefinedRuleAspect = new PredefinedRuleAspect(aspect); |
| 1054 | RuleAspect<?> oldAspect = |
| 1055 | this.aspects.put(predefinedRuleAspect.getName(), predefinedRuleAspect); |
| 1056 | if (oldAspect != null) { |
| 1057 | throw new AssertionError( |
| 1058 | String.format("Aspect %s has already been added", oldAspect.getName())); |
| 1059 | } |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 1060 | return this; |
| 1061 | } |
| 1062 | |
Dmitry Lomov | 5a8f1c0 | 2015-11-26 10:49:16 +0000 | [diff] [blame] | 1063 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1064 | * Sets the predicate-like edge validity checker. |
| 1065 | */ |
| 1066 | public Builder<TYPE> validityPredicate(ValidityPredicate validityPredicate) { |
| 1067 | propertyFlags.add(PropertyFlag.STRICT_LABEL_CHECKING); |
| 1068 | this.validityPredicate = validityPredicate; |
| 1069 | return this; |
| 1070 | } |
| 1071 | |
| 1072 | /** |
| 1073 | * The value of the attribute must be one of allowedValues. |
| 1074 | */ |
| 1075 | public Builder<TYPE> allowedValues(PredicateWithMessage<Object> allowedValues) { |
| 1076 | this.allowedValues = allowedValues; |
| 1077 | propertyFlags.add(PropertyFlag.CHECK_ALLOWED_VALUES); |
| 1078 | return this; |
| 1079 | } |
| 1080 | |
| 1081 | /** |
| 1082 | * Makes the built attribute "non-configurable", i.e. its value cannot be influenced by |
| 1083 | * the build configuration. Attributes are "configurable" unless explicitly opted out here. |
| 1084 | * |
| 1085 | * <p>Non-configurability indicates an exceptional state: there exists Blaze logic that needs |
| 1086 | * the attribute's value, has no access to configurations, and can't apply a workaround |
| 1087 | * through an appropriate {@link AbstractAttributeMapper} implementation. Scenarios like |
| 1088 | * this should be as uncommon as possible, so it's important we maintain clear documentation |
| 1089 | * on what causes them and why users consequently can't configure certain attributes. |
| 1090 | * |
| 1091 | * @param reason why this attribute can't be configurable. This isn't used by Blaze - it's |
| 1092 | * solely a documentation mechanism. |
| 1093 | */ |
| 1094 | public Builder<TYPE> nonconfigurable(String reason) { |
| 1095 | Preconditions.checkState(!reason.isEmpty()); |
| 1096 | return setPropertyFlag(PropertyFlag.NONCONFIGURABLE, "nonconfigurable"); |
| 1097 | } |
| 1098 | |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 1099 | /** Returns an {@link ImmutableAttributeFactory} that can be invoked to create attributes. */ |
| 1100 | public ImmutableAttributeFactory buildPartial() { |
Googler | 72f3a10 | 2017-12-01 16:28:28 -0800 | [diff] [blame] | 1101 | Preconditions.checkState( |
| 1102 | !allowedRuleClassesForLabels.consideredOverlapping(allowedRuleClassesForLabelsWarning), |
| 1103 | "allowedRuleClasses %s and allowedRuleClassesWithWarning %s " |
| 1104 | + "may not contain the same rule classes", |
| 1105 | allowedRuleClassesForLabels, |
| 1106 | allowedRuleClassesForLabelsWarning); |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 1107 | |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 1108 | return new ImmutableAttributeFactory( |
Yun Peng | 5c34e96 | 2016-02-22 15:13:19 +0000 | [diff] [blame] | 1109 | type, |
Klaus Aehlig | 04952fc | 2019-03-01 11:36:16 -0800 | [diff] [blame] | 1110 | doc, |
Yun Peng | 5c34e96 | 2016-02-22 15:13:19 +0000 | [diff] [blame] | 1111 | Sets.immutableEnumSet(propertyFlags), |
| 1112 | valueSet ? value : type.getDefaultValue(), |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 1113 | transitionFactory, |
Yun Peng | 5c34e96 | 2016-02-22 15:13:19 +0000 | [diff] [blame] | 1114 | allowedRuleClassesForLabels, |
| 1115 | allowedRuleClassesForLabelsWarning, |
| 1116 | allowedFileTypesForLabels, |
| 1117 | validityPredicate, |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 1118 | valueSource, |
| 1119 | valueSet, |
Yun Peng | 5c34e96 | 2016-02-22 15:13:19 +0000 | [diff] [blame] | 1120 | condition, |
| 1121 | allowedValues, |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 1122 | requiredProvidersBuilder.build(), |
Dmitry Lomov | f188dc2 | 2016-07-19 09:00:55 +0000 | [diff] [blame] | 1123 | ImmutableList.copyOf(aspects.values())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1124 | } |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 1125 | |
| 1126 | /** |
| 1127 | * Creates the attribute. Uses name, type, optionality, configuration type and the default value |
| 1128 | * configured by the builder. |
| 1129 | */ |
| 1130 | public Attribute build() { |
| 1131 | return build(this.name); |
| 1132 | } |
| 1133 | |
| 1134 | /** |
| 1135 | * Creates the attribute. Uses type, optionality, configuration type and the default value |
| 1136 | * configured by the builder. Use the name passed as an argument. This function is used by |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 1137 | * Starlark where the name is provided only when we build. We don't want to modify the builder, |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 1138 | * as it is shared in a multithreaded environment. |
| 1139 | */ |
| 1140 | public Attribute build(String name) { |
| 1141 | return buildPartial().build(name); |
| 1142 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | /** |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1146 | * A strategy for dealing with too many computations, used when creating lookup tables for {@link |
| 1147 | * ComputedDefault}s. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1148 | * |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1149 | * @param <TException> The type of exception this strategy throws if too many computations are |
| 1150 | * attempted. |
| 1151 | */ |
| 1152 | interface ComputationLimiter<TException extends Exception> { |
| 1153 | void onComputationCount(int count) throws TException; |
| 1154 | } |
| 1155 | |
| 1156 | /** |
| 1157 | * An implementation of {@link ComputationLimiter} that never throws. For use with |
| 1158 | * natively-defined {@link ComputedDefault}s, which are limited in the number of configurable |
| 1159 | * attributes they depend on, not on the number of different combinations of possible inputs. |
| 1160 | */ |
| 1161 | private static final ComputationLimiter<RuntimeException> NULL_COMPUTATION_LIMITER = |
| 1162 | new ComputationLimiter<RuntimeException>() { |
| 1163 | @Override |
| 1164 | public void onComputationCount(int count) throws RuntimeException {} |
| 1165 | }; |
| 1166 | |
| 1167 | /** Exception for computed default attributes that depend on too many configurable attributes. */ |
| 1168 | private static class TooManyConfigurableAttributesException extends Exception { |
| 1169 | TooManyConfigurableAttributesException(int max) { |
| 1170 | super( |
| 1171 | String.format( |
| 1172 | "Too many configurable attributes to compute all possible values: " |
| 1173 | + "Found more than %d possible values.", |
| 1174 | max)); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | private static class FixedComputationLimiter |
| 1179 | implements ComputationLimiter<TooManyConfigurableAttributesException> { |
| 1180 | |
| 1181 | /** Upper bound of the number of combinations of values for a computed default attribute. */ |
| 1182 | private static final int COMPUTED_DEFAULT_MAX_COMBINATIONS = 64; |
| 1183 | |
| 1184 | private static final FixedComputationLimiter INSTANCE = new FixedComputationLimiter(); |
| 1185 | |
| 1186 | @Override |
| 1187 | public void onComputationCount(int count) throws TooManyConfigurableAttributesException { |
| 1188 | if (count > COMPUTED_DEFAULT_MAX_COMBINATIONS) { |
| 1189 | throw new TooManyConfigurableAttributesException(COMPUTED_DEFAULT_MAX_COMBINATIONS); |
| 1190 | } |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | /** |
| 1195 | * Specifies how values of {@link ComputedDefault} attributes are computed based on the values of |
| 1196 | * other attributes. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1197 | * |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1198 | * <p>The {@code TComputeException} type parameter allows the two specializations of this class to |
| 1199 | * describe whether and how their computations throw. For natively defined computed defaults, |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 1200 | * computation does not throw, but for Starlark-defined computed defaults, computation may throw |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1201 | * {@link InterruptedException}. |
| 1202 | */ |
| 1203 | private abstract static class ComputationStrategy<TComputeException extends Exception> { |
| 1204 | abstract Object compute(AttributeMap map) throws TComputeException; |
| 1205 | |
| 1206 | /** |
| 1207 | * Returns a lookup table mapping from: |
| 1208 | * |
| 1209 | * <ul> |
| 1210 | * <li>tuples of values that may be assigned by {@code rule} to attributes with names in {@code |
| 1211 | * dependencies} (note that there may be more than one such tuple for any given rule, if any |
| 1212 | * of the dependencies are configurable) |
| 1213 | * </ul> |
| 1214 | * |
| 1215 | * <p>to: |
| 1216 | * |
| 1217 | * <ul> |
| 1218 | * <li>the value {@link #compute(AttributeMap)} evaluates to when the provided {@link |
| 1219 | * AttributeMap} contains the values specified by that assignment, or {@code null} if the |
| 1220 | * {@link ComputationStrategy} failed to evaluate. |
| 1221 | * </ul> |
| 1222 | * |
| 1223 | * <p>The lookup table contains a tuple for each possible assignment to the {@code dependencies} |
| 1224 | * attributes. The meaning of each tuple is well-defined because {@code dependencies} is |
| 1225 | * ordered. |
| 1226 | * |
| 1227 | * <p>This is useful because configurable attributes may have many possible values. During the |
| 1228 | * loading phase a configurable attribute can't be resolved to a single value. Configuration |
| 1229 | * information, needed to resolve such an attribute, is only available during analysis. However, |
| 1230 | * any labels that a ComputedDefault attribute may evaluate to must be loaded during the loading |
| 1231 | * phase. |
| 1232 | */ |
| 1233 | <T, TLimitException extends Exception> Map<List<Object>, T> computeValuesForAllCombinations( |
| 1234 | List<String> dependencies, |
| 1235 | Type<T> type, |
| 1236 | Rule rule, |
| 1237 | ComputationLimiter<TLimitException> limiter) |
| 1238 | throws TComputeException, TLimitException { |
djasper | 1b86912 | 2019-03-04 10:41:50 -0800 | [diff] [blame] | 1239 | AggregatingAttributeMapper mapper = AggregatingAttributeMapper.of(rule); |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1240 | // This will hold every (value1, value2, ..) combination of the declared dependencies. |
| 1241 | // Collect those combinations. |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1242 | List<Map<String, Object>> depMaps = mapper.visitAttributes(dependencies, limiter); |
| 1243 | // For each combination, call compute() on a specialized AttributeMap providing those |
| 1244 | // values. |
nharmata | b81776f | 2019-02-25 15:52:33 -0800 | [diff] [blame] | 1245 | Map<List<Object>, T> valueMap = Maps.newHashMapWithExpectedSize(depMaps.size()); |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1246 | for (Map<String, Object> depMap : depMaps) { |
| 1247 | AttributeMap attrMap = mapper.createMapBackedAttributeMap(depMap); |
| 1248 | Object value = compute(attrMap); |
| 1249 | List<Object> key = createDependencyAssignmentTuple(dependencies, attrMap); |
| 1250 | valueMap.put(key, type.cast(value)); |
| 1251 | } |
| 1252 | return valueMap; |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * Given an {@link AttributeMap}, containing an assignment to each attribute in {@code |
| 1257 | * dependencies}, this returns a list of the assigned values, ordered as {@code dependencies} is |
| 1258 | * ordered. |
| 1259 | */ |
| 1260 | static List<Object> createDependencyAssignmentTuple( |
| 1261 | List<String> dependencies, AttributeMap attrMap) { |
| 1262 | ArrayList<Object> tuple = new ArrayList<>(dependencies.size()); |
| 1263 | for (String attrName : dependencies) { |
| 1264 | Type<?> attrType = attrMap.getAttributeType(attrName); |
| 1265 | tuple.add(attrMap.get(attrName, attrType)); |
| 1266 | } |
| 1267 | return tuple; |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | /** |
| 1272 | * A computed default is a default value for a Rule attribute that is a function of other |
| 1273 | * attributes of the rule. |
| 1274 | * |
| 1275 | * <p>Attributes whose defaults are computed are first initialized to the default for their type, |
| 1276 | * and then the computed defaults are evaluated after all non-computed defaults have been |
| 1277 | * initialized. There is no defined order among computed defaults, so they must not depend on each |
| 1278 | * other. |
| 1279 | * |
| 1280 | * <p>If a computed default reads the value of another attribute, at least one of the following |
| 1281 | * must be true: |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1282 | * |
| 1283 | * <ol> |
Googler | 084b64b | 2019-11-19 14:41:30 -0800 | [diff] [blame] | 1284 | * <li>The other attribute must be declared in the computed default's constructor |
| 1285 | * <li>The other attribute must be non-configurable ({@link Builder#nonconfigurable} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1286 | * </ol> |
| 1287 | * |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1288 | * <p>The reason for enforced declarations is that, since attribute values might be configurable, |
| 1289 | * a computed default that depends on them may itself take multiple values. Since we have no |
| 1290 | * access to a target's configuration at the time these values are computed, we need the ability |
| 1291 | * to probe the default's *complete* dependency space. Declared dependencies allow us to do so |
| 1292 | * sanely. Non-configurable attributes don't have this problem because their value is fixed and |
| 1293 | * known even without configuration information. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1294 | * |
| 1295 | * <p>Implementations of this interface must be immutable. |
| 1296 | */ |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 1297 | public abstract static class ComputedDefault implements StarlarkValue { |
Mark Schaller | 93c7da6 | 2016-07-26 16:59:59 +0000 | [diff] [blame] | 1298 | private final ImmutableList<String> dependencies; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1299 | |
| 1300 | /** |
| 1301 | * Create a computed default that can read all non-configurable attribute values and no |
| 1302 | * configurable attribute values. |
| 1303 | */ |
| 1304 | public ComputedDefault() { |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1305 | this(ImmutableList.<String>of()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | /** |
| 1309 | * Create a computed default that can read all non-configurable attributes values and one |
| 1310 | * explicitly specified configurable attribute value |
| 1311 | */ |
| 1312 | public ComputedDefault(String depAttribute) { |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1313 | this(ImmutableList.of(depAttribute)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | /** |
| 1317 | * Create a computed default that can read all non-configurable attributes values and two |
| 1318 | * explicitly specified configurable attribute values. |
| 1319 | */ |
| 1320 | public ComputedDefault(String depAttribute1, String depAttribute2) { |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1321 | this(ImmutableList.of(depAttribute1, depAttribute2)); |
| 1322 | } |
| 1323 | |
| 1324 | /** |
| 1325 | * Creates a computed default that can read all non-configurable attributes and some explicitly |
| 1326 | * specified configurable attribute values. |
| 1327 | * |
| 1328 | * <p>This constructor should not be used by native {@link ComputedDefault} functions. The limit |
| 1329 | * of at-most-two depended-on configurable attributes is intended, to limit the exponential |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 1330 | * growth of possible values. {@link StarlarkComputedDefault} uses this, but is limited by |
| 1331 | * {@link FixedComputationLimiter#COMPUTED_DEFAULT_MAX_COMBINATIONS}. |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1332 | */ |
| 1333 | protected ComputedDefault(ImmutableList<String> dependencies) { |
| 1334 | // Order is important for #createDependencyAssignmentTuple. |
| 1335 | this.dependencies = Ordering.natural().immutableSortedCopy(dependencies); |
| 1336 | } |
| 1337 | |
| 1338 | <T> Iterable<T> getPossibleValues(Type<T> type, Rule rule) { |
| 1339 | final ComputedDefault owner = ComputedDefault.this; |
djasper | 1b86912 | 2019-03-04 10:41:50 -0800 | [diff] [blame] | 1340 | if (dependencies.isEmpty()) { |
| 1341 | AggregatingAttributeMapper mapper = AggregatingAttributeMapper.of(rule); |
| 1342 | Object value = owner.getDefault(mapper.createMapBackedAttributeMap(ImmutableMap.of())); |
| 1343 | return Lists.newArrayList(type.cast(value)); |
| 1344 | } |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1345 | ComputationStrategy<RuntimeException> strategy = |
| 1346 | new ComputationStrategy<RuntimeException>() { |
| 1347 | @Override |
| 1348 | public Object compute(AttributeMap map) { |
| 1349 | return owner.getDefault(map); |
| 1350 | } |
| 1351 | }; |
| 1352 | // Note that this uses ArrayList instead of something like ImmutableList because some |
| 1353 | // values may be null. |
| 1354 | return new ArrayList<>( |
| 1355 | strategy |
| 1356 | .computeValuesForAllCombinations(dependencies, type, rule, NULL_COMPUTATION_LIMITER) |
| 1357 | .values()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1358 | } |
| 1359 | |
Mark Schaller | 93c7da6 | 2016-07-26 16:59:59 +0000 | [diff] [blame] | 1360 | /** The list of configurable attributes this ComputedDefault declares it may read. */ |
| 1361 | public ImmutableList<String> dependencies() { |
| 1362 | return dependencies; |
| 1363 | } |
| 1364 | |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1365 | /** |
| 1366 | * Returns the value this {@link ComputedDefault} evaluates to, given the inputs contained in |
| 1367 | * {@code rule}. |
| 1368 | */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1369 | public abstract Object getDefault(AttributeMap rule); |
| 1370 | } |
| 1371 | |
| 1372 | /** |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 1373 | * A Starlark-defined computed default, which can be precomputed for a specific {@link Rule} by |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 1374 | * calling {@link #computePossibleValues}, which returns a {@link StarlarkComputedDefault} that |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1375 | * contains a lookup table. |
| 1376 | */ |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 1377 | public static final class StarlarkComputedDefaultTemplate { |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1378 | private final Type<?> type; |
Googler | d78a091 | 2019-09-04 15:13:10 -0700 | [diff] [blame] | 1379 | private final StarlarkCallbackHelper callback; |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1380 | private final ImmutableList<String> dependencies; |
| 1381 | |
| 1382 | /** |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 1383 | * Creates a new StarlarkComputedDefaultTemplate that allows the computation of attribute values |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1384 | * via a callback function during loading phase. |
| 1385 | * |
| 1386 | * @param type The type of the value of this attribute. |
| 1387 | * @param dependencies A list of all names of other attributes that are accessed by this |
| 1388 | * attribute. |
| 1389 | * @param callback A function to compute the actual attribute value. |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1390 | */ |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 1391 | public StarlarkComputedDefaultTemplate( |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 1392 | Type<?> type, ImmutableList<String> dependencies, StarlarkCallbackHelper callback) { |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1393 | this.type = Preconditions.checkNotNull(type); |
| 1394 | // Order is important for #createDependencyAssignmentTuple. |
| 1395 | this.dependencies = |
| 1396 | Ordering.natural().immutableSortedCopy(Preconditions.checkNotNull(dependencies)); |
| 1397 | this.callback = Preconditions.checkNotNull(callback); |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | /** |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 1401 | * Returns a {@link StarlarkComputedDefault} containing a lookup table specifying the output of |
| 1402 | * this {@link StarlarkComputedDefaultTemplate}'s callback given each possible assignment {@code |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1403 | * rule} might make to the attributes specified by {@link #dependencies}. |
| 1404 | * |
| 1405 | * <p>If the rule is missing an attribute specified by {@link #dependencies}, or if there are |
| 1406 | * too many possible assignments, or if any evaluation fails, this throws {@link |
| 1407 | * CannotPrecomputeDefaultsException}. |
| 1408 | * |
| 1409 | * <p>May only be called after all non-{@link ComputedDefault} attributes have been set on the |
| 1410 | * {@code rule}. |
| 1411 | */ |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 1412 | StarlarkComputedDefault computePossibleValues( |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1413 | Attribute attr, final Rule rule, final EventHandler eventHandler) |
| 1414 | throws InterruptedException, CannotPrecomputeDefaultsException { |
| 1415 | |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 1416 | final StarlarkComputedDefaultTemplate owner = StarlarkComputedDefaultTemplate.this; |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1417 | final String msg = |
| 1418 | String.format( |
| 1419 | "Cannot compute default value of attribute '%s' in rule '%s': ", |
Florian Weikert | ba40525 | 2017-01-30 14:42:50 +0000 | [diff] [blame] | 1420 | attr.getPublicName(), rule.getLabel()); |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1421 | final AtomicReference<EvalException> caughtEvalExceptionIfAny = new AtomicReference<>(); |
| 1422 | ComputationStrategy<InterruptedException> strategy = |
| 1423 | new ComputationStrategy<InterruptedException>() { |
| 1424 | @Override |
| 1425 | public Object compute(AttributeMap map) throws InterruptedException { |
| 1426 | try { |
vladmos | 076977e | 2017-12-02 14:15:32 -0800 | [diff] [blame] | 1427 | return owner.computeValue(eventHandler, map); |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1428 | } catch (EvalException ex) { |
| 1429 | caughtEvalExceptionIfAny.compareAndSet(null, ex); |
| 1430 | return null; |
| 1431 | } |
| 1432 | } |
| 1433 | }; |
| 1434 | |
| 1435 | ImmutableList.Builder<Type<?>> dependencyTypesBuilder = ImmutableList.builder(); |
| 1436 | Map<List<Object>, Object> lookupTable = new HashMap<>(); |
| 1437 | try { |
| 1438 | for (String dependency : dependencies) { |
| 1439 | Attribute attribute = rule.getRuleClassObject().getAttributeByNameMaybe(dependency); |
| 1440 | if (attribute == null) { |
| 1441 | throw new AttributeNotFoundException( |
| 1442 | String.format("No such attribute %s in rule %s", dependency, rule.getLabel())); |
| 1443 | } |
| 1444 | dependencyTypesBuilder.add(attribute.getType()); |
| 1445 | } |
| 1446 | lookupTable.putAll( |
| 1447 | strategy.computeValuesForAllCombinations( |
| 1448 | dependencies, attr.getType(), rule, FixedComputationLimiter.INSTANCE)); |
| 1449 | if (caughtEvalExceptionIfAny.get() != null) { |
| 1450 | throw caughtEvalExceptionIfAny.get(); |
| 1451 | } |
| 1452 | } catch (AttributeNotFoundException |
| 1453 | | TooManyConfigurableAttributesException |
| 1454 | | EvalException ex) { |
| 1455 | String error = msg + ex.getMessage(); |
| 1456 | rule.reportError(error, eventHandler); |
| 1457 | throw new CannotPrecomputeDefaultsException(error); |
| 1458 | } |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 1459 | return new StarlarkComputedDefault(dependencies, dependencyTypesBuilder.build(), lookupTable); |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
vladmos | 076977e | 2017-12-02 14:15:32 -0800 | [diff] [blame] | 1462 | private Object computeValue(EventHandler eventHandler, AttributeMap rule) |
| 1463 | throws EvalException, InterruptedException { |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1464 | Map<String, Object> attrValues = new HashMap<>(); |
| 1465 | for (String attrName : rule.getAttributeNames()) { |
| 1466 | Attribute attr = rule.getAttributeDefinition(attrName); |
| 1467 | if (!attr.hasComputedDefault()) { |
| 1468 | Object value = rule.get(attrName, attr.getType()); |
adonovan | 032a25b | 2020-08-26 07:04:30 -0700 | [diff] [blame] | 1469 | if (!Starlark.isNullOrNone(value)) { |
adonovan | 553dc6c | 2019-12-10 11:22:48 -0800 | [diff] [blame] | 1470 | // Some attribute values are not valid Starlark values: |
| 1471 | // visibility is an ImmutableList, for example. |
| 1472 | attrValues.put(attr.getName(), Starlark.fromJava(value, /*mutability=*/ null)); |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1473 | } |
| 1474 | } |
| 1475 | } |
vladmos | 076977e | 2017-12-02 14:15:32 -0800 | [diff] [blame] | 1476 | return invokeCallback(eventHandler, attrValues); |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
vladmos | 076977e | 2017-12-02 14:15:32 -0800 | [diff] [blame] | 1479 | private Object invokeCallback(EventHandler eventHandler, Map<String, Object> attrValues) |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1480 | throws EvalException, InterruptedException { |
adonovan | 267bdaa | 2020-11-04 11:32:24 -0800 | [diff] [blame] | 1481 | Structure attrs = |
cparsons | 0c5c1c6 | 2018-05-24 10:37:03 -0700 | [diff] [blame] | 1482 | StructProvider.STRUCT.create( |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1483 | attrValues, "No such regular (non computed) attribute '%s'."); |
vladmos | 076977e | 2017-12-02 14:15:32 -0800 | [diff] [blame] | 1484 | Object result = callback.call(eventHandler, attrs); |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1485 | try { |
Googler | 641bdf7 | 2019-11-12 10:32:26 -0800 | [diff] [blame] | 1486 | return type.cast((result == Starlark.NONE) ? type.getDefaultValue() : result); |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1487 | } catch (ClassCastException ex) { |
adonovan | 8bee707 | 2020-04-29 11:59:53 -0700 | [diff] [blame] | 1488 | throw Starlark.errorf("expected '%s', but got '%s'", type, Starlark.type(result)); |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | private static class AttributeNotFoundException extends Exception { |
| 1493 | private AttributeNotFoundException(String message) { |
| 1494 | super(message); |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | static class CannotPrecomputeDefaultsException extends Exception { |
| 1499 | private CannotPrecomputeDefaultsException(String message) { |
| 1500 | super(message); |
| 1501 | } |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | /** |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 1506 | * A class for computed attributes defined in Starlark. |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1507 | * |
| 1508 | * <p>Unlike {@link ComputedDefault}, instances of this class contain a pre-computed table of all |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 1509 | * possible assignments of depended-on attributes and what the Starlark function evaluates to, and |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1510 | * {@link #getPossibleValues(Type, Rule)} and {@link #getDefault(AttributeMap)} do lookups in that |
| 1511 | * table. |
| 1512 | */ |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 1513 | static final class StarlarkComputedDefault extends ComputedDefault { |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1514 | |
| 1515 | private final List<Type<?>> dependencyTypes; |
| 1516 | private final Map<List<Object>, Object> lookupTable; |
| 1517 | |
| 1518 | /** |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 1519 | * Creates a new StarlarkComputedDefault containing a lookup table. |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1520 | * |
mjhalupka | 7b398f9 | 2018-03-08 12:08:25 -0800 | [diff] [blame] | 1521 | * @param dependencies A list of all names of other attributes that are accessed by this |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1522 | * attribute. |
| 1523 | * @param dependencyTypes A list of requiredAttributes' types. |
| 1524 | * @param lookupTable An exhaustive mapping from requiredAttributes assignments to values this |
| 1525 | * computed default evaluates to. |
| 1526 | */ |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 1527 | StarlarkComputedDefault( |
mjhalupka | 7b398f9 | 2018-03-08 12:08:25 -0800 | [diff] [blame] | 1528 | ImmutableList<String> dependencies, |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1529 | ImmutableList<Type<?>> dependencyTypes, |
| 1530 | Map<List<Object>, Object> lookupTable) { |
mjhalupka | 7b398f9 | 2018-03-08 12:08:25 -0800 | [diff] [blame] | 1531 | super(Preconditions.checkNotNull(dependencies)); |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1532 | this.dependencyTypes = Preconditions.checkNotNull(dependencyTypes); |
| 1533 | this.lookupTable = Preconditions.checkNotNull(lookupTable); |
| 1534 | } |
| 1535 | |
| 1536 | List<Type<?>> getDependencyTypes() { |
| 1537 | return dependencyTypes; |
| 1538 | } |
| 1539 | |
| 1540 | Map<List<Object>, Object> getLookupTable() { |
| 1541 | return lookupTable; |
| 1542 | } |
| 1543 | |
| 1544 | @Override |
| 1545 | public Object getDefault(AttributeMap rule) { |
| 1546 | List<Object> key = ComputationStrategy.createDependencyAssignmentTuple(dependencies(), rule); |
| 1547 | Preconditions.checkState( |
| 1548 | lookupTable.containsKey(key), |
| 1549 | "Error in rule '%s': precomputed value missing for dependencies: %s. Available keys: %s.", |
| 1550 | rule.getLabel(), |
| 1551 | Iterables.toString(key), |
| 1552 | Iterables.toString(lookupTable.keySet())); |
| 1553 | return lookupTable.get(key); |
| 1554 | } |
| 1555 | |
| 1556 | @Override |
| 1557 | <T> Iterable<T> getPossibleValues(Type<T> type, Rule rule) { |
| 1558 | List<T> result = new ArrayList<>(lookupTable.size()); |
| 1559 | for (Object obj : lookupTable.values()) { |
| 1560 | result.add(type.cast(obj)); |
| 1561 | } |
| 1562 | return result; |
| 1563 | } |
| 1564 | } |
| 1565 | |
janakr | 8688b68 | 2018-03-26 09:07:11 -0700 | [diff] [blame] | 1566 | static class SimpleLateBoundDefault<FragmentT, ValueT> |
cparsons | d8d9172 | 2017-11-03 19:10:05 +0100 | [diff] [blame] | 1567 | extends LateBoundDefault<FragmentT, ValueT> { |
janakr | f4523e7 | 2019-06-05 14:11:06 -0700 | [diff] [blame] | 1568 | private final Resolver<FragmentT, ValueT> resolver; |
cparsons | d8d9172 | 2017-11-03 19:10:05 +0100 | [diff] [blame] | 1569 | |
| 1570 | private SimpleLateBoundDefault(boolean useHostConfiguration, |
| 1571 | Class<FragmentT> fragmentClass, |
| 1572 | ValueT defaultValue, Resolver<FragmentT, ValueT> resolver) { |
| 1573 | super(useHostConfiguration, fragmentClass, defaultValue); |
| 1574 | |
| 1575 | this.resolver = resolver; |
| 1576 | } |
| 1577 | |
| 1578 | @Override |
| 1579 | public ValueT resolve(Rule rule, AttributeMap attributes, FragmentT input) { |
| 1580 | return resolver.resolve(rule, attributes, input); |
| 1581 | } |
| 1582 | } |
| 1583 | |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 1584 | // TODO(b/65746853): Remove documentation about accepting BuildConfiguration when uses are cleaned |
| 1585 | // up. |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1586 | /** |
janakr | a56a6ad | 2018-02-02 15:52:22 -0800 | [diff] [blame] | 1587 | * Provider of values for late-bound attributes. See {@link Attribute#value(LateBoundDefault<?, ? |
| 1588 | * extends TYPE> value)}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1589 | * |
| 1590 | * <p>Use sparingly - having different values for attributes during loading and analysis can |
| 1591 | * confuse users. |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 1592 | * |
| 1593 | * @param <FragmentT> The type of value that is used to compute this value. This is usually a |
| 1594 | * subclass of BuildConfiguration.Fragment. It may also be Void to receive null, or |
| 1595 | * BuildConfiguration itself to receive the entire configuration. |
janakr | a56a6ad | 2018-02-02 15:52:22 -0800 | [diff] [blame] | 1596 | * @param <ValueT> The type of value returned by this class. Must be either {@link Void}, a {@link |
| 1597 | * Label}, or a {@link List} of {@link Label} objects. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1598 | */ |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 1599 | @Immutable |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 1600 | public abstract static class LateBoundDefault<FragmentT, ValueT> implements StarlarkValue { |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 1601 | /** |
| 1602 | * Functional interface for computing the value of a late-bound attribute. |
| 1603 | * |
| 1604 | * <p>Implementations of this interface must be immutable. |
| 1605 | */ |
| 1606 | @FunctionalInterface |
| 1607 | public interface Resolver<FragmentT, ValueT> { |
| 1608 | ValueT resolve(Rule rule, AttributeMap attributeMap, FragmentT input); |
| 1609 | } |
| 1610 | |
| 1611 | private final boolean useHostConfiguration; |
| 1612 | private final ValueT defaultValue; |
| 1613 | private final Class<FragmentT> fragmentClass; |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 1614 | |
| 1615 | /** |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 1616 | * Creates a new LateBoundDefault which always returns the given value. |
| 1617 | * |
| 1618 | * <p>This is used primarily for matching names with late-bound attributes on other rules and |
| 1619 | * for testing. Use normal default values if the name does not matter. |
| 1620 | */ |
janakr | a56a6ad | 2018-02-02 15:52:22 -0800 | [diff] [blame] | 1621 | @VisibleForTesting |
| 1622 | public static LabelLateBoundDefault<Void> fromConstantForTesting(Label defaultValue) { |
| 1623 | return new LabelLateBoundDefault<Void>( |
| 1624 | false, |
| 1625 | Void.class, |
| 1626 | Preconditions.checkNotNull(defaultValue), |
| 1627 | (rule, attributes, unused) -> defaultValue) {}; |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | /** |
| 1631 | * Creates a new LateBoundDefault which always returns null. |
| 1632 | * |
| 1633 | * <p>This is used primarily for matching names with late-bound attributes on other rules and |
| 1634 | * for testing. Use normal default values if the name does not matter. |
| 1635 | */ |
| 1636 | @SuppressWarnings("unchecked") // bivariant implementation |
| 1637 | public static <ValueT> LateBoundDefault<Void, ValueT> alwaysNull() { |
janakr | a56a6ad | 2018-02-02 15:52:22 -0800 | [diff] [blame] | 1638 | return (LateBoundDefault<Void, ValueT>) AlwaysNullLateBoundDefault.INSTANCE; |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 1639 | } |
| 1640 | |
juliexxia | 40fc5e8 | 2018-12-17 15:40:53 -0800 | [diff] [blame] | 1641 | LateBoundDefault( |
| 1642 | boolean useHostConfiguration, Class<FragmentT> fragmentClass, ValueT defaultValue) { |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 1643 | this.useHostConfiguration = useHostConfiguration; |
| 1644 | this.defaultValue = defaultValue; |
| 1645 | this.fragmentClass = fragmentClass; |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 1646 | } |
| 1647 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1648 | /** |
Greg Estren | 974bbd9 | 2016-07-27 14:22:41 +0000 | [diff] [blame] | 1649 | * Whether to look up the label in the host configuration. This is only here for host |
| 1650 | * compilation tools - we usually need to look up labels in the target configuration. |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 1651 | */ |
cparsons | d8d9172 | 2017-11-03 19:10:05 +0100 | [diff] [blame] | 1652 | public final boolean useHostConfiguration() { |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 1653 | return useHostConfiguration; |
| 1654 | } |
| 1655 | |
| 1656 | /** |
| 1657 | * Returns the input type that the attribute expects. This is almost always a configuration |
| 1658 | * fragment to be retrieved from the target's configuration (or the host configuration). |
Greg Estren | 974bbd9 | 2016-07-27 14:22:41 +0000 | [diff] [blame] | 1659 | * |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 1660 | * <p>It may also be {@link Void} to receive null. This is rarely necessary, but can be used, |
| 1661 | * e.g., if the attribute is named to match an attribute in another rule which is late-bound. |
| 1662 | * |
| 1663 | * <p>It may also be BuildConfiguration to receive the entire configuration. This is deprecated, |
| 1664 | * and only necessary when the default is computed from methods of BuildConfiguration itself. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1665 | */ |
cparsons | d8d9172 | 2017-11-03 19:10:05 +0100 | [diff] [blame] | 1666 | public final Class<FragmentT> getFragmentClass() { |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 1667 | return fragmentClass; |
| 1668 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1669 | |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 1670 | /** The default value for the attribute that is set during the loading phase. */ |
cparsons | d8d9172 | 2017-11-03 19:10:05 +0100 | [diff] [blame] | 1671 | public final ValueT getDefault() { |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 1672 | return defaultValue; |
| 1673 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1674 | |
| 1675 | /** |
| 1676 | * The actual value for the attribute for the analysis phase, which depends on the build |
| 1677 | * configuration. Note that configurations transitions are applied after the late-bound |
| 1678 | * attribute was evaluated. |
Greg Estren | 33ec191 | 2016-02-10 15:57:58 +0000 | [diff] [blame] | 1679 | * |
| 1680 | * @param rule the rule being evaluated |
| 1681 | * @param attributes interface for retrieving the values of the rule's other attributes |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 1682 | * @param input the configuration fragment to evaluate with |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1683 | */ |
cparsons | d8d9172 | 2017-11-03 19:10:05 +0100 | [diff] [blame] | 1684 | public abstract ValueT resolve(Rule rule, AttributeMap attributes, FragmentT input); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1685 | } |
| 1686 | |
janakr | a56a6ad | 2018-02-02 15:52:22 -0800 | [diff] [blame] | 1687 | /** |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 1688 | * An abstract {@link LateBoundDefault} class so that {@code StarlarkLateBoundDefault} can derive |
janakr | a56a6ad | 2018-02-02 15:52:22 -0800 | [diff] [blame] | 1689 | * from {@link LateBoundDefault} without compromising the type-safety of the second generic |
| 1690 | * parameter to {@link LateBoundDefault}. |
| 1691 | */ |
| 1692 | public abstract static class AbstractLabelLateBoundDefault<FragmentT> |
| 1693 | extends LateBoundDefault<FragmentT, Label> { |
| 1694 | protected AbstractLabelLateBoundDefault( |
| 1695 | boolean useHostConfiguration, Class<FragmentT> fragmentClass, Label defaultValue) { |
| 1696 | super(useHostConfiguration, fragmentClass, defaultValue); |
| 1697 | } |
| 1698 | } |
| 1699 | |
janakr | f4523e7 | 2019-06-05 14:11:06 -0700 | [diff] [blame] | 1700 | @AutoCodec.VisibleForSerialization |
| 1701 | static class AlwaysNullLateBoundDefault extends SimpleLateBoundDefault<Void, Void> { |
| 1702 | @AutoCodec @AutoCodec.VisibleForSerialization |
janakr | a56a6ad | 2018-02-02 15:52:22 -0800 | [diff] [blame] | 1703 | static final AlwaysNullLateBoundDefault INSTANCE = new AlwaysNullLateBoundDefault(); |
| 1704 | |
| 1705 | private AlwaysNullLateBoundDefault() { |
| 1706 | super(false, Void.class, null, (rule, attributes, unused) -> null); |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | /** A {@link LateBoundDefault} for a {@link Label}. */ |
| 1711 | public static class LabelLateBoundDefault<FragmentT> |
| 1712 | extends SimpleLateBoundDefault<FragmentT, Label> { |
janakr | f4523e7 | 2019-06-05 14:11:06 -0700 | [diff] [blame] | 1713 | @VisibleForTesting |
juliexxia | 40fc5e8 | 2018-12-17 15:40:53 -0800 | [diff] [blame] | 1714 | protected LabelLateBoundDefault( |
janakr | a56a6ad | 2018-02-02 15:52:22 -0800 | [diff] [blame] | 1715 | boolean useHostConfiguration, |
| 1716 | Class<FragmentT> fragmentClass, |
| 1717 | Label defaultValue, |
| 1718 | Resolver<FragmentT, Label> resolver) { |
| 1719 | super(useHostConfiguration, fragmentClass, defaultValue, resolver); |
| 1720 | } |
| 1721 | |
| 1722 | /** |
| 1723 | * Creates a new LabelLateBoundDefault which uses the rule, its configured attributes, and a |
| 1724 | * fragment of the target configuration. |
| 1725 | * |
| 1726 | * <p>Note that the configuration fragment here does not take into account any transitions that |
| 1727 | * are on the attribute with this LabelLateBoundDefault as its value. The configuration will be |
| 1728 | * the same as the configuration given to the target bearing the attribute. |
| 1729 | * |
| 1730 | * <p>Nearly all LateBoundDefaults should use this constructor or {@link |
| 1731 | * LabelListLateBoundDefault#fromTargetConfiguration}. There are few situations where it isn't |
| 1732 | * the appropriate option. |
| 1733 | * |
| 1734 | * <p>If you want a late-bound dependency which is configured in the host configuration, just |
| 1735 | * use this method with {@link com.google.devtools.build.lib.analysis.config.HostTransition}. If |
| 1736 | * you also need to decide the label of the dependency with information gained from the host |
| 1737 | * configuration - and it's very unlikely that you do - you can use {@link |
| 1738 | * LabelLateBoundDefault#fromHostConfiguration} as well. |
| 1739 | * |
| 1740 | * <p>If you want to decide an attribute's value based on the value of its other attributes, use |
| 1741 | * a subclass of {@link ComputedDefault}. The only time you should need {@link |
| 1742 | * LabelListLateBoundDefault#fromRuleAndAttributesOnly} is if you need access to three or more |
| 1743 | * configurable attributes, or if you need to match names with a late-bound attribute on another |
| 1744 | * rule. |
| 1745 | * |
| 1746 | * <p>If you have a constant-valued attribute, but you need it to have the same name as an |
| 1747 | * attribute on another rule which is late-bound, use {@link #alwaysNull}. |
| 1748 | * |
| 1749 | * @param fragmentClass The fragment to receive from the target configuration. May also be |
| 1750 | * BuildConfiguration.class to receive the entire configuration (deprecated) - in this case, |
| 1751 | * you must only use methods of BuildConfiguration itself, and not use any fragments. |
| 1752 | * @param defaultValue The default {@link Label} to return at loading time, when the |
| 1753 | * configuration is not available. |
| 1754 | * @param resolver A function which will compute the actual value with the configuration. |
| 1755 | */ |
| 1756 | public static <FragmentT> LabelLateBoundDefault<FragmentT> fromTargetConfiguration( |
| 1757 | Class<FragmentT> fragmentClass, Label defaultValue, Resolver<FragmentT, Label> resolver) { |
| 1758 | Preconditions.checkArgument( |
| 1759 | !fragmentClass.equals(Void.class), |
| 1760 | "Use fromRuleAndAttributesOnly to specify a LateBoundDefault which does not use " |
| 1761 | + "configuration."); |
| 1762 | return new LabelLateBoundDefault<>(false, fragmentClass, defaultValue, resolver); |
| 1763 | } |
| 1764 | |
| 1765 | /** |
| 1766 | * Creates a new LateBoundDefault which uses the rule, its configured attributes, and a fragment |
| 1767 | * of the host configuration. |
| 1768 | * |
| 1769 | * <p>This should only be necessary in very specialized cases. In almost all cases, you don't |
| 1770 | * need this method, just {@link #fromTargetConfiguration} and {@link |
| 1771 | * com.google.devtools.build.lib.analysis.config.HostTransition}. |
| 1772 | * |
| 1773 | * <p>This method only affects the configuration fragment passed to {@link #resolve}. You must |
| 1774 | * also use {@link com.google.devtools.build.lib.analysis.config.HostTransition}, so that the |
| 1775 | * dependency will be analyzed in the host configuration. |
| 1776 | * |
| 1777 | * @param fragmentClass The fragment to receive from the host configuration. May also be |
| 1778 | * BuildConfiguration.class to receive the entire configuration (deprecated) - in this case, |
| 1779 | * you must only use methods of BuildConfiguration itself, and not use any fragments. It is |
| 1780 | * very rare that a LateBoundDefault should need a host configuration fragment; use {@link |
| 1781 | * #fromTargetConfiguration} in most cases. |
| 1782 | * @param defaultValue The default {@link Label} to return at loading time, when the |
| 1783 | * configuration is not available. |
| 1784 | * @param resolver A function which will compute the actual value with the configuration. |
| 1785 | */ |
| 1786 | public static <FragmentT> LabelLateBoundDefault<FragmentT> fromHostConfiguration( |
| 1787 | Class<FragmentT> fragmentClass, Label defaultValue, Resolver<FragmentT, Label> resolver) { |
| 1788 | Preconditions.checkArgument( |
| 1789 | !fragmentClass.equals(Void.class), |
| 1790 | "Use fromRuleAndAttributesOnly to specify a LateBoundDefault which does not use " |
| 1791 | + "configuration."); |
| 1792 | return new LabelLateBoundDefault<>(true, fragmentClass, defaultValue, resolver); |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | /** A {@link LateBoundDefault} for a {@link List} of {@link Label} objects. */ |
| 1797 | public static class LabelListLateBoundDefault<FragmentT> |
| 1798 | extends SimpleLateBoundDefault<FragmentT, List<Label>> { |
janakr | f4523e7 | 2019-06-05 14:11:06 -0700 | [diff] [blame] | 1799 | private LabelListLateBoundDefault( |
janakr | a56a6ad | 2018-02-02 15:52:22 -0800 | [diff] [blame] | 1800 | boolean useHostConfiguration, |
| 1801 | Class<FragmentT> fragmentClass, |
| 1802 | Resolver<FragmentT, List<Label>> resolver) { |
| 1803 | super(useHostConfiguration, fragmentClass, ImmutableList.of(), resolver); |
| 1804 | } |
| 1805 | |
| 1806 | public static <FragmentT> LabelListLateBoundDefault<FragmentT> fromTargetConfiguration( |
| 1807 | Class<FragmentT> fragmentClass, Resolver<FragmentT, List<Label>> resolver) { |
| 1808 | Preconditions.checkArgument( |
| 1809 | !fragmentClass.equals(Void.class), |
| 1810 | "Use fromRuleAndAttributesOnly to specify a LateBoundDefault which does not use " |
| 1811 | + "configuration."); |
| 1812 | return new LabelListLateBoundDefault<>(false, fragmentClass, resolver); |
| 1813 | } |
| 1814 | |
| 1815 | /** |
| 1816 | * Creates a new LabelListLateBoundDefault which uses only the rule and its configured |
| 1817 | * attributes. |
| 1818 | * |
| 1819 | * <p>This should only be necessary in very specialized cases. In almost all cases, you don't |
| 1820 | * need this method, just use {@link ComputedDefault}. |
| 1821 | * |
| 1822 | * <p>This is used primarily for computing values based on three or more configurable attributes |
| 1823 | * and/or matching names with late-bound attributes on other rules. |
| 1824 | * |
| 1825 | * @param resolver A function which will compute the actual value with the configuration. |
| 1826 | */ |
| 1827 | public static LabelListLateBoundDefault<Void> fromRuleAndAttributesOnly( |
| 1828 | Resolver<Void, List<Label>> resolver) { |
| 1829 | return new LabelListLateBoundDefault<>(false, Void.class, resolver); |
| 1830 | } |
| 1831 | } |
| 1832 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1833 | private final String name; |
| 1834 | |
Klaus Aehlig | 04952fc | 2019-03-01 11:36:16 -0800 | [diff] [blame] | 1835 | private final String doc; |
| 1836 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1837 | private final Type<?> type; |
| 1838 | |
| 1839 | private final Set<PropertyFlag> propertyFlags; |
| 1840 | |
| 1841 | // Exactly one of these conditions is true: |
| 1842 | // 1. defaultValue == null. |
| 1843 | // 2. defaultValue instanceof ComputedDefault && |
| 1844 | // type.isValid(defaultValue.getDefault()) |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 1845 | // 3. defaultValue instanceof StarlarkComputedDefaultTemplate && |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 1846 | // type.isValid(defaultValue.computePossibleValues().getDefault()) |
| 1847 | // 4. type.isValid(defaultValue). |
| 1848 | // 5. defaultValue instanceof LateBoundDefault && |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1849 | // type.isValid(defaultValue.getDefault(configuration)) |
| 1850 | // (We assume a hypothetical Type.isValid(Object) predicate.) |
| 1851 | private final Object defaultValue; |
| 1852 | |
John Cater | 2c0dece | 2019-04-02 09:18:18 -0700 | [diff] [blame] | 1853 | private final TransitionFactory<AttributeTransitionData> transitionFactory; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1854 | |
| 1855 | /** |
| 1856 | * For label or label-list attributes, this predicate returns which rule |
| 1857 | * classes are allowed for the targets in the attribute. |
| 1858 | */ |
Googler | 72f3a10 | 2017-12-01 16:28:28 -0800 | [diff] [blame] | 1859 | private final RuleClassNamePredicate allowedRuleClassesForLabels; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1860 | |
| 1861 | /** |
| 1862 | * For label or label-list attributes, this predicate returns which rule |
| 1863 | * classes are allowed for the targets in the attribute with warning. |
| 1864 | */ |
Googler | 72f3a10 | 2017-12-01 16:28:28 -0800 | [diff] [blame] | 1865 | private final RuleClassNamePredicate allowedRuleClassesForLabelsWarning; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1866 | |
| 1867 | /** |
| 1868 | * For label or label-list attributes, this predicate returns which file |
| 1869 | * types are allowed for targets in the attribute that happen to be file |
| 1870 | * targets (rather than rules). |
| 1871 | */ |
| 1872 | private final FileTypeSet allowedFileTypesForLabels; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1873 | |
| 1874 | /** |
| 1875 | * This predicate-like object checks |
| 1876 | * if the edge between two rules using this attribute is valid |
| 1877 | * in the dependency graph. Returns null if valid, otherwise an error message. |
| 1878 | */ |
| 1879 | private final ValidityPredicate validityPredicate; |
| 1880 | |
| 1881 | private final Predicate<AttributeMap> condition; |
| 1882 | |
| 1883 | private final PredicateWithMessage<Object> allowedValues; |
| 1884 | |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 1885 | private final RequiredProviders requiredProviders; |
Liam Miller-Cushon | bbae4d9 | 2016-05-03 18:23:03 +0000 | [diff] [blame] | 1886 | |
Dmitry Lomov | f188dc2 | 2016-07-19 09:00:55 +0000 | [diff] [blame] | 1887 | private final ImmutableList<RuleAspect<?>> aspects; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1888 | |
nharmata | 977c880 | 2018-03-21 12:18:21 -0700 | [diff] [blame] | 1889 | private final int hashCode; |
| 1890 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1891 | /** |
dslomov | de965ac | 2017-07-31 21:07:51 +0200 | [diff] [blame] | 1892 | * Constructs a rule attribute with the specified name, type and default value. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1893 | * |
| 1894 | * @param name the name of the attribute |
| 1895 | * @param type the type of the attribute |
dslomov | de965ac | 2017-07-31 21:07:51 +0200 | [diff] [blame] | 1896 | * @param defaultValue the default value to use for this attribute if none is specified in rule |
| 1897 | * declaration in the BUILD file. Must be null, or of type "type". May be an instance of |
| 1898 | * ComputedDefault, in which case its getDefault() method must return an instance of "type", |
| 1899 | * or null. Must be immutable. |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 1900 | * @param transitionFactory the configuration transition for this attribute (which must be of type |
dslomov | de965ac | 2017-07-31 21:07:51 +0200 | [diff] [blame] | 1901 | * LABEL, LABEL_LIST, NODEP_LABEL or NODEP_LABEL_LIST). |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1902 | */ |
mjhalupka | cfa0bb7 | 2018-03-12 12:43:15 -0700 | [diff] [blame] | 1903 | Attribute( |
Yun Peng | 5c34e96 | 2016-02-22 15:13:19 +0000 | [diff] [blame] | 1904 | String name, |
Klaus Aehlig | 04952fc | 2019-03-01 11:36:16 -0800 | [diff] [blame] | 1905 | String doc, |
Yun Peng | 5c34e96 | 2016-02-22 15:13:19 +0000 | [diff] [blame] | 1906 | Type<?> type, |
| 1907 | Set<PropertyFlag> propertyFlags, |
| 1908 | Object defaultValue, |
John Cater | 2c0dece | 2019-04-02 09:18:18 -0700 | [diff] [blame] | 1909 | TransitionFactory<AttributeTransitionData> transitionFactory, |
Googler | 72f3a10 | 2017-12-01 16:28:28 -0800 | [diff] [blame] | 1910 | RuleClassNamePredicate allowedRuleClassesForLabels, |
| 1911 | RuleClassNamePredicate allowedRuleClassesForLabelsWarning, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1912 | FileTypeSet allowedFileTypesForLabels, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1913 | ValidityPredicate validityPredicate, |
| 1914 | Predicate<AttributeMap> condition, |
| 1915 | PredicateWithMessage<Object> allowedValues, |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 1916 | RequiredProviders requiredProviders, |
Dmitry Lomov | f188dc2 | 2016-07-19 09:00:55 +0000 | [diff] [blame] | 1917 | ImmutableList<RuleAspect<?>> aspects) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1918 | Preconditions.checkArgument( |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 1919 | (NoTransition.isInstance(transitionFactory)) |
| 1920 | || type.getLabelClass() == LabelClass.DEPENDENCY |
| 1921 | || type.getLabelClass() == LabelClass.NONDEP_REFERENCE, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1922 | "Configuration transitions can only be specified for label or label list attributes"); |
Ulf Adams | 07dba94 | 2015-03-05 14:47:37 +0000 | [diff] [blame] | 1923 | Preconditions.checkArgument( |
| 1924 | isLateBound(name) == (defaultValue instanceof LateBoundDefault), |
| 1925 | "late bound attributes require a default value that is late bound (and vice versa): %s", |
| 1926 | name); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1927 | if (isLateBound(name)) { |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 1928 | LateBoundDefault<?, ?> lateBoundDefault = (LateBoundDefault<?, ?>) defaultValue; |
jcater | 0eee8c5 | 2019-03-25 10:42:33 -0700 | [diff] [blame] | 1929 | Preconditions.checkArgument( |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 1930 | !lateBoundDefault.useHostConfiguration() || transitionFactory.isHost(), |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1931 | "a late bound default value using the host configuration must use the host transition"); |
| 1932 | } |
| 1933 | |
| 1934 | this.name = name; |
Klaus Aehlig | 04952fc | 2019-03-01 11:36:16 -0800 | [diff] [blame] | 1935 | this.doc = doc; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1936 | this.type = type; |
| 1937 | this.propertyFlags = propertyFlags; |
| 1938 | this.defaultValue = defaultValue; |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 1939 | this.transitionFactory = transitionFactory; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1940 | this.allowedRuleClassesForLabels = allowedRuleClassesForLabels; |
| 1941 | this.allowedRuleClassesForLabelsWarning = allowedRuleClassesForLabelsWarning; |
| 1942 | this.allowedFileTypesForLabels = allowedFileTypesForLabels; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1943 | this.validityPredicate = validityPredicate; |
| 1944 | this.condition = condition; |
| 1945 | this.allowedValues = allowedValues; |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 1946 | this.requiredProviders = requiredProviders; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1947 | this.aspects = aspects; |
Klaus Aehlig | 04952fc | 2019-03-01 11:36:16 -0800 | [diff] [blame] | 1948 | this.hashCode = |
| 1949 | Objects.hash( |
| 1950 | name, |
| 1951 | doc, |
| 1952 | type, |
| 1953 | propertyFlags, |
| 1954 | defaultValue, |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 1955 | transitionFactory, |
Klaus Aehlig | 04952fc | 2019-03-01 11:36:16 -0800 | [diff] [blame] | 1956 | allowedRuleClassesForLabels, |
| 1957 | allowedRuleClassesForLabelsWarning, |
| 1958 | allowedFileTypesForLabels, |
| 1959 | validityPredicate, |
| 1960 | condition, |
| 1961 | allowedValues, |
| 1962 | requiredProviders, |
| 1963 | aspects); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | /** |
| 1967 | * Returns the name of this attribute. |
| 1968 | */ |
| 1969 | public String getName() { |
| 1970 | return name; |
| 1971 | } |
| 1972 | |
Klaus Aehlig | 04952fc | 2019-03-01 11:36:16 -0800 | [diff] [blame] | 1973 | /** Returns the doc string for that attribute, if any. */ |
| 1974 | public String getDoc() { |
| 1975 | return doc; |
| 1976 | } |
| 1977 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1978 | /** |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 1979 | * Returns the public name of this attribute. This is the name we use in Starlark code and we can |
| 1980 | * use it to display to the end-user. Implicit and late-bound attributes start with '_' (instead |
| 1981 | * of '$' or ':'). |
Laurent Le Brun | 7d90353 | 2015-08-20 20:04:41 +0000 | [diff] [blame] | 1982 | */ |
| 1983 | public String getPublicName() { |
gregce | d7c1cef | 2020-05-12 07:51:48 -0700 | [diff] [blame] | 1984 | return getStarlarkName(getName()); |
Laurent Le Brun | 7d90353 | 2015-08-20 20:04:41 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1988 | * Returns the logical type of this attribute. (May differ from the actual |
| 1989 | * representation as a value in the build interpreter; for example, an |
| 1990 | * attribute may logically be a list of labels, but be represented as a list |
| 1991 | * of strings.) |
| 1992 | */ |
| 1993 | public Type<?> getType() { |
| 1994 | return type; |
| 1995 | } |
| 1996 | |
| 1997 | private boolean getPropertyFlag(PropertyFlag flag) { |
| 1998 | return propertyFlags.contains(flag); |
| 1999 | } |
| 2000 | |
| 2001 | /** |
| 2002 | * Returns true if this parameter is mandatory. |
| 2003 | */ |
| 2004 | public boolean isMandatory() { |
| 2005 | return getPropertyFlag(PropertyFlag.MANDATORY); |
| 2006 | } |
| 2007 | |
| 2008 | /** |
| 2009 | * Returns true if this list parameter cannot have an empty list as a value. |
| 2010 | */ |
| 2011 | public boolean isNonEmpty() { |
| 2012 | return getPropertyFlag(PropertyFlag.NON_EMPTY); |
| 2013 | } |
| 2014 | |
| 2015 | /** |
| 2016 | * Returns true if this label parameter must produce a single artifact. |
| 2017 | */ |
| 2018 | public boolean isSingleArtifact() { |
| 2019 | return getPropertyFlag(PropertyFlag.SINGLE_ARTIFACT); |
| 2020 | } |
| 2021 | |
| 2022 | /** |
| 2023 | * Returns true if this label type parameter is checked by silent ruleclass filtering. |
| 2024 | */ |
| 2025 | public boolean isSilentRuleClassFilter() { |
| 2026 | return getPropertyFlag(PropertyFlag.SILENT_RULECLASS_FILTER); |
| 2027 | } |
| 2028 | |
| 2029 | /** |
| 2030 | * Returns true if this label type parameter skips the analysis time filetype check. |
| 2031 | */ |
| 2032 | public boolean isSkipAnalysisTimeFileTypeCheck() { |
| 2033 | return getPropertyFlag(PropertyFlag.SKIP_ANALYSIS_TIME_FILETYPE_CHECK); |
| 2034 | } |
| 2035 | |
| 2036 | /** |
| 2037 | * Returns true if this parameter is order-independent. |
| 2038 | */ |
| 2039 | public boolean isOrderIndependent() { |
| 2040 | return getPropertyFlag(PropertyFlag.ORDER_INDEPENDENT); |
| 2041 | } |
| 2042 | |
| 2043 | /** |
Irina Iancu | 2f23599 | 2017-01-10 16:03:29 +0000 | [diff] [blame] | 2044 | * Returns true if output_licenses should be used for checking licensing. |
| 2045 | */ |
| 2046 | public boolean useOutputLicenses() { |
| 2047 | return getPropertyFlag(PropertyFlag.OUTPUT_LICENSES); |
| 2048 | } |
| 2049 | |
| 2050 | /** |
cparsons | f626639 | 2018-10-18 15:42:52 -0700 | [diff] [blame] | 2051 | * Returns true if this attribute uses a starlark-defined, non analysis-test configuration |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 2052 | * transition. Starlark-defined analysis-test configuration transitions are handled separately. |
| 2053 | * See {@link #hasAnalysisTestTransition}. |
Googler | c2200fd | 2018-09-14 17:35:59 -0700 | [diff] [blame] | 2054 | */ |
cparsons | f626639 | 2018-10-18 15:42:52 -0700 | [diff] [blame] | 2055 | public boolean hasStarlarkDefinedTransition() { |
| 2056 | return getPropertyFlag(PropertyFlag.HAS_STARLARK_DEFINED_TRANSITION); |
| 2057 | } |
| 2058 | |
| 2059 | /** |
| 2060 | * Returns true if this attributes uses Starlark-defined configuration transition designed |
| 2061 | * specifically for rules which run analysis tests. |
| 2062 | */ |
| 2063 | public boolean hasAnalysisTestTransition() { |
| 2064 | return getPropertyFlag(PropertyFlag.HAS_ANALYSIS_TEST_TRANSITION); |
Googler | c2200fd | 2018-09-14 17:35:59 -0700 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | /** |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 2068 | * Returns the configuration transition factory for this attribute for label or label list |
| 2069 | * attributes. For other attributes it will always return {@code NONE}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2070 | */ |
John Cater | 2c0dece | 2019-04-02 09:18:18 -0700 | [diff] [blame] | 2071 | public TransitionFactory<AttributeTransitionData> getTransitionFactory() { |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 2072 | return transitionFactory; |
juliexxia | cf4d89e | 2019-01-15 12:16:56 -0800 | [diff] [blame] | 2073 | } |
| 2074 | |
Chris Parsons | 2e62c0d | 2016-04-19 22:13:59 +0000 | [diff] [blame] | 2075 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2076 | * Returns whether the target is required to be executable for label or label |
| 2077 | * list attributes. For other attributes it always returns {@code false}. |
| 2078 | */ |
| 2079 | public boolean isExecutable() { |
| 2080 | return getPropertyFlag(PropertyFlag.EXECUTABLE); |
| 2081 | } |
| 2082 | |
| 2083 | /** |
| 2084 | * Returns {@code true} iff the rule is a direct input for an action. |
| 2085 | */ |
| 2086 | public boolean isDirectCompileTimeInput() { |
| 2087 | return getPropertyFlag(PropertyFlag.DIRECT_COMPILE_TIME_INPUT); |
| 2088 | } |
| 2089 | |
| 2090 | /** |
| 2091 | * Returns {@code true} iff this attribute requires documentation. |
| 2092 | */ |
| 2093 | public boolean isDocumented() { |
| 2094 | return !getPropertyFlag(PropertyFlag.UNDOCUMENTED); |
| 2095 | } |
| 2096 | |
| 2097 | /** |
| 2098 | * Returns {@code true} iff this attribute should be published to the rule's |
| 2099 | * tag set. Note that not all Type classes support tag conversion. |
| 2100 | */ |
| 2101 | public boolean isTaggable() { |
| 2102 | return getPropertyFlag(PropertyFlag.TAGGABLE); |
| 2103 | } |
| 2104 | |
| 2105 | public boolean isStrictLabelCheckingEnabled() { |
| 2106 | return getPropertyFlag(PropertyFlag.STRICT_LABEL_CHECKING); |
| 2107 | } |
| 2108 | |
| 2109 | /** |
| 2110 | * Returns true if the value of this attribute should be a part of a given set. |
| 2111 | */ |
| 2112 | public boolean checkAllowedValues() { |
| 2113 | return getPropertyFlag(PropertyFlag.CHECK_ALLOWED_VALUES); |
| 2114 | } |
| 2115 | |
Greg Estren | 875c7a7 | 2015-09-24 19:57:54 +0000 | [diff] [blame] | 2116 | public boolean performPrereqValidatorCheck() { |
| 2117 | return !getPropertyFlag(PropertyFlag.SKIP_PREREQ_VALIDATOR_CHECKS); |
Ulf Adams | 0b63897 | 2015-09-08 13:25:35 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Greg Estren | 4a10251 | 2015-10-28 20:49:22 +0000 | [diff] [blame] | 2120 | public boolean checkConstraintsOverride() { |
Greg Estren | 9d83784 | 2016-12-01 21:36:59 +0000 | [diff] [blame] | 2121 | return getPropertyFlag(PropertyFlag.CHECK_CONSTRAINTS_OVERRIDE); |
| 2122 | } |
| 2123 | |
| 2124 | public boolean skipConstraintsOverride() { |
| 2125 | return getPropertyFlag(PropertyFlag.SKIP_CONSTRAINTS_OVERRIDE); |
Greg Estren | 4a10251 | 2015-10-28 20:49:22 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2128 | /** |
| 2129 | * Returns true if this attribute's value can be influenced by the build configuration. |
| 2130 | */ |
| 2131 | public boolean isConfigurable() { |
Michael Staib | a751f92 | 2017-02-14 15:50:04 +0000 | [diff] [blame] | 2132 | // Output types are excluded because of Rule#populateExplicitOutputFiles. |
| 2133 | return !(type.getLabelClass() == LabelClass.OUTPUT |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2134 | || getPropertyFlag(PropertyFlag.NONCONFIGURABLE)); |
| 2135 | } |
| 2136 | |
| 2137 | /** |
Googler | 72f3a10 | 2017-12-01 16:28:28 -0800 | [diff] [blame] | 2138 | * Returns a predicate that evaluates to true for rule classes that are allowed labels in this |
| 2139 | * attribute. If this is not a label or label-list attribute, the returned predicate always |
| 2140 | * evaluates to true. |
| 2141 | * |
| 2142 | * <p>NOTE: This may return Predicates.<RuleClass>alwaysTrue() as a sentinel meaning "do the right |
| 2143 | * thing", rather than actually allowing all rule classes in that attribute. Others parts of bazel |
| 2144 | * code check for that specific instance. |
| 2145 | */ |
| 2146 | public Predicate<RuleClass> getAllowedRuleClassesPredicate() { |
| 2147 | return allowedRuleClassesForLabels.asPredicateOfRuleClass(); |
| 2148 | } |
| 2149 | |
| 2150 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2151 | * Returns a predicate that evaluates to true for rule classes that are |
| 2152 | * allowed labels in this attribute with warning. If this is not a label or label-list |
| 2153 | * attribute, the returned predicate always evaluates to true. |
| 2154 | */ |
| 2155 | public Predicate<RuleClass> getAllowedRuleClassesWarningPredicate() { |
Googler | 72f3a10 | 2017-12-01 16:28:28 -0800 | [diff] [blame] | 2156 | return allowedRuleClassesForLabelsWarning.asPredicateOfRuleClass(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2157 | } |
| 2158 | |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 2159 | public RequiredProviders getRequiredProviders() { |
| 2160 | return requiredProviders; |
Liam Miller-Cushon | bbae4d9 | 2016-05-03 18:23:03 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2163 | public FileTypeSet getAllowedFileTypesPredicate() { |
| 2164 | return allowedFileTypesForLabels; |
| 2165 | } |
| 2166 | |
| 2167 | public ValidityPredicate getValidityPredicate() { |
| 2168 | return validityPredicate; |
| 2169 | } |
| 2170 | |
| 2171 | public Predicate<AttributeMap> getCondition() { |
| 2172 | return condition == null ? Predicates.<AttributeMap>alwaysTrue() : condition; |
| 2173 | } |
| 2174 | |
| 2175 | public PredicateWithMessage<Object> getAllowedValues() { |
| 2176 | return allowedValues; |
| 2177 | } |
| 2178 | |
ulfjack | 5756b35 | 2020-01-23 04:53:22 -0800 | [diff] [blame] | 2179 | public boolean hasAspects() { |
| 2180 | return !aspects.isEmpty(); |
| 2181 | } |
| 2182 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2183 | /** |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 2184 | * Returns the list of aspects required for dependencies through this attribute. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2185 | */ |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 2186 | public ImmutableList<Aspect> getAspects(Rule rule) { |
ulfjack | 5756b35 | 2020-01-23 04:53:22 -0800 | [diff] [blame] | 2187 | if (aspects.isEmpty()) { |
| 2188 | return ImmutableList.of(); |
| 2189 | } |
shreyax | 6b9b607 | 2018-02-26 18:22:13 -0800 | [diff] [blame] | 2190 | ImmutableList.Builder<Aspect> builder = null; |
Googler | 3e1bf5c | 2019-11-04 12:41:49 -0800 | [diff] [blame] | 2191 | for (RuleAspect<?> aspect : aspects) { |
Googler | 459f130 | 2017-04-05 19:40:35 +0000 | [diff] [blame] | 2192 | Aspect a = aspect.getAspect(rule); |
| 2193 | if (a != null) { |
shreyax | 6b9b607 | 2018-02-26 18:22:13 -0800 | [diff] [blame] | 2194 | if (builder == null) { |
| 2195 | builder = ImmutableList.builder(); |
| 2196 | } |
Googler | 459f130 | 2017-04-05 19:40:35 +0000 | [diff] [blame] | 2197 | builder.add(a); |
| 2198 | } |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 2199 | } |
shreyax | 6b9b607 | 2018-02-26 18:22:13 -0800 | [diff] [blame] | 2200 | return builder == null ? ImmutableList.of() : builder.build(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2201 | } |
| 2202 | |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 2203 | public ImmutableList<AspectClass> getAspectClasses() { |
| 2204 | ImmutableList.Builder<AspectClass> result = ImmutableList.builder(); |
| 2205 | for (RuleAspect<?> aspect : aspects) { |
| 2206 | result.add(aspect.getAspectClass()); |
| 2207 | } |
| 2208 | return result.build(); |
| 2209 | } |
| 2210 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2211 | /** |
| 2212 | * Returns the default value of this attribute in the context of the |
| 2213 | * specified Rule. For attributes with a computed default, i.e. {@code |
| 2214 | * hasComputedDefault()}, {@code rule} must be non-null since the result may |
| 2215 | * depend on the values of its other attributes. |
| 2216 | * |
| 2217 | * <p>The result may be null (although this is not a value in the build |
| 2218 | * language). |
| 2219 | * |
| 2220 | * <p>During population of the rule's attribute dictionary, all non-computed |
| 2221 | * defaults must be set before all computed ones. |
| 2222 | * |
| 2223 | * @param rule the rule to which this attribute belongs; non-null if |
| 2224 | * {@code hasComputedDefault()}; ignored otherwise. |
| 2225 | */ |
| 2226 | public Object getDefaultValue(Rule rule) { |
| 2227 | if (!getCondition().apply(rule == null ? null : NonconfigurableAttributeMapper.of(rule))) { |
| 2228 | return null; |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 2229 | } else if (defaultValue instanceof LateBoundDefault<?, ?>) { |
| 2230 | return ((LateBoundDefault<?, ?>) defaultValue).getDefault(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2231 | } else { |
| 2232 | return defaultValue; |
| 2233 | } |
| 2234 | } |
| 2235 | |
| 2236 | /** |
| 2237 | * Returns the default value of this attribute, even if it has a condition, is a computed default, |
| 2238 | * or a late-bound default. |
| 2239 | */ |
| 2240 | @VisibleForTesting |
juliexxia | 84d1a66 | 2018-12-26 14:07:04 -0800 | [diff] [blame] | 2241 | public Object getDefaultValueUnchecked() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2242 | return defaultValue; |
| 2243 | } |
| 2244 | |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 2245 | public LateBoundDefault<?, ?> getLateBoundDefault() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2246 | Preconditions.checkState(isLateBound()); |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 2247 | return (LateBoundDefault<?, ?>) defaultValue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | /** |
| 2251 | * Returns true iff this attribute has a computed default or a condition. |
| 2252 | * |
| 2253 | * @see #getDefaultValue(Rule) |
| 2254 | */ |
| 2255 | boolean hasComputedDefault() { |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 2256 | return (defaultValue instanceof ComputedDefault) |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 2257 | || (defaultValue instanceof StarlarkComputedDefaultTemplate) |
Florian Weikert | ea6c82d | 2016-09-05 12:15:31 +0000 | [diff] [blame] | 2258 | || (condition != null); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | /** |
| 2262 | * Returns if this attribute is an implicit dependency according to the naming policy that |
| 2263 | * designates implicit attributes. |
| 2264 | */ |
| 2265 | public boolean isImplicit() { |
| 2266 | return isImplicit(getName()); |
| 2267 | } |
| 2268 | |
| 2269 | /** |
| 2270 | * Returns if an attribute with the given name is an implicit dependency according to the |
| 2271 | * naming policy that designates implicit attributes. |
| 2272 | */ |
| 2273 | public static boolean isImplicit(String name) { |
| 2274 | return name.startsWith("$"); |
| 2275 | } |
| 2276 | |
| 2277 | /** |
| 2278 | * Returns if this attribute is late-bound according to the naming policy that designates |
| 2279 | * late-bound attributes. |
| 2280 | */ |
| 2281 | public boolean isLateBound() { |
| 2282 | return isLateBound(getName()); |
| 2283 | } |
| 2284 | |
| 2285 | /** |
| 2286 | * Returns if an attribute with the given name is late-bound according to the naming policy |
| 2287 | * that designates late-bound attributes. |
| 2288 | */ |
| 2289 | public static boolean isLateBound(String name) { |
| 2290 | return name.startsWith(":"); |
| 2291 | } |
| 2292 | |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 2293 | /** Returns whether this attribute is considered private in Starlark. */ |
Florian Weikert | ba40525 | 2017-01-30 14:42:50 +0000 | [diff] [blame] | 2294 | private static boolean isPrivateAttribute(String nativeAttrName) { |
| 2295 | return isLateBound(nativeAttrName) || isImplicit(nativeAttrName); |
| 2296 | } |
| 2297 | |
| 2298 | /** |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 2299 | * Returns the Starlark-usable name of this attribute. |
Florian Weikert | ba40525 | 2017-01-30 14:42:50 +0000 | [diff] [blame] | 2300 | * |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 2301 | * <p>Implicit and late-bound attributes start with '_' (instead of '$' or ':'). |
Florian Weikert | ba40525 | 2017-01-30 14:42:50 +0000 | [diff] [blame] | 2302 | */ |
gregce | d7c1cef | 2020-05-12 07:51:48 -0700 | [diff] [blame] | 2303 | public static String getStarlarkName(String nativeAttrName) { |
Florian Weikert | ba40525 | 2017-01-30 14:42:50 +0000 | [diff] [blame] | 2304 | if (isPrivateAttribute(nativeAttrName)) { |
| 2305 | return "_" + nativeAttrName.substring(1); |
| 2306 | } |
| 2307 | return nativeAttrName; |
| 2308 | } |
| 2309 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2310 | @Override |
| 2311 | public String toString() { |
| 2312 | return "Attribute(" + name + ", " + type + ")"; |
| 2313 | } |
| 2314 | |
| 2315 | @Override |
| 2316 | public int compareTo(Attribute other) { |
| 2317 | return name.compareTo(other.name); |
| 2318 | } |
| 2319 | |
janakr | 9ba46f8 | 2018-03-13 13:07:52 -0700 | [diff] [blame] | 2320 | @Override |
| 2321 | public boolean equals(Object o) { |
| 2322 | if (this == o) { |
| 2323 | return true; |
| 2324 | } |
| 2325 | if (o == null || getClass() != o.getClass()) { |
| 2326 | return false; |
| 2327 | } |
| 2328 | Attribute attribute = (Attribute) o; |
nharmata | 977c880 | 2018-03-21 12:18:21 -0700 | [diff] [blame] | 2329 | return Objects.equals(hashCode, attribute.hashCode) |
| 2330 | && Objects.equals(name, attribute.name) |
Klaus Aehlig | 04952fc | 2019-03-01 11:36:16 -0800 | [diff] [blame] | 2331 | && Objects.equals(doc, attribute.doc) |
janakr | 9ba46f8 | 2018-03-13 13:07:52 -0700 | [diff] [blame] | 2332 | && Objects.equals(type, attribute.type) |
| 2333 | && Objects.equals(propertyFlags, attribute.propertyFlags) |
| 2334 | && Objects.equals(defaultValue, attribute.defaultValue) |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 2335 | && Objects.equals(transitionFactory, attribute.transitionFactory) |
janakr | 9ba46f8 | 2018-03-13 13:07:52 -0700 | [diff] [blame] | 2336 | && Objects.equals(allowedRuleClassesForLabels, attribute.allowedRuleClassesForLabels) |
| 2337 | && Objects.equals( |
| 2338 | allowedRuleClassesForLabelsWarning, attribute.allowedRuleClassesForLabelsWarning) |
| 2339 | && Objects.equals(allowedFileTypesForLabels, attribute.allowedFileTypesForLabels) |
| 2340 | && Objects.equals(validityPredicate, attribute.validityPredicate) |
| 2341 | && Objects.equals(condition, attribute.condition) |
| 2342 | && Objects.equals(allowedValues, attribute.allowedValues) |
| 2343 | && Objects.equals(requiredProviders, attribute.requiredProviders) |
| 2344 | && Objects.equals(aspects, attribute.aspects); |
| 2345 | } |
| 2346 | |
| 2347 | @Override |
| 2348 | public int hashCode() { |
nharmata | 977c880 | 2018-03-21 12:18:21 -0700 | [diff] [blame] | 2349 | return hashCode; |
janakr | 9ba46f8 | 2018-03-13 13:07:52 -0700 | [diff] [blame] | 2350 | } |
| 2351 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2352 | /** |
| 2353 | * Returns a replica builder of this Attribute. |
| 2354 | */ |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 2355 | public <TYPE> Attribute.Builder<TYPE> cloneBuilder(Type<TYPE> tp) { |
| 2356 | Preconditions.checkArgument(tp == this.type); |
Michajlo Matijkiw | 69d9b41 | 2016-10-06 20:06:59 +0000 | [diff] [blame] | 2357 | Builder<TYPE> builder = new Builder<>(name, tp); |
Klaus Aehlig | 04952fc | 2019-03-01 11:36:16 -0800 | [diff] [blame] | 2358 | builder.doc = doc; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2359 | builder.allowedFileTypesForLabels = allowedFileTypesForLabels; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2360 | builder.allowedRuleClassesForLabels = allowedRuleClassesForLabels; |
| 2361 | builder.allowedRuleClassesForLabelsWarning = allowedRuleClassesForLabelsWarning; |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 2362 | builder.requiredProvidersBuilder = requiredProviders.copyAsBuilder(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2363 | builder.validityPredicate = validityPredicate; |
| 2364 | builder.condition = condition; |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 2365 | builder.transitionFactory = transitionFactory; |
Greg Estren | 4ccabd3 | 2017-03-14 17:12:45 +0000 | [diff] [blame] | 2366 | builder.propertyFlags = newEnumSet(propertyFlags, PropertyFlag.class); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2367 | builder.value = defaultValue; |
| 2368 | builder.valueSet = false; |
| 2369 | builder.allowedValues = allowedValues; |
Dmitry Lomov | f188dc2 | 2016-07-19 09:00:55 +0000 | [diff] [blame] | 2370 | builder.aspects = new LinkedHashMap<>(); |
| 2371 | for (RuleAspect<?> aspect : aspects) { |
| 2372 | builder.aspects.put(aspect.getName(), aspect); |
| 2373 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2374 | |
| 2375 | return builder; |
| 2376 | } |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 2377 | |
| 2378 | public Attribute.Builder<?> cloneBuilder() { |
| 2379 | return cloneBuilder(this.type); |
| 2380 | } |
adonovan | b74f160 | 2020-11-02 17:33:49 -0800 | [diff] [blame] | 2381 | |
| 2382 | /** |
| 2383 | * Converts a rule attribute value from internal form to Starlark form. Internal form may use any |
| 2384 | * subtype of {@link List} or {@link Map} for {@code list} and {@code dict} attributes, whereas |
| 2385 | * Starlark uses only immutable {@link StarlarkList} and {@link Dict}. |
| 2386 | * |
| 2387 | * <p>The conversion is similar to {@link Starlark#fromJava} for all types except {@code |
| 2388 | * attr.string_list_dict} ({@code Map<String, List<String>>}), for which fromJava does not |
| 2389 | * recursively convert elements. (Doing so is expensive.) |
| 2390 | * |
| 2391 | * <p>It is tempting to require that attributes are stored internally in Starlark form. However, a |
| 2392 | * number of obstacles would need to be overcome: |
| 2393 | * |
| 2394 | * <ol> |
| 2395 | * <li>Some obscure attribute types such as TRISTATE and DISTRIBUTION are not currently legal |
| 2396 | * Starlark values. |
| 2397 | * <li>ImmutableList is significantly more compact than StarlarkList for small lists (n < 2). |
| 2398 | * StarlarkList would need multiple representations and a builder to achieve parity. |
| 2399 | * <li>The types used by the Type mechanism would need changing; this has extensive |
| 2400 | * ramifications. |
| 2401 | * </ol> |
| 2402 | */ |
| 2403 | public static Object valueToStarlark(Object x) { |
| 2404 | // Is x a non-empty string_list_dict? |
| 2405 | if (x instanceof Map) { |
| 2406 | Map<?, ?> map = (Map) x; |
| 2407 | if (!map.isEmpty() && map.values().iterator().next() instanceof List) { |
| 2408 | // Recursively convert subelements. |
| 2409 | Dict.Builder<Object, Object> dict = Dict.builder(); |
| 2410 | for (Map.Entry<?, ?> e : map.entrySet()) { |
| 2411 | dict.put((String) e.getKey(), Starlark.fromJava(e.getValue(), null)); |
| 2412 | } |
| 2413 | return dict.buildImmutable(); |
| 2414 | } |
| 2415 | } |
| 2416 | |
| 2417 | // For all other attribute values, shallow conversion is safe. |
| 2418 | return Starlark.fromJava(x, null); |
| 2419 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2420 | } |