Search in sources :

Example 1 with TargetParsingCompleteEvent

use of com.google.devtools.build.lib.pkgcache.TargetParsingCompleteEvent in project bazel by bazelbuild.

the class LegacyLoadingPhaseRunner method execute.

/**
   * Performs target pattern evaluation, test suite expansion (if requested), and loads the
   * transitive closure of the resulting targets as well as of the targets needed to use the given
   * build configuration provider.
   */
@Override
public LoadingResult execute(ExtendedEventHandler eventHandler, List<String> targetPatterns, PathFragment relativeWorkingDirectory, LoadingOptions options, boolean keepGoing, boolean determineTests, @Nullable LoadingCallback callback) throws TargetParsingException, LoadingFailedException, InterruptedException {
    LOG.info("Starting pattern evaluation");
    Stopwatch timer = Stopwatch.createStarted();
    if (options.buildTestsOnly && options.compileOneDependency) {
        throw new LoadingFailedException("--compile_one_dependency cannot be used together with " + "the --build_tests_only option or the 'bazel test' command ");
    }
    targetPatternEvaluator.updateOffset(relativeWorkingDirectory);
    ExtendedEventHandler parseFailureListener = new ParseFailureListenerImpl(eventHandler);
    // Determine targets to build:
    ResolvedTargets<Target> targets = getTargetsToBuild(parseFailureListener, targetPatterns, options.compileOneDependency, options.buildTagFilterList, keepGoing);
    ImmutableSet<Target> filteredTargets = targets.getFilteredTargets();
    boolean buildTestsOnly = options.buildTestsOnly;
    ImmutableSet<Target> testsToRun = null;
    ImmutableSet<Target> testFilteredTargets = ImmutableSet.of();
    // then the list of filtered targets will be set as build list as well.
    if (determineTests || buildTestsOnly) {
        // Parse the targets to get the tests.
        ResolvedTargets<Target> testTargets = determineTests(parseFailureListener, targetPatterns, options, keepGoing);
        if (testTargets.getTargets().isEmpty() && !testTargets.getFilteredTargets().isEmpty()) {
            eventHandler.handle(Event.warn("All specified test targets were excluded by filters"));
        }
        if (buildTestsOnly) {
            // 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<Target> 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.<Target>builder().merge(testTargets).mergeError(targets.hasError()).build();
            if (determineTests) {
                testsToRun = testTargets.getTargets();
            }
        } else /*if (determineTests)*/
        {
            testsToRun = testTargets.getTargets();
            targets = ResolvedTargets.<Target>builder().merge(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.
            Preconditions.checkState(targets.getTargets().containsAll(testsToRun));
        }
    }
    if (targets.hasError()) {
        eventHandler.handle(Event.warn("Target pattern parsing failed. Continuing anyway"));
    }
    LoadingPhaseRunner.maybeReportDeprecation(eventHandler, targets.getTargets());
    long targetPatternEvalTime = timer.stop().elapsed(TimeUnit.MILLISECONDS);
    LOG.info("Starting test suite expansion");
    timer = Stopwatch.createStarted();
    ImmutableSet<Target> targetsToLoad = targets.getTargets();
    ResolvedTargets<Target> expandedResult;
    try {
        expandedResult = expandTestSuites(eventHandler, targetsToLoad, keepGoing);
    } catch (TargetParsingException e) {
        throw new LoadingFailedException("Loading failed; build aborted", e);
    }
    ImmutableSet<Target> expandedTargetsToLoad = expandedResult.getTargets();
    ImmutableSet<Target> testSuiteTargets = ImmutableSet.copyOf(Sets.difference(targetsToLoad, expandedTargetsToLoad));
    long testSuiteTime = timer.stop().elapsed(TimeUnit.MILLISECONDS);
    TargetPatternPhaseValue patternParsingValue = new TargetPatternPhaseValue(expandedTargetsToLoad, testsToRun, targets.hasError(), expandedResult.hasError(), filteredTargets, testFilteredTargets, /*originalTargets=*/
    targets.getTargets(), testSuiteTargets, getWorkspaceName(eventHandler));
    // This is the same code as SkyframeLoadingPhaseRunner.
    eventHandler.post(new TargetParsingCompleteEvent(patternParsingValue.getOriginalTargets(), patternParsingValue.getFilteredTargets(), patternParsingValue.getTestFilteredTargets(), targetPatternEvalTime, targetPatterns, patternParsingValue.getTargets()));
    if (callback != null) {
        callback.notifyTargets(patternParsingValue.getTargets());
    }
    eventHandler.post(new LoadingPhaseCompleteEvent(patternParsingValue.getTargets(), patternParsingValue.getTestSuiteTargets(), packageManager.getStatistics(), testSuiteTime));
    LOG.info("Target pattern evaluation finished");
    return patternParsingValue.toLoadingResult();
}
Also used : LoadingPhaseCompleteEvent(com.google.devtools.build.lib.pkgcache.LoadingPhaseCompleteEvent) Stopwatch(com.google.common.base.Stopwatch) ExtendedEventHandler(com.google.devtools.build.lib.events.ExtendedEventHandler) TargetParsingCompleteEvent(com.google.devtools.build.lib.pkgcache.TargetParsingCompleteEvent) Target(com.google.devtools.build.lib.packages.Target) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) LoadingFailedException(com.google.devtools.build.lib.pkgcache.LoadingFailedException) HashSet(java.util.HashSet)

Aggregations

Stopwatch (com.google.common.base.Stopwatch)1 TargetParsingException (com.google.devtools.build.lib.cmdline.TargetParsingException)1 ExtendedEventHandler (com.google.devtools.build.lib.events.ExtendedEventHandler)1 Target (com.google.devtools.build.lib.packages.Target)1 LoadingFailedException (com.google.devtools.build.lib.pkgcache.LoadingFailedException)1 LoadingPhaseCompleteEvent (com.google.devtools.build.lib.pkgcache.LoadingPhaseCompleteEvent)1 TargetParsingCompleteEvent (com.google.devtools.build.lib.pkgcache.TargetParsingCompleteEvent)1 HashSet (java.util.HashSet)1