Search in sources :

Example 51 with ExecutionContext

use of com.facebook.buck.step.ExecutionContext in project buck by facebook.

the class CxxCompileStepIntegrationTest method createsAnArgfile.

@Test
public void createsAnArgfile() throws Exception {
    ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot());
    CxxPlatform platform = CxxPlatformUtils.build(new CxxBuckConfig(FakeBuckConfig.builder().build()));
    // Build up the paths to various files the archive step will use.
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
    Compiler compiler = platform.getCc().resolve(resolver);
    ImmutableList<String> compilerCommandPrefix = compiler.getCommandPrefix(pathResolver);
    Path output = filesystem.resolve(Paths.get("output.o"));
    Path depFile = filesystem.resolve(Paths.get("output.dep"));
    Path relativeInput = Paths.get("input.c");
    Path input = filesystem.resolve(relativeInput);
    filesystem.writeContentsToPath("int main() {}", relativeInput);
    Path scratchDir = filesystem.getPath("scratchDir");
    filesystem.mkdirs(scratchDir);
    ImmutableList.Builder<String> preprocessorArguments = ImmutableList.builder();
    ImmutableList.Builder<String> compilerArguments = ImmutableList.builder();
    compilerArguments.add("-g");
    // Build an archive step.
    CxxPreprocessAndCompileStep step = new CxxPreprocessAndCompileStep(filesystem, CxxPreprocessAndCompileStep.Operation.PREPROCESS_AND_COMPILE, output, depFile, relativeInput, CxxSource.Type.C, Optional.of(new CxxPreprocessAndCompileStep.ToolCommand(compilerCommandPrefix, preprocessorArguments.build(), ImmutableMap.of(), Optional.empty())), Optional.of(new CxxPreprocessAndCompileStep.ToolCommand(compilerCommandPrefix, compilerArguments.build(), ImmutableMap.of(), Optional.empty())), HeaderPathNormalizer.empty(pathResolver), CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, CxxPlatformUtils.DEFAULT_ASSEMBLER_DEBUG_PATH_SANITIZER, scratchDir, true, compiler);
    // Execute the archive step and verify it ran successfully.
    ExecutionContext executionContext = TestExecutionContext.newInstance();
    TestConsole console = (TestConsole) executionContext.getConsole();
    int exitCode = step.execute(executionContext).getExitCode();
    assertEquals("compile step failed: " + console.getTextWrittenToStdErr(), 0, exitCode);
    Path argfile = filesystem.resolve(scratchDir.resolve("ppandcompile.argsfile"));
    assertThat(filesystem, pathExists(argfile));
    assertThat(Files.readAllLines(argfile, StandardCharsets.UTF_8), hasItem(equalTo("-g")));
    // Cleanup.
    Files.delete(input);
    Files.deleteIfExists(output);
}
Also used : Path(java.nio.file.Path) ImmutableList(com.google.common.collect.ImmutableList) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Example 52 with ExecutionContext

use of com.facebook.buck.step.ExecutionContext in project buck by facebook.

the class ArchiveStepIntegrationTest method inputDirs.

@Test
public void inputDirs() throws IOException, InterruptedException {
    assumeTrue(Platform.detect() == Platform.MACOS || Platform.detect() == Platform.LINUX);
    ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot());
    CxxPlatform platform = CxxPlatformUtils.build(new CxxBuckConfig(FakeBuckConfig.builder().build()));
    // Build up the paths to various files the archive step will use.
    SourcePathResolver sourcePathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
    Archiver archiver = platform.getAr();
    Path output = filesystem.getPath("output.a");
    Path input = filesystem.getPath("foo/blah.dat");
    filesystem.mkdirs(input.getParent());
    filesystem.writeContentsToPath("blah", input);
    // Build an archive step.
    ArchiveStep archiveStep = new ArchiveStep(filesystem, archiver.getEnvironment(sourcePathResolver), archiver.getCommandPrefix(sourcePathResolver), ImmutableList.of(), getArchiveOptions(false), output, ImmutableList.of(input.getParent()), archiver);
    // Execute the archive step and verify it ran successfully.
    ExecutionContext executionContext = TestExecutionContext.newInstance();
    TestConsole console = (TestConsole) executionContext.getConsole();
    int exitCode = archiveStep.execute(executionContext).getExitCode();
    assertEquals("archive step failed: " + console.getTextWrittenToStdErr(), 0, exitCode);
    // zero'd out.
    try (ArArchiveInputStream stream = new ArArchiveInputStream(new FileInputStream(filesystem.resolve(output).toFile()))) {
        ArArchiveEntry entry = stream.getNextArEntry();
        assertThat(entry.getName(), Matchers.equalTo("blah.dat"));
    }
}
Also used : Path(java.nio.file.Path) ArArchiveEntry(org.apache.commons.compress.archivers.ar.ArArchiveEntry) ArArchiveInputStream(org.apache.commons.compress.archivers.ar.ArArchiveInputStream) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) FileInputStream(java.io.FileInputStream) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Example 53 with ExecutionContext

