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 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 16 | import com.google.common.base.Preconditions; |
Kristina Chodorow | 73fa203 | 2015-08-28 17:57:46 +0000 | [diff] [blame] | 17 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
| 19 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
| 20 | import com.google.devtools.build.lib.collect.nestedset.Order; |
| 21 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
| 22 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 24 | import com.google.devtools.build.lib.vfs.Root; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.devtools.build.lib.vfs.UnixGlob; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | import com.google.devtools.build.skyframe.SkyValue; |
| 27 | |
Janak Ramakrishnan | 29c5ab4 | 2015-05-14 19:38:12 +0000 | [diff] [blame] | 28 | /** A value corresponding to a glob. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | @Immutable |
| 30 | @ThreadSafe |
Janak Ramakrishnan | 29c5ab4 | 2015-05-14 19:38:12 +0000 | [diff] [blame] | 31 | public final class GlobValue implements SkyValue { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 32 | |
Michajlo Matijkiw | c3738b1 | 2015-10-14 23:36:38 +0000 | [diff] [blame] | 33 | public static final GlobValue EMPTY = new GlobValue( |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 34 | NestedSetBuilder.<PathFragment>emptySet(Order.STABLE_ORDER)); |
| 35 | |
| 36 | private final NestedSet<PathFragment> matches; |
| 37 | |
Michajlo Matijkiw | c3738b1 | 2015-10-14 23:36:38 +0000 | [diff] [blame] | 38 | /** |
| 39 | * Create a GlobValue wrapping {@code matches}. {@code matches} must have order |
| 40 | * {@link Order#STABLE_ORDER}. |
| 41 | */ |
| 42 | public GlobValue(NestedSet<PathFragment> matches) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | this.matches = Preconditions.checkNotNull(matches); |
Michajlo Matijkiw | c3738b1 | 2015-10-14 23:36:38 +0000 | [diff] [blame] | 44 | Preconditions.checkState(matches.getOrder() == Order.STABLE_ORDER, |
| 45 | "Only STABLE_ORDER is supported, but got %s", matches.getOrder()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /** |
Janak Ramakrishnan | 063b488 | 2016-07-18 20:33:28 +0000 | [diff] [blame] | 49 | * Returns glob matches. The matches will be in a deterministic but unspecified order. If a |
| 50 | * particular order is required, the returned iterable should be sorted. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 51 | */ |
Janak Ramakrishnan | 29c5ab4 | 2015-05-14 19:38:12 +0000 | [diff] [blame] | 52 | public NestedSet<PathFragment> getMatches() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | return matches; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public boolean equals(Object other) { |
| 58 | if (other == this) { |
| 59 | return true; |
| 60 | } |
| 61 | if (!(other instanceof GlobValue)) { |
| 62 | return false; |
| 63 | } |
| 64 | // shallowEquals() may fail to detect that two equivalent (according to toString()) |
| 65 | // NestedSets are equal, but will always detect when two NestedSets are different. |
| 66 | // This makes this implementation of equals() overly strict, but we only call this |
| 67 | // method when doing change pruning, which can accept false negatives. |
| 68 | return getMatches().shallowEquals(((GlobValue) other).getMatches()); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public int hashCode() { |
| 73 | return matches.shallowHashCode(); |
| 74 | } |
| 75 | |
| 76 | /** |
janakr | 43c642d | 2018-10-18 09:05:08 -0700 | [diff] [blame] | 77 | * Constructs a {@link GlobDescriptor} for a glob lookup. {@code packageName} is assumed to be an |
| 78 | * existing package. Trying to glob into a non-package is undefined behavior. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 79 | * |
| 80 | * @throws InvalidGlobPatternException if the pattern is not valid. |
| 81 | */ |
| 82 | @ThreadSafe |
janakr | 43c642d | 2018-10-18 09:05:08 -0700 | [diff] [blame] | 83 | public static GlobDescriptor key( |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 84 | PackageIdentifier packageId, |
| 85 | Root packageRoot, |
| 86 | String pattern, |
| 87 | boolean excludeDirs, |
| 88 | PathFragment subdir) |
| 89 | throws InvalidGlobPatternException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 90 | if (pattern.indexOf('?') != -1) { |
| 91 | throw new InvalidGlobPatternException(pattern, "wildcard ? forbidden"); |
| 92 | } |
| 93 | |
| 94 | String error = UnixGlob.checkPatternForError(pattern); |
| 95 | if (error != null) { |
| 96 | throw new InvalidGlobPatternException(pattern, error); |
| 97 | } |
| 98 | |
Nathan Harmata | b795e6b | 2016-02-04 01:10:19 +0000 | [diff] [blame] | 99 | return internalKey(packageId, packageRoot, subdir, pattern, excludeDirs); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
janakr | 43c642d | 2018-10-18 09:05:08 -0700 | [diff] [blame] | 103 | * Constructs a {@link GlobDescriptor} for a glob lookup. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 104 | * |
| 105 | * <p>Do not use outside {@code GlobFunction}. |
| 106 | */ |
| 107 | @ThreadSafe |
janakr | 43c642d | 2018-10-18 09:05:08 -0700 | [diff] [blame] | 108 | static GlobDescriptor internalKey( |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 109 | PackageIdentifier packageId, |
| 110 | Root packageRoot, |
| 111 | PathFragment subdir, |
| 112 | String pattern, |
| 113 | boolean excludeDirs) { |
michajlo | 19e42c9 | 2018-01-03 12:44:17 -0800 | [diff] [blame] | 114 | return GlobDescriptor.create(packageId, packageRoot, subdir, pattern, excludeDirs); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 118 | * An exception that indicates that a glob pattern is syntactically invalid. |
| 119 | */ |
| 120 | @ThreadSafe |
Janak Ramakrishnan | 29c5ab4 | 2015-05-14 19:38:12 +0000 | [diff] [blame] | 121 | public static final class InvalidGlobPatternException extends Exception { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 122 | private final String pattern; |
| 123 | |
| 124 | InvalidGlobPatternException(String pattern, String error) { |
| 125 | super(error); |
| 126 | this.pattern = pattern; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public String toString() { |
| 131 | return String.format("invalid glob pattern '%s': %s", pattern, getMessage()); |
| 132 | } |
| 133 | } |
| 134 | } |