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.analysis; |
| 16 | |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 17 | import static com.google.common.collect.ImmutableList.toImmutableList; |
Austin Schuh | d181353 | 2023-02-14 22:48:22 -0800 | [diff] [blame] | 18 | import static com.google.devtools.build.lib.analysis.test.ExecutionInfo.DEFAULT_TEST_RUNNER_EXEC_GROUP; |
jcater | 64534e2 | 2021-05-06 07:56:27 -0700 | [diff] [blame] | 19 | import static com.google.devtools.build.lib.packages.ExecGroup.DEFAULT_EXEC_GROUP_NAME; |
juliexxia | ff58081 | 2020-04-30 15:01:09 -0700 | [diff] [blame] | 20 | |
Janak Ramakrishnan | 7857c79 | 2016-03-21 08:35:35 +0000 | [diff] [blame] | 21 | import com.google.common.annotations.VisibleForTesting; |
Yun Peng | 5c34e96 | 2016-02-22 15:13:19 +0000 | [diff] [blame] | 22 | import com.google.common.base.Joiner; |
Googler | 59480b9 | 2016-07-22 17:06:40 +0000 | [diff] [blame] | 23 | import com.google.common.base.Optional; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 24 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.common.base.Predicate; |
Yun Peng | efd7ca1 | 2016-03-03 13:14:38 +0000 | [diff] [blame] | 26 | import com.google.common.base.Predicates; |
Florian Weikert | 082c054 | 2015-08-06 10:24:29 +0000 | [diff] [blame] | 27 | import com.google.common.collect.ImmutableCollection; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | import com.google.common.collect.ImmutableList; |
| 29 | import com.google.common.collect.ImmutableListMultimap; |
| 30 | import com.google.common.collect.ImmutableMap; |
| 31 | import com.google.common.collect.ImmutableSet; |
Manuel Klimek | 6d9fb36 | 2015-04-30 12:50:55 +0000 | [diff] [blame] | 32 | import com.google.common.collect.ImmutableSortedSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | import com.google.common.collect.Iterables; |
| 34 | import com.google.common.collect.ListMultimap; |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 35 | import com.google.common.collect.Lists; |
mjhalupka | de47f21 | 2018-02-12 12:31:21 -0800 | [diff] [blame] | 36 | import com.google.common.collect.Maps; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | import com.google.common.collect.Multimaps; |
| 38 | import com.google.common.collect.Sets; |
Googler | 3834975 | 2023-01-11 09:02:30 -0800 | [diff] [blame] | 39 | import com.google.common.collect.Streams; |
Rumou Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 40 | import com.google.devtools.build.lib.actions.ActionAnalysisMetadata; |
jhorvitz | 3daedc3 | 2020-07-22 18:33:55 -0700 | [diff] [blame] | 41 | import com.google.devtools.build.lib.actions.ActionLookupKey; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 42 | import com.google.devtools.build.lib.actions.ActionOwner; |
| 43 | import com.google.devtools.build.lib.actions.ActionRegistry; |
| 44 | import com.google.devtools.build.lib.actions.Artifact; |
cpeyser | ac09f0a | 2018-02-05 09:33:15 -0800 | [diff] [blame] | 45 | import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact; |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 46 | import com.google.devtools.build.lib.actions.ArtifactRoot; |
lberki | ee2dcae | 2018-03-28 05:49:04 -0700 | [diff] [blame] | 47 | import com.google.devtools.build.lib.analysis.AliasProvider.TargetMode; |
jcater | 645c42b | 2021-05-12 09:35:48 -0700 | [diff] [blame] | 48 | import com.google.devtools.build.lib.analysis.ExecGroupCollection.InvalidExecGroupException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 49 | import com.google.devtools.build.lib.analysis.actions.ActionConstructionContext; |
jcater | 1865d89 | 2020-04-14 10:04:29 -0700 | [diff] [blame] | 50 | import com.google.devtools.build.lib.analysis.buildinfo.BuildInfoKey; |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 51 | import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue; |
gregce | 79989f9 | 2021-02-01 07:01:55 -0800 | [diff] [blame] | 52 | import com.google.devtools.build.lib.analysis.config.ConfigConditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | import com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider; |
jcater | a666f00 | 2020-04-14 14:55:20 -0700 | [diff] [blame] | 54 | import com.google.devtools.build.lib.analysis.config.Fragment; |
jcater | 0a1e9eb | 2019-12-17 09:58:38 -0800 | [diff] [blame] | 55 | import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo; |
John Cater | 15c90b3 | 2017-12-18 08:34:40 -0800 | [diff] [blame] | 56 | import com.google.devtools.build.lib.analysis.platform.PlatformInfo; |
Googler | 928e0fe | 2023-01-18 02:31:45 -0800 | [diff] [blame] | 57 | import com.google.devtools.build.lib.analysis.platform.ToolchainInfo; |
brandjon | f1cc4dd | 2021-02-01 08:53:40 -0800 | [diff] [blame] | 58 | import com.google.devtools.build.lib.analysis.starlark.StarlarkRuleContext; |
ulfjack | 01bf32e | 2017-11-02 17:50:07 -0400 | [diff] [blame] | 59 | import com.google.devtools.build.lib.analysis.stringtemplate.TemplateContext; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 60 | import com.google.devtools.build.lib.cmdline.Label; |
dslomov | fd62e76 | 2017-09-19 16:55:53 +0200 | [diff] [blame] | 61 | import com.google.devtools.build.lib.cmdline.RepositoryName; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 62 | import com.google.devtools.build.lib.collect.ImmutableSortedKeyListMultimap; |
| 63 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 64 | import com.google.devtools.build.lib.events.Event; |
dslomov | 44d1571 | 2017-12-20 05:42:28 -0800 | [diff] [blame] | 65 | import com.google.devtools.build.lib.packages.Aspect; |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 66 | import com.google.devtools.build.lib.packages.AspectDescriptor; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 67 | import com.google.devtools.build.lib.packages.Attribute; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 68 | import com.google.devtools.build.lib.packages.AttributeMap; |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 69 | import com.google.devtools.build.lib.packages.BazelStarlarkContext; |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 70 | import com.google.devtools.build.lib.packages.BuildType; |
cparsons | 2d67cf9 | 2018-05-24 14:02:09 -0700 | [diff] [blame] | 71 | import com.google.devtools.build.lib.packages.BuiltinProvider; |
Michael Staib | b51251e | 2015-09-29 23:31:51 +0000 | [diff] [blame] | 72 | import com.google.devtools.build.lib.packages.ConfigurationFragmentPolicy; |
cparsons | 7892779 | 2017-10-11 00:14:19 +0200 | [diff] [blame] | 73 | import com.google.devtools.build.lib.packages.ConfiguredAttributeMapper; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 74 | import com.google.devtools.build.lib.packages.ImplicitOutputsFunction; |
dslomov | de965ac | 2017-07-31 21:07:51 +0200 | [diff] [blame] | 75 | import com.google.devtools.build.lib.packages.Info; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 76 | import com.google.devtools.build.lib.packages.OutputFile; |
Googler | 720dc5f | 2022-09-16 03:43:51 -0700 | [diff] [blame] | 77 | import com.google.devtools.build.lib.packages.Package; |
gregce | 79989f9 | 2021-02-01 07:01:55 -0800 | [diff] [blame] | 78 | import com.google.devtools.build.lib.packages.Package.ConfigSettingVisibilityPolicy; |
cushon | 34ff85e | 2017-11-15 08:59:27 -0800 | [diff] [blame] | 79 | import com.google.devtools.build.lib.packages.PackageSpecification.PackageGroupContents; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 80 | import com.google.devtools.build.lib.packages.RawAttributeMapper; |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 81 | import com.google.devtools.build.lib.packages.RequiredProviders; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 82 | import com.google.devtools.build.lib.packages.Rule; |
| 83 | import com.google.devtools.build.lib.packages.RuleClass; |
brandjon | f1cc4dd | 2021-02-01 08:53:40 -0800 | [diff] [blame] | 84 | import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException; |
Googler | 995ef63 | 2019-09-04 12:25:41 -0700 | [diff] [blame] | 85 | import com.google.devtools.build.lib.packages.SymbolGenerator; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 86 | import com.google.devtools.build.lib.packages.Target; |
| 87 | import com.google.devtools.build.lib.packages.TargetUtils; |
Googler | c5fcc86 | 2019-09-06 16:17:47 -0700 | [diff] [blame] | 88 | import com.google.devtools.build.lib.packages.Type; |
| 89 | import com.google.devtools.build.lib.packages.Type.LabelClass; |
adonovan | 028e1ad | 2020-09-07 07:09:59 -0700 | [diff] [blame] | 90 | import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions; |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 91 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndData; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 92 | import com.google.devtools.build.lib.util.FileTypeSet; |
cparsons | 0623d3c | 2018-10-23 16:06:30 -0700 | [diff] [blame] | 93 | import com.google.devtools.build.lib.util.OS; |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 94 | import com.google.devtools.build.lib.util.OrderedSetMultimap; |
Googler | e49cd59 | 2016-07-29 19:32:38 +0000 | [diff] [blame] | 95 | import com.google.devtools.build.lib.util.StringUtil; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 96 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
| 97 | import com.google.devtools.build.lib.vfs.PathFragment; |
Kurt Alfred Kluever | 4192ca6 | 2022-07-05 06:30:29 -0700 | [diff] [blame] | 98 | import com.google.errorprone.annotations.CanIgnoreReturnValue; |
gregce | 31e4479 | 2020-04-27 09:10:40 -0700 | [diff] [blame] | 99 | import java.util.ArrayList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 100 | import java.util.Collection; |
| 101 | import java.util.HashMap; |
| 102 | import java.util.HashSet; |
Dmitry Lomov | fd2bdc3 | 2016-10-07 08:52:10 +0000 | [diff] [blame] | 103 | import java.util.LinkedHashSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 104 | import java.util.List; |
| 105 | import java.util.Map; |
| 106 | import java.util.Set; |
dslomov | 44d1571 | 2017-12-20 05:42:28 -0800 | [diff] [blame] | 107 | import java.util.stream.Collectors; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 108 | import javax.annotation.Nullable; |
adonovan | 450c7ad | 2020-09-14 13:00:21 -0700 | [diff] [blame] | 109 | import net.starlark.java.eval.EvalException; |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 110 | import net.starlark.java.eval.Mutability; |
brandjon | 4f331eb | 2021-02-01 09:09:39 -0800 | [diff] [blame] | 111 | import net.starlark.java.eval.Starlark; |
adonovan | 450c7ad | 2020-09-14 13:00:21 -0700 | [diff] [blame] | 112 | import net.starlark.java.eval.StarlarkSemantics; |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 113 | import net.starlark.java.eval.StarlarkThread; |
adonovan | 450c7ad | 2020-09-14 13:00:21 -0700 | [diff] [blame] | 114 | import net.starlark.java.syntax.Location; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 115 | |
| 116 | /** |
Lukacs Berki | 2300cd6 | 2016-05-19 11:06:37 +0000 | [diff] [blame] | 117 | * The totality of data available during the analysis of a rule. |
Janak Ramakrishnan | 81c5bd8 | 2016-03-22 20:07:43 +0000 | [diff] [blame] | 118 | * |
jcater | 02ce2a6 | 2020-04-07 08:06:00 -0700 | [diff] [blame] | 119 | * <p>These objects should not outlast the analysis phase. Do not pass them to {@link |
| 120 | * com.google.devtools.build.lib.actions.Action} objects or other persistent objects. There are |
| 121 | * internal tests to ensure that RuleContext objects are not persisted that check the name of this |
| 122 | * class, so update those tests if you change this class's name. |
Lukacs Berki | 2300cd6 | 2016-05-19 11:06:37 +0000 | [diff] [blame] | 123 | * |
ulfjack | 26d0e49 | 2017-08-07 13:42:33 +0200 | [diff] [blame] | 124 | * @see com.google.devtools.build.lib.analysis.RuleConfiguredTargetFactory |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 125 | */ |
| 126 | public final class RuleContext extends TargetContext |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 127 | implements ActionConstructionContext, ActionRegistry, RuleErrorConsumer, AutoCloseable { |
Carmi Grushko | 33aa306 | 2016-11-11 02:45:29 +0000 | [diff] [blame] | 128 | |
jcater | 0de1097 | 2020-04-07 12:15:05 -0700 | [diff] [blame] | 129 | /** Custom dependency validation logic. */ |
| 130 | public interface PrerequisiteValidator { |
| 131 | /** |
| 132 | * Checks whether the rule in {@code contextBuilder} is allowed to depend on {@code |
| 133 | * prerequisite} through the attribute {@code attribute}. |
| 134 | * |
| 135 | * <p>Can be used for enforcing any organization-specific policies about the layout of the |
| 136 | * workspace. |
| 137 | */ |
| 138 | void validate( |
| 139 | Builder contextBuilder, ConfiguredTargetAndData prerequisite, Attribute attribute); |
| 140 | } |
| 141 | |
ilist | 67c9936 | 2022-01-04 02:39:00 -0800 | [diff] [blame] | 142 | private static final String TOOL_CONFIGURATION_PROGRESS_TAG = "for tool"; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 143 | |
| 144 | private final Rule rule; |
dslomov | 44d1571 | 2017-12-20 05:42:28 -0800 | [diff] [blame] | 145 | /** |
brandjon | f1cc4dd | 2021-02-01 08:53:40 -0800 | [diff] [blame] | 146 | * A list of all aspects applied to the target. If this {@code RuleContext} is for a rule |
| 147 | * implementation, {@code aspects} is an empty list. |
dslomov | 44d1571 | 2017-12-20 05:42:28 -0800 | [diff] [blame] | 148 | * |
brandjon | f1cc4dd | 2021-02-01 08:53:40 -0800 | [diff] [blame] | 149 | * <p>Otherwise, the last aspect in the list is the one that this {@code RuleContext} is for. |
dslomov | 44d1571 | 2017-12-20 05:42:28 -0800 | [diff] [blame] | 150 | */ |
| 151 | private final ImmutableList<Aspect> aspects; |
brandjon | f1cc4dd | 2021-02-01 08:53:40 -0800 | [diff] [blame] | 152 | |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 153 | private final ImmutableList<AspectDescriptor> aspectDescriptors; |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 154 | private final ListMultimap<String, ConfiguredTargetAndData> targetMap; |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 155 | private final ImmutableMap<Label, ConfigMatchingProvider> configConditions; |
Greg Estren | 7f53423 | 2016-12-01 21:38:25 +0000 | [diff] [blame] | 156 | private final AspectAwareAttributeMapper attributes; |
Googler | 993f1de | 2018-02-20 02:31:10 -0800 | [diff] [blame] | 157 | private final ImmutableSet<String> enabledFeatures; |
| 158 | private final ImmutableSet<String> disabledFeatures; |
Michael Staib | 8824d5e | 2016-01-20 21:37:05 +0000 | [diff] [blame] | 159 | private final String ruleClassNameForLogging; |
Michael Staib | b51251e | 2015-09-29 23:31:51 +0000 | [diff] [blame] | 160 | private final ConfigurationFragmentPolicy configurationFragmentPolicy; |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 161 | private final ConfiguredRuleClassProvider ruleClassProvider; |
cparsons | e8d450c | 2018-10-04 16:01:53 -0700 | [diff] [blame] | 162 | private final RuleErrorConsumer reporter; |
juliexxia | b5f9d74 | 2020-04-09 13:07:13 -0700 | [diff] [blame] | 163 | @Nullable private final ToolchainCollection<ResolvedToolchainContext> toolchainContexts; |
jcater | 645c42b | 2021-05-12 09:35:48 -0700 | [diff] [blame] | 164 | private final ExecGroupCollection execGroupCollection; |
jhorvitz | 8128f52 | 2021-08-18 17:33:43 -0700 | [diff] [blame] | 165 | @Nullable private final RequiredConfigFragmentsProvider requiredConfigFragments; |
Googler | 720dc5f | 2022-09-16 03:43:51 -0700 | [diff] [blame] | 166 | @Nullable private final NestedSet<Package> transitivePackagesForRunfileRepoMappingManifest; |
gregce | 31e4479 | 2020-04-27 09:10:40 -0700 | [diff] [blame] | 167 | private final List<Expander> makeVariableExpanders = new ArrayList<>(); |
Michajlo Matijkiw | e64bfce | 2016-07-18 14:37:08 +0000 | [diff] [blame] | 168 | |
juliexxia | ff58081 | 2020-04-30 15:01:09 -0700 | [diff] [blame] | 169 | /** Map of exec group names to ActionOwners. */ |
| 170 | private final Map<String, ActionOwner> actionOwners = new HashMap<>(); |
| 171 | |
jhorvitz | 3daedc3 | 2020-07-22 18:33:55 -0700 | [diff] [blame] | 172 | private final SymbolGenerator<ActionLookupKey> actionOwnerSymbolGenerator; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 173 | |
| 174 | /* lazily computed cache for Make variables, computed from the above. See get... method */ |
| 175 | private transient ConfigurationMakeVariableContext configurationMakeVariableContext = null; |
| 176 | |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 177 | /** |
| 178 | * Thread used for any Starlark evaluation during analysis, e.g. rule implementation function for |
| 179 | * a Starlark-defined rule, or Starlarkified helper logic for native rules that have been |
| 180 | * partially migrated to {@code @_builtins}. |
| 181 | */ |
| 182 | private final StarlarkThread starlarkThread; |
brandjon | f1cc4dd | 2021-02-01 08:53:40 -0800 | [diff] [blame] | 183 | /** |
| 184 | * The {@code ctx} object passed to a Starlark-defined rule's or aspect's implementation function. |
| 185 | * This object may outlive the analysis phase, e.g. if it is returned in a provider. |
| 186 | * |
| 187 | * <p>Initialized explicitly by calling {@link #initStarlarkRuleContext}. Native rules that do not |
| 188 | * pass this object to {@code @_builtins} might avoid the cost of initializing this object, but |
| 189 | * for everyone else it's mandatory. |
| 190 | */ |
| 191 | @Nullable private StarlarkRuleContext starlarkRuleContext; |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 192 | |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 193 | private RuleContext( |
| 194 | Builder builder, |
Nathan Harmata | fcb1711 | 2016-04-13 16:56:58 +0000 | [diff] [blame] | 195 | AttributeMap attributes, |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 196 | ListMultimap<String, ConfiguredTargetAndData> targetMap, |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 197 | ExecGroupCollection execGroupCollection) { |
lpino | 5fe7ed0 | 2018-07-18 05:55:23 -0700 | [diff] [blame] | 198 | super( |
| 199 | builder.env, |
| 200 | builder.target.getAssociatedRule(), |
| 201 | builder.configuration, |
| 202 | builder.prerequisiteMap.get(null), |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 203 | builder.visibility); |
lpino | 5fe7ed0 | 2018-07-18 05:55:23 -0700 | [diff] [blame] | 204 | this.rule = builder.target.getAssociatedRule(); |
dslomov | 44d1571 | 2017-12-20 05:42:28 -0800 | [diff] [blame] | 205 | this.aspects = builder.aspects; |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 206 | this.aspectDescriptors = aspects.stream().map(Aspect::getDescriptor).collect(toImmutableList()); |
Michael Staib | b51251e | 2015-09-29 23:31:51 +0000 | [diff] [blame] | 207 | this.configurationFragmentPolicy = builder.configurationFragmentPolicy; |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 208 | this.ruleClassProvider = builder.ruleClassProvider; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 209 | this.targetMap = targetMap; |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 210 | this.configConditions = builder.configConditions.asProviders(); |
brandjon | 45ce545 | 2022-02-02 11:52:06 -0800 | [diff] [blame] | 211 | this.attributes = new AspectAwareAttributeMapper(attributes, builder.aspectAttributes); |
Googler | 993f1de | 2018-02-20 02:31:10 -0800 | [diff] [blame] | 212 | Set<String> allEnabledFeatures = new HashSet<>(); |
| 213 | Set<String> allDisabledFeatures = new HashSet<>(); |
| 214 | getAllFeatures(allEnabledFeatures, allDisabledFeatures); |
| 215 | this.enabledFeatures = ImmutableSortedSet.copyOf(allEnabledFeatures); |
| 216 | this.disabledFeatures = ImmutableSortedSet.copyOf(allDisabledFeatures); |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 217 | this.ruleClassNameForLogging = builder.getRuleClassNameForLogging(); |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 218 | this.actionOwnerSymbolGenerator = new SymbolGenerator<>(builder.actionOwnerSymbol); |
| 219 | this.reporter = builder.reporter; |
| 220 | this.toolchainContexts = builder.toolchainContexts; |
jcater | 645c42b | 2021-05-12 09:35:48 -0700 | [diff] [blame] | 221 | this.execGroupCollection = execGroupCollection; |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 222 | this.requiredConfigFragments = builder.requiredConfigFragments; |
Googler | 720dc5f | 2022-09-16 03:43:51 -0700 | [diff] [blame] | 223 | this.transitivePackagesForRunfileRepoMappingManifest = |
| 224 | builder.transitivePackagesForRunfileRepoMappingManifest; |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 225 | this.starlarkThread = createStarlarkThread(builder.mutability); // uses above state |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Googler | 993f1de | 2018-02-20 02:31:10 -0800 | [diff] [blame] | 228 | private void getAllFeatures(Set<String> allEnabledFeatures, Set<String> allDisabledFeatures) { |
Manuel Klimek | 6d9fb36 | 2015-04-30 12:50:55 +0000 | [diff] [blame] | 229 | Set<String> globallyEnabled = new HashSet<>(); |
| 230 | Set<String> globallyDisabled = new HashSet<>(); |
| 231 | parseFeatures(getConfiguration().getDefaultFeatures(), globallyEnabled, globallyDisabled); |
| 232 | Set<String> packageEnabled = new HashSet<>(); |
| 233 | Set<String> packageDisabled = new HashSet<>(); |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 234 | parseFeatures(rule.getPackage().getFeatures(), packageEnabled, packageDisabled); |
Googler | 8e3afcc | 2017-12-20 08:10:21 -0800 | [diff] [blame] | 235 | Set<String> ruleEnabled = new HashSet<>(); |
| 236 | Set<String> ruleDisabled = new HashSet<>(); |
| 237 | if (attributes().has("features", Type.STRING_LIST)) { |
| 238 | parseFeatures(attributes().get("features", Type.STRING_LIST), ruleEnabled, ruleDisabled); |
| 239 | } |
Googler | 993f1de | 2018-02-20 02:31:10 -0800 | [diff] [blame] | 240 | |
Googler | 8e3afcc | 2017-12-20 08:10:21 -0800 | [diff] [blame] | 241 | Set<String> ruleDisabledFeatures = |
| 242 | Sets.union(ruleDisabled, Sets.difference(packageDisabled, ruleEnabled)); |
Googler | 993f1de | 2018-02-20 02:31:10 -0800 | [diff] [blame] | 243 | allDisabledFeatures.addAll(Sets.union(ruleDisabledFeatures, globallyDisabled)); |
Googler | 993f1de | 2018-02-20 02:31:10 -0800 | [diff] [blame] | 244 | |
Manuel Klimek | 6d9fb36 | 2015-04-30 12:50:55 +0000 | [diff] [blame] | 245 | Set<String> packageFeatures = |
| 246 | Sets.difference(Sets.union(globallyEnabled, packageEnabled), packageDisabled); |
Googler | 8e3afcc | 2017-12-20 08:10:21 -0800 | [diff] [blame] | 247 | Set<String> ruleFeatures = |
| 248 | Sets.difference(Sets.union(packageFeatures, ruleEnabled), ruleDisabled); |
Googler | 993f1de | 2018-02-20 02:31:10 -0800 | [diff] [blame] | 249 | allEnabledFeatures.addAll(Sets.difference(ruleFeatures, globallyDisabled)); |
Manuel Klimek | 6d9fb36 | 2015-04-30 12:50:55 +0000 | [diff] [blame] | 250 | } |
| 251 | |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 252 | private static void parseFeatures( |
| 253 | Iterable<String> features, Set<String> enabled, Set<String> disabled) { |
Manuel Klimek | 6d9fb36 | 2015-04-30 12:50:55 +0000 | [diff] [blame] | 254 | for (String feature : features) { |
| 255 | if (feature.startsWith("-")) { |
| 256 | disabled.add(feature.substring(1)); |
| 257 | } else if (feature.equals("no_layering_check")) { |
| 258 | // TODO(bazel-team): Remove once we do not have BUILD files left that contain |
| 259 | // 'no_layering_check'. |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 260 | disabled.add("layering_check"); |
Manuel Klimek | 6d9fb36 | 2015-04-30 12:50:55 +0000 | [diff] [blame] | 261 | } else { |
| 262 | enabled.add(feature); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 267 | public boolean isAllowTagsPropagation() { |
| 268 | return getAnalysisEnvironment() |
| 269 | .getStarlarkSemantics() |
| 270 | .getBool(BuildLanguageOptions.EXPERIMENTAL_ALLOW_TAGS_PROPAGATION); |
| 271 | } |
| 272 | |
dslomov | fd62e76 | 2017-09-19 16:55:53 +0200 | [diff] [blame] | 273 | public RepositoryName getRepository() { |
| 274 | return rule.getRepository(); |
| 275 | } |
| 276 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 277 | @Override |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 278 | public ArtifactRoot getBinDirectory() { |
Googler | 0617f2c | 2020-10-22 08:43:54 -0700 | [diff] [blame] | 279 | return getConfiguration().getBinDirectory(getLabel().getRepository()); |
dslomov | fd62e76 | 2017-09-19 16:55:53 +0200 | [diff] [blame] | 280 | } |
| 281 | |
janakr | f2c9391 | 2022-03-08 01:40:32 -0800 | [diff] [blame] | 282 | @SuppressWarnings("Unused") // Goal is to migrate here. |
| 283 | public ArtifactRoot getBuildInfoDirectory() { |
| 284 | return getConfiguration().getBuildInfoDirectory(getLabel().getRepository()); |
Googler | 36fa385 | 2020-10-13 13:30:14 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Googler | 4e7b1e5 | 2020-10-09 11:58:45 -0700 | [diff] [blame] | 287 | public ArtifactRoot getGenfilesDirectory() { |
Googler | 0617f2c | 2020-10-22 08:43:54 -0700 | [diff] [blame] | 288 | return getConfiguration().getGenfilesDirectory(getLabel().getRepository()); |
Googler | 4e7b1e5 | 2020-10-09 11:58:45 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Googler | 36fa385 | 2020-10-13 13:30:14 -0700 | [diff] [blame] | 291 | public ArtifactRoot getCoverageMetadataDirectory() { |
Googler | 0617f2c | 2020-10-22 08:43:54 -0700 | [diff] [blame] | 292 | return getConfiguration().getCoverageMetadataDirectory(getLabel().getRepository()); |
Googler | 36fa385 | 2020-10-13 13:30:14 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | public ArtifactRoot getTestLogsDirectory() { |
Googler | 0617f2c | 2020-10-22 08:43:54 -0700 | [diff] [blame] | 296 | return getConfiguration().getTestLogsDirectory(getLabel().getRepository()); |
Googler | 36fa385 | 2020-10-13 13:30:14 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Googler | 4e7b1e5 | 2020-10-09 11:58:45 -0700 | [diff] [blame] | 299 | public PathFragment getBinFragment() { |
Googler | 0617f2c | 2020-10-22 08:43:54 -0700 | [diff] [blame] | 300 | return getConfiguration().getBinFragment(getLabel().getRepository()); |
Googler | 4e7b1e5 | 2020-10-09 11:58:45 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | public PathFragment getGenfilesFragment() { |
Googler | 0617f2c | 2020-10-22 08:43:54 -0700 | [diff] [blame] | 304 | return getConfiguration().getGenfilesFragment(getLabel().getRepository()); |
Googler | 4e7b1e5 | 2020-10-09 11:58:45 -0700 | [diff] [blame] | 305 | } |
| 306 | |
dslomov | fd62e76 | 2017-09-19 16:55:53 +0200 | [diff] [blame] | 307 | @Override |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 308 | public ArtifactRoot getMiddlemanDirectory() { |
Googler | 0617f2c | 2020-10-22 08:43:54 -0700 | [diff] [blame] | 309 | return getConfiguration().getMiddlemanDirectory(getLabel().getRepository()); |
dslomov | fd62e76 | 2017-09-19 16:55:53 +0200 | [diff] [blame] | 310 | } |
cparsons | 8882192 | 2017-10-11 01:21:46 +0200 | [diff] [blame] | 311 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 312 | public Rule getRule() { |
| 313 | return rule; |
| 314 | } |
| 315 | |
dslomov | 44d1571 | 2017-12-20 05:42:28 -0800 | [diff] [blame] | 316 | public ImmutableList<Aspect> getAspects() { |
| 317 | return aspects; |
| 318 | } |
| 319 | |
| 320 | /** |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 321 | * If this target's configuration suppresses analysis failures, this returns a list of strings, |
| 322 | * where each string corresponds to a description of an error that occurred during processing this |
| 323 | * target. |
cparsons | e8d450c | 2018-10-04 16:01:53 -0700 | [diff] [blame] | 324 | * |
| 325 | * @throws IllegalStateException if this target's configuration does not suppress analysis |
| 326 | * failures (if {@code getConfiguration().allowAnalysisFailures()} is false) |
| 327 | */ |
| 328 | public List<String> getSuppressedErrorMessages() { |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 329 | Preconditions.checkState( |
| 330 | getConfiguration().allowAnalysisFailures(), |
cparsons | e8d450c | 2018-10-04 16:01:53 -0700 | [diff] [blame] | 331 | "Error messages can only be retrieved via RuleContext if allow_analysis_failures is true"); |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 332 | Preconditions.checkState( |
| 333 | reporter instanceof SuppressingErrorReporter, "Unexpected error reporter"); |
cparsons | e8d450c | 2018-10-04 16:01:53 -0700 | [diff] [blame] | 334 | return ((SuppressingErrorReporter) reporter).getErrorMessages(); |
| 335 | } |
| 336 | |
| 337 | /** |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 338 | * If this <code>RuleContext</code> is for an aspect implementation, returns that aspect. (it is |
| 339 | * the last aspect in the list of aspects applied to a target; all other aspects are the ones main |
| 340 | * aspect sees as specified by its "required_aspect_providers") Otherwise returns <code>null |
| 341 | * </code>. |
dslomov | 44d1571 | 2017-12-20 05:42:28 -0800 | [diff] [blame] | 342 | */ |
| 343 | @Nullable |
| 344 | public Aspect getMainAspect() { |
Googler | 3834975 | 2023-01-11 09:02:30 -0800 | [diff] [blame] | 345 | return Streams.findLast(aspects.stream()).orElse(null); |
dslomov | 44d1571 | 2017-12-20 05:42:28 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 348 | /** |
Michael Staib | 8824d5e | 2016-01-20 21:37:05 +0000 | [diff] [blame] | 349 | * Returns a rule class name suitable for log messages, including an aspect name if applicable. |
| 350 | */ |
| 351 | public String getRuleClassNameForLogging() { |
| 352 | return ruleClassNameForLogging; |
| 353 | } |
| 354 | |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 355 | /** Returns the workspace name for the rule. */ |
Kristina Chodorow | 91876f0 | 2015-02-27 17:14:12 +0000 | [diff] [blame] | 356 | public String getWorkspaceName() { |
Googler | f7d370a | 2021-01-19 08:17:23 -0800 | [diff] [blame] | 357 | return rule.getPackage().getWorkspaceName(); |
Kristina Chodorow | 91876f0 | 2015-02-27 17:14:12 +0000 | [diff] [blame] | 358 | } |
| 359 | |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 360 | /** The configuration conditions that trigger this rule's configurable attributes. */ |
gregce | 593f7f9 | 2017-09-19 02:02:21 +0200 | [diff] [blame] | 361 | public ImmutableMap<Label, ConfigMatchingProvider> getConfigConditions() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 362 | return configConditions; |
| 363 | } |
| 364 | |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 365 | /** All aspects applied to the rule. */ |
Dmitry Lomov | 503c2f7 | 2017-02-23 08:55:47 +0000 | [diff] [blame] | 366 | public ImmutableList<AspectDescriptor> getAspectDescriptors() { |
| 367 | return aspectDescriptors; |
| 368 | } |
| 369 | |
| 370 | /** |
Greg Estren | 7f53423 | 2016-12-01 21:38:25 +0000 | [diff] [blame] | 371 | * Accessor for the attributes of the rule and its aspects. |
| 372 | * |
| 373 | * <p>The rule's native attributes can be queried both on their structure / existence and values |
| 374 | * Aspect attributes can only be queried on their structure. |
| 375 | * |
| 376 | * <p>This should be the sole interface for reading rule/aspect attributes in {@link RuleContext}. |
| 377 | * Don't expose other access points through new public methods. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 378 | */ |
| 379 | public AttributeMap attributes() { |
| 380 | return attributes; |
| 381 | } |
| 382 | |
cparsons | 0dcffc5 | 2017-10-13 23:40:31 +0200 | [diff] [blame] | 383 | @Override |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 384 | public boolean hasErrors() { |
cparsons | 146b9ff | 2019-06-21 09:45:14 -0700 | [diff] [blame] | 385 | return reporter.hasErrors(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 386 | } |
Michajlo Matijkiw | e64bfce | 2016-07-18 14:37:08 +0000 | [diff] [blame] | 387 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 388 | /** |
| 389 | * Returns an immutable map from attribute name to list of configured targets for that attribute. |
| 390 | */ |
| 391 | public ListMultimap<String, ? extends TransitiveInfoCollection> getConfiguredTargetMap() { |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 392 | return Multimaps.transformValues(targetMap, ConfiguredTargetAndData::getConfiguredTarget); |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 393 | } |
| 394 | |
janakr | 6586621 | 2018-03-10 22:30:33 -0800 | [diff] [blame] | 395 | /** |
| 396 | * Returns an immutable map from attribute name to list of {@link ConfiguredTargetAndData} objects |
| 397 | * for that attribute. |
| 398 | */ |
| 399 | public ListMultimap<String, ConfiguredTargetAndData> getConfiguredTargetAndDataMap() { |
| 400 | return targetMap; |
| 401 | } |
| 402 | |
jcater | 0b8280e | 2020-08-25 08:23:03 -0700 | [diff] [blame] | 403 | /** Returns the {@link ConfiguredTargetAndData} the given attribute. */ |
| 404 | public List<ConfiguredTargetAndData> getPrerequisiteConfiguredTargets(String attributeName) { |
| 405 | return targetMap.get(attributeName); |
mjhalupka | de47f21 | 2018-02-12 12:31:21 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 408 | @Override |
| 409 | public ActionOwner getActionOwner() { |
juliexxia | ff58081 | 2020-04-30 15:01:09 -0700 | [diff] [blame] | 410 | return getActionOwner(DEFAULT_EXEC_GROUP_NAME); |
| 411 | } |
| 412 | |
Austin Schuh | d181353 | 2023-02-14 22:48:22 -0800 | [diff] [blame] | 413 | /** |
| 414 | * Returns a special action owner for test actions. Test actions should run on the target platform |
| 415 | * rather than the host platform. Note that the value is not cached (on the assumption that this |
| 416 | * method is only called once). |
| 417 | */ |
| 418 | public ActionOwner getTestActionOwner() { |
| 419 | PlatformInfo testExecutionPlatform; |
| 420 | ImmutableMap<String, String> testExecProperties; |
| 421 | |
| 422 | // If we have a toolchain, pull the target platform out of it. |
| 423 | if (toolchainContexts != null) { |
| 424 | // TODO(https://github.com/bazelbuild/bazel/issues/17466): This doesn't respect execution |
| 425 | // properties coming from the target's `exec_properties` attribute. |
| 426 | // src/test/java/com/google/devtools/build/lib/analysis/test/TestActionBuilderTest.java has a |
| 427 | // test to test for it when it gets figured out. |
| 428 | testExecutionPlatform = toolchainContexts.getTargetPlatform(); |
| 429 | testExecProperties = testExecutionPlatform.execProperties(); |
| 430 | } else { |
| 431 | testExecutionPlatform = null; |
| 432 | testExecProperties = getExecGroups().getExecProperties(DEFAULT_TEST_RUNNER_EXEC_GROUP); |
| 433 | } |
| 434 | |
| 435 | ActionOwner actionOwner = |
| 436 | createActionOwner( |
| 437 | rule, aspectDescriptors, getConfiguration(), testExecProperties, testExecutionPlatform); |
| 438 | |
| 439 | if (actionOwner == null) { |
| 440 | actionOwner = getActionOwner(); |
| 441 | } |
| 442 | return actionOwner; |
| 443 | } |
| 444 | |
juliexxia | ff58081 | 2020-04-30 15:01:09 -0700 | [diff] [blame] | 445 | @Override |
juliexxia | 2c97d38 | 2020-06-12 10:16:26 -0700 | [diff] [blame] | 446 | @Nullable |
juliexxia | ff58081 | 2020-04-30 15:01:09 -0700 | [diff] [blame] | 447 | public ActionOwner getActionOwner(String execGroup) { |
| 448 | if (actionOwners.containsKey(execGroup)) { |
| 449 | return actionOwners.get(execGroup); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 450 | } |
juliexxia | 2c97d38 | 2020-06-12 10:16:26 -0700 | [diff] [blame] | 451 | if (toolchainContexts != null && !toolchainContexts.hasToolchainContext(execGroup)) { |
| 452 | return null; |
| 453 | } |
juliexxia | ff58081 | 2020-04-30 15:01:09 -0700 | [diff] [blame] | 454 | ActionOwner actionOwner = |
| 455 | createActionOwner( |
| 456 | rule, |
| 457 | aspectDescriptors, |
| 458 | getConfiguration(), |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 459 | execGroupCollection.getExecProperties(execGroup), |
jcater | 645c42b | 2021-05-12 09:35:48 -0700 | [diff] [blame] | 460 | getExecutionPlatform(execGroup)); |
juliexxia | ff58081 | 2020-04-30 15:01:09 -0700 | [diff] [blame] | 461 | actionOwners.put(execGroup, actionOwner); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 462 | return actionOwner; |
| 463 | } |
| 464 | |
| 465 | /** |
janakr | eaff19c | 2019-01-31 13:59:40 -0800 | [diff] [blame] | 466 | * An opaque symbol generator to be used when identifying objects by their action owner/index of |
| 467 | * creation. Only needed if an object needs to know whether it was created by the same action |
| 468 | * owner in the same order as another object. Each symbol must call {@link |
| 469 | * SymbolGenerator#generate} separately to obtain a unique object. |
| 470 | */ |
| 471 | public SymbolGenerator<?> getSymbolGenerator() { |
| 472 | return actionOwnerSymbolGenerator; |
| 473 | } |
| 474 | |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 475 | /** Returns a configuration fragment for this this target. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 476 | @Nullable |
Googler | cba99e3 | 2022-12-20 13:33:49 -0800 | [diff] [blame] | 477 | public <T extends Fragment> T getFragment(Class<T> fragment) { |
| 478 | return getFragment(fragment, fragment.getSimpleName(), ""); |
Florian Weikert | d2a2461 | 2015-09-07 14:35:23 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | @Nullable |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 482 | private <T extends Fragment> T getFragment( |
Googler | cba99e3 | 2022-12-20 13:33:49 -0800 | [diff] [blame] | 483 | Class<T> fragment, String name, String additionalErrorMessage) { |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 484 | // TODO(bazel-team): The fragments can also be accessed directly through |
| 485 | // BuildConfigurationValue. Can we lock that down somehow? |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 486 | Preconditions.checkArgument( |
Googler | cba99e3 | 2022-12-20 13:33:49 -0800 | [diff] [blame] | 487 | isLegalFragment(fragment), |
| 488 | "%s has to declare '%s' as a required fragment in order to access it.%s", |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 489 | ruleClassNameForLogging, |
| 490 | name, |
Florian Weikert | d2a2461 | 2015-09-07 14:35:23 +0000 | [diff] [blame] | 491 | additionalErrorMessage); |
Googler | cba99e3 | 2022-12-20 13:33:49 -0800 | [diff] [blame] | 492 | return getConfiguration().getFragment(fragment); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 493 | } |
| 494 | |
Florian Weikert | 082c054 | 2015-08-06 10:24:29 +0000 | [diff] [blame] | 495 | @Nullable |
Googler | cba99e3 | 2022-12-20 13:33:49 -0800 | [diff] [blame] | 496 | public Fragment getStarlarkFragment(String name) throws EvalException { |
| 497 | Class<? extends Fragment> fragmentClass = getConfiguration().getStarlarkFragmentByName(name); |
Florian Weikert | d2a2461 | 2015-09-07 14:35:23 +0000 | [diff] [blame] | 498 | if (fragmentClass == null) { |
| 499 | return null; |
| 500 | } |
adonovan | b8eae2d | 2019-12-09 15:11:06 -0800 | [diff] [blame] | 501 | try { |
| 502 | return getFragment( |
| 503 | fragmentClass, |
| 504 | name, |
| 505 | String.format( |
Googler | cba99e3 | 2022-12-20 13:33:49 -0800 | [diff] [blame] | 506 | " Please update the 'fragments' argument of the rule definition " |
| 507 | + "(for example: fragments = [\"%1$s\"])", |
| 508 | name)); |
adonovan | b8eae2d | 2019-12-09 15:11:06 -0800 | [diff] [blame] | 509 | } catch (IllegalArgumentException ex) { // fishy |
adonovan | a628736 | 2020-08-07 07:15:30 -0700 | [diff] [blame] | 510 | throw new EvalException(ex.getMessage()); |
adonovan | b8eae2d | 2019-12-09 15:11:06 -0800 | [diff] [blame] | 511 | } |
Florian Weikert | 3f8aac9 | 2015-09-07 12:06:02 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Googler | cba99e3 | 2022-12-20 13:33:49 -0800 | [diff] [blame] | 514 | public ImmutableCollection<String> getStarlarkFragmentNames() { |
| 515 | return getConfiguration().getStarlarkFragmentNames(); |
Florian Weikert | 082c054 | 2015-08-06 10:24:29 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Ulf Adams | ea11fc5 | 2015-08-04 14:23:58 +0000 | [diff] [blame] | 518 | public <T extends Fragment> boolean isLegalFragment(Class<T> fragment) { |
Googler | cba99e3 | 2022-12-20 13:33:49 -0800 | [diff] [blame] | 519 | return ruleClassProvider.getFragmentRegistry().getUniversalFragments().contains(fragment) |
| 520 | || configurationFragmentPolicy.isLegalConfigurationFragment(fragment); |
Ulf Adams | ea11fc5 | 2015-08-04 14:23:58 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 523 | @Override |
jhorvitz | 3daedc3 | 2020-07-22 18:33:55 -0700 | [diff] [blame] | 524 | public ActionLookupKey getOwner() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 525 | return getAnalysisEnvironment().getOwner(); |
| 526 | } |
| 527 | |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 528 | public ImmutableList<Artifact> getBuildInfo(BuildInfoKey key) throws InterruptedException { |
plf | 1f341a6 | 2019-04-01 14:02:14 -0700 | [diff] [blame] | 529 | return getAnalysisEnvironment() |
| 530 | .getBuildInfo( |
| 531 | AnalysisUtils.isStampingEnabled(this, getConfiguration()), key, getConfiguration()); |
Ulf Adams | c272e0f | 2015-04-22 19:56:21 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Janak Ramakrishnan | 7857c79 | 2016-03-21 08:35:35 +0000 | [diff] [blame] | 534 | @VisibleForTesting |
Carmi Grushko | 33aa306 | 2016-11-11 02:45:29 +0000 | [diff] [blame] | 535 | public static ActionOwner createActionOwner( |
| 536 | Rule rule, |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 537 | ImmutableList<AspectDescriptor> aspectDescriptors, |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 538 | BuildConfigurationValue configuration, |
jcater | 645c42b | 2021-05-12 09:35:48 -0700 | [diff] [blame] | 539 | ImmutableMap<String, String> execProperties, |
| 540 | @Nullable PlatformInfo executionPlatform) { |
Carmi Grushko | 4665e70 | 2016-11-09 21:51:27 +0000 | [diff] [blame] | 541 | return ActionOwner.create( |
Janak Ramakrishnan | 7857c79 | 2016-03-21 08:35:35 +0000 | [diff] [blame] | 542 | rule.getLabel(), |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 543 | aspectDescriptors, |
Janak Ramakrishnan | 7857c79 | 2016-03-21 08:35:35 +0000 | [diff] [blame] | 544 | rule.getLocation(), |
| 545 | configuration.getMnemonic(), |
| 546 | rule.getTargetKind(), |
| 547 | configuration.checksum(), |
shahan | 50f99d5 | 2018-03-10 05:14:09 -0800 | [diff] [blame] | 548 | configuration.toBuildEvent(), |
ilist | 67c9936 | 2022-01-04 02:39:00 -0800 | [diff] [blame] | 549 | configuration.isToolConfiguration() ? TOOL_CONFIGURATION_PROGRESS_TAG : null, |
jcater | 645c42b | 2021-05-12 09:35:48 -0700 | [diff] [blame] | 550 | execProperties, |
John Cater | ec5d2ed | 2018-01-04 11:52:21 -0800 | [diff] [blame] | 551 | executionPlatform); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | @Override |
janakr | d459197 | 2021-04-14 08:37:06 -0700 | [diff] [blame] | 555 | public void registerAction(ActionAnalysisMetadata action) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 556 | getAnalysisEnvironment().registerAction(action); |
| 557 | } |
| 558 | |
| 559 | /** |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 560 | * Convenience function for subclasses to report non-attribute-specific errors in the current |
| 561 | * rule. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 562 | */ |
| 563 | @Override |
| 564 | public void ruleError(String message) { |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 565 | reporter.ruleError(message); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 566 | } |
Michajlo Matijkiw | e64bfce | 2016-07-18 14:37:08 +0000 | [diff] [blame] | 567 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 568 | /** |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 569 | * Convenience function for subclasses to report non-attribute-specific warnings in the current |
| 570 | * rule. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 571 | */ |
| 572 | @Override |
| 573 | public void ruleWarning(String message) { |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 574 | reporter.ruleWarning(message); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /** |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 578 | * Convenience function for subclasses to report attribute-specific errors in the current rule. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 579 | * |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 580 | * <p>If the name of the attribute starts with <code>$</code> it is replaced with a string <code> |
| 581 | * (an implicit dependency)</code>. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 582 | */ |
| 583 | @Override |
| 584 | public void attributeError(String attrName, String message) { |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 585 | reporter.attributeError(attrName, message); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 586 | } |
| 587 | |
Chris Parsons | 4dfb22c | 2016-05-23 17:39:42 +0000 | [diff] [blame] | 588 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 589 | * Like attributeError, but does not mark the configured target as errored. |
| 590 | * |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 591 | * <p>If the name of the attribute starts with <code>$</code> it is replaced with a string <code> |
| 592 | * (an implicit dependency)</code>. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 593 | */ |
| 594 | @Override |
| 595 | public void attributeWarning(String attrName, String message) { |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 596 | reporter.attributeWarning(attrName, message); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /** |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 600 | * Returns an artifact beneath the root of either the "bin" or "genfiles" tree, whose path is |
| 601 | * based on the name of this target and the current configuration. The choice of which tree to use |
| 602 | * is based on the rule with which this target (which must be an OutputFile or a Rule) is |
| 603 | * associated. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 604 | */ |
| 605 | public Artifact createOutputArtifact() { |
cparsons | 0623d3c | 2018-10-23 16:06:30 -0700 | [diff] [blame] | 606 | Target target = getTarget(); |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 607 | PathFragment rootRelativePath = |
| 608 | getPackageDirectory().getRelative(PathFragment.create(target.getName())); |
cparsons | 0623d3c | 2018-10-23 16:06:30 -0700 | [diff] [blame] | 609 | |
| 610 | return internalCreateOutputArtifact(rootRelativePath, target, OutputFile.Kind.FILE); |
| 611 | } |
| 612 | |
| 613 | /** |
Googler | bf15534 | 2022-06-24 11:17:23 -0700 | [diff] [blame] | 614 | * Returns the output artifact of an {@link OutputFile} of this target. |
| 615 | * |
| 616 | * @see #createOutputArtifact() |
| 617 | */ |
| 618 | public Artifact createOutputArtifact(OutputFile out) { |
| 619 | PathFragment packageRelativePath = |
| 620 | getPackageDirectory().getRelative(PathFragment.create(out.getName())); |
| 621 | return internalCreateOutputArtifact(packageRelativePath, out, out.getKind()); |
| 622 | } |
| 623 | |
| 624 | /** |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 625 | * Returns an artifact beneath the root of either the "bin" or "genfiles" tree, whose path is |
| 626 | * based on the name of this target and the current configuration, with a script suffix |
| 627 | * appropriate for the current host platform. ({@code .cmd} for Windows, otherwise {@code .sh}). |
| 628 | * The choice of which tree to use is based on the rule with which this target (which must be an |
| 629 | * OutputFile or a Rule) is associated. |
cparsons | 0623d3c | 2018-10-23 16:06:30 -0700 | [diff] [blame] | 630 | */ |
| 631 | public Artifact createOutputArtifactScript() { |
| 632 | Target target = getTarget(); |
| 633 | // TODO(laszlocsomor): Use the execution platform, not the host platform. |
| 634 | boolean isExecutedOnWindows = OS.getCurrent() == OS.WINDOWS; |
| 635 | |
| 636 | String fileExtension = isExecutedOnWindows ? ".cmd" : ".sh"; |
| 637 | |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 638 | PathFragment rootRelativePath = |
| 639 | getPackageDirectory().getRelative(PathFragment.create(target.getName() + fileExtension)); |
cparsons | 0623d3c | 2018-10-23 16:06:30 -0700 | [diff] [blame] | 640 | |
| 641 | return internalCreateOutputArtifact(rootRelativePath, target, OutputFile.Kind.FILE); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /** |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 645 | * Implementation for {@link #createOutputArtifact()} and {@link |
| 646 | * #createOutputArtifact(OutputFile)}. This is private so that {@link |
| 647 | * #createOutputArtifact(OutputFile)} can have a more specific signature. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 648 | */ |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 649 | private Artifact internalCreateOutputArtifact( |
| 650 | PathFragment rootRelativePath, Target target, OutputFile.Kind outputFileKind) { |
Lukacs Berki | 4a89a9b | 2015-07-29 06:54:07 +0000 | [diff] [blame] | 651 | Preconditions.checkState( |
| 652 | target.getLabel().getPackageIdentifier().equals(getLabel().getPackageIdentifier()), |
| 653 | "Creating output artifact for target '%s' in different package than the rule '%s' " |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 654 | + "being analyzed", |
| 655 | target.getLabel(), |
| 656 | getLabel()); |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 657 | ArtifactRoot root = getBinOrGenfilesDirectory(); |
cparsons | 0623d3c | 2018-10-23 16:06:30 -0700 | [diff] [blame] | 658 | |
Dmitry Lomov | 5b1ce4d | 2018-05-30 04:34:08 -0700 | [diff] [blame] | 659 | switch (outputFileKind) { |
| 660 | case FILE: |
cparsons | 0623d3c | 2018-10-23 16:06:30 -0700 | [diff] [blame] | 661 | return getDerivedArtifact(rootRelativePath, root); |
Dmitry Lomov | 5b1ce4d | 2018-05-30 04:34:08 -0700 | [diff] [blame] | 662 | case FILESET: |
cparsons | 0623d3c | 2018-10-23 16:06:30 -0700 | [diff] [blame] | 663 | return getAnalysisEnvironment().getFilesetArtifact(rootRelativePath, root); |
Dmitry Lomov | 5b1ce4d | 2018-05-30 04:34:08 -0700 | [diff] [blame] | 664 | default: |
| 665 | throw new IllegalStateException(); |
| 666 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /** |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 670 | * Returns the root of either the "bin" or "genfiles" tree, based on this target and the current |
| 671 | * configuration. The choice of which tree to use is based on the rule with which this target |
| 672 | * (which must be an OutputFile or a Rule) is associated. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 673 | */ |
David Ostrovsky | a138877 | 2020-07-08 12:11:01 -0700 | [diff] [blame] | 674 | @Override |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 675 | public ArtifactRoot getBinOrGenfilesDirectory() { |
Googler | 482c26d | 2021-05-18 08:29:51 -0700 | [diff] [blame] | 676 | return rule.outputsToBindir() |
Googler | 0617f2c | 2020-10-22 08:43:54 -0700 | [diff] [blame] | 677 | ? getConfiguration().getBinDirectory(getLabel().getRepository()) |
| 678 | : getConfiguration().getGenfilesDirectory(getLabel().getRepository()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 679 | } |
| 680 | |
Lukacs Berki | 4a89a9b | 2015-07-29 06:54:07 +0000 | [diff] [blame] | 681 | /** |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 682 | * Creates an artifact in a directory that is unique to the package that contains the rule, thus |
| 683 | * guaranteeing that it never clashes with artifacts created by rules in other packages. |
Lukacs Berki | 4a89a9b | 2015-07-29 06:54:07 +0000 | [diff] [blame] | 684 | */ |
Carmi Grushko | f414b4e | 2016-06-23 20:35:27 +0000 | [diff] [blame] | 685 | public Artifact getBinArtifact(String relative) { |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 686 | return getBinArtifact(PathFragment.create(relative)); |
Kristina Chodorow | f8a1ae6 | 2016-08-11 14:44:40 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | public Artifact getBinArtifact(PathFragment relative) { |
Carmi Grushko | f414b4e | 2016-06-23 20:35:27 +0000 | [diff] [blame] | 690 | return getPackageRelativeArtifact( |
Googler | 0617f2c | 2020-10-22 08:43:54 -0700 | [diff] [blame] | 691 | relative, getConfiguration().getBinDirectory(getLabel().getRepository())); |
Carmi Grushko | f414b4e | 2016-06-23 20:35:27 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /** |
| 695 | * Creates an artifact in a directory that is unique to the package that contains the rule, thus |
| 696 | * guaranteeing that it never clashes with artifacts created by rules in other packages. |
| 697 | */ |
| 698 | public Artifact getGenfilesArtifact(String relative) { |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 699 | return getGenfilesArtifact(PathFragment.create(relative)); |
Kristina Chodorow | f8a1ae6 | 2016-08-11 14:44:40 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | public Artifact getGenfilesArtifact(PathFragment relative) { |
Carmi Grushko | f414b4e | 2016-06-23 20:35:27 +0000 | [diff] [blame] | 703 | return getPackageRelativeArtifact( |
Googler | 0617f2c | 2020-10-22 08:43:54 -0700 | [diff] [blame] | 704 | relative, getConfiguration().getGenfilesDirectory(getLabel().getRepository())); |
Carmi Grushko | f414b4e | 2016-06-23 20:35:27 +0000 | [diff] [blame] | 705 | } |
| 706 | |
plf | b74df04 | 2018-10-31 02:15:10 -0700 | [diff] [blame] | 707 | @Override |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 708 | public Artifact getShareableArtifact(PathFragment rootRelativePath, ArtifactRoot root) { |
Lukacs Berki | 21a04f2 | 2015-08-20 13:31:24 +0000 | [diff] [blame] | 709 | return getAnalysisEnvironment().getDerivedArtifact(rootRelativePath, root); |
| 710 | } |
| 711 | |
asteinb | 0de4546 | 2018-05-17 08:07:12 -0700 | [diff] [blame] | 712 | @Override |
janakr | 658d47f | 2019-05-29 11:11:30 -0700 | [diff] [blame] | 713 | public Artifact.DerivedArtifact getPackageRelativeArtifact( |
| 714 | PathFragment relative, ArtifactRoot root) { |
Googler | 3834975 | 2023-01-11 09:02:30 -0800 | [diff] [blame] | 715 | return getPackageRelativeArtifact(relative, root, /* contentBasedPath= */ false); |
gregce | 11f3b0e | 2019-06-07 17:12:06 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /** |
| 719 | * Same as {@link #getPackageRelativeArtifact(PathFragment, ArtifactRoot)} but includes the option |
| 720 | * option to use a content-based path for this artifact (see {@link |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 721 | * BuildConfigurationValue#useContentBasedOutputPaths()}). |
gregce | 11f3b0e | 2019-06-07 17:12:06 -0700 | [diff] [blame] | 722 | */ |
| 723 | private Artifact.DerivedArtifact getPackageRelativeArtifact( |
| 724 | PathFragment relative, ArtifactRoot root, boolean contentBasedPath) { |
| 725 | return getDerivedArtifact(getPackageDirectory().getRelative(relative), root, contentBasedPath); |
Lukacs Berki | 4a89a9b | 2015-07-29 06:54:07 +0000 | [diff] [blame] | 726 | } |
| 727 | |
janakr | 658d47f | 2019-05-29 11:11:30 -0700 | [diff] [blame] | 728 | /** |
| 729 | * Creates an artifact in a directory that is unique to the package that contains the rule, thus |
| 730 | * guaranteeing that it never clashes with artifacts created by rules in other packages. |
| 731 | */ |
| 732 | public Artifact getPackageRelativeArtifact(String relative, ArtifactRoot root) { |
Googler | 3834975 | 2023-01-11 09:02:30 -0800 | [diff] [blame] | 733 | return getPackageRelativeArtifact(relative, root, /* contentBasedPath= */ false); |
gregce | 11f3b0e | 2019-06-07 17:12:06 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /** |
| 737 | * Same as {@link #getPackageRelativeArtifact(String, ArtifactRoot)} but includes the option to |
| 738 | * use a content-based path for this artifact (see {@link |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 739 | * BuildConfigurationValue#useContentBasedOutputPaths()}). |
gregce | 11f3b0e | 2019-06-07 17:12:06 -0700 | [diff] [blame] | 740 | */ |
| 741 | private Artifact getPackageRelativeArtifact( |
| 742 | String relative, ArtifactRoot root, boolean contentBasedPath) { |
| 743 | return getPackageRelativeArtifact(PathFragment.create(relative), root, contentBasedPath); |
janakr | 658d47f | 2019-05-29 11:11:30 -0700 | [diff] [blame] | 744 | } |
| 745 | |
asteinb | 0de4546 | 2018-05-17 08:07:12 -0700 | [diff] [blame] | 746 | @Override |
Lukacs Berki | 4a89a9b | 2015-07-29 06:54:07 +0000 | [diff] [blame] | 747 | public PathFragment getPackageDirectory() { |
Googler | 0021700 | 2020-10-27 06:41:26 -0700 | [diff] [blame] | 748 | return getLabel() |
| 749 | .getPackageIdentifier() |
| 750 | .getPackagePath(getConfiguration().isSiblingRepositoryLayout()); |
Lukacs Berki | 4a89a9b | 2015-07-29 06:54:07 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /** |
| 754 | * Creates an artifact under a given root with the given root-relative path. |
| 755 | * |
| 756 | * <p>Verifies that it is in the root-relative directory corresponding to the package of the rule, |
| 757 | * thus ensuring that it doesn't clash with other artifacts generated by other rules using this |
| 758 | * method. |
| 759 | */ |
Ulf Adams | 3ab82f7 | 2015-09-04 12:10:53 +0000 | [diff] [blame] | 760 | @Override |
janakr | 658d47f | 2019-05-29 11:11:30 -0700 | [diff] [blame] | 761 | public Artifact.DerivedArtifact getDerivedArtifact( |
| 762 | PathFragment rootRelativePath, ArtifactRoot root) { |
Googler | 3834975 | 2023-01-11 09:02:30 -0800 | [diff] [blame] | 763 | return getDerivedArtifact(rootRelativePath, root, /* contentBasedPath= */ false); |
gregce | 11f3b0e | 2019-06-07 17:12:06 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | /** |
| 767 | * Same as {@link #getDerivedArtifact(PathFragment, ArtifactRoot)} but includes the option to use |
| 768 | * a content-based path for this artifact (see {@link |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 769 | * BuildConfigurationValue#useContentBasedOutputPaths()}). |
gregce | 11f3b0e | 2019-06-07 17:12:06 -0700 | [diff] [blame] | 770 | */ |
| 771 | public Artifact.DerivedArtifact getDerivedArtifact( |
| 772 | PathFragment rootRelativePath, ArtifactRoot root, boolean contentBasedPath) { |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 773 | Preconditions.checkState( |
| 774 | rootRelativePath.startsWith(getPackageDirectory()), |
Lukacs Berki | 4a89a9b | 2015-07-29 06:54:07 +0000 | [diff] [blame] | 775 | "Output artifact '%s' not under package directory '%s' for target '%s'", |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 776 | rootRelativePath, |
| 777 | getPackageDirectory(), |
| 778 | getLabel()); |
gregce | 11f3b0e | 2019-06-07 17:12:06 -0700 | [diff] [blame] | 779 | return getAnalysisEnvironment().getDerivedArtifact(rootRelativePath, root, contentBasedPath); |
Lukacs Berki | 4a89a9b | 2015-07-29 06:54:07 +0000 | [diff] [blame] | 780 | } |
Michael Thvedt | e3b1cb7 | 2016-02-08 23:32:27 +0000 | [diff] [blame] | 781 | |
plf | 441fd75 | 2019-02-06 01:28:18 -0800 | [diff] [blame] | 782 | @Override |
cpeyser | ac09f0a | 2018-02-05 09:33:15 -0800 | [diff] [blame] | 783 | public SpecialArtifact getTreeArtifact(PathFragment rootRelativePath, ArtifactRoot root) { |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 784 | Preconditions.checkState( |
| 785 | rootRelativePath.startsWith(getPackageDirectory()), |
Michael Thvedt | e3b1cb7 | 2016-02-08 23:32:27 +0000 | [diff] [blame] | 786 | "Output artifact '%s' not under package directory '%s' for target '%s'", |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 787 | rootRelativePath, |
| 788 | getPackageDirectory(), |
| 789 | getLabel()); |
Michael Thvedt | e3b1cb7 | 2016-02-08 23:32:27 +0000 | [diff] [blame] | 790 | return getAnalysisEnvironment().getTreeArtifact(rootRelativePath, root); |
| 791 | } |
| 792 | |
Lukacs Berki | 4a89a9b | 2015-07-29 06:54:07 +0000 | [diff] [blame] | 793 | /** |
allevato | 060a448 | 2017-03-30 20:45:52 +0000 | [diff] [blame] | 794 | * Creates a tree artifact in a directory that is unique to the package that contains the rule, |
| 795 | * thus guaranteeing that it never clashes with artifacts created by rules in other packages. |
| 796 | */ |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 797 | public Artifact getPackageRelativeTreeArtifact(PathFragment relative, ArtifactRoot root) { |
allevato | 060a448 | 2017-03-30 20:45:52 +0000 | [diff] [blame] | 798 | return getTreeArtifact(getPackageDirectory().getRelative(relative), root); |
| 799 | } |
| 800 | |
| 801 | /** |
Lukacs Berki | 4a89a9b | 2015-07-29 06:54:07 +0000 | [diff] [blame] | 802 | * Creates an artifact in a directory that is unique to the rule, thus guaranteeing that it never |
| 803 | * clashes with artifacts created by other rules. |
| 804 | */ |
| 805 | public Artifact getUniqueDirectoryArtifact( |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 806 | String uniqueDirectory, String relative, ArtifactRoot root) { |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 807 | return getUniqueDirectoryArtifact(uniqueDirectory, PathFragment.create(relative), root); |
Lukacs Berki | 4a89a9b | 2015-07-29 06:54:07 +0000 | [diff] [blame] | 808 | } |
| 809 | |
asteinb | 0de4546 | 2018-05-17 08:07:12 -0700 | [diff] [blame] | 810 | @Override |
| 811 | public Artifact getUniqueDirectoryArtifact(String uniqueDirectorySuffix, String relative) { |
| 812 | return getUniqueDirectoryArtifact(uniqueDirectorySuffix, relative, getBinOrGenfilesDirectory()); |
| 813 | } |
| 814 | |
ahumesky | 4b00ab1 | 2018-11-15 16:23:46 -0800 | [diff] [blame] | 815 | @Override |
| 816 | public Artifact getUniqueDirectoryArtifact(String uniqueDirectorySuffix, PathFragment relative) { |
| 817 | return getUniqueDirectoryArtifact(uniqueDirectorySuffix, relative, getBinOrGenfilesDirectory()); |
| 818 | } |
| 819 | |
plf | 727a07d | 2019-02-01 02:27:35 -0800 | [diff] [blame] | 820 | @Override |
Lukacs Berki | 4a89a9b | 2015-07-29 06:54:07 +0000 | [diff] [blame] | 821 | public Artifact getUniqueDirectoryArtifact( |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 822 | String uniqueDirectory, PathFragment relative, ArtifactRoot root) { |
Lukacs Berki | 4a89a9b | 2015-07-29 06:54:07 +0000 | [diff] [blame] | 823 | return getDerivedArtifact(getUniqueDirectory(uniqueDirectory).getRelative(relative), root); |
| 824 | } |
| 825 | |
Googler | 0308385 | 2015-12-06 18:31:53 +0000 | [diff] [blame] | 826 | /** |
Carmi Grushko | 8e589dc | 2016-12-01 02:28:42 +0000 | [diff] [blame] | 827 | * Returns true iff the rule, or any attached aspect, has an attribute with the given name and |
| 828 | * type. |
| 829 | */ |
| 830 | public boolean isAttrDefined(String attrName, Type<?> type) { |
Greg Estren | 7f53423 | 2016-12-01 21:38:25 +0000 | [diff] [blame] | 831 | return attributes().has(attrName, type); |
Carmi Grushko | 8e589dc | 2016-12-01 02:28:42 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | /** |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 835 | * Returns the dependencies through a {@code LABEL_DICT_UNARY} attribute as a map from a string to |
| 836 | * a {@link TransitiveInfoCollection}. |
Lukacs Berki | 8bdae76 | 2016-07-13 14:58:54 +0000 | [diff] [blame] | 837 | */ |
| 838 | public Map<String, TransitiveInfoCollection> getPrerequisiteMap(String attributeName) { |
Greg Estren | 7f53423 | 2016-12-01 21:38:25 +0000 | [diff] [blame] | 839 | Preconditions.checkState(attributes().has(attributeName, BuildType.LABEL_DICT_UNARY)); |
Lukacs Berki | 8bdae76 | 2016-07-13 14:58:54 +0000 | [diff] [blame] | 840 | |
| 841 | ImmutableMap.Builder<String, TransitiveInfoCollection> result = ImmutableMap.builder(); |
| 842 | Map<String, Label> dict = attributes().get(attributeName, BuildType.LABEL_DICT_UNARY); |
| 843 | Map<Label, ConfiguredTarget> labelToDep = new HashMap<>(); |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 844 | for (ConfiguredTargetAndData dep : targetMap.get(attributeName)) { |
Googler | f592470 | 2022-12-08 14:55:42 -0800 | [diff] [blame] | 845 | labelToDep.put(dep.getTargetLabel(), dep.getConfiguredTarget()); |
Lukacs Berki | 8bdae76 | 2016-07-13 14:58:54 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | for (Map.Entry<String, Label> entry : dict.entrySet()) { |
| 849 | result.put(entry.getKey(), Preconditions.checkNotNull(labelToDep.get(entry.getValue()))); |
| 850 | } |
| 851 | |
Googler | 92ce115 | 2022-02-01 06:37:06 -0800 | [diff] [blame] | 852 | return result.buildOrThrow(); |
Lukacs Berki | 8bdae76 | 2016-07-13 14:58:54 +0000 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | /** |
Googler | 46b285a | 2020-03-06 13:33:24 -0800 | [diff] [blame] | 856 | * Returns the prerequisites keyed by their configuration transition keys. If the split transition |
mjhalupka | de47f21 | 2018-02-12 12:31:21 -0800 | [diff] [blame] | 857 | * is not active (e.g. split() returned an empty list), the key is an empty Optional. |
| 858 | */ |
| 859 | public Map<Optional<String>, ? extends List<? extends TransitiveInfoCollection>> |
| 860 | getSplitPrerequisites(String attributeName) { |
| 861 | return Maps.transformValues( |
| 862 | getSplitPrerequisiteConfiguredTargetAndTargets(attributeName), |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 863 | (ctatList) -> Lists.transform(ctatList, ConfiguredTargetAndData::getConfiguredTarget)); |
mjhalupka | de47f21 | 2018-02-12 12:31:21 -0800 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /** |
Googler | 46b285a | 2020-03-06 13:33:24 -0800 | [diff] [blame] | 867 | * Returns the prerequisites keyed by their transition keys. If the split transition is not active |
| 868 | * (e.g. split() returned an empty list), the key is an empty Optional. |
janakr | 0758d35 | 2018-03-10 20:38:45 -0800 | [diff] [blame] | 869 | */ |
| 870 | public Map<Optional<String>, List<ConfiguredTargetAndData>> |
mjhalupka | de47f21 | 2018-02-12 12:31:21 -0800 | [diff] [blame] | 871 | getSplitPrerequisiteConfiguredTargetAndTargets(String attributeName) { |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 872 | checkAttributeIsDependency(attributeName); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 873 | // Use an ImmutableListMultimap.Builder here to preserve ordering. |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 874 | ImmutableListMultimap.Builder<Optional<String>, ConfiguredTargetAndData> result = |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 875 | ImmutableListMultimap.builder(); |
jcater | 0b8280e | 2020-08-25 08:23:03 -0700 | [diff] [blame] | 876 | List<ConfiguredTargetAndData> deps = getPrerequisiteConfiguredTargets(attributeName); |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 877 | for (ConfiguredTargetAndData t : deps) { |
Googler | 3cc6cc3 | 2020-04-29 10:50:38 -0700 | [diff] [blame] | 878 | ImmutableList<String> transitionKeys = t.getTransitionKeys(); |
| 879 | if (transitionKeys.isEmpty()) { |
| 880 | // The split transition is not active, i.e. does not change build configurations. |
Googler | fb55091b | 2020-09-29 20:41:18 -0700 | [diff] [blame] | 881 | // TODO(jungjw): Investigate if we need to do a check here. |
Googler | 3cc6cc3 | 2020-04-29 10:50:38 -0700 | [diff] [blame] | 882 | return ImmutableMap.of(Optional.absent(), deps); |
| 883 | } |
| 884 | for (String key : transitionKeys) { |
| 885 | result.put(Optional.of(key), t); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | return Multimaps.asMap(result.build()); |
| 889 | } |
| 890 | |
| 891 | /** |
jcater | f7d726f | 2020-04-13 10:34:54 -0700 | [diff] [blame] | 892 | * Returns the specified provider of the prerequisite referenced by the attribute in the argument. |
jcater | 3a9532d | 2020-08-26 06:09:28 -0700 | [diff] [blame] | 893 | * If the attribute is empty or it does not support the specified provider, returns null. |
| 894 | */ |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 895 | @Nullable |
jcater | 3a9532d | 2020-08-26 06:09:28 -0700 | [diff] [blame] | 896 | public <C extends TransitiveInfoProvider> C getPrerequisite( |
| 897 | String attributeName, Class<C> provider) { |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 898 | TransitiveInfoCollection prerequisite = getPrerequisite(attributeName); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 899 | return prerequisite == null ? null : prerequisite.getProvider(provider); |
| 900 | } |
| 901 | |
| 902 | /** |
| 903 | * Returns the transitive info collection that feeds into this target through the specified |
jcater | 3a9532d | 2020-08-26 06:09:28 -0700 | [diff] [blame] | 904 | * attribute. Returns null if the attribute is empty. |
| 905 | */ |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 906 | @Nullable |
| 907 | public TransitiveInfoCollection getPrerequisite(String attributeName) { |
| 908 | ConfiguredTargetAndData result = getPrerequisiteConfiguredTargetAndData(attributeName); |
janakr | 27bb6dd | 2018-03-10 19:10:00 -0800 | [diff] [blame] | 909 | return result == null ? null : result.getConfiguredTarget(); |
| 910 | } |
| 911 | |
| 912 | /** |
Googler | bf15534 | 2022-06-24 11:17:23 -0700 | [diff] [blame] | 913 | * Returns the declared provider (native and Starlark) for the specified constructor under the |
| 914 | * specified attribute of this target in the BUILD file. May return null if there is no |
| 915 | * TransitiveInfoCollection under the specified attribute. |
| 916 | */ |
| 917 | @Nullable |
| 918 | public <T extends Info> T getPrerequisite(String attributeName, BuiltinProvider<T> starlarkKey) { |
| 919 | TransitiveInfoCollection prerequisite = getPrerequisite(attributeName); |
| 920 | return prerequisite == null ? null : prerequisite.get(starlarkKey); |
| 921 | } |
| 922 | |
| 923 | /** |
janakr | 27bb6dd | 2018-03-10 19:10:00 -0800 | [diff] [blame] | 924 | * Returns the {@link ConfiguredTargetAndData} that feeds ino this target through the specified |
jcater | 3a9532d | 2020-08-26 06:09:28 -0700 | [diff] [blame] | 925 | * attribute. Returns null if the attribute is empty. |
| 926 | */ |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 927 | @Nullable |
jcater | 3a9532d | 2020-08-26 06:09:28 -0700 | [diff] [blame] | 928 | public ConfiguredTargetAndData getPrerequisiteConfiguredTargetAndData(String attributeName) { |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 929 | checkAttributeIsDependency(attributeName); |
jcater | 0b8280e | 2020-08-25 08:23:03 -0700 | [diff] [blame] | 930 | List<ConfiguredTargetAndData> elements = getPrerequisiteConfiguredTargets(attributeName); |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 931 | Preconditions.checkState( |
| 932 | elements.size() <= 1, |
| 933 | "%s attribute %s produces more than one prerequisite", |
| 934 | ruleClassNameForLogging, |
| 935 | attributeName); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 936 | return elements.isEmpty() ? null : elements.get(0); |
| 937 | } |
| 938 | |
| 939 | /** |
dslomov | de965ac | 2017-07-31 21:07:51 +0200 | [diff] [blame] | 940 | * For a given attribute, returns all declared provider provided by targets of that attribute. |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 941 | * Each declared provider is keyed by the {@link BuildConfigurationValue} under which the provider |
| 942 | * was created. |
dslomov | 4e9fa19 | 2017-07-12 14:59:07 +0200 | [diff] [blame] | 943 | */ |
dslomov | de965ac | 2017-07-31 21:07:51 +0200 | [diff] [blame] | 944 | public <C extends Info> |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 945 | ImmutableListMultimap<BuildConfigurationValue, C> getPrerequisitesByConfiguration( |
jcater | 3a9532d | 2020-08-26 06:09:28 -0700 | [diff] [blame] | 946 | String attributeName, BuiltinProvider<C> provider) { |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 947 | checkAttributeIsDependency(attributeName); |
jcater | b979f6e | 2020-08-25 08:54:38 -0700 | [diff] [blame] | 948 | List<ConfiguredTargetAndData> ctatCollection = getPrerequisiteConfiguredTargets(attributeName); |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 949 | ImmutableListMultimap.Builder<BuildConfigurationValue, C> result = |
| 950 | ImmutableListMultimap.builder(); |
jcater | b979f6e | 2020-08-25 08:54:38 -0700 | [diff] [blame] | 951 | for (ConfiguredTargetAndData prerequisite : ctatCollection) { |
janakr | 12b78fe | 2018-03-10 18:06:56 -0800 | [diff] [blame] | 952 | C prerequisiteProvider = prerequisite.getConfiguredTarget().get(provider); |
dslomov | 4e9fa19 | 2017-07-12 14:59:07 +0200 | [diff] [blame] | 953 | if (prerequisiteProvider != null) { |
| 954 | result.put(prerequisite.getConfiguration(), prerequisiteProvider); |
| 955 | } |
| 956 | } |
| 957 | return result.build(); |
| 958 | } |
| 959 | |
| 960 | /** |
cparsons | b35c0a4 | 2018-08-20 11:37:41 -0700 | [diff] [blame] | 961 | * Returns the list of transitive info collections that feed into this target through the |
jcater | 3a9532d | 2020-08-26 06:09:28 -0700 | [diff] [blame] | 962 | * specified attribute. |
| 963 | */ |
| 964 | public List<? extends TransitiveInfoCollection> getPrerequisites(String attributeName) { |
jcater | 1ae143b | 2020-09-10 11:38:09 -0700 | [diff] [blame] | 965 | if (!attributes().has(attributeName)) { |
| 966 | return ImmutableList.of(); |
| 967 | } |
| 968 | |
| 969 | List<ConfiguredTargetAndData> prerequisiteConfiguredTargets; |
Googler | 09cd1fc | 2021-04-05 17:57:11 -0700 | [diff] [blame] | 970 | // android_binary, android_test, and android_binary_internal override deps to use a split |
| 971 | // transition. |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 972 | if ((rule.getRuleClass().equals("android_binary") |
| 973 | || rule.getRuleClass().equals("android_test") |
| 974 | || rule.getRuleClass().equals("android_binary_internal")) |
jcater | 1ae143b | 2020-09-10 11:38:09 -0700 | [diff] [blame] | 975 | && attributeName.equals("deps") |
| 976 | && attributes().getAttributeDefinition(attributeName).getTransitionFactory().isSplit()) { |
| 977 | // TODO(b/168038145): Restore legacy behavior of returning the prerequisites from the first |
| 978 | // portion of the split transition. |
| 979 | // Callers should be identified, cleaned up, and this check removed. |
| 980 | Map<Optional<String>, List<ConfiguredTargetAndData>> map = |
| 981 | getSplitPrerequisiteConfiguredTargetAndTargets(attributeName); |
| 982 | prerequisiteConfiguredTargets = |
| 983 | map.isEmpty() ? ImmutableList.of() : map.entrySet().iterator().next().getValue(); |
| 984 | } else { |
| 985 | prerequisiteConfiguredTargets = getPrerequisiteConfiguredTargets(attributeName); |
| 986 | } |
| 987 | |
cparsons | b35c0a4 | 2018-08-20 11:37:41 -0700 | [diff] [blame] | 988 | return Lists.transform( |
jcater | 1ae143b | 2020-09-10 11:38:09 -0700 | [diff] [blame] | 989 | prerequisiteConfiguredTargets, ConfiguredTargetAndData::getConfiguredTarget); |
cparsons | b35c0a4 | 2018-08-20 11:37:41 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 993 | * Returns all the providers of the specified type that are listed under the specified attribute |
| 994 | * of this target in the BUILD file. |
| 995 | */ |
ulfjack | 6c3908c | 2019-12-18 05:03:36 -0800 | [diff] [blame] | 996 | public <C extends TransitiveInfoProvider> List<C> getPrerequisites( |
jcater | 3a9532d | 2020-08-26 06:09:28 -0700 | [diff] [blame] | 997 | String attributeName, Class<C> classType) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 998 | AnalysisUtils.checkProvider(classType); |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 999 | return AnalysisUtils.getProviders(getPrerequisites(attributeName), classType); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | /** |
gregce | ca48e9a | 2020-04-14 08:54:38 -0700 | [diff] [blame] | 1003 | * Returns all the declared providers (native and Starlark) for the specified constructor under |
| 1004 | * the specified attribute of this target in the BUILD file. |
Sergio Campama | fd93143 | 2016-12-09 21:47:35 +0000 | [diff] [blame] | 1005 | */ |
ulfjack | 6c3908c | 2019-12-18 05:03:36 -0800 | [diff] [blame] | 1006 | public <T extends Info> List<T> getPrerequisites( |
jcater | 3a9532d | 2020-08-26 06:09:28 -0700 | [diff] [blame] | 1007 | String attributeName, BuiltinProvider<T> starlarkKey) { |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 1008 | return AnalysisUtils.getProviders(getPrerequisites(attributeName), starlarkKey); |
cparsons | 2d67cf9 | 2018-05-24 14:02:09 -0700 | [diff] [blame] | 1009 | } |
cparsons | abeb851 | 2018-06-11 12:44:06 -0700 | [diff] [blame] | 1010 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1011 | * Returns all the providers of the specified type that are listed under the specified attribute |
| 1012 | * of this target in the BUILD file, and that contain the specified provider. |
| 1013 | */ |
jcater | f7d726f | 2020-04-13 10:34:54 -0700 | [diff] [blame] | 1014 | public <C extends TransitiveInfoProvider> |
| 1015 | Iterable<? extends TransitiveInfoCollection> getPrerequisitesIf( |
jcater | 3a9532d | 2020-08-26 06:09:28 -0700 | [diff] [blame] | 1016 | String attributeName, Class<C> classType) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1017 | AnalysisUtils.checkProvider(classType); |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 1018 | return AnalysisUtils.filterByProvider(getPrerequisites(attributeName), classType); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | /** |
dslomov | 73527c3 | 2017-07-27 17:35:46 +0200 | [diff] [blame] | 1022 | * Returns all the providers of the specified type that are listed under the specified attribute |
| 1023 | * of this target in the BUILD file, and that contain the specified provider. |
| 1024 | */ |
dslomov | de965ac | 2017-07-31 21:07:51 +0200 | [diff] [blame] | 1025 | public <C extends Info> Iterable<? extends TransitiveInfoCollection> getPrerequisitesIf( |
jcater | 3a9532d | 2020-08-26 06:09:28 -0700 | [diff] [blame] | 1026 | String attributeName, BuiltinProvider<C> classType) { |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 1027 | return AnalysisUtils.filterByProvider(getPrerequisites(attributeName), classType); |
Googler | 7ac7723 | 2019-06-04 14:26:47 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /** |
jcater | f7d726f | 2020-04-13 10:34:54 -0700 | [diff] [blame] | 1031 | * Returns the prerequisite referred to by the specified attribute. Also checks whether the |
| 1032 | * attribute is marked as executable and that the target referred to can actually be executed. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1033 | * |
jcater | 3a9532d | 2020-08-26 06:09:28 -0700 | [diff] [blame] | 1034 | * @param attributeName the name of the attribute |
| 1035 | * @return the {@link FilesToRunProvider} interface of the prerequisite. |
| 1036 | */ |
| 1037 | @Nullable |
| 1038 | public FilesToRunProvider getExecutablePrerequisite(String attributeName) { |
Greg Estren | 7f53423 | 2016-12-01 21:38:25 +0000 | [diff] [blame] | 1039 | Attribute ruleDefinition = attributes().getAttributeDefinition(attributeName); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1040 | |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 1041 | Preconditions.checkNotNull( |
| 1042 | ruleDefinition, "%s attribute %s is not defined", ruleClassNameForLogging, attributeName); |
| 1043 | Preconditions.checkState( |
| 1044 | ruleDefinition.isExecutable(), |
| 1045 | "%s attribute %s is not configured to be executable", |
| 1046 | ruleClassNameForLogging, |
| 1047 | attributeName); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1048 | |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 1049 | TransitiveInfoCollection prerequisite = getPrerequisite(attributeName); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1050 | if (prerequisite == null) { |
| 1051 | return null; |
| 1052 | } |
| 1053 | |
| 1054 | FilesToRunProvider result = prerequisite.getProvider(FilesToRunProvider.class); |
| 1055 | if (result == null || result.getExecutable() == null) { |
| 1056 | attributeError( |
| 1057 | attributeName, prerequisite.getLabel() + " does not refer to a valid executable target"); |
| 1058 | } |
| 1059 | return result; |
| 1060 | } |
| 1061 | |
ulfjack | 08ff9b8 | 2017-09-28 04:08:06 -0400 | [diff] [blame] | 1062 | public void initConfigurationMakeVariableContext( |
| 1063 | Iterable<? extends MakeVariableSupplier> makeVariableSuppliers) { |
brandjon | 96e223a | 2021-02-01 09:05:36 -0800 | [diff] [blame] | 1064 | Preconditions.checkState( |
| 1065 | configurationMakeVariableContext == null, |
| 1066 | "Attempted to init an already initialized Make var context (did you call" |
| 1067 | + " initConfigurationMakeVariableContext() after accessing ctx.var?)"); |
ulfjack | 08ff9b8 | 2017-09-28 04:08:06 -0400 | [diff] [blame] | 1068 | configurationMakeVariableContext = |
| 1069 | new ConfigurationMakeVariableContext( |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 1070 | this, rule.getPackage(), getConfiguration(), makeVariableSuppliers); |
ulfjack | 08ff9b8 | 2017-09-28 04:08:06 -0400 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | public void initConfigurationMakeVariableContext(MakeVariableSupplier... makeVariableSuppliers) { |
| 1074 | initConfigurationMakeVariableContext(ImmutableList.copyOf(makeVariableSuppliers)); |
| 1075 | } |
| 1076 | |
ulfjack | 01bf32e | 2017-11-02 17:50:07 -0400 | [diff] [blame] | 1077 | public Expander getExpander(TemplateContext templateContext) { |
gregce | 31e4479 | 2020-04-27 09:10:40 -0700 | [diff] [blame] | 1078 | Expander expander = new Expander(this, templateContext); |
| 1079 | makeVariableExpanders.add(expander); |
| 1080 | return expander; |
Liam Miller-Cushon | ce6a7b2 | 2016-02-03 18:12:36 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
ulfjack | 0786013 | 2017-09-29 08:59:44 -0400 | [diff] [blame] | 1083 | public Expander getExpander() { |
gregce | 31e4479 | 2020-04-27 09:10:40 -0700 | [diff] [blame] | 1084 | Expander expander = new Expander(this, getConfigurationMakeVariableContext()); |
| 1085 | makeVariableExpanders.add(expander); |
| 1086 | return expander; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1087 | } |
| 1088 | |
Googler | a45a8e0 | 2019-05-13 11:17:02 -0700 | [diff] [blame] | 1089 | public Expander getExpander(ImmutableMap<Label, ImmutableCollection<Artifact>> labelMap) { |
gregce | 31e4479 | 2020-04-27 09:10:40 -0700 | [diff] [blame] | 1090 | Expander expander = new Expander(this, getConfigurationMakeVariableContext(), labelMap); |
| 1091 | makeVariableExpanders.add(expander); |
| 1092 | return expander; |
Googler | a45a8e0 | 2019-05-13 11:17:02 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1095 | /** |
ulfjack | 08ff9b8 | 2017-09-28 04:08:06 -0400 | [diff] [blame] | 1096 | * Returns a cached context that maps Make variable names (string) to values (string) without any |
| 1097 | * extra {@link MakeVariableSupplier}. |
brandjon | 96e223a | 2021-02-01 09:05:36 -0800 | [diff] [blame] | 1098 | * |
| 1099 | * <p>CAUTION: If there's no context, this will initialize the context with no |
| 1100 | * MakeVariableSuppliers. Call {@link #initConfigurationMakeVariableContext} first if you want to |
| 1101 | * register suppliers. |
hlopko | a477805 | 2017-05-26 11:37:28 +0200 | [diff] [blame] | 1102 | */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1103 | public ConfigurationMakeVariableContext getConfigurationMakeVariableContext() { |
| 1104 | if (configurationMakeVariableContext == null) { |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 1105 | initConfigurationMakeVariableContext(ImmutableList.of()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1106 | } |
| 1107 | return configurationMakeVariableContext; |
| 1108 | } |
| 1109 | |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 1110 | private StarlarkThread createStarlarkThread(Mutability mutability) { |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 1111 | AnalysisEnvironment env = getAnalysisEnvironment(); |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 1112 | StarlarkThread thread = new StarlarkThread(mutability, env.getStarlarkSemantics()); |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 1113 | thread.setPrintHandler(Event.makeDebugPrintHandler(env.getEventHandler())); |
| 1114 | new BazelStarlarkContext( |
| 1115 | BazelStarlarkContext.Phase.ANALYSIS, |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 1116 | ruleClassProvider.getToolsRepository(), |
Googler | 3834975 | 2023-01-11 09:02:30 -0800 | [diff] [blame] | 1117 | /* fragmentNameToClass= */ null, |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 1118 | getSymbolGenerator(), |
ajurkowski | d5fd56e | 2021-08-19 13:56:16 -0700 | [diff] [blame] | 1119 | getLabel(), |
Googler | 3834975 | 2023-01-11 09:02:30 -0800 | [diff] [blame] | 1120 | /* networkAllowlistForTests= */ null) |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 1121 | .storeInThread(thread); |
| 1122 | return thread; |
| 1123 | } |
| 1124 | |
| 1125 | public StarlarkThread getStarlarkThread() { |
| 1126 | return starlarkThread; |
| 1127 | } |
| 1128 | |
| 1129 | /** |
ilist | 2c158fb | 2021-11-30 00:19:59 -0800 | [diff] [blame] | 1130 | * Initializes the StarlarkRuleContext for use and returns it. No-op if already initialized. |
brandjon | f1cc4dd | 2021-02-01 08:53:40 -0800 | [diff] [blame] | 1131 | * |
| 1132 | * <p>Throws RuleErrorException on failure. |
| 1133 | */ |
| 1134 | public StarlarkRuleContext initStarlarkRuleContext() throws RuleErrorException { |
ilist | 2c158fb | 2021-11-30 00:19:59 -0800 | [diff] [blame] | 1135 | if (starlarkRuleContext == null) { |
| 1136 | AspectDescriptor descriptor = |
| 1137 | aspects.isEmpty() ? null : Iterables.getLast(aspects).getDescriptor(); |
| 1138 | this.starlarkRuleContext = new StarlarkRuleContext(this, descriptor); |
| 1139 | } |
brandjon | f1cc4dd | 2021-02-01 08:53:40 -0800 | [diff] [blame] | 1140 | return starlarkRuleContext; |
| 1141 | } |
| 1142 | |
| 1143 | public StarlarkRuleContext getStarlarkRuleContext() { |
| 1144 | Preconditions.checkNotNull(starlarkRuleContext, "Must call initStarlarkRuleContext() first"); |
| 1145 | return starlarkRuleContext; |
| 1146 | } |
| 1147 | |
| 1148 | /** |
brandjon | 4f331eb | 2021-02-01 09:09:39 -0800 | [diff] [blame] | 1149 | * Retrieves the {@code @_builtins}-defined Starlark object registered in the {@code |
| 1150 | * exported_to_java} mapping under the given name. |
| 1151 | * |
| 1152 | * <p>Reports and raises a rule error if no symbol by that name is defined. |
| 1153 | */ |
| 1154 | public Object getStarlarkDefinedBuiltin(String name) |
| 1155 | throws RuleErrorException, InterruptedException { |
| 1156 | Object result = getAnalysisEnvironment().getStarlarkDefinedBuiltins().get(name); |
| 1157 | if (result == null) { |
| 1158 | throwWithRuleError( |
| 1159 | String.format( |
| 1160 | "(Internal error) No symbol named '%s' defined in the @_builtins exported_to_java" |
| 1161 | + " dict", |
| 1162 | name)); |
| 1163 | } |
| 1164 | return result; |
| 1165 | } |
| 1166 | |
| 1167 | /** |
| 1168 | * Calls a Starlark function in this rule's Starlark thread with the given positional and keyword |
| 1169 | * arguments. On failure, calls {@link #throwWithRuleError} with the Starlark stack trace. |
| 1170 | * |
| 1171 | * <p>This convenience method avoids the need to catch EvalException when the failure would just |
| 1172 | * immediately terminate rule analysis anyway. |
| 1173 | */ |
| 1174 | public Object callStarlarkOrThrowRuleError( |
| 1175 | Object func, List<Object> args, Map<String, Object> kwargs) |
| 1176 | throws RuleErrorException, InterruptedException { |
| 1177 | try { |
| 1178 | return Starlark.call(starlarkThread, func, args, kwargs); |
| 1179 | } catch (EvalException e) { |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 1180 | throw throwWithRuleError(e.getMessageWithStack()); |
brandjon | 4f331eb | 2021-02-01 09:09:39 -0800 | [diff] [blame] | 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | /** |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 1185 | * Prepares Starlark objects created during this target's analysis for use by others. Freezes |
| 1186 | * mutability, clears expensive references. |
| 1187 | */ |
| 1188 | @Override |
| 1189 | public void close() { |
| 1190 | starlarkThread.mutability().freeze(); |
brandjon | f1cc4dd | 2021-02-01 08:53:40 -0800 | [diff] [blame] | 1191 | if (starlarkRuleContext != null) { |
| 1192 | starlarkRuleContext.nullify(); |
Googler | 5c8d1d0 | 2023-02-20 06:57:32 -0800 | [diff] [blame] | 1193 | starlarkRuleContext = null; |
brandjon | f1cc4dd | 2021-02-01 08:53:40 -0800 | [diff] [blame] | 1194 | } |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 1195 | } |
| 1196 | |
John Cater | 13263f7 | 2017-05-24 19:06:47 +0200 | [diff] [blame] | 1197 | @Nullable |
jcater | df7689d | 2021-10-14 11:28:53 -0700 | [diff] [blame] | 1198 | public Label targetPlatform() { |
| 1199 | if (toolchainContexts == null) { |
| 1200 | return null; |
| 1201 | } |
| 1202 | PlatformInfo targetPlatform = toolchainContexts.getTargetPlatform(); |
| 1203 | if (targetPlatform == null) { |
| 1204 | return null; |
| 1205 | } |
| 1206 | return targetPlatform.label(); |
| 1207 | } |
| 1208 | |
Googler | 3834975 | 2023-01-11 09:02:30 -0800 | [diff] [blame] | 1209 | private boolean useAutoExecGroupsForRule() { |
| 1210 | if (attributes().has("$use_auto_exec_groups")) { |
| 1211 | return (boolean) attributes().get("$use_auto_exec_groups", Type.BOOLEAN); |
| 1212 | } else { |
| 1213 | return getConfiguration().useAutoExecGroups(); |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | private boolean useAutoExecGroupsForAspect(Aspect aspect) { |
| 1218 | ImmutableMap<String, Attribute> aspectAttributes = aspect.getDefinition().getAttributes(); |
| 1219 | |
| 1220 | if (aspectAttributes.containsKey("$use_auto_exec_groups")) { |
| 1221 | return (boolean) aspectAttributes.get("$use_auto_exec_groups").getDefaultValueUnchecked(); |
| 1222 | } else { |
| 1223 | return getConfiguration().useAutoExecGroups(); |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | public boolean useAutoExecGroups() { |
| 1228 | Aspect aspect = getMainAspect(); |
| 1229 | |
| 1230 | if (aspect == null) { |
| 1231 | return useAutoExecGroupsForRule(); |
| 1232 | } else { |
| 1233 | return useAutoExecGroupsForAspect(aspect); |
| 1234 | } |
| 1235 | } |
| 1236 | |
Googler | 928e0fe | 2023-01-18 02:31:45 -0800 | [diff] [blame] | 1237 | /** |
| 1238 | * Returns the toolchain context from the default exec group. Important note: In case automatic |
Googler | 48adeaa | 2023-01-25 09:48:40 -0800 | [diff] [blame] | 1239 | * exec groups are enabled, use `getToolchainInfo(Label toolchainType)` function. |
Googler | 928e0fe | 2023-01-18 02:31:45 -0800 | [diff] [blame] | 1240 | */ |
jcater | df7689d | 2021-10-14 11:28:53 -0700 | [diff] [blame] | 1241 | @Nullable |
John Cater | cdfa9ca | 2019-04-05 12:32:09 -0700 | [diff] [blame] | 1242 | public ResolvedToolchainContext getToolchainContext() { |
juliexxia | b5f9d74 | 2020-04-09 13:07:13 -0700 | [diff] [blame] | 1243 | return toolchainContexts == null ? null : toolchainContexts.getDefaultToolchainContext(); |
| 1244 | } |
| 1245 | |
| 1246 | @Nullable |
juliexxia | ff58081 | 2020-04-30 15:01:09 -0700 | [diff] [blame] | 1247 | private ResolvedToolchainContext getToolchainContext(String execGroup) { |
| 1248 | return toolchainContexts == null ? null : toolchainContexts.getToolchainContext(execGroup); |
| 1249 | } |
| 1250 | |
Googler | 928e0fe | 2023-01-18 02:31:45 -0800 | [diff] [blame] | 1251 | /** |
| 1252 | * Returns the toolchain info from the default exec group in case automatic exec groups are not |
| 1253 | * enabled. If they are enabled, retrieves toolchain info from the corresponding automatic exec |
| 1254 | * group. |
| 1255 | */ |
| 1256 | @Nullable |
| 1257 | public ToolchainInfo getToolchainInfo(Label toolchainType) { |
| 1258 | ResolvedToolchainContext toolchainContext; |
| 1259 | if (useAutoExecGroups()) { |
| 1260 | toolchainContext = toolchainContexts.getToolchainContext(toolchainType.toString()); |
| 1261 | } else { |
| 1262 | toolchainContext = getToolchainContext(); |
| 1263 | } |
| 1264 | return toolchainContext == null ? null : toolchainContext.forToolchainType(toolchainType); |
| 1265 | } |
| 1266 | |
juliexxia | ff58081 | 2020-04-30 15:01:09 -0700 | [diff] [blame] | 1267 | public boolean hasToolchainContext(String execGroup) { |
| 1268 | return toolchainContexts != null && toolchainContexts.hasToolchainContext(execGroup); |
| 1269 | } |
| 1270 | |
| 1271 | @Nullable |
juliexxia | 0a0a13b | 2020-04-21 15:55:00 -0700 | [diff] [blame] | 1272 | public ToolchainCollection<ResolvedToolchainContext> getToolchainContexts() { |
juliexxia | b5f9d74 | 2020-04-09 13:07:13 -0700 | [diff] [blame] | 1273 | return toolchainContexts; |
John Cater | eca2840 | 2017-05-17 21:44:12 +0200 | [diff] [blame] | 1274 | } |
| 1275 | |
jcater | 645c42b | 2021-05-12 09:35:48 -0700 | [diff] [blame] | 1276 | public ExecGroupCollection getExecGroups() { |
| 1277 | return execGroupCollection; |
| 1278 | } |
| 1279 | |
jcater | 0a1e9eb | 2019-12-17 09:58:38 -0800 | [diff] [blame] | 1280 | public boolean targetPlatformHasConstraint(ConstraintValueInfo constraintValue) { |
juliexxia | cede76b | 2020-09-23 13:49:34 -0700 | [diff] [blame] | 1281 | if (toolchainContexts == null || toolchainContexts.getTargetPlatform() == null) { |
jcater | 0a1e9eb | 2019-12-17 09:58:38 -0800 | [diff] [blame] | 1282 | return false; |
| 1283 | } |
juliexxia | b5f9d74 | 2020-04-09 13:07:13 -0700 | [diff] [blame] | 1284 | // All toolchain contexts should have the same target platform so we access via the default. |
juliexxia | cede76b | 2020-09-23 13:49:34 -0700 | [diff] [blame] | 1285 | return toolchainContexts.getTargetPlatform().constraints().hasConstraintValue(constraintValue); |
jcater | 0a1e9eb | 2019-12-17 09:58:38 -0800 | [diff] [blame] | 1286 | } |
| 1287 | |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 1288 | public ConfiguredRuleClassProvider getRuleClassProvider() { |
| 1289 | return ruleClassProvider; |
gregce | 6acfe4e | 2018-05-25 08:38:39 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Googler | 5c8d1d0 | 2023-02-20 06:57:32 -0800 | [diff] [blame] | 1292 | /** |
| 1293 | * Returns the configuration fragments this rule uses if it should be included for this rule. |
| 1294 | * Otherwise it returns null. |
| 1295 | */ |
| 1296 | @Nullable |
jhorvitz | 6f63d44 | 2021-08-19 10:47:16 -0700 | [diff] [blame] | 1297 | public RequiredConfigFragmentsProvider getRequiredConfigFragments() { |
Googler | 5c8d1d0 | 2023-02-20 06:57:32 -0800 | [diff] [blame] | 1298 | if (requiredConfigFragments == null) { |
| 1299 | return null; |
| 1300 | } |
| 1301 | |
jhorvitz | 6f63d44 | 2021-08-19 10:47:16 -0700 | [diff] [blame] | 1302 | RequiredConfigFragmentsProvider.Builder merged = null; |
jhorvitz | 09e3c53 | 2021-10-04 16:30:39 -0700 | [diff] [blame] | 1303 | |
| 1304 | // Add variables accessed through ctx.var, if this is a Starlark rule. |
| 1305 | if (starlarkRuleContext != null) { |
| 1306 | for (String makeVariable : starlarkRuleContext.lookedUpVariables()) { |
| 1307 | if (isUserDefinedMakeVariable(makeVariable)) { |
jhorvitz | 6f63d44 | 2021-08-19 10:47:16 -0700 | [diff] [blame] | 1308 | if (merged == null) { |
| 1309 | merged = RequiredConfigFragmentsProvider.builder().merge(requiredConfigFragments); |
| 1310 | } |
| 1311 | merged.addDefine(makeVariable); |
gregce | 31e4479 | 2020-04-27 09:10:40 -0700 | [diff] [blame] | 1312 | } |
| 1313 | } |
| 1314 | } |
jhorvitz | 09e3c53 | 2021-10-04 16:30:39 -0700 | [diff] [blame] | 1315 | |
| 1316 | // Add variables accessed through Make variable substitution. |
| 1317 | for (Expander makeVariableExpander : makeVariableExpanders) { |
| 1318 | for (String makeVariable : makeVariableExpander.lookedUpVariables()) { |
| 1319 | if (isUserDefinedMakeVariable(makeVariable)) { |
| 1320 | if (merged == null) { |
| 1321 | merged = RequiredConfigFragmentsProvider.builder().merge(requiredConfigFragments); |
| 1322 | } |
| 1323 | merged.addDefine(makeVariable); |
| 1324 | } |
| 1325 | } |
| 1326 | } |
| 1327 | |
jhorvitz | 6f63d44 | 2021-08-19 10:47:16 -0700 | [diff] [blame] | 1328 | return merged == null ? requiredConfigFragments : merged.build(); |
gregce | 966dc23 | 2019-10-18 15:34:07 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Googler | 720dc5f | 2022-09-16 03:43:51 -0700 | [diff] [blame] | 1331 | /** |
| 1332 | * Returns the set of transitive packages. This is only intended to be used to create the repo |
| 1333 | * mapping manifest for the runfiles tree. Can be null if transitive packages are not tracked (see |
| 1334 | * {@link |
| 1335 | * com.google.devtools.build.lib.skyframe.SkyframeExecutor#shouldStoreTransitivePackagesInLoadingAndAnalysis}). |
| 1336 | */ |
| 1337 | @Nullable |
| 1338 | public NestedSet<Package> getTransitivePackagesForRunfileRepoMappingManifest() { |
| 1339 | return transitivePackagesForRunfileRepoMappingManifest; |
| 1340 | } |
| 1341 | |
jhorvitz | 09e3c53 | 2021-10-04 16:30:39 -0700 | [diff] [blame] | 1342 | private boolean isUserDefinedMakeVariable(String makeVariable) { |
| 1343 | // User-defined make values may be set either in "--define foo=bar" or in a vardef in the rule's |
| 1344 | // package. Both are equivalent for these purposes, since in both cases setting |
| 1345 | // "--define foo=bar" impacts the rule's output. |
| 1346 | return rule.getPackage().getMakeEnvironment().containsKey(makeVariable) |
| 1347 | || getConfiguration().getCommandLineBuildVariables().containsKey(makeVariable); |
| 1348 | } |
| 1349 | |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 1350 | private void checkAttributeIsDependency(String attributeName) { |
Greg Estren | 7f53423 | 2016-12-01 21:38:25 +0000 | [diff] [blame] | 1351 | Attribute attributeDefinition = attributes.getAttributeDefinition(attributeName); |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 1352 | Preconditions.checkNotNull( |
| 1353 | attributeDefinition, |
| 1354 | "%s: %s attribute %s is not defined", |
| 1355 | rule.getLocation(), |
| 1356 | ruleClassNameForLogging, |
| 1357 | attributeName); |
| 1358 | Preconditions.checkState( |
| 1359 | attributeDefinition.getType().getLabelClass() == LabelClass.DEPENDENCY, |
| 1360 | "%s attribute %s is not a label type attribute", |
| 1361 | ruleClassNameForLogging, |
| 1362 | attributeName); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1363 | } |
| 1364 | |
juliexxia | 6fe70c2 | 2020-05-18 14:38:42 -0700 | [diff] [blame] | 1365 | @Override |
| 1366 | @Nullable |
| 1367 | public PlatformInfo getExecutionPlatform() { |
| 1368 | if (getToolchainContext() == null) { |
| 1369 | return null; |
| 1370 | } |
| 1371 | return getToolchainContext().executionPlatform(); |
| 1372 | } |
| 1373 | |
| 1374 | @Override |
| 1375 | @Nullable |
| 1376 | public PlatformInfo getExecutionPlatform(String execGroup) { |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 1377 | if (toolchainContexts == null) { |
juliexxia | 6fe70c2 | 2020-05-18 14:38:42 -0700 | [diff] [blame] | 1378 | return null; |
| 1379 | } |
| 1380 | ResolvedToolchainContext toolchainContext = getToolchainContext(execGroup); |
| 1381 | return toolchainContext == null ? null : toolchainContext.executionPlatform(); |
| 1382 | } |
| 1383 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1384 | /** |
jcater | f7d726f | 2020-04-13 10:34:54 -0700 | [diff] [blame] | 1385 | * For the specified attribute "attributeName" (which must be of type list(label)), resolve all |
| 1386 | * the labels into ConfiguredTargets (for the configuration appropriate to the attribute) and |
| 1387 | * return their build artifacts as a {@link PrerequisiteArtifacts} instance. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1388 | * |
| 1389 | * @param attributeName the name of the attribute to traverse |
| 1390 | */ |
jcater | 3a9532d | 2020-08-26 06:09:28 -0700 | [diff] [blame] | 1391 | public PrerequisiteArtifacts getPrerequisiteArtifacts(String attributeName) { |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 1392 | return PrerequisiteArtifacts.get(this, attributeName); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | /** |
jcater | f7d726f | 2020-04-13 10:34:54 -0700 | [diff] [blame] | 1396 | * For the specified attribute "attributeName" (which must be of type label), resolves the |
| 1397 | * ConfiguredTarget and returns its single build artifact. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1398 | * |
jcater | f7d726f | 2020-04-13 10:34:54 -0700 | [diff] [blame] | 1399 | * <p>If the attribute is optional, has no default and was not specified, then null will be |
| 1400 | * returned. Note also that null is returned (and an attribute error is raised) if there wasn't |
| 1401 | * exactly one build artifact for the target. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1402 | */ |
jcater | 3a9532d | 2020-08-26 06:09:28 -0700 | [diff] [blame] | 1403 | public Artifact getPrerequisiteArtifact(String attributeName) { |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 1404 | TransitiveInfoCollection target = getPrerequisite(attributeName); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1405 | return transitiveInfoCollectionToArtifact(attributeName, target); |
| 1406 | } |
| 1407 | |
Googler | 7b649f9 | 2022-07-06 05:12:46 -0700 | [diff] [blame] | 1408 | @Nullable |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1409 | private Artifact transitiveInfoCollectionToArtifact( |
| 1410 | String attributeName, TransitiveInfoCollection target) { |
| 1411 | if (target != null) { |
ulfjack | 8d8f62f | 2019-12-05 15:03:53 -0800 | [diff] [blame] | 1412 | NestedSet<Artifact> artifacts = target.getProvider(FileProvider.class).getFilesToBuild(); |
| 1413 | if (artifacts.isSingleton()) { |
| 1414 | return artifacts.getSingleton(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1415 | } else { |
| 1416 | attributeError(attributeName, target.getLabel() + " expected a single artifact"); |
| 1417 | } |
| 1418 | } |
| 1419 | return null; |
| 1420 | } |
| 1421 | |
| 1422 | /** |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 1423 | * Returns the sole file in the "srcs" attribute. Reports an error and (possibly) returns null if |
| 1424 | * "srcs" does not identify a single file of the expected type. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1425 | */ |
Googler | 7b649f9 | 2022-07-06 05:12:46 -0700 | [diff] [blame] | 1426 | @Nullable |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1427 | public Artifact getSingleSource(String fileTypeName) { |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 1428 | List<Artifact> srcs = PrerequisiteArtifacts.get(this, "srcs").list(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1429 | switch (srcs.size()) { |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 1430 | case 0: // error already issued by getSrc() |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1431 | return null; |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 1432 | case 1: // ok |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1433 | return Iterables.getOnlyElement(srcs); |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 1434 | default: |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1435 | attributeError("srcs", "only a single " + fileTypeName + " is allowed here"); |
| 1436 | return srcs.get(0); |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | public Artifact getSingleSource() { |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 1441 | return getSingleSource(ruleClassNameForLogging + " source file"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | /** |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 1445 | * Returns a path fragment qualified by the rule name and unique fragment to disambiguate |
| 1446 | * artifacts produced from the source file appearing in multiple rules. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1447 | * |
| 1448 | * <p>For example "pkg/dir/name" -> "pkg/<fragment>/rule/dir/name. |
| 1449 | */ |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 1450 | public PathFragment getUniqueDirectory(String fragment) { |
nharmata | 5e924af | 2017-05-02 18:16:23 +0200 | [diff] [blame] | 1451 | return getUniqueDirectory(PathFragment.create(fragment)); |
| 1452 | } |
| 1453 | |
| 1454 | /** |
David Ostrovsky | a138877 | 2020-07-08 12:11:01 -0700 | [diff] [blame] | 1455 | * Returns a path fragment qualified by the rule name and unique fragment to disambiguate |
| 1456 | * artifacts produced from the source file appearing in multiple rules. |
nharmata | 5e924af | 2017-05-02 18:16:23 +0200 | [diff] [blame] | 1457 | * |
| 1458 | * <p>For example "pkg/dir/name" -> "pkg/<fragment>/rule/dir/name. |
| 1459 | */ |
David Ostrovsky | a138877 | 2020-07-08 12:11:01 -0700 | [diff] [blame] | 1460 | @Override |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 1461 | public PathFragment getUniqueDirectory(PathFragment fragment) { |
Googler | 7b644a6 | 2020-10-21 11:56:33 -0700 | [diff] [blame] | 1462 | return AnalysisUtils.getUniqueDirectory( |
| 1463 | getLabel(), fragment, getConfiguration().isSiblingRepositoryLayout()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | /** |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 1467 | * Check that all targets that were specified as sources are from the same package as this rule. |
| 1468 | * Output a warning or an error for every target that is imported from a different package. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1469 | */ |
| 1470 | public void checkSrcsSamePackage(boolean onlyWarn) { |
| 1471 | PathFragment packageName = getLabel().getPackageFragment(); |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 1472 | for (Artifact srcItem : PrerequisiteArtifacts.get(this, "srcs").list()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1473 | if (!srcItem.isSourceArtifact()) { |
| 1474 | // In theory, we should not do this check. However, in practice, we |
| 1475 | // have a couple of rules that do not obey the "srcs must contain |
| 1476 | // files and only files" rule. Thus, we are stuck with this hack here :( |
| 1477 | continue; |
| 1478 | } |
| 1479 | Label associatedLabel = srcItem.getOwner(); |
| 1480 | PathFragment itemPackageName = associatedLabel.getPackageFragment(); |
| 1481 | if (!itemPackageName.equals(packageName)) { |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 1482 | String message = |
| 1483 | "please do not import '" |
| 1484 | + associatedLabel |
| 1485 | + "' directly. " |
| 1486 | + "You should either move the file to this package or depend on " |
| 1487 | + "an appropriate rule there"; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1488 | if (onlyWarn) { |
| 1489 | attributeWarning("srcs", message); |
| 1490 | } else { |
| 1491 | attributeError("srcs", message); |
| 1492 | } |
| 1493 | } |
| 1494 | } |
| 1495 | } |
| 1496 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1497 | /** |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 1498 | * Returns the label to which the {@code NODEP_LABEL} attribute {@code attrName} refers, checking |
| 1499 | * that it is a valid label, and that it is referring to a local target. Reports a warning |
| 1500 | * otherwise. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1501 | */ |
Googler | 7b649f9 | 2022-07-06 05:12:46 -0700 | [diff] [blame] | 1502 | @Nullable |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1503 | public Label getLocalNodepLabelAttribute(String attrName) { |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 1504 | Label label = attributes().get(attrName, BuildType.NODEP_LABEL); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1505 | if (label == null) { |
| 1506 | return null; |
| 1507 | } |
| 1508 | |
| 1509 | if (!getTarget().getLabel().getPackageFragment().equals(label.getPackageFragment())) { |
| 1510 | attributeWarning(attrName, "does not reference a local rule"); |
| 1511 | } |
| 1512 | |
| 1513 | return label; |
| 1514 | } |
| 1515 | |
asteinb | 0de4546 | 2018-05-17 08:07:12 -0700 | [diff] [blame] | 1516 | @Override |
Florian Weikert | 4b67d4f | 2015-09-14 13:35:34 +0000 | [diff] [blame] | 1517 | public Artifact getImplicitOutputArtifact(ImplicitOutputsFunction function) |
| 1518 | throws InterruptedException { |
Googler | 3834975 | 2023-01-11 09:02:30 -0800 | [diff] [blame] | 1519 | return getImplicitOutputArtifact(function, /* contentBasedPath= */ false); |
gregce | 11f3b0e | 2019-06-07 17:12:06 -0700 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | /** |
| 1523 | * Same as {@link #getImplicitOutputArtifact(ImplicitOutputsFunction)} but includes the option to |
| 1524 | * use a content-based path for this artifact (see {@link |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 1525 | * BuildConfigurationValue#useContentBasedOutputPaths()}). |
gregce | 11f3b0e | 2019-06-07 17:12:06 -0700 | [diff] [blame] | 1526 | */ |
| 1527 | public Artifact getImplicitOutputArtifact( |
| 1528 | ImplicitOutputsFunction function, boolean contentBasedPath) throws InterruptedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1529 | Iterable<String> result; |
| 1530 | try { |
vladmos | 076977e | 2017-12-02 14:15:32 -0800 | [diff] [blame] | 1531 | result = |
| 1532 | function.getImplicitOutputs( |
| 1533 | getAnalysisEnvironment().getEventHandler(), RawAttributeMapper.of(rule)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1534 | } catch (EvalException e) { |
gregce | ca48e9a | 2020-04-14 08:54:38 -0700 | [diff] [blame] | 1535 | // It's ok as long as we don't use this method from Starlark. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1536 | throw new IllegalStateException(e); |
| 1537 | } |
gregce | 11f3b0e | 2019-06-07 17:12:06 -0700 | [diff] [blame] | 1538 | return getImplicitOutputArtifact(Iterables.getOnlyElement(result), contentBasedPath); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1539 | } |
| 1540 | |
gregce | ca48e9a | 2020-04-14 08:54:38 -0700 | [diff] [blame] | 1541 | /** Only use from Starlark. Returns the implicit output artifact for a given output path. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1542 | public Artifact getImplicitOutputArtifact(String path) { |
Googler | 3834975 | 2023-01-11 09:02:30 -0800 | [diff] [blame] | 1543 | return getImplicitOutputArtifact(path, /* contentBasedPath= */ false); |
gregce | 11f3b0e | 2019-06-07 17:12:06 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | /** |
| 1547 | * Same as {@link #getImplicitOutputArtifact(String)} but includes the option to use a a |
| 1548 | * content-based path for this artifact (see {@link |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 1549 | * BuildConfigurationValue#useContentBasedOutputPaths()}). |
gregce | 11f3b0e | 2019-06-07 17:12:06 -0700 | [diff] [blame] | 1550 | */ |
brandjon | d258263 | 2021-01-29 16:10:58 -0800 | [diff] [blame] | 1551 | // TODO(bazel-team): Consider removing contentBasedPath stuff, which is unused as of 18 months |
| 1552 | // after its introduction in cl/252148134. |
gregce | 11f3b0e | 2019-06-07 17:12:06 -0700 | [diff] [blame] | 1553 | private Artifact getImplicitOutputArtifact(String path, boolean contentBasedPath) { |
| 1554 | return getPackageRelativeArtifact(path, getBinOrGenfilesDirectory(), contentBasedPath); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | /** |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 1558 | * Convenience method to return a configured target for the "compiler" attribute. Allows caller to |
| 1559 | * decide whether a warning should be printed if the "compiler" attribute is not set to the |
| 1560 | * default value. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1561 | * |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 1562 | * @param warnIfNotDefault if true, print a warning if the value for the "compiler" attribute is |
| 1563 | * set to something other than the default |
| 1564 | * @return a ConfiguredTarget for the "compiler" attribute |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1565 | */ |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 1566 | public FilesToRunProvider getCompiler(boolean warnIfNotDefault) { |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 1567 | Label label = attributes().get("compiler", BuildType.LABEL); |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 1568 | if (warnIfNotDefault && !label.equals(rule.getAttrDefaultValue("compiler"))) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1569 | attributeWarning("compiler", "setting the compiler is strongly discouraged"); |
| 1570 | } |
jcater | 7ba8310 | 2020-09-10 11:05:29 -0700 | [diff] [blame] | 1571 | return getExecutablePrerequisite("compiler"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | /** |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 1575 | * Returns the (unmodifiable, ordered) list of artifacts which are the outputs of this target. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1576 | * |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 1577 | * <p>Each element in this list is associated with a single output, either declared implicitly |
| 1578 | * (via setImplicitOutputsFunction()) or explicitly (listed in the 'outs' attribute of our rule). |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1579 | */ |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 1580 | public ImmutableList<Artifact> getOutputArtifacts() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1581 | ImmutableList.Builder<Artifact> artifacts = ImmutableList.builder(); |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 1582 | for (OutputFile out : rule.getOutputFiles()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1583 | artifacts.add(createOutputArtifact(out)); |
| 1584 | } |
| 1585 | return artifacts.build(); |
| 1586 | } |
| 1587 | |
| 1588 | /** |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 1589 | * Like {@link #getOutputArtifacts()} but for a singular output item. Reports an error if the |
| 1590 | * "out" attribute is not a singleton. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1591 | * |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 1592 | * @return null if the output list is empty, the artifact for the first item of the output list |
| 1593 | * otherwise |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1594 | */ |
Googler | 7b649f9 | 2022-07-06 05:12:46 -0700 | [diff] [blame] | 1595 | @Nullable |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1596 | public Artifact getOutputArtifact() { |
| 1597 | List<Artifact> outs = getOutputArtifacts(); |
| 1598 | if (outs.size() != 1) { |
| 1599 | attributeError("out", "exactly one output file required"); |
| 1600 | if (outs.isEmpty()) { |
| 1601 | return null; |
| 1602 | } |
| 1603 | } |
| 1604 | return outs.get(0); |
| 1605 | } |
| 1606 | |
plf | 727a07d | 2019-02-01 02:27:35 -0800 | [diff] [blame] | 1607 | @Override |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 1608 | public Artifact.DerivedArtifact getRelatedArtifact(PathFragment pathFragment, String extension) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1609 | PathFragment file = FileSystemUtils.replaceExtension(pathFragment, extension); |
Googler | 0617f2c | 2020-10-22 08:43:54 -0700 | [diff] [blame] | 1610 | return getDerivedArtifact(file, getConfiguration().getBinDirectory(getLabel().getRepository())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1611 | } |
| 1612 | |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 1613 | /** Returns true if the target for this context is a test target. */ |
Googler | f812a2f | 2016-06-13 16:14:10 +0000 | [diff] [blame] | 1614 | public boolean isTestTarget() { |
| 1615 | return TargetUtils.isTestRule(getTarget()); |
| 1616 | } |
| 1617 | |
Googler | cfdeb4d | 2018-02-08 07:13:33 -0800 | [diff] [blame] | 1618 | /** Returns true if the testonly attribute is set on this context. */ |
| 1619 | public boolean isTestOnlyTarget() { |
| 1620 | return attributes().has("testonly", Type.BOOLEAN) && attributes().get("testonly", Type.BOOLEAN); |
| 1621 | } |
| 1622 | |
Googler | f812a2f | 2016-06-13 16:14:10 +0000 | [diff] [blame] | 1623 | /** |
Klaus Aehlig | a85af94 | 2019-10-29 07:48:32 -0700 | [diff] [blame] | 1624 | * Returns true if {@code label} is visible from {@code prerequisite}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1625 | * |
| 1626 | * <p>This only computes the logic as implemented by the visibility system. The final decision |
jcater | 0de1097 | 2020-04-07 12:15:05 -0700 | [diff] [blame] | 1627 | * whether a dependency is allowed is made by {@link PrerequisiteValidator}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1628 | */ |
Klaus Aehlig | a85af94 | 2019-10-29 07:48:32 -0700 | [diff] [blame] | 1629 | public static boolean isVisible(Label label, TransitiveInfoCollection prerequisite) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1630 | // Check visibility attribute |
cushon | 34ff85e | 2017-11-15 08:59:27 -0800 | [diff] [blame] | 1631 | for (PackageGroupContents specification : |
Ulf Adams | cd31d3b | 2019-12-16 00:50:03 -0800 | [diff] [blame] | 1632 | prerequisite.getProvider(VisibilityProvider.class).getVisibility().toList()) { |
Klaus Aehlig | a85af94 | 2019-10-29 07:48:32 -0700 | [diff] [blame] | 1633 | if (specification.containsPackage(label.getPackageIdentifier())) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1634 | return true; |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | return false; |
| 1639 | } |
| 1640 | |
| 1641 | /** |
Klaus Aehlig | a85af94 | 2019-10-29 07:48:32 -0700 | [diff] [blame] | 1642 | * Returns true if {@code rule} is visible from {@code prerequisite}. |
| 1643 | * |
| 1644 | * <p>This only computes the logic as implemented by the visibility system. The final decision |
jcater | 0de1097 | 2020-04-07 12:15:05 -0700 | [diff] [blame] | 1645 | * whether a dependency is allowed is made by {@link PrerequisiteValidator}. |
Klaus Aehlig | a85af94 | 2019-10-29 07:48:32 -0700 | [diff] [blame] | 1646 | */ |
| 1647 | public static boolean isVisible(Rule rule, TransitiveInfoCollection prerequisite) { |
| 1648 | return isVisible(rule.getLabel(), prerequisite); |
| 1649 | } |
| 1650 | |
Googler | 3834975 | 2023-01-11 09:02:30 -0800 | [diff] [blame] | 1651 | /** |
| 1652 | * @return the set of features applicable for the current rule. |
| 1653 | */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1654 | public ImmutableSet<String> getFeatures() { |
Googler | 993f1de | 2018-02-20 02:31:10 -0800 | [diff] [blame] | 1655 | return enabledFeatures; |
| 1656 | } |
| 1657 | |
Googler | 3834975 | 2023-01-11 09:02:30 -0800 | [diff] [blame] | 1658 | /** |
| 1659 | * @return the set of features that are disabled for the current rule. |
| 1660 | */ |
Googler | 993f1de | 2018-02-20 02:31:10 -0800 | [diff] [blame] | 1661 | public ImmutableSet<String> getDisabledFeatures() { |
| 1662 | return disabledFeatures; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1663 | } |
| 1664 | |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 1665 | @Override |
plf | 4310989 | 2019-02-12 05:45:05 -0800 | [diff] [blame] | 1666 | public RuleErrorConsumer getRuleErrorConsumer() { |
| 1667 | return this; |
| 1668 | } |
| 1669 | |
jhorvitz | 7410c80 | 2020-07-22 11:55:07 -0700 | [diff] [blame] | 1670 | /** |
| 1671 | * Returns {@code true} if a {@link RequiredConfigFragmentsProvider} should be included for this |
| 1672 | * rule. |
| 1673 | */ |
| 1674 | public boolean shouldIncludeRequiredConfigFragmentsProvider() { |
jhorvitz | 8128f52 | 2021-08-18 17:33:43 -0700 | [diff] [blame] | 1675 | return requiredConfigFragments != null; |
jhorvitz | 7410c80 | 2020-07-22 11:55:07 -0700 | [diff] [blame] | 1676 | } |
| 1677 | |
plf | 4310989 | 2019-02-12 05:45:05 -0800 | [diff] [blame] | 1678 | @Override |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 1679 | public String toString() { |
| 1680 | return "RuleContext(" + getLabel() + ", " + getConfiguration() + ")"; |
| 1681 | } |
| 1682 | |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 1683 | /** Builder class for a RuleContext. */ |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 1684 | public static final class Builder implements RuleErrorConsumer { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1685 | private final AnalysisEnvironment env; |
lpino | 5fe7ed0 | 2018-07-18 05:55:23 -0700 | [diff] [blame] | 1686 | private final Target target; |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 1687 | private final ImmutableList<Aspect> aspects; |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 1688 | private final BuildConfigurationValue configuration; |
cparsons | e8d450c | 2018-10-04 16:01:53 -0700 | [diff] [blame] | 1689 | private final RuleErrorConsumer reporter; |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 1690 | private ConfiguredRuleClassProvider ruleClassProvider; |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 1691 | private ConfigurationFragmentPolicy configurationFragmentPolicy; |
| 1692 | private ActionLookupKey actionOwnerSymbol; |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 1693 | private OrderedSetMultimap<Attribute, ConfiguredTargetAndData> prerequisiteMap; |
gregce | 79989f9 | 2021-02-01 07:01:55 -0800 | [diff] [blame] | 1694 | private ConfigConditions configConditions; |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 1695 | private Mutability mutability; |
cushon | 34ff85e | 2017-11-15 08:59:27 -0800 | [diff] [blame] | 1696 | private NestedSet<PackageGroupContents> visibility; |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 1697 | private ImmutableMap<String, Attribute> aspectAttributes = ImmutableMap.of(); |
juliexxia | b5f9d74 | 2020-04-09 13:07:13 -0700 | [diff] [blame] | 1698 | private ToolchainCollection<ResolvedToolchainContext> toolchainContexts; |
jcater | 645c42b | 2021-05-12 09:35:48 -0700 | [diff] [blame] | 1699 | private ExecGroupCollection.Builder execGroupCollectionBuilder; |
juliexxia | 3eb806e | 2020-07-15 08:42:34 -0700 | [diff] [blame] | 1700 | private ImmutableMap<String, String> rawExecProperties; |
jhorvitz | 8128f52 | 2021-08-18 17:33:43 -0700 | [diff] [blame] | 1701 | @Nullable private RequiredConfigFragmentsProvider requiredConfigFragments; |
Googler | 720dc5f | 2022-09-16 03:43:51 -0700 | [diff] [blame] | 1702 | @Nullable private NestedSet<Package> transitivePackagesForRunfileRepoMappingManifest; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1703 | |
ulfjack | 865b621 | 2018-06-14 03:41:55 -0700 | [diff] [blame] | 1704 | @VisibleForTesting |
| 1705 | public Builder( |
Michael Staib | 8824d5e | 2016-01-20 21:37:05 +0000 | [diff] [blame] | 1706 | AnalysisEnvironment env, |
lpino | 5fe7ed0 | 2018-07-18 05:55:23 -0700 | [diff] [blame] | 1707 | Target target, |
dslomov | 44d1571 | 2017-12-20 05:42:28 -0800 | [diff] [blame] | 1708 | ImmutableList<Aspect> aspects, |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 1709 | BuildConfigurationValue configuration) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1710 | this.env = Preconditions.checkNotNull(env); |
lpino | 5fe7ed0 | 2018-07-18 05:55:23 -0700 | [diff] [blame] | 1711 | this.target = Preconditions.checkNotNull(target); |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 1712 | this.aspects = Preconditions.checkNotNull(aspects); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1713 | this.configuration = Preconditions.checkNotNull(configuration); |
cparsons | e8d450c | 2018-10-04 16:01:53 -0700 | [diff] [blame] | 1714 | if (configuration.allowAnalysisFailures()) { |
| 1715 | reporter = new SuppressingErrorReporter(); |
| 1716 | } else { |
ulfjack | 0791764 | 2019-09-12 02:09:36 -0700 | [diff] [blame] | 1717 | reporter = |
| 1718 | new ErrorReporter( |
| 1719 | env, target.getAssociatedRule(), configuration, getRuleClassNameForLogging()); |
cparsons | e8d450c | 2018-10-04 16:01:53 -0700 | [diff] [blame] | 1720 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1721 | } |
| 1722 | |
Googler | 2570fe1 | 2022-08-26 07:30:31 -0700 | [diff] [blame] | 1723 | /** |
| 1724 | * Same as {@link #build}, except without some attribute checks. |
| 1725 | * |
| 1726 | * <p>Don't use this function outside of testing. The use should be limited to cases where |
| 1727 | * specifying ConfigConditions.EMPTY, which can cause a noMatchError when accessing attributes |
| 1728 | * within attribute checking. |
| 1729 | */ |
| 1730 | @VisibleForTesting |
| 1731 | public RuleContext unsafeBuild() throws InvalidExecGroupException { |
| 1732 | return build(false); |
| 1733 | } |
| 1734 | |
ulfjack | 865b621 | 2018-06-14 03:41:55 -0700 | [diff] [blame] | 1735 | @VisibleForTesting |
juliexxia | 6fe70c2 | 2020-05-18 14:38:42 -0700 | [diff] [blame] | 1736 | public RuleContext build() throws InvalidExecGroupException { |
Googler | 2570fe1 | 2022-08-26 07:30:31 -0700 | [diff] [blame] | 1737 | return build(true); |
| 1738 | } |
| 1739 | |
| 1740 | private RuleContext build(boolean attributeChecks) throws InvalidExecGroupException { |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 1741 | Preconditions.checkNotNull(ruleClassProvider); |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 1742 | Preconditions.checkNotNull(configurationFragmentPolicy); |
| 1743 | Preconditions.checkNotNull(actionOwnerSymbol); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1744 | Preconditions.checkNotNull(prerequisiteMap); |
| 1745 | Preconditions.checkNotNull(configConditions); |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 1746 | Preconditions.checkNotNull(mutability); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1747 | Preconditions.checkNotNull(visibility); |
Googler | 2570fe1 | 2022-08-26 07:30:31 -0700 | [diff] [blame] | 1748 | ConfiguredAttributeMapper attributes = |
gregce | 3d33d7b | 2021-04-20 10:35:03 -0700 | [diff] [blame] | 1749 | ConfiguredAttributeMapper.of( |
jcater | ae8f15e | 2021-11-19 13:29:59 -0800 | [diff] [blame] | 1750 | target.getAssociatedRule(), configConditions.asProviders(), configuration); |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 1751 | ListMultimap<String, ConfiguredTargetAndData> targetMap = createTargetMap(); |
Googler | 9ba1ada | 2022-10-11 09:30:38 -0700 | [diff] [blame] | 1752 | // These checks can fail when ConfigConditions.EMPTY are empty, resulting in noMatchError |
| 1753 | // accessing attributes without a default condition. |
| 1754 | // ConfigConditions.EMPTY is always true for non-rules: |
| 1755 | // https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/skyframe/ConfiguredTargetFunction.java;l=943;drc=720dc5fd640de692db129777c7c7c32924627c43 |
| 1756 | // This can happen in BuildViewForTesting.getRuleContextForTesting as it specifies |
| 1757 | // ConfigConditions.EMPTY. |
| 1758 | if (attributeChecks && target instanceof Rule) { |
Googler | 2570fe1 | 2022-08-26 07:30:31 -0700 | [diff] [blame] | 1759 | checkAttributesNonEmpty(attributes); |
| 1760 | checkAttributesForDuplicateLabels(attributes); |
| 1761 | } |
gregce | 79989f9 | 2021-02-01 07:01:55 -0800 | [diff] [blame] | 1762 | // This conditionally checks visibility on config_setting rules based on |
| 1763 | // --config_setting_visibility_policy. This should be removed as soon as it's deemed safe |
Googler | 2570fe1 | 2022-08-26 07:30:31 -0700 | [diff] [blame] | 1764 | // to unconditionally check visibility. See |
| 1765 | // https://github.com/bazelbuild/bazel/issues/12669. |
Googler | a3a4cf8 | 2022-10-07 11:34:58 -0700 | [diff] [blame] | 1766 | ConfigSettingVisibilityPolicy configSettingVisibilityPolicy = |
| 1767 | target.getPackage().getConfigSettingVisibilityPolicy(); |
| 1768 | if (configSettingVisibilityPolicy != ConfigSettingVisibilityPolicy.LEGACY_OFF) { |
gregce | 79989f9 | 2021-02-01 07:01:55 -0800 | [diff] [blame] | 1769 | Attribute configSettingAttr = attributes.getAttributeDefinition("$config_dependencies"); |
| 1770 | for (ConfiguredTargetAndData condition : configConditions.asConfiguredTargets().values()) { |
Googler | a3a4cf8 | 2022-10-07 11:34:58 -0700 | [diff] [blame] | 1771 | validateDirectPrerequisite( |
| 1772 | configSettingAttr, |
| 1773 | // Another nuance: when both --incompatible_enforce_config_setting_visibility and |
| 1774 | // --incompatible_config_setting_private_default_visibility are disabled, both of |
| 1775 | // these are ignored: |
| 1776 | // |
| 1777 | // - visibility settings on a select() -> config_setting dep |
| 1778 | // - visibility settings on a select() -> alias -> config_setting dep chain |
| 1779 | // |
| 1780 | // In that scenario, both are ignored because the logic here that checks the |
| 1781 | // select() -> ??? edge is completely skipped. |
| 1782 | // |
| 1783 | // When just --incompatible_enforce_config_setting_visibility is on, that means |
| 1784 | // "enforce config_setting visibility with public default". That's a temporary state |
| 1785 | // to support depot migration. In that case, we continue to ignore the alias' |
| 1786 | // visibility in preference for the config_setting. So skip select() -> alias as |
| 1787 | // before, but now enforce select() -> config_setting_the_alias_refers_to. |
| 1788 | // |
| 1789 | // When we also turn on --incompatible_config_setting_private_default_visibility, we |
| 1790 | // expect full standard visibility compliance. In that case we directly evaluate the |
| 1791 | // alias visibility, as is usual semantics. So two the following two edges are |
| 1792 | // checked: 1: select() -> alias and 2: alias -> config_setting. |
| 1793 | configSettingVisibilityPolicy == ConfigSettingVisibilityPolicy.DEFAULT_PUBLIC |
Googler | 268be23 | 2022-10-11 07:59:09 -0700 | [diff] [blame] | 1794 | ? condition.fromConfiguredTargetNoCheck( |
| 1795 | condition.getConfiguredTarget().getActual()) |
Googler | a3a4cf8 | 2022-10-07 11:34:58 -0700 | [diff] [blame] | 1796 | : condition); |
gregce | 79989f9 | 2021-02-01 07:01:55 -0800 | [diff] [blame] | 1797 | } |
| 1798 | } |
jcater | 645c42b | 2021-05-12 09:35:48 -0700 | [diff] [blame] | 1799 | |
Michael Staib | 8824d5e | 2016-01-20 21:37:05 +0000 | [diff] [blame] | 1800 | return new RuleContext( |
| 1801 | this, |
Nathan Harmata | fcb1711 | 2016-04-13 16:56:58 +0000 | [diff] [blame] | 1802 | attributes, |
Michael Staib | 8824d5e | 2016-01-20 21:37:05 +0000 | [diff] [blame] | 1803 | targetMap, |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 1804 | createExecGroupCollection(execGroupCollectionBuilder, attributes)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1805 | } |
| 1806 | |
jcater | 645c42b | 2021-05-12 09:35:48 -0700 | [diff] [blame] | 1807 | private ExecGroupCollection createExecGroupCollection( |
| 1808 | ExecGroupCollection.Builder execGroupCollectionBuilder, AttributeMap attributes) |
| 1809 | throws InvalidExecGroupException { |
| 1810 | if (rawExecProperties == null) { |
jcater | c41ff07 | 2021-05-24 14:12:06 -0700 | [diff] [blame] | 1811 | if (!attributes.has(RuleClass.EXEC_PROPERTIES_ATTR, Type.STRING_DICT)) { |
jcater | 645c42b | 2021-05-12 09:35:48 -0700 | [diff] [blame] | 1812 | rawExecProperties = ImmutableMap.of(); |
| 1813 | } else { |
| 1814 | rawExecProperties = |
jcater | c41ff07 | 2021-05-24 14:12:06 -0700 | [diff] [blame] | 1815 | ImmutableMap.copyOf(attributes.get(RuleClass.EXEC_PROPERTIES_ATTR, Type.STRING_DICT)); |
jcater | 645c42b | 2021-05-12 09:35:48 -0700 | [diff] [blame] | 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | return execGroupCollectionBuilder.build(toolchainContexts, rawExecProperties); |
| 1820 | } |
| 1821 | |
adonovan | ec1cdc9 | 2020-08-07 08:15:51 -0700 | [diff] [blame] | 1822 | private void checkAttributesNonEmpty(AttributeMap attributes) { |
| 1823 | for (String attributeName : attributes.getAttributeNames()) { |
| 1824 | Attribute attr = attributes.getAttributeDefinition(attributeName); |
| 1825 | if (!attr.isNonEmpty()) { |
| 1826 | continue; |
| 1827 | } |
| 1828 | Object attributeValue = attributes.get(attributeName, attr.getType()); |
| 1829 | |
| 1830 | // TODO(adonovan): define in terms of Starlark.len? |
| 1831 | boolean isEmpty = false; |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 1832 | if (attributeValue instanceof List) { |
| 1833 | isEmpty = ((List<?>) attributeValue).isEmpty(); |
| 1834 | } else if (attributeValue instanceof Map) { |
| 1835 | isEmpty = ((Map<?, ?>) attributeValue).isEmpty(); |
adonovan | ec1cdc9 | 2020-08-07 08:15:51 -0700 | [diff] [blame] | 1836 | } |
| 1837 | if (isEmpty) { |
| 1838 | reporter.attributeError(attr.getName(), "attribute must be non empty"); |
| 1839 | } |
| 1840 | } |
Nathan Harmata | fcb1711 | 2016-04-13 16:56:58 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Googler | 2570fe1 | 2022-08-26 07:30:31 -0700 | [diff] [blame] | 1843 | private void checkAttributesForDuplicateLabels(ConfiguredAttributeMapper attributes) { |
| 1844 | for (String attributeName : attributes.getAttributeNames()) { |
| 1845 | Attribute attr = attributes.getAttributeDefinition(attributeName); |
| 1846 | if (attr.getType() != BuildType.LABEL_LIST) { |
| 1847 | continue; |
| 1848 | } |
| 1849 | |
| 1850 | Set<Label> duplicates = attributes.checkForDuplicateLabels(attr); |
| 1851 | for (Label label : duplicates) { |
| 1852 | reporter.attributeError(attr.getName(), String.format("Label '%s' is duplicated", label)); |
| 1853 | } |
| 1854 | } |
| 1855 | } |
| 1856 | |
Kurt Alfred Kluever | 4192ca6 | 2022-07-05 06:30:29 -0700 | [diff] [blame] | 1857 | @CanIgnoreReturnValue |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 1858 | public Builder setRuleClassProvider(ConfiguredRuleClassProvider ruleClassProvider) { |
| 1859 | this.ruleClassProvider = ruleClassProvider; |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 1860 | return this; |
| 1861 | } |
| 1862 | |
Kurt Alfred Kluever | 4192ca6 | 2022-07-05 06:30:29 -0700 | [diff] [blame] | 1863 | @CanIgnoreReturnValue |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 1864 | public Builder setConfigurationFragmentPolicy(ConfigurationFragmentPolicy policy) { |
| 1865 | this.configurationFragmentPolicy = policy; |
| 1866 | return this; |
| 1867 | } |
| 1868 | |
Kurt Alfred Kluever | 4192ca6 | 2022-07-05 06:30:29 -0700 | [diff] [blame] | 1869 | @CanIgnoreReturnValue |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 1870 | public Builder setActionOwnerSymbol(ActionLookupKey actionOwnerSymbol) { |
| 1871 | this.actionOwnerSymbol = actionOwnerSymbol; |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 1872 | return this; |
| 1873 | } |
| 1874 | |
Kurt Alfred Kluever | 4192ca6 | 2022-07-05 06:30:29 -0700 | [diff] [blame] | 1875 | @CanIgnoreReturnValue |
brandjon | bfd332e | 2020-12-17 14:56:04 -0800 | [diff] [blame] | 1876 | public Builder setMutability(Mutability mutability) { |
| 1877 | this.mutability = mutability; |
| 1878 | return this; |
| 1879 | } |
| 1880 | |
Kurt Alfred Kluever | 4192ca6 | 2022-07-05 06:30:29 -0700 | [diff] [blame] | 1881 | @CanIgnoreReturnValue |
ulfjack | 865b621 | 2018-06-14 03:41:55 -0700 | [diff] [blame] | 1882 | public Builder setVisibility(NestedSet<PackageGroupContents> visibility) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1883 | this.visibility = visibility; |
| 1884 | return this; |
| 1885 | } |
| 1886 | |
| 1887 | /** |
| 1888 | * Sets the prerequisites and checks their visibility. It also generates appropriate error or |
| 1889 | * warning messages and sets the error flag as appropriate. |
| 1890 | */ |
Kurt Alfred Kluever | 4192ca6 | 2022-07-05 06:30:29 -0700 | [diff] [blame] | 1891 | @CanIgnoreReturnValue |
ulfjack | 865b621 | 2018-06-14 03:41:55 -0700 | [diff] [blame] | 1892 | public Builder setPrerequisites( |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 1893 | OrderedSetMultimap<Attribute, ConfiguredTargetAndData> prerequisiteMap) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1894 | this.prerequisiteMap = Preconditions.checkNotNull(prerequisiteMap); |
| 1895 | return this; |
| 1896 | } |
| 1897 | |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 1898 | /** Adds attributes which are defined by an Aspect (and not by RuleClass). */ |
Kurt Alfred Kluever | 4192ca6 | 2022-07-05 06:30:29 -0700 | [diff] [blame] | 1899 | @CanIgnoreReturnValue |
ulfjack | 865b621 | 2018-06-14 03:41:55 -0700 | [diff] [blame] | 1900 | public Builder setAspectAttributes(Map<String, Attribute> aspectAttributes) { |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 1901 | this.aspectAttributes = ImmutableMap.copyOf(aspectAttributes); |
Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 1902 | return this; |
| 1903 | } |
| 1904 | |
| 1905 | /** |
gregce | 79989f9 | 2021-02-01 07:01:55 -0800 | [diff] [blame] | 1906 | * Sets the configuration conditions needed to determine which paths to follow for this rule's |
| 1907 | * configurable attributes. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1908 | */ |
Kurt Alfred Kluever | 4192ca6 | 2022-07-05 06:30:29 -0700 | [diff] [blame] | 1909 | @CanIgnoreReturnValue |
gregce | 79989f9 | 2021-02-01 07:01:55 -0800 | [diff] [blame] | 1910 | public Builder setConfigConditions(ConfigConditions configConditions) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1911 | this.configConditions = Preconditions.checkNotNull(configConditions); |
| 1912 | return this; |
| 1913 | } |
| 1914 | |
juliexxia | b5f9d74 | 2020-04-09 13:07:13 -0700 | [diff] [blame] | 1915 | /** Sets the collection of {@link ResolvedToolchainContext}s available to this rule. */ |
Kurt Alfred Kluever | 4192ca6 | 2022-07-05 06:30:29 -0700 | [diff] [blame] | 1916 | @CanIgnoreReturnValue |
juliexxia | b5f9d74 | 2020-04-09 13:07:13 -0700 | [diff] [blame] | 1917 | @VisibleForTesting |
| 1918 | public Builder setToolchainContexts( |
| 1919 | ToolchainCollection<ResolvedToolchainContext> toolchainContexts) { |
| 1920 | Preconditions.checkState( |
| 1921 | this.toolchainContexts == null, |
| 1922 | "toolchainContexts has already been set for this Builder"); |
| 1923 | this.toolchainContexts = toolchainContexts; |
John Cater | 13263f7 | 2017-05-24 19:06:47 +0200 | [diff] [blame] | 1924 | return this; |
| 1925 | } |
| 1926 | |
Kurt Alfred Kluever | 4192ca6 | 2022-07-05 06:30:29 -0700 | [diff] [blame] | 1927 | @CanIgnoreReturnValue |
jcater | 645c42b | 2021-05-12 09:35:48 -0700 | [diff] [blame] | 1928 | public Builder setExecGroupCollectionBuilder( |
| 1929 | ExecGroupCollection.Builder execGroupCollectionBuilder) { |
| 1930 | this.execGroupCollectionBuilder = execGroupCollectionBuilder; |
| 1931 | return this; |
| 1932 | } |
| 1933 | |
juliexxia | 3eb806e | 2020-07-15 08:42:34 -0700 | [diff] [blame] | 1934 | /** |
| 1935 | * Warning: if you set the exec properties using this method any exec_properties attribute value |
| 1936 | * will be ignored in favor of this value. |
| 1937 | */ |
Kurt Alfred Kluever | 4192ca6 | 2022-07-05 06:30:29 -0700 | [diff] [blame] | 1938 | @CanIgnoreReturnValue |
juliexxia | 3eb806e | 2020-07-15 08:42:34 -0700 | [diff] [blame] | 1939 | public Builder setExecProperties(ImmutableMap<String, String> execProperties) { |
| 1940 | this.rawExecProperties = execProperties; |
| 1941 | return this; |
| 1942 | } |
| 1943 | |
Kurt Alfred Kluever | 4192ca6 | 2022-07-05 06:30:29 -0700 | [diff] [blame] | 1944 | @CanIgnoreReturnValue |
jhorvitz | 8128f52 | 2021-08-18 17:33:43 -0700 | [diff] [blame] | 1945 | public Builder setRequiredConfigFragments( |
| 1946 | @Nullable RequiredConfigFragmentsProvider requiredConfigFragments) { |
gregce | 966dc23 | 2019-10-18 15:34:07 -0700 | [diff] [blame] | 1947 | this.requiredConfigFragments = requiredConfigFragments; |
| 1948 | return this; |
| 1949 | } |
| 1950 | |
Googler | 720dc5f | 2022-09-16 03:43:51 -0700 | [diff] [blame] | 1951 | @CanIgnoreReturnValue |
| 1952 | public Builder setTransitivePackagesForRunfileRepoMappingManifest( |
| 1953 | @Nullable NestedSet<Package> packages) { |
| 1954 | this.transitivePackagesForRunfileRepoMappingManifest = packages; |
| 1955 | return this; |
| 1956 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1957 | |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 1958 | /** Determines and returns a map from attribute name to list of configured targets. */ |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 1959 | private ImmutableSortedKeyListMultimap<String, ConfiguredTargetAndData> createTargetMap() { |
| 1960 | ImmutableSortedKeyListMultimap.Builder<String, ConfiguredTargetAndData> mapBuilder = |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1961 | ImmutableSortedKeyListMultimap.builder(); |
| 1962 | |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 1963 | for (Map.Entry<Attribute, Collection<ConfiguredTargetAndData>> entry : |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1964 | prerequisiteMap.asMap().entrySet()) { |
| 1965 | Attribute attribute = entry.getKey(); |
| 1966 | if (attribute == null) { |
| 1967 | continue; |
| 1968 | } |
lberki | 1cd6d1e | 2017-06-14 16:20:19 +0200 | [diff] [blame] | 1969 | |
| 1970 | if (attribute.isSingleArtifact() && entry.getValue().size() > 1) { |
| 1971 | attributeError(attribute.getName(), "must contain a single dependency"); |
| 1972 | continue; |
| 1973 | } |
| 1974 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1975 | if (attribute.isSilentRuleClassFilter()) { |
Googler | f592470 | 2022-12-08 14:55:42 -0800 | [diff] [blame] | 1976 | Predicate<String> filter = attribute.getAllowedRuleClassPredicate(); |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 1977 | for (ConfiguredTargetAndData configuredTarget : entry.getValue()) { |
Googler | f592470 | 2022-12-08 14:55:42 -0800 | [diff] [blame] | 1978 | if (filter.apply(configuredTarget.getRuleClass())) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1979 | validateDirectPrerequisite(attribute, configuredTarget); |
| 1980 | mapBuilder.put(attribute.getName(), configuredTarget); |
| 1981 | } |
| 1982 | } |
| 1983 | } else { |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 1984 | for (ConfiguredTargetAndData configuredTarget : entry.getValue()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1985 | validateDirectPrerequisite(attribute, configuredTarget); |
| 1986 | mapBuilder.put(attribute.getName(), configuredTarget); |
| 1987 | } |
| 1988 | } |
| 1989 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1990 | return mapBuilder.build(); |
| 1991 | } |
| 1992 | |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 1993 | @Override |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1994 | public void ruleError(String message) { |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 1995 | reporter.ruleError(message); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1996 | } |
| 1997 | |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 1998 | @Override |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1999 | public void attributeError(String attrName, String message) { |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 2000 | reporter.attributeError(attrName, message); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2001 | } |
| 2002 | |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 2003 | @Override |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2004 | public void ruleWarning(String message) { |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 2005 | reporter.ruleWarning(message); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2006 | } |
| 2007 | |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 2008 | @Override |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2009 | public void attributeWarning(String attrName, String message) { |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 2010 | reporter.attributeWarning(attrName, message); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2011 | } |
| 2012 | |
cparsons | 0dcffc5 | 2017-10-13 23:40:31 +0200 | [diff] [blame] | 2013 | @Override |
cparsons | 0dcffc5 | 2017-10-13 23:40:31 +0200 | [diff] [blame] | 2014 | public boolean hasErrors() { |
| 2015 | return reporter.hasErrors(); |
| 2016 | } |
| 2017 | |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 2018 | private static String badPrerequisiteMessage( |
lberki | ee2dcae | 2018-03-28 05:49:04 -0700 | [diff] [blame] | 2019 | ConfiguredTargetAndData prerequisite, String reason, boolean isWarning) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2020 | String msgReason = reason != null ? " (" + reason + ")" : ""; |
| 2021 | if (isWarning) { |
Googler | e49cd59 | 2016-07-29 19:32:38 +0000 | [diff] [blame] | 2022 | return String.format( |
lberki | ee2dcae | 2018-03-28 05:49:04 -0700 | [diff] [blame] | 2023 | "%s is unexpected here%s; continuing anyway", |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 2024 | AliasProvider.describeTargetWithAliases(prerequisite, TargetMode.WITH_KIND), msgReason); |
Googler | e49cd59 | 2016-07-29 19:32:38 +0000 | [diff] [blame] | 2025 | } |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 2026 | return String.format( |
| 2027 | "%s is misplaced here%s", |
lberki | ee2dcae | 2018-03-28 05:49:04 -0700 | [diff] [blame] | 2028 | AliasProvider.describeTargetWithAliases(prerequisite, TargetMode.WITH_KIND), msgReason); |
Googler | e49cd59 | 2016-07-29 19:32:38 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 2031 | private void reportBadPrerequisite( |
| 2032 | Attribute attribute, |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 2033 | ConfiguredTargetAndData prerequisite, |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 2034 | String reason, |
| 2035 | boolean isWarning) { |
lberki | ee2dcae | 2018-03-28 05:49:04 -0700 | [diff] [blame] | 2036 | String message = badPrerequisiteMessage(prerequisite, reason, isWarning); |
Googler | e49cd59 | 2016-07-29 19:32:38 +0000 | [diff] [blame] | 2037 | if (isWarning) { |
| 2038 | attributeWarning(attribute.getName(), message); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2039 | } else { |
Googler | e49cd59 | 2016-07-29 19:32:38 +0000 | [diff] [blame] | 2040 | attributeError(attribute.getName(), message); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2041 | } |
| 2042 | } |
| 2043 | |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 2044 | private void validateDirectPrerequisiteType( |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 2045 | ConfiguredTargetAndData prerequisite, Attribute attribute) { |
Googler | f592470 | 2022-12-08 14:55:42 -0800 | [diff] [blame] | 2046 | String ruleClass = prerequisite.getRuleClass(); |
| 2047 | if (!ruleClass.isEmpty()) { |
lpino | 5fe7ed0 | 2018-07-18 05:55:23 -0700 | [diff] [blame] | 2048 | String reason = |
| 2049 | attribute |
| 2050 | .getValidityPredicate() |
Googler | f592470 | 2022-12-08 14:55:42 -0800 | [diff] [blame] | 2051 | .checkValid(target.getAssociatedRule(), ruleClass, prerequisite.getRuleTags()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2052 | if (reason != null) { |
lberki | ee2dcae | 2018-03-28 05:49:04 -0700 | [diff] [blame] | 2053 | reportBadPrerequisite(attribute, prerequisite, reason, false); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2054 | } |
Googler | f592470 | 2022-12-08 14:55:42 -0800 | [diff] [blame] | 2055 | validateRuleDependency(prerequisite, attribute); |
| 2056 | return; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2057 | } |
| 2058 | |
Googler | f592470 | 2022-12-08 14:55:42 -0800 | [diff] [blame] | 2059 | if (!(prerequisite.isTargetFile() && attribute.isStrictLabelCheckingEnabled())) { |
| 2060 | return; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2061 | } |
Googler | f592470 | 2022-12-08 14:55:42 -0800 | [diff] [blame] | 2062 | |
| 2063 | Label prerequisiteTargetLabel = prerequisite.getTargetLabel(); |
| 2064 | if (attribute.getAllowedFileTypesPredicate().apply(prerequisiteTargetLabel.getName())) { |
| 2065 | return; |
| 2066 | } |
| 2067 | |
| 2068 | if (prerequisite.isTargetInputFile() && !prerequisite.getInputPath().exists()) { |
| 2069 | // Misplaced labels, no corresponding target exists |
| 2070 | if (attribute.getAllowedFileTypesPredicate().isNone() |
| 2071 | && !prerequisiteTargetLabel.getName().contains(".")) { |
| 2072 | // There are no allowed files in the attribute but it's not a valid rule, |
| 2073 | // and the filename doesn't contain a dot --> probably a misspelled rule |
| 2074 | attributeError( |
| 2075 | attribute.getName(), "rule '" + prerequisiteTargetLabel + "' does not exist"); |
| 2076 | } else { |
| 2077 | attributeError( |
| 2078 | attribute.getName(), "target '" + prerequisiteTargetLabel + "' does not exist"); |
| 2079 | } |
| 2080 | return; |
| 2081 | } |
| 2082 | // The file exists but has a bad extension |
| 2083 | reportBadPrerequisite( |
| 2084 | attribute, prerequisite, "expected " + attribute.getAllowedFileTypesPredicate(), false); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2085 | } |
| 2086 | |
Michael Staib | 2707a88 | 2016-09-16 21:06:40 +0000 | [diff] [blame] | 2087 | /** Returns whether the context being constructed is for the evaluation of an aspect. */ |
| 2088 | public boolean forAspect() { |
dslomov | 44d1571 | 2017-12-20 05:42:28 -0800 | [diff] [blame] | 2089 | return !aspects.isEmpty(); |
Michael Staib | 2707a88 | 2016-09-16 21:06:40 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2092 | public Rule getRule() { |
lpino | 5fe7ed0 | 2018-07-18 05:55:23 -0700 | [diff] [blame] | 2093 | return target.getAssociatedRule(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2094 | } |
| 2095 | |
Michael Staib | 8824d5e | 2016-01-20 21:37:05 +0000 | [diff] [blame] | 2096 | /** |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 2097 | * Returns the {@link StarlarkSemantics} governs the building of this rule (and the rest of the |
| 2098 | * build). |
Klaus Aehlig | 3805300 | 2019-10-29 09:54:32 -0700 | [diff] [blame] | 2099 | */ |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 2100 | public StarlarkSemantics getStarlarkSemantics() { |
gregce | 61ec8d1 | 2020-05-18 11:02:05 -0700 | [diff] [blame] | 2101 | return env.getStarlarkSemantics(); |
Klaus Aehlig | 3805300 | 2019-10-29 09:54:32 -0700 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | /** |
Michael Staib | 8824d5e | 2016-01-20 21:37:05 +0000 | [diff] [blame] | 2105 | * Returns a rule class name suitable for log messages, including an aspect name if applicable. |
| 2106 | */ |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 2107 | private String getRuleClassNameForLogging() { |
dslomov | 44d1571 | 2017-12-20 05:42:28 -0800 | [diff] [blame] | 2108 | if (aspects.isEmpty()) { |
lpino | 5fe7ed0 | 2018-07-18 05:55:23 -0700 | [diff] [blame] | 2109 | return target.getAssociatedRule().getRuleClass(); |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
dslomov | 44d1571 | 2017-12-20 05:42:28 -0800 | [diff] [blame] | 2112 | return Joiner.on(",") |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 2113 | .join(aspects.stream().map(Aspect::getDescriptor).collect(Collectors.toList())) |
dslomov | 44d1571 | 2017-12-20 05:42:28 -0800 | [diff] [blame] | 2114 | + " aspect on " |
lpino | 5fe7ed0 | 2018-07-18 05:55:23 -0700 | [diff] [blame] | 2115 | + target.getAssociatedRule().getRuleClass(); |
Michael Staib | 8824d5e | 2016-01-20 21:37:05 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 2118 | public BuildConfigurationValue getConfiguration() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2119 | return configuration; |
| 2120 | } |
| 2121 | |
| 2122 | /** |
| 2123 | * @return true if {@code rule} is visible from {@code prerequisite}. |
jcater | 0de1097 | 2020-04-07 12:15:05 -0700 | [diff] [blame] | 2124 | * <p>This only computes the logic as implemented by the visibility system. The final |
| 2125 | * decision whether a dependency is allowed is made by {@link PrerequisiteValidator}, who is |
| 2126 | * supposed to call this method to determine whether a dependency is allowed as per |
| 2127 | * visibility rules. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2128 | */ |
| 2129 | public boolean isVisible(TransitiveInfoCollection prerequisite) { |
lpino | 5fe7ed0 | 2018-07-18 05:55:23 -0700 | [diff] [blame] | 2130 | return RuleContext.isVisible(target.getAssociatedRule(), prerequisite); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2131 | } |
| 2132 | |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 2133 | private void validateDirectPrerequisiteFileTypes( |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 2134 | ConfiguredTargetAndData prerequisite, Attribute attribute) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2135 | if (attribute.isSkipAnalysisTimeFileTypeCheck()) { |
| 2136 | return; |
| 2137 | } |
| 2138 | FileTypeSet allowedFileTypes = attribute.getAllowedFileTypesPredicate(); |
Ulf Adams | 788fd1a | 2015-03-12 13:54:09 +0000 | [diff] [blame] | 2139 | if (allowedFileTypes == null) { |
| 2140 | // It's not a label or label_list attribute. |
| 2141 | return; |
| 2142 | } |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 2143 | if (allowedFileTypes == FileTypeSet.ANY_FILE |
| 2144 | && !attribute.isNonEmpty() |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2145 | && !attribute.isSingleArtifact()) { |
| 2146 | return; |
| 2147 | } |
| 2148 | |
| 2149 | // If we allow any file we still need to check if there are actually files generated |
| 2150 | // Note that this check only runs for ANY_FILE predicates if the attribute is NON_EMPTY |
| 2151 | // or SINGLE_ARTIFACT |
| 2152 | // If we performed this check when allowedFileTypes == NO_FILE this would |
| 2153 | // always throw an error in those cases |
| 2154 | if (allowedFileTypes != FileTypeSet.NO_FILE) { |
Ulf Adams | cd31d3b | 2019-12-16 00:50:03 -0800 | [diff] [blame] | 2155 | NestedSet<Artifact> artifacts = |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 2156 | prerequisite.getConfiguredTarget().getProvider(FileProvider.class).getFilesToBuild(); |
Ulf Adams | cd31d3b | 2019-12-16 00:50:03 -0800 | [diff] [blame] | 2157 | if (attribute.isSingleArtifact() && !artifacts.isSingleton()) { |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 2158 | attributeError( |
| 2159 | attribute.getName(), |
Googler | f592470 | 2022-12-08 14:55:42 -0800 | [diff] [blame] | 2160 | "'" + prerequisite.getTargetLabel() + "' must produce a single file"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2161 | return; |
| 2162 | } |
Ulf Adams | cd31d3b | 2019-12-16 00:50:03 -0800 | [diff] [blame] | 2163 | for (Artifact sourceArtifact : artifacts.toList()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2164 | if (allowedFileTypes.apply(sourceArtifact.getFilename())) { |
| 2165 | return; |
| 2166 | } |
Googler | 2d90b6a | 2018-07-26 12:37:34 -0700 | [diff] [blame] | 2167 | if (sourceArtifact.isTreeArtifact()) { |
| 2168 | return; |
| 2169 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2170 | } |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 2171 | attributeError( |
| 2172 | attribute.getName(), |
| 2173 | "'" |
Googler | f592470 | 2022-12-08 14:55:42 -0800 | [diff] [blame] | 2174 | + prerequisite.getTargetLabel() |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 2175 | + "' does not produce any " |
| 2176 | + getRuleClassNameForLogging() |
| 2177 | + " " |
| 2178 | + attribute.getName() |
| 2179 | + " files (expected " |
| 2180 | + allowedFileTypes |
| 2181 | + ")"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2182 | } |
| 2183 | } |
| 2184 | |
dslomov | 7df8515 | 2017-08-01 20:47:59 +0200 | [diff] [blame] | 2185 | /** |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 2186 | * Because some rules still have to use allowedRuleClasses to do rule dependency validation. A |
| 2187 | * dependency is valid if it is from a rule in allowedRuledClasses, OR if all of the providers |
| 2188 | * in requiredProviders are provided by the target. |
dslomov | 7df8515 | 2017-08-01 20:47:59 +0200 | [diff] [blame] | 2189 | */ |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 2190 | private void validateRuleDependency(ConfiguredTargetAndData prerequisite, Attribute attribute) { |
dslomov | 7df8515 | 2017-08-01 20:47:59 +0200 | [diff] [blame] | 2191 | |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 2192 | Set<String> unfulfilledRequirements = new LinkedHashSet<>(); |
| 2193 | if (checkRuleDependencyClass(prerequisite, attribute, unfulfilledRequirements)) { |
| 2194 | return; |
| 2195 | } |
| 2196 | |
| 2197 | if (checkRuleDependencyClassWarnings(prerequisite, attribute)) { |
| 2198 | return; |
| 2199 | } |
| 2200 | |
| 2201 | if (checkRuleDependencyMandatoryProviders(prerequisite, attribute, unfulfilledRequirements)) { |
| 2202 | return; |
| 2203 | } |
| 2204 | |
| 2205 | // not allowed rule class and some mandatory providers missing => reject. |
| 2206 | if (!unfulfilledRequirements.isEmpty()) { |
| 2207 | attributeError( |
| 2208 | attribute.getName(), StringUtil.joinEnglishList(unfulfilledRequirements, "and")); |
| 2209 | } |
| 2210 | } |
| 2211 | |
| 2212 | /** Check if prerequisite should be allowed based on its rule class. */ |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 2213 | private static boolean checkRuleDependencyClass( |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 2214 | ConfiguredTargetAndData prerequisite, |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 2215 | Attribute attribute, |
| 2216 | Set<String> unfulfilledRequirements) { |
Googler | f592470 | 2022-12-08 14:55:42 -0800 | [diff] [blame] | 2217 | var predicate = attribute.getAllowedRuleClassPredicate(); |
| 2218 | if (predicate == Predicates.<String>alwaysTrue()) { |
| 2219 | // alwaysTrue is a special sentinel value. See |
| 2220 | // RuleClass.Builder.RuleClassNamePredicate.unspecified. |
| 2221 | return false; |
dslomov | c32e1b1 | 2017-07-31 19:23:52 +0200 | [diff] [blame] | 2222 | } |
Googler | f592470 | 2022-12-08 14:55:42 -0800 | [diff] [blame] | 2223 | |
| 2224 | if (predicate.apply(prerequisite.getRuleClass())) { |
| 2225 | // prerequisite has an allowed rule class => accept. |
| 2226 | return true; |
| 2227 | } |
| 2228 | // remember that the rule class that was not allowed; |
| 2229 | // but maybe prerequisite provides required providers? do not reject yet. |
| 2230 | unfulfilledRequirements.add( |
| 2231 | badPrerequisiteMessage(prerequisite, "expected " + predicate, false)); |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 2232 | return false; |
| 2233 | } |
dslomov | c32e1b1 | 2017-07-31 19:23:52 +0200 | [diff] [blame] | 2234 | |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 2235 | /** |
| 2236 | * Check if prerequisite should be allowed with warning based on its rule class. |
| 2237 | * |
| 2238 | * <p>If yes, also issues said warning. |
| 2239 | */ |
| 2240 | private boolean checkRuleDependencyClassWarnings( |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 2241 | ConfiguredTargetAndData prerequisite, Attribute attribute) { |
Googler | f592470 | 2022-12-08 14:55:42 -0800 | [diff] [blame] | 2242 | if (!attribute.getAllowedRuleClassWarningPredicate().apply(prerequisite.getRuleClass())) { |
| 2243 | return false; |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 2244 | } |
Googler | f592470 | 2022-12-08 14:55:42 -0800 | [diff] [blame] | 2245 | |
| 2246 | Predicate<String> allowedRuleClasses = attribute.getAllowedRuleClassPredicate(); |
| 2247 | reportBadPrerequisite( |
| 2248 | attribute, |
| 2249 | prerequisite, |
| 2250 | allowedRuleClasses == Predicates.<String>alwaysTrue() |
| 2251 | ? null |
| 2252 | : "expected " + allowedRuleClasses, |
| 2253 | true); |
| 2254 | // prerequisite has a rule class allowed with a warning => accept, emitting a warning. |
| 2255 | return true; |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | /** Check if prerequisite should be allowed based on required providers on the attribute. */ |
jhorvitz | 04816f5 | 2021-08-11 10:51:51 -0700 | [diff] [blame] | 2259 | private static boolean checkRuleDependencyMandatoryProviders( |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 2260 | ConfiguredTargetAndData prerequisite, |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 2261 | Attribute attribute, |
| 2262 | Set<String> unfulfilledRequirements) { |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 2263 | RequiredProviders requiredProviders = attribute.getRequiredProviders(); |
| 2264 | |
| 2265 | if (requiredProviders.acceptsAny()) { |
| 2266 | // If no required providers specified, we do not know if we should accept. |
| 2267 | return false; |
dslomov | c32e1b1 | 2017-07-31 19:23:52 +0200 | [diff] [blame] | 2268 | } |
| 2269 | |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 2270 | if (prerequisite.getConfiguredTarget().satisfies(requiredProviders)) { |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 2271 | return true; |
dslomov | c32e1b1 | 2017-07-31 19:23:52 +0200 | [diff] [blame] | 2272 | } |
| 2273 | |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 2274 | unfulfilledRequirements.add( |
| 2275 | String.format( |
| 2276 | "'%s' does not have mandatory providers: %s", |
Googler | f592470 | 2022-12-08 14:55:42 -0800 | [diff] [blame] | 2277 | prerequisite.getTargetLabel(), |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 2278 | prerequisite |
| 2279 | .getConfiguredTarget() |
| 2280 | .missingProviders(requiredProviders) |
| 2281 | .getDescription())); |
dslomov | c32e1b1 | 2017-07-31 19:23:52 +0200 | [diff] [blame] | 2282 | |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 2283 | return false; |
dslomov | c32e1b1 | 2017-07-31 19:23:52 +0200 | [diff] [blame] | 2284 | } |
| 2285 | |
janakr | f3e6f25 | 2018-01-18 07:45:12 -0800 | [diff] [blame] | 2286 | private void validateDirectPrerequisite( |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 2287 | Attribute attribute, ConfiguredTargetAndData prerequisite) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2288 | validateDirectPrerequisiteType(prerequisite, attribute); |
| 2289 | validateDirectPrerequisiteFileTypes(prerequisite, attribute); |
Greg Estren | 875c7a7 | 2015-09-24 19:57:54 +0000 | [diff] [blame] | 2290 | if (attribute.performPrereqValidatorCheck()) { |
jhorvitz | 2cdb647 | 2021-10-06 14:22:36 -0700 | [diff] [blame] | 2291 | ruleClassProvider.getPrerequisiteValidator().validate(this, prerequisite, attribute); |
Ulf Adams | 0b63897 | 2015-09-08 13:25:35 +0000 | [diff] [blame] | 2292 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2293 | } |
| 2294 | } |
| 2295 | |
ruperts | 8986b6e | 2017-10-17 00:26:29 +0200 | [diff] [blame] | 2296 | /** Helper class for reporting errors and warnings. */ |
cparsons | 963881a | 2018-10-03 14:23:55 -0700 | [diff] [blame] | 2297 | private static final class ErrorReporter extends EventHandlingErrorReporter |
ruperts | 8986b6e | 2017-10-17 00:26:29 +0200 | [diff] [blame] | 2298 | implements RuleErrorConsumer { |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 2299 | private final Rule rule; |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 2300 | private final BuildConfigurationValue configuration; |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 2301 | |
ulfjack | 0791764 | 2019-09-12 02:09:36 -0700 | [diff] [blame] | 2302 | ErrorReporter( |
| 2303 | AnalysisEnvironment env, |
| 2304 | Rule rule, |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 2305 | BuildConfigurationValue configuration, |
ulfjack | 0791764 | 2019-09-12 02:09:36 -0700 | [diff] [blame] | 2306 | String ruleClassNameForLogging) { |
asteinb | b82db78 | 2018-05-18 08:21:39 -0700 | [diff] [blame] | 2307 | super(ruleClassNameForLogging, env); |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 2308 | this.rule = rule; |
ulfjack | 0791764 | 2019-09-12 02:09:36 -0700 | [diff] [blame] | 2309 | this.configuration = configuration; |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
| 2312 | @Override |
Googler | 706d75b | 2019-09-18 10:09:07 -0700 | [diff] [blame] | 2313 | protected String getMacroMessageAppendix(String unusedAttrName) { |
| 2314 | // TODO(b/141234726): Historically this reported the location |
| 2315 | // of the rule attribute in the macro call (assuming no **kwargs), |
| 2316 | // but we no longer locations for individual attributes. |
| 2317 | // We should record the instantiation call stack in each rule |
| 2318 | // and report the position of its topmost frame here. |
asteinb | b82db78 | 2018-05-18 08:21:39 -0700 | [diff] [blame] | 2319 | return rule.wasCreatedByMacro() |
| 2320 | ? String.format( |
| 2321 | ". Since this rule was created by the macro '%s', the error might have been " |
Googler | 706d75b | 2019-09-18 10:09:07 -0700 | [diff] [blame] | 2322 | + "caused by the macro implementation", |
| 2323 | getGeneratorFunction()) |
asteinb | b82db78 | 2018-05-18 08:21:39 -0700 | [diff] [blame] | 2324 | : ""; |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 2327 | private String getGeneratorFunction() { |
Googler | a40c64a | 2020-08-11 16:39:39 -0700 | [diff] [blame] | 2328 | return (String) rule.getAttr("generator_function"); |
Florian Weikert | b8a6a94 | 2015-09-25 12:36:08 +0000 | [diff] [blame] | 2329 | } |
asteinb | b82db78 | 2018-05-18 08:21:39 -0700 | [diff] [blame] | 2330 | |
| 2331 | @Override |
| 2332 | protected Label getLabel() { |
| 2333 | return rule.getLabel(); |
| 2334 | } |
| 2335 | |
| 2336 | @Override |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 2337 | protected BuildConfigurationValue getConfiguration() { |
ulfjack | 0791764 | 2019-09-12 02:09:36 -0700 | [diff] [blame] | 2338 | return configuration; |
| 2339 | } |
| 2340 | |
| 2341 | @Override |
asteinb | b82db78 | 2018-05-18 08:21:39 -0700 | [diff] [blame] | 2342 | protected Location getRuleLocation() { |
| 2343 | return rule.getLocation(); |
| 2344 | } |
cparsons | e8d450c | 2018-10-04 16:01:53 -0700 | [diff] [blame] | 2345 | } |
asteinb | b82db78 | 2018-05-18 08:21:39 -0700 | [diff] [blame] | 2346 | |
cparsons | e8d450c | 2018-10-04 16:01:53 -0700 | [diff] [blame] | 2347 | /** |
| 2348 | * Implementation of an error consumer which does not post any events, saves rule and attribute |
| 2349 | * errors for future consumption, and drops warnings. |
| 2350 | */ |
| 2351 | public static final class SuppressingErrorReporter implements RuleErrorConsumer { |
| 2352 | private final List<String> errorMessages = Lists.newArrayList(); |
| 2353 | |
| 2354 | @Override |
| 2355 | public void ruleWarning(String message) {} |
| 2356 | |
| 2357 | @Override |
| 2358 | public void ruleError(String message) { |
| 2359 | errorMessages.add(message); |
| 2360 | } |
| 2361 | |
| 2362 | @Override |
| 2363 | public void attributeWarning(String attrName, String message) {} |
| 2364 | |
| 2365 | @Override |
| 2366 | public void attributeError(String attrName, String message) { |
| 2367 | errorMessages.add(message); |
| 2368 | } |
| 2369 | |
| 2370 | @Override |
| 2371 | public boolean hasErrors() { |
| 2372 | return !errorMessages.isEmpty(); |
| 2373 | } |
| 2374 | |
jhorvitz | b659082 | 2021-08-13 15:18:27 -0700 | [diff] [blame] | 2375 | /** Returns the error message strings reported to this error consumer. */ |
cparsons | e8d450c | 2018-10-04 16:01:53 -0700 | [diff] [blame] | 2376 | public List<String> getErrorMessages() { |
| 2377 | return errorMessages; |
| 2378 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2379 | } |
| 2380 | } |