use of com.facebook.buck.rules.BuildRuleParams in project buck by facebook.
the class AndroidResourceDescription method createSymlinkTree.
private SymlinkTree createSymlinkTree(SourcePathRuleFinder ruleFinder, BuildRuleParams params, Optional<Either<SourcePath, ImmutableSortedMap<String, SourcePath>>> symlinkAttribute, String outputDirName) {
ImmutableMap<Path, SourcePath> links = ImmutableMap.of();
if (symlinkAttribute.isPresent()) {
if (symlinkAttribute.get().isLeft()) {
// If our resources are coming from a `PathSourcePath`, we collect only the inputs we care
// about and pass those in separately, so that that `AndroidResource` rule knows to only
// hash these into it's rule key.
// TODO(k21): This is deprecated and should be disabled or removed.
// Accessing the filesystem during rule creation is problematic because the accesses are
// not cached or tracked in any way.
Preconditions.checkArgument(symlinkAttribute.get().getLeft() instanceof PathSourcePath, "Resource or asset symlink tree can only be built for a PathSourcePath");
PathSourcePath path = (PathSourcePath) symlinkAttribute.get().getLeft();
links = collectInputFiles(path.getFilesystem(), path.getRelativePath());
} else {
links = RichStream.from(symlinkAttribute.get().getRight().entrySet()).map(e -> new AbstractMap.SimpleEntry<>(Paths.get(e.getKey()), e.getValue())).filter(e -> isPossibleResourcePath(e.getKey())).collect(MoreCollectors.toImmutableMap(e -> e.getKey(), e -> e.getValue()));
}
}
Path symlinkTreeRoot = BuildTargets.getGenPath(params.getProjectFilesystem(), params.getBuildTarget(), "%s").resolve(outputDirName);
return new SymlinkTree(params.getBuildTarget(), params.getProjectFilesystem(), symlinkTreeRoot, links, ruleFinder);
}
use of com.facebook.buck.rules.BuildRuleParams in project buck by facebook.
the class AndroidLibraryDescription method createBuildRule.
@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
if (params.getBuildTarget().getFlavors().contains(JavaLibrary.SRC_JAR)) {
return new JavaSourceJar(params, args.srcs, args.mavenCoords);
}
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
JavacOptions javacOptions = JavacOptionsFactory.create(defaultOptions, params, resolver, ruleFinder, args);
final ImmutableSet.Builder<BuildRule> queriedDepsBuilder = ImmutableSet.builder();
if (args.depsQuery.isPresent()) {
queriedDepsBuilder.addAll(QueryUtils.resolveDepQuery(params, args.depsQuery.get(), resolver, targetGraph).collect(Collectors.toList()));
}
final ImmutableSet<BuildRule> queriedDeps = queriedDepsBuilder.build();
AndroidLibraryGraphEnhancer graphEnhancer = new AndroidLibraryGraphEnhancer(params.getBuildTarget(), params.copyReplacingExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(queriedDeps).addAll(resolver.getAllRules(args.exportedDeps)).build())), javacOptions, DependencyMode.FIRST_ORDER, /* forceFinalResourceIds */
false, args.resourceUnionPackage, args.finalRName, false);
boolean hasDummyRDotJavaFlavor = params.getBuildTarget().getFlavors().contains(DUMMY_R_DOT_JAVA_FLAVOR);
if (CalculateAbi.isAbiTarget(params.getBuildTarget())) {
if (hasDummyRDotJavaFlavor) {
return graphEnhancer.getBuildableForAndroidResourcesAbi(resolver, ruleFinder);
}
BuildTarget libraryTarget = CalculateAbi.getLibraryTarget(params.getBuildTarget());
BuildRule libraryRule = resolver.requireRule(libraryTarget);
return CalculateAbi.of(params.getBuildTarget(), ruleFinder, params, Preconditions.checkNotNull(libraryRule.getSourcePathToOutput()));
}
Optional<DummyRDotJava> dummyRDotJava = graphEnhancer.getBuildableForAndroidResources(resolver, /* createBuildableIfEmpty */
hasDummyRDotJavaFlavor);
if (hasDummyRDotJavaFlavor) {
return dummyRDotJava.get();
} else {
ImmutableSet<Either<SourcePath, Path>> additionalClasspathEntries = ImmutableSet.of();
if (dummyRDotJava.isPresent()) {
additionalClasspathEntries = ImmutableSet.of(Either.ofLeft(dummyRDotJava.get().getSourcePathToOutput()));
ImmutableSortedSet<BuildRule> newDeclaredDeps = ImmutableSortedSet.<BuildRule>naturalOrder().addAll(params.getDeclaredDeps().get()).add(dummyRDotJava.get()).build();
params = params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(newDeclaredDeps), params.getExtraDeps());
}
AndroidLibraryCompiler compiler = compilerFactory.getCompiler(args.language.orElse(JvmLanguage.JAVA));
ImmutableSortedSet<BuildRule> exportedDeps = resolver.getAllRules(args.exportedDeps);
ImmutableSortedSet.Builder<BuildRule> declaredDepsBuilder = ImmutableSortedSet.<BuildRule>naturalOrder().addAll(params.getDeclaredDeps().get()).addAll(queriedDeps).addAll(compiler.getDeclaredDeps(args, resolver));
ImmutableSortedSet<BuildRule> declaredDeps = declaredDepsBuilder.build();
ImmutableSortedSet<BuildRule> extraDeps = ImmutableSortedSet.<BuildRule>naturalOrder().addAll(params.getExtraDeps().get()).addAll(BuildRules.getExportedRules(Iterables.concat(declaredDeps, exportedDeps, resolver.getAllRules(args.providedDeps)))).addAll(ruleFinder.filterBuildRuleInputs(javacOptions.getInputs(ruleFinder))).addAll(compiler.getExtraDeps(args, resolver)).build();
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
BuildRuleParams androidLibraryParams = params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(declaredDeps), Suppliers.ofInstance(extraDeps));
return new AndroidLibrary(androidLibraryParams, pathResolver, ruleFinder, args.srcs, ResourceValidator.validateResources(pathResolver, params.getProjectFilesystem(), args.resources), args.proguardConfig, args.postprocessClassesCommands, exportedDeps, resolver.getAllRules(args.providedDeps), JavaLibraryRules.getAbiInputs(resolver, androidLibraryParams.getDeps()), additionalClasspathEntries, javacOptions, compiler.trackClassUsage(javacOptions), compiler.compileToJar(args, javacOptions, resolver), args.resourcesRoot, args.mavenCoords, args.manifest, args.tests);
}
}
use of com.facebook.buck.rules.BuildRuleParams in project buck by facebook.
the class AndroidLibraryGraphEnhancer method getBuildableForAndroidResources.
public Optional<DummyRDotJava> getBuildableForAndroidResources(BuildRuleResolver ruleResolver, boolean createBuildableIfEmptyDeps) {
Preconditions.checkState(!CalculateAbi.isAbiTarget(dummyRDotJavaBuildTarget));
Optional<BuildRule> previouslyCreated = ruleResolver.getRuleOptional(dummyRDotJavaBuildTarget);
if (previouslyCreated.isPresent()) {
return previouslyCreated.map(input -> (DummyRDotJava) input);
}
ImmutableSortedSet<BuildRule> originalDeps = originalBuildRuleParams.getDeps();
ImmutableSet<HasAndroidResourceDeps> androidResourceDeps;
switch(resourceDependencyMode) {
case FIRST_ORDER:
androidResourceDeps = FluentIterable.from(originalDeps).filter(HasAndroidResourceDeps.class).filter(input -> input.getRes() != null).toSet();
break;
case TRANSITIVE:
androidResourceDeps = UnsortedAndroidResourceDeps.createFrom(originalDeps, Optional.empty()).getResourceDeps();
break;
default:
throw new IllegalStateException("Invalid resource dependency mode: " + resourceDependencyMode);
}
if (androidResourceDeps.isEmpty() && !createBuildableIfEmptyDeps) {
return Optional.empty();
}
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
BuildRuleParams dummyRDotJavaParams = originalBuildRuleParams.withBuildTarget(dummyRDotJavaBuildTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.copyOf(ruleFinder.filterBuildRuleInputs(javacOptions.getInputs(ruleFinder)))), Suppliers.ofInstance(ImmutableSortedSet.of()));
DummyRDotJava dummyRDotJava = new DummyRDotJava(dummyRDotJavaParams, ruleFinder, androidResourceDeps, javacOptions, forceFinalResourceIds, resourceUnionPackage, finalRName, useOldStyleableFormat);
ruleResolver.addToIndex(dummyRDotJava);
return Optional.of(dummyRDotJava);
}
use of com.facebook.buck.rules.BuildRuleParams 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.BuildRuleParams in project buck by facebook.
the class AndroidBinaryGraphEnhancer method createPreDexRulesForLibraries.
@VisibleForTesting
ImmutableMultimap<APKModule, DexProducedFromJavaLibrary> createPreDexRulesForLibraries(Iterable<BuildRule> additionalJavaLibrariesToDex, AndroidPackageableCollection packageableCollection) {
Iterable<BuildTarget> additionalJavaLibraryTargets = FluentIterable.from(additionalJavaLibrariesToDex).transform(BuildRule::getBuildTarget);
ImmutableMultimap.Builder<APKModule, DexProducedFromJavaLibrary> preDexDeps = ImmutableMultimap.builder();
for (BuildTarget buildTarget : Iterables.concat(packageableCollection.getJavaLibrariesToDex(), additionalJavaLibraryTargets)) {
Preconditions.checkState(!buildTargetsToExcludeFromDex.contains(buildTarget), "JavaLibrary should have been excluded from target to dex: %s", buildTarget);
BuildRule libraryRule = ruleResolver.getRule(buildTarget);
Preconditions.checkState(libraryRule instanceof JavaLibrary);
JavaLibrary javaLibrary = (JavaLibrary) libraryRule;
// resources, but export_deps is true), then there will not be anything to dx.
if (javaLibrary.getSourcePathToOutput() == null) {
continue;
}
// See whether the corresponding IntermediateDexRule has already been added to the
// ruleResolver.
BuildTarget originalTarget = javaLibrary.getBuildTarget();
BuildTarget preDexTarget = BuildTarget.builder(originalTarget).addFlavors(DEX_FLAVOR).build();
Optional<BuildRule> preDexRule = ruleResolver.getRuleOptional(preDexTarget);
if (preDexRule.isPresent()) {
preDexDeps.put(apkModuleGraph.findModuleForTarget(buildTarget), (DexProducedFromJavaLibrary) preDexRule.get());
continue;
}
// Create the IntermediateDexRule and add it to both the ruleResolver and preDexDeps.
BuildRuleParams paramsForPreDex = buildRuleParams.withBuildTarget(preDexTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of(ruleResolver.getRule(javaLibrary.getBuildTarget()))), Suppliers.ofInstance(ImmutableSortedSet.of()));
DexProducedFromJavaLibrary preDex = new DexProducedFromJavaLibrary(paramsForPreDex, javaLibrary);
ruleResolver.addToIndex(preDex);
preDexDeps.put(apkModuleGraph.findModuleForTarget(buildTarget), preDex);
}
return preDexDeps.build();
}
Aggregations