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