Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [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 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 16 | import com.google.common.base.Preconditions; |
Eric Fellheimer | e27d063 | 2015-09-25 21:35:26 +0000 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableList; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.lib.cmdline.Label; |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
| 20 | import com.google.devtools.build.lib.events.EventHandler; |
Eric Fellheimer | b5c9884 | 2015-08-12 23:24:21 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.packages.AspectDefinition; |
Eric Fellheimer | b5c9884 | 2015-08-12 23:24:21 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.packages.Attribute; |
Dmitry Lomov | 940ea07 | 2016-01-21 22:34:14 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.packages.DependencyFilter; |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.packages.NoSuchPackageException; |
| 25 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
Eric Fellheimer | e27d063 | 2015-09-25 21:35:26 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.packages.NoSuchThingException; |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.packages.Rule; |
Mark Schaller | 24ed98d | 2015-10-21 17:47:51 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.skyframe.TransitiveTraversalFunction.FirstErrorMessageAccumulator; |
shreyax | 246f0aa | 2018-02-23 07:46:54 -0800 | [diff] [blame] | 29 | import com.google.devtools.build.lib.util.GroupedList; |
| 30 | import com.google.devtools.build.skyframe.SkyFunction; |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.skyframe.SkyKey; |
| 32 | import com.google.devtools.build.skyframe.SkyValue; |
| 33 | import com.google.devtools.build.skyframe.ValueOrException2; |
Eric Fellheimer | e27d063 | 2015-09-25 21:35:26 +0000 | [diff] [blame] | 34 | import java.util.Collection; |
shreyax | 246f0aa | 2018-02-23 07:46:54 -0800 | [diff] [blame] | 35 | import java.util.List; |
| 36 | import java.util.Map; |
Mark Schaller | 24ed98d | 2015-10-21 17:47:51 +0000 | [diff] [blame] | 37 | import javax.annotation.Nullable; |
| 38 | |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 39 | /** |
| 40 | * This class is like {@link TransitiveTargetFunction}, but the values it returns do not contain |
Mark Schaller | 24ed98d | 2015-10-21 17:47:51 +0000 | [diff] [blame] | 41 | * {@link NestedSet}s. It performs the side-effects of {@link TransitiveTargetFunction} (i.e., |
| 42 | * ensuring that transitive targets and their packages have been loaded). It evaluates to a |
| 43 | * {@link TransitiveTraversalValue} that contains the first error message it encountered, and a |
| 44 | * set of names of providers if the target is a rule. |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 45 | */ |
Mark Schaller | 24ed98d | 2015-10-21 17:47:51 +0000 | [diff] [blame] | 46 | public class TransitiveTraversalFunction |
| 47 | extends TransitiveBaseTraversalFunction<FirstErrorMessageAccumulator> { |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 48 | |
| 49 | @Override |
ulfjack | dc73a1d | 2017-10-30 07:05:53 -0400 | [diff] [blame] | 50 | Label argumentFromKey(SkyKey key) { |
| 51 | return (Label) key.argument(); |
| 52 | } |
| 53 | |
| 54 | @Override |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 55 | SkyKey getKey(Label label) { |
| 56 | return TransitiveTraversalValue.key(label); |
| 57 | } |
| 58 | |
| 59 | @Override |
Mark Schaller | 24ed98d | 2015-10-21 17:47:51 +0000 | [diff] [blame] | 60 | FirstErrorMessageAccumulator processTarget(Label label, TargetAndErrorIfAny targetAndErrorIfAny) { |
| 61 | NoSuchTargetException errorIfAny = targetAndErrorIfAny.getErrorLoadingTarget(); |
| 62 | String errorMessageIfAny = errorIfAny == null ? null : errorIfAny.getMessage(); |
| 63 | return new FirstErrorMessageAccumulator(errorMessageIfAny); |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
Mark Schaller | 24ed98d | 2015-10-21 17:47:51 +0000 | [diff] [blame] | 67 | void processDeps( |
| 68 | FirstErrorMessageAccumulator accumulator, |
| 69 | EventHandler eventHandler, |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 70 | TargetAndErrorIfAny targetAndErrorIfAny, |
jcater | cecb3a8 | 2018-05-01 14:37:48 -0700 | [diff] [blame^] | 71 | Iterable<Map.Entry<SkyKey, ValueOrException2<NoSuchPackageException, NoSuchTargetException>>> |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 72 | depEntries) { |
jcater | cecb3a8 | 2018-05-01 14:37:48 -0700 | [diff] [blame^] | 73 | for (Map.Entry<SkyKey, ValueOrException2<NoSuchPackageException, NoSuchTargetException>> entry : |
Mark Schaller | 24ed98d | 2015-10-21 17:47:51 +0000 | [diff] [blame] | 74 | depEntries) { |
| 75 | TransitiveTraversalValue transitiveTraversalValue; |
| 76 | try { |
| 77 | transitiveTraversalValue = (TransitiveTraversalValue) entry.getValue().get(); |
| 78 | if (transitiveTraversalValue == null) { |
| 79 | continue; |
| 80 | } |
| 81 | } catch (NoSuchPackageException | NoSuchTargetException e) { |
| 82 | accumulator.maybeSet(e.getMessage()); |
| 83 | continue; |
| 84 | } |
| 85 | String firstErrorMessage = transitiveTraversalValue.getFirstErrorMessage(); |
| 86 | if (firstErrorMessage != null) { |
| 87 | accumulator.maybeSet(firstErrorMessage); |
| 88 | } |
| 89 | } |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Googler | 7184b6f | 2017-05-16 05:25:49 +0200 | [diff] [blame] | 92 | @Override |
| 93 | protected Collection<Label> getAspectLabels( |
| 94 | Rule fromRule, |
| 95 | Attribute attr, |
| 96 | Label toLabel, |
| 97 | ValueOrException2<NoSuchPackageException, NoSuchTargetException> toVal, |
| 98 | Environment env) { |
Eric Fellheimer | e27d063 | 2015-09-25 21:35:26 +0000 | [diff] [blame] | 99 | try { |
| 100 | if (toVal == null) { |
| 101 | return ImmutableList.of(); |
| 102 | } |
| 103 | TransitiveTraversalValue traversalVal = (TransitiveTraversalValue) toVal.get(); |
| 104 | if (traversalVal == null || traversalVal.getProviders() == null) { |
| 105 | return ImmutableList.of(); |
| 106 | } |
| 107 | // Retrieve the providers of the dep from the TransitiveTraversalValue, so we can avoid |
| 108 | // issuing a dep on its defining Package. |
Lukacs Berki | 549bfce | 2016-04-22 15:29:12 +0000 | [diff] [blame] | 109 | return AspectDefinition.visitAspectsIfRequired(fromRule, attr, |
Dmitry Lomov | b91d392 | 2017-01-09 20:12:57 +0000 | [diff] [blame] | 110 | traversalVal.getProviders(), |
Dmitry Lomov | 940ea07 | 2016-01-21 22:34:14 +0000 | [diff] [blame] | 111 | DependencyFilter.ALL_DEPS).values(); |
Eric Fellheimer | e27d063 | 2015-09-25 21:35:26 +0000 | [diff] [blame] | 112 | } catch (NoSuchThingException e) { |
| 113 | // Do nothing. This error was handled when we computed the corresponding |
| 114 | // TransitiveTargetValue. |
| 115 | return ImmutableList.of(); |
| 116 | } |
| 117 | } |
| 118 | |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 119 | @Override |
Lukacs Berki | 549bfce | 2016-04-22 15:29:12 +0000 | [diff] [blame] | 120 | SkyValue computeSkyValue(TargetAndErrorIfAny targetAndErrorIfAny, |
| 121 | FirstErrorMessageAccumulator accumulator) { |
Mark Schaller | 24ed98d | 2015-10-21 17:47:51 +0000 | [diff] [blame] | 122 | boolean targetLoadedSuccessfully = targetAndErrorIfAny.getErrorLoadingTarget() == null; |
| 123 | String firstErrorMessage = accumulator.getFirstErrorMessage(); |
| 124 | return targetLoadedSuccessfully |
| 125 | ? TransitiveTraversalValue.forTarget(targetAndErrorIfAny.getTarget(), firstErrorMessage) |
Googler | 7184b6f | 2017-05-16 05:25:49 +0200 | [diff] [blame] | 126 | : TransitiveTraversalValue.unsuccessfulTransitiveTraversal( |
| 127 | firstErrorMessage, targetAndErrorIfAny.getTarget()); |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Mark Schaller | 6b6d8a9 | 2015-10-23 01:00:28 +0000 | [diff] [blame] | 130 | @Override |
| 131 | TargetMarkerValue getTargetMarkerValue(SkyKey targetMarkerKey, Environment env) |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 132 | throws NoSuchTargetException, NoSuchPackageException, InterruptedException { |
Mark Schaller | 6b6d8a9 | 2015-10-23 01:00:28 +0000 | [diff] [blame] | 133 | return TargetMarkerFunction.computeTargetMarkerValue(targetMarkerKey, env); |
| 134 | } |
| 135 | |
shreyax | 246f0aa | 2018-02-23 07:46:54 -0800 | [diff] [blame] | 136 | @Override |
| 137 | Collection<SkyKey> getLabelDepKeys( |
| 138 | SkyFunction.Environment env, TargetAndErrorIfAny targetAndErrorIfAny) |
| 139 | throws InterruptedException { |
| 140 | // As a performance optimization we may already know the deps we are about to request from |
| 141 | // last time #compute was called. By requesting these from the environment, we can avoid |
| 142 | // repeating the label visitation step. For TransitiveTraversalFunction#compute, the label deps |
| 143 | // dependency group is requested immediately after the package. |
| 144 | // |
| 145 | // IMPORTANT: No other package values should be requested inside |
| 146 | // TransitiveTraversalFunction#compute from this point forward. |
| 147 | Collection<SkyKey> oldDepKeys = getDepsAfterLastPackageDep(env, /*offset=*/ 1); |
| 148 | return oldDepKeys == null ? super.getLabelDepKeys(env, targetAndErrorIfAny) : oldDepKeys; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | Iterable<SkyKey> getStrictLabelAspectDepKeys( |
| 153 | SkyFunction.Environment env, |
| 154 | Map<SkyKey, ValueOrException2<NoSuchPackageException, NoSuchTargetException>> depMap, |
| 155 | TargetAndErrorIfAny targetAndErrorIfAny) |
| 156 | throws InterruptedException { |
| 157 | // As a performance optimization we may already know the deps we are about to request from |
| 158 | // last time #compute was called. By requesting these from the environment, we can avoid |
| 159 | // repeating the label visitation step. For TransitiveTraversalFunction#compute, the label |
| 160 | // aspect deps dependency group is requested two groups after the package. |
| 161 | Collection<SkyKey> oldAspectDepKeys = getDepsAfterLastPackageDep(env, /*offset=*/ 2); |
| 162 | return oldAspectDepKeys == null |
| 163 | ? super.getStrictLabelAspectDepKeys(env, depMap, targetAndErrorIfAny) |
| 164 | : oldAspectDepKeys; |
| 165 | } |
| 166 | |
| 167 | @Nullable |
| 168 | private static Collection<SkyKey> getDepsAfterLastPackageDep( |
| 169 | SkyFunction.Environment env, int offset) { |
| 170 | GroupedList<SkyKey> temporaryDirectDeps = env.getTemporaryDirectDeps(); |
| 171 | if (temporaryDirectDeps == null) { |
| 172 | return null; |
| 173 | } |
| 174 | int lastPackageDepIndex = getLastPackageValueIndex(temporaryDirectDeps); |
| 175 | if (lastPackageDepIndex == -1 |
| 176 | || temporaryDirectDeps.listSize() <= lastPackageDepIndex + offset) { |
| 177 | return null; |
| 178 | } |
| 179 | return temporaryDirectDeps.get(lastPackageDepIndex + offset); |
| 180 | } |
| 181 | |
| 182 | private static int getLastPackageValueIndex(GroupedList<SkyKey> directDeps) { |
| 183 | int directDepsNumGroups = directDeps.listSize(); |
| 184 | for (int i = directDepsNumGroups - 1; i >= 0; i--) { |
| 185 | List<SkyKey> depGroup = directDeps.get(i); |
| 186 | if (depGroup.size() == 1 && depGroup.get(0).functionName().equals(SkyFunctions.PACKAGE)) { |
| 187 | return i; |
| 188 | } |
| 189 | } |
| 190 | return -1; |
| 191 | } |
| 192 | |
Mark Schaller | 24ed98d | 2015-10-21 17:47:51 +0000 | [diff] [blame] | 193 | /** |
| 194 | * Keeps track of the first error message encountered while traversing itself and its |
| 195 | * dependencies. |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 196 | */ |
Mark Schaller | 24ed98d | 2015-10-21 17:47:51 +0000 | [diff] [blame] | 197 | static class FirstErrorMessageAccumulator { |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 198 | |
Mark Schaller | 24ed98d | 2015-10-21 17:47:51 +0000 | [diff] [blame] | 199 | @Nullable private String firstErrorMessage; |
| 200 | |
| 201 | public FirstErrorMessageAccumulator(@Nullable String firstErrorMessage) { |
| 202 | this.firstErrorMessage = firstErrorMessage; |
| 203 | } |
| 204 | |
| 205 | /** Remembers {@param errorMessage} if it is the first error message. */ |
| 206 | void maybeSet(String errorMessage) { |
| 207 | Preconditions.checkNotNull(errorMessage); |
| 208 | if (firstErrorMessage == null) { |
| 209 | firstErrorMessage = errorMessage; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | @Nullable |
| 214 | String getFirstErrorMessage() { |
| 215 | return firstErrorMessage; |
| 216 | } |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 217 | } |
| 218 | } |