use of com.facebook.buck.rules.BuildRuleParams in project buck by facebook.
the class CxxLinkableEnhancerTest method testThatBuildTargetsFromNativeLinkableDepsContributeToActualDeps.
@Test
public void testThatBuildTargetsFromNativeLinkableDepsContributeToActualDeps() throws Exception {
BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
BuildTarget target = BuildTargetFactory.newInstance("//foo:bar");
BuildRuleParams params = new FakeBuildRuleParamsBuilder(target).build();
// Create a dummy build rule and add it to the resolver.
BuildTarget fakeBuildTarget = BuildTargetFactory.newInstance("//:fake");
FakeBuildRule fakeBuildRule = new FakeBuildRule(new FakeBuildRuleParamsBuilder(fakeBuildTarget).build(), pathResolver);
fakeBuildRule.setOutputFile("foo");
resolver.addToIndex(fakeBuildRule);
// Create a native linkable dep and have it list the fake build rule above as a link
// time dependency.
NativeLinkableInput nativeLinkableInput = NativeLinkableInput.of(ImmutableList.of(SourcePathArg.of(fakeBuildRule.getSourcePathToOutput())), ImmutableSet.of(), ImmutableSet.of());
FakeNativeLinkable nativeLinkable = createNativeLinkable("//:dep", pathResolver, nativeLinkableInput, nativeLinkableInput);
// Construct a CxxLink object and pass the native linkable above as the dep.
CxxLink cxxLink = CxxLinkableEnhancer.createCxxLinkableBuildRule(CxxPlatformUtils.DEFAULT_CONFIG, CXX_PLATFORM, params, resolver, pathResolver, ruleFinder, target, Linker.LinkType.EXECUTABLE, Optional.empty(), DEFAULT_OUTPUT, Linker.LinkableDepType.STATIC, ImmutableList.<NativeLinkable>of(nativeLinkable), Optional.empty(), Optional.empty(), ImmutableSet.of(), NativeLinkableInput.builder().setArgs(DEFAULT_INPUTS).build());
// Verify that the fake build rule made it in as a dep.
assertTrue(cxxLink.getDeps().contains(fakeBuildRule));
}
use of com.facebook.buck.rules.BuildRuleParams 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());
}
use of com.facebook.buck.rules.BuildRuleParams in project buck by facebook.
the class CxxPrecompiledHeaderRuleTest method samePchIffSameFlags.
@Test
public void samePchIffSameFlags() throws Exception {
assumeTrue(platformOkForPCHTests());
BuildTarget pchTarget = newTarget("//test:pch");
CxxPrecompiledHeaderTemplate pch = newPCH(pchTarget);
ruleResolver.addToIndex(pch);
BuildTarget lib1Target = newTarget("//test:lib1");
BuildRuleParams lib1Params = newParams(lib1Target);
CxxSourceRuleFactory factory1 = newFactoryBuilder(lib1Params, "-frtti").setPrecompiledHeader(new DefaultBuildTargetSourcePath(pchTarget)).build();
CxxPreprocessAndCompile lib1 = factory1.createPreprocessAndCompileBuildRule("lib1.cpp", newSource("lib1.cpp"));
ruleResolver.addToIndex(lib1);
ImmutableList<String> cmd1 = lib1.makeMainStep(pathResolver, Paths.get("/tmp/x"), false).getCommand();
BuildTarget lib2Target = newTarget("//test:lib2");
BuildRuleParams lib2Params = newParams(lib2Target);
CxxSourceRuleFactory factory2 = newFactoryBuilder(lib2Params, "-frtti").setPrecompiledHeader(new DefaultBuildTargetSourcePath(pchTarget)).build();
CxxPreprocessAndCompile lib2 = factory2.createPreprocessAndCompileBuildRule("lib2.cpp", newSource("lib2.cpp"));
ruleResolver.addToIndex(lib2);
ImmutableList<String> cmd2 = lib2.makeMainStep(pathResolver, Paths.get("/tmp/x"), false).getCommand();
BuildTarget lib3Target = newTarget("//test:lib3");
BuildRuleParams lib3Params = newParams(lib3Target);
CxxSourceRuleFactory factory3 = newFactoryBuilder(lib3Params, "-fno-rtti").setPrecompiledHeader(new DefaultBuildTargetSourcePath(pchTarget)).build();
CxxPreprocessAndCompile lib3 = factory3.createPreprocessAndCompileBuildRule("lib3.cpp", newSource("lib3.cpp"));
ruleResolver.addToIndex(lib3);
ImmutableList<String> cmd3 = lib3.makeMainStep(pathResolver, Paths.get("/tmp/x"), false).getCommand();
assertTrue(seek(cmd1, "-frtti").size() > 0);
assertTrue(seek(cmd2, "-frtti").size() > 0);
assertFalse(seek(cmd3, "-frtti").size() > 0);
assertFalse(seek(cmd1, "-fno-rtti").size() > 0);
assertFalse(seek(cmd2, "-fno-rtti").size() > 0);
assertTrue(seek(cmd3, "-fno-rtti").size() > 0);
List<String> pchFlag1 = seek(cmd1, "-include-pch");
assertTrue(pchFlag1.size() >= 2);
pchFlag1 = pchFlag1.subList(0, 2);
List<String> pchFlag2 = seek(cmd2, "-include-pch");
assertTrue(pchFlag2.size() >= 2);
pchFlag2 = pchFlag2.subList(0, 2);
List<String> pchFlag3 = seek(cmd3, "-include-pch");
assertTrue(pchFlag3.size() >= 2);
pchFlag3 = pchFlag3.subList(0, 2);
assertEquals(pchFlag1, pchFlag2);
assertNotEquals(pchFlag2, pchFlag3);
}
use of com.facebook.buck.rules.BuildRuleParams in project buck by facebook.
the class DefaultJavaLibraryTest method createDefaultJavaLibraryRuleWithAbiKey.
private BuildRule createDefaultJavaLibraryRuleWithAbiKey(BuildTarget buildTarget, ImmutableSet<String> srcs, ImmutableSortedSet<BuildRule> deps, ImmutableSortedSet<BuildRule> exportedDeps, Optional<AbstractJavacOptions.SpoolMode> spoolMode, ImmutableList<String> postprocessClassesCommands) {
ProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
ImmutableSortedSet<? extends SourcePath> srcsAsPaths = FluentIterable.from(srcs).transform(Paths::get).transform(p -> new PathSourcePath(projectFilesystem, p)).toSortedSet(Ordering.natural());
BuildRuleParams buildRuleParams = new FakeBuildRuleParamsBuilder(buildTarget).setDeclaredDeps(ImmutableSortedSet.copyOf(deps)).build();
JavacOptions javacOptions = spoolMode.isPresent() ? JavacOptions.builder(DEFAULT_JAVAC_OPTIONS).setSpoolMode(spoolMode.get()).build() : DEFAULT_JAVAC_OPTIONS;
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
DefaultJavaLibrary defaultJavaLibrary = new DefaultJavaLibrary(buildRuleParams, new SourcePathResolver(ruleFinder), ruleFinder, srcsAsPaths, /* resources */
ImmutableSet.of(), javacOptions.getGeneratedSourceFolderName(), /* proguardConfig */
Optional.empty(), postprocessClassesCommands, exportedDeps, /* providedDeps */
ImmutableSortedSet.of(), ImmutableSortedSet.of(), javacOptions.trackClassUsage(), /* additionalClasspathEntries */
ImmutableSet.of(), new JavacToJarStepFactory(javacOptions, JavacOptionsAmender.IDENTITY), /* resourcesRoot */
Optional.empty(), /* manifest file */
Optional.empty(), /* mavenCoords */
Optional.empty(), /* tests */
ImmutableSortedSet.of(), /* classesToRemoveFromJar */
ImmutableSet.of()) {
};
ruleResolver.addToIndex(defaultJavaLibrary);
return defaultJavaLibrary;
}
use of com.facebook.buck.rules.BuildRuleParams in project buck by facebook.
the class JavaBinaryTest method testGetExecutableCommand.
@Test
public void testGetExecutableCommand() throws Exception {
// prebuilt_jar //third_party/generator:generator
PrebuiltJarBuilder.createBuilder(BuildTargetFactory.newInstance("//third_party/generator:generator")).setBinaryJar(PATH_TO_GENERATOR_JAR).build();
// prebuilt_jar //third_party/guava:guava
TargetNode<?, ?> guavaNode = PrebuiltJarBuilder.createBuilder(BuildTargetFactory.newInstance("//third_party/guava:guava")).setBinaryJar(PATH_TO_GUAVA_JAR).build();
// java_library //java/com/facebook/base:base
TargetNode<?, ?> libraryNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//java/com/facebook/base:base")).addSrc(Paths.get("java/com/facebook/base/Base.java")).addDep(guavaNode.getBuildTarget()).build();
TargetGraph targetGraph = TargetGraphFactory.newInstance(guavaNode, libraryNode);
BuildRuleResolver ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(ruleResolver));
BuildRule libraryRule = ruleResolver.requireRule(libraryNode.getBuildTarget());
BuildRuleParams params = new FakeBuildRuleParamsBuilder(BuildTargetFactory.newInstance("//java/com/facebook/base:Main")).setDeclaredDeps(ImmutableSortedSet.of(libraryRule)).build();
// java_binary //java/com/facebook/base:Main
JavaBinary javaBinary = ruleResolver.addToIndex(new JavaBinary(params, new ExternalJavaRuntimeLauncher("/foobar/java"), "com.facebook.base.Main", null, /* merge manifests */
true, null, /* blacklist */
ImmutableSet.of(), ImmutableSet.of(), ImmutableSet.of(), /* cache */
true));
// Strip the trailing "." from the absolute path to the current directory.
final String basePath = new File(".").getAbsolutePath().replaceFirst("\\.$", "");
// Each classpath entry is specified via its absolute path so that the executable command can be
// run from a /tmp directory, if necessary.
String expectedClasspath = basePath + pathResolver.getRelativePath(javaBinary.getSourcePathToOutput());
List<String> expectedCommand = ImmutableList.of("/foobar/java", "-jar", expectedClasspath);
assertEquals(expectedCommand, javaBinary.getExecutableCommand().getCommandPrefix(pathResolver));
assertFalse("Library rules that are used exclusively by genrules should not be part of the classpath.", expectedClasspath.contains(PATH_TO_GENERATOR_JAR.toString()));
}
Aggregations