Search in sources :

Example 1 with Value

use of org.immutables.value.Value in project buck by facebook.

the class Build method executeBuild.

/**
   * If {@code isKeepGoing} is false, then this returns a future that succeeds only if all of
   * {@code rulesToBuild} build successfully. Otherwise, this returns a future that should always
   * succeed, even if individual rules fail to build. In that case, a failed build rule is indicated
   * by a {@code null} value in the corresponding position in the iteration order of
   * {@code rulesToBuild}.
   * @param targetish The targets to build. All targets in this iterable must be unique.
   */
@SuppressWarnings("PMD.EmptyCatchBlock")
public BuildExecutionResult executeBuild(Iterable<? extends BuildTarget> targetish, boolean isKeepGoing) throws IOException, ExecutionException, InterruptedException {
    BuildId buildId = executionContext.getBuildId();
    BuildEngineBuildContext buildContext = BuildEngineBuildContext.builder().setBuildContext(BuildContext.builder().setActionGraph(actionGraph).setSourcePathResolver(new SourcePathResolver(new SourcePathRuleFinder(ruleResolver))).setJavaPackageFinder(javaPackageFinder).setEventBus(executionContext.getBuckEventBus()).setAndroidPlatformTargetSupplier(executionContext.getAndroidPlatformTargetSupplier()).build()).setClock(clock).setArtifactCache(artifactCache).setBuildId(buildId).setObjectMapper(objectMapper).putAllEnvironment(executionContext.getEnvironment()).setKeepGoing(isKeepGoing).build();
    // It is important to use this logic to determine the set of rules to build rather than
    // build.getActionGraph().getNodesWithNoIncomingEdges() because, due to graph enhancement,
    // there could be disconnected subgraphs in the DependencyGraph that we do not want to build.
    ImmutableSet<BuildTarget> targetsToBuild = StreamSupport.stream(targetish.spliterator(), false).collect(MoreCollectors.toImmutableSet());
    // It is important to use this logic to determine the set of rules to build rather than
    // build.getActionGraph().getNodesWithNoIncomingEdges() because, due to graph enhancement,
    // there could be disconnected subgraphs in the DependencyGraph that we do not want to build.
    ImmutableList<BuildRule> rulesToBuild = ImmutableList.copyOf(targetsToBuild.stream().map(buildTarget -> {
        try {
            return getRuleResolver().requireRule(buildTarget);
        } catch (NoSuchBuildTargetException e) {
            throw new HumanReadableException("No build rule found for target %s", buildTarget);
        }
    }).collect(MoreCollectors.toImmutableSet()));
    // Calculate and post the number of rules that need to built.
    int numRules = buildEngine.getNumRulesToBuild(rulesToBuild);
    getExecutionContext().getBuckEventBus().post(BuildEvent.ruleCountCalculated(targetsToBuild, numRules));
    // Setup symlinks required when configuring the output path.
    createConfiguredBuckOutSymlinks();
    List<ListenableFuture<BuildResult>> futures = rulesToBuild.stream().map(rule -> buildEngine.build(buildContext, executionContext, rule)).collect(MoreCollectors.toImmutableList());
    // Get the Future representing the build and then block until everything is built.
    ListenableFuture<List<BuildResult>> buildFuture = Futures.allAsList(futures);
    List<BuildResult> results;
    try {
        results = buildFuture.get();
        if (!isKeepGoing) {
            for (BuildResult result : results) {
                Throwable thrown = result.getFailure();
                if (thrown != null) {
                    throw new ExecutionException(thrown);
                }
            }
        }
    } catch (ExecutionException | InterruptedException | RuntimeException e) {
        Throwable t = Throwables.getRootCause(e);
        if (e instanceof InterruptedException || t instanceof InterruptedException || t instanceof ClosedByInterruptException) {
            try {
                buildFuture.cancel(true);
            } catch (CancellationException ignored) {
            // Rethrow original InterruptedException instead.
            }
            Thread.currentThread().interrupt();
        }
        throw e;
    }
    // Insertion order matters
    LinkedHashMap<BuildRule, Optional<BuildResult>> resultBuilder = new LinkedHashMap<>();
    Preconditions.checkState(rulesToBuild.size() == results.size());
    for (int i = 0, len = rulesToBuild.size(); i < len; i++) {
        BuildRule rule = rulesToBuild.get(i);
        resultBuilder.put(rule, Optional.ofNullable(results.get(i)));
    }
    return BuildExecutionResult.builder().setFailures(FluentIterable.from(results).filter(input -> input.getSuccess() == null)).setResults(resultBuilder).build();
}
Also used : ArtifactCache(com.facebook.buck.artifact_cache.ArtifactCache) ActionGraph(com.facebook.buck.rules.ActionGraph) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) AdbOptions(com.facebook.buck.step.AdbOptions) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) BuckConfig(com.facebook.buck.cli.BuckConfig) BuildId(com.facebook.buck.model.BuildId) WorkerProcessPool(com.facebook.buck.shell.WorkerProcessPool) FluentIterable(com.google.common.collect.FluentIterable) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) Map(java.util.Map) Clock(com.facebook.buck.timing.Clock) ConcurrencyLimit(com.facebook.buck.util.concurrent.ConcurrencyLimit) Cell(com.facebook.buck.rules.Cell) Path(java.nio.file.Path) JavaPackageFinder(com.facebook.buck.jvm.core.JavaPackageFinder) AndroidPlatformTarget(com.facebook.buck.android.AndroidPlatformTarget) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) CancellationException(java.util.concurrent.CancellationException) Platform(com.facebook.buck.util.environment.Platform) BuckPaths(com.facebook.buck.io.BuckPaths) BuildTarget(com.facebook.buck.model.BuildTarget) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) List(java.util.List) BuildEngineBuildContext(com.facebook.buck.rules.BuildEngineBuildContext) StepFailedException(com.facebook.buck.step.StepFailedException) ExceptionWithHumanReadableMessage(com.facebook.buck.util.ExceptionWithHumanReadableMessage) ExecutorPool(com.facebook.buck.step.ExecutorPool) Optional(java.util.Optional) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) BuckEventBus(com.facebook.buck.event.BuckEventBus) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Supplier(com.google.common.base.Supplier) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) ConcurrentMap(java.util.concurrent.ConcurrentMap) BuildRule(com.facebook.buck.rules.BuildRule) ExecutionContext(com.facebook.buck.step.ExecutionContext) LinkedHashMap(java.util.LinkedHashMap) ImmutableList(com.google.common.collect.ImmutableList) ThrowableConsoleEvent(com.facebook.buck.event.ThrowableConsoleEvent) Files(com.google.common.io.Files) Value(org.immutables.value.Value) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) StreamSupport(java.util.stream.StreamSupport) MoreCollectors(com.facebook.buck.util.MoreCollectors) Logger(com.facebook.buck.log.Logger) Charsets(com.google.common.base.Charsets) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TargetDeviceOptions(com.facebook.buck.step.TargetDeviceOptions) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) Console(com.facebook.buck.util.Console) HumanReadableException(com.facebook.buck.util.HumanReadableException) BuckStyleImmutable(com.facebook.buck.util.immutables.BuckStyleImmutable) TargetDevice(com.facebook.buck.step.TargetDevice) BuildResult(com.facebook.buck.rules.BuildResult) ExecutionException(java.util.concurrent.ExecutionException) Futures(com.google.common.util.concurrent.Futures) BuildEvent(com.facebook.buck.rules.BuildEvent) BuildEngine(com.facebook.buck.rules.BuildEngine) Closeable(java.io.Closeable) BuildContext(com.facebook.buck.rules.BuildContext) Preconditions(com.google.common.base.Preconditions) LinkedHashMap(java.util.LinkedHashMap) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) BuildTarget(com.facebook.buck.model.BuildTarget) BuildEngineBuildContext(com.facebook.buck.rules.BuildEngineBuildContext) BuildRule(com.facebook.buck.rules.BuildRule) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ExecutionException(java.util.concurrent.ExecutionException) Optional(java.util.Optional) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildResult(com.facebook.buck.rules.BuildResult) BuildId(com.facebook.buck.model.BuildId) CancellationException(java.util.concurrent.CancellationException) HumanReadableException(com.facebook.buck.util.HumanReadableException) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) ListenableFuture(com.google.common.util.concurrent.ListenableFuture)

