Search in sources :

Example 21 with DefaultProcessExecutor

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

the class ProjectWorkspace method doRunCommand.

private ProcessExecutor.Result doRunCommand(List<String> command) throws IOException, InterruptedException {
    ProcessExecutorParams params = ProcessExecutorParams.builder().setCommand(command).build();
    ProcessExecutor executor = new DefaultProcessExecutor(new TestConsole());
    String currentDir = System.getProperty("user.dir");
    try {
        System.setProperty("user.dir", destPath.toAbsolutePath().toString());
        return executor.launchAndExecute(params);
    } finally {
        System.setProperty("user.dir", currentDir);
    }
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) TestConsole(com.facebook.buck.testutil.TestConsole)

Example 22 with DefaultProcessExecutor

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

the class HostnameFetchingTest method fetchedHostnameMatchesCommandLineHostname.

@Test
public void fetchedHostnameMatchesCommandLineHostname() throws IOException, InterruptedException {
    ProcessExecutor executor = new DefaultProcessExecutor(new TestConsole());
    ProcessExecutor.Result result = executor.launchAndExecute(ProcessExecutorParams.ofCommand("hostname"));
    assumeThat("hostname returns success", result.getExitCode(), equalTo(0));
    String expectedHostname = result.getStdout().orElse("").trim();
    assumeThat("hostname returns non-empty string", expectedHostname, not(emptyString()));
    assertThat("fetched hostname should equal hostname returned from CLI", HostnameFetching.getHostname(), equalTo(expectedHostname));
}
Also used : DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) Matchers.emptyString(org.hamcrest.Matchers.emptyString) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Example 23 with DefaultProcessExecutor

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

the class ProjectWorkspace method asCell.

public Cell asCell() throws IOException, InterruptedException {
    ProjectFilesystemAndConfig filesystemAndConfig = projectFilesystemAndConfig.get();
    ProjectFilesystem filesystem = filesystemAndConfig.projectFilesystem;
    Config config = filesystemAndConfig.config;
    TestConsole console = new TestConsole();
    ImmutableMap<String, String> env = ImmutableMap.copyOf(System.getenv());
    DefaultAndroidDirectoryResolver directoryResolver = new DefaultAndroidDirectoryResolver(filesystem.getRootPath().getFileSystem(), env, Optional.empty(), Optional.empty());
    return CellProvider.createForLocalBuild(filesystem, Watchman.NULL_WATCHMAN, new BuckConfig(config, filesystem, Architecture.detect(), Platform.detect(), env, new DefaultCellPathResolver(filesystem.getRootPath(), config)), CellConfig.of(), new KnownBuildRuleTypesFactory(new DefaultProcessExecutor(console), directoryResolver)).getCellByPath(filesystem.getRootPath());
}
Also used : DefaultCellPathResolver(com.facebook.buck.rules.DefaultCellPathResolver) DefaultAndroidDirectoryResolver(com.facebook.buck.android.DefaultAndroidDirectoryResolver) BuckConfig(com.facebook.buck.cli.BuckConfig) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) BuckConfig(com.facebook.buck.cli.BuckConfig) Config(com.facebook.buck.config.Config) CellConfig(com.facebook.buck.config.CellConfig) KnownBuildRuleTypesFactory(com.facebook.buck.rules.KnownBuildRuleTypesFactory) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) TestConsole(com.facebook.buck.testutil.TestConsole)

Example 24 with DefaultProcessExecutor

use of com.facebook.buck.util.DefaultProcessExecutor 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)

Example 25 with DefaultProcessExecutor

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

the class AndroidBinaryIntegrationTest method testNativeLibraryMerging.

