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; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import com.google.common.collect.Multimap; |
jhorvitz | 11dc104 | 2021-08-13 07:30:19 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.analysis.config.Fragment; |
John Cater | fb16270 | 2022-02-16 10:09:34 -0800 | [diff] [blame] | 23 | import com.google.devtools.build.lib.analysis.config.ToolchainTypeRequirement; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.cmdline.Label; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
ilist | d7432e2 | 2020-06-04 13:51:45 -0700 | [diff] [blame] | 26 | import com.google.devtools.build.lib.packages.Attribute.ComputedDefault; |
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; |
Googler | aa55c4d | 2023-08-30 00:48:57 -0700 | [diff] [blame] | 30 | import com.google.devtools.build.lib.starlarkbuildapi.StarlarkSubruleApi; |
Kurt Alfred Kluever | 29e8e7d | 2022-07-04 06:50:48 -0700 | [diff] [blame] | 31 | import com.google.errorprone.annotations.CanIgnoreReturnValue; |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 32 | import java.util.Collection; |
John Cater | 6f2c3e2 | 2022-03-03 08:15:22 -0800 | [diff] [blame] | 33 | import java.util.HashSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 34 | import java.util.LinkedHashMap; |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 35 | import java.util.LinkedHashSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 36 | import java.util.Map; |
John Cater | 6f2c3e2 | 2022-03-03 08:15:22 -0800 | [diff] [blame] | 37 | import java.util.Set; |
nharmata | f2b26ad | 2019-03-06 09:37:00 -0800 | [diff] [blame] | 38 | import java.util.function.BiConsumer; |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 39 | import javax.annotation.Nullable; |
| 40 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | /** |
janakr | ee5bf48 | 2018-03-26 10:55:59 -0700 | [diff] [blame] | 42 | * The definition of an aspect (see {@link Aspect} for more information). |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | * |
| 44 | * <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] | 45 | * to build the Skyframe node (that is the territory of {@link com.google.devtools.build.lib.view |
| 46 | * AspectFactory}). In particular: |
| 47 | * |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 48 | * <ul> |
| 49 | * <li>The condition that must be fulfilled for an aspect to be able to operate on a configured |
| 50 | * target |
| 51 | * <li>The (implicit or late-bound) attributes of the aspect that denote dependencies the aspect |
| 52 | * itself needs (e.g. runtime libraries for a new language for protocol buffers) |
| 53 | * <li>The aspects this aspect requires from its direct dependencies |
| 54 | * </ul> |
| 55 | * |
| 56 | * <p>The way to build the Skyframe node is not here because this data needs to be accessible from |
| 57 | * the {@code .packages} package and that one requires references to the {@code .view} package. |
| 58 | */ |
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; |
John Cater | fb16270 | 2022-02-16 10:09:34 -0800 | [diff] [blame] | 66 | private final ImmutableSet<ToolchainTypeRequirement> toolchainTypes; |
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: |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 70 | * |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 71 | * <ul> |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 72 | * <li>A {@code null} value means propagate along all attributes |
| 73 | * <li>A (possibly empty) set means to propagate only along the attributes in a set |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 74 | * </ul> |
| 75 | */ |
| 76 | @Nullable private final ImmutableSet<String> restrictToAttributes; |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 77 | |
Googler | b63ad3f | 2024-06-18 11:42:35 -0700 | [diff] [blame] | 78 | /** Toolchain types for which the aspect will propagate to matching resolved toolchains. */ |
| 79 | private final ImmutableSet<Label> propagateToToolchainsTypes; |
| 80 | |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 81 | @Nullable private final ConfigurationFragmentPolicy configurationFragmentPolicy; |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 82 | private final boolean applyToFiles; |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 83 | private final boolean applyToGeneratingRules; |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 84 | |
messa | f8c3408 | 2021-06-04 02:51:04 -0700 | [diff] [blame] | 85 | private final ImmutableSet<AspectClass> requiredAspectClasses; |
| 86 | |
Googler | 7e3755d | 2022-06-01 12:46:09 -0700 | [diff] [blame] | 87 | private final ImmutableSet<Label> execCompatibleWith; |
| 88 | private final ImmutableMap<String, ExecGroup> execGroups; |
Googler | aa55c4d | 2023-08-30 00:48:57 -0700 | [diff] [blame] | 89 | private final ImmutableSet<? extends StarlarkSubruleApi> subrules; |
Googler | 7e3755d | 2022-06-01 12:46:09 -0700 | [diff] [blame] | 90 | |
Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 91 | public AdvertisedProviderSet getAdvertisedProviders() { |
| 92 | return advertisedProviders; |
| 93 | } |
| 94 | |
Googler | 7f74878 | 2021-11-22 17:20:58 -0800 | [diff] [blame] | 95 | private AspectDefinition( |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 96 | AspectClass aspectClass, |
Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 97 | AdvertisedProviderSet advertisedProviders, |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 98 | RequiredProviders requiredProviders, |
janakr | ee5bf48 | 2018-03-26 10:55:59 -0700 | [diff] [blame] | 99 | RequiredProviders requiredProvidersForAspects, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 100 | ImmutableMap<String, Attribute> attributes, |
John Cater | fb16270 | 2022-02-16 10:09:34 -0800 | [diff] [blame] | 101 | ImmutableSet<ToolchainTypeRequirement> toolchainTypes, |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 102 | @Nullable ImmutableSet<String> restrictToAttributes, |
Googler | b63ad3f | 2024-06-18 11:42:35 -0700 | [diff] [blame] | 103 | ImmutableSet<Label> propagateToToolchainsTypes, |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 104 | @Nullable ConfigurationFragmentPolicy configurationFragmentPolicy, |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 105 | boolean applyToFiles, |
gregce | db27113 | 2021-04-05 15:50:50 -0700 | [diff] [blame] | 106 | boolean applyToGeneratingRules, |
Googler | 7e3755d | 2022-06-01 12:46:09 -0700 | [diff] [blame] | 107 | ImmutableSet<AspectClass> requiredAspectClasses, |
| 108 | ImmutableSet<Label> execCompatibleWith, |
Googler | aa55c4d | 2023-08-30 00:48:57 -0700 | [diff] [blame] | 109 | ImmutableMap<String, ExecGroup> execGroups, |
| 110 | ImmutableSet<? extends StarlarkSubruleApi> subrules) { |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 111 | this.aspectClass = aspectClass; |
Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 112 | this.advertisedProviders = advertisedProviders; |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 113 | this.requiredProviders = requiredProviders; |
janakr | ee5bf48 | 2018-03-26 10:55:59 -0700 | [diff] [blame] | 114 | this.requiredProvidersForAspects = requiredProvidersForAspects; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 115 | this.attributes = attributes; |
John Cater | fb16270 | 2022-02-16 10:09:34 -0800 | [diff] [blame] | 116 | this.toolchainTypes = toolchainTypes; |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 117 | this.restrictToAttributes = restrictToAttributes; |
Googler | b63ad3f | 2024-06-18 11:42:35 -0700 | [diff] [blame] | 118 | this.propagateToToolchainsTypes = propagateToToolchainsTypes; |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 119 | this.configurationFragmentPolicy = configurationFragmentPolicy; |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 120 | this.applyToFiles = applyToFiles; |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 121 | this.applyToGeneratingRules = applyToGeneratingRules; |
messa | f8c3408 | 2021-06-04 02:51:04 -0700 | [diff] [blame] | 122 | this.requiredAspectClasses = requiredAspectClasses; |
Googler | 7e3755d | 2022-06-01 12:46:09 -0700 | [diff] [blame] | 123 | this.execCompatibleWith = execCompatibleWith; |
| 124 | this.execGroups = execGroups; |
Googler | aa55c4d | 2023-08-30 00:48:57 -0700 | [diff] [blame] | 125 | this.subrules = subrules; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | public String getName() { |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 129 | return aspectClass.getName(); |
| 130 | } |
| 131 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 132 | /** |
| 133 | * Returns the attributes of the aspect in the form of a String -> {@link Attribute} map. |
| 134 | * |
| 135 | * <p>All attributes are either implicit or late-bound. |
| 136 | */ |
| 137 | public ImmutableMap<String, Attribute> getAttributes() { |
| 138 | return attributes; |
| 139 | } |
| 140 | |
John Cater | 13263f7 | 2017-05-24 19:06:47 +0200 | [diff] [blame] | 141 | /** Returns the required toolchains declared by this aspect. */ |
John Cater | fb16270 | 2022-02-16 10:09:34 -0800 | [diff] [blame] | 142 | public ImmutableSet<ToolchainTypeRequirement> getToolchainTypes() { |
| 143 | return toolchainTypes; |
John Cater | 13263f7 | 2017-05-24 19:06:47 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 146 | /** |
Googler | 7e3755d | 2022-06-01 12:46:09 -0700 | [diff] [blame] | 147 | * Returns the constraint values that must be present on an execution platform for this aspect. |
| 148 | */ |
| 149 | public ImmutableSet<Label> execCompatibleWith() { |
| 150 | return execCompatibleWith; |
| 151 | } |
| 152 | |
| 153 | /** Returns the execution groups that this aspect can use when creating actions. */ |
| 154 | public ImmutableMap<String, ExecGroup> execGroups() { |
| 155 | return execGroups; |
| 156 | } |
| 157 | |
Googler | aa55c4d | 2023-08-30 00:48:57 -0700 | [diff] [blame] | 158 | /** Returns the subrules declared by this aspect. */ |
| 159 | public ImmutableSet<? extends StarlarkSubruleApi> getSubrules() { |
| 160 | return subrules; |
| 161 | } |
| 162 | |
Googler | 7e3755d | 2022-06-01 12:46:09 -0700 | [diff] [blame] | 163 | /** |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 164 | * Returns {@link RequiredProviders} that a configured target must have so that this aspect can be |
| 165 | * applied to it. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 166 | * |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 167 | * <p>If a configured target does not satisfy required providers, the aspect is silently not |
| 168 | * created for it. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 169 | */ |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 170 | public RequiredProviders getRequiredProviders() { |
| 171 | return requiredProviders; |
Eric Fellheimer | e27d063 | 2015-09-25 21:35:26 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 175 | * Aspects do not depend on other aspects applied to the same target <em>unless</em> the other |
| 176 | * aspect satisfies the {@link RequiredProviders} this method returns |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 177 | */ |
| 178 | public RequiredProviders getRequiredProvidersForAspects() { |
| 179 | return requiredProvidersForAspects; |
| 180 | } |
| 181 | |
messa | f8c3408 | 2021-06-04 02:51:04 -0700 | [diff] [blame] | 182 | /** Returns whether the aspect propagates along the give {@code attributeName} or not. */ |
lberki | 3bd2839 | 2019-02-05 01:01:02 -0800 | [diff] [blame] | 183 | public boolean propagateAlong(String attributeName) { |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 184 | if (restrictToAttributes != null) { |
lberki | 3bd2839 | 2019-02-05 01:01:02 -0800 | [diff] [blame] | 185 | return restrictToAttributes.contains(attributeName); |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 186 | } |
| 187 | return true; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Googler | b63ad3f | 2024-06-18 11:42:35 -0700 | [diff] [blame] | 190 | /** Returns whether the aspect propagates to toolchains of the given {@code toolchainType}. */ |
| 191 | public boolean canPropagateToToolchainType(Label toolchainType) { |
| 192 | return propagateToToolchainsTypes.contains(toolchainType); |
| 193 | } |
| 194 | |
Googler | 436a84f | 2024-07-11 14:57:39 -0700 | [diff] [blame] | 195 | /** Returns whether the aspect propagates to toolchains. */ |
| 196 | public boolean propagatesToToolchains() { |
| 197 | return !propagateToToolchainsTypes.isEmpty(); |
| 198 | } |
| 199 | |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 200 | /** Returns the set of configuration fragments required by this Aspect. */ |
Michael Staib | af27046 | 2016-05-18 16:22:07 +0000 | [diff] [blame] | 201 | public ConfigurationFragmentPolicy getConfigurationFragmentPolicy() { |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 202 | return configurationFragmentPolicy; |
| 203 | } |
| 204 | |
| 205 | /** |
dslomov | ce59d4d | 2017-08-04 17:32:47 +0200 | [diff] [blame] | 206 | * Returns whether this aspect applies to (output) files. |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 207 | * |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 208 | * <p>Currently only supported for top-level aspects and targets, and only for output files. |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 209 | */ |
| 210 | public boolean applyToFiles() { |
| 211 | return applyToFiles; |
| 212 | } |
| 213 | |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 214 | /** |
| 215 | * Returns whether this aspect should, when it would be applied to an output file, instead apply |
| 216 | * to the generating rule of that output file. |
| 217 | */ |
| 218 | public boolean applyToGeneratingRules() { |
| 219 | return applyToGeneratingRules; |
| 220 | } |
| 221 | |
nharmata | f2b26ad | 2019-03-06 09:37:00 -0800 | [diff] [blame] | 222 | public static boolean satisfies(Aspect aspect, AdvertisedProviderSet advertisedProviderSet) { |
jhorvitz | 9302ebd | 2021-06-28 11:09:06 -0700 | [diff] [blame] | 223 | return aspect.getDefinition().requiredProviders.isSatisfiedBy(advertisedProviderSet); |
Marian Lobur | fdd788e | 2015-03-25 09:36:28 +0000 | [diff] [blame] | 224 | } |
| 225 | |
messa | f8c3408 | 2021-06-04 02:51:04 -0700 | [diff] [blame] | 226 | /** Checks if the given {@code maybeRequiredAspect} is required by this aspect definition */ |
| 227 | public boolean requires(Aspect maybeRequiredAspect) { |
| 228 | return requiredAspectClasses.contains(maybeRequiredAspect.getAspectClass()); |
| 229 | } |
| 230 | |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 231 | /** Collects all attribute labels from the specified aspectDefinition. */ |
Dmitry Lomov | 6073eb6 | 2016-01-21 21:26:32 +0000 | [diff] [blame] | 232 | public static void addAllAttributesOfAspect( |
Googler | b21cf25 | 2023-03-20 16:57:40 -0700 | [diff] [blame] | 233 | Multimap<Attribute, Label> labelBuilder, Aspect aspect, DependencyFilter dependencyFilter) { |
| 234 | forEachLabelDepFromAllAttributesOfAspect(aspect, dependencyFilter, labelBuilder::put); |
nharmata | f2b26ad | 2019-03-06 09:37:00 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | public static void forEachLabelDepFromAllAttributesOfAspect( |
nharmata | f2b26ad | 2019-03-06 09:37:00 -0800 | [diff] [blame] | 238 | Aspect aspect, |
| 239 | DependencyFilter dependencyFilter, |
| 240 | BiConsumer<Attribute, Label> consumer) { |
jhorvitz | 00d9ba0 | 2021-06-29 08:38:18 -0700 | [diff] [blame] | 241 | LabelVisitor labelVisitor = |
shreyax | 2bcbecf | 2018-09-26 12:27:32 -0700 | [diff] [blame] | 242 | (label, aspectAttribute) -> { |
wyv | 103ea9d | 2021-12-09 04:52:37 -0800 | [diff] [blame] | 243 | if (label == null) { |
shreyax | 2bcbecf | 2018-09-26 12:27:32 -0700 | [diff] [blame] | 244 | return; |
| 245 | } |
wyv | 103ea9d | 2021-12-09 04:52:37 -0800 | [diff] [blame] | 246 | consumer.accept(aspectAttribute, label); |
shreyax | 2bcbecf | 2018-09-26 12:27:32 -0700 | [diff] [blame] | 247 | }; |
jhorvitz | 9302ebd | 2021-06-28 11:09:06 -0700 | [diff] [blame] | 248 | for (Attribute aspectAttribute : aspect.getDefinition().attributes.values()) { |
jhorvitz | 835f0a1 | 2021-07-02 06:14:33 -0700 | [diff] [blame] | 249 | if (!dependencyFilter.test(aspect, aspectAttribute)) { |
Marian Lobur | fdd788e | 2015-03-25 09:36:28 +0000 | [diff] [blame] | 250 | continue; |
| 251 | } |
nharmata | d705c98 | 2017-04-04 19:09:15 +0000 | [diff] [blame] | 252 | Type<?> type = aspectAttribute.getType(); |
Michael Staib | a751f92 | 2017-02-14 15:50:04 +0000 | [diff] [blame] | 253 | if (type.getLabelClass() != LabelClass.DEPENDENCY) { |
| 254 | continue; |
| 255 | } |
Googler | b21cf25 | 2023-03-20 16:57:40 -0700 | [diff] [blame] | 256 | visitSingleAttribute(aspectAttribute, aspectAttribute.getType(), labelVisitor); |
Marian Lobur | fdd788e | 2015-03-25 09:36:28 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
jhorvitz | 21e1659 | 2021-06-29 17:11:58 -0700 | [diff] [blame] | 260 | private static <T> void visitSingleAttribute( |
Googler | b21cf25 | 2023-03-20 16:57:40 -0700 | [diff] [blame] | 261 | Attribute attribute, Type<T> type, LabelVisitor labelVisitor) { |
Googler | 658b3a6 | 2023-08-11 10:42:53 -0700 | [diff] [blame] | 262 | type.visitLabels(labelVisitor, type.cast(attribute.getDefaultValue(null)), attribute); |
jhorvitz | 21e1659 | 2021-06-29 17:11:58 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Dmitry Lomov | 0c66dee | 2017-02-13 17:24:43 +0000 | [diff] [blame] | 265 | public static Builder builder(AspectClass aspectClass) { |
| 266 | return new Builder(aspectClass); |
| 267 | } |
| 268 | |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 269 | /** Builder class for {@link AspectDefinition}. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 270 | public static final class Builder { |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 271 | private final AspectClass aspectClass; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 272 | private final Map<String, Attribute> attributes = new LinkedHashMap<>(); |
Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 273 | private final AdvertisedProviderSet.Builder advertisedProviders = |
| 274 | AdvertisedProviderSet.builder(); |
jhorvitz | 9302ebd | 2021-06-28 11:09:06 -0700 | [diff] [blame] | 275 | private final RequiredProviders.Builder requiredProviders = |
| 276 | RequiredProviders.acceptAnyBuilder(); |
jhorvitz | 9302ebd | 2021-06-28 11:09:06 -0700 | [diff] [blame] | 277 | private final RequiredProviders.Builder requiredAspectProviders = |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 278 | RequiredProviders.acceptNoneBuilder(); |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 279 | @Nullable private LinkedHashSet<String> propagateAlongAttributes = new LinkedHashSet<>(); |
Googler | b63ad3f | 2024-06-18 11:42:35 -0700 | [diff] [blame] | 280 | private ImmutableSet<Label> propagateToToolchainsTypes = ImmutableSet.of(); |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 281 | private final ConfigurationFragmentPolicy.Builder configurationFragmentPolicy = |
| 282 | new ConfigurationFragmentPolicy.Builder(); |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 283 | private boolean applyToFiles = false; |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 284 | private boolean applyToGeneratingRules = false; |
John Cater | 6f2c3e2 | 2022-03-03 08:15:22 -0800 | [diff] [blame] | 285 | private final Set<ToolchainTypeRequirement> toolchainTypes = new HashSet<>(); |
messa | f8c3408 | 2021-06-04 02:51:04 -0700 | [diff] [blame] | 286 | private ImmutableSet<AspectClass> requiredAspectClasses = ImmutableSet.of(); |
Googler | 7e3755d | 2022-06-01 12:46:09 -0700 | [diff] [blame] | 287 | private ImmutableSet<Label> execCompatibleWith = ImmutableSet.of(); |
| 288 | private ImmutableMap<String, ExecGroup> execGroups = ImmutableMap.of(); |
Googler | aa55c4d | 2023-08-30 00:48:57 -0700 | [diff] [blame] | 289 | private ImmutableSet<? extends StarlarkSubruleApi> subrules = ImmutableSet.of(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 290 | |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 291 | public Builder(AspectClass aspectClass) { |
| 292 | this.aspectClass = aspectClass; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 293 | } |
| 294 | |
Googler | 90bf727 | 2022-12-21 14:24:13 -0800 | [diff] [blame] | 295 | @CanIgnoreReturnValue |
| 296 | public Builder requireProviders(RequiredProviders requiredProviders) { |
Googler | d32ea79 | 2024-01-19 13:54:03 -0800 | [diff] [blame] | 297 | this.requireStarlarkProviderSets(requiredProviders.getStarlarkProviders()); |
Googler | 90bf727 | 2022-12-21 14:24:13 -0800 | [diff] [blame] | 298 | return this; |
| 299 | } |
| 300 | |
| 301 | /** |
messa | 1bca1bd | 2021-05-14 00:48:19 -0700 | [diff] [blame] | 302 | * Asserts that this aspect can only be evaluated for rules that supply all of the providers |
| 303 | * from at least one set of required providers. |
| 304 | */ |
Kurt Alfred Kluever | 29e8e7d | 2022-07-04 06:50:48 -0700 | [diff] [blame] | 305 | @CanIgnoreReturnValue |
messa | 1bca1bd | 2021-05-14 00:48:19 -0700 | [diff] [blame] | 306 | public Builder requireStarlarkProviderSets( |
| 307 | Iterable<ImmutableSet<StarlarkProviderIdentifier>> providerSets) { |
| 308 | for (ImmutableSet<StarlarkProviderIdentifier> providerSet : providerSets) { |
| 309 | if (!providerSet.isEmpty()) { |
| 310 | requiredProviders.addStarlarkSet(providerSet); |
| 311 | } |
| 312 | } |
| 313 | return this; |
| 314 | } |
| 315 | |
| 316 | /** |
elenairina | aeb21bc | 2017-09-27 12:13:52 -0400 | [diff] [blame] | 317 | * Asserts that this aspect can only be evaluated for rules that supply all of the specified |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 318 | * Starlark providers. |
elenairina | aeb21bc | 2017-09-27 12:13:52 -0400 | [diff] [blame] | 319 | */ |
Kurt Alfred Kluever | 29e8e7d | 2022-07-04 06:50:48 -0700 | [diff] [blame] | 320 | @CanIgnoreReturnValue |
gregce | 773b95f | 2020-05-19 09:51:09 -0700 | [diff] [blame] | 321 | public Builder requireStarlarkProviders(StarlarkProviderIdentifier... starlarkProviders) { |
| 322 | requiredProviders.addStarlarkSet(ImmutableSet.copyOf(starlarkProviders)); |
elenairina | aeb21bc | 2017-09-27 12:13:52 -0400 | [diff] [blame] | 323 | return this; |
| 324 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 325 | |
gregce | db27113 | 2021-04-05 15:50:50 -0700 | [diff] [blame] | 326 | /** |
messa | f8c3408 | 2021-06-04 02:51:04 -0700 | [diff] [blame] | 327 | * Asserts that this aspect requires a list of aspects to be applied before it on the configured |
| 328 | * target. |
| 329 | */ |
Kurt Alfred Kluever | 29e8e7d | 2022-07-04 06:50:48 -0700 | [diff] [blame] | 330 | @CanIgnoreReturnValue |
messa | f8c3408 | 2021-06-04 02:51:04 -0700 | [diff] [blame] | 331 | public Builder requiredAspectClasses(ImmutableSet<AspectClass> requiredAspectClasses) { |
| 332 | this.requiredAspectClasses = requiredAspectClasses; |
| 333 | return this; |
| 334 | } |
| 335 | |
Kurt Alfred Kluever | 29e8e7d | 2022-07-04 06:50:48 -0700 | [diff] [blame] | 336 | @CanIgnoreReturnValue |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 337 | public Builder requireAspectsWithProviders( |
gregce | 74d84d4 | 2020-04-17 10:02:03 -0700 | [diff] [blame] | 338 | Iterable<ImmutableSet<StarlarkProviderIdentifier>> providerSets) { |
| 339 | for (ImmutableSet<StarlarkProviderIdentifier> providerSet : providerSets) { |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 340 | if (!providerSet.isEmpty()) { |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 341 | requiredAspectProviders.addStarlarkSet(providerSet); |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | return this; |
| 345 | } |
| 346 | |
gregce | 74d84d4 | 2020-04-17 10:02:03 -0700 | [diff] [blame] | 347 | /** State that the aspect being built provides given providers. */ |
Kurt Alfred Kluever | 29e8e7d | 2022-07-04 06:50:48 -0700 | [diff] [blame] | 348 | @CanIgnoreReturnValue |
gregce | 74d84d4 | 2020-04-17 10:02:03 -0700 | [diff] [blame] | 349 | public Builder advertiseProvider(ImmutableList<StarlarkProviderIdentifier> providers) { |
| 350 | for (StarlarkProviderIdentifier provider : providers) { |
gregce | d7c1cef | 2020-05-12 07:51:48 -0700 | [diff] [blame] | 351 | advertisedProviders.addStarlark(provider); |
Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 352 | } |
| 353 | return this; |
| 354 | } |
| 355 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 356 | /** |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 357 | * Declares that this aspect propagates along an {@code attribute} on the target associated with |
| 358 | * this aspect. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 359 | * |
jhorvitz | 9302ebd | 2021-06-28 11:09:06 -0700 | [diff] [blame] | 360 | * <p>Specify multiple attributes by calling this method repeatedly. |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 361 | * |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 362 | * <p>Aspect can also declare to propagate along all attributes with {@link |
| 363 | * #propagateAlongAttributes}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 364 | */ |
Kurt Alfred Kluever | 29e8e7d | 2022-07-04 06:50:48 -0700 | [diff] [blame] | 365 | @CanIgnoreReturnValue |
jhorvitz | 9302ebd | 2021-06-28 11:09:06 -0700 | [diff] [blame] | 366 | public Builder propagateAlongAttribute(String attribute) { |
Rumou Duan | 5b0b310 | 2015-06-03 20:27:57 +0000 | [diff] [blame] | 367 | Preconditions.checkNotNull(attribute); |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 368 | Preconditions.checkState( |
| 369 | this.propagateAlongAttributes != null, |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 370 | "Either propagate along all attributes, or along specific attributes, not both"); |
| 371 | |
| 372 | this.propagateAlongAttributes.add(attribute); |
| 373 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 374 | return this; |
| 375 | } |
| 376 | |
| 377 | /** |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 378 | * Declares that this aspect propagates along all attributes on the target associated with this |
| 379 | * aspect. |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 380 | * |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 381 | * <p>Specify either this or {@link #propagateAlongAttribute(String)}, not both. |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 382 | */ |
Kurt Alfred Kluever | 29e8e7d | 2022-07-04 06:50:48 -0700 | [diff] [blame] | 383 | @CanIgnoreReturnValue |
jhorvitz | 9302ebd | 2021-06-28 11:09:06 -0700 | [diff] [blame] | 384 | public Builder propagateAlongAllAttributes() { |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 385 | Preconditions.checkState( |
| 386 | this.propagateAlongAttributes != null, |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 387 | "Aspects for all attributes must only be specified once"); |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 388 | |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 389 | Preconditions.checkState( |
| 390 | this.propagateAlongAttributes.isEmpty(), |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 391 | "Specify either aspects for all attributes, or for specific attributes, not both"); |
| 392 | this.propagateAlongAttributes = null; |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 393 | return this; |
| 394 | } |
| 395 | |
Googler | b63ad3f | 2024-06-18 11:42:35 -0700 | [diff] [blame] | 396 | /** Declares that this aspect propagates to toolchains of the given types. */ |
| 397 | @CanIgnoreReturnValue |
| 398 | public Builder propagateToToolchainsTypes(ImmutableSet<Label> toolchainsTypes) { |
| 399 | this.propagateToToolchainsTypes = toolchainsTypes; |
| 400 | return this; |
| 401 | } |
| 402 | |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 403 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 404 | * Adds an attribute to the aspect. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 405 | */ |
| 406 | public <TYPE> Builder add(Attribute.Builder<TYPE> attr) { |
| 407 | Attribute attribute = attr.build(); |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 408 | return add(attribute); |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Adds an attribute to the aspect. |
| 413 | * |
Googler | 20e0482 | 2022-06-02 08:10:03 -0700 | [diff] [blame] | 414 | * <p>Aspects attributes can be of any data type if they are not public, i.e. implicit (starting |
| 415 | * with '$') or late-bound (starting with ':'). While public attributes can only be of types |
| 416 | * string, integer or boolean. |
ilist | d7432e2 | 2020-06-04 13:51:45 -0700 | [diff] [blame] | 417 | * |
| 418 | * <p>Aspect definition currently cannot handle {@link ComputedDefault} dependencies (type LABEL |
| 419 | * or LABEL_LIST), because all the dependencies are resolved from the aspect definition and the |
| 420 | * defining rule. |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 421 | */ |
Kurt Alfred Kluever | 29e8e7d | 2022-07-04 06:50:48 -0700 | [diff] [blame] | 422 | @CanIgnoreReturnValue |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 423 | public Builder add(Attribute attribute) { |
ilist | d7432e2 | 2020-06-04 13:51:45 -0700 | [diff] [blame] | 424 | Preconditions.checkArgument( |
| 425 | attribute.isImplicit() |
| 426 | || attribute.isLateBound() |
messa | 14292d1 | 2021-12-20 07:46:23 -0800 | [diff] [blame] | 427 | || (attribute.getType() == Type.STRING && attribute.checkAllowedValues()) |
messa | 30fd508 | 2021-12-22 05:39:13 -0800 | [diff] [blame] | 428 | || (attribute.getType() == Type.INTEGER && attribute.checkAllowedValues()) |
| 429 | || attribute.getType() == Type.BOOLEAN, |
ilist | d7432e2 | 2020-06-04 13:51:45 -0700 | [diff] [blame] | 430 | "%s: Invalid attribute '%s' (%s)", |
| 431 | aspectClass.getName(), |
| 432 | attribute.getName(), |
| 433 | attribute.getType()); |
| 434 | |
| 435 | // Attributes specifying dependencies using ComputedDefault value are currently not supported. |
| 436 | // The limitation is in place because: |
| 437 | // - blaze query requires that all possible values are knowable without BuildConguration |
| 438 | // - aspects can attach to any rule |
| 439 | // Current logic in #forEachLabelDepFromAllAttributesOfAspect is not enough, |
| 440 | // however {Conservative,Precise}AspectResolver can probably be improved to make that work. |
| 441 | Preconditions.checkArgument( |
| 442 | !(attribute.getType().getLabelClass() == LabelClass.DEPENDENCY |
| 443 | && (attribute.getDefaultValueUnchecked() instanceof ComputedDefault)), |
| 444 | "%s: Invalid attribute '%s' (%s) with computed default dependencies", |
| 445 | aspectClass.getName(), |
| 446 | attribute.getName(), |
| 447 | attribute.getType()); |
| 448 | Preconditions.checkArgument( |
| 449 | !attributes.containsKey(attribute.getName()), |
| 450 | "%s: An attribute with the name '%s' already exists.", |
| 451 | aspectClass.getName(), |
| 452 | attribute.getName()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 453 | attributes.put(attribute.getName(), attribute); |
| 454 | return this; |
| 455 | } |
| 456 | |
| 457 | /** |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 458 | * Declares that the implementation of the associated aspect definition requires the given |
Googler | 92f0d6a | 2023-01-06 09:45:16 -0800 | [diff] [blame] | 459 | * fragments to be present in this rule's exec and target configurations. |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 460 | * |
| 461 | * <p>The value is inherited by subclasses. |
| 462 | */ |
Kurt Alfred Kluever | 29e8e7d | 2022-07-04 06:50:48 -0700 | [diff] [blame] | 463 | @CanIgnoreReturnValue |
jhorvitz | 11dc104 | 2021-08-13 07:30:19 -0700 | [diff] [blame] | 464 | public Builder requiresConfigurationFragments( |
| 465 | Class<? extends Fragment>... configurationFragments) { |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 466 | configurationFragmentPolicy.requiresConfigurationFragments( |
| 467 | ImmutableSet.copyOf(configurationFragments)); |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 468 | return this; |
| 469 | } |
| 470 | |
| 471 | /** |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 472 | * Declares the configuration fragments that are required by this rule for the target |
| 473 | * configuration. |
| 474 | * |
| 475 | * <p>In contrast to {@link #requiresConfigurationFragments(Class...)}, this method takes the |
gregce | 3377c11 | 2020-04-13 09:29:59 -0700 | [diff] [blame] | 476 | * Starlark module names of fragments instead of their classes. |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 477 | */ |
Kurt Alfred Kluever | 29e8e7d | 2022-07-04 06:50:48 -0700 | [diff] [blame] | 478 | @CanIgnoreReturnValue |
gregce | 220a1c8 | 2020-05-01 10:19:52 -0700 | [diff] [blame] | 479 | public Builder requiresConfigurationFragmentsByStarlarkBuiltinName( |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 480 | Collection<String> configurationFragmentNames) { |
gregce | 220a1c8 | 2020-05-01 10:19:52 -0700 | [diff] [blame] | 481 | configurationFragmentPolicy.requiresConfigurationFragmentsByStarlarkBuiltinName( |
| 482 | configurationFragmentNames); |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 483 | return this; |
| 484 | } |
| 485 | |
| 486 | /** |
twigg | 8e9f9e1 | 2020-09-17 09:07:58 -0700 | [diff] [blame] | 487 | * Sets the policy for the case where the configuration is missing the required fragment class |
| 488 | * (see {@link #requiresConfigurationFragments}). |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 489 | */ |
Kurt Alfred Kluever | 29e8e7d | 2022-07-04 06:50:48 -0700 | [diff] [blame] | 490 | @CanIgnoreReturnValue |
twigg | 8e9f9e1 | 2020-09-17 09:07:58 -0700 | [diff] [blame] | 491 | public Builder setMissingFragmentPolicy( |
| 492 | Class<?> fragmentClass, MissingFragmentPolicy missingFragmentPolicy) { |
| 493 | configurationFragmentPolicy.setMissingFragmentPolicy(fragmentClass, missingFragmentPolicy); |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 494 | return this; |
| 495 | } |
| 496 | |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 497 | /** |
| 498 | * Sets whether this aspect should apply to files. |
| 499 | * |
messa | 4fc24bc | 2021-05-20 01:42:18 -0700 | [diff] [blame] | 500 | * <p>Default is <code>false</code>. Currently only supported for top-level aspects and targets, |
| 501 | * and only for output files. |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 502 | */ |
Kurt Alfred Kluever | 29e8e7d | 2022-07-04 06:50:48 -0700 | [diff] [blame] | 503 | @CanIgnoreReturnValue |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 504 | public Builder applyToFiles(boolean propagateOverGeneratedFiles) { |
| 505 | this.applyToFiles = propagateOverGeneratedFiles; |
| 506 | return this; |
| 507 | } |
| 508 | |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 509 | /** |
| 510 | * Sets whether this aspect should, when it would be applied to an output file, instead apply to |
| 511 | * the generating rule of that output file. |
| 512 | * |
| 513 | * <p>Default is <code>false</code>. Currently only supported for aspects which do not have a |
| 514 | * "required providers" list. |
| 515 | */ |
Kurt Alfred Kluever | 29e8e7d | 2022-07-04 06:50:48 -0700 | [diff] [blame] | 516 | @CanIgnoreReturnValue |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 517 | public Builder applyToGeneratingRules(boolean applyToGeneratingRules) { |
| 518 | this.applyToGeneratingRules = applyToGeneratingRules; |
| 519 | return this; |
| 520 | } |
| 521 | |
John Cater | 13263f7 | 2017-05-24 19:06:47 +0200 | [diff] [blame] | 522 | /** Adds the given toolchains as requirements for this aspect. */ |
John Cater | 6f2c3e2 | 2022-03-03 08:15:22 -0800 | [diff] [blame] | 523 | public Builder addToolchainTypes(ToolchainTypeRequirement... toolchainTypes) { |
| 524 | return this.addToolchainTypes(ImmutableSet.copyOf(toolchainTypes)); |
John Cater | fb16270 | 2022-02-16 10:09:34 -0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /** Adds the given toolchains as requirements for this aspect. */ |
Kurt Alfred Kluever | 29e8e7d | 2022-07-04 06:50:48 -0700 | [diff] [blame] | 528 | @CanIgnoreReturnValue |
John Cater | 6f2c3e2 | 2022-03-03 08:15:22 -0800 | [diff] [blame] | 529 | public Builder addToolchainTypes(Collection<ToolchainTypeRequirement> toolchainTypes) { |
John Cater | fb16270 | 2022-02-16 10:09:34 -0800 | [diff] [blame] | 530 | this.toolchainTypes.addAll(toolchainTypes); |
jcater | 7582875 | 2018-04-27 12:53:19 -0700 | [diff] [blame] | 531 | return this; |
| 532 | } |
| 533 | |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 534 | /** |
Googler | 7e3755d | 2022-06-01 12:46:09 -0700 | [diff] [blame] | 535 | * Adds the given constraint values to the set required for execution platforms for this aspect. |
| 536 | */ |
Kurt Alfred Kluever | 29e8e7d | 2022-07-04 06:50:48 -0700 | [diff] [blame] | 537 | @CanIgnoreReturnValue |
Googler | 7e3755d | 2022-06-01 12:46:09 -0700 | [diff] [blame] | 538 | public Builder execCompatibleWith(ImmutableSet<Label> execCompatibleWith) { |
| 539 | this.execCompatibleWith = execCompatibleWith; |
| 540 | return this; |
| 541 | } |
| 542 | |
| 543 | /** Sets the execution groups that are available for actions created by this aspect. */ |
Kurt Alfred Kluever | 29e8e7d | 2022-07-04 06:50:48 -0700 | [diff] [blame] | 544 | @CanIgnoreReturnValue |
Googler | 7e3755d | 2022-06-01 12:46:09 -0700 | [diff] [blame] | 545 | public Builder execGroups(ImmutableMap<String, ExecGroup> execGroups) { |
| 546 | // TODO(b/230337573): validate names |
| 547 | // TODO(b/230337573): handle copy_from_default |
| 548 | this.execGroups = execGroups; |
| 549 | return this; |
| 550 | } |
| 551 | |
Googler | aa55c4d | 2023-08-30 00:48:57 -0700 | [diff] [blame] | 552 | @CanIgnoreReturnValue |
| 553 | public Builder subrules(ImmutableSet<? extends StarlarkSubruleApi> subrules) { |
| 554 | this.subrules = subrules; |
| 555 | return this; |
| 556 | } |
| 557 | |
Googler | 7e3755d | 2022-06-01 12:46:09 -0700 | [diff] [blame] | 558 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 559 | * Builds the aspect definition. |
| 560 | * |
| 561 | * <p>The builder object is reusable afterwards. |
| 562 | */ |
| 563 | public AspectDefinition build() { |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 564 | RequiredProviders requiredProviders = this.requiredProviders.build(); |
| 565 | if (applyToGeneratingRules && !requiredProviders.acceptsAny()) { |
| 566 | throw new IllegalStateException( |
| 567 | "An aspect cannot simultaneously have required providers " |
| 568 | + "and apply to generating rules."); |
| 569 | } |
| 570 | |
John Cater | 13263f7 | 2017-05-24 19:06:47 +0200 | [diff] [blame] | 571 | return new AspectDefinition( |
| 572 | aspectClass, |
Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 573 | advertisedProviders.build(), |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 574 | requiredProviders, |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 575 | requiredAspectProviders.build(), |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 576 | ImmutableMap.copyOf(attributes), |
John Cater | fb16270 | 2022-02-16 10:09:34 -0800 | [diff] [blame] | 577 | ImmutableSet.copyOf(toolchainTypes), |
John Cater | 13263f7 | 2017-05-24 19:06:47 +0200 | [diff] [blame] | 578 | propagateAlongAttributes == null ? null : ImmutableSet.copyOf(propagateAlongAttributes), |
Googler | b63ad3f | 2024-06-18 11:42:35 -0700 | [diff] [blame] | 579 | propagateToToolchainsTypes, |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 580 | configurationFragmentPolicy.build(), |
cparsons | 089148b | 2019-09-17 08:14:41 -0700 | [diff] [blame] | 581 | applyToFiles, |
gregce | db27113 | 2021-04-05 15:50:50 -0700 | [diff] [blame] | 582 | applyToGeneratingRules, |
Googler | 7e3755d | 2022-06-01 12:46:09 -0700 | [diff] [blame] | 583 | requiredAspectClasses, |
| 584 | execCompatibleWith, |
Googler | aa55c4d | 2023-08-30 00:48:57 -0700 | [diff] [blame] | 585 | execGroups, |
| 586 | subrules); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | } |