use of com.facebook.buck.rules.TargetGraph in project buck by facebook.
the class AndroidBinaryGraphEnhancerTest method testCreateDepsForPreDexing.
@Test
public void testCreateDepsForPreDexing() throws Exception {
// Create three Java rules, :dep1, :dep2, and :lib. :lib depends on :dep1 and :dep2.
BuildTarget javaDep1BuildTarget = BuildTargetFactory.newInstance("//java/com/example:dep1");
TargetNode<?, ?> javaDep1Node = JavaLibraryBuilder.createBuilder(javaDep1BuildTarget).addSrc(Paths.get("java/com/example/Dep1.java")).build();
BuildTarget javaDep2BuildTarget = BuildTargetFactory.newInstance("//java/com/example:dep2");
TargetNode<?, ?> javaDep2Node = JavaLibraryBuilder.createBuilder(javaDep2BuildTarget).addSrc(Paths.get("java/com/example/Dep2.java")).build();
BuildTarget javaLibBuildTarget = BuildTargetFactory.newInstance("//java/com/example:lib");
TargetNode<?, ?> javaLibNode = JavaLibraryBuilder.createBuilder(javaLibBuildTarget).addSrc(Paths.get("java/com/example/Lib.java")).addDep(javaDep1Node.getBuildTarget()).addDep(javaDep2Node.getBuildTarget()).build();
TargetGraph targetGraph = TargetGraphFactory.newInstance(javaDep1Node, javaDep2Node, javaLibNode);
BuildRuleResolver ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
BuildRule javaDep1 = ruleResolver.requireRule(javaDep1BuildTarget);
BuildRule javaDep2 = ruleResolver.requireRule(javaDep2BuildTarget);
BuildRule javaLib = ruleResolver.requireRule(javaLibBuildTarget);
// Assume we are enhancing an android_binary rule whose only dep
// is //java/com/example:lib, and that //java/com/example:dep2 is in its no_dx list.
ImmutableSortedSet<BuildRule> originalDeps = ImmutableSortedSet.of(javaLib);
ImmutableSet<BuildTarget> buildRulesToExcludeFromDex = ImmutableSet.of(javaDep2BuildTarget);
BuildTarget apkTarget = BuildTargetFactory.newInstance("//java/com/example:apk");
FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
BuildRuleParams originalParams = new BuildRuleParams(apkTarget, Suppliers.ofInstance(originalDeps), Suppliers.ofInstance(originalDeps), filesystem, TestCellBuilder.createCellRoots(filesystem));
AndroidBinaryGraphEnhancer graphEnhancer = new AndroidBinaryGraphEnhancer(originalParams, ruleResolver, ResourcesFilter.ResourceCompressionMode.DISABLED, FilterResourcesStep.ResourceFilter.EMPTY_FILTER, /* bannedDuplicateResourceTypes */
EnumSet.noneOf(RType.class), Optional.empty(), /* locales */
ImmutableSet.of(), createStrictMock(PathSourcePath.class), AndroidBinary.PackageType.DEBUG, /* cpuFilters */
ImmutableSet.of(), /* shouldBuildStringSourceMap */
false, /* shouldPreDex */
true, BuildTargets.getScratchPath(originalParams.getProjectFilesystem(), apkTarget, "%s/classes.dex"), DexSplitMode.NO_SPLIT, buildRulesToExcludeFromDex, /* resourcesToExclude */
ImmutableSet.of(), /* skipCrunchPngs */
false, /* includesVectorDrawables */
false, ANDROID_JAVAC_OPTIONS, EnumSet.noneOf(ExopackageMode.class), /* buildConfigValues */
BuildConfigFields.empty(), /* buildConfigValuesFile */
Optional.empty(), /* xzCompressionLevel */
Optional.empty(), /* trimResourceIds */
false, /* keepResourcePattern */
Optional.empty(), /* nativePlatforms */
ImmutableMap.of(), /* nativeLibraryMergeMap */
Optional.empty(), /* nativeLibraryMergeGlue */
Optional.empty(), /* nativeLibraryMergeCodeGenerator */
Optional.empty(), AndroidBinary.RelinkerMode.DISABLED, MoreExecutors.newDirectExecutorService(), /* manifestEntries */
ManifestEntries.empty(), CxxPlatformUtils.DEFAULT_CONFIG, new APKModuleGraph(TargetGraph.EMPTY, originalParams.getBuildTarget(), Optional.empty()), new DxConfig(FakeBuckConfig.builder().build()));
BuildTarget aaptPackageResourcesTarget = BuildTargetFactory.newInstance("//java/com/example:apk#aapt_package");
BuildRuleParams aaptPackageResourcesParams = new FakeBuildRuleParamsBuilder(aaptPackageResourcesTarget).build();
AaptPackageResources aaptPackageResources = new AaptPackageResources(aaptPackageResourcesParams, ruleFinder, ruleResolver, /* manifest */
new FakeSourcePath("java/src/com/facebook/base/AndroidManifest.xml"), new IdentityResourcesProvider(ImmutableList.of()), ImmutableList.of(), ImmutableSortedSet.of(), ImmutableSet.of(), /* resourceUnionPackage */
Optional.empty(), false, /* skipCrunchPngs */
false, /* includesVectorDrawables */
false, /* bannedDuplicateResourceTypes */
EnumSet.noneOf(RType.class), /* manifestEntries */
ManifestEntries.empty());
ruleResolver.addToIndex(aaptPackageResources);
AndroidPackageableCollection collection = new AndroidPackageableCollector(/* collectionRoot */
apkTarget, ImmutableSet.of(javaDep2BuildTarget), /* resourcesToExclude */
ImmutableSet.of(), new APKModuleGraph(TargetGraph.EMPTY, apkTarget, Optional.empty())).addClasspathEntry(((HasJavaClassHashes) javaDep1), new FakeSourcePath("ignored")).addClasspathEntry(((HasJavaClassHashes) javaDep2), new FakeSourcePath("ignored")).addClasspathEntry(((HasJavaClassHashes) javaLib), new FakeSourcePath("ignored")).build();
ImmutableMultimap<APKModule, DexProducedFromJavaLibrary> preDexedLibraries = graphEnhancer.createPreDexRulesForLibraries(/* additionalJavaLibrariesToDex */
ImmutableList.of(), collection);
BuildTarget fakeUberRDotJavaCompileTarget = BuildTargetFactory.newInstance("//fake:uber_r_dot_java#compile");
JavaLibrary fakeUberRDotJavaCompile = JavaLibraryBuilder.createBuilder(fakeUberRDotJavaCompileTarget).build(ruleResolver);
BuildTarget fakeUberRDotJavaDexTarget = BuildTargetFactory.newInstance("//fake:uber_r_dot_java#dex");
DexProducedFromJavaLibrary fakeUberRDotJavaDex = new DexProducedFromJavaLibrary(new FakeBuildRuleParamsBuilder(fakeUberRDotJavaDexTarget).build(), fakeUberRDotJavaCompile);
ruleResolver.addToIndex(fakeUberRDotJavaDex);
BuildRule preDexMergeRule = graphEnhancer.createPreDexMergeRule(preDexedLibraries, fakeUberRDotJavaDex);
BuildTarget dexMergeTarget = BuildTargetFactory.newInstance("//java/com/example:apk#dex_merge");
BuildRule dexMergeRule = ruleResolver.getRule(dexMergeTarget);
assertEquals(dexMergeRule, preDexMergeRule);
BuildTarget javaDep1DexBuildTarget = BuildTarget.builder(javaDep1BuildTarget).addFlavors(AndroidBinaryGraphEnhancer.DEX_FLAVOR).build();
BuildTarget javaDep2DexBuildTarget = BuildTarget.builder(javaDep2BuildTarget).addFlavors(AndroidBinaryGraphEnhancer.DEX_FLAVOR).build();
BuildTarget javaLibDexBuildTarget = BuildTarget.builder(javaLibBuildTarget).addFlavors(AndroidBinaryGraphEnhancer.DEX_FLAVOR).build();
assertThat("There should be a #dex rule for dep1 and lib, but not dep2 because it is in the no_dx " + "list. And we should depend on uber_r_dot_java", Iterables.transform(dexMergeRule.getDeps(), BuildRule::getBuildTarget), Matchers.allOf(Matchers.not(Matchers.hasItem(javaDep1BuildTarget)), Matchers.hasItem(javaDep1DexBuildTarget), Matchers.not(Matchers.hasItem(javaDep2BuildTarget)), Matchers.not(Matchers.hasItem(javaDep2DexBuildTarget)), Matchers.hasItem(javaLibDexBuildTarget), Matchers.hasItem(fakeUberRDotJavaDex.getBuildTarget())));
}
use of com.facebook.buck.rules.TargetGraph in project buck by facebook.
the class APKModuleTest method testAPKModuleGraphSimple.
/*
+ - - - - - -+
' root: '
' '
' +--------+ '
+-------------- ' | Binary | ' --------+
| ' +--------+ ' |
| ' | ' |
| ' | ' |
v ' | ' v
+ - - - - - - + ' | ' +- - - - - +
' android: ' ' | ' ' java: '
' ' ' | ' ' '
' +---------+ ' ' | ' ' +------+ '
' | Android | ' ' | ' ' | Java | '
' +---------+ ' ' | ' ' +------+ '
' ' ' | ' ' '
+ - - - - - - + ' | ' +- - - - - +
| ' | ' |
| ' | ' |
| ' v ' |
| ' +--------+ ' |
+-------------> ' | Common | ' <-------+
' +--------+ '
' '
+ - - - - - -+
*/
@Test
public void testAPKModuleGraphSimple() throws Exception {
ImmutableSet.Builder<TargetNode<?, ?>> nodeBuilder = ImmutableSet.builder();
BuildTarget commonLibraryTarget = BuildTargetFactory.newInstance("//src/com/facebook/test-common-library:test-common-library");
nodeBuilder.add(AndroidLibraryBuilder.createBuilder(commonLibraryTarget).addSrc(Paths.get("src/com/facebook/TestCommonLibrary.java")).build());
BuildTarget javaLibraryTarget = BuildTargetFactory.newInstance("//src/com/facebook/test-java-library:test-java-library").withFlavors(InternalFlavor.of("flavor"));
nodeBuilder.add(JavaLibraryBuilder.createBuilder(javaLibraryTarget).addSrc(Paths.get("src/com/facebook/TestJavaLibrary.java")).addDep(commonLibraryTarget).build());
BuildTarget androidLibraryTarget = BuildTargetFactory.newInstance("//src/com/facebook/test-android-library:test-android-library");
nodeBuilder.add(AndroidLibraryBuilder.createBuilder(androidLibraryTarget).addSrc(Paths.get("src/com/facebook/TestAndroidLibrary.java")).addDep(commonLibraryTarget).build());
BuildTarget keystoreTarget = BuildTargetFactory.newInstance("//:keystore");
nodeBuilder.add(KeystoreBuilder.createBuilder(keystoreTarget).setStore(new FakeSourcePath("debug.keystore")).setProperties(new FakeSourcePath("keystore.properties")).build());
BuildTarget androidBinaryTarget = BuildTargetFactory.newInstance("//src/com/facebook/test-android-binary:test-android-binary");
nodeBuilder.add(AndroidBinaryBuilder.createBuilder(androidBinaryTarget).setManifest(new FakeSourcePath("AndroidManifest.xml")).setKeystore(keystoreTarget).setOriginalDeps(ImmutableSortedSet.of(androidLibraryTarget, javaLibraryTarget, commonLibraryTarget)).build());
TargetGraph graph = TargetGraphFactory.newInstance(nodeBuilder.build());
Set<BuildTarget> seedTargets = new HashSet<>();
seedTargets.add(androidLibraryTarget);
seedTargets.add(javaLibraryTarget);
APKModuleGraph dag = new APKModuleGraph(graph, androidBinaryTarget, Optional.of(seedTargets));
ImmutableSet<APKModule> topLevelNodes = dag.getGraph().getNodesWithNoIncomingEdges();
assertThat(topLevelNodes.size(), is(2));
for (APKModule apkModule : topLevelNodes) {
assertThat(apkModule.getName(), oneOf("src.com.facebook.test.android.library", "src.com.facebook.test.java.library.test.java.library.flavor"));
ImmutableSet<APKModule> dependencies = dag.getGraph().getOutgoingNodesFor(apkModule);
assertThat(apkModule.isRootModule(), is(false));
assertThat(dependencies.size(), is(1));
assertThat(Iterables.getFirst(dependencies, null).getName(), is(APKModuleGraph.ROOT_APKMODULE_NAME));
assertThat(Iterables.getFirst(dependencies, null).isRootModule(), is(true));
}
assertThat(dag.getAPKModules().stream().map(APKModule::getName).collect(MoreCollectors.toImmutableSet()), containsInAnyOrder("dex", "src.com.facebook.test.android.library", "src.com.facebook.test.java.library.test.java.library.flavor"));
}
use of com.facebook.buck.rules.TargetGraph in project buck by facebook.
the class AndroidBinaryDescriptionTest method testNoDxRulesBecomeFirstOrderDeps.
@Test
public void testNoDxRulesBecomeFirstOrderDeps() throws Exception {
TargetNode<?, ?> transitiveDepNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//exciting:dep")).addSrc(Paths.get("Dep.java")).build();
TargetNode<?, ?> depNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//exciting:target")).addSrc(Paths.get("Other.java")).addDep(transitiveDepNode.getBuildTarget()).build();
TargetNode<?, ?> keystoreNode = KeystoreBuilder.createBuilder(BuildTargetFactory.newInstance("//:keystore")).setStore(new FakeSourcePath("store")).setProperties(new FakeSourcePath("properties")).build();
BuildTarget target = BuildTargetFactory.newInstance("//:rule");
TargetNode<?, ?> androidBinaryNode = AndroidBinaryBuilder.createBuilder(target).setManifest(new FakeSourcePath("manifest.xml")).setKeystore(BuildTargetFactory.newInstance("//:keystore")).setNoDx(ImmutableSet.of(transitiveDepNode.getBuildTarget())).setOriginalDeps(ImmutableSortedSet.of(depNode.getBuildTarget())).build();
TargetGraph targetGraph = TargetGraphFactory.newInstance(transitiveDepNode, depNode, keystoreNode, androidBinaryNode);
BuildRuleResolver ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
BuildRule transitiveDep = ruleResolver.requireRule(transitiveDepNode.getBuildTarget());
AndroidBinary androidBinary = (AndroidBinary) ruleResolver.requireRule(target);
assertThat(androidBinary.getDeps(), Matchers.hasItem(transitiveDep));
}
use of com.facebook.buck.rules.TargetGraph in project buck by facebook.
the class AndroidLibraryDescriptionTest method rulesMatchingDepQueryBecomeFirstOrderDeps.
@Test
public void rulesMatchingDepQueryBecomeFirstOrderDeps() throws Exception {
// Set up target graph: rule -> lib -> sublib -> bottom
TargetNode<JavaLibraryDescription.Arg, JavaLibraryDescription> bottomNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:bottom")).build();
TargetNode<JavaLibraryDescription.Arg, JavaLibraryDescription> sublibNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:sublib")).addDep(bottomNode.getBuildTarget()).build();
TargetNode<JavaLibraryDescription.Arg, JavaLibraryDescription> libNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:lib")).addDep(sublibNode.getBuildTarget()).build();
TargetGraph targetGraph = TargetGraphFactory.newInstance(bottomNode, libNode, sublibNode);
BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
FakeBuildRule bottomRule = resolver.addToIndex(new FakeBuildRule(bottomNode.getBuildTarget(), pathResolver));
FakeBuildRule sublibRule = resolver.addToIndex(new FakeBuildRule(sublibNode.getBuildTarget(), pathResolver, ImmutableSortedSet.of(bottomRule)));
FakeBuildRule libRule = resolver.addToIndex(new FakeBuildRule(libNode.getBuildTarget(), pathResolver, ImmutableSortedSet.of(sublibRule)));
BuildTarget target = BuildTargetFactory.newInstance("//:rule");
BuildRule javaLibrary = AndroidLibraryBuilder.createBuilder(target).addDep(libNode.getBuildTarget()).setDepsQuery(Query.of("filter('.*lib', deps($declared_deps))")).build(resolver, targetGraph);
assertThat(javaLibrary.getDeps(), Matchers.hasItems(libRule, sublibRule));
// The bottom rule should be filtered since it does not match the regex
assertThat(javaLibrary.getDeps(), Matchers.not(Matchers.hasItem(bottomRule)));
}
use of com.facebook.buck.rules.TargetGraph in project buck by facebook.
the class AndroidPackageableCollectorTest method testFindTransitiveDependencies.
/**
* This is a regression test to ensure that an additional 1 second startup cost is not
* re-introduced to fb4a.
*/
@Test
public void testFindTransitiveDependencies() throws Exception {
ProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
Path prebuiltNativeLibraryPath = Paths.get("java/com/facebook/prebuilt_native_library/libs");
projectFilesystem.mkdirs(prebuiltNativeLibraryPath);
// Create an AndroidBinaryRule that transitively depends on two prebuilt JARs. One of the two
// prebuilt JARs will be listed in the AndroidBinaryRule's no_dx list.
BuildTarget guavaTarget = BuildTargetFactory.newInstance("//third_party/guava:guava");
TargetNode<?, ?> guava = PrebuiltJarBuilder.createBuilder(guavaTarget).setBinaryJar(Paths.get("third_party/guava/guava-10.0.1.jar")).build();
BuildTarget jsr305Target = BuildTargetFactory.newInstance("//third_party/jsr-305:jsr-305");
TargetNode<?, ?> jsr = PrebuiltJarBuilder.createBuilder(jsr305Target).setBinaryJar(Paths.get("third_party/jsr-305/jsr305.jar")).build();
TargetNode<?, ?> ndkLibrary = new NdkLibraryBuilder(BuildTargetFactory.newInstance("//java/com/facebook/native_library:library"), projectFilesystem).build();
BuildTarget prebuiltNativeLibraryTarget = BuildTargetFactory.newInstance("//java/com/facebook/prebuilt_native_library:library");
TargetNode<?, ?> prebuiltNativeLibraryBuild = PrebuiltNativeLibraryBuilder.newBuilder(prebuiltNativeLibraryTarget, projectFilesystem).setNativeLibs(prebuiltNativeLibraryPath).setIsAsset(true).build();
BuildTarget libraryRuleTarget = BuildTargetFactory.newInstance("//java/src/com/facebook:example");
TargetNode<?, ?> library = JavaLibraryBuilder.createBuilder(libraryRuleTarget).setProguardConfig(new FakeSourcePath("debug.pro")).addSrc(Paths.get("Example.java")).addDep(guavaTarget).addDep(jsr305Target).addDep(prebuiltNativeLibraryBuild.getBuildTarget()).addDep(ndkLibrary.getBuildTarget()).build();
BuildTarget manifestTarget = BuildTargetFactory.newInstance("//java/src/com/facebook:res");
TargetNode<?, ?> manifest = AndroidResourceBuilder.createBuilder(manifestTarget).setManifest(new PathSourcePath(projectFilesystem, Paths.get("java/src/com/facebook/module/AndroidManifest.xml"))).setAssets(new FakeSourcePath("assets")).build();
BuildTarget keystoreTarget = BuildTargetFactory.newInstance("//keystore:debug");
TargetNode<?, ?> keystore = KeystoreBuilder.createBuilder(keystoreTarget).setStore(new FakeSourcePath(projectFilesystem, "keystore/debug.keystore")).setProperties(new FakeSourcePath(projectFilesystem, "keystore/debug.keystore.properties")).build();
ImmutableSortedSet<BuildTarget> originalDepsTargets = ImmutableSortedSet.of(libraryRuleTarget, manifestTarget);
BuildTarget binaryTarget = BuildTargetFactory.newInstance("//java/src/com/facebook:app");
TargetNode<?, ?> binary = AndroidBinaryBuilder.createBuilder(binaryTarget).setOriginalDeps(originalDepsTargets).setBuildTargetsToExcludeFromDex(ImmutableSet.of(BuildTargetFactory.newInstance("//third_party/guava:guava"))).setManifest(new FakeSourcePath("java/src/com/facebook/AndroidManifest.xml")).setKeystore(keystoreTarget).build();
TargetGraph targetGraph = TargetGraphFactory.newInstance(binary, library, manifest, keystore, ndkLibrary, prebuiltNativeLibraryBuild, guava, jsr);
BuildRuleResolver ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(ruleResolver));
AndroidBinary binaryRule = (AndroidBinary) ruleResolver.requireRule(binaryTarget);
NdkLibrary ndkLibraryRule = (NdkLibrary) ruleResolver.requireRule(ndkLibrary.getBuildTarget());
NativeLibraryBuildRule prebuildNativeLibraryRule = (NativeLibraryBuildRule) ruleResolver.requireRule(prebuiltNativeLibraryTarget);
// Verify that the correct transitive dependencies are found.
AndroidPackageableCollection packageableCollection = binaryRule.getAndroidPackageableCollection();
assertResolvedEquals("Because guava was passed to no_dx, it should not be in the classpathEntriesToDex list", pathResolver, ImmutableSet.of(ruleResolver.getRule(jsr305Target).getSourcePathToOutput(), ruleResolver.getRule(libraryRuleTarget).getSourcePathToOutput()), packageableCollection.getClasspathEntriesToDex());
assertResolvedEquals("Because guava was passed to no_dx, it should not be treated as a third-party JAR whose " + "resources need to be extracted and repacked in the APK. If this is done, then code " + "in the guava-10.0.1.dex.1.jar in the APK's assets/ tmp may try to load the resource " + "from the APK as a ZipFileEntry rather than as a resource within " + "guava-10.0.1.dex.1.jar. Loading a resource in this way could take substantially " + "longer. Specifically, this was observed to take over one second longer to load " + "the resource in fb4a. Because the resource was loaded on startup, this introduced a " + "substantial regression in the startup time for the fb4a app.", pathResolver, ImmutableSet.of(ruleResolver.getRule(jsr305Target).getSourcePathToOutput()), packageableCollection.getPathsToThirdPartyJars());
assertResolvedEquals("Because assets directory was passed an AndroidResourceRule it should be added to the " + "transitive dependencies", pathResolver, ImmutableSet.of(new DefaultBuildTargetSourcePath(manifestTarget.withAppendedFlavors(AndroidResourceDescription.ASSETS_SYMLINK_TREE_FLAVOR))), packageableCollection.getAssetsDirectories());
assertResolvedEquals("Because a native library was declared as a dependency, it should be added to the " + "transitive dependencies.", pathResolver, ImmutableSet.<SourcePath>of(new PathSourcePath(new FakeProjectFilesystem(), ndkLibraryRule.getLibraryPath())), ImmutableSet.copyOf(packageableCollection.getNativeLibsDirectories().values()));
assertResolvedEquals("Because a prebuilt native library was declared as a dependency (and asset), it should " + "be added to the transitive dependecies.", pathResolver, ImmutableSet.<SourcePath>of(new PathSourcePath(new FakeProjectFilesystem(), prebuildNativeLibraryRule.getLibraryPath())), ImmutableSet.copyOf(packageableCollection.getNativeLibAssetsDirectories().values()));
assertEquals(ImmutableSet.of(new FakeSourcePath("debug.pro")), packageableCollection.getProguardConfigs());
}
Aggregations