Search in sources :

Example 1 with SkyframeExecutor

use of com.google.devtools.build.lib.skyframe.SkyframeExecutor in project bazel by bazelbuild.

the class CommandEnvironment method beforeCommand.

/**
   * Hook method called by the BlazeCommandDispatcher prior to the dispatch of
   * each command.
   *
   * @param options The CommonCommandOptions used by every command.
   * @throws AbruptExitException if this command is unsuitable to be run as specified
   */
void beforeCommand(Command command, OptionsParser optionsParser, CommonCommandOptions options, long execStartTimeNanos, long waitTimeInMs) throws AbruptExitException {
    commandStartTime -= options.startupTime;
    if (runtime.getStartupOptionsProvider().getOptions(BlazeServerStartupOptions.class).watchFS) {
        try {
            // TODO(ulfjack): Get rid of the startup option and drop this code.
            optionsParser.parse("--watchfs");
        } catch (OptionsParsingException e) {
            // This should never happen.
            throw new IllegalStateException(e);
        }
    }
    this.commandName = command.name();
    this.options = optionsParser;
    eventBus.post(new GotOptionsEvent(runtime.getStartupOptionsProvider(), optionsParser));
    throwPendingException();
    outputService = null;
    BlazeModule outputModule = null;
    if (command.builds()) {
        for (BlazeModule module : runtime.getBlazeModules()) {
            OutputService moduleService = module.getOutputService();
            if (moduleService != null) {
                if (outputService != null) {
                    throw new IllegalStateException(String.format("More than one module (%s and %s) returns an output service", module.getClass(), outputModule.getClass()));
                }
                outputService = moduleService;
                outputModule = module;
            }
        }
    }
    SkyframeExecutor skyframeExecutor = getSkyframeExecutor();
    skyframeExecutor.setOutputService(outputService);
    // Ensure that the working directory will be under the workspace directory.
    Path workspace = getWorkspace();
    Path workingDirectory;
    if (inWorkspace()) {
        workingDirectory = workspace.getRelative(options.clientCwd);
    } else {
        workspace = FileSystemUtils.getWorkingDirectory(getDirectories().getFileSystem());
        workingDirectory = workspace;
    }
    this.relativeWorkingDirectory = workingDirectory.relativeTo(workspace);
    this.workingDirectory = workingDirectory;
    updateClientEnv(options.clientEnv);
    // Fail fast in the case where a Blaze command forgets to install the package path correctly.
    skyframeExecutor.setActive(false);
    // Let skyframe figure out if it needs to store graph edges for this build.
    skyframeExecutor.decideKeepIncrementalState(runtime.getStartupOptionsProvider().getOptions(BlazeServerStartupOptions.class).batch, optionsParser.getOptions(BuildView.Options.class));
    // Start the performance and memory profilers.
    runtime.beforeCommand(this, options, execStartTimeNanos);
    // actionClientEnv contains the environment where values from actionEnvironment are
    // overridden.
    actionClientEnv.clear();
    actionClientEnv.putAll(clientEnv);
    if (command.builds()) {
        Map<String, String> testEnv = new TreeMap<>();
        for (Map.Entry<String, String> entry : optionsParser.getOptions(BuildConfiguration.Options.class).testEnvironment) {
            testEnv.put(entry.getKey(), entry.getValue());
        }
        // for inheritence.
        for (Map.Entry<String, String> entry : optionsParser.getOptions(BuildConfiguration.Options.class).actionEnvironment) {
            if (entry.getValue() == null) {
                visibleClientEnv.add(entry.getKey());
            } else {
                visibleClientEnv.remove(entry.getKey());
                actionClientEnv.put(entry.getKey(), entry.getValue());
            }
        }
        try {
            for (Map.Entry<String, String> entry : testEnv.entrySet()) {
                if (entry.getValue() == null) {
                    String clientValue = clientEnv.get(entry.getKey());
                    if (clientValue != null) {
                        optionsParser.parse(OptionPriority.SOFTWARE_REQUIREMENT, "test environment variable from client environment", ImmutableList.of("--test_env=" + entry.getKey() + "=" + clientEnv.get(entry.getKey())));
                    }
                }
            }
        } catch (OptionsParsingException e) {
            throw new IllegalStateException(e);
        }
    }
    eventBus.post(new CommandStartEvent(command.name(), getCommandId(), getClientEnv(), workingDirectory, getDirectories(), waitTimeInMs + options.waitTime));
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) BuildOptions(com.google.devtools.build.lib.analysis.config.BuildOptions) PackageCacheOptions(com.google.devtools.build.lib.pkgcache.PackageCacheOptions) TreeMap(java.util.TreeMap) SkyframeExecutor(com.google.devtools.build.lib.skyframe.SkyframeExecutor) OutputService(com.google.devtools.build.lib.exec.OutputService) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 2 with SkyframeExecutor

