use of com.facebook.buck.rules.PathSourcePath in project buck by facebook.
the class CxxLibraryDescriptionTest method createCxxLibraryBuildRules.
@Test
public void createCxxLibraryBuildRules() throws Exception {
Assume.assumeFalse("This test assumes no sandboxing", cxxBuckConfig.sandboxSources());
FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
CxxPlatform cxxPlatform = CxxLibraryBuilder.createDefaultPlatform();
// Setup a normal C++ source
String sourceName = "test/bar.cpp";
// 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 sharedLibraryDepTarget = BuildTarget.builder(depTarget).addFlavors(CxxDescriptionEnhancer.SHARED_FLAVOR).addFlavors(cxxPlatform.getFlavor()).build();
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 cxxSourceRuleFactoryPDC = CxxSourceRuleFactoryHelper.of(filesystem.getRootPath(), target, cxxPlatform, cxxBuckConfig, CxxSourceRuleFactory.PicType.PDC);
CxxLibraryBuilder cxxLibraryBuilder = new CxxLibraryBuilder(target, cxxBuckConfig).setExportedHeaders(ImmutableSortedMap.of(genHeaderName, new DefaultBuildTargetSourcePath(genHeaderTarget))).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath(sourceName)), 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 target graph.
TargetGraph targetGraph = TargetGraphFactory.newInstance(genHeaderBuilder.build(), genSourceBuilder.build(), depBuilder.build(), cxxLibraryBuilder.build());
// Construct C/C++ library build 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 the C/C++ preprocessor input is setup correctly.
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(genHeaderName), new DefaultBuildTargetSourcePath(genHeaderTarget))));
assertThat(publicHeaders.getHeaderMap(), equalTo(getHeaderMaps(filesystem, target, resolver, cxxPlatform, HeaderVisibility.PUBLIC)));
// Verify that the archive rule has the correct deps: the object files from our sources.
rule.getNativeLinkableInput(cxxPlatform, Linker.LinkableDepType.STATIC);
BuildRule staticRule = resolver.getRule(CxxDescriptionEnhancer.createStaticLibraryBuildTarget(target, cxxPlatform.getFlavor(), CxxSourceRuleFactory.PicType.PDC));
assertNotNull(staticRule);
assertEquals(ImmutableSet.of(cxxSourceRuleFactoryPDC.createCompileBuildTarget("test/bar.cpp"), cxxSourceRuleFactoryPDC.createCompileBuildTarget(genSourceName)), staticRule.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 staticCompileRule1 = resolver.getRule(cxxSourceRuleFactoryPDC.createCompileBuildTarget("test/bar.cpp"));
assertNotNull(staticCompileRule1);
assertThat(DependencyAggregationTestUtil.getDisaggregatedDeps(staticCompileRule1).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 user-provided source has correct deps setup
// for the various header rules.
BuildRule staticCompileRule2 = resolver.getRule(cxxSourceRuleFactoryPDC.createCompileBuildTarget(genSourceName));
assertNotNull(staticCompileRule2);
assertThat(DependencyAggregationTestUtil.getDisaggregatedDeps(staticCompileRule2).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())));
// Verify that the archive rule has the correct deps: the object files from our sources.
CxxSourceRuleFactory cxxSourceRuleFactoryPIC = CxxSourceRuleFactoryHelper.of(filesystem.getRootPath(), target, cxxPlatform, cxxBuckConfig, CxxSourceRuleFactory.PicType.PIC);
rule.getNativeLinkableInput(cxxPlatform, Linker.LinkableDepType.SHARED);
BuildRule sharedRule = resolver.getRule(CxxDescriptionEnhancer.createSharedLibraryBuildTarget(target, cxxPlatform.getFlavor(), Linker.LinkType.SHARED));
assertNotNull(sharedRule);
assertEquals(ImmutableSet.of(sharedLibraryDepTarget, cxxSourceRuleFactoryPIC.createCompileBuildTarget("test/bar.cpp"), cxxSourceRuleFactoryPIC.createCompileBuildTarget(genSourceName)), sharedRule.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 sharedCompileRule1 = resolver.getRule(cxxSourceRuleFactoryPIC.createCompileBuildTarget("test/bar.cpp"));
assertNotNull(sharedCompileRule1);
assertThat(DependencyAggregationTestUtil.getDisaggregatedDeps(sharedCompileRule1).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 user-provided source has correct deps setup
// for the various header rules.
BuildRule sharedCompileRule2 = resolver.getRule(cxxSourceRuleFactoryPIC.createCompileBuildTarget(genSourceName));
assertNotNull(sharedCompileRule2);
assertThat(DependencyAggregationTestUtil.getDisaggregatedDeps(sharedCompileRule2).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.PathSourcePath in project buck by facebook.
the class CxxLibraryDescriptionTest method thinArchiveSettingIsPropagatedToArchive.
@Test
public void thinArchiveSettingIsPropagatedToArchive() throws Exception {
ProjectFilesystem filesystem = new FakeProjectFilesystem();
BuildTarget target = BuildTargetFactory.newInstance("//:rule").withFlavors(CxxDescriptionEnhancer.STATIC_FLAVOR, CxxLibraryBuilder.createDefaultPlatform().getFlavor());
CxxLibraryBuilder libBuilder = new CxxLibraryBuilder(target, new CxxBuckConfig(FakeBuckConfig.builder().setSections("[cxx]", "archive_contents=thin", "sandbox_sources=" + cxxBuckConfig.sandboxSources()).build()), CxxPlatformUtils.DEFAULT_PLATFORMS);
TargetGraph targetGraph = TargetGraphFactory.newInstance(libBuilder.build());
BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
libBuilder.setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new PathSourcePath(filesystem, Paths.get("test.cpp")))));
Archive lib = (Archive) libBuilder.build(resolver, filesystem, targetGraph);
assertThat(lib.getContents(), equalTo(Archive.Contents.THIN));
}
use of com.facebook.buck.rules.PathSourcePath in project buck by facebook.
the class CxxLibraryDescriptionTest method locationMacroExpandedPlatformLinkerFlagNoPlatformMatch.
@Test
public void locationMacroExpandedPlatformLinkerFlagNoPlatformMatch() throws Exception {
BuildTarget location = BuildTargetFactory.newInstance("//:loc");
BuildTarget target = BuildTargetFactory.newInstance("//foo:bar").withFlavors(CxxDescriptionEnhancer.SHARED_FLAVOR, CxxLibraryBuilder.createDefaultPlatform().getFlavor());
ProjectFilesystem filesystem = new FakeProjectFilesystem();
ExportFileBuilder locBuilder = ExportFileBuilder.newExportFileBuilder(location);
locBuilder.setOut("somewhere.over.the.rainbow");
CxxLibraryBuilder libBuilder = new CxxLibraryBuilder(target, cxxBuckConfig);
libBuilder.setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new PathSourcePath(filesystem, Paths.get("test.cpp")))));
libBuilder.setPlatformLinkerFlags(PatternMatchedCollection.<ImmutableList<StringWithMacros>>builder().add(Pattern.compile("notarealplatform"), ImmutableList.of(StringWithMacrosUtils.format("-Wl,--version-script=%s", LocationMacro.of(location)))).build());
TargetGraph targetGraph = TargetGraphFactory.newInstance(libBuilder.build(), locBuilder.build());
BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
ExportFile loc = locBuilder.build(resolver, filesystem, targetGraph);
CxxLink lib = (CxxLink) libBuilder.build(resolver, filesystem, targetGraph);
assertThat(lib.getDeps(), not(hasItem(loc)));
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
assertThat(Arg.stringify(lib.getArgs(), pathResolver), not(hasItem(containsString(pathResolver.getRelativePath(Preconditions.checkNotNull(loc.getSourcePathToOutput())).toString()))));
}
use of com.facebook.buck.rules.PathSourcePath in project buck by facebook.
the class HeaderPathNormalizerTest method managedPrefixHeaderDir.
@Test
public void managedPrefixHeaderDir() {
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
Path header = filesystem.getPath("foo/bar.pch");
SourcePath headerPath = new PathSourcePath(filesystem, header);
HeaderPathNormalizer normalizer = new HeaderPathNormalizer.Builder(pathResolver).addPrefixHeader(headerPath).build();
assertThat(normalizer.getAbsolutePathForUnnormalizedPath(pathResolver.getAbsolutePath(headerPath)), Matchers.equalTo(Optional.of(pathResolver.getAbsolutePath(headerPath))));
assertThat(normalizer.getSourcePathForAbsolutePath(pathResolver.getAbsolutePath(headerPath)), Matchers.equalTo(headerPath));
}
use of com.facebook.buck.rules.PathSourcePath in project buck by facebook.
the class HeaderPathNormalizerTest method managedHeaderWithRelativePath.
@Test
public void managedHeaderWithRelativePath() {
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
Path header = filesystem.getPath("foo/bar.h");
SourcePath headerPath = new PathSourcePath(filesystem, header);
HeaderPathNormalizer normalizer = new HeaderPathNormalizer.Builder(pathResolver).addHeader(headerPath).build();
assertThat(normalizer.getAbsolutePathForUnnormalizedPath(filesystem.resolve(header)), Matchers.equalTo(Optional.of(pathResolver.getAbsolutePath(headerPath))));
assertThat(normalizer.getAbsolutePathForUnnormalizedPath(pathResolver.getAbsolutePath(headerPath)), Matchers.equalTo(Optional.of(pathResolver.getAbsolutePath(headerPath))));
assertThat(normalizer.getSourcePathForAbsolutePath(pathResolver.getAbsolutePath(headerPath)), Matchers.equalTo(headerPath));
}
Aggregations