use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.
the class TestCommand method runTestsExternal.
private int runTestsExternal(final CommandRunnerParams params, Build build, Iterable<String> command, Iterable<TestRule> testRules, SourcePathResolver pathResolver) throws InterruptedException, IOException {
TestRunningOptions options = getTestRunningOptions(params);
// Walk the test rules, collecting all the specs.
List<ExternalTestRunnerTestSpec> specs = Lists.newArrayList();
for (TestRule testRule : testRules) {
if (!(testRule instanceof ExternalTestRunnerRule)) {
params.getBuckEventBus().post(ConsoleEvent.severe(String.format("Test %s does not support external test running", testRule.getBuildTarget())));
return 1;
}
ExternalTestRunnerRule rule = (ExternalTestRunnerRule) testRule;
specs.add(rule.getExternalTestRunnerSpec(build.getExecutionContext(), options, pathResolver));
}
// Serialize the specs to a file to pass into the test runner.
Path infoFile = params.getCell().getFilesystem().resolve(params.getCell().getFilesystem().getBuckPaths().getScratchDir()).resolve("external_runner_specs.json");
Files.createDirectories(infoFile.getParent());
Files.deleteIfExists(infoFile);
params.getObjectMapper().writerWithDefaultPrettyPrinter().writeValue(infoFile.toFile(), specs);
// Launch and run the external test runner, forwarding it's stdout/stderr to the console.
// We wait for it to complete then returns its error code.
ListeningProcessExecutor processExecutor = new ListeningProcessExecutor();
ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().addAllCommand(command).addAllCommand(withDashArguments).setEnvironment(params.getEnvironment()).addCommand("--buck-test-info", infoFile.toString()).addCommand("--jobs", String.valueOf(getConcurrencyLimit(params.getBuckConfig()).threadLimit)).setDirectory(params.getCell().getFilesystem().getRootPath()).build();
ForwardingProcessListener processListener = new ForwardingProcessListener(Channels.newChannel(params.getConsole().getStdOut()), Channels.newChannel(params.getConsole().getStdErr()));
ListeningProcessExecutor.LaunchedProcess process = processExecutor.launchProcess(processExecutorParams, processListener);
try {
return processExecutor.waitForProcess(process);
} finally {
processExecutor.destroyProcess(process, /* force */
false);
processExecutor.waitForProcess(process);
}
}
use of com.facebook.buck.util.ProcessExecutorParams 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;
}
use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.
the class ArchiveStep method runArchiver.
private ProcessExecutor.Result runArchiver(ExecutionContext context, final ImmutableList<String> command) throws IOException, InterruptedException {
Map<String, String> env = new HashMap<>(context.getEnvironment());
env.putAll(environment);
ProcessExecutorParams params = ProcessExecutorParams.builder().setDirectory(filesystem.getRootPath()).setEnvironment(ImmutableMap.copyOf(env)).setCommand(command).build();
ProcessExecutor.Result result = context.getProcessExecutor().launchAndExecute(params);
if (result.getExitCode() != 0 && result.getStderr().isPresent()) {
context.getBuckEventBus().post(ConsoleEvent.create(Level.SEVERE, result.getStderr().get()));
}
return result;
}
use of com.facebook.buck.util.ProcessExecutorParams 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);
}
}
use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.
the class ExternalJavac method buildWithClasspath.
@Override
public int buildWithClasspath(JavacExecutionContext context, BuildTarget invokingRule, ImmutableList<String> options, ImmutableList<ResolvedJavacPluginProperties> annotationProcessors, ImmutableSortedSet<Path> javaSourceFilePaths, Path pathToSrcsList, Optional<Path> workingDirectory, JavacOptions.AbiGenerationMode abiGenerationMode) throws InterruptedException {
Preconditions.checkArgument(abiGenerationMode == AbstractJavacOptions.AbiGenerationMode.CLASS, "Source ABI verification requires JSR199");
ImmutableList.Builder<String> command = ImmutableList.builder();
command.add(pathToJavac.isLeft() ? pathToJavac.getLeft().toString() : context.getAbsolutePathsForInputs().get(0).toString());
command.addAll(options);
ImmutableList<Path> expandedSources;
try {
expandedSources = getExpandedSourcePaths(context.getProjectFilesystem(), invokingRule, javaSourceFilePaths, workingDirectory);
} catch (IOException e) {
throw new HumanReadableException("Unable to expand sources for %s into %s", invokingRule, workingDirectory);
}
try {
context.getProjectFilesystem().writeLinesToPath(FluentIterable.from(expandedSources).transform(Object::toString).transform(ARGFILES_ESCAPER), pathToSrcsList);
command.add("@" + pathToSrcsList);
} catch (IOException e) {
context.getEventSink().reportThrowable(e, "Cannot write list of .java files to compile to %s file! Terminating compilation.", pathToSrcsList);
return 1;
}
// Run the command
int exitCode = -1;
try {
ProcessExecutorParams params = ProcessExecutorParams.builder().setCommand(command.build()).setEnvironment(context.getEnvironment()).setDirectory(context.getProjectFilesystem().getRootPath().toAbsolutePath()).build();
ProcessExecutor.Result result = context.getProcessExecutor().launchAndExecute(params);
exitCode = result.getExitCode();
} catch (IOException e) {
e.printStackTrace(context.getStdErr());
return exitCode;
}
return exitCode;
}
Aggregations