Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +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.query2; |
| 15 | |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 16 | import com.google.common.base.Predicate; |
| 17 | import com.google.common.collect.ImmutableList; |
Miguel Alcon Pinto | b651026 | 2015-12-10 17:50:15 +0000 | [diff] [blame] | 18 | import com.google.common.collect.Iterables; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.cmdline.Label; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.cmdline.TargetParsingException; |
| 22 | import com.google.devtools.build.lib.events.ErrorSensingEventHandler; |
| 23 | import com.google.devtools.build.lib.events.Event; |
| 24 | import com.google.devtools.build.lib.events.EventHandler; |
| 25 | import com.google.devtools.build.lib.packages.Attribute; |
| 26 | import com.google.devtools.build.lib.packages.Rule; |
| 27 | import com.google.devtools.build.lib.packages.Target; |
Janak Ramakrishnan | fe1056f | 2015-02-11 21:16:06 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.pkgcache.PackageProvider; |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.pkgcache.TargetPatternEvaluator; |
Janak Ramakrishnan | fe1056f | 2015-02-11 21:16:06 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.pkgcache.TransitivePackageLoader; |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 32 | import com.google.devtools.build.lib.profiler.AutoProfiler; |
Miguel Alcon Pinto | b651026 | 2015-12-10 17:50:15 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.query2.engine.Callback; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.query2.engine.QueryEnvironment; |
| 35 | import com.google.devtools.build.lib.query2.engine.QueryEvalResult; |
| 36 | import com.google.devtools.build.lib.query2.engine.QueryException; |
| 37 | import com.google.devtools.build.lib.query2.engine.QueryExpression; |
Janak Ramakrishnan | 0dbc1f9 | 2016-01-07 19:12:06 +0000 | [diff] [blame] | 38 | import com.google.devtools.build.lib.query2.engine.QueryUtil.AggregateAllCallback; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.lib.util.BinaryPredicate; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 40 | import com.google.devtools.build.lib.util.Preconditions; |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 41 | import com.google.devtools.build.skyframe.WalkableGraph.WalkableGraphFactory; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 42 | |
| 43 | import java.util.Collection; |
| 44 | import java.util.HashMap; |
| 45 | import java.util.LinkedHashSet; |
| 46 | import java.util.List; |
| 47 | import java.util.Map; |
| 48 | import java.util.Set; |
Miguel Alcon Pinto | b651026 | 2015-12-10 17:50:15 +0000 | [diff] [blame] | 49 | import java.util.concurrent.atomic.AtomicBoolean; |
Eric Fellheimer | a39f8a9 | 2015-07-28 19:11:23 +0000 | [diff] [blame] | 50 | import java.util.logging.Logger; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 51 | |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 52 | import javax.annotation.Nullable; |
| 53 | |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 54 | /** |
| 55 | * {@link QueryEnvironment} that can evaluate queries to produce a result, and implements as much |
| 56 | * of QueryEnvironment as possible while remaining mostly agnostic as to the objects being stored. |
| 57 | */ |
| 58 | public abstract class AbstractBlazeQueryEnvironment<T> implements QueryEnvironment<T> { |
| 59 | protected final ErrorSensingEventHandler eventHandler; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 60 | private final Map<String, Set<T>> letBindings = new HashMap<>(); |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 61 | protected final boolean keepGoing; |
| 62 | protected final boolean strictScope; |
| 63 | |
| 64 | protected final BinaryPredicate<Rule, Attribute> dependencyFilter; |
| 65 | private final Predicate<Label> labelFilter; |
| 66 | |
| 67 | private final Set<Setting> settings; |
| 68 | private final List<QueryFunction> extraFunctions; |
| 69 | |
Eric Fellheimer | a39f8a9 | 2015-07-28 19:11:23 +0000 | [diff] [blame] | 70 | private static final Logger LOG = Logger.getLogger(AbstractBlazeQueryEnvironment.class.getName()); |
| 71 | |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 72 | protected AbstractBlazeQueryEnvironment(boolean keepGoing, |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 73 | boolean strictScope, |
| 74 | Predicate<Label> labelFilter, |
| 75 | EventHandler eventHandler, |
| 76 | Set<Setting> settings, |
| 77 | Iterable<QueryFunction> extraFunctions) { |
| 78 | this.eventHandler = new ErrorSensingEventHandler(eventHandler); |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 79 | this.keepGoing = keepGoing; |
| 80 | this.strictScope = strictScope; |
| 81 | this.dependencyFilter = constructDependencyFilter(settings); |
| 82 | this.labelFilter = labelFilter; |
| 83 | this.settings = Sets.immutableEnumSet(settings); |
| 84 | this.extraFunctions = ImmutableList.copyOf(extraFunctions); |
| 85 | } |
| 86 | |
| 87 | private static BinaryPredicate<Rule, Attribute> constructDependencyFilter(Set<Setting> settings) { |
| 88 | BinaryPredicate<Rule, Attribute> specifiedFilter = |
| 89 | settings.contains(Setting.NO_HOST_DEPS) ? Rule.NO_HOST_DEPS : Rule.ALL_DEPS; |
| 90 | if (settings.contains(Setting.NO_IMPLICIT_DEPS)) { |
| 91 | specifiedFilter = Rule.and(specifiedFilter, Rule.NO_IMPLICIT_DEPS); |
| 92 | } |
| 93 | if (settings.contains(Setting.NO_NODEP_DEPS)) { |
| 94 | specifiedFilter = Rule.and(specifiedFilter, Rule.NO_NODEP_ATTRIBUTES); |
| 95 | } |
| 96 | return specifiedFilter; |
| 97 | } |
| 98 | |
Janak Ramakrishnan | fe1056f | 2015-02-11 21:16:06 +0000 | [diff] [blame] | 99 | public static AbstractBlazeQueryEnvironment<Target> newQueryEnvironment( |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 100 | TransitivePackageLoader transitivePackageLoader, WalkableGraphFactory graphFactory, |
| 101 | PackageProvider packageProvider, |
Janak Ramakrishnan | fe1056f | 2015-02-11 21:16:06 +0000 | [diff] [blame] | 102 | TargetPatternEvaluator targetPatternEvaluator, boolean keepGoing, boolean orderedResults, |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 103 | List<String> universeScope, int loadingPhaseThreads, |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 104 | EventHandler eventHandler, Set<Setting> settings, Iterable<QueryFunction> functions, |
| 105 | @Nullable PathPackageLocator packagePath) { |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 106 | return newQueryEnvironment(transitivePackageLoader, graphFactory, packageProvider, |
Janak Ramakrishnan | fe1056f | 2015-02-11 21:16:06 +0000 | [diff] [blame] | 107 | targetPatternEvaluator, keepGoing, /*strictScope=*/true, orderedResults, |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 108 | universeScope, loadingPhaseThreads, Rule.ALL_LABELS, eventHandler, settings, functions, |
| 109 | packagePath); |
Janak Ramakrishnan | fe1056f | 2015-02-11 21:16:06 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | public static AbstractBlazeQueryEnvironment<Target> newQueryEnvironment( |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 113 | TransitivePackageLoader transitivePackageLoader, WalkableGraphFactory graphFactory, |
| 114 | PackageProvider packageProvider, |
Janak Ramakrishnan | fe1056f | 2015-02-11 21:16:06 +0000 | [diff] [blame] | 115 | TargetPatternEvaluator targetPatternEvaluator, boolean keepGoing, boolean strictScope, |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 116 | boolean orderedResults, List<String> universeScope, int loadingPhaseThreads, |
| 117 | Predicate<Label> labelFilter, |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 118 | EventHandler eventHandler, Set<Setting> settings, Iterable<QueryFunction> functions, |
| 119 | @Nullable PathPackageLocator packagePath) { |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 120 | Preconditions.checkNotNull(universeScope); |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 121 | return orderedResults || universeScope.isEmpty() || packagePath == null |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 122 | ? new BlazeQueryEnvironment(transitivePackageLoader, packageProvider, |
Janak Ramakrishnan | fe1056f | 2015-02-11 21:16:06 +0000 | [diff] [blame] | 123 | targetPatternEvaluator, keepGoing, strictScope, loadingPhaseThreads, |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 124 | labelFilter, eventHandler, settings, functions) |
| 125 | : new SkyQueryEnvironment( |
| 126 | keepGoing, strictScope, loadingPhaseThreads, labelFilter, eventHandler, settings, |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 127 | functions, targetPatternEvaluator.getOffset(), graphFactory, universeScope, |
| 128 | packagePath); |
Janak Ramakrishnan | fe1056f | 2015-02-11 21:16:06 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 131 | /** |
| 132 | * Evaluate the specified query expression in this environment. |
| 133 | * |
| 134 | * @return a {@link QueryEvalResult} object that contains the resulting set of targets and a bit |
Mark Schaller | 895b3d2 | 2015-03-23 14:27:26 +0000 | [diff] [blame] | 135 | * to indicate whether errors occurred during evaluation; note that the |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 136 | * success status can only be false if {@code --keep_going} was in effect |
| 137 | * @throws QueryException if the evaluation failed and {@code --nokeep_going} was in |
| 138 | * effect |
| 139 | */ |
Miguel Alcon Pinto | b651026 | 2015-12-10 17:50:15 +0000 | [diff] [blame] | 140 | public QueryEvalResult evaluateQuery(QueryExpression expr, final Callback<T> callback) |
Janak Ramakrishnan | 8990739 | 2015-07-08 21:43:31 +0000 | [diff] [blame] | 141 | throws QueryException, InterruptedException { |
Miguel Alcon Pinto | b651026 | 2015-12-10 17:50:15 +0000 | [diff] [blame] | 142 | |
| 143 | final AtomicBoolean empty = new AtomicBoolean(true); |
| 144 | try (final AutoProfiler p = AutoProfiler.logged("evaluating query", LOG)) { |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 145 | |
| 146 | // In the --nokeep_going case, errors are reported in the order in which the patterns are |
| 147 | // specified; using a linked hash set here makes sure that the left-most error is reported. |
| 148 | Set<String> targetPatternSet = new LinkedHashSet<>(); |
| 149 | expr.collectTargetPatterns(targetPatternSet); |
| 150 | try { |
Janak Ramakrishnan | ee6208b | 2016-01-07 20:17:41 +0000 | [diff] [blame^] | 151 | preloadOrThrow(expr, targetPatternSet); |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 152 | } catch (TargetParsingException e) { |
| 153 | // Unfortunately, by evaluating the patterns in parallel, we lose some location information. |
| 154 | throw new QueryException(expr, e.getMessage()); |
| 155 | } |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 156 | try { |
Miguel Alcon Pinto | b651026 | 2015-12-10 17:50:15 +0000 | [diff] [blame] | 157 | this.eval(expr, new Callback<T>() { |
| 158 | @Override |
| 159 | public void process(Iterable<T> partialResult) |
| 160 | throws QueryException, InterruptedException { |
| 161 | empty.compareAndSet(true, Iterables.isEmpty(partialResult)); |
| 162 | callback.process(partialResult); |
| 163 | } |
| 164 | }); |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 165 | } catch (QueryException e) { |
| 166 | throw new QueryException(e, expr); |
Eric Fellheimer | a39f8a9 | 2015-07-28 19:11:23 +0000 | [diff] [blame] | 167 | } |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | if (eventHandler.hasErrors()) { |
| 171 | if (!keepGoing) { |
| 172 | // This case represents loading-phase errors reported during evaluation |
| 173 | // of target patterns that don't cause evaluation to fail per se. |
| 174 | throw new QueryException("Evaluation of query \"" + expr |
| 175 | + "\" failed due to BUILD file errors"); |
| 176 | } else { |
| 177 | eventHandler.handle(Event.warn("--keep_going specified, ignoring errors. " |
| 178 | + "Results may be inaccurate")); |
| 179 | } |
| 180 | } |
| 181 | |
Miguel Alcon Pinto | b651026 | 2015-12-10 17:50:15 +0000 | [diff] [blame] | 182 | return new QueryEvalResult(!eventHandler.hasErrors(), empty.get()); |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Miguel Alcon Pinto | b651026 | 2015-12-10 17:50:15 +0000 | [diff] [blame] | 185 | public QueryEvalResult evaluateQuery(String query, Callback<T> callback) |
Janak Ramakrishnan | 8990739 | 2015-07-08 21:43:31 +0000 | [diff] [blame] | 186 | throws QueryException, InterruptedException { |
Miguel Alcon Pinto | b651026 | 2015-12-10 17:50:15 +0000 | [diff] [blame] | 187 | return evaluateQuery(QueryExpression.parse(query, this), callback); |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void reportBuildFileError(QueryExpression caller, String message) throws QueryException { |
| 192 | if (!keepGoing) { |
| 193 | throw new QueryException(caller, message); |
| 194 | } else { |
| 195 | // Keep consistent with evaluateQuery() above. |
| 196 | eventHandler.handle(Event.error("Evaluation of query \"" + caller + "\" failed: " + message)); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | public abstract Target getTarget(Label label) throws TargetNotFoundException, QueryException; |
| 201 | |
| 202 | @Override |
| 203 | public Set<T> getVariable(String name) { |
| 204 | return letBindings.get(name); |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public Set<T> setVariable(String name, Set<T> value) { |
| 209 | return letBindings.put(name, value); |
| 210 | } |
| 211 | |
| 212 | protected boolean validateScope(Label label, boolean strict) throws QueryException { |
| 213 | if (!labelFilter.apply(label)) { |
| 214 | String error = String.format("target '%s' is not within the scope of the query", label); |
| 215 | if (strict) { |
| 216 | throw new QueryException(error); |
| 217 | } else { |
| 218 | eventHandler.handle(Event.warn(error + ". Skipping")); |
| 219 | return false; |
| 220 | } |
| 221 | } |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | public Set<T> evalTargetPattern(QueryExpression caller, String pattern) |
| 226 | throws QueryException { |
Janak Ramakrishnan | ee6208b | 2016-01-07 20:17:41 +0000 | [diff] [blame^] | 227 | try { |
| 228 | preloadOrThrow(caller, ImmutableList.of(pattern)); |
| 229 | } catch (TargetParsingException e) { |
| 230 | // Will skip the target and keep going if -k is specified. |
| 231 | reportBuildFileError(caller, e.getMessage()); |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 232 | } |
Janak Ramakrishnan | 0dbc1f9 | 2016-01-07 19:12:06 +0000 | [diff] [blame] | 233 | AggregateAllCallback<T> aggregatingCallback = new AggregateAllCallback<>(); |
| 234 | getTargetsMatchingPattern(caller, pattern, aggregatingCallback); |
| 235 | return aggregatingCallback.getResult(); |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Janak Ramakrishnan | ee6208b | 2016-01-07 20:17:41 +0000 | [diff] [blame^] | 238 | /** |
| 239 | * Perform any work that should be done ahead of time to resolve the target patterns in the |
| 240 | * query. Implementations may choose to cache the results of resolving the patterns, cache |
| 241 | * intermediate work, or not cache and resolve patterns on the fly. |
| 242 | */ |
| 243 | protected abstract void preloadOrThrow(QueryExpression caller, Collection<String> patterns) |
Janak Ramakrishnan | cbe2634 | 2015-08-17 18:57:57 +0000 | [diff] [blame] | 244 | throws QueryException, TargetParsingException; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 245 | |
| 246 | @Override |
| 247 | public boolean isSettingEnabled(Setting setting) { |
| 248 | return settings.contains(Preconditions.checkNotNull(setting)); |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public Iterable<QueryFunction> getFunctions() { |
| 253 | ImmutableList.Builder<QueryFunction> builder = ImmutableList.builder(); |
| 254 | builder.addAll(DEFAULT_QUERY_FUNCTIONS); |
| 255 | builder.addAll(extraFunctions); |
| 256 | return builder.build(); |
| 257 | } |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 258 | } |