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 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 17 | import com.google.common.base.Preconditions; |
Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
jcater | 7582875 | 2018-04-27 12:53:19 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Iterables; |
jcater | 7582875 | 2018-04-27 12:53:19 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Lists; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | import com.google.common.collect.Multimap; |
gregce | be55e11 | 2018-01-30 11:04:53 -0800 | [diff] [blame] | 24 | import com.google.devtools.build.lib.analysis.config.transitions.ConfigurationTransition; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.cmdline.Label; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.packages.ConfigurationFragmentPolicy.MissingFragmentPolicy; |
Googler | c5fcc86 | 2019-09-06 16:17:47 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.packages.Type.LabelClass; |
| 29 | import com.google.devtools.build.lib.packages.Type.LabelVisitor; |
janakr | ee5bf48 | 2018-03-26 10:55:59 -0700 | [diff] [blame] | 30 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
John Cater | 13263f7 | 2017-05-24 19:06:47 +0200 | [diff] [blame] | 31 | import java.util.ArrayList; |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 32 | import java.util.Collection; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | import java.util.LinkedHashMap; |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 34 | import java.util.LinkedHashSet; |
John Cater | 13263f7 | 2017-05-24 19:06:47 +0200 | [diff] [blame] | 35 | import java.util.List; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 36 | import java.util.Map; |
nharmata | f2b26ad | 2019-03-06 09:37:00 -0800 | [diff] [blame] | 37 | import java.util.function.BiConsumer; |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 38 | import javax.annotation.Nullable; |
| 39 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | /** |
janakr | ee5bf48 | 2018-03-26 10:55:59 -0700 | [diff] [blame] | 41 | * The definition of an aspect (see {@link Aspect} for more information). |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 42 | * |
| 43 | * <p>Contains enough information to build up the configured target graph except for the actual way |
janakr | ee5bf48 | 2018-03-26 10:55:59 -0700 | [diff] [blame] | 44 | * to build the Skyframe node (that is the territory of {@link com.google.devtools.build.lib.view |
| 45 | * AspectFactory}). In particular: |
| 46 | * |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 47 | * <ul> |
| 48 | * <li>The condition that must be fulfilled for an aspect to be able to operate on a configured |
| 49 | * target |
| 50 | * <li>The (implicit or late-bound) attributes of the aspect that denote dependencies the aspect |
| 51 | * itself needs (e.g. runtime libraries for a new language for protocol buffers) |
| 52 | * <li>The aspects this aspect requires from its direct dependencies |
| 53 | * </ul> |
| 54 | * |
| 55 | * <p>The way to build the Skyframe node is not here because this data needs to be accessible from |
| 56 | * the {@code .packages} package and that one requires references to the {@code .view} package. |
| 57 | */ |
janakr | ee5bf48 | 2018-03-26 10:55:59 -0700 | [diff] [blame] | 58 | @AutoCodec |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 59 | @Immutable |
| 60 | public final class AspectDefinition { |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 61 | private final AspectClass aspectClass; |
Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 62 | private final AdvertisedProviderSet advertisedProviders; |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 63 | private final RequiredProviders requiredProviders; |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 64 | private final RequiredProviders requiredProvidersForAspects; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 65 | private final ImmutableMap<String, Attribute> attributes; |
cpeyser | fb82999 | 2017-09-07 17:17:03 +0200 | [diff] [blame] | 66 | private final ImmutableSet<Label> requiredToolchains; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 67 | |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 68 | /** |
| 69 | * Which attributes aspect should propagate along: |
| 70 | * <ul> |
| 71 | * <li>A {@code null} value means propagate along all attributes</li> |
| 72 | * <li>A (possibly empty) set means to propagate only along the attributes in a set</li> |
| 73 | * </ul> |
| 74 | */ |
| 75 | @Nullable private final ImmutableSet<String> restrictToAttributes; |
| 76 | @Nullable private final ConfigurationFragmentPolicy configurationFragmentPolicy; |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 77 | private final boolean applyToFiles; |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 78 | private final boolean applyToGeneratingRules; |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 79 | |
Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 80 | public AdvertisedProviderSet getAdvertisedProviders() { |
| 81 | return advertisedProviders; |
| 82 | } |
| 83 | |
janakr | ee5bf48 | 2018-03-26 10:55:59 -0700 | [diff] [blame] | 84 | @AutoCodec.VisibleForSerialization |
| 85 | AspectDefinition( |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 86 | AspectClass aspectClass, |
Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 87 | AdvertisedProviderSet advertisedProviders, |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 88 | RequiredProviders requiredProviders, |
janakr | ee5bf48 | 2018-03-26 10:55:59 -0700 | [diff] [blame] | 89 | RequiredProviders requiredProvidersForAspects, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 90 | ImmutableMap<String, Attribute> attributes, |
cpeyser | fb82999 | 2017-09-07 17:17:03 +0200 | [diff] [blame] | 91 | ImmutableSet<Label> requiredToolchains, |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 92 | @Nullable ImmutableSet<String> restrictToAttributes, |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 93 | @Nullable ConfigurationFragmentPolicy configurationFragmentPolicy, |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 94 | boolean applyToFiles, |
| 95 | boolean applyToGeneratingRules) { |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 96 | this.aspectClass = aspectClass; |
Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 97 | this.advertisedProviders = advertisedProviders; |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 98 | this.requiredProviders = requiredProviders; |
janakr | ee5bf48 | 2018-03-26 10:55:59 -0700 | [diff] [blame] | 99 | this.requiredProvidersForAspects = requiredProvidersForAspects; |
Dmitry Lomov | a701988 | 2016-12-05 16:57:02 +0000 | [diff] [blame] | 100 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 101 | this.attributes = attributes; |
John Cater | 13263f7 | 2017-05-24 19:06:47 +0200 | [diff] [blame] | 102 | this.requiredToolchains = requiredToolchains; |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 103 | this.restrictToAttributes = restrictToAttributes; |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 104 | this.configurationFragmentPolicy = configurationFragmentPolicy; |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 105 | this.applyToFiles = applyToFiles; |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 106 | this.applyToGeneratingRules = applyToGeneratingRules; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | public String getName() { |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 110 | return aspectClass.getName(); |
| 111 | } |
| 112 | |
| 113 | public AspectClass getAspectClass() { |
| 114 | return aspectClass; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns the attributes of the aspect in the form of a String -> {@link Attribute} map. |
| 119 | * |
| 120 | * <p>All attributes are either implicit or late-bound. |
| 121 | */ |
| 122 | public ImmutableMap<String, Attribute> getAttributes() { |
| 123 | return attributes; |
| 124 | } |
| 125 | |
John Cater | 13263f7 | 2017-05-24 19:06:47 +0200 | [diff] [blame] | 126 | /** Returns the required toolchains declared by this aspect. */ |
cpeyser | fb82999 | 2017-09-07 17:17:03 +0200 | [diff] [blame] | 127 | public ImmutableSet<Label> getRequiredToolchains() { |
John Cater | 13263f7 | 2017-05-24 19:06:47 +0200 | [diff] [blame] | 128 | return requiredToolchains; |
| 129 | } |
| 130 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 131 | /** |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 132 | * Returns {@link RequiredProviders} that a configured target must have so that |
| 133 | * this aspect can be applied to it. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 134 | * |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 135 | * <p>If a configured target does not satisfy required providers, the aspect is |
| 136 | * silently not created for it. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 137 | */ |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 138 | public RequiredProviders getRequiredProviders() { |
| 139 | return requiredProviders; |
Eric Fellheimer | e27d063 | 2015-09-25 21:35:26 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 143 | * Aspects do not depend on other aspects applied to the same target <em>unless</em> |
| 144 | * the other aspect satisfies the {@link RequiredProviders} this method returns |
| 145 | */ |
| 146 | public RequiredProviders getRequiredProvidersForAspects() { |
| 147 | return requiredProvidersForAspects; |
| 148 | } |
| 149 | |
lberki | 3bd2839 | 2019-02-05 01:01:02 -0800 | [diff] [blame] | 150 | /** Returns the set of required aspects for a given attribute. */ |
| 151 | public boolean propagateAlong(String attributeName) { |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 152 | if (restrictToAttributes != null) { |
lberki | 3bd2839 | 2019-02-05 01:01:02 -0800 | [diff] [blame] | 153 | return restrictToAttributes.contains(attributeName); |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 154 | } |
| 155 | return true; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
Michael Staib | af27046 | 2016-05-18 16:22:07 +0000 | [diff] [blame] | 159 | * Returns the set of configuration fragments required by this Aspect. |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 160 | */ |
Michael Staib | af27046 | 2016-05-18 16:22:07 +0000 | [diff] [blame] | 161 | public ConfigurationFragmentPolicy getConfigurationFragmentPolicy() { |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 162 | return configurationFragmentPolicy; |
| 163 | } |
| 164 | |
| 165 | /** |
dslomov | ce59d4d | 2017-08-04 17:32:47 +0200 | [diff] [blame] | 166 | * Returns whether this aspect applies to (output) files. |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 167 | * |
dslomov | ce59d4d | 2017-08-04 17:32:47 +0200 | [diff] [blame] | 168 | * Currently only supported for top-level aspects and targets, and |
| 169 | * only for output files. |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 170 | */ |
| 171 | public boolean applyToFiles() { |
| 172 | return applyToFiles; |
| 173 | } |
| 174 | |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 175 | /** |
| 176 | * Returns whether this aspect should, when it would be applied to an output file, instead apply |
| 177 | * to the generating rule of that output file. |
| 178 | */ |
| 179 | public boolean applyToGeneratingRules() { |
| 180 | return applyToGeneratingRules; |
| 181 | } |
| 182 | |
nharmata | f2b26ad | 2019-03-06 09:37:00 -0800 | [diff] [blame] | 183 | public static boolean satisfies(Aspect aspect, AdvertisedProviderSet advertisedProviderSet) { |
| 184 | return aspect.getDefinition().getRequiredProviders().isSatisfiedBy(advertisedProviderSet); |
Marian Lobur | fdd788e | 2015-03-25 09:36:28 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Nathan Harmata | 19f2238 | 2016-06-15 19:06:56 +0000 | [diff] [blame] | 187 | @Nullable |
| 188 | private static Label maybeGetRepositoryRelativeLabel(Rule from, @Nullable Label label) { |
| 189 | return label == null ? null : from.getLabel().resolveRepositoryRelative(label); |
| 190 | } |
| 191 | |
Marian Lobur | fdd788e | 2015-03-25 09:36:28 +0000 | [diff] [blame] | 192 | /** |
| 193 | * Collects all attribute labels from the specified aspectDefinition. |
| 194 | */ |
Dmitry Lomov | 6073eb6 | 2016-01-21 21:26:32 +0000 | [diff] [blame] | 195 | public static void addAllAttributesOfAspect( |
Michael Staib | a751f92 | 2017-02-14 15:50:04 +0000 | [diff] [blame] | 196 | final Rule from, |
| 197 | final Multimap<Attribute, Label> labelBuilder, |
Dmitry Lomov | 940ea07 | 2016-01-21 22:34:14 +0000 | [diff] [blame] | 198 | Aspect aspect, |
| 199 | DependencyFilter dependencyFilter) { |
nharmata | f2b26ad | 2019-03-06 09:37:00 -0800 | [diff] [blame] | 200 | forEachLabelDepFromAllAttributesOfAspect(from, aspect, dependencyFilter, labelBuilder::put); |
| 201 | } |
| 202 | |
| 203 | public static void forEachLabelDepFromAllAttributesOfAspect( |
| 204 | Rule from, |
| 205 | Aspect aspect, |
| 206 | DependencyFilter dependencyFilter, |
| 207 | BiConsumer<Attribute, Label> consumer) { |
shreyax | 2bcbecf | 2018-09-26 12:27:32 -0700 | [diff] [blame] | 208 | LabelVisitor<Attribute> labelVisitor = |
| 209 | (label, aspectAttribute) -> { |
nharmata | f2b26ad | 2019-03-06 09:37:00 -0800 | [diff] [blame] | 210 | Label repositoryRelativeLabel = maybeGetRepositoryRelativeLabel(from, label); |
| 211 | if (repositoryRelativeLabel == null) { |
shreyax | 2bcbecf | 2018-09-26 12:27:32 -0700 | [diff] [blame] | 212 | return; |
| 213 | } |
nharmata | f2b26ad | 2019-03-06 09:37:00 -0800 | [diff] [blame] | 214 | consumer.accept(aspectAttribute, repositoryRelativeLabel); |
shreyax | 2bcbecf | 2018-09-26 12:27:32 -0700 | [diff] [blame] | 215 | }; |
nharmata | f2b26ad | 2019-03-06 09:37:00 -0800 | [diff] [blame] | 216 | for (Attribute aspectAttribute : aspect.getDefinition().getAttributes().values()) { |
Dmitry Lomov | 940ea07 | 2016-01-21 22:34:14 +0000 | [diff] [blame] | 217 | if (!dependencyFilter.apply(aspect, aspectAttribute)) { |
Marian Lobur | fdd788e | 2015-03-25 09:36:28 +0000 | [diff] [blame] | 218 | continue; |
| 219 | } |
nharmata | d705c98 | 2017-04-04 19:09:15 +0000 | [diff] [blame] | 220 | Type<?> type = aspectAttribute.getType(); |
Michael Staib | a751f92 | 2017-02-14 15:50:04 +0000 | [diff] [blame] | 221 | if (type.getLabelClass() != LabelClass.DEPENDENCY) { |
| 222 | continue; |
| 223 | } |
shreyax | 2bcbecf | 2018-09-26 12:27:32 -0700 | [diff] [blame] | 224 | type.visitLabels(labelVisitor, aspectAttribute.getDefaultValue(from), aspectAttribute); |
Marian Lobur | fdd788e | 2015-03-25 09:36:28 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Dmitry Lomov | 0c66dee | 2017-02-13 17:24:43 +0000 | [diff] [blame] | 228 | public static Builder builder(AspectClass aspectClass) { |
| 229 | return new Builder(aspectClass); |
| 230 | } |
| 231 | |
Marian Lobur | fdd788e | 2015-03-25 09:36:28 +0000 | [diff] [blame] | 232 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 233 | * Builder class for {@link AspectDefinition}. |
| 234 | */ |
| 235 | public static final class Builder { |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 236 | private final AspectClass aspectClass; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 237 | private final Map<String, Attribute> attributes = new LinkedHashMap<>(); |
Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 238 | private final AdvertisedProviderSet.Builder advertisedProviders = |
| 239 | AdvertisedProviderSet.builder(); |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 240 | private RequiredProviders.Builder requiredProviders = RequiredProviders.acceptAnyBuilder(); |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 241 | private RequiredProviders.Builder requiredAspectProviders = |
| 242 | RequiredProviders.acceptNoneBuilder(); |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 243 | @Nullable |
| 244 | private LinkedHashSet<String> propagateAlongAttributes = new LinkedHashSet<>(); |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 245 | private final ConfigurationFragmentPolicy.Builder configurationFragmentPolicy = |
| 246 | new ConfigurationFragmentPolicy.Builder(); |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 247 | private boolean applyToFiles = false; |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 248 | private boolean applyToGeneratingRules = false; |
John Cater | 9a8d16e | 2017-07-05 16:12:07 -0400 | [diff] [blame] | 249 | private final List<Label> requiredToolchains = new ArrayList<>(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 250 | |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 251 | public Builder(AspectClass aspectClass) { |
| 252 | this.aspectClass = aspectClass; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /** |
Rumou Duan | 6f8393f | 2016-11-30 16:03:10 +0000 | [diff] [blame] | 256 | * Asserts that this aspect can only be evaluated for rules that supply all of the providers |
| 257 | * from at least one set of required providers. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 258 | */ |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 259 | public Builder requireProviderSets(Iterable<ImmutableSet<Class<?>>> providerSets) { |
| 260 | for (ImmutableSet<Class<?>> providerSet : providerSets) { |
| 261 | requiredProviders.addNativeSet(providerSet); |
Rumou Duan | 6f8393f | 2016-11-30 16:03:10 +0000 | [diff] [blame] | 262 | } |
Rumou Duan | 6f8393f | 2016-11-30 16:03:10 +0000 | [diff] [blame] | 263 | return this; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Asserts that this aspect can only be evaluated for rules that supply all of the specified |
| 268 | * providers. |
| 269 | */ |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 270 | public Builder requireProviders(Class<?>... providers) { |
| 271 | requiredProviders.addNativeSet(ImmutableSet.copyOf(providers)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 272 | return this; |
| 273 | } |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 274 | |
elenairina | aeb21bc | 2017-09-27 12:13:52 -0400 | [diff] [blame] | 275 | /** |
| 276 | * Asserts that this aspect can only be evaluated for rules that supply all of the specified |
| 277 | * Skylark providers. |
| 278 | */ |
| 279 | public Builder requireSkylarkProviders(SkylarkProviderIdentifier... skylarkProviders) { |
| 280 | requiredProviders.addSkylarkSet(ImmutableSet.copyOf(skylarkProviders)); |
| 281 | return this; |
| 282 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 283 | |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 284 | public Builder requireAspectsWithProviders( |
| 285 | Iterable<ImmutableSet<SkylarkProviderIdentifier>> providerSets) { |
| 286 | for (ImmutableSet<SkylarkProviderIdentifier> providerSet : providerSets) { |
| 287 | if (!providerSet.isEmpty()) { |
| 288 | requiredAspectProviders.addSkylarkSet(providerSet); |
| 289 | } |
| 290 | } |
| 291 | return this; |
| 292 | } |
| 293 | |
| 294 | public Builder requireAspectsWithNativeProviders( |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 295 | Class<?>... providers) { |
| 296 | requiredAspectProviders.addNativeSet(ImmutableSet.copyOf(providers)); |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 297 | return this; |
| 298 | } |
| 299 | |
Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 300 | /** |
| 301 | * State that the aspect being built provides given providers. |
| 302 | */ |
| 303 | public Builder advertiseProvider(Class<?>... providers) { |
| 304 | for (Class<?> provider : providers) { |
| 305 | advertisedProviders.addNative(provider); |
| 306 | } |
| 307 | return this; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * State that the aspect being built provides given providers. |
| 312 | */ |
| 313 | public Builder advertiseProvider(ImmutableList<SkylarkProviderIdentifier> providers) { |
| 314 | for (SkylarkProviderIdentifier provider : providers) { |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 315 | advertisedProviders.addSkylark(provider); |
Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 316 | } |
| 317 | return this; |
| 318 | } |
| 319 | |
| 320 | |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 321 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 322 | /** |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 323 | * Declares that this aspect propagates along an {@code attribute} on the target |
| 324 | * associated with this aspect. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 325 | * |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 326 | * Specify multiple attributes by calling {@link #propagateAlongAttribute(String)} |
| 327 | * repeatedly. |
| 328 | * |
| 329 | * Aspect can also declare to propagate along all attributes with |
| 330 | * {@link #propagateAlongAttributes}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 331 | */ |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 332 | public final Builder propagateAlongAttribute(String attribute) { |
Rumou Duan | 5b0b310 | 2015-06-03 20:27:57 +0000 | [diff] [blame] | 333 | Preconditions.checkNotNull(attribute); |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 334 | Preconditions.checkState(this.propagateAlongAttributes != null, |
| 335 | "Either propagate along all attributes, or along specific attributes, not both"); |
| 336 | |
| 337 | this.propagateAlongAttributes.add(attribute); |
| 338 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 339 | return this; |
| 340 | } |
| 341 | |
| 342 | /** |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 343 | * Declares that this aspect propagates along all attributes on the target |
| 344 | * associated with this aspect. |
| 345 | * |
| 346 | * Specify either this or {@link #propagateAlongAttribute(String)}, not both. |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 347 | */ |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 348 | public final Builder propagateAlongAllAttributes() { |
| 349 | Preconditions.checkState(this.propagateAlongAttributes != null, |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 350 | "Aspects for all attributes must only be specified once"); |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 351 | |
| 352 | Preconditions.checkState(this.propagateAlongAttributes.isEmpty(), |
| 353 | "Specify either aspects for all attributes, or for specific attributes, not both"); |
| 354 | this.propagateAlongAttributes = null; |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 355 | return this; |
| 356 | } |
| 357 | |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 358 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 359 | * Adds an attribute to the aspect. |
| 360 | * |
| 361 | * <p>Since aspects do not appear in BUILD files, the attribute must be either implicit |
| 362 | * (not available in the BUILD file, starting with '$') or late-bound (determined after the |
| 363 | * configuration is available, starting with ':') |
| 364 | */ |
| 365 | public <TYPE> Builder add(Attribute.Builder<TYPE> attr) { |
| 366 | Attribute attribute = attr.build(); |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 367 | return add(attribute); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Adds an attribute to the aspect. |
| 372 | * |
| 373 | * <p>Since aspects do not appear in BUILD files, the attribute must be either implicit |
| 374 | * (not available in the BUILD file, starting with '$') or late-bound (determined after the |
| 375 | * configuration is available, starting with ':') |
| 376 | */ |
| 377 | public Builder add(Attribute attribute) { |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 378 | Preconditions.checkArgument(attribute.isImplicit() || attribute.isLateBound() |
| 379 | || (attribute.getType() == Type.STRING && attribute.checkAllowedValues()), |
| 380 | "Invalid attribute '%s' (%s)", attribute.getName(), attribute.getType()); |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 381 | Preconditions.checkArgument(!attributes.containsKey(attribute.getName()), |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 382 | "An attribute with the name '%s' already exists.", attribute.getName()); |
| 383 | attributes.put(attribute.getName(), attribute); |
| 384 | return this; |
| 385 | } |
| 386 | |
| 387 | /** |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 388 | * Declares that the implementation of the associated aspect definition requires the given |
| 389 | * fragments to be present in this rule's host and target configurations. |
| 390 | * |
| 391 | * <p>The value is inherited by subclasses. |
| 392 | */ |
| 393 | public Builder requiresConfigurationFragments(Class<?>... configurationFragments) { |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 394 | configurationFragmentPolicy |
| 395 | .requiresConfigurationFragments(ImmutableSet.copyOf(configurationFragments)); |
| 396 | return this; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Declares that the implementation of the associated aspect definition requires the given |
gregce | 614dc50 | 2017-12-20 17:24:46 -0800 | [diff] [blame] | 401 | * fragments to be present in the given configuration that isn't the aspect's configuration but |
| 402 | * is also readable by the aspect. |
| 403 | * |
| 404 | * <p>You probably don't want to use this, because aspects generally shouldn't read |
| 405 | * configurations other than their own. If you want to declare host config fragments, see |
| 406 | * {@link com.google.devtools.build.lib.analysis.config.ConfigAwareAspectBuilder}. |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 407 | * |
| 408 | * <p>The value is inherited by subclasses. |
| 409 | */ |
gregce | be55e11 | 2018-01-30 11:04:53 -0800 | [diff] [blame] | 410 | public Builder requiresConfigurationFragments(ConfigurationTransition transition, |
gregce | 614dc50 | 2017-12-20 17:24:46 -0800 | [diff] [blame] | 411 | Class<?>... configurationFragments) { |
| 412 | configurationFragmentPolicy.requiresConfigurationFragments(transition, |
| 413 | ImmutableSet.copyOf(configurationFragments)); |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 414 | return this; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Declares the configuration fragments that are required by this rule for the target |
| 419 | * configuration. |
| 420 | * |
| 421 | * <p>In contrast to {@link #requiresConfigurationFragments(Class...)}, this method takes the |
| 422 | * Skylark module names of fragments instead of their classes. |
| 423 | */ |
| 424 | public Builder requiresConfigurationFragmentsBySkylarkModuleName( |
| 425 | Collection<String> configurationFragmentNames) { |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 426 | configurationFragmentPolicy |
| 427 | .requiresConfigurationFragmentsBySkylarkModuleName(configurationFragmentNames); |
| 428 | return this; |
| 429 | } |
| 430 | |
| 431 | /** |
gregce | 614dc50 | 2017-12-20 17:24:46 -0800 | [diff] [blame] | 432 | * Declares that the implementation of the associated aspect definition requires the given |
| 433 | * fragments to be present in the given configuration that isn't the aspect's configuration but |
| 434 | * is also readable by the aspect. |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 435 | * |
gregce | be55e11 | 2018-01-30 11:04:53 -0800 | [diff] [blame] | 436 | * <p>In contrast to {@link #requiresConfigurationFragments(ConfigurationTransition, Class...)}, |
| 437 | * this method takes the Skylark module names of fragments instead of their classes. |
gregce | 614dc50 | 2017-12-20 17:24:46 -0800 | [diff] [blame] | 438 | * |
| 439 | * <p>You probably don't want to use this, because aspects generally shouldn't read |
| 440 | * configurations other than their own. If you want to declare host config fragments, see |
| 441 | * {@link com.google.devtools.build.lib.analysis.config.ConfigAwareAspectBuilder}. |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 442 | */ |
gregce | be55e11 | 2018-01-30 11:04:53 -0800 | [diff] [blame] | 443 | public Builder requiresConfigurationFragmentsBySkylarkModuleName( |
| 444 | ConfigurationTransition transition, Collection<String> configurationFragmentNames) { |
gregce | 614dc50 | 2017-12-20 17:24:46 -0800 | [diff] [blame] | 445 | configurationFragmentPolicy.requiresConfigurationFragmentsBySkylarkModuleName(transition, |
| 446 | configurationFragmentNames); |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 447 | return this; |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Sets the policy for the case where the configuration is missing required fragments (see |
| 452 | * {@link #requiresConfigurationFragments}). |
| 453 | */ |
| 454 | public Builder setMissingFragmentPolicy(MissingFragmentPolicy missingFragmentPolicy) { |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 455 | configurationFragmentPolicy.setMissingFragmentPolicy(missingFragmentPolicy); |
| 456 | return this; |
| 457 | } |
| 458 | |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 459 | /** |
| 460 | * Sets whether this aspect should apply to files. |
| 461 | * |
| 462 | * Default is <code>false</code>. |
dslomov | ce59d4d | 2017-08-04 17:32:47 +0200 | [diff] [blame] | 463 | * Currently only supported for top-level aspects and targets, and only for |
| 464 | * output files. |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 465 | */ |
| 466 | public Builder applyToFiles(boolean propagateOverGeneratedFiles) { |
| 467 | this.applyToFiles = propagateOverGeneratedFiles; |
| 468 | return this; |
| 469 | } |
| 470 | |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 471 | /** |
| 472 | * Sets whether this aspect should, when it would be applied to an output file, instead apply to |
| 473 | * the generating rule of that output file. |
| 474 | * |
| 475 | * <p>Default is <code>false</code>. Currently only supported for aspects which do not have a |
| 476 | * "required providers" list. |
| 477 | */ |
| 478 | public Builder applyToGeneratingRules(boolean applyToGeneratingRules) { |
| 479 | this.applyToGeneratingRules = applyToGeneratingRules; |
| 480 | return this; |
| 481 | } |
| 482 | |
John Cater | 13263f7 | 2017-05-24 19:06:47 +0200 | [diff] [blame] | 483 | /** Adds the given toolchains as requirements for this aspect. */ |
jcater | 7582875 | 2018-04-27 12:53:19 -0700 | [diff] [blame] | 484 | public Builder addRequiredToolchains(Label... toolchainLabels) { |
| 485 | Iterables.addAll(this.requiredToolchains, Lists.newArrayList(toolchainLabels)); |
| 486 | return this; |
| 487 | } |
| 488 | |
| 489 | /** Adds the given toolchains as requirements for this aspect. */ |
John Cater | 9a8d16e | 2017-07-05 16:12:07 -0400 | [diff] [blame] | 490 | public Builder addRequiredToolchains(List<Label> requiredToolchains) { |
John Cater | 13263f7 | 2017-05-24 19:06:47 +0200 | [diff] [blame] | 491 | this.requiredToolchains.addAll(requiredToolchains); |
| 492 | return this; |
| 493 | } |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 494 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 495 | * Builds the aspect definition. |
| 496 | * |
| 497 | * <p>The builder object is reusable afterwards. |
| 498 | */ |
| 499 | public AspectDefinition build() { |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 500 | RequiredProviders requiredProviders = this.requiredProviders.build(); |
| 501 | if (applyToGeneratingRules && !requiredProviders.acceptsAny()) { |
| 502 | throw new IllegalStateException( |
| 503 | "An aspect cannot simultaneously have required providers " |
| 504 | + "and apply to generating rules."); |
| 505 | } |
| 506 | |
John Cater | 13263f7 | 2017-05-24 19:06:47 +0200 | [diff] [blame] | 507 | return new AspectDefinition( |
| 508 | aspectClass, |
Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 509 | advertisedProviders.build(), |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 510 | requiredProviders, |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 511 | requiredAspectProviders.build(), |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 512 | ImmutableMap.copyOf(attributes), |
cpeyser | fb82999 | 2017-09-07 17:17:03 +0200 | [diff] [blame] | 513 | ImmutableSet.copyOf(requiredToolchains), |
John Cater | 13263f7 | 2017-05-24 19:06:47 +0200 | [diff] [blame] | 514 | propagateAlongAttributes == null ? null : ImmutableSet.copyOf(propagateAlongAttributes), |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 515 | configurationFragmentPolicy.build(), |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 516 | applyToFiles, |
| 517 | applyToGeneratingRules); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | } |