Search in sources :

Example 1 with TargetPattern

use of com.google.devtools.build.lib.cmdline.TargetPattern in project bazel by bazelbuild.

the class GraphBackedRecursivePackageProvider method getPackagesUnderDirectory.

@Override
public Iterable<PathFragment> getPackagesUnderDirectory(ExtendedEventHandler eventHandler, RepositoryName repository, PathFragment directory, ImmutableSet<PathFragment> excludedSubdirectories) throws InterruptedException {
    PathFragment.checkAllPathsAreUnder(excludedSubdirectories, directory);
    // Check that this package is covered by at least one of our universe patterns.
    boolean inUniverse = false;
    for (TargetPatternKey patternKey : universeTargetPatternKeys) {
        TargetPattern pattern = patternKey.getParsedPattern();
        boolean isTBD = pattern.getType().equals(Type.TARGETS_BELOW_DIRECTORY);
        PackageIdentifier packageIdentifier = PackageIdentifier.create(repository, directory);
        if (isTBD && pattern.containsAllTransitiveSubdirectoriesForTBD(packageIdentifier)) {
            inUniverse = true;
            break;
        }
    }
    if (!inUniverse) {
        return ImmutableList.of();
    }
    List<Path> roots = new ArrayList<>();
    if (repository.isMain()) {
        roots.addAll(pkgPath.getPathEntries());
    } else {
        RepositoryDirectoryValue repositoryValue = (RepositoryDirectoryValue) graph.getValue(RepositoryDirectoryValue.key(repository));
        if (repositoryValue == null) {
            // "nothing".
            return ImmutableList.of();
        }
        roots.add(repositoryValue.getPath());
    }
    // If we found a TargetsBelowDirectory pattern in the universe that contains this directory,
    // then we can look for packages in and under it in the graph. If we didn't find one, then the
    // directory wasn't in the universe, so return an empty list.
    ImmutableList.Builder<PathFragment> builder = ImmutableList.builder();
    for (Path root : roots) {
        RootedPath rootedDir = RootedPath.toRootedPath(root, directory);
        TraversalInfo info = new TraversalInfo(rootedDir, excludedSubdirectories);
        collectPackagesUnder(eventHandler, repository, ImmutableSet.of(info), builder);
    }
    return builder.build();
}
Also used : RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Path(com.google.devtools.build.lib.vfs.Path) TargetPatternKey(com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternKey) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) TargetPattern(com.google.devtools.build.lib.cmdline.TargetPattern) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) RepositoryDirectoryValue(com.google.devtools.build.lib.rules.repository.RepositoryDirectoryValue)

Example 2 with TargetPattern

use of com.google.devtools.build.lib.cmdline.TargetPattern in project bazel by bazelbuild.

the class TargetPatternFunction method compute.

@Override
public SkyValue compute(SkyKey key, Environment env) throws TargetPatternFunctionException, InterruptedException {
    TargetPatternValue.TargetPatternKey patternKey = ((TargetPatternValue.TargetPatternKey) key.argument());
    ResolvedTargets<Target> resolvedTargets;
    try {
        EnvironmentBackedRecursivePackageProvider provider = new EnvironmentBackedRecursivePackageProvider(env);
        RecursivePackageProviderBackedTargetPatternResolver resolver = new RecursivePackageProviderBackedTargetPatternResolver(provider, env.getListener(), patternKey.getPolicy(), MultisetSemaphore.<PackageIdentifier>unbounded());
        TargetPattern parsedPattern = patternKey.getParsedPattern();
        ImmutableSet<PathFragment> excludedSubdirectories = patternKey.getExcludedSubdirectories();
        final Set<Target> results = CompactHashSet.create();
        BatchCallback<Target, RuntimeException> callback = new BatchCallback<Target, RuntimeException>() {

            @Override
            public void process(Iterable<Target> partialResult) {
                Iterables.addAll(results, partialResult);
            }
        };
        parsedPattern.eval(resolver, excludedSubdirectories, callback, RuntimeException.class);
        resolvedTargets = ResolvedTargets.<Target>builder().addAll(results).build();
    } catch (TargetParsingException e) {
        throw new TargetPatternFunctionException(e);
    } catch (MissingDepException e) {
        // implementations that are unconcerned with MissingDepExceptions.
        return null;
    }
    Preconditions.checkNotNull(resolvedTargets, key);
    ResolvedTargets.Builder<Label> resolvedLabelsBuilder = ResolvedTargets.builder();
    for (Target target : resolvedTargets.getTargets()) {
        resolvedLabelsBuilder.add(target.getLabel());
    }
    for (Target target : resolvedTargets.getFilteredTargets()) {
        resolvedLabelsBuilder.remove(target.getLabel());
    }
    return new TargetPatternValue(resolvedLabelsBuilder.build());
}
Also used : PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Label(com.google.devtools.build.lib.cmdline.Label) Target(com.google.devtools.build.lib.packages.Target) TargetPattern(com.google.devtools.build.lib.cmdline.TargetPattern) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) ResolvedTargets(com.google.devtools.build.lib.cmdline.ResolvedTargets) BatchCallback(com.google.devtools.build.lib.util.BatchCallback) MissingDepException(com.google.devtools.build.lib.skyframe.EnvironmentBackedRecursivePackageProvider.MissingDepException)