Example 2 with Value

use of org.immutables.value.Value in project buck by facebook.

the class ProjectBuildFileParser method init.

/**
   * Initialize the parser, starting buck.py.
   */
private void init() throws IOException {
    projectBuildFileParseEventStarted = new ProjectBuildFileParseEvents.Started();
    buckEventBus.post(projectBuildFileParseEventStarted);
    try (SimplePerfEvent.Scope scope = SimplePerfEvent.scope(buckEventBus, PerfEventId.of("ParserInit"))) {
        ImmutableMap.Builder<String, String> pythonEnvironmentBuilder = ImmutableMap.builder();
        // Strip out PYTHONPATH. buck.py manually sets this to include only nailgun. We don't want
        // to inject nailgun into the parser's PYTHONPATH, so strip that value out.
        // If we wanted to pass on some environmental PYTHONPATH, we would have to do some actual
        // merging of this and the BuckConfig's python module search path.
        pythonEnvironmentBuilder.putAll(Maps.filterKeys(environment, k -> !PYTHONPATH_ENV_VAR_NAME.equals(k)));
        if (options.getPythonModuleSearchPath().isPresent()) {
            pythonEnvironmentBuilder.put(PYTHONPATH_ENV_VAR_NAME, options.getPythonModuleSearchPath().get());
        }
        ImmutableMap<String, String> pythonEnvironment = pythonEnvironmentBuilder.build();
        ProcessExecutorParams params = ProcessExecutorParams.builder().setCommand(buildArgs()).setEnvironment(pythonEnvironment).build();
        LOG.debug("Starting buck.py command: %s environment: %s", params.getCommand(), params.getEnvironment());
        buckPyProcess = processExecutor.launchProcess(params);
        LOG.debug("Started process %s successfully", buckPyProcess);
        OutputStream stdin = buckPyProcess.getOutputStream();
        InputStream stderr = buckPyProcess.getErrorStream();
        InputStreamConsumer stderrConsumer = new InputStreamConsumer(stderr, (InputStreamConsumer.Handler) line -> buckEventBus.post(ConsoleEvent.warning("Warning raised by BUCK file parser: %s", line)));
        stderrConsumerTerminationFuture = new FutureTask<>(stderrConsumer);
        stderrConsumerThread = Threads.namedThread(ProjectBuildFileParser.class.getSimpleName(), stderrConsumerTerminationFuture);
        stderrConsumerThread.start();
        buckPyStdinWriter = new BufferedOutputStream(stdin);
    }
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) ProjectWatch(com.facebook.buck.io.ProjectWatch) PerfEventId(com.facebook.buck.event.PerfEventId) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent) Supplier(com.google.common.base.Supplier) FutureTask(java.util.concurrent.FutureTask) WatchmanDiagnosticEvent(com.facebook.buck.io.WatchmanDiagnosticEvent) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) BufferedOutputStream(java.io.BufferedOutputStream) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) Value(org.immutables.value.Value) BserSerializer(com.facebook.buck.bser.BserSerializer) Map(java.util.Map) BserDeserializer(com.facebook.buck.bser.BserDeserializer) Suppliers(com.google.common.base.Suppliers) BuckStyleTuple(com.facebook.buck.util.immutables.BuckStyleTuple) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) MoreCollectors(com.facebook.buck.util.MoreCollectors) OutputStream(java.io.OutputStream) Logger(com.facebook.buck.log.Logger) WatchmanDiagnostic(com.facebook.buck.io.WatchmanDiagnostic) MoreThrowables(com.facebook.buck.util.MoreThrowables) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Files(java.nio.file.Files) Threads(com.facebook.buck.util.Threads) IOException(java.io.IOException) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) PathOrGlobMatcher(com.facebook.buck.io.PathOrGlobMatcher) Maps(com.google.common.collect.Maps) ExecutionException(java.util.concurrent.ExecutionException) ConstructorArgMarshaller(com.facebook.buck.rules.ConstructorArgMarshaller) List(java.util.List) Paths(java.nio.file.Paths) AssertScopeExclusiveAccess(com.facebook.buck.util.concurrent.AssertScopeExclusiveAccess) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) InputStreamConsumer(com.facebook.buck.util.InputStreamConsumer) Collections(java.util.Collections) Description(com.facebook.buck.rules.Description) InputStream(java.io.InputStream) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) InputStreamConsumer(com.facebook.buck.util.InputStreamConsumer) InputStream(java.io.InputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) ImmutableMap(com.google.common.collect.ImmutableMap) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

BuckEventBus (com.facebook.buck.event.BuckEventBus)2 ConsoleEvent (com.facebook.buck.event.ConsoleEvent)2 Logger (com.facebook.buck.log.Logger)2 MoreCollectors (com.facebook.buck.util.MoreCollectors)2 Preconditions (com.google.common.base.Preconditions)2 Supplier (com.google.common.base.Supplier)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 ExecutionException (java.util.concurrent.ExecutionException)2 Value (org.immutables.value.Value)2 AndroidPlatformTarget (com.facebook.buck.android.AndroidPlatformTarget)1 ArtifactCache (com.facebook.buck.artifact_cache.ArtifactCache)1 BserDeserializer (com.facebook.buck.bser.BserDeserializer)1 BserSerializer (com.facebook.buck.bser.BserSerializer)1