use of com.facebook.buck.shell.GenruleBuilder in project buck by facebook.
the class CxxLibraryDescriptionTest method linkerFlagsLocationMacro.
@Test
public void linkerFlagsLocationMacro() throws Exception {
ProjectFilesystem filesystem = new FakeProjectFilesystem();
BuildTarget target = BuildTargetFactory.newInstance("//:rule").withFlavors(CxxDescriptionEnhancer.SHARED_FLAVOR, CxxPlatformUtils.DEFAULT_PLATFORM.getFlavor());
GenruleBuilder depBuilder = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:dep")).setOut("out");
CxxLibraryBuilder builder = new CxxLibraryBuilder(target, cxxBuckConfig).setLinkerFlags(ImmutableList.of(StringWithMacrosUtils.format("--linker-script=%s", LocationMacro.of(depBuilder.getTarget())))).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("foo.c"))));
TargetGraph targetGraph = TargetGraphFactory.newInstance(depBuilder.build(), builder.build());
BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
Genrule dep = depBuilder.build(resolver, filesystem, targetGraph);
assertThat(builder.build().getExtraDeps(), hasItem(dep.getBuildTarget()));
BuildRule binary = builder.build(resolver, filesystem, targetGraph);
assertThat(binary, instanceOf(CxxLink.class));
assertThat(Arg.stringify(((CxxLink) binary).getArgs(), pathResolver), hasItem(String.format("--linker-script=%s", dep.getAbsoluteOutputFilePath(pathResolver))));
assertThat(binary.getDeps(), hasItem(dep));
}
use of com.facebook.buck.shell.GenruleBuilder 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.shell.GenruleBuilder in project buck by facebook.
the class PrebuiltCxxLibraryTest method testGetNativeLinkWithDep.
@Test
public void testGetNativeLinkWithDep() throws Exception {
ProjectFilesystem filesystem = new FakeProjectFilesystem();
CxxPlatform platform = CxxLibraryBuilder.createDefaultPlatform();
GenruleBuilder genSrcBuilder = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:gen_libx")).setOut("gen_libx").setCmd("something");
BuildTarget target = BuildTargetFactory.newInstance("//:x");
PrebuiltCxxLibraryBuilder builder = new PrebuiltCxxLibraryBuilder(target).setLibName("x").setLibDir("$(location //:gen_libx)");
TargetGraph targetGraph = TargetGraphFactory.newInstance(genSrcBuilder.build(), builder.build());
BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
BuildRule genSrc = genSrcBuilder.build(resolver, filesystem, targetGraph);
filesystem.writeContentsToPath("class Test {}", pathResolver.getAbsolutePath(genSrc.getSourcePathToOutput()).resolve("libx.a"));
PrebuiltCxxLibrary lib = (PrebuiltCxxLibrary) builder.build(resolver, filesystem, targetGraph);
lib.getNativeLinkableInput(platform, Linker.LinkableDepType.STATIC);
FileHashCache originalHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
DefaultRuleKeyFactory factory = new DefaultRuleKeyFactory(0, originalHashCache, pathResolver, ruleFinder);
RuleKey ruleKey = factory.build(lib);
assertNotNull(ruleKey);
}
use of com.facebook.buck.shell.GenruleBuilder in project buck by facebook.
the class PrebuiltCxxLibraryDescriptionTest method findDepsFromParamsWithLocation.
@Test
public void findDepsFromParamsWithLocation() throws NoSuchBuildTargetException {
BuildTarget genTarget = BuildTargetFactory.newInstance("//other:gen_lib");
GenruleBuilder genruleBuilder = GenruleBuilder.newGenruleBuilder(genTarget).setOut("lib_dir");
PrebuiltCxxLibraryBuilder builder = new PrebuiltCxxLibraryBuilder(TARGET);
builder.setSoname("test");
builder.setLibDir("$(location //other:gen_lib)");
TargetGraph targetGraph = TargetGraphFactory.newInstance(genruleBuilder.build(), builder.build());
ProjectFilesystem filesystem = new FakeProjectFilesystem();
BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
BuildRule genrule = genruleBuilder.build(resolver, filesystem, targetGraph);
PrebuiltCxxLibrary lib = (PrebuiltCxxLibrary) builder.build(resolver, filesystem, targetGraph);
Iterable<BuildTarget> implicit = builder.findImplicitDeps();
assertEquals(1, Iterables.size(implicit));
assertTrue(Iterables.contains(implicit, genTarget));
assertThat(lib.getDeps(), Matchers.contains(genrule));
}
use of com.facebook.buck.shell.GenruleBuilder in project buck by facebook.
the class PythonTestDescriptionTest method versionedSrcs.
@Test
public void versionedSrcs() throws Exception {
ProjectFilesystem filesystem = new FakeProjectFilesystem();
BuildTarget target = BuildTargetFactory.newInstance("//foo:lib");
SourcePath matchedSource = new FakeSourcePath("foo/a.py");
SourcePath unmatchedSource = new FakeSourcePath("foo/b.py");
GenruleBuilder depBuilder = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:dep")).setOut("out");
AbstractNodeBuilder<?, ?, ?> builder = PythonTestBuilder.create(target).setVersionedSrcs(VersionMatchedCollection.<SourceList>builder().add(ImmutableMap.of(depBuilder.getTarget(), Version.of("1.0")), SourceList.ofUnnamedSources(ImmutableSortedSet.of(matchedSource))).add(ImmutableMap.of(depBuilder.getTarget(), Version.of("2.0")), SourceList.ofUnnamedSources(ImmutableSortedSet.of(unmatchedSource))).build()).setSelectedVersions(ImmutableMap.of(depBuilder.getTarget(), Version.of("1.0")));
TargetGraph targetGraph = TargetGraphFactory.newInstance(depBuilder.build(), builder.build());
BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
depBuilder.build(resolver, filesystem, targetGraph);
PythonTest test = (PythonTest) builder.build(resolver, filesystem, targetGraph);
assertThat(test.getBinary().getComponents().getModules().values(), Matchers.allOf(Matchers.hasItem(matchedSource), Matchers.not(Matchers.hasItem(unmatchedSource))));
}
Aggregations