Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +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 | |
| 16 | import com.google.common.collect.ImmutableList; |
| 17 | import com.google.common.collect.ImmutableSet; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.lib.cmdline.Label; |
Kristina Chodorow | 73fa203 | 2015-08-28 17:57:46 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
Lukacs Berki | d72db8d | 2015-09-22 07:40:24 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.cmdline.PackageIdentifier.RepositoryName; |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.cmdline.ResolvedTargets; |
| 22 | import com.google.devtools.build.lib.cmdline.TargetParsingException; |
| 23 | import com.google.devtools.build.lib.cmdline.TargetPattern; |
| 24 | import com.google.devtools.build.lib.cmdline.TargetPatternResolver; |
| 25 | import com.google.devtools.build.lib.events.Event; |
| 26 | import com.google.devtools.build.lib.packages.NoSuchPackageException; |
| 27 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
| 28 | import com.google.devtools.build.lib.packages.NoSuchThingException; |
| 29 | import com.google.devtools.build.lib.packages.Package; |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.packages.Target; |
| 31 | import com.google.devtools.build.lib.pkgcache.FilteringPolicies; |
| 32 | import com.google.devtools.build.lib.pkgcache.FilteringPolicy; |
| 33 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
| 34 | import com.google.devtools.build.lib.pkgcache.TargetPatternResolverUtil; |
| 35 | import com.google.devtools.build.lib.skyframe.EnvironmentBackedRecursivePackageProvider.MissingDepException; |
Janak Ramakrishnan | 5b5f22a | 2016-01-07 17:07:29 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.util.BatchCallback; |
Janak Ramakrishnan | 006ab49 | 2016-01-07 17:39:41 +0000 | [diff] [blame] | 37 | import com.google.devtools.build.lib.util.BatchCallback.NullCallback; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 38 | import com.google.devtools.build.lib.util.Preconditions; |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.lib.vfs.Path; |
| 40 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 41 | import com.google.devtools.build.lib.vfs.RootedPath; |
| 42 | import com.google.devtools.build.skyframe.SkyFunction; |
| 43 | import com.google.devtools.build.skyframe.SkyFunctionException; |
| 44 | import com.google.devtools.build.skyframe.SkyKey; |
| 45 | import com.google.devtools.build.skyframe.SkyValue; |
| 46 | |
Lukacs Berki | c7106d4 | 2015-10-15 07:45:54 +0000 | [diff] [blame] | 47 | import java.util.ArrayList; |
| 48 | import java.util.List; |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 49 | import java.util.concurrent.atomic.AtomicReference; |
| 50 | |
| 51 | import javax.annotation.Nullable; |
| 52 | |
| 53 | /** |
| 54 | * PrepareDepsOfPatternFunction ensures the graph loads targets matching the pattern and its |
| 55 | * transitive dependencies. |
| 56 | */ |
| 57 | public class PrepareDepsOfPatternFunction implements SkyFunction { |
| 58 | |
| 59 | private final AtomicReference<PathPackageLocator> pkgPath; |
| 60 | |
| 61 | public PrepareDepsOfPatternFunction(AtomicReference<PathPackageLocator> pkgPath) { |
| 62 | this.pkgPath = pkgPath; |
| 63 | } |
| 64 | |
| 65 | @Nullable |
| 66 | @Override |
| 67 | public SkyValue compute(SkyKey key, Environment env) |
| 68 | throws SkyFunctionException, InterruptedException { |
| 69 | TargetPatternValue.TargetPatternKey patternKey = |
| 70 | ((TargetPatternValue.TargetPatternKey) key.argument()); |
Mark Schaller | d7311e0 | 2015-07-07 16:36:09 +0000 | [diff] [blame] | 71 | |
| 72 | // DepsOfPatternPreparer below expects to be able to ignore the filtering policy from the |
| 73 | // TargetPatternKey, which should be valid because PrepareDepsOfPatternValue.keys |
| 74 | // unconditionally creates TargetPatternKeys with the NO_FILTER filtering policy. (Compare |
| 75 | // with SkyframeTargetPatternEvaluator, which can create TargetPatternKeys with other |
| 76 | // filtering policies like FILTER_TESTS or FILTER_MANUAL.) This check makes sure that the |
| 77 | // key's filtering policy is NO_FILTER as expected. |
| 78 | Preconditions.checkState(patternKey.getPolicy().equals(FilteringPolicies.NO_FILTER), |
| 79 | patternKey.getPolicy()); |
| 80 | |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 81 | try { |
| 82 | TargetPattern parsedPattern = patternKey.getParsedPattern(); |
Mark Schaller | d7311e0 | 2015-07-07 16:36:09 +0000 | [diff] [blame] | 83 | DepsOfPatternPreparer preparer = new DepsOfPatternPreparer(env, pkgPath.get()); |
Janak Ramakrishnan | 3d9441b | 2016-01-13 17:38:29 +0000 | [diff] [blame^] | 84 | ImmutableSet<PathFragment> excludedSubdirectories = patternKey.getExcludedSubdirectories(); |
Janak Ramakrishnan | 006ab49 | 2016-01-07 17:39:41 +0000 | [diff] [blame] | 85 | parsedPattern.<Void, RuntimeException>eval( |
| 86 | preparer, excludedSubdirectories, NullCallback.<Void>instance()); |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 87 | } catch (TargetParsingException e) { |
| 88 | throw new PrepareDepsOfPatternFunctionException(e); |
| 89 | } catch (MissingDepException e) { |
| 90 | // The DepsOfPatternPreparer constructed above might throw MissingDepException to signal |
| 91 | // when it has a dependency on a missing Environment value. |
| 92 | return null; |
| 93 | } |
| 94 | return PrepareDepsOfPatternValue.INSTANCE; |
| 95 | } |
| 96 | |
| 97 | @Nullable |
| 98 | @Override |
| 99 | public String extractTag(SkyKey skyKey) { |
| 100 | return null; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Used to declare all the exception types that can be wrapped in the exception thrown by {@link |
| 105 | * PrepareDepsOfPatternFunction#compute}. |
| 106 | */ |
| 107 | private static final class PrepareDepsOfPatternFunctionException extends SkyFunctionException { |
| 108 | |
| 109 | public PrepareDepsOfPatternFunctionException(TargetParsingException e) { |
| 110 | super(e, Transience.PERSISTENT); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
Ulf Adams | 3ab82f7 | 2015-09-04 12:10:53 +0000 | [diff] [blame] | 115 | * A {@link TargetPatternResolver} backed by an {@link |
| 116 | * com.google.devtools.build.skyframe.SkyFunction.Environment} whose methods do not actually |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 117 | * return resolved targets, but that ensures the graph loads the matching targets <b>and</b> their |
| 118 | * transitive dependencies. Its methods may throw {@link MissingDepException} if the package |
| 119 | * values this depends on haven't been calculated and added to its environment. |
| 120 | */ |
| 121 | static class DepsOfPatternPreparer implements TargetPatternResolver<Void> { |
| 122 | |
| 123 | private final EnvironmentBackedRecursivePackageProvider packageProvider; |
| 124 | private final Environment env; |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 125 | private final PathPackageLocator pkgPath; |
| 126 | |
Mark Schaller | d7311e0 | 2015-07-07 16:36:09 +0000 | [diff] [blame] | 127 | public DepsOfPatternPreparer(Environment env, PathPackageLocator pkgPath) { |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 128 | this.env = env; |
| 129 | this.packageProvider = new EnvironmentBackedRecursivePackageProvider(env); |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 130 | this.pkgPath = pkgPath; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void warn(String msg) { |
| 135 | env.getListener().handle(Event.warn(msg)); |
| 136 | } |
| 137 | |
| 138 | @Override |
Lukacs Berki | 960dc27 | 2015-09-24 07:48:36 +0000 | [diff] [blame] | 139 | public Void getTargetOrNull(Label label) throws InterruptedException { |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 140 | // Note: |
| 141 | // This method is used in just one place, TargetPattern.TargetsInPackage#getWildcardConflict. |
| 142 | // Returning null tells #getWildcardConflict that there is not a target with a name like |
| 143 | // "all" or "all-targets", which means that TargetPattern.TargetsInPackage will end up |
| 144 | // calling DepsOfTargetPreparer#getTargetsInPackage. |
| 145 | // TODO (bazel-team): Consider replacing this with an isTarget method on the interface. |
| 146 | return null; |
| 147 | } |
| 148 | |
| 149 | @Override |
Lukacs Berki | 960dc27 | 2015-09-24 07:48:36 +0000 | [diff] [blame] | 150 | public ResolvedTargets<Void> getExplicitTarget(Label label) |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 151 | throws TargetParsingException, InterruptedException { |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 152 | try { |
| 153 | Target target = packageProvider.getTarget(env.getListener(), label); |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 154 | SkyKey key = TransitiveTraversalValue.key(target.getLabel()); |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 155 | SkyValue token = |
| 156 | env.getValueOrThrow(key, NoSuchPackageException.class, NoSuchTargetException.class); |
| 157 | if (token == null) { |
| 158 | throw new MissingDepException(); |
| 159 | } |
| 160 | return ResolvedTargets.empty(); |
| 161 | } catch (NoSuchThingException e) { |
| 162 | throw new TargetParsingException(e.getMessage(), e); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | @Override |
Lukacs Berki | 10e3b2b | 2015-09-22 07:58:20 +0000 | [diff] [blame] | 167 | public ResolvedTargets<Void> getTargetsInPackage(String originalPattern, |
| 168 | PackageIdentifier packageIdentifier, boolean rulesOnly) throws TargetParsingException { |
Mark Schaller | d7311e0 | 2015-07-07 16:36:09 +0000 | [diff] [blame] | 169 | FilteringPolicy policy = |
| 170 | rulesOnly ? FilteringPolicies.RULES_ONLY : FilteringPolicies.NO_FILTER; |
Lukacs Berki | 10e3b2b | 2015-09-22 07:58:20 +0000 | [diff] [blame] | 171 | return getTargetsInPackage(originalPattern, packageIdentifier, policy); |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | private ResolvedTargets<Void> getTargetsInPackage(String originalPattern, |
Lukacs Berki | 10e3b2b | 2015-09-22 07:58:20 +0000 | [diff] [blame] | 175 | PackageIdentifier packageIdentifier, FilteringPolicy policy) |
Ulf Adams | 3ab82f7 | 2015-09-04 12:10:53 +0000 | [diff] [blame] | 176 | throws TargetParsingException { |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 177 | try { |
Lukacs Berki | 10e3b2b | 2015-09-22 07:58:20 +0000 | [diff] [blame] | 178 | Package pkg = packageProvider.getPackage(env.getListener(), packageIdentifier); |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 179 | ResolvedTargets<Target> packageTargets = |
| 180 | TargetPatternResolverUtil.resolvePackageTargets(pkg, policy); |
| 181 | ImmutableList.Builder<SkyKey> builder = ImmutableList.builder(); |
| 182 | for (Target target : packageTargets.getTargets()) { |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 183 | builder.add(TransitiveTraversalValue.key(target.getLabel())); |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 184 | } |
| 185 | ImmutableList<SkyKey> skyKeys = builder.build(); |
| 186 | env.getValuesOrThrow(skyKeys, NoSuchPackageException.class, NoSuchTargetException.class); |
| 187 | if (env.valuesMissing()) { |
| 188 | throw new MissingDepException(); |
| 189 | } |
| 190 | return ResolvedTargets.empty(); |
| 191 | } catch (NoSuchThingException e) { |
| 192 | String message = TargetPatternResolverUtil.getParsingErrorMessage( |
| 193 | "package contains errors", originalPattern); |
| 194 | throw new TargetParsingException(message, e); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | @Override |
Lukacs Berki | 10e3b2b | 2015-09-22 07:58:20 +0000 | [diff] [blame] | 199 | public boolean isPackage(PackageIdentifier packageIdentifier) { |
| 200 | return packageProvider.isPackage(env.getListener(), packageIdentifier); |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public String getTargetKind(Void target) { |
| 205 | // Note: |
| 206 | // This method is used in just one place, TargetPattern.TargetsInPackage#getWildcardConflict. |
| 207 | // Because DepsOfPatternPreparer#getTargetOrNull always returns null, this method is never |
| 208 | // called. |
| 209 | throw new UnsupportedOperationException(); |
| 210 | } |
| 211 | |
| 212 | @Override |
Janak Ramakrishnan | 5b5f22a | 2016-01-07 17:07:29 +0000 | [diff] [blame] | 213 | public <E extends Exception> void findTargetsBeneathDirectory( |
| 214 | RepositoryName repository, |
| 215 | String originalPattern, |
| 216 | String directory, |
| 217 | boolean rulesOnly, |
Janak Ramakrishnan | 3d9441b | 2016-01-13 17:38:29 +0000 | [diff] [blame^] | 218 | ImmutableSet<PathFragment> excludedSubdirectories, |
Janak Ramakrishnan | 5b5f22a | 2016-01-07 17:07:29 +0000 | [diff] [blame] | 219 | BatchCallback<Void, E> callback) |
| 220 | throws TargetParsingException, E, InterruptedException { |
Mark Schaller | d7311e0 | 2015-07-07 16:36:09 +0000 | [diff] [blame] | 221 | FilteringPolicy policy = |
| 222 | rulesOnly ? FilteringPolicies.RULES_ONLY : FilteringPolicies.NO_FILTER; |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 223 | PathFragment pathFragment = TargetPatternResolverUtil.getPathFragment(directory); |
Lukacs Berki | c7106d4 | 2015-10-15 07:45:54 +0000 | [diff] [blame] | 224 | List<Path> roots = new ArrayList<>(); |
| 225 | if (repository.isDefault()) { |
| 226 | roots.addAll(pkgPath.getPathEntries()); |
| 227 | } else { |
| 228 | RepositoryValue repositoryValue = |
| 229 | (RepositoryValue) env.getValue(RepositoryValue.key(repository)); |
| 230 | if (repositoryValue == null) { |
| 231 | throw new MissingDepException(); |
| 232 | } |
| 233 | |
| 234 | roots.add(repositoryValue.getPath()); |
| 235 | } |
| 236 | |
| 237 | for (Path root : roots) { |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 238 | RootedPath rootedPath = RootedPath.toRootedPath(root, pathFragment); |
Janak Ramakrishnan | 3d9441b | 2016-01-13 17:38:29 +0000 | [diff] [blame^] | 239 | SkyValue token = |
| 240 | env.getValue( |
| 241 | PrepareDepsOfTargetsUnderDirectoryValue.key( |
| 242 | repository, rootedPath, excludedSubdirectories, policy)); |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 243 | if (token == null) { |
| 244 | // A null token value means there is a missing dependency, because RecursivePkgFunction |
| 245 | // never throws. |
| 246 | throw new MissingDepException(); |
| 247 | } |
| 248 | } |
Mark Schaller | 7b0bc0a | 2015-06-30 23:57:45 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |