Search in sources :

Example 1 with LineProcessorRunnable

use of com.facebook.buck.util.LineProcessorRunnable in project buck by facebook.

the class CxxPreprocessAndCompileStep method executeCompilation.

private int executeCompilation(ExecutionContext context) throws Exception {
    ProcessExecutorParams.Builder builder = makeSubprocessBuilder(context, ImmutableMap.of());
    if (useArgfile) {
        filesystem.writeLinesToPath(Iterables.transform(getArguments(context.getAnsi().isAnsiTerminal()), Escaper.ARGFILE_ESCAPER), getArgfile());
        builder.setCommand(ImmutableList.<String>builder().addAll(getCommandPrefix()).add("@" + getArgfile()).build());
    } else {
        builder.setCommand(ImmutableList.<String>builder().addAll(getCommandPrefix()).addAll(getArguments(context.getAnsi().isAnsiTerminal())).build());
    }
    ProcessExecutorParams params = builder.build();
    LOG.debug("Running command (pwd=%s): %s", params.getDirectory(), getDescription(context));
    // Start the process.
    ProcessExecutor executor = new DefaultProcessExecutor(Console.createNullConsole());
    ProcessExecutor.LaunchedProcess process = executor.launchProcess(params);
    // We buffer error messages in memory, as these are typically small.
    ByteArrayOutputStream error = new ByteArrayOutputStream();
    // Fire up managed threads to process the stdout and stderr lines.
    int exitCode;
    try {
        try (LineProcessorRunnable errorProcessor = createErrorTransformerFactory(context).createTransformerThread(context, compiler.getErrorStream(process), error)) {
            errorProcessor.start();
            errorProcessor.waitFor();
        } catch (Throwable thrown) {
            executor.destroyLaunchedProcess(process);
            throw thrown;
        }
        exitCode = executor.waitForLaunchedProcess(process).getExitCode();
    } finally {
        executor.destroyLaunchedProcess(process);
        executor.waitForLaunchedProcess(process);
    }
    // If we generated any error output, print that to the console.
    String err = new String(error.toByteArray());
    if (!err.isEmpty()) {
        context.getBuckEventBus().post(createConsoleEvent(context, preprocessorCommand.map(Optional::of).orElse(compilerCommand).get().supportsColorsInDiagnostics(), exitCode == 0 ? Level.WARNING : Level.SEVERE, err));
    }
    return exitCode;
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) LineProcessorRunnable(com.facebook.buck.util.LineProcessorRunnable)

Aggregations

DefaultProcessExecutor (com.facebook.buck.util.DefaultProcessExecutor)1 LineProcessorRunnable (com.facebook.buck.util.LineProcessorRunnable)1 ProcessExecutor (com.facebook.buck.util.ProcessExecutor)1 ProcessExecutorParams (com.facebook.buck.util.ProcessExecutorParams)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1