use of com.facebook.buck.step.ExecutionContext in project buck by facebook.

the class ArchiveStepIntegrationTest method thinArchives.

@Test
public void thinArchives() throws IOException, InterruptedException {
    assumeTrue(Platform.detect() == Platform.MACOS || Platform.detect() == Platform.LINUX);
    ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot());
    CxxPlatform platform = CxxPlatformUtils.build(new CxxBuckConfig(FakeBuckConfig.builder().build()));
    assumeTrue(platform.getAr().supportsThinArchives());
    // Build up the paths to various files the archive step will use.
    SourcePathResolver sourcePathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
    Archiver archiver = platform.getAr();
    Path output = filesystem.getPath("foo/libthin.a");
    filesystem.mkdirs(output.getParent());
    // Create a really large input file so it's obvious that the archive is thin.
    Path input = filesystem.getPath("bar/blah.dat");
    filesystem.mkdirs(input.getParent());
    byte[] largeInputFile = new byte[1024 * 1024];
    byte[] fillerToRepeat = "hello\n".getBytes(StandardCharsets.UTF_8);
    for (int i = 0; i < largeInputFile.length; i++) {
        largeInputFile[i] = fillerToRepeat[i % fillerToRepeat.length];
    }
    filesystem.writeBytesToPath(largeInputFile, input);
    // Build an archive step.
    ArchiveStep archiveStep = new ArchiveStep(filesystem, archiver.getEnvironment(sourcePathResolver), archiver.getCommandPrefix(sourcePathResolver), ImmutableList.of(), getArchiveOptions(true), output, ImmutableList.of(input), archiver);
    // Execute the archive step and verify it ran successfully.
    ExecutionContext executionContext = TestExecutionContext.newInstance();
    TestConsole console = (TestConsole) executionContext.getConsole();
    int exitCode = archiveStep.execute(executionContext).getExitCode();
    assertEquals("archive step failed: " + console.getTextWrittenToStdErr(), 0, exitCode);
    // Verify that the thin header is present.
    assertThat(filesystem.readFirstLine(output), Matchers.equalTo(Optional.of("!<thin>")));
    // Verify that even though the archived contents is really big, the archive is still small.
    assertThat(filesystem.getFileSize(output), Matchers.lessThan(1000L));
    // can parse the archive contents.
    try (OutputStream outputStream = Files.newOutputStream(filesystem.resolve(output), StandardOpenOption.WRITE)) {
        outputStream.write(ObjectFileScrubbers.GLOBAL_HEADER);
    }
    // zero'd out.
    try (ArArchiveInputStream stream = new ArArchiveInputStream(new FileInputStream(filesystem.resolve(output).toFile()))) {
        ArArchiveEntry entry = stream.getNextArEntry();
        // Verify that the input names are relative paths from the outputs parent dir.
        assertThat(entry.getName(), Matchers.equalTo(output.getParent().relativize(input).toString()));
    }
}
Also used : Path(java.nio.file.Path) ArArchiveEntry(org.apache.commons.compress.archivers.ar.ArArchiveEntry) OutputStream(java.io.OutputStream) ArArchiveInputStream(org.apache.commons.compress.archivers.ar.ArArchiveInputStream) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) FileInputStream(java.io.FileInputStream) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Example 54 with ExecutionContext

use of com.facebook.buck.step.ExecutionContext in project buck by facebook.

the class ArchiveStepIntegrationTest method thatGeneratedArchivesAreDeterministic.

