use of com.facebook.buck.model.Pair in project buck by facebook.
the class ProjectTest method createActionGraphForTesting.
/**
* Creates an ActionGraph with two android_binary rules, each of which depends on the same
* android_library. The difference between the two is that one lists Guava in its no_dx list and
* the other does not.
* <p>
* The ActionGraph also includes three project_config rules: one for the android_library, and one
* for each of the android_binary rules.
*/
public Pair<ProjectWithModules, BuildRuleResolver> createActionGraphForTesting(@Nullable JavaPackageFinder javaPackageFinder) throws Exception {
ImmutableSet.Builder<TargetNode<?, ?>> nodes = ImmutableSet.builder();
// prebuilt_jar //third_party/guava:guava
TargetNode<?, ?> guavaNode = PrebuiltJarBuilder.createBuilder(BuildTargetFactory.newInstance("//third_party/guava:guava")).setBinaryJar(PATH_TO_GUAVA_JAR).build();
nodes.add(guavaNode);
// android_resouce android_res/base:res
TargetNode<?, ?> androidResNode = AndroidResourceBuilder.createBuilder(BuildTargetFactory.newInstance("//android_res/base:res")).setRes(new FakeSourcePath("android_res/base/res")).setRDotJavaPackage("com.facebook").build();
nodes.add(androidResNode);
// project_config android_res/base:res
TargetNode<?, ?> projectConfigForResourceNode = ProjectConfigBuilder.createBuilder(BuildTargetFactory.newInstance("//android_res/base:project_config")).setSrcRule(androidResNode.getBuildTarget()).setSrcRoots(ImmutableList.of("res")).build();
nodes.add(projectConfigForResourceNode);
// java_library //java/src/com/facebook/grandchild:grandchild
BuildTarget grandchildTarget = BuildTargetFactory.newInstance("//java/src/com/facebook/grandchild:grandchild");
TargetNode<?, ?> grandchildNode = JavaLibraryBuilder.createBuilder(grandchildTarget).addSrc(Paths.get("Grandchild.java")).build();
nodes.add(grandchildNode);
// java_library //java/src/com/facebook/child:child
TargetNode<?, ?> childNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//java/src/com/facebook/child:child")).addSrc(Paths.get("Child.java")).addDep(grandchildNode.getBuildTarget()).build();
nodes.add(childNode);
// java_library //java/src/com/facebook/exportlib:exportlib
TargetNode<?, ?> exportLibNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//java/src/com/facebook/exportlib:exportlib")).addSrc(Paths.get("ExportLib.java")).addDep(guavaNode.getBuildTarget()).addExportedDep(guavaNode.getBuildTarget()).build();
nodes.add(exportLibNode);
// android_library //java/src/com/facebook/base:base
TargetNode<?, ?> baseNode = AndroidLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//java/src/com/facebook/base:base")).addSrc(Paths.get("Base.java")).addDep(exportLibNode.getBuildTarget()).addDep(childNode.getBuildTarget()).addDep(androidResNode.getBuildTarget()).build();
nodes.add(baseNode);
// project_config //java/src/com/facebook/base:project_config
TargetNode<?, ?> projectConfigForLibraryNode = ProjectConfigBuilder.createBuilder(BuildTargetFactory.newInstance("//java/src/com/facebook/base:project_config")).setSrcRule(baseNode.getBuildTarget()).setSrcRoots(ImmutableList.of("src", "src-gen")).build();
nodes.add(projectConfigForLibraryNode);
TargetNode<?, ?> projectConfigForExportLibraryNode = ProjectConfigBuilder.createBuilder(BuildTargetFactory.newInstance("//java/src/com/facebook/exportlib:project_config")).setSrcRule(exportLibNode.getBuildTarget()).setSrcRoots(ImmutableList.of("src")).build();
nodes.add(projectConfigForExportLibraryNode);
// keystore //keystore:debug
BuildTarget keystoreTarget = BuildTargetFactory.newInstance("//keystore:debug");
TargetNode<?, ?> keystoreNode = KeystoreBuilder.createBuilder(keystoreTarget).setStore(new FakeSourcePath("keystore/debug.keystore")).setProperties(new FakeSourcePath("keystore/debug.keystore.properties")).build();
nodes.add(keystoreNode);
// android_binary //foo:app
ImmutableSortedSet<BuildTarget> androidBinaryRuleDepsTarget = ImmutableSortedSet.of(baseNode.getBuildTarget());
TargetNode<?, ?> androidBinaryNode = AndroidBinaryBuilder.createBuilder(BuildTargetFactory.newInstance("//foo:app")).setOriginalDeps(androidBinaryRuleDepsTarget).setManifest(new FakeSourcePath("foo/AndroidManifest.xml")).setKeystore(keystoreNode.getBuildTarget()).setBuildTargetsToExcludeFromDex(ImmutableSet.of(BuildTargetFactory.newInstance("//third_party/guava:guava"))).build();
nodes.add(androidBinaryNode);
// project_config //foo:project_config
TargetNode<?, ?> projectConfigUsingNoDxNode = ProjectConfigBuilder.createBuilder(BuildTargetFactory.newInstance("//foo:project_config")).setSrcRule(androidBinaryNode.getBuildTarget()).build();
nodes.add(projectConfigUsingNoDxNode);
// android_binary //bar:app
ImmutableSortedSet<BuildTarget> barAppBuildRuleDepsTarget = ImmutableSortedSet.of(baseNode.getBuildTarget());
TargetNode<?, ?> barAppBuildNode = AndroidBinaryBuilder.createBuilder(BuildTargetFactory.newInstance("//bar:app")).setOriginalDeps(barAppBuildRuleDepsTarget).setManifest(new FakeSourcePath("foo/AndroidManifest.xml")).setKeystore(keystoreNode.getBuildTarget()).build();
nodes.add(barAppBuildNode);
// project_config //bar:project_config
TargetNode<?, ?> projectConfigNode = ProjectConfigBuilder.createBuilder(BuildTargetFactory.newInstance("//bar:project_config")).setSrcRule(barAppBuildNode.getBuildTarget()).build();
nodes.add(projectConfigNode);
TargetGraph targetGraph = TargetGraphFactory.newInstance(nodes.build());
BuildRuleResolver ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
guava = ruleResolver.requireRule(guavaNode.getBuildTarget());
ProjectConfig projectConfigForExportLibrary = (ProjectConfig) ruleResolver.requireRule(projectConfigForExportLibraryNode.getBuildTarget());
ProjectConfig projectConfigForLibrary = (ProjectConfig) ruleResolver.requireRule(projectConfigForLibraryNode.getBuildTarget());
ProjectConfig projectConfigForResource = (ProjectConfig) ruleResolver.requireRule(projectConfigForResourceNode.getBuildTarget());
ProjectConfig projectConfigUsingNoDx = (ProjectConfig) ruleResolver.requireRule(projectConfigUsingNoDxNode.getBuildTarget());
ProjectConfig projectConfig = (ProjectConfig) ruleResolver.requireRule(projectConfigNode.getBuildTarget());
return new Pair<>(getModulesForActionGraph(ruleResolver, ImmutableSortedSet.of(projectConfigForExportLibrary, projectConfigForLibrary, projectConfigForResource, projectConfigUsingNoDx, projectConfig), javaPackageFinder, null), ruleResolver);
}
use of com.facebook.buck.model.Pair in project buck by facebook.
the class TargetNodeTranslatorTest method translatePair.
@Test
public void translatePair() {
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.translatePair(CELL_PATH_RESOLVER, PATTERN, new Pair<>("hello", a)), Matchers.equalTo(Optional.of(new Pair<>("hello", b))));
}
use of com.facebook.buck.model.Pair in project buck by facebook.
the class AndroidNativeLibsPackageableGraphEnhancer method generateStripRules.
// Note: this method produces rules that will be shared between multiple apps,
// so be careful not to let information about this particular app slip into the definitions.
private static ImmutableMap<StripLinkable, StrippedObjectDescription> generateStripRules(BuildRuleParams buildRuleParams, SourcePathRuleFinder ruleFinder, BuildRuleResolver ruleResolver, BuildTarget appRuleTarget, ImmutableMap<NdkCxxPlatforms.TargetCpuType, NdkCxxPlatform> nativePlatforms, ImmutableMap<Pair<NdkCxxPlatforms.TargetCpuType, String>, SourcePath> libs) {
ImmutableMap.Builder<StripLinkable, StrippedObjectDescription> result = ImmutableMap.builder();
for (Map.Entry<Pair<NdkCxxPlatforms.TargetCpuType, String>, SourcePath> entry : libs.entrySet()) {
SourcePath sourcePath = entry.getValue();
NdkCxxPlatforms.TargetCpuType targetCpuType = entry.getKey().getFirst();
NdkCxxPlatform platform = Preconditions.checkNotNull(nativePlatforms.get(targetCpuType));
// To be safe, default to using the app rule target as the base for the strip rule.
// This will be used for stripping the C++ runtime. We could use something more easily
// shareable (like just using the app's containing directory, or even the repo root),
// but stripping the C++ runtime is pretty fast, so just keep the safe old behavior for now.
BuildTarget baseBuildTarget = appRuleTarget;
// to allow sharing the rule between all apps that depend on it.
if (sourcePath instanceof BuildTargetSourcePath) {
baseBuildTarget = ((BuildTargetSourcePath<?>) sourcePath).getTarget();
}
String sharedLibrarySoName = entry.getKey().getSecond();
BuildTarget targetForStripRule = BuildTarget.builder(baseBuildTarget).addFlavors(InternalFlavor.of("android-strip")).addFlavors(InternalFlavor.of(Flavor.replaceInvalidCharacters(sharedLibrarySoName))).addFlavors(InternalFlavor.of(Flavor.replaceInvalidCharacters(targetCpuType.name()))).build();
Optional<BuildRule> previouslyCreated = ruleResolver.getRuleOptional(targetForStripRule);
StripLinkable stripLinkable;
if (previouslyCreated.isPresent()) {
stripLinkable = (StripLinkable) previouslyCreated.get();
} else {
BuildRuleParams paramsForStripLinkable = buildRuleParams.withBuildTarget(targetForStripRule).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(ruleFinder.filterBuildRuleInputs(ImmutableList.of(sourcePath))).build()), Suppliers.ofInstance(ImmutableSortedSet.of()));
stripLinkable = new StripLinkable(paramsForStripLinkable, platform.getCxxPlatform().getStrip(), sourcePath, sharedLibrarySoName);
ruleResolver.addToIndex(stripLinkable);
}
result.put(stripLinkable, StrippedObjectDescription.builder().setSourcePath(stripLinkable.getSourcePathToOutput()).setStrippedObjectName(sharedLibrarySoName).setTargetCpuType(targetCpuType).build());
}
return result.build();
}
use of com.facebook.buck.model.Pair in project buck by facebook.
the class DistBuildRunCommand method getBuildJobStateAndBuildName.
public Pair<BuildJobState, String> getBuildJobStateAndBuildName(ProjectFilesystem filesystem, Console console, DistBuildService service) throws IOException {
if (buildStateFile != null) {
Path buildStateFilePath = Paths.get(buildStateFile);
console.getStdOut().println(String.format("Retrieving BuildJobState for from file [%s].", buildStateFilePath));
return new Pair<>(BuildJobStateSerializer.deserialize(filesystem.newFileInputStream(buildStateFilePath)), String.format("LocalFile=[%s]", buildStateFile));
} else {
StampedeId stampedeId = getStampedeId();
console.getStdOut().println(String.format("Retrieving BuildJobState for build [%s].", stampedeId));
return new Pair<>(service.fetchBuildJobState(stampedeId), String.format("DistBuild=[%s]", stampedeId.toString()));
}
}
use of com.facebook.buck.model.Pair in project buck by facebook.
the class JarDirectoryStepHelper method addFilesInDirectoryToJar.
/**
* @param directory that must not contain symlinks with loops.
* @param jar is the file being written.
*/
private static void addFilesInDirectoryToJar(final ProjectFilesystem filesystem, final Path directory, CustomZipOutputStream jar, final Set<String> alreadyAddedEntries, final Iterable<Pattern> blacklist, final JavacEventSink eventSink) throws IOException {
// Since filesystem traversals can be non-deterministic, sort the entries we find into
// a tree map before writing them out.
final Map<String, Pair<JarEntry, Optional<Path>>> entries = Maps.newTreeMap();
filesystem.walkFileTree(directory, EnumSet.of(FileVisitOption.FOLLOW_LINKS), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String relativePath = MorePaths.pathWithUnixSeparators(MorePaths.relativize(directory, file));
// Skip re-reading the manifest
if (JarFile.MANIFEST_NAME.equals(relativePath)) {
return FileVisitResult.CONTINUE;
}
// Check if the entry belongs to the blacklist and it should be excluded from the Jar.
if (shouldEntryBeRemovedFromJar(eventSink, relativePath, blacklist)) {
return FileVisitResult.CONTINUE;
}
JarEntry entry = new JarEntry(relativePath);
String entryName = entry.getName();
// We want deterministic JARs, so avoid mtimes.
entry.setTime(ZipConstants.getFakeTime());
// those repeatedly would be lame, so don't do that.
if (!isDuplicateAllowed(entryName) && !alreadyAddedEntries.add(entryName)) {
if (!entryName.endsWith("/")) {
eventSink.reportEvent(determineSeverity(entry), "Duplicate found when adding directory to jar: %s", relativePath);
}
return FileVisitResult.CONTINUE;
}
entries.put(entry.getName(), new Pair<>(entry, Optional.of(file)));
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
String relativePath = MorePaths.pathWithUnixSeparators(MorePaths.relativize(directory, dir));
if (relativePath.isEmpty()) {
// root of the tree. Skip.
return FileVisitResult.CONTINUE;
}
String entryName = relativePath.replace('\\', '/') + "/";
if (alreadyAddedEntries.contains(entryName)) {
return FileVisitResult.CONTINUE;
}
JarEntry entry = new JarEntry(entryName);
// We want deterministic JARs, so avoid mtimes.
entry.setTime(ZipConstants.getFakeTime());
entries.put(entry.getName(), new Pair<>(entry, Optional.empty()));
return FileVisitResult.CONTINUE;
}
});
// Write the entries out using the iteration order of the tree map above.
for (Pair<JarEntry, Optional<Path>> entry : entries.values()) {
jar.putNextEntry(entry.getFirst());
if (entry.getSecond().isPresent()) {
Files.copy(entry.getSecond().get(), jar);
}
jar.closeEntry();
}
}
Aggregations