use of com.facebook.buck.rules.DefaultBuildTargetSourcePath in project buck by facebook.
the class CxxDescriptionEnhancerTest method libraryTestIncludesPrivateHeadersOfLibraryUnderTest.
@Test
public void libraryTestIncludesPrivateHeadersOfLibraryUnderTest() throws Exception {
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
BuildTarget libTarget = BuildTargetFactory.newInstance("//:lib");
BuildTarget testTarget = BuildTargetFactory.newInstance("//:test");
BuildRuleParams libParams = new FakeBuildRuleParamsBuilder(libTarget).build();
FakeCxxLibrary libRule = new FakeCxxLibrary(libParams, BuildTargetFactory.newInstance("//:header"), BuildTargetFactory.newInstance("//:symlink"), BuildTargetFactory.newInstance("//:privateheader"), BuildTargetFactory.newInstance("//:privatesymlink"), new FakeBuildRule("//:archive", pathResolver), new FakeBuildRule("//:shared", pathResolver), Paths.get("output/path/lib.so"), "lib.so", // Ensure the test is listed as a dep of the lib.
ImmutableSortedSet.of(testTarget));
BuildRuleParams testParams = new FakeBuildRuleParamsBuilder(testTarget).setDeclaredDeps(ImmutableSortedSet.of(libRule)).build();
ImmutableList<CxxPreprocessorInput> combinedInput = CxxDescriptionEnhancer.collectCxxPreprocessorInput(testParams, CxxPlatformUtils.DEFAULT_PLATFORM, ImmutableMultimap.of(), ImmutableList.of(), ImmutableSet.of(), CxxPreprocessables.getTransitiveCxxPreprocessorInput(CxxPlatformUtils.DEFAULT_PLATFORM, FluentIterable.from(testParams.getDeps()).filter(CxxPreprocessorDep.class::isInstance)), ImmutableList.of(), Optional.empty());
Set<SourcePath> roots = new HashSet<>();
for (CxxHeaders headers : CxxPreprocessorInput.concat(combinedInput).getIncludes()) {
roots.add(headers.getRoot());
}
assertThat("Test of library should include both public and private headers", roots, Matchers.hasItems(new DefaultBuildTargetSourcePath(BuildTargetFactory.newInstance("//:symlink")), new DefaultBuildTargetSourcePath(BuildTargetFactory.newInstance("//:privatesymlink"))));
}
use of com.facebook.buck.rules.DefaultBuildTargetSourcePath in project buck by facebook.
the class CxxLibraryDescriptionTest method createBuildRule.
@Test
public void createBuildRule() throws Exception {
Assume.assumeFalse("This test assumes no sandboxing", cxxBuckConfig.sandboxSources());
ProjectFilesystem filesystem = new FakeProjectFilesystem();
CxxPlatform cxxPlatform = CxxLibraryBuilder.createDefaultPlatform();
// Setup a genrule the generates a header we'll list.
String genHeaderName = "test/foo.h";
BuildTarget genHeaderTarget = BuildTargetFactory.newInstance("//:genHeader");
GenruleBuilder genHeaderBuilder = GenruleBuilder.newGenruleBuilder(genHeaderTarget).setOut(genHeaderName);
// Setup a genrule the generates a source we'll list.
String genSourceName = "test/foo.cpp";
BuildTarget genSourceTarget = BuildTargetFactory.newInstance("//:genSource");
GenruleBuilder genSourceBuilder = GenruleBuilder.newGenruleBuilder(genSourceTarget).setOut(genSourceName);
// Setup a C/C++ library that we'll depend on form the C/C++ binary description.
BuildTarget depTarget = BuildTargetFactory.newInstance("//:dep");
CxxLibraryBuilder depBuilder = new CxxLibraryBuilder(depTarget, cxxBuckConfig).setExportedHeaders(SourceList.ofUnnamedSources(ImmutableSortedSet.of(new FakeSourcePath("blah.h")))).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("test.cpp"))));
BuildTarget headerSymlinkTreeTarget = BuildTarget.builder(depTarget).addFlavors(CxxDescriptionEnhancer.EXPORTED_HEADER_SYMLINK_TREE_FLAVOR).addFlavors(CxxPreprocessables.HeaderMode.SYMLINK_TREE_ONLY.getFlavor()).build();
// Setup the build params we'll pass to description when generating the build rules.
BuildTarget target = BuildTargetFactory.newInstance("//:rule");
CxxSourceRuleFactory cxxSourceRuleFactory = CxxSourceRuleFactoryHelper.of(filesystem.getRootPath(), target, cxxPlatform, cxxBuckConfig);
String headerName = "test/bar.h";
String privateHeaderName = "test/bar_private.h";
CxxLibraryBuilder cxxLibraryBuilder = new CxxLibraryBuilder(target, cxxBuckConfig).setExportedHeaders(ImmutableSortedSet.of(new FakeSourcePath(headerName), new DefaultBuildTargetSourcePath(genHeaderTarget))).setHeaders(ImmutableSortedSet.of(new FakeSourcePath(privateHeaderName))).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("test/bar.cpp")), SourceWithFlags.of(new DefaultBuildTargetSourcePath(genSourceTarget)))).setFrameworks(ImmutableSortedSet.of(FrameworkPath.ofSourcePath(new FakeSourcePath("/some/framework/path/s.dylib")), FrameworkPath.ofSourcePath(new FakeSourcePath("/another/framework/path/a.dylib")))).setDeps(ImmutableSortedSet.of(depTarget));
// Build the target graph.
TargetGraph targetGraph = TargetGraphFactory.newInstance(genHeaderBuilder.build(), genSourceBuilder.build(), depBuilder.build(), cxxLibraryBuilder.build());
// Build the rules.
BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
genHeaderBuilder.build(resolver, filesystem, targetGraph);
genSourceBuilder.build(resolver, filesystem, targetGraph);
depBuilder.build(resolver, filesystem, targetGraph);
CxxLibrary rule = (CxxLibrary) cxxLibraryBuilder.build(resolver, filesystem, targetGraph);
// Verify public preprocessor input.
CxxPreprocessorInput publicInput = rule.getCxxPreprocessorInput(cxxPlatform, HeaderVisibility.PUBLIC);
assertThat(publicInput.getFrameworks(), containsInAnyOrder(FrameworkPath.ofSourcePath(new PathSourcePath(filesystem, Paths.get("/some/framework/path/s.dylib"))), FrameworkPath.ofSourcePath(new PathSourcePath(filesystem, Paths.get("/another/framework/path/a.dylib")))));
CxxSymlinkTreeHeaders publicHeaders = (CxxSymlinkTreeHeaders) publicInput.getIncludes().get(0);
assertThat(publicHeaders.getIncludeType(), equalTo(CxxPreprocessables.IncludeType.LOCAL));
assertThat(publicHeaders.getNameToPathMap(), equalTo(ImmutableMap.<Path, SourcePath>of(Paths.get(headerName), new FakeSourcePath(headerName), Paths.get(genHeaderName), new DefaultBuildTargetSourcePath(genHeaderTarget))));
assertThat(publicHeaders.getHeaderMap(), equalTo(getHeaderMaps(filesystem, target, resolver, cxxPlatform, HeaderVisibility.PUBLIC)));
// Verify private preprocessor input.
CxxPreprocessorInput privateInput = rule.getCxxPreprocessorInput(cxxPlatform, HeaderVisibility.PRIVATE);
assertThat(privateInput.getFrameworks(), containsInAnyOrder(FrameworkPath.ofSourcePath(new PathSourcePath(filesystem, Paths.get("/some/framework/path/s.dylib"))), FrameworkPath.ofSourcePath(new PathSourcePath(filesystem, Paths.get("/another/framework/path/a.dylib")))));
CxxSymlinkTreeHeaders privateHeaders = (CxxSymlinkTreeHeaders) privateInput.getIncludes().get(0);
assertThat(privateHeaders.getIncludeType(), equalTo(CxxPreprocessables.IncludeType.LOCAL));
assertThat(privateHeaders.getNameToPathMap(), equalTo(ImmutableMap.<Path, SourcePath>of(Paths.get(privateHeaderName), new FakeSourcePath(privateHeaderName))));
assertThat(privateHeaders.getHeaderMap(), equalTo(getHeaderMaps(filesystem, target, resolver, cxxPlatform, HeaderVisibility.PRIVATE)));
// Verify that the archive rule has the correct deps: the object files from our sources.
rule.getNativeLinkableInput(cxxPlatform, Linker.LinkableDepType.STATIC);
BuildRule archiveRule = resolver.getRule(CxxDescriptionEnhancer.createStaticLibraryBuildTarget(target, cxxPlatform.getFlavor(), CxxSourceRuleFactory.PicType.PDC));
assertNotNull(archiveRule);
assertEquals(ImmutableSet.of(cxxSourceRuleFactory.createCompileBuildTarget("test/bar.cpp"), cxxSourceRuleFactory.createCompileBuildTarget(genSourceName)), archiveRule.getDeps().stream().map(BuildRule::getBuildTarget).collect(MoreCollectors.toImmutableSet()));
// Verify that the compile rule for our user-provided source has correct deps setup
// for the various header rules.
BuildRule compileRule1 = resolver.getRule(cxxSourceRuleFactory.createCompileBuildTarget("test/bar.cpp"));
assertNotNull(compileRule1);
assertThat(DependencyAggregationTestUtil.getDisaggregatedDeps(compileRule1).map(BuildRule::getBuildTarget).collect(MoreCollectors.toImmutableSet()), containsInAnyOrder(genHeaderTarget, headerSymlinkTreeTarget, CxxDescriptionEnhancer.createHeaderSymlinkTreeTarget(target, HeaderVisibility.PRIVATE, cxxPlatform.getFlavor()), CxxDescriptionEnhancer.createHeaderSymlinkTreeTarget(target, HeaderVisibility.PUBLIC, CxxPreprocessables.HeaderMode.SYMLINK_TREE_ONLY.getFlavor())));
// Verify that the compile rule for our genrule-generated source has correct deps setup
// for the various header rules and the generating genrule.
BuildRule compileRule2 = resolver.getRule(cxxSourceRuleFactory.createCompileBuildTarget(genSourceName));
assertNotNull(compileRule2);
assertThat(DependencyAggregationTestUtil.getDisaggregatedDeps(compileRule2).map(BuildRule::getBuildTarget).collect(MoreCollectors.toImmutableSet()), containsInAnyOrder(genHeaderTarget, genSourceTarget, headerSymlinkTreeTarget, CxxDescriptionEnhancer.createHeaderSymlinkTreeTarget(target, HeaderVisibility.PRIVATE, cxxPlatform.getFlavor()), CxxDescriptionEnhancer.createHeaderSymlinkTreeTarget(target, HeaderVisibility.PUBLIC, CxxPreprocessables.HeaderMode.SYMLINK_TREE_ONLY.getFlavor())));
}
use of com.facebook.buck.rules.DefaultBuildTargetSourcePath in project buck by facebook.
the class CxxLibraryDescriptionTest method srcsFromCxxGenrule.
@Test
public void srcsFromCxxGenrule() throws Exception {
CxxGenruleBuilder srcBuilder = new CxxGenruleBuilder(BuildTargetFactory.newInstance("//:src")).setOut("foo.cpp");
CxxLibraryBuilder libraryBuilder = new CxxLibraryBuilder(BuildTargetFactory.newInstance("//:lib")).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new DefaultBuildTargetSourcePath(srcBuilder.getTarget()))));
TargetGraph targetGraph = TargetGraphFactory.newInstance(srcBuilder.build(), libraryBuilder.build());
BuildRuleResolver ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
ruleResolver.requireRule(libraryBuilder.getTarget().withAppendedFlavors(CxxPlatformUtils.DEFAULT_PLATFORM.getFlavor(), CxxLibraryDescription.Type.STATIC.getFlavor()));
verifySourcePaths(ruleResolver);
}
use of com.facebook.buck.rules.DefaultBuildTargetSourcePath in project buck by facebook.
the class CxxLibraryTest method headerOnlyExports.
@Test
public void headerOnlyExports() throws Exception {
BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(ruleResolver));
BuildTarget target = BuildTargetFactory.newInstance("//foo:bar");
BuildRuleParams params = new FakeBuildRuleParamsBuilder(target).build();
CxxPlatform cxxPlatform = CxxPlatformUtils.build(new CxxBuckConfig(FakeBuckConfig.builder().build()));
BuildTarget staticPicLibraryTarget = params.getBuildTarget().withAppendedFlavors(cxxPlatform.getFlavor(), CxxDescriptionEnhancer.STATIC_PIC_FLAVOR);
ruleResolver.addToIndex(new FakeBuildRule(new FakeBuildRuleParamsBuilder(staticPicLibraryTarget).build(), pathResolver));
FrameworkPath frameworkPath = FrameworkPath.ofSourcePath(new DefaultBuildTargetSourcePath(BuildTargetFactory.newInstance("//foo:baz")));
// Construct a CxxLibrary object to test.
CxxLibrary cxxLibrary = new CxxLibrary(params, ruleResolver, FluentIterable.from(params.getDeclaredDeps().get()), /* hasExportedHeaders */
x -> true, /* headerOnly */
x -> true, Functions.constant(StringArg.from("-ldl")), /* linkTargetInput */
Functions.constant(NativeLinkableInput.of()), /* supportedPlatformsRegex */
Optional.empty(), ImmutableSet.of(frameworkPath), ImmutableSet.of(), NativeLinkable.Linkage.STATIC, /* linkWhole */
false, Optional.empty(), ImmutableSortedSet.of(), /* isAsset */
false, true);
NativeLinkableInput expectedSharedNativeLinkableInput = NativeLinkableInput.of(StringArg.from("-ldl"), ImmutableSet.of(frameworkPath), ImmutableSet.of());
assertEquals(expectedSharedNativeLinkableInput, cxxLibrary.getNativeLinkableInput(cxxPlatform, Linker.LinkableDepType.SHARED));
}
use of com.facebook.buck.rules.DefaultBuildTargetSourcePath in project buck by facebook.
the class CxxPrecompiledHeaderRuleTest method userRuleIncludePathsChangedByPCH.
@Test
public void userRuleIncludePathsChangedByPCH() throws Exception {
assumeTrue(platformOkForPCHTests());
CxxPreprocessorInput cxxPreprocessorInput = CxxPreprocessorInput.builder().addIncludes(CxxHeadersDir.of(CxxPreprocessables.IncludeType.SYSTEM, new FakeSourcePath("/tmp/sys"))).build();
BuildTarget lib1Target = newTarget("//some/other/dir:lib1");
BuildRuleParams lib1Params = newParams(lib1Target);
CxxSourceRuleFactory lib1Factory = newFactoryBuilder(lib1Params).addCxxPreprocessorInput(cxxPreprocessorInput).build();
CxxPreprocessAndCompile lib1 = lib1Factory.createPreprocessAndCompileBuildRule("lib1.cpp", newSource("lib1.cpp"));
ruleResolver.addToIndex(lib1);
ImmutableList<String> lib1Cmd = lib1.makeMainStep(pathResolver, Paths.get("/tmp/x"), false).getCommand();
BuildTarget pchTarget = newTarget("//test:pch");
CxxPrecompiledHeaderTemplate pch = newPCH(pchTarget, new FakeSourcePath("header.h"), ImmutableSortedSet.of(lib1));
ruleResolver.addToIndex(pch);
BuildTarget lib2Target = newTarget("//test:lib2");
BuildRuleParams lib2Params = newParams(lib2Target);
CxxSourceRuleFactory lib2Factory = newFactoryBuilder(lib2Params).setPrecompiledHeader(new DefaultBuildTargetSourcePath(pchTarget)).build();
CxxPreprocessAndCompile lib2 = lib2Factory.createPreprocessAndCompileBuildRule("lib2.cpp", newSource("lib2.cpp"));
ruleResolver.addToIndex(lib2);
ImmutableList<String> lib2Cmd = lib2.makeMainStep(pathResolver, Paths.get("/tmp/y"), false).getCommand();
CxxPrecompiledHeader pchInstance = null;
for (BuildRule dep : lib2.getDeps()) {
if (dep instanceof CxxPrecompiledHeader) {
pchInstance = (CxxPrecompiledHeader) dep;
}
}
assertNotNull(pchInstance);
ImmutableList<String> pchCmd = pchInstance.makeMainStep(pathResolver, Paths.get("/tmp/z")).getCommand();
// (pretend that) lib1 has a dep resulting in adding this dir to the include path flags
assertContains(lib1Cmd, ImmutableList.of("-isystem", "/tmp/sys"));
// PCH should inherit those flags
assertContains(pchCmd, ImmutableList.of("-isystem", "/tmp/sys"));
// and because PCH uses them, these should be used in lib2 which uses PCH; also, used *first*
assertContains(lib2Cmd, ImmutableList.of("-isystem", "/tmp/sys"));
Iterator<String> iter = lib2Cmd.iterator();
while (iter.hasNext()) {
if (iter.next().equals("-isystem")) {
break;
}
}
assertTrue(iter.hasNext());
assertEquals("/tmp/sys", iter.next());
}
Aggregations