Search in sources :

Example 1 with NativeRelinker

use of com.facebook.buck.android.relinker.NativeRelinker 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();
}
Also used : NativeLinkable(com.facebook.buck.cxx.NativeLinkable) ArrayList(java.util.ArrayList) SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) BuildRule(com.facebook.buck.rules.BuildRule) Pair(com.facebook.buck.model.Pair) PathSourcePath(com.facebook.buck.rules.PathSourcePath) NativeRelinker(com.facebook.buck.android.relinker.NativeRelinker) ImmutableMap(com.google.common.collect.ImmutableMap) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams)

Aggregations

NativeRelinker (com.facebook.buck.android.relinker.NativeRelinker)1 NativeLinkable (com.facebook.buck.cxx.NativeLinkable)1 Pair (com.facebook.buck.model.Pair)1 BuildRule (com.facebook.buck.rules.BuildRule)1 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)1 BuildTargetSourcePath (com.facebook.buck.rules.BuildTargetSourcePath)1 PathSourcePath (com.facebook.buck.rules.PathSourcePath)1 SourcePath (com.facebook.buck.rules.SourcePath)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1