@Test
@SuppressWarnings("PMD.AvoidUsingOctalValues")
public void thatGeneratedArchivesAreDeterministic() throws IOException, InterruptedException {
    assumeTrue(Platform.detect() == Platform.MACOS || Platform.detect() == Platform.LINUX);
    ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot());
    CxxPlatform platform = CxxPlatformUtils.build(new CxxBuckConfig(FakeBuckConfig.builder().build()));
    // Build up the paths to various files the archive step will use.
    SourcePathResolver sourcePathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
    Archiver archiver = platform.getAr();
    Path output = filesystem.getPath("output.a");
    Path input = filesystem.getPath("input.dat");
    filesystem.writeContentsToPath("blah", input);
    Preconditions.checkState(filesystem.resolve(input).toFile().setExecutable(true));
    // Build an archive step.
    ArchiveStep archiveStep = new ArchiveStep(filesystem, archiver.getEnvironment(sourcePathResolver), archiver.getCommandPrefix(sourcePathResolver), ImmutableList.of(), getArchiveOptions(false), output, ImmutableList.of(input), archiver);
    FileScrubberStep fileScrubberStep = new FileScrubberStep(filesystem, output, platform.getAr().getScrubbers());
    // Execute the archive step and verify it ran successfully.
    ExecutionContext executionContext = TestExecutionContext.newInstance();
    TestConsole console = (TestConsole) executionContext.getConsole();
    int exitCode = archiveStep.execute(executionContext).getExitCode();
    assertEquals("archive step failed: " + console.getTextWrittenToStdErr(), 0, exitCode);
    exitCode = fileScrubberStep.execute(executionContext).getExitCode();
    assertEquals("archive scrub step failed: " + console.getTextWrittenToStdErr(), 0, exitCode);
    // zero'd out.
    try (ArArchiveInputStream stream = new ArArchiveInputStream(new FileInputStream(filesystem.resolve(output).toFile()))) {
        ArArchiveEntry entry = stream.getNextArEntry();
        assertEquals(ObjectFileCommonModificationDate.COMMON_MODIFICATION_TIME_STAMP, entry.getLastModified());
        assertEquals(0, entry.getUserId());
        assertEquals(0, entry.getGroupId());
        assertEquals(String.format("0%o", entry.getMode()), 0100644, entry.getMode());
    }
}
Also used : Path(java.nio.file.Path) ArArchiveEntry(org.apache.commons.compress.archivers.ar.ArArchiveEntry) ArArchiveInputStream(org.apache.commons.compress.archivers.ar.ArArchiveInputStream) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) FileScrubberStep(com.facebook.buck.step.fs.FileScrubberStep) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) FileInputStream(java.io.FileInputStream) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Example 55 with ExecutionContext

use of com.facebook.buck.step.ExecutionContext in project buck by facebook.

the class CxxCollectAndLogInferDependenciesStepTest method testStepWritesNoCellTokenInFileWhenCellIsAbsent.

@Test
public void testStepWritesNoCellTokenInFileWhenCellIsAbsent() throws IOException, InterruptedException {
    assumeThat(Platform.detect(), is(not(WINDOWS)));
    ProjectFilesystem filesystem = createFakeFilesystem("/Users/user/src");
    BuildTarget testBuildTarget = BuildTarget.builder().setUnflavoredBuildTarget(UnflavoredBuildTarget.of(filesystem.getRootPath(), Optional.empty(), "//target", "short")).addFlavors(CxxInferEnhancer.InferFlavors.INFER.get()).build();
    BuildRuleParams testBuildRuleParams = new FakeBuildRuleParamsBuilder(testBuildTarget).setProjectFilesystem(filesystem).build();
    InferBuckConfig inferBuckConfig = new InferBuckConfig(FakeBuckConfig.builder().build());
    CxxInferCaptureAndAggregatingRules<CxxInferAnalyze> captureAndAggregatingRules = new CxxInferCaptureAndAggregatingRules<>(ImmutableSet.of(), ImmutableSet.<CxxInferAnalyze>of());
    CxxInferAnalyze analyzeRule = new CxxInferAnalyze(testBuildRuleParams, inferBuckConfig, captureAndAggregatingRules);
    Path outputFile = Paths.get("infer-deps.txt");
    CxxCollectAndLogInferDependenciesStep step = CxxCollectAndLogInferDependenciesStep.fromAnalyzeRule(analyzeRule, filesystem, outputFile);
    ExecutionContext executionContext = TestExecutionContext.newInstance();
    int exitCode = step.execute(executionContext).getExitCode();
    assertThat(exitCode, is(StepExecutionResult.SUCCESS.getExitCode()));
    String expectedOutput = InferLogLine.fromBuildTarget(testBuildTarget, analyzeRule.getAbsolutePathToResultsDir()).toString();
    assertEquals(expectedOutput + "\n", filesystem.readFileIfItExists(outputFile).get());
}
Also used : FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) BuildTarget(com.facebook.buck.model.BuildTarget) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Test(org.junit.Test)

Aggregations

ExecutionContext (com.facebook.buck.step.ExecutionContext)176 TestExecutionContext (com.facebook.buck.step.TestExecutionContext)140 Test (org.junit.Test)128 Path (java.nio.file.Path)79 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)67 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)66 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)55 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)50 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)45 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)44 Step (com.facebook.buck.step.Step)34 ImmutableList (com.google.common.collect.ImmutableList)32 BuildTarget (com.facebook.buck.model.BuildTarget)31 SourcePath (com.facebook.buck.rules.SourcePath)26 TestConsole (com.facebook.buck.testutil.TestConsole)25 FakeProcess (com.facebook.buck.util.FakeProcess)21 ProcessExecutorParams (com.facebook.buck.util.ProcessExecutorParams)20 ImmutableMap (com.google.common.collect.ImmutableMap)20 MakeCleanDirectoryStep (com.facebook.buck.step.fs.MakeCleanDirectoryStep)19 FakeProcessExecutor (com.facebook.buck.util.FakeProcessExecutor)19