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);
}
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"));
}
}
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()));
}
}
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());
}
}
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());
}
Aggregations