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