Example 3 with TargetPattern

use of com.google.devtools.build.lib.cmdline.TargetPattern in project bazel by bazelbuild.

the class PrepareDepsOfPatternFunction method compute.

@Nullable
@Override
public SkyValue compute(SkyKey key, Environment env) throws SkyFunctionException, InterruptedException {
    TargetPatternValue.TargetPatternKey patternKey = ((TargetPatternValue.TargetPatternKey) key.argument());
    // DepsOfPatternPreparer below expects to be able to ignore the filtering policy from the
    // TargetPatternKey, which should be valid because PrepareDepsOfPatternValue.keys
    // unconditionally creates TargetPatternKeys with the NO_FILTER filtering policy. (Compare
    // with SkyframeTargetPatternEvaluator, which can create TargetPatternKeys with other
    // filtering policies like FILTER_TESTS or FILTER_MANUAL.) This check makes sure that the
    // key's filtering policy is NO_FILTER as expected.
    Preconditions.checkState(patternKey.getPolicy().equals(FilteringPolicies.NO_FILTER), patternKey.getPolicy());
    TargetPattern parsedPattern = patternKey.getParsedPattern();
    BlacklistedPackagePrefixesValue blacklist = (BlacklistedPackagePrefixesValue) env.getValue(BlacklistedPackagePrefixesValue.key());
    if (blacklist == null) {
        return null;
    }
    ImmutableSet<PathFragment> subdirectoriesToExclude = patternKey.getAllSubdirectoriesToExclude(blacklist.getPatterns());
    DepsOfPatternPreparer preparer = new DepsOfPatternPreparer(env, pkgPath.get());
    try {
        parsedPattern.eval(preparer, subdirectoriesToExclude, NullCallback.<Void>instance(), RuntimeException.class);
    } catch (TargetParsingException e) {
        throw new PrepareDepsOfPatternFunctionException(e);
    } catch (MissingDepException e) {
        // when it has a dependency on a missing Environment value.
        return null;
    }
    return PrepareDepsOfPatternValue.INSTANCE;
}
Also used : TargetPattern(com.google.devtools.build.lib.cmdline.TargetPattern) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) MissingDepException(com.google.devtools.build.lib.skyframe.EnvironmentBackedRecursivePackageProvider.MissingDepException) Nullable(javax.annotation.Nullable)

Example 4 with TargetPattern

use of com.google.devtools.build.lib.cmdline.TargetPattern in project bazel by bazelbuild.

the class TargetPatternValue method keys.

/**
   * Returns an iterable of {@link TargetPatternSkyKeyOrException}, with {@link TargetPatternKey}
   * arguments, in the same order as the list of patterns provided as input. If a provided pattern
   * fails to parse, the element in the returned iterable corresponding to it will throw when its
   * {@link TargetPatternSkyKeyOrException#getSkyKey} method is called.
   *
   * @param patterns The list of patterns, eg "-foo/biz...". If a pattern's first character is "-",
   *     it is treated as a negative pattern.
   * @param policy The filtering policy, eg "only return test targets"
   * @param offset The offset to apply to relative target patterns.
   */
