use of com.facebook.buck.io.ProjectFilesystem in project buck by facebook.
the class CxxCompilationDatabaseIntegrationTest method testUberCompilationDatabase.
@Test
public void testUberCompilationDatabase() throws IOException {
ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "compilation_database", tmp);
workspace.setUp();
BuildTarget target = BuildTargetFactory.newInstance("//:test#default,uber-compilation-database");
ProjectFilesystem filesystem = new FakeProjectFilesystem();
Path compilationDatabase = workspace.buildAndReturnOutput(target.getFullyQualifiedName());
Path rootPath = tmp.getRoot();
assertEquals(BuildTargets.getGenPath(filesystem, target, "uber-compilation-database-%s/compile_commands.json"), rootPath.relativize(compilationDatabase));
Path binaryHeaderSymlinkTreeFolder = BuildTargets.getGenPath(filesystem, target.withFlavors(InternalFlavor.of("default"), CxxDescriptionEnhancer.HEADER_SYMLINK_TREE_FLAVOR), "%s");
Map<String, CxxCompilationDatabaseEntry> fileToEntry = CxxCompilationDatabaseUtils.parseCompilationDatabaseJsonFile(compilationDatabase);
assertEquals(1, fileToEntry.size());
String path = sandboxSources ? "buck-out/gen/test#default,sandbox/test.cpp" : "test.cpp";
BuildTarget compilationTarget = target.withFlavors(InternalFlavor.of("default"), InternalFlavor.of("compile-" + sanitize("test.cpp.o")));
assertHasEntry(fileToEntry, path, new ImmutableList.Builder<String>().add(COMPILER_PATH).add("-I").add(headerSymlinkTreePath(binaryHeaderSymlinkTreeFolder).toString()).addAll(getExtraFlagsForHeaderMaps(filesystem)).addAll(COMPILER_SPECIFIC_FLAGS).add("-x").add("c++").add("-fdebug-prefix-map=" + rootPath + "=.").addAll(MORE_COMPILER_SPECIFIC_FLAGS).add("-c").add("-MD").add("-MF").add(BuildTargets.getGenPath(filesystem, compilationTarget, "%s/test.cpp.o.dep").toString()).add(Paths.get(path).toString()).add("-o").add(BuildTargets.getGenPath(filesystem, compilationTarget, "%s/test.cpp.o").toString()).build());
}
use of com.facebook.buck.io.ProjectFilesystem in project buck by facebook.
the class CxxCompilationDatabaseIntegrationTest method compilationDatabaseWithGeneratedFilesFetchedFromCacheAlsoFetchesGeneratedSources.
@Test
public void compilationDatabaseWithGeneratedFilesFetchedFromCacheAlsoFetchesGeneratedSources() throws Exception {
// Create a new temporary path since this test uses a different testdata directory than the
// one used in the common setup method.
tmp.after();
tmp = new TemporaryPaths();
tmp.before();
ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "compilation_database_with_generated_files", tmp);
workspace.setUp();
workspace.enableDirCache();
BuildTarget target = BuildTargetFactory.newInstance("//dep1:dep1#default,compilation-database");
workspace.runBuckBuild(target.getFullyQualifiedName()).assertSuccess();
workspace.runBuckCommand("clean").assertSuccess();
workspace.runBuckBuild(target.getFullyQualifiedName()).assertSuccess();
ProjectFilesystem filesystem = new FakeProjectFilesystem();
BuildTarget sourceTarget = BuildTargetFactory.newInstance("//dep1:source");
Path source = workspace.getPath(BuildTargets.getGenPath(filesystem, sourceTarget, "%s"));
assertThat(Files.exists(source), is(true));
}
use of com.facebook.buck.io.ProjectFilesystem in project buck by facebook.
the class CxxCompilationDatabaseTest method testCompilationDatabase.
@Test
public void testCompilationDatabase() throws Exception {
BuildTarget testBuildTarget = BuildTarget.builder(BuildTargetFactory.newInstance("//foo:baz")).addAllFlavors(ImmutableSet.of(CxxCompilationDatabase.COMPILATION_DATABASE)).build();
final String root = "/Users/user/src";
final Path fakeRoot = Paths.get(root);
ProjectFilesystem filesystem = new FakeProjectFilesystem(fakeRoot);
BuildRuleParams testBuildRuleParams = new FakeBuildRuleParamsBuilder(testBuildTarget).setProjectFilesystem(filesystem).build();
BuildRuleResolver testBuildRuleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver testSourcePathResolver = new SourcePathResolver(new SourcePathRuleFinder(testBuildRuleResolver));
HeaderSymlinkTree privateSymlinkTree = CxxDescriptionEnhancer.createHeaderSymlinkTree(testBuildRuleParams, testBuildRuleResolver, CxxPlatformUtils.DEFAULT_PLATFORM, ImmutableMap.of(), HeaderVisibility.PRIVATE, true);
testBuildRuleResolver.addToIndex(privateSymlinkTree);
HeaderSymlinkTree exportedSymlinkTree = CxxDescriptionEnhancer.createHeaderSymlinkTree(testBuildRuleParams, testBuildRuleResolver, CxxPlatformUtils.DEFAULT_PLATFORM, ImmutableMap.of(), HeaderVisibility.PUBLIC, true);
testBuildRuleResolver.addToIndex(exportedSymlinkTree);
BuildTarget compileTarget = BuildTarget.builder(testBuildRuleParams.getBuildTarget().getUnflavoredBuildTarget()).addFlavors(InternalFlavor.of("compile-test.cpp")).build();
PreprocessorFlags preprocessorFlags = PreprocessorFlags.builder().addIncludes(CxxHeadersDir.of(CxxPreprocessables.IncludeType.SYSTEM, new FakeSourcePath("/foo/bar")), CxxHeadersDir.of(CxxPreprocessables.IncludeType.SYSTEM, new FakeSourcePath("/test"))).build();
ImmutableSortedSet.Builder<CxxPreprocessAndCompile> rules = ImmutableSortedSet.naturalOrder();
BuildRuleParams compileBuildRuleParams = new FakeBuildRuleParamsBuilder(compileTarget).setProjectFilesystem(filesystem).setDeclaredDeps(ImmutableSortedSet.of(privateSymlinkTree, exportedSymlinkTree)).build();
rules.add(testBuildRuleResolver.addToIndex(CxxPreprocessAndCompile.preprocessAndCompile(compileBuildRuleParams, new PreprocessorDelegate(testSourcePathResolver, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, CxxPlatformUtils.DEFAULT_PLATFORM.getHeaderVerification(), filesystem.getRootPath(), new GccPreprocessor(new HashedFileTool(Paths.get("preprocessor"))), preprocessorFlags, new RuleKeyAppendableFunction<FrameworkPath, Path>() {
@Override
public void appendToRuleKey(RuleKeyObjectSink sink) {
// Do nothing.
}
@Override
public Path apply(FrameworkPath input) {
throw new UnsupportedOperationException("should not be called");
}
}, Optional.empty(), /* leadingIncludePaths */
Optional.empty()), new CompilerDelegate(testSourcePathResolver, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, new GccCompiler(new HashedFileTool(Paths.get("compiler"))), CxxToolFlags.of()), Paths.get("test.o"), new FakeSourcePath(filesystem, "test.cpp"), CxxSource.Type.CXX, Optional.empty(), CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, CxxPlatformUtils.DEFAULT_ASSEMBLER_DEBUG_PATH_SANITIZER, Optional.empty())));
CxxCompilationDatabase compilationDatabase = CxxCompilationDatabase.createCompilationDatabase(testBuildRuleParams, rules.build());
testBuildRuleResolver.addToIndex(compilationDatabase);
assertThat(compilationDatabase.getRuntimeDeps().collect(MoreCollectors.toImmutableSet()), Matchers.contains(exportedSymlinkTree.getBuildTarget(), privateSymlinkTree.getBuildTarget()));
assertEquals("getPathToOutput() should be a function of the build target.", BuildTargets.getGenPath(filesystem, testBuildTarget, "__%s.json"), testSourcePathResolver.getRelativePath(compilationDatabase.getSourcePathToOutput()));
List<Step> buildSteps = compilationDatabase.getBuildSteps(FakeBuildContext.withSourcePathResolver(testSourcePathResolver), new FakeBuildableContext());
assertEquals(2, buildSteps.size());
assertTrue(buildSteps.get(0) instanceof MkdirStep);
assertTrue(buildSteps.get(1) instanceof CxxCompilationDatabase.GenerateCompilationCommandsJson);
CxxCompilationDatabase.GenerateCompilationCommandsJson step = (CxxCompilationDatabase.GenerateCompilationCommandsJson) buildSteps.get(1);
Iterable<CxxCompilationDatabaseEntry> observedEntries = step.createEntries();
Iterable<CxxCompilationDatabaseEntry> expectedEntries = ImmutableList.of(CxxCompilationDatabaseEntry.of(root, root + "/test.cpp", ImmutableList.of("compiler", "-isystem", "/foo/bar", "-isystem", "/test", "-x", "c++", "-c", "-MD", "-MF", "test.o.dep", "test.cpp", "-o", "test.o")));
MoreAsserts.assertIterablesEquals(expectedEntries, observedEntries);
}
use of com.facebook.buck.io.ProjectFilesystem 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.io.ProjectFilesystem 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"));
}
}
Aggregations