// Copyright 2015 The Bazel Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.google.devtools.build.lib.skyframe; import static com.google.common.collect.ImmutableSetMultimap.flatteningToImmutableSetMultimap; import com.google.auto.value.AutoValue; import com.google.common.base.Preconditions; import com.google.common.base.Predicates; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSetMultimap; import com.google.common.collect.Iterables; import com.google.common.collect.Sets; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.cmdline.ResolvedTargets; import com.google.devtools.build.lib.cmdline.TargetParsingException; import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.events.ExtendedEventHandler; import com.google.devtools.build.lib.packages.NoSuchPackageException; import com.google.devtools.build.lib.packages.NonconfigurableAttributeMapper; import com.google.devtools.build.lib.packages.Rule; import com.google.devtools.build.lib.packages.Target; import com.google.devtools.build.lib.packages.TargetUtils; import com.google.devtools.build.lib.pkgcache.AbstractRecursivePackageProvider.MissingDepException; import com.google.devtools.build.lib.pkgcache.CompileOneDependencyTransformer; import com.google.devtools.build.lib.pkgcache.FilteringPolicies; import com.google.devtools.build.lib.pkgcache.LoadingPhaseCompleteEvent; import com.google.devtools.build.lib.pkgcache.ParsingFailedEvent; import com.google.devtools.build.lib.pkgcache.TargetParsingCompleteEvent; import com.google.devtools.build.lib.pkgcache.TargetProvider; import com.google.devtools.build.lib.pkgcache.TestFilter; import com.google.devtools.build.lib.skyframe.TargetPatternPhaseValue.TargetPatternPhaseKey; import com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternKey; import com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternSkyKeyOrException; import com.google.devtools.build.lib.syntax.Type; import com.google.devtools.build.skyframe.SkyFunction; import com.google.devtools.build.skyframe.SkyKey; import com.google.devtools.build.skyframe.SkyValue; import com.google.devtools.build.skyframe.ValueOrException; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; import javax.annotation.Nullable; /** * Takes a list of target patterns corresponding to a command line and turns it into a set of * resolved Targets. */ final class TargetPatternPhaseFunction implements SkyFunction { public TargetPatternPhaseFunction() { } @Override public TargetPatternPhaseValue compute(SkyKey key, Environment env) throws InterruptedException { TargetPatternPhaseKey options = (TargetPatternPhaseKey) key.argument(); PackageValue packageValue = null; boolean workspaceError = false; try { packageValue = (PackageValue) env.getValueOrThrow( PackageValue.key(Label.EXTERNAL_PACKAGE_IDENTIFIER), NoSuchPackageException.class); } catch (NoSuchPackageException e) { env.getListener().handle(Event.error(e.getMessage())); workspaceError = true; } if (env.valuesMissing()) { return null; } String workspaceName = ""; if (!workspaceError) { workspaceName = packageValue.getPackage().getWorkspaceName(); } // Determine targets to build: List failedPatterns = new ArrayList(); List expandedPatterns = getTargetsToBuild(env, options, failedPatterns); ResolvedTargets targets = env.valuesMissing() ? null : mergeAll(expandedPatterns, !failedPatterns.isEmpty(), env, options); // If the --build_tests_only option was specified or we want to run tests, we need to determine // the list of targets to test. For that, we remove manual tests and apply the command-line // filters. Also, if --build_tests_only is specified, then the list of filtered targets will be // set as build list as well. ResolvedTargets testTargets = null; if (options.getDetermineTests() || options.getBuildTestsOnly()) { testTargets = determineTests(env, options.getTargetPatterns(), options.getOffset(), options.getTestFilter()); Preconditions.checkState(env.valuesMissing() || (testTargets != null)); } Map testExpansionKeys = new LinkedHashMap<>(); if (targets != null) { for (Target target : targets.getTargets()) { if (TargetUtils.isTestSuiteRule(target) && options.isExpandTestSuites()) { Label label = target.getLabel(); SkyKey testExpansionKey = TestSuiteExpansionValue.key(ImmutableSet.of(label)); testExpansionKeys.put(label, testExpansionKey); } } } Map expandedTests = env.getValues(testExpansionKeys.values()); if (env.valuesMissing()) { return null; } ImmutableSet filteredTargets = targets.getFilteredTargets(); ImmutableSet testsToRun = null; ImmutableSet testFilteredTargets = ImmutableSet.of(); if (testTargets != null) { // Parse the targets to get the tests. if (testTargets.getTargets().isEmpty() && !testTargets.getFilteredTargets().isEmpty()) { env.getListener().handle(Event.warn("All specified test targets were excluded by filters")); } if (options.getBuildTestsOnly()) { // Replace original targets to build with test targets, so that only targets that are // actually going to be built are loaded in the loading phase. Note that this has a side // effect that any test_suite target requested to be built is replaced by the set of *_test // targets it represents; for example, this affects the status and the summary reports. Set allFilteredTargets = new HashSet<>(); allFilteredTargets.addAll(targets.getTargets()); allFilteredTargets.addAll(targets.getFilteredTargets()); allFilteredTargets.removeAll(testTargets.getTargets()); allFilteredTargets.addAll(testTargets.getFilteredTargets()); testFilteredTargets = ImmutableSet.copyOf(allFilteredTargets); filteredTargets = ImmutableSet.of(); targets = ResolvedTargets.builder() .merge(testTargets) .mergeError(targets.hasError()) .build(); if (options.getDetermineTests()) { testsToRun = testTargets.getTargets(); } } else /*if (determineTests)*/ { testsToRun = testTargets.getTargets(); targets = ResolvedTargets.builder() .merge(targets) // Avoid merge() here which would remove the filteredTargets from the targets. .addAll(testsToRun) .mergeError(testTargets.hasError()) .build(); // filteredTargets is correct in this case - it cannot contain tests that got back in // through test_suite expansion, because the test determination would also filter those out. // However, that's not obvious, and it might be better to explicitly recompute it. } if (testsToRun != null) { // Note that testsToRun can still be null here, if buildTestsOnly && !shouldRunTests. if (!targets.getTargets().containsAll(testsToRun)) { throw new IllegalStateException(String.format( "Internal consistency check failed; some targets are scheduled for test execution " + "but not for building (%s)", Sets.difference(testsToRun, targets.getTargets()))); } } } if (targets.hasError()) { env.getListener().handle(Event.warn("Target pattern parsing failed.")); } maybeReportDeprecation(env.getListener(), targets.getTargets()); ResolvedTargets.Builder