use of com.facebook.buck.rules.DefaultBuildTargetSourcePath 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.DefaultBuildTargetSourcePath in project buck by facebook.
the class JvmLibraryArgInterpreterTest method compilerArgTakesPrecedenceOverJavacJarArg.
@Test
public void compilerArgTakesPrecedenceOverJavacJarArg() throws Exception {
Path javacJarPath = Paths.get("langtools").resolve("javac.jar");
BuildTarget target = BuildTargetFactory.newInstance("//langtools:javac");
PrebuiltJarBuilder.createBuilder(target).setBinaryJar(javacJarPath).build(ruleResolver);
SourcePath sourcePath = new DefaultBuildTargetSourcePath(target);
Either<BuiltInJavac, SourcePath> either = Either.ofRight(sourcePath);
arg.compiler = Optional.of(either);
arg.javacJar = Optional.of(new PathSourcePath(new FakeProjectFilesystem(), Paths.get("does-not-exist")));
JavacOptions options = createJavacOptions(arg);
Javac javac = options.getJavac();
assertEquals(pathResolver.getRelativePath(sourcePath), pathResolver.getRelativePath(options.getJavacJarPath().get()));
assertEquals(Optional.empty(), options.getJavacPath());
assertTrue(javac.getClass().getName(), javac instanceof Jsr199Javac);
}
use of com.facebook.buck.rules.DefaultBuildTargetSourcePath in project buck by facebook.
the class PythonLibraryDescriptionTest method cxxGenruleSrcs.
@Test
public void cxxGenruleSrcs() throws Exception {
CxxGenruleBuilder srcBuilder = new CxxGenruleBuilder(BuildTargetFactory.newInstance("//:src")).setOut("out.py");
PythonLibraryBuilder libraryBuilder = new PythonLibraryBuilder(BuildTargetFactory.newInstance("//:lib")).setSrcs(SourceList.ofUnnamedSources(ImmutableSortedSet.of(new DefaultBuildTargetSourcePath(srcBuilder.getTarget()))));
TargetGraph targetGraph = TargetGraphFactory.newInstance(srcBuilder.build(), libraryBuilder.build());
BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
CxxGenrule src = (CxxGenrule) resolver.requireRule(srcBuilder.getTarget());
PythonLibrary library = (PythonLibrary) resolver.requireRule(libraryBuilder.getTarget());
PythonPackageComponents components = library.getPythonPackageComponents(PythonTestUtils.PYTHON_PLATFORM, CxxPlatformUtils.DEFAULT_PLATFORM);
assertThat(components.getModules().values(), Matchers.contains(src.getGenrule(CxxPlatformUtils.DEFAULT_PLATFORM)));
}
use of com.facebook.buck.rules.DefaultBuildTargetSourcePath in project buck by facebook.
the class TargetNodeTranslatorTest method translateSourceWithFlags.
@Test
public void translateSourceWithFlags() {
BuildTarget a = BuildTargetFactory.newInstance("//:a");
BuildTarget b = BuildTargetFactory.newInstance("//:b");
TargetNodeTranslator translator = new TargetNodeTranslator(ImmutableList.of()) {
@Override
public Optional<BuildTarget> translateBuildTarget(BuildTarget target) {
return Optional.of(b);
}
@Override
public Optional<ImmutableMap<BuildTarget, Version>> getSelectedVersions(BuildTarget target) {
return Optional.empty();
}
};
assertThat(translator.translateSourceWithFlags(CELL_PATH_RESOLVER, PATTERN, SourceWithFlags.of(new DefaultBuildTargetSourcePath(a), ImmutableList.of("-flag"))), Matchers.equalTo(Optional.of(SourceWithFlags.of(new DefaultBuildTargetSourcePath(b), ImmutableList.of("-flag")))));
}
use of com.facebook.buck.rules.DefaultBuildTargetSourcePath in project buck by facebook.
the class AppleBuildRulesTest method buildingStopsAtGenruleDepButNotAtBundleDep.
@Test
public void buildingStopsAtGenruleDepButNotAtBundleDep() throws Exception {
// Pass a random static lib in a genrule and make sure a framework
// depending on the genrule doesn't build the dependencies of that genrule.
BuildTarget fooLibTarget = BuildTargetFactory.newInstance("//foo:lib");
TargetNode<?, ?> fooLibNode = AppleLibraryBuilder.createBuilder(fooLibTarget).build();
BuildTarget fooGenruleTarget = BuildTargetFactory.newInstance("//foo:genrule");
TargetNode<?, ?> fooGenruleNode = GenruleBuilder.newGenruleBuilder(fooGenruleTarget).setOut("foo").setCmd("echo hi > $OUT").setSrcs(ImmutableList.of(new DefaultBuildTargetSourcePath(fooLibTarget))).build();
BuildTarget barLibTarget = BuildTargetFactory.newInstance("//bar:lib#" + CxxDescriptionEnhancer.SHARED_FLAVOR);
TargetNode<?, ?> barLibNode = AppleLibraryBuilder.createBuilder(barLibTarget).setDeps(ImmutableSortedSet.of(fooGenruleTarget)).build();
BuildTarget barFrameworkTarget = BuildTargetFactory.newInstance("//bar:framework");
TargetNode<?, ?> barFrameworkNode = AppleBundleBuilder.createBuilder(barFrameworkTarget).setExtension(Either.ofLeft(AppleBundleExtension.FRAMEWORK)).setBinary(barLibTarget).build();
BuildTarget bazLibTarget = BuildTargetFactory.newInstance("//baz:lib#" + CxxDescriptionEnhancer.SHARED_FLAVOR);
TargetNode<?, ?> bazLibNode = AppleLibraryBuilder.createBuilder(bazLibTarget).setDeps(ImmutableSortedSet.of(barFrameworkTarget)).build();
BuildTarget bazFrameworkTarget = BuildTargetFactory.newInstance("//baz:framework");
TargetNode<?, ?> bazFrameworkNode = AppleBundleBuilder.createBuilder(bazFrameworkTarget).setExtension(Either.ofLeft(AppleBundleExtension.FRAMEWORK)).setBinary(bazLibTarget).build();
ImmutableSet<TargetNode<?, ?>> targetNodes = ImmutableSet.<TargetNode<?, ?>>builder().add(fooLibNode, fooGenruleNode, barLibNode, barFrameworkNode, bazLibNode, bazFrameworkNode).build();
TargetGraph targetGraph = TargetGraphFactory.newInstance(targetNodes);
Optional<AppleDependenciesCache> cache = useCache ? Optional.of(new AppleDependenciesCache(targetGraph)) : Optional.empty();
for (int i = 0; i < (useCache ? 2 : 1); i++) {
Iterable<TargetNode<?, ?>> rules = AppleBuildRules.getRecursiveTargetNodeDependenciesOfTypes(targetGraph, cache, AppleBuildRules.RecursiveDependenciesMode.BUILDING, bazFrameworkNode, Optional.empty());
assertEquals(ImmutableSortedSet.of(barFrameworkNode, fooGenruleNode), ImmutableSortedSet.copyOf(rules));
}
}
Aggregations