@ThreadSafe
public static Iterable<TargetPatternSkyKeyOrException> keys(List<String> patterns, FilteringPolicy policy, String offset) {
    TargetPattern.Parser parser = new TargetPattern.Parser(offset);
    ImmutableList.Builder<TargetPatternSkyKeyOrException> builder = ImmutableList.builder();
    for (String pattern : patterns) {
        boolean positive = !pattern.startsWith("-");
        String absoluteValueOfPattern = positive ? pattern : pattern.substring(1);
        TargetPattern targetPattern;
        try {
            targetPattern = parser.parse(absoluteValueOfPattern);
        } catch (TargetParsingException e) {
            builder.add(new TargetPatternSkyKeyException(e, absoluteValueOfPattern));
            continue;
        }
        TargetPatternKey targetPatternKey = new TargetPatternKey(targetPattern, positive ? policy : FilteringPolicies.NO_FILTER, /*isNegative=*/
        !positive, offset, ImmutableSet.<PathFragment>of());
        SkyKey skyKey = SkyKey.create(SkyFunctions.TARGET_PATTERN, targetPatternKey);
        builder.add(new TargetPatternSkyKeyValue(skyKey));
    }
    return builder.build();
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) ImmutableList(com.google.common.collect.ImmutableList) TargetPattern(com.google.devtools.build.lib.cmdline.TargetPattern) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) ThreadSafe(com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)

Example 5 with TargetPattern

use of com.google.devtools.build.lib.cmdline.TargetPattern in project bazel by bazelbuild.

the class SkyQueryEnvironment method getTargetsMatchingPattern.

@ThreadSafe
@Override
public QueryTaskFuture<Void> getTargetsMatchingPattern(final QueryExpression owner, String pattern, Callback<Target> callback) {
    // Directly evaluate the target pattern, making use of packages in the graph.
    Pair<TargetPattern, ImmutableSet<PathFragment>> patternToEvalAndSubdirectoriesToExclude;
    try {
        patternToEvalAndSubdirectoriesToExclude = getPatternAndExcludes(pattern);
    } catch (TargetParsingException tpe) {
        try {
            reportBuildFileError(owner, tpe.getMessage());
        } catch (QueryException qe) {
            return immediateFailedFuture(qe);
        }
        return immediateSuccessfulFuture(null);
    } catch (InterruptedException ie) {
        return immediateCancelledFuture();
    }
    TargetPattern patternToEval = patternToEvalAndSubdirectoriesToExclude.getFirst();
    ImmutableSet<PathFragment> subdirectoriesToExclude = patternToEvalAndSubdirectoriesToExclude.getSecond();
    AsyncFunction<TargetParsingException, Void> reportBuildFileErrorAsyncFunction = new AsyncFunction<TargetParsingException, Void>() {

        @Override
        public ListenableFuture<Void> apply(TargetParsingException exn) throws QueryException {
            reportBuildFileError(owner, exn.getMessage());
            return Futures.immediateFuture(null);
        }
    };
    ListenableFuture<Void> evalFuture = patternToEval.evalAsync(resolver, subdirectoriesToExclude, callback, QueryException.class, executor);
    return QueryTaskFutureImpl.ofDelegate(Futures.catchingAsync(evalFuture, TargetParsingException.class, reportBuildFileErrorAsyncFunction));
}
Also used : QueryException(com.google.devtools.build.lib.query2.engine.QueryException) TargetPattern(com.google.devtools.build.lib.cmdline.TargetPattern) ImmutableSet(com.google.common.collect.ImmutableSet) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) ThreadSafe(com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)

Aggregations

TargetPattern (com.google.devtools.build.lib.cmdline.TargetPattern)6 TargetParsingException (com.google.devtools.build.lib.cmdline.TargetParsingException)5 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)5 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 ThreadSafe (com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)2 MissingDepException (com.google.devtools.build.lib.skyframe.EnvironmentBackedRecursivePackageProvider.MissingDepException)2 TargetPatternKey (com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternKey)2 SkyKey (com.google.devtools.build.skyframe.SkyKey)2 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)1 Label (com.google.devtools.build.lib.cmdline.Label)1 PackageIdentifier (com.google.devtools.build.lib.cmdline.PackageIdentifier)1 ResolvedTargets (com.google.devtools.build.lib.cmdline.ResolvedTargets)1 Target (com.google.devtools.build.lib.packages.Target)1 QueryException (com.google.devtools.build.lib.query2.engine.QueryException)1 RepositoryDirectoryValue (com.google.devtools.build.lib.rules.repository.RepositoryDirectoryValue)1 TargetPatternSkyKeyOrException (com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternSkyKeyOrException)1 BatchCallback (com.google.devtools.build.lib.util.BatchCallback)1 Path (com.google.devtools.build.lib.vfs.Path)1 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)1