Search in sources :

Example 6 with ResolvedTargets

use of com.google.devtools.build.lib.cmdline.ResolvedTargets 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 7 with ResolvedTargets

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

the class TargetPatternPhaseFunction method determineTests.

/**
   * Interpret test target labels from the command-line arguments and return the corresponding set
   * of targets, handling the filter flags, and expanding test suites.
   *
   * @param targetPatterns the list of command-line target patterns specified by the user
   * @param testFilter the test filter
   */
private static ResolvedTargets<Target> determineTests(Environment env, List<String> targetPatterns, String offset, TestFilter testFilter) throws InterruptedException {
    List<SkyKey> patternSkyKeys = new ArrayList<>();
    for (TargetPatternSkyKeyOrException keyOrException : TargetPatternValue.keys(targetPatterns, FilteringPolicies.FILTER_TESTS, offset)) {
        try {
            patternSkyKeys.add(keyOrException.getSkyKey());
        } catch (TargetParsingException e) {
        // Skip.
        }
    }
    Map<SkyKey, ValueOrException<TargetParsingException>> resolvedPatterns = env.getValuesOrThrow(patternSkyKeys, TargetParsingException.class);
    if (env.valuesMissing()) {
        return null;
    }
    List<SkyKey> expandedSuiteKeys = new ArrayList<>();
    for (SkyKey key : patternSkyKeys) {
        TargetPatternValue value;
        try {
            value = (TargetPatternValue) resolvedPatterns.get(key).get();
        } catch (TargetParsingException e) {
            // Skip.
            continue;
        }
        expandedSuiteKeys.add(TestSuiteExpansionValue.key(value.getTargets().getTargets()));
    }
    Map<SkyKey, SkyValue> expandedSuites = env.getValues(expandedSuiteKeys);
    if (env.valuesMissing()) {
        return null;
    }
    ResolvedTargets.Builder<Target> testTargetsBuilder = ResolvedTargets.builder();
    for (SkyKey key : patternSkyKeys) {
        TargetPatternKey pattern = (TargetPatternKey) key.argument();
        TargetPatternValue value;
        try {
            value = (TargetPatternValue) resolvedPatterns.get(key).get();
        } catch (TargetParsingException e) {
            // This was already reported in getTargetsToBuild (maybe merge the two code paths?).
            continue;
        }
        TestSuiteExpansionValue expandedSuitesValue = (TestSuiteExpansionValue) expandedSuites.get(TestSuiteExpansionValue.key(value.getTargets().getTargets()));
        if (pattern.isNegative()) {
            ResolvedTargets<Target> negativeTargets = expandedSuitesValue.getTargets();
            testTargetsBuilder.filter(Predicates.not(Predicates.in(negativeTargets.getTargets())));
            testTargetsBuilder.mergeError(negativeTargets.hasError());
        } else {
            ResolvedTargets<Target> positiveTargets = expandedSuitesValue.getTargets();
            testTargetsBuilder.addAll(positiveTargets.getTargets());
            testTargetsBuilder.mergeError(positiveTargets.hasError());
        }
    }
    testTargetsBuilder.filter(testFilter);
    return testTargetsBuilder.build();
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) TargetPatternKey(com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternKey) ArrayList(java.util.ArrayList) TargetPatternSkyKeyOrException(com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternSkyKeyOrException) ValueOrException(com.google.devtools.build.skyframe.ValueOrException) SkyValue(com.google.devtools.build.skyframe.SkyValue) Target(com.google.devtools.build.lib.packages.Target) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) ResolvedTargets(com.google.devtools.build.lib.cmdline.ResolvedTargets)

Example 8 with ResolvedTargets

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

the class TargetPatternPhaseFunction method getTargetsToBuild.

/**
   * Interpret the command-line arguments.
   *
   * @param options the command-line arguments in structured form
   */