@Test
public void testNativeLibraryMerging() throws IOException, InterruptedException {
    NdkCxxPlatform platform = AndroidNdkHelper.getNdkCxxPlatform(workspace, filesystem);
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
    Path tmpDir = tmpFolder.newFolder("merging_tmp");
    SymbolGetter syms = new SymbolGetter(new DefaultProcessExecutor(new TestConsole()), tmpDir, platform.getObjdump(), pathResolver);
    SymbolsAndDtNeeded info;
    workspace.replaceFileContents(".buckconfig", "#cpu_abis", "cpu_abis = x86");
    Map<String, Path> paths = workspace.buildMultipleAndReturnOutputs("//apps/sample:app_with_merged_libs", "//apps/sample:app_with_alternate_merge_glue", "//apps/sample:app_with_merged_libs_modular");
    Path apkPath = paths.get("//apps/sample:app_with_merged_libs");
    ZipInspector zipInspector = new ZipInspector(apkPath);
    zipInspector.assertFileDoesNotExist("lib/x86/lib1a.so");
    zipInspector.assertFileDoesNotExist("lib/x86/lib1b.so");
    zipInspector.assertFileDoesNotExist("lib/x86/lib2e.so");
    zipInspector.assertFileDoesNotExist("lib/x86/lib2f.so");
    info = syms.getSymbolsAndDtNeeded(apkPath, "lib/x86/lib1.so");
    assertThat(info.symbols.global, Matchers.hasItem("A"));
    assertThat(info.symbols.global, Matchers.hasItem("B"));
    assertThat(info.symbols.global, Matchers.hasItem("glue_1"));
    assertThat(info.symbols.global, not(Matchers.hasItem("glue_2")));
    assertThat(info.dtNeeded, Matchers.hasItem("libnative_merge_C.so"));
    assertThat(info.dtNeeded, Matchers.hasItem("libnative_merge_D.so"));
    assertThat(info.dtNeeded, not(Matchers.hasItem("libnative_merge_B.so")));
    info = syms.getSymbolsAndDtNeeded(apkPath, "lib/x86/libnative_merge_C.so");
    assertThat(info.symbols.global, Matchers.hasItem("C"));
    assertThat(info.symbols.global, Matchers.hasItem("static_func_C"));
    assertThat(info.symbols.global, not(Matchers.hasItem("glue_1")));
    assertThat(info.symbols.global, not(Matchers.hasItem("glue_2")));
    assertThat(info.dtNeeded, Matchers.hasItem("libnative_merge_D.so"));
    assertThat(info.dtNeeded, Matchers.hasItem("libprebuilt_for_C.so"));
    info = syms.getSymbolsAndDtNeeded(apkPath, "lib/x86/libnative_merge_D.so");
    assertThat(info.symbols.global, Matchers.hasItem("D"));
    assertThat(info.symbols.global, not(Matchers.hasItem("glue_1")));
    assertThat(info.symbols.global, not(Matchers.hasItem("glue_2")));
    assertThat(info.dtNeeded, Matchers.hasItem("lib2.so"));
    assertThat(info.dtNeeded, not(Matchers.hasItem("libnative_merge_E.so")));
    assertThat(info.dtNeeded, not(Matchers.hasItem("libnative_merge_F.so")));
    info = syms.getSymbolsAndDtNeeded(apkPath, "lib/x86/lib2.so");
    assertThat(info.symbols.global, Matchers.hasItem("E"));
    assertThat(info.symbols.global, Matchers.hasItem("F"));
    assertThat(info.symbols.global, Matchers.hasItem("static_func_F"));
    assertThat(info.symbols.global, Matchers.hasItem("glue_1"));
    assertThat(info.symbols.global, not(Matchers.hasItem("glue_2")));
    assertThat(info.dtNeeded, Matchers.hasItem("libprebuilt_for_F.so"));
    Path otherPath = paths.get("//apps/sample:app_with_alternate_merge_glue");
    info = syms.getSymbolsAndDtNeeded(otherPath, "lib/x86/lib2.so");
    assertThat(info.symbols.global, not(Matchers.hasItem("glue_1")));
    assertThat(info.symbols.global, Matchers.hasItem("glue_2"));
    assertThat(info.dtNeeded, Matchers.hasItem("libprebuilt_for_F.so"));
    Path modularPath = paths.get("//apps/sample:app_with_merged_libs_modular");
    ZipInspector modularZipInspector = new ZipInspector(modularPath);
    modularZipInspector.assertFileDoesNotExist("lib/x86/lib1a.so");
    modularZipInspector.assertFileDoesNotExist("lib/x86/lib1b.so");
    modularZipInspector.assertFileDoesNotExist("lib/x86/lib2e.so");
    modularZipInspector.assertFileDoesNotExist("lib/x86/lib2f.so");
    modularZipInspector.assertFileExists("assets/native.merge.A/libs.txt");
    modularZipInspector.assertFileExists("assets/native.merge.A/libs.xzs");
    modularZipInspector.assertFileDoesNotExist("lib/x86/lib1.so");
    modularZipInspector.assertFileDoesNotExist("lib/x86/lib2.so");
    Path disassembly = workspace.buildAndReturnOutput("//apps/sample:disassemble_app_with_merged_libs_gencode");
    List<String> disassembledLines = filesystem.readLines(disassembly);
    Pattern fieldPattern = Pattern.compile("^\\.field public static final ([^:]+):Ljava/lang/String; = \"([^\"]+)\"$");
    ImmutableMap.Builder<String, String> mapBuilder = ImmutableMap.builder();
    for (String line : disassembledLines) {
        Matcher m = fieldPattern.matcher(line);
        if (!m.matches()) {
            continue;
        }
        mapBuilder.put(m.group(1), m.group(2));
    }
    assertThat(mapBuilder.build(), Matchers.equalTo(ImmutableMap.of("lib1a_so", "lib1_so", "lib1b_so", "lib1_so", "lib2e_so", "lib2_so", "lib2f_so", "lib2_so")));
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) SymbolsAndDtNeeded(com.facebook.buck.android.AndroidNdkHelper.SymbolsAndDtNeeded) Matcher(java.util.regex.Matcher) ZipInspector(com.facebook.buck.testutil.integration.ZipInspector) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) ImmutableMap(com.google.common.collect.ImmutableMap) SymbolGetter(com.facebook.buck.android.AndroidNdkHelper.SymbolGetter) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Aggregations

DefaultProcessExecutor (com.facebook.buck.util.DefaultProcessExecutor)37 TestConsole (com.facebook.buck.testutil.TestConsole)27 Test (org.junit.Test)21 ProcessExecutor (com.facebook.buck.util.ProcessExecutor)20 Path (java.nio.file.Path)13 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)8 ProcessExecutorParams (com.facebook.buck.util.ProcessExecutorParams)7 ExecutableFinder (com.facebook.buck.io.ExecutableFinder)5 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)5 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)5 NSDictionary (com.dd.plist.NSDictionary)3 NSString (com.dd.plist.NSString)3 SymbolGetter (com.facebook.buck.android.AndroidNdkHelper.SymbolGetter)3 BuckConfig (com.facebook.buck.cli.BuckConfig)3 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)3 DefaultCellPathResolver (com.facebook.buck.rules.DefaultCellPathResolver)3 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)3 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)3 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)3 HumanReadableException (com.facebook.buck.util.HumanReadableException)3