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 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 16 | import com.google.common.collect.ImmutableList; |
| 17 | import com.google.common.collect.ImmutableSet; |
| 18 | import com.google.common.collect.Iterables; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.cmdline.Label; |
Lukacs Berki | a643436 | 2015-09-15 13:56:14 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.cmdline.LabelSyntaxException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import com.google.devtools.build.lib.cmdline.ResolvedTargets; |
| 22 | import com.google.devtools.build.lib.cmdline.ResolvedTargets.Builder; |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.cmdline.TargetParsingException; |
| 24 | import com.google.devtools.build.lib.cmdline.TargetPattern; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
| 26 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; |
Mark Schaller | 20c2d03 | 2015-06-18 16:51:03 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.pkgcache.FilteringPolicies; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | import com.google.devtools.build.lib.pkgcache.FilteringPolicy; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame^] | 29 | import com.google.devtools.build.lib.util.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 30 | import com.google.devtools.build.skyframe.SkyKey; |
| 31 | import com.google.devtools.build.skyframe.SkyValue; |
| 32 | |
| 33 | import java.io.IOException; |
| 34 | import java.io.ObjectInputStream; |
| 35 | import java.io.ObjectOutputStream; |
| 36 | import java.io.Serializable; |
| 37 | import java.util.ArrayList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 38 | import java.util.List; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 39 | import java.util.Objects; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * A value referring to a computed set of resolved targets. This is used for the results of target |
| 43 | * pattern parsing. |
| 44 | */ |
| 45 | @Immutable |
| 46 | @ThreadSafe |
| 47 | public final class TargetPatternValue implements SkyValue { |
| 48 | |
Nathan Harmata | 3fae366 | 2015-04-22 20:10:48 +0000 | [diff] [blame] | 49 | private ResolvedTargets<Label> targets; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 50 | |
Nathan Harmata | 3fae366 | 2015-04-22 20:10:48 +0000 | [diff] [blame] | 51 | TargetPatternValue(ResolvedTargets<Label> targets) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 52 | this.targets = Preconditions.checkNotNull(targets); |
| 53 | } |
| 54 | |
| 55 | private void writeObject(ObjectOutputStream out) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 56 | List<String> ts = new ArrayList<>(); |
| 57 | List<String> filteredTs = new ArrayList<>(); |
Nathan Harmata | 3fae366 | 2015-04-22 20:10:48 +0000 | [diff] [blame] | 58 | for (Label target : targets.getTargets()) { |
| 59 | ts.add(target.toString()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 60 | } |
Nathan Harmata | 3fae366 | 2015-04-22 20:10:48 +0000 | [diff] [blame] | 61 | for (Label target : targets.getFilteredTargets()) { |
| 62 | filteredTs.add(target.toString()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 65 | out.writeObject(ts); |
| 66 | out.writeObject(filteredTs); |
| 67 | } |
| 68 | |
Nathan Harmata | 3fae366 | 2015-04-22 20:10:48 +0000 | [diff] [blame] | 69 | private Label labelFromString(String labelString) { |
| 70 | try { |
| 71 | return Label.parseAbsolute(labelString); |
Lukacs Berki | a643436 | 2015-09-15 13:56:14 +0000 | [diff] [blame] | 72 | } catch (LabelSyntaxException e) { |
Nathan Harmata | 3fae366 | 2015-04-22 20:10:48 +0000 | [diff] [blame] | 73 | throw new IllegalStateException(e); |
| 74 | } |
| 75 | } |
| 76 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 77 | @SuppressWarnings("unchecked") |
| 78 | private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 79 | List<String> ts = (List<String>) in.readObject(); |
| 80 | List<String> filteredTs = (List<String>) in.readObject(); |
| 81 | |
Nathan Harmata | 3fae366 | 2015-04-22 20:10:48 +0000 | [diff] [blame] | 82 | Builder<Label> builder = ResolvedTargets.<Label>builder(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 83 | for (String labelString : ts) { |
Nathan Harmata | 3fae366 | 2015-04-22 20:10:48 +0000 | [diff] [blame] | 84 | builder.add(labelFromString(labelString)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | for (String labelString : filteredTs) { |
Nathan Harmata | 3fae366 | 2015-04-22 20:10:48 +0000 | [diff] [blame] | 88 | builder.remove(labelFromString(labelString)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 89 | } |
| 90 | this.targets = builder.build(); |
| 91 | } |
| 92 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 93 | @SuppressWarnings("unused") |
| 94 | private void readObjectNoData() { |
| 95 | throw new IllegalStateException(); |
| 96 | } |
| 97 | |
| 98 | /** |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 99 | * Create a target pattern {@link SkyKey}. Throws {@link TargetParsingException} if the provided |
| 100 | * {@code pattern} cannot be parsed. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 101 | * |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 102 | * @param pattern The pattern, eg "-foo/biz...". If the first character is "-", the pattern is |
| 103 | * treated as a negative pattern. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 104 | * @param policy The filtering policy, eg "only return test targets" |
| 105 | * @param offset The offset to apply to relative target patterns. |
| 106 | */ |
| 107 | @ThreadSafe |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 108 | public static SkyKey key(String pattern, FilteringPolicy policy, String offset) |
| 109 | throws TargetParsingException { |
| 110 | return Iterables.getOnlyElement(keys(ImmutableList.of(pattern), policy, offset)).getSkyKey(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 114 | * Returns an iterable of {@link TargetPatternSkyKeyOrException}, with {@link TargetPatternKey} |
Mark Schaller | 20c2d03 | 2015-06-18 16:51:03 +0000 | [diff] [blame] | 115 | * arguments, in the same order as the list of patterns provided as input. If a provided pattern |
| 116 | * fails to parse, the element in the returned iterable corresponding to it will throw when its |
| 117 | * {@link TargetPatternSkyKeyOrException#getSkyKey} method is called. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 118 | * |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 119 | * @param patterns The list of patterns, eg "-foo/biz...". If a pattern's first character is "-", |
| 120 | * it is treated as a negative pattern. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 121 | * @param policy The filtering policy, eg "only return test targets" |
| 122 | * @param offset The offset to apply to relative target patterns. |
| 123 | */ |
| 124 | @ThreadSafe |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 125 | public static Iterable<TargetPatternSkyKeyOrException> keys(List<String> patterns, |
| 126 | FilteringPolicy policy, String offset) { |
| 127 | TargetPattern.Parser parser = new TargetPattern.Parser(offset); |
| 128 | ImmutableList.Builder<TargetPatternSkyKeyOrException> builder = ImmutableList.builder(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 129 | for (String pattern : patterns) { |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 130 | boolean positive = !pattern.startsWith("-"); |
| 131 | String absoluteValueOfPattern = positive ? pattern : pattern.substring(1); |
Mark Schaller | 20c2d03 | 2015-06-18 16:51:03 +0000 | [diff] [blame] | 132 | TargetPattern targetPattern; |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 133 | try { |
Mark Schaller | 20c2d03 | 2015-06-18 16:51:03 +0000 | [diff] [blame] | 134 | targetPattern = parser.parse(absoluteValueOfPattern); |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 135 | } catch (TargetParsingException e) { |
| 136 | builder.add(new TargetPatternSkyKeyException(e, absoluteValueOfPattern)); |
Mark Schaller | 20c2d03 | 2015-06-18 16:51:03 +0000 | [diff] [blame] | 137 | continue; |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 138 | } |
Mark Schaller | 20c2d03 | 2015-06-18 16:51:03 +0000 | [diff] [blame] | 139 | TargetPatternKey targetPatternKey = new TargetPatternKey(targetPattern, |
| 140 | positive ? policy : FilteringPolicies.NO_FILTER, /*isNegative=*/!positive, offset, |
| 141 | ImmutableSet.<String>of()); |
| 142 | SkyKey skyKey = new SkyKey(SkyFunctions.TARGET_PATTERN, targetPatternKey); |
| 143 | builder.add(new TargetPatternSkyKeyValue(skyKey)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 144 | } |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 145 | return builder.build(); |
| 146 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 147 | |
Nathan Harmata | 3fae366 | 2015-04-22 20:10:48 +0000 | [diff] [blame] | 148 | public ResolvedTargets<Label> getTargets() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 149 | return targets; |
| 150 | } |
| 151 | |
| 152 | /** |
Mark Schaller | 248f044 | 2015-05-19 17:59:36 +0000 | [diff] [blame] | 153 | * A TargetPatternKey is a tuple of pattern (eg, "foo/..."), filtering policy, a relative pattern |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 154 | * offset, whether it is a positive or negative match, and a set of excluded subdirectories. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 155 | */ |
| 156 | @ThreadSafe |
Mark Schaller | 248f044 | 2015-05-19 17:59:36 +0000 | [diff] [blame] | 157 | public static class TargetPatternKey implements Serializable { |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 158 | |
| 159 | private final TargetPattern parsedPattern; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 160 | private final FilteringPolicy policy; |
| 161 | private final boolean isNegative; |
| 162 | |
| 163 | private final String offset; |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 164 | private final ImmutableSet<String> excludedSubdirectories; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 165 | |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 166 | public TargetPatternKey(TargetPattern parsedPattern, FilteringPolicy policy, |
| 167 | boolean isNegative, String offset, ImmutableSet<String> excludedSubdirectories) { |
| 168 | this.parsedPattern = Preconditions.checkNotNull(parsedPattern); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 169 | this.policy = Preconditions.checkNotNull(policy); |
| 170 | this.isNegative = isNegative; |
| 171 | this.offset = offset; |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 172 | this.excludedSubdirectories = Preconditions.checkNotNull(excludedSubdirectories); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | public String getPattern() { |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 176 | return parsedPattern.getOriginalPattern(); |
| 177 | } |
| 178 | |
| 179 | public TargetPattern getParsedPattern() { |
| 180 | return parsedPattern; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | public boolean isNegative() { |
| 184 | return isNegative; |
| 185 | } |
| 186 | |
| 187 | public FilteringPolicy getPolicy() { |
| 188 | return policy; |
| 189 | } |
| 190 | |
| 191 | public String getOffset() { |
| 192 | return offset; |
| 193 | } |
| 194 | |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 195 | public ImmutableSet<String> getExcludedSubdirectories() { |
| 196 | return excludedSubdirectories; |
| 197 | } |
| 198 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 199 | @Override |
| 200 | public String toString() { |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 201 | return (isNegative ? "-" : "") + parsedPattern.getOriginalPattern(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public int hashCode() { |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 206 | return Objects.hash(parsedPattern, isNegative, policy, offset, |
| 207 | excludedSubdirectories); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public boolean equals(Object obj) { |
Mark Schaller | 248f044 | 2015-05-19 17:59:36 +0000 | [diff] [blame] | 212 | if (!(obj instanceof TargetPatternKey)) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 213 | return false; |
| 214 | } |
Mark Schaller | 248f044 | 2015-05-19 17:59:36 +0000 | [diff] [blame] | 215 | TargetPatternKey other = (TargetPatternKey) obj; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 216 | |
Mark Schaller | 66b35f3 | 2015-05-19 21:19:37 +0000 | [diff] [blame] | 217 | return other.isNegative == this.isNegative && other.parsedPattern.equals(this.parsedPattern) |
| 218 | && other.offset.equals(this.offset) && other.policy.equals(this.policy) |
| 219 | && other.excludedSubdirectories.equals(this.excludedSubdirectories); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Wrapper for a target pattern {@link SkyKey} or the {@link TargetParsingException} thrown when |
| 225 | * trying to compute it. |
| 226 | */ |
| 227 | public interface TargetPatternSkyKeyOrException { |
| 228 | |
| 229 | /** |
| 230 | * Returns the stored {@link SkyKey} or throws {@link TargetParsingException} if one was thrown |
| 231 | * when computing the key. |
| 232 | */ |
| 233 | SkyKey getSkyKey() throws TargetParsingException; |
| 234 | |
| 235 | /** |
| 236 | * Returns the pattern that resulted in the stored {@link SkyKey} or {@link |
| 237 | * TargetParsingException}. |
| 238 | */ |
| 239 | String getOriginalPattern(); |
| 240 | } |
| 241 | |
| 242 | private static final class TargetPatternSkyKeyValue implements TargetPatternSkyKeyOrException { |
| 243 | |
| 244 | private final SkyKey value; |
| 245 | |
| 246 | private TargetPatternSkyKeyValue(SkyKey value) { |
| 247 | this.value = value; |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public SkyKey getSkyKey() throws TargetParsingException { |
| 252 | return value; |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public String getOriginalPattern() { |
| 257 | return ((TargetPatternKey) value.argument()).getPattern(); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | private static final class TargetPatternSkyKeyException implements |
| 262 | TargetPatternSkyKeyOrException { |
| 263 | |
| 264 | private final TargetParsingException exception; |
| 265 | private final String originalPattern; |
| 266 | |
| 267 | private TargetPatternSkyKeyException(TargetParsingException exception, String originalPattern) { |
| 268 | this.exception = exception; |
| 269 | this.originalPattern = originalPattern; |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public SkyKey getSkyKey() throws TargetParsingException { |
| 274 | throw exception; |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public String getOriginalPattern() { |
| 279 | return originalPattern; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | } |