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