use of com.facebook.buck.rules.SourcePath in project buck by facebook.
the class AndroidManifestDescription method createBuildRule.
@Override
public <A extends Arg> AndroidManifest createBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver resolver, A args) {
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
AndroidTransitiveDependencyGraph transitiveDependencyGraph = new AndroidTransitiveDependencyGraph(resolver.getAllRules(args.deps));
ImmutableSet<SourcePath> manifestFiles = transitiveDependencyGraph.findManifestFiles();
// The only rules that need to be built before this AndroidManifest are those
// responsible for generating the AndroidManifest.xml files in the manifestFiles set (and
// possibly the skeleton).
//
// If the skeleton is a BuildTargetSourcePath, then its build rule must also be in the deps.
// The skeleton does not appear to be in either params.getDeclaredDeps() or
// params.getExtraDeps(), even though the type of Arg.skeleton is SourcePath.
// TODO(shs96c): t4744625 This should happen automagically.
ImmutableSortedSet<BuildRule> newDeps = ImmutableSortedSet.<BuildRule>naturalOrder().addAll(ruleFinder.filterBuildRuleInputs(Sets.union(manifestFiles, Collections.singleton(args.skeleton)))).build();
return new AndroidManifest(params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(newDeps), params.getExtraDeps()), args.skeleton, manifestFiles);
}
use of com.facebook.buck.rules.SourcePath in project buck by facebook.
the class AndroidNativeLibsPackageableGraphEnhancer method enhance.
public AndroidNativeLibsGraphEnhancementResult enhance(AndroidPackageableCollection packageableCollection) throws NoSuchBuildTargetException {
@SuppressWarnings("PMD.PrematureDeclaration") AndroidNativeLibsGraphEnhancementResult.Builder resultBuilder = AndroidNativeLibsGraphEnhancementResult.builder();
ImmutableMultimap<APKModule, NativeLinkable> nativeLinkables = packageableCollection.getNativeLinkables();
ImmutableMultimap<APKModule, NativeLinkable> nativeLinkablesAssets = packageableCollection.getNativeLinkablesAssets();
if (nativeLibraryMergeMap.isPresent() && !nativeLibraryMergeMap.get().isEmpty()) {
NativeLibraryMergeEnhancementResult enhancement = NativeLibraryMergeEnhancer.enhance(cxxBuckConfig, ruleResolver, pathResolver, ruleFinder, buildRuleParams, nativePlatforms, nativeLibraryMergeMap.get(), nativeLibraryMergeGlue, nativeLinkables, nativeLinkablesAssets);
nativeLinkables = enhancement.getMergedLinkables();
nativeLinkablesAssets = enhancement.getMergedLinkablesAssets();
resultBuilder.setSonameMergeMap(enhancement.getSonameMapping());
}
// Iterate over all the {@link AndroidNativeLinkable}s from the collector and grab the shared
// libraries for all the {@link TargetCpuType}s that we care about. We deposit them into a map
// of CPU type and SONAME to the shared library path, which the {@link CopyNativeLibraries}
// rule will use to compose the destination name.
ImmutableMap.Builder<APKModule, CopyNativeLibraries> moduleMappedCopyNativeLibriesBuilder = ImmutableMap.builder();
boolean hasCopyNativeLibraries = false;
List<NdkCxxPlatform> platformsWithNativeLibs = new ArrayList<>();
List<NdkCxxPlatform> platformsWithNativeLibsAssets = new ArrayList<>();
// Make sure we process the root module last so that we know if any of the module contain
// libraries that depend on a non-system runtime and add it to the root module if needed.
ImmutableSet<APKModule> apkModules = FluentIterable.from(apkModuleGraph.getAPKModules()).filter(input -> !input.isRootModule()).append(apkModuleGraph.getRootAPKModule()).toSet();
for (APKModule module : apkModules) {
ImmutableMap.Builder<Pair<NdkCxxPlatforms.TargetCpuType, String>, SourcePath> nativeLinkableLibsBuilder = ImmutableMap.builder();
ImmutableMap.Builder<Pair<NdkCxxPlatforms.TargetCpuType, String>, SourcePath> nativeLinkableLibsAssetsBuilder = ImmutableMap.builder();
// TODO(andrewjcg): We currently treat an empty set of filters to mean to allow everything.
// We should fix this by assigning a default list of CPU filters in the descriptions, but
// until we do, if the set of filters is empty, just build for all available platforms.
ImmutableSet<NdkCxxPlatforms.TargetCpuType> filters = cpuFilters.isEmpty() ? nativePlatforms.keySet() : cpuFilters;
for (NdkCxxPlatforms.TargetCpuType targetCpuType : filters) {
NdkCxxPlatform platform = Preconditions.checkNotNull(nativePlatforms.get(targetCpuType), "Unknown platform type " + targetCpuType.toString());
// Populate nativeLinkableLibs and nativeLinkableLibsAssets with the appropriate entries.
if (populateMapWithLinkables(nativeLinkables.get(module), nativeLinkableLibsBuilder, targetCpuType, platform) && !platformsWithNativeLibs.contains(platform)) {
platformsWithNativeLibs.add(platform);
}
if (populateMapWithLinkables(nativeLinkablesAssets.get(module), nativeLinkableLibsAssetsBuilder, targetCpuType, platform) && !platformsWithNativeLibsAssets.contains(platform)) {
platformsWithNativeLibsAssets.add(platform);
}
if (module.isRootModule()) {
// If we're using a C/C++ runtime other than the system one, add it to the APK.
NdkCxxPlatforms.CxxRuntime cxxRuntime = platform.getCxxRuntime();
if ((platformsWithNativeLibs.contains(platform) || platformsWithNativeLibsAssets.contains(platform)) && !cxxRuntime.equals(NdkCxxPlatforms.CxxRuntime.SYSTEM)) {
nativeLinkableLibsBuilder.put(new Pair<>(targetCpuType, cxxRuntime.getSoname()), new PathSourcePath(buildRuleParams.getProjectFilesystem(), platform.getCxxSharedRuntimePath().get()));
}
}
}
ImmutableMap<Pair<NdkCxxPlatforms.TargetCpuType, String>, SourcePath> nativeLinkableLibs = nativeLinkableLibsBuilder.build();
ImmutableMap<Pair<NdkCxxPlatforms.TargetCpuType, String>, SourcePath> nativeLinkableLibsAssets = nativeLinkableLibsAssetsBuilder.build();
if (packageableCollection.getNativeLibsDirectories().get(module).isEmpty() && nativeLinkableLibs.isEmpty() && nativeLinkableLibsAssets.isEmpty()) {
continue;
}
if (relinkerMode == RelinkerMode.ENABLED && (!nativeLinkableLibs.isEmpty() || !nativeLinkableLibsAssets.isEmpty())) {
NativeRelinker relinker = new NativeRelinker(buildRuleParams.copyReplacingExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(ruleFinder.filterBuildRuleInputs(nativeLinkableLibs.values())).addAll(ruleFinder.filterBuildRuleInputs(nativeLinkableLibsAssets.values())).build())), pathResolver, ruleFinder, cxxBuckConfig, nativePlatforms, nativeLinkableLibs, nativeLinkableLibsAssets);
nativeLinkableLibs = relinker.getRelinkedLibs();
nativeLinkableLibsAssets = relinker.getRelinkedLibsAssets();
for (BuildRule rule : relinker.getRules()) {
ruleResolver.addToIndex(rule);
}
}
ImmutableMap<StripLinkable, StrippedObjectDescription> strippedLibsMap = generateStripRules(buildRuleParams, ruleFinder, ruleResolver, originalBuildTarget, nativePlatforms, nativeLinkableLibs);
ImmutableMap<StripLinkable, StrippedObjectDescription> strippedLibsAssetsMap = generateStripRules(buildRuleParams, ruleFinder, ruleResolver, originalBuildTarget, nativePlatforms, nativeLinkableLibsAssets);
ImmutableSortedSet<BuildRule> nativeLibsRules = BuildRules.toBuildRulesFor(originalBuildTarget, ruleResolver, packageableCollection.getNativeLibsTargets().get(module));
BuildRuleParams paramsForCopyNativeLibraries = buildRuleParams.withAppendedFlavor(InternalFlavor.of(COPY_NATIVE_LIBS + "_" + module.getName())).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(nativeLibsRules).addAll(ruleFinder.filterBuildRuleInputs(packageableCollection.getNativeLibsDirectories().get(module))).addAll(strippedLibsMap.keySet()).addAll(strippedLibsAssetsMap.keySet()).build()), Suppliers.ofInstance(ImmutableSortedSet.of()));
moduleMappedCopyNativeLibriesBuilder.put(module, new CopyNativeLibraries(paramsForCopyNativeLibraries, ImmutableSet.copyOf(packageableCollection.getNativeLibsDirectories().get(module)), ImmutableSet.copyOf(strippedLibsMap.values()), ImmutableSet.copyOf(strippedLibsAssetsMap.values()), cpuFilters, module.getName()));
hasCopyNativeLibraries = true;
}
return resultBuilder.setCopyNativeLibraries(hasCopyNativeLibraries ? Optional.of(moduleMappedCopyNativeLibriesBuilder.build()) : Optional.empty()).build();
}
use of com.facebook.buck.rules.SourcePath in project buck by facebook.
the class AndroidBinary method getStepsForNativeAssets.
private void getStepsForNativeAssets(SourcePathResolver resolver, ImmutableList.Builder<Step> steps, Optional<ImmutableCollection<SourcePath>> nativeLibDirs, final Path libSubdirectory, final String metadataFilename, final APKModule module) {
steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), libSubdirectory));
// Filter, rename and copy the ndk libraries marked as assets.
if (nativeLibDirs.isPresent()) {
for (SourcePath nativeLibDir : nativeLibDirs.get()) {
CopyNativeLibraries.copyNativeLibrary(getProjectFilesystem(), resolver.getAbsolutePath(nativeLibDir), libSubdirectory, cpuFilters, steps);
}
}
// Input asset libraries are sorted in descending filesize order.
final ImmutableSortedSet.Builder<Path> inputAssetLibrariesBuilder = ImmutableSortedSet.orderedBy((libPath1, libPath2) -> {
try {
ProjectFilesystem filesystem = getProjectFilesystem();
int filesizeResult = -Long.compare(filesystem.getFileSize(libPath1), filesystem.getFileSize(libPath2));
int pathnameResult = libPath1.compareTo(libPath2);
return filesizeResult != 0 ? filesizeResult : pathnameResult;
} catch (IOException e) {
return 0;
}
});
if (packageAssetLibraries || !module.isRootModule()) {
if (enhancementResult.getCopyNativeLibraries().isPresent() && enhancementResult.getCopyNativeLibraries().get().containsKey(module)) {
// Copy in cxx libraries marked as assets. Filtering and renaming was already done
// in CopyNativeLibraries.getBuildSteps().
Path cxxNativeLibsSrc = enhancementResult.getCopyNativeLibraries().get().get(module).getPathToNativeLibsAssetsDir();
steps.add(CopyStep.forDirectory(getProjectFilesystem(), cxxNativeLibsSrc, libSubdirectory, CopyStep.DirectoryMode.CONTENTS_ONLY));
}
steps.add(// Step that populates a list of libraries and writes a metadata.txt to decompress.
new AbstractExecutionStep("write_metadata_for_asset_libraries_" + module.getName()) {
@Override
public StepExecutionResult execute(ExecutionContext context) {
ProjectFilesystem filesystem = getProjectFilesystem();
try {
// Walk file tree to find libraries
filesystem.walkRelativeFileTree(libSubdirectory, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (!file.toString().endsWith(".so")) {
throw new IOException("unexpected file in lib directory");
}
inputAssetLibrariesBuilder.add(file);
return FileVisitResult.CONTINUE;
}
});
// Write a metadata
ImmutableList.Builder<String> metadataLines = ImmutableList.builder();
Path metadataOutput = libSubdirectory.resolve(metadataFilename);
for (Path libPath : inputAssetLibrariesBuilder.build()) {
// Should return something like x86/libfoo.so
Path relativeLibPath = libSubdirectory.relativize(libPath);
long filesize = filesystem.getFileSize(libPath);
String desiredOutput = relativeLibPath.toString();
String checksum = filesystem.computeSha256(libPath);
metadataLines.add(desiredOutput + ' ' + filesize + ' ' + checksum);
}
ImmutableList<String> metadata = metadataLines.build();
if (!metadata.isEmpty()) {
filesystem.writeLinesToPath(metadata, metadataOutput);
}
} catch (IOException e) {
context.logError(e, "Writing metadata for asset libraries failed.");
return StepExecutionResult.ERROR;
}
return StepExecutionResult.SUCCESS;
}
});
}
if (compressAssetLibraries || !module.isRootModule()) {
final ImmutableList.Builder<Path> outputAssetLibrariesBuilder = ImmutableList.builder();
steps.add(new AbstractExecutionStep("rename_asset_libraries_as_temp_files_" + module.getName()) {
@Override
public StepExecutionResult execute(ExecutionContext context) {
try {
ProjectFilesystem filesystem = getProjectFilesystem();
for (Path libPath : inputAssetLibrariesBuilder.build()) {
Path tempPath = libPath.resolveSibling(libPath.getFileName() + "~");
filesystem.move(libPath, tempPath);
outputAssetLibrariesBuilder.add(tempPath);
}
return StepExecutionResult.SUCCESS;
} catch (IOException e) {
context.logError(e, "Renaming asset libraries failed");
return StepExecutionResult.ERROR;
}
}
});
// Concat and xz compress.
Path libOutputBlob = libSubdirectory.resolve("libraries.blob");
steps.add(new ConcatStep(getProjectFilesystem(), outputAssetLibrariesBuilder, libOutputBlob));
int compressionLevel = xzCompressionLevel.orElse(XzStep.DEFAULT_COMPRESSION_LEVEL).intValue();
steps.add(new XzStep(getProjectFilesystem(), libOutputBlob, libSubdirectory.resolve(SOLID_COMPRESSED_ASSET_LIBRARY_FILENAME), compressionLevel));
}
}
use of com.facebook.buck.rules.SourcePath in project buck by facebook.
the class AppleTestDescription method getXctool.
private Optional<SourcePath> getXctool(BuildRuleParams params, BuildRuleResolver resolver) {
// can use that directly.
if (appleConfig.getXctoolZipTarget().isPresent()) {
final BuildRule xctoolZipBuildRule = resolver.getRule(appleConfig.getXctoolZipTarget().get());
BuildTarget unzipXctoolTarget = BuildTarget.builder(xctoolZipBuildRule.getBuildTarget()).addFlavors(UNZIP_XCTOOL_FLAVOR).build();
final Path outputDirectory = BuildTargets.getGenPath(params.getProjectFilesystem(), unzipXctoolTarget, "%s/unzipped");
if (!resolver.getRuleOptional(unzipXctoolTarget).isPresent()) {
BuildRuleParams unzipXctoolParams = params.withBuildTarget(unzipXctoolTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of(xctoolZipBuildRule)), Suppliers.ofInstance(ImmutableSortedSet.of()));
resolver.addToIndex(new AbstractBuildRule(unzipXctoolParams) {
@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
buildableContext.recordArtifact(outputDirectory);
return ImmutableList.of(new MakeCleanDirectoryStep(getProjectFilesystem(), outputDirectory), new UnzipStep(getProjectFilesystem(), context.getSourcePathResolver().getAbsolutePath(Preconditions.checkNotNull(xctoolZipBuildRule.getSourcePathToOutput())), outputDirectory));
}
@Override
public SourcePath getSourcePathToOutput() {
return new ExplicitBuildTargetSourcePath(getBuildTarget(), outputDirectory);
}
});
}
return Optional.of(new ExplicitBuildTargetSourcePath(unzipXctoolTarget, outputDirectory.resolve("bin/xctool")));
} else if (appleConfig.getXctoolPath().isPresent()) {
return Optional.of(new PathSourcePath(params.getProjectFilesystem(), appleConfig.getXctoolPath().get()));
} else {
return Optional.empty();
}
}
use of com.facebook.buck.rules.SourcePath in project buck by facebook.
the class AppleTestDescription method createTestHostInfo.
private TestHostInfo createTestHostInfo(BuildRuleParams params, BuildRuleResolver resolver, BuildTarget testHostAppBuildTarget, AppleDebugFormat debugFormat, Iterable<Flavor> additionalFlavors, ImmutableList<CxxPlatform> cxxPlatforms) throws NoSuchBuildTargetException {
BuildRule rule = resolver.requireRule(BuildTarget.builder(testHostAppBuildTarget).addAllFlavors(additionalFlavors).addFlavors(debugFormat.getFlavor()).addFlavors(StripStyle.NON_GLOBAL_SYMBOLS.getFlavor()).build());
if (!(rule instanceof AppleBundle)) {
throw new HumanReadableException("Apple test rule '%s' has test_host_app '%s' not of type '%s'.", params.getBuildTarget(), testHostAppBuildTarget, Description.getBuildRuleType(AppleBundleDescription.class));
}
AppleBundle testHostApp = (AppleBundle) rule;
SourcePath testHostAppBinarySourcePath = testHostApp.getBinaryBuildRule().getSourcePathToOutput();
ImmutableMap<BuildTarget, NativeLinkable> roots = NativeLinkables.getNativeLinkableRoots(testHostApp.getBinary().get().getDeps(), x -> true);
// Union the blacklist of all the platforms. This should give a superset for each particular
// platform, which should be acceptable as items in the blacklist thare are unmatched are simply
// ignored.
ImmutableSet.Builder<BuildTarget> blacklistBuilder = ImmutableSet.builder();
for (CxxPlatform platform : cxxPlatforms) {
blacklistBuilder.addAll(NativeLinkables.getTransitiveNativeLinkables(platform, roots.values()).keySet());
}
return TestHostInfo.of(testHostApp, testHostAppBinarySourcePath, blacklistBuilder.build());
}
Aggregations