use of com.google.devtools.build.lib.skyframe.SkyframeExecutor in project bazel by bazelbuild.

the class ExecutionTool method executeBuild.

/**
   * Performs the execution phase (phase 3) of the build, in which the Builder
   * is applied to the action graph to bring the targets up to date. (This
   * function will return prior to execution-proper if --nobuild was specified.)
   * @param buildId UUID of the build id
   * @param analysisResult the analysis phase output
   * @param buildResult the mutable build result
   * @param packageRoots package roots collected from loading phase and BuildConfigurationCollection
   * creation
   */
void executeBuild(UUID buildId, AnalysisResult analysisResult, BuildResult buildResult, BuildConfigurationCollection configurations, ImmutableMap<PackageIdentifier, Path> packageRoots, TopLevelArtifactContext topLevelArtifactContext) throws BuildFailedException, InterruptedException, TestExecException, AbruptExitException {
    Stopwatch timer = Stopwatch.createStarted();
    prepare(packageRoots, analysisResult.getWorkspaceName());
    ActionGraph actionGraph = analysisResult.getActionGraph();
    // Get top-level artifacts.
    ImmutableSet<Artifact> additionalArtifacts = analysisResult.getAdditionalArtifactsToBuild();
    OutputService outputService = env.getOutputService();
    ModifiedFileSet modifiedOutputFiles = ModifiedFileSet.EVERYTHING_MODIFIED;
    if (outputService != null) {
        modifiedOutputFiles = outputService.startBuild(buildId, request.getBuildOptions().finalizeActions);
    } else {
        // TODO(bazel-team): this could be just another OutputService
        startLocalOutputBuild(analysisResult.getWorkspaceName());
    }
    List<BuildConfiguration> targetConfigurations = configurations.getTargetConfigurations();
    BuildConfiguration targetConfiguration = targetConfigurations.size() == 1 ? targetConfigurations.get(0) : null;
    if (targetConfigurations.size() == 1) {
        String productName = runtime.getProductName();
        String dirName = env.getWorkspaceName();
        String workspaceName = analysisResult.getWorkspaceName();
        OutputDirectoryLinksUtils.createOutputDirectoryLinks(dirName, env.getWorkspace(), env.getDirectories().getExecRoot(workspaceName), env.getDirectories().getOutputPath(workspaceName), getReporter(), targetConfiguration, request.getBuildOptions().getSymlinkPrefix(productName), productName);
    }
    ActionCache actionCache = getActionCache();
    SkyframeExecutor skyframeExecutor = env.getSkyframeExecutor();
    Builder builder = createBuilder(request, actionCache, skyframeExecutor, modifiedOutputFiles);
    //
    // Execution proper.  All statements below are logically nested in
    // begin/end pairs.  No early returns or exceptions please!
    //
    Collection<ConfiguredTarget> configuredTargets = buildResult.getActualTargets();
    env.getEventBus().post(new ExecutionStartingEvent(configuredTargets));
    getReporter().handle(Event.progress("Building..."));
    // Conditionally record dependency-checker log:
    ExplanationHandler explanationHandler = installExplanationHandler(request.getBuildOptions().explanationPath, request.getOptionsDescription());
    Set<ConfiguredTarget> builtTargets = new HashSet<>();
    Collection<AspectValue> aspects = analysisResult.getAspects();
    Iterable<Artifact> allArtifactsForProviders = Iterables.concat(additionalArtifacts, TopLevelArtifactHelper.getAllArtifactsToBuild(analysisResult.getTargetsToBuild(), analysisResult.getTopLevelContext()).getAllArtifacts(), TopLevelArtifactHelper.getAllArtifactsToBuildFromAspects(aspects, analysisResult.getTopLevelContext()).getAllArtifacts(), //TODO(dslomov): Artifacts to test from aspects?
    TopLevelArtifactHelper.getAllArtifactsToTest(analysisResult.getTargetsToTest()));
    if (request.isRunningInEmacs()) {
        // The syntax of this message is tightly constrained by lisp/progmodes/compile.el in emacs
        request.getOutErr().printErrLn("blaze: Entering directory `" + getExecRoot() + "/'");
    }
    boolean buildCompleted = false;
    try {
        for (ActionContextProvider actionContextProvider : actionContextProviders) {
            actionContextProvider.executionPhaseStarting(actionGraph, allArtifactsForProviders);
        }
        executor.executionPhaseStarting();
        skyframeExecutor.drainChangedFiles();
        if (request.getViewOptions().discardAnalysisCache) {
            // Free memory by removing cache entries that aren't going to be needed. Note that in
            // skyframe full, this destroys the action graph as well, so we can only do it after the
            // action graph is no longer needed.
            env.getSkyframeBuildView().clearAnalysisCache(analysisResult.getTargetsToBuild());
        }
        configureResourceManager(request);
        Profiler.instance().markPhase(ProfilePhase.EXECUTE);
        builder.buildArtifacts(env.getReporter(), additionalArtifacts, analysisResult.getParallelTests(), analysisResult.getExclusiveTests(), analysisResult.getTargetsToBuild(), analysisResult.getAspects(), executor, builtTargets, request.getBuildOptions().explanationPath != null, env.getBlazeWorkspace().getLastExecutionTimeRange(), topLevelArtifactContext);
        buildCompleted = true;
    } catch (BuildFailedException | TestExecException e) {
        buildCompleted = true;
        throw e;
    } finally {
        env.recordLastExecutionTime();
        if (request.isRunningInEmacs()) {
            request.getOutErr().printErrLn("blaze: Leaving directory `" + getExecRoot() + "/'");
        }
        if (buildCompleted) {
            getReporter().handle(Event.progress("Building complete."));
        }
        env.getEventBus().post(new ExecutionFinishedEvent(ImmutableMap.<String, Long>of(), 0L, skyframeExecutor.getOutputDirtyFilesAndClear(), skyframeExecutor.getModifiedFilesDuringPreviousBuildAndClear()));
        executor.executionPhaseEnding();
        for (ActionContextProvider actionContextProvider : actionContextProviders) {
            actionContextProvider.executionPhaseEnding();
        }
        Profiler.instance().markPhase(ProfilePhase.FINISH);
        if (buildCompleted) {
            saveCaches(actionCache);
        }
        try (AutoProfiler p = AutoProfiler.profiled("Show results", ProfilerTask.INFO)) {
            buildResult.setSuccessfulTargets(determineSuccessfulTargets(configuredTargets, builtTargets, timer));
            BuildResultPrinter buildResultPrinter = new BuildResultPrinter(env);
            buildResultPrinter.showBuildResult(request, buildResult, configuredTargets, analysisResult.getAspects());
        }
        try (AutoProfiler p = AutoProfiler.profiled("Show artifacts", ProfilerTask.INFO)) {
            if (request.getBuildOptions().showArtifacts) {
                BuildResultPrinter buildResultPrinter = new BuildResultPrinter(env);
                buildResultPrinter.showArtifacts(request, configuredTargets, analysisResult.getAspects());
            }
        }
        if (explanationHandler != null) {
            uninstallExplanationHandler(explanationHandler);
        }
        // code has already run.
        if (env.getOutputService() != null) {
            boolean isBuildSuccessful = buildResult.getSuccessfulTargets().size() == configuredTargets.size();
            env.getOutputService().finalizeBuild(isBuildSuccessful);
        }
    }
}
Also used : Builder(com.google.devtools.build.lib.skyframe.Builder) ExecutorBuilder(com.google.devtools.build.lib.exec.ExecutorBuilder) Stopwatch(com.google.common.base.Stopwatch) SkyframeExecutor(com.google.devtools.build.lib.skyframe.SkyframeExecutor) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) BuildFailedException(com.google.devtools.build.lib.actions.BuildFailedException) ActionContextProvider(com.google.devtools.build.lib.exec.ActionContextProvider) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) AspectValue(com.google.devtools.build.lib.skyframe.AspectValue) ActionGraph(com.google.devtools.build.lib.actions.ActionGraph) AutoProfiler(com.google.devtools.build.lib.profiler.AutoProfiler) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) Artifact(com.google.devtools.build.lib.actions.Artifact) ExecutionStartingEvent(com.google.devtools.build.lib.buildtool.buildevent.ExecutionStartingEvent) ActionCache(com.google.devtools.build.lib.actions.cache.ActionCache) ModifiedFileSet(com.google.devtools.build.lib.vfs.ModifiedFileSet) OutputService(com.google.devtools.build.lib.exec.OutputService) TestExecException(com.google.devtools.build.lib.actions.TestExecException)

