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