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