Example 3 with SkyframeExecutor

use of com.google.devtools.build.lib.skyframe.SkyframeExecutor in project bazel by bazelbuild.

the class CommandEnvironment method setupPackageCache.

/**
   * Initializes the package cache using the given options, and syncs the package cache. Also
   * injects a defaults package using the options for the {@link BuildConfiguration}.
   *
   * @see DefaultsPackage
   */
public void setupPackageCache(OptionsClassProvider options, String defaultsPackageContents) throws InterruptedException, AbruptExitException {
    SkyframeExecutor skyframeExecutor = getSkyframeExecutor();
    if (!skyframeExecutor.hasIncrementalState()) {
        skyframeExecutor.resetEvaluator();
    }
    skyframeExecutor.sync(reporter, options.getOptions(PackageCacheOptions.class), getOutputBase(), getWorkingDirectory(), defaultsPackageContents, getCommandId(), clientEnv, timestampGranularityMonitor, options);
}
Also used : PackageCacheOptions(com.google.devtools.build.lib.pkgcache.PackageCacheOptions) SkyframeExecutor(com.google.devtools.build.lib.skyframe.SkyframeExecutor)

Aggregations

SkyframeExecutor (com.google.devtools.build.lib.skyframe.SkyframeExecutor)3 OutputService (com.google.devtools.build.lib.exec.OutputService)2 PackageCacheOptions (com.google.devtools.build.lib.pkgcache.PackageCacheOptions)2 Stopwatch (com.google.common.base.Stopwatch)1 ActionGraph (com.google.devtools.build.lib.actions.ActionGraph)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 BuildFailedException (com.google.devtools.build.lib.actions.BuildFailedException)1 TestExecException (com.google.devtools.build.lib.actions.TestExecException)1 ActionCache (com.google.devtools.build.lib.actions.cache.ActionCache)1 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)1 BuildOptions (com.google.devtools.build.lib.analysis.config.BuildOptions)1 ExecutionStartingEvent (com.google.devtools.build.lib.buildtool.buildevent.ExecutionStartingEvent)1 ActionContextProvider (com.google.devtools.build.lib.exec.ActionContextProvider)1 ExecutorBuilder (com.google.devtools.build.lib.exec.ExecutorBuilder)1 AutoProfiler (com.google.devtools.build.lib.profiler.AutoProfiler)1 AspectValue (com.google.devtools.build.lib.skyframe.AspectValue)1 Builder (com.google.devtools.build.lib.skyframe.Builder)1 ModifiedFileSet (com.google.devtools.build.lib.vfs.ModifiedFileSet)1 Path (com.google.devtools.build.lib.vfs.Path)1