private static ResolvedTargets<Target> getTargetsToBuild(Environment env, TargetPatternList options) throws InterruptedException {
    List<SkyKey> patternSkyKeys = new ArrayList<>();
    for (TargetPatternSkyKeyOrException keyOrException : TargetPatternValue.keys(options.getTargetPatterns(), FilteringPolicies.FILTER_MANUAL, options.getOffset())) {
        try {
            patternSkyKeys.add(keyOrException.getSkyKey());
        } catch (TargetParsingException e) {
        // Skip.
        }
    }
    Map<SkyKey, ValueOrException<TargetParsingException>> resolvedPatterns = env.getValuesOrThrow(patternSkyKeys, TargetParsingException.class);
    if (env.valuesMissing()) {
        return null;
    }
    ResolvedTargets.Builder<Target> builder = ResolvedTargets.builder();
    for (SkyKey key : patternSkyKeys) {
        TargetPatternKey pattern = (TargetPatternKey) key.argument();
        TargetPatternValue value;
        try {
            value = (TargetPatternValue) resolvedPatterns.get(key).get();
        } catch (TargetParsingException e) {
            // TODO(ulfjack): Report to EventBus.
            String rawPattern = pattern.getPattern();
            String errorMessage = e.getMessage();
            env.getListener().handle(Event.error("Skipping '" + rawPattern + "': " + errorMessage));
            builder.setError();
            continue;
        }
        // TODO(ulfjack): This is terribly inefficient.
        ResolvedTargets<Target> asTargets = TestSuiteExpansionFunction.labelsToTargets(env, value.getTargets().getTargets(), value.getTargets().hasError());
        if (pattern.isNegative()) {
            builder.filter(Predicates.not(Predicates.in(asTargets.getTargets())));
        } else {
            builder.merge(asTargets);
        }
    }
    ResolvedTargets<Target> result = builder.filter(TargetUtils.tagFilter(options.getBuildTargetFilter())).build();
    if (options.getCompileOneDependency()) {
        TargetProvider targetProvider = new EnvironmentBackedRecursivePackageProvider(env);
        try {
            return new CompileOneDependencyTransformer(targetProvider).transformCompileOneDependency(env.getListener(), result);
        } catch (MissingDepException e) {
            return null;
        } catch (TargetParsingException e) {
            env.getListener().handle(Event.error(e.getMessage()));
            return ResolvedTargets.failed();
        }
    }
    return result;
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) TargetPatternKey(com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternKey) TargetProvider(com.google.devtools.build.lib.pkgcache.TargetProvider) ArrayList(java.util.ArrayList) TargetPatternSkyKeyOrException(com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternSkyKeyOrException) ValueOrException(com.google.devtools.build.skyframe.ValueOrException) CompileOneDependencyTransformer(com.google.devtools.build.lib.pkgcache.CompileOneDependencyTransformer) Target(com.google.devtools.build.lib.packages.Target) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) ResolvedTargets(com.google.devtools.build.lib.cmdline.ResolvedTargets) MissingDepException(com.google.devtools.build.lib.skyframe.EnvironmentBackedRecursivePackageProvider.MissingDepException)

Example 9 with ResolvedTargets

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

the class RecursivePackageProviderBackedTargetPatternResolver method bulkGetTargetsInPackage.

private Map<PackageIdentifier, ResolvedTargets<Target>> bulkGetTargetsInPackage(String originalPattern, Iterable<PackageIdentifier> pkgIds, FilteringPolicy policy) throws InterruptedException {
    try {
        Map<PackageIdentifier, Package> pkgs = bulkGetPackages(pkgIds);
        if (pkgs.size() != Iterables.size(pkgIds)) {
            throw new IllegalStateException("Bulk package retrieval missing results: " + Sets.difference(ImmutableSet.copyOf(pkgIds), pkgs.keySet()));
        }
        ImmutableMap.Builder<PackageIdentifier, ResolvedTargets<Target>> result = ImmutableMap.builder();
        for (PackageIdentifier pkgId : pkgIds) {
            Package pkg = pkgs.get(pkgId);
            result.put(pkgId, TargetPatternResolverUtil.resolvePackageTargets(pkg, policy));
        }
        return result.build();
    } catch (NoSuchThingException e) {
        String message = TargetPatternResolverUtil.getParsingErrorMessage(e.getMessage(), originalPattern);
        throw new IllegalStateException("Mismatch: Expected given pkgIds to correspond to valid Packages. " + message, e);
    }
}
Also used : NoSuchThingException(com.google.devtools.build.lib.packages.NoSuchThingException) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) ResolvedTargets(com.google.devtools.build.lib.cmdline.ResolvedTargets) Package(com.google.devtools.build.lib.packages.Package) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

ResolvedTargets (com.google.devtools.build.lib.cmdline.ResolvedTargets)9 Target (com.google.devtools.build.lib.packages.Target)8 Label (com.google.devtools.build.lib.cmdline.Label)4 TargetParsingException (com.google.devtools.build.lib.cmdline.TargetParsingException)4 SkyKey (com.google.devtools.build.skyframe.SkyKey)4 ArrayList (java.util.ArrayList)4 PackageIdentifier (com.google.devtools.build.lib.cmdline.PackageIdentifier)3 SkyValue (com.google.devtools.build.skyframe.SkyValue)3 Package (com.google.devtools.build.lib.packages.Package)2 MissingDepException (com.google.devtools.build.lib.skyframe.EnvironmentBackedRecursivePackageProvider.MissingDepException)2 TargetPatternKey (com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternKey)2 TargetPatternSkyKeyOrException (com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternSkyKeyOrException)2 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)2 ValueOrException (com.google.devtools.build.skyframe.ValueOrException)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 TargetPattern (com.google.devtools.build.lib.cmdline.TargetPattern)1 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)1 NoSuchTargetException (com.google.devtools.build.lib.packages.NoSuchTargetException)1