@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( ImmutableFlavor.of("default"), CxxDescriptionEnhancer.HEADER_SYMLINK_TREE_FLAVOR), "%s"); Path binaryExportedHeaderSymlinkTreeFolder = BuildTargets.getGenPath( filesystem, target.withFlavors( ImmutableFlavor.of("default"), CxxDescriptionEnhancer.EXPORTED_HEADER_SYMLINK_TREE_FLAVOR), "%s"); Map<String, CxxCompilationDatabaseEntry> fileToEntry = CxxCompilationDatabaseUtils.parseCompilationDatabaseJsonFile(compilationDatabase); assertEquals(1, fileToEntry.size()); assertHasEntry( fileToEntry, "test.cpp", new ImmutableList.Builder<String>() .add(COMPILER_PATH) .add("-fPIC") .add("-fPIC") .add("-I") .add(headerSymlinkTreePath(binaryHeaderSymlinkTreeFolder).toString()) .add("-I") .add(headerSymlinkTreePath(binaryExportedHeaderSymlinkTreeFolder).toString()) .addAll(getExtraFlagsForHeaderMaps(filesystem)) .addAll(COMPILER_SPECIFIC_FLAGS) .add("-x") .add("c++") .add("-c") .add("-o") .add( BuildTargets.getGenPath( filesystem, target.withFlavors( ImmutableFlavor.of("default"), ImmutableFlavor.of("compile-pic-" + sanitize("test.cpp.o"))), "%s/test.cpp.o") .toString()) .add(rootPath.resolve(Paths.get("test.cpp")).toRealPath().toString()) .build()); }
@Test public void binaryWithDependenciesCompilationDatabase() throws IOException { BuildTarget target = BuildTargetFactory.newInstance("//:binary_with_dep#compilation-database"); Path compilationDatabase = workspace.buildAndReturnOutput(target.getFullyQualifiedName()); ProjectFilesystem filesystem = new FakeProjectFilesystem(); Path rootPath = tmp.getRoot(); assertEquals( BuildTargets.getGenPath(filesystem, target, "__%s.json"), rootPath.relativize(compilationDatabase)); Path binaryHeaderSymlinkTreeFolder = BuildTargets.getGenPath( filesystem, target.withFlavors( ImmutableFlavor.of("default"), CxxDescriptionEnhancer.HEADER_SYMLINK_TREE_FLAVOR), "%s"); assertTrue(Files.exists(rootPath.resolve(binaryHeaderSymlinkTreeFolder))); BuildTarget libraryTarget = BuildTargetFactory.newInstance("//:library_with_header"); Path libraryExportedHeaderSymlinkTreeFolder = BuildTargets.getGenPath( filesystem, libraryTarget.withFlavors( ImmutableFlavor.of("default"), CxxDescriptionEnhancer.EXPORTED_HEADER_SYMLINK_TREE_FLAVOR), "%s"); // Verify that symlink folders for headers are created and header file is linked. assertTrue(Files.exists(rootPath.resolve(libraryExportedHeaderSymlinkTreeFolder))); assertTrue(Files.exists(rootPath.resolve(libraryExportedHeaderSymlinkTreeFolder + "/bar.h"))); Map<String, CxxCompilationDatabaseEntry> fileToEntry = CxxCompilationDatabaseUtils.parseCompilationDatabaseJsonFile(compilationDatabase); assertEquals(1, fileToEntry.size()); assertHasEntry( fileToEntry, "foo.cpp", new ImmutableList.Builder<String>() .add(COMPILER_PATH) .add("-I") .add(headerSymlinkTreePath(binaryHeaderSymlinkTreeFolder).toString()) .add("-I") .add(headerSymlinkTreePath(libraryExportedHeaderSymlinkTreeFolder).toString()) .addAll(getExtraFlagsForHeaderMaps(filesystem)) .addAll(COMPILER_SPECIFIC_FLAGS) .add("-x") .add("c++") .add("-c") .add("-o") .add( BuildTargets.getGenPath( filesystem, target.withFlavors( ImmutableFlavor.of("default"), ImmutableFlavor.of("compile-" + sanitize("foo.cpp.o"))), "%s/foo.cpp.o") .toString()) .add(rootPath.resolve(Paths.get("foo.cpp")).toRealPath().toString()) .build()); }