Search in sources :

Example 16 with JavaLibrary

use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.

the class AndroidAarDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams originalBuildRuleParams, BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
    UnflavoredBuildTarget originalBuildTarget = originalBuildRuleParams.getBuildTarget().checkUnflavored();
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    ImmutableList.Builder<BuildRule> aarExtraDepsBuilder = ImmutableList.<BuildRule>builder().addAll(originalBuildRuleParams.getExtraDeps().get());
    /* android_manifest */
    AndroidManifestDescription.Arg androidManifestArgs = androidManifestDescription.createUnpopulatedConstructorArg();
    androidManifestArgs.skeleton = args.manifestSkeleton;
    androidManifestArgs.deps = args.deps;
    BuildRuleParams androidManifestParams = originalBuildRuleParams.withBuildTarget(BuildTargets.createFlavoredBuildTarget(originalBuildTarget, AAR_ANDROID_MANIFEST_FLAVOR));
    AndroidManifest manifest = androidManifestDescription.createBuildRule(targetGraph, androidManifestParams, resolver, androidManifestArgs);
    aarExtraDepsBuilder.add(resolver.addToIndex(manifest));
    final APKModuleGraph apkModuleGraph = new APKModuleGraph(targetGraph, originalBuildRuleParams.getBuildTarget(), Optional.empty());
    /* assemble dirs */
    AndroidPackageableCollector collector = new AndroidPackageableCollector(originalBuildRuleParams.getBuildTarget(), /* buildTargetsToExcludeFromDex */
    ImmutableSet.of(), /* resourcesToExclude */
    ImmutableSet.of(), apkModuleGraph);
    collector.addPackageables(AndroidPackageableCollector.getPackageableRules(originalBuildRuleParams.getDeps()));
    AndroidPackageableCollection packageableCollection = collector.build();
    ImmutableSortedSet<BuildRule> androidResourceDeclaredDeps = AndroidResourceHelper.androidResOnly(originalBuildRuleParams.getDeclaredDeps().get());
    ImmutableSortedSet<BuildRule> androidResourceExtraDeps = AndroidResourceHelper.androidResOnly(originalBuildRuleParams.getExtraDeps().get());
    BuildRuleParams assembleAssetsParams = originalBuildRuleParams.withBuildTarget(BuildTargets.createFlavoredBuildTarget(originalBuildTarget, AAR_ASSEMBLE_ASSETS_FLAVOR)).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(androidResourceDeclaredDeps), Suppliers.ofInstance(androidResourceExtraDeps));
    ImmutableCollection<SourcePath> assetsDirectories = packageableCollection.getAssetsDirectories();
    AssembleDirectories assembleAssetsDirectories = new AssembleDirectories(assembleAssetsParams, assetsDirectories);
    aarExtraDepsBuilder.add(resolver.addToIndex(assembleAssetsDirectories));
    BuildRuleParams assembleResourceParams = originalBuildRuleParams.withBuildTarget(BuildTargets.createFlavoredBuildTarget(originalBuildTarget, AAR_ASSEMBLE_RESOURCE_FLAVOR)).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(androidResourceDeclaredDeps), Suppliers.ofInstance(androidResourceExtraDeps));
    ImmutableCollection<SourcePath> resDirectories = packageableCollection.getResourceDetails().getResourceDirectories();
    MergeAndroidResourceSources assembleResourceDirectories = new MergeAndroidResourceSources(assembleResourceParams, resDirectories);
    aarExtraDepsBuilder.add(resolver.addToIndex(assembleResourceDirectories));
    /* android_resource */
    BuildRuleParams androidResourceParams = originalBuildRuleParams.withBuildTarget(BuildTargets.createFlavoredBuildTarget(originalBuildTarget, AAR_ANDROID_RESOURCE_FLAVOR)).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of(manifest, assembleAssetsDirectories, assembleResourceDirectories)), Suppliers.ofInstance(ImmutableSortedSet.of()));
    AndroidResource androidResource = new AndroidResource(androidResourceParams, ruleFinder, /* deps */
    ImmutableSortedSet.<BuildRule>naturalOrder().add(assembleAssetsDirectories).add(assembleResourceDirectories).addAll(originalBuildRuleParams.getDeclaredDeps().get()).build(), assembleResourceDirectories.getSourcePathToOutput(), /* resSrcs */
    ImmutableSortedMap.of(), /* rDotJavaPackage */
    null, assembleAssetsDirectories.getSourcePathToOutput(), /* assetsSrcs */
    ImmutableSortedMap.of(), manifest.getSourcePathToOutput(), /* hasWhitelistedStrings */
    false);
    aarExtraDepsBuilder.add(resolver.addToIndex(androidResource));
    ImmutableSortedSet.Builder<SourcePath> classpathToIncludeInAar = ImmutableSortedSet.naturalOrder();
    classpathToIncludeInAar.addAll(packageableCollection.getClasspathEntriesToDex());
    aarExtraDepsBuilder.addAll(BuildRules.toBuildRulesFor(originalBuildRuleParams.getBuildTarget(), resolver, packageableCollection.getJavaLibrariesToDex()));
    if (!args.buildConfigValues.getNameToField().isEmpty() && !args.includeBuildConfigClass) {
        throw new HumanReadableException("Rule %s has build_config_values set but does not set " + "include_build_config_class to True. Either indicate you want to include the " + "BuildConfig class in the final .aar or do not specify build config values.", originalBuildRuleParams.getBuildTarget());
    }
    if (args.includeBuildConfigClass) {
        ImmutableSortedSet<JavaLibrary> buildConfigRules = AndroidBinaryGraphEnhancer.addBuildConfigDeps(originalBuildRuleParams, AndroidBinary.PackageType.RELEASE, EnumSet.noneOf(AndroidBinary.ExopackageMode.class), args.buildConfigValues, Optional.empty(), resolver, javacOptions, packageableCollection);
        resolver.addAllToIndex(buildConfigRules);
        aarExtraDepsBuilder.addAll(buildConfigRules);
        classpathToIncludeInAar.addAll(buildConfigRules.stream().map(BuildRule::getSourcePathToOutput).collect(Collectors.toList()));
    }
    /* native_libraries */
    AndroidNativeLibsPackageableGraphEnhancer packageableGraphEnhancer = new AndroidNativeLibsPackageableGraphEnhancer(resolver, originalBuildRuleParams, nativePlatforms, ImmutableSet.of(), cxxBuckConfig, /* nativeLibraryMergeMap */
    Optional.empty(), /* nativeLibraryMergeGlue */
    Optional.empty(), AndroidBinary.RelinkerMode.DISABLED, apkModuleGraph);
    Optional<ImmutableMap<APKModule, CopyNativeLibraries>> nativeLibrariesOptional = packageableGraphEnhancer.enhance(packageableCollection).getCopyNativeLibraries();
    if (nativeLibrariesOptional.isPresent() && nativeLibrariesOptional.get().containsKey(apkModuleGraph.getRootAPKModule())) {
        aarExtraDepsBuilder.add(resolver.addToIndex(nativeLibrariesOptional.get().get(apkModuleGraph.getRootAPKModule())));
    }
    Optional<Path> assembledNativeLibsDir = nativeLibrariesOptional.map(input -> {
        CopyNativeLibraries copyNativeLibraries = input.get(apkModuleGraph.getRootAPKModule());
        if (copyNativeLibraries == null) {
            throw new HumanReadableException("Native libraries are present but not in the root application module.");
        }
        return copyNativeLibraries.getPathToNativeLibsDir();
    });
    BuildRuleParams androidAarParams = originalBuildRuleParams.copyReplacingExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.copyOf(aarExtraDepsBuilder.build())));
    return new AndroidAar(androidAarParams, manifest, androidResource, assembleResourceDirectories.getSourcePathToOutput(), assembleAssetsDirectories.getSourcePathToOutput(), assembledNativeLibsDir, ImmutableSet.copyOf(packageableCollection.getNativeLibAssetsDirectories().values()), classpathToIncludeInAar.build());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) SourcePath(com.facebook.buck.rules.SourcePath) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) BuildRule(com.facebook.buck.rules.BuildRule) SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) ImmutableMap(com.google.common.collect.ImmutableMap) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) HumanReadableException(com.facebook.buck.util.HumanReadableException) MergeAndroidResourceSources(com.facebook.buck.android.aapt.MergeAndroidResourceSources)

Example 17 with JavaLibrary

use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.

the class AndroidBinary method addProguardCommands.

/**
   * @return the resulting set of ProGuarded classpath entries to dex.
   */
@VisibleForTesting
ImmutableSet<Path> addProguardCommands(Set<Path> classpathEntriesToDex, Set<Path> depsProguardConfigs, boolean skipProguard, ImmutableList.Builder<Step> steps, BuildableContext buildableContext, SourcePathResolver resolver) {
    ImmutableSet.Builder<Path> additionalLibraryJarsForProguardBuilder = ImmutableSet.builder();
    for (JavaLibrary buildRule : rulesToExcludeFromDex) {
        additionalLibraryJarsForProguardBuilder.addAll(buildRule.getImmediateClasspaths().stream().map(resolver::getAbsolutePath).collect(MoreCollectors.toImmutableSet()));
    }
    // Create list of proguard Configs for the app project and its dependencies
    ImmutableSet.Builder<Path> proguardConfigsBuilder = ImmutableSet.builder();
    proguardConfigsBuilder.addAll(depsProguardConfigs);
    if (proguardConfig.isPresent()) {
        proguardConfigsBuilder.add(resolver.getAbsolutePath(proguardConfig.get()));
    }
    Path proguardConfigDir = getProguardTextFilesPath();
    // Transform our input classpath to a set of output locations for each input classpath.
    // TODO(jasta): the output path we choose is the result of a slicing function against
    // input classpath. This is fragile and should be replaced with knowledge of the BuildTarget.
    final ImmutableMap<Path, Path> inputOutputEntries = classpathEntriesToDex.stream().collect(MoreCollectors.toImmutableMap(java.util.function.Function.identity(), (path) -> getProguardOutputFromInputClasspath(proguardConfigDir, path)));
    // Run ProGuard on the classpath entries.
    ProGuardObfuscateStep.create(javaRuntimeLauncher, getProjectFilesystem(), proguardJarOverride.isPresent() ? Optional.of(resolver.getAbsolutePath(proguardJarOverride.get())) : Optional.empty(), proguardMaxHeapSize, proguardAgentPath, resolver.getRelativePath(aaptGeneratedProguardConfigFile), proguardConfigsBuilder.build(), sdkProguardConfig, optimizationPasses, proguardJvmArgs, inputOutputEntries, additionalLibraryJarsForProguardBuilder.build(), proguardConfigDir, buildableContext, skipProguard, steps);
    // ProGuard then return the input classes to dex.
    if (skipProguard) {
        return ImmutableSet.copyOf(inputOutputEntries.keySet());
    } else {
        return ImmutableSet.copyOf(inputOutputEntries.values());
    }
}
Also used : Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) OptionalCompat(com.facebook.buck.util.OptionalCompat) ExopackageInfo(com.facebook.buck.rules.ExopackageInfo) HasRuntimeDeps(com.facebook.buck.rules.HasRuntimeDeps) CopyStep(com.facebook.buck.step.fs.CopyStep) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) ImmutableCollection(com.google.common.collect.ImmutableCollection) RichStream(com.facebook.buck.util.RichStream) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) MkdirStep(com.facebook.buck.step.fs.MkdirStep) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) FluentIterable(com.google.common.collect.FluentIterable) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SymlinkFilesIntoDirectoryStep(com.facebook.buck.shell.SymlinkFilesIntoDirectoryStep) Keystore(com.facebook.buck.jvm.java.Keystore) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Path(java.nio.file.Path) EnumSet(java.util.EnumSet) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) Function(com.google.common.base.Function) ImmutableSet(com.google.common.collect.ImmutableSet) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) ImmutableMap(com.google.common.collect.ImmutableMap) BuildableProperties(com.facebook.buck.rules.BuildableProperties) BuildableContext(com.facebook.buck.rules.BuildableContext) Set(java.util.Set) BuildTarget(com.facebook.buck.model.BuildTarget) HasClasspathEntries(com.facebook.buck.jvm.java.HasClasspathEntries) AbstractBuildRule(com.facebook.buck.rules.AbstractBuildRule) FileVisitResult(java.nio.file.FileVisitResult) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) PACKAGING(com.facebook.buck.rules.BuildableProperties.Kind.PACKAGING) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) JavaRuntimeLauncher(com.facebook.buck.jvm.java.JavaRuntimeLauncher) AccumulateClassNamesStep(com.facebook.buck.jvm.java.AccumulateClassNamesStep) Joiner(com.google.common.base.Joiner) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Iterables(com.google.common.collect.Iterables) Step(com.facebook.buck.step.Step) ResourceCompressionMode(com.facebook.buck.android.ResourcesFilter.ResourceCompressionMode) Supplier(com.google.common.base.Supplier) RepackZipEntriesStep(com.facebook.buck.zip.RepackZipEntriesStep) SourcePath(com.facebook.buck.rules.SourcePath) Multimap(com.google.common.collect.Multimap) BuildRule(com.facebook.buck.rules.BuildRule) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) ExecutionContext(com.facebook.buck.step.ExecutionContext) ImmutableList(com.google.common.collect.ImmutableList) Files(com.google.common.io.Files) Suppliers(com.google.common.base.Suppliers) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) RedexOptions(com.facebook.buck.android.redex.RedexOptions) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Nullable(javax.annotation.Nullable) SupportsInputBasedRuleKey(com.facebook.buck.rules.keys.SupportsInputBasedRuleKey) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) JavaLibraryClasspathProvider(com.facebook.buck.jvm.java.JavaLibraryClasspathProvider) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) HashCode(com.google.common.hash.HashCode) ManifestEntries(com.facebook.buck.rules.coercer.ManifestEntries) ReDexStep(com.facebook.buck.android.redex.ReDexStep) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) ANDROID(com.facebook.buck.rules.BuildableProperties.Kind.ANDROID) AbstractMap(java.util.AbstractMap) XzStep(com.facebook.buck.step.fs.XzStep) Paths(java.nio.file.Paths) ResourceFilter(com.facebook.buck.android.FilterResourcesStep.ResourceFilter) ZipScrubberStep(com.facebook.buck.zip.ZipScrubberStep) BuildContext(com.facebook.buck.rules.BuildContext) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BuildTargets(com.facebook.buck.model.BuildTargets) AbstractGenruleStep(com.facebook.buck.shell.AbstractGenruleStep) ImmutableSet(com.google.common.collect.ImmutableSet) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 18 with JavaLibrary

use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.

the class AndroidBinaryDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
    try (SimplePerfEvent.Scope ignored = SimplePerfEvent.scope(Optional.ofNullable(resolver.getEventBus()), PerfEventId.of("AndroidBinaryDescription"), "target", params.getBuildTarget().toString())) {
        ResourceCompressionMode compressionMode = getCompressionMode(args);
        BuildTarget target = params.getBuildTarget();
        boolean isFlavored = target.isFlavored();
        if (isFlavored) {
            if (target.getFlavors().contains(PACKAGE_STRING_ASSETS_FLAVOR) && !compressionMode.isStoreStringsAsAssets()) {
                throw new HumanReadableException("'package_string_assets' flavor does not exist for %s.", target.getUnflavoredBuildTarget());
            }
            params = params.withBuildTarget(BuildTarget.of(target.getUnflavoredBuildTarget()));
        }
        BuildRule keystore = resolver.getRule(args.keystore);
        if (!(keystore instanceof Keystore)) {
            throw new HumanReadableException("In %s, keystore='%s' must be a keystore() but was %s().", params.getBuildTarget(), keystore.getFullyQualifiedName(), keystore.getType());
        }
        APKModuleGraph apkModuleGraph = new APKModuleGraph(targetGraph, target, Optional.of(args.applicationModuleTargets));
        ProGuardObfuscateStep.SdkProguardType androidSdkProguardConfig = args.androidSdkProguardConfig.orElse(ProGuardObfuscateStep.SdkProguardType.DEFAULT);
        // was not specified, and allow the old form to override the default.
        if (args.useAndroidProguardConfigWithOptimizations.isPresent()) {
            Preconditions.checkArgument(!args.androidSdkProguardConfig.isPresent(), "The deprecated use_android_proguard_config_with_optimizations parameter" + " cannot be used with android_sdk_proguard_config.");
            LOG.error("Target %s specified use_android_proguard_config_with_optimizations, " + "which is deprecated. Use android_sdk_proguard_config.", params.getBuildTarget());
            androidSdkProguardConfig = args.useAndroidProguardConfigWithOptimizations.orElse(false) ? ProGuardObfuscateStep.SdkProguardType.OPTIMIZED : ProGuardObfuscateStep.SdkProguardType.DEFAULT;
        }
        EnumSet<ExopackageMode> exopackageModes = EnumSet.noneOf(ExopackageMode.class);
        if (!args.exopackageModes.isEmpty()) {
            exopackageModes = EnumSet.copyOf(args.exopackageModes);
        } else if (args.exopackage.orElse(false)) {
            LOG.error("Target %s specified exopackage=True, which is deprecated. Use exopackage_modes.", params.getBuildTarget());
            exopackageModes = EnumSet.of(ExopackageMode.SECONDARY_DEX);
        }
        DexSplitMode dexSplitMode = createDexSplitMode(args, exopackageModes);
        PackageType packageType = getPackageType(args);
        boolean shouldPreDex = !args.disablePreDex && PackageType.DEBUG.equals(packageType) && !args.preprocessJavaClassesBash.isPresent();
        ResourceFilter resourceFilter = new ResourceFilter(args.resourceFilter);
        AndroidBinaryGraphEnhancer graphEnhancer = new AndroidBinaryGraphEnhancer(params, resolver, compressionMode, resourceFilter, args.getBannedDuplicateResourceTypes(), args.resourceUnionPackage, addFallbackLocales(args.locales), args.manifest, packageType, ImmutableSet.copyOf(args.cpuFilters), args.buildStringSourceMap, shouldPreDex, AndroidBinary.getPrimaryDexPath(params.getBuildTarget(), params.getProjectFilesystem()), dexSplitMode, ImmutableSet.copyOf(args.noDx.orElse(ImmutableSet.of())), /* resourcesToExclude */
        ImmutableSet.of(), args.skipCrunchPngs, args.includesVectorDrawables, javacOptions, exopackageModes, args.buildConfigValues, args.buildConfigValuesFile, Optional.empty(), args.trimResourceIds, args.keepResourcePattern, nativePlatforms, Optional.of(args.nativeLibraryMergeMap), args.nativeLibraryMergeGlue, args.nativeLibraryMergeCodeGenerator, args.enableRelinker ? RelinkerMode.ENABLED : RelinkerMode.DISABLED, dxExecutorService, args.manifestEntries, cxxBuckConfig, apkModuleGraph, dxConfig);
        AndroidGraphEnhancementResult result = graphEnhancer.createAdditionalBuildables();
        if (target.getFlavors().contains(PACKAGE_STRING_ASSETS_FLAVOR)) {
            Optional<PackageStringAssets> packageStringAssets = result.getPackageStringAssets();
            Preconditions.checkState(packageStringAssets.isPresent());
            return packageStringAssets.get();
        }
        // Build rules added to "no_dx" are only hints, not hard dependencies. Therefore, although a
        // target may be mentioned in that parameter, it may not be present as a build rule.
        ImmutableSortedSet.Builder<BuildRule> builder = ImmutableSortedSet.naturalOrder();
        for (BuildTarget noDxTarget : args.noDx.orElse(ImmutableSet.of())) {
            Optional<BuildRule> ruleOptional = resolver.getRuleOptional(noDxTarget);
            if (ruleOptional.isPresent()) {
                builder.add(ruleOptional.get());
            } else {
                LOG.info("%s: no_dx target not a dependency: %s", target, noDxTarget);
            }
        }
        ImmutableSortedSet<BuildRule> buildRulesToExcludeFromDex = builder.build();
        ImmutableSortedSet<JavaLibrary> rulesToExcludeFromDex = RichStream.from(buildRulesToExcludeFromDex).filter(JavaLibrary.class).collect(MoreCollectors.toImmutableSortedSet(Ordering.natural()));
        SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
        Optional<RedexOptions> redexOptions = getRedexOptions(params, resolver, args);
        ImmutableSortedSet<BuildRule> redexExtraDeps = redexOptions.map(a -> a.getRedexExtraArgs().stream().flatMap(arg -> arg.getDeps(ruleFinder).stream()).collect(MoreCollectors.toImmutableSortedSet(Ordering.natural()))).orElse(ImmutableSortedSet.of());
        return new AndroidBinary(params.copyReplacingExtraDeps(Suppliers.ofInstance(result.getFinalDeps())).copyAppendingExtraDeps(ruleFinder.filterBuildRuleInputs(result.getPackageableCollection().getProguardConfigs())).copyAppendingExtraDeps(rulesToExcludeFromDex).copyAppendingExtraDeps(redexExtraDeps), ruleFinder, proGuardConfig.getProguardJarOverride(), proGuardConfig.getProguardMaxHeapSize(), Optional.of(args.proguardJvmArgs), proGuardConfig.getProguardAgentPath(), (Keystore) keystore, packageType, dexSplitMode, args.noDx.orElse(ImmutableSet.of()), androidSdkProguardConfig, args.optimizationPasses, args.proguardConfig, args.skipProguard, redexOptions, compressionMode, args.cpuFilters, resourceFilter, exopackageModes, MACRO_HANDLER.getExpander(params.getBuildTarget(), params.getCellRoots(), resolver), args.preprocessJavaClassesBash, rulesToExcludeFromDex, result, args.reorderClassesIntraDex, args.dexReorderToolFile, args.dexReorderDataDumpFile, args.xzCompressionLevel, dxExecutorService, args.packageAssetLibraries, args.compressAssetLibraries, args.manifestEntries, javaOptions.getJavaRuntimeLauncher(), dxConfig.getDxMaxHeapSize());
    }
}
Also used : PerfEventId(com.facebook.buck.event.PerfEventId) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) RichStream(com.facebook.buck.util.RichStream) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) HasTests(com.facebook.buck.model.HasTests) ExopackageMode(com.facebook.buck.android.AndroidBinary.ExopackageMode) BuckConfig(com.facebook.buck.cli.BuckConfig) Matcher(java.util.regex.Matcher) Locale(java.util.Locale) Map(java.util.Map) DexSplitStrategy(com.facebook.buck.dalvik.ZipSplitter.DexSplitStrategy) Keystore(com.facebook.buck.jvm.java.Keystore) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) EnumSet(java.util.EnumSet) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) TargetGraph(com.facebook.buck.rules.TargetGraph) Set(java.util.Set) MacroException(com.facebook.buck.model.MacroException) BuildTarget(com.facebook.buck.model.BuildTarget) SuppressFieldNotInitialized(com.facebook.infer.annotation.SuppressFieldNotInitialized) Collectors(java.util.stream.Collectors) ExecutableMacroExpander(com.facebook.buck.rules.macros.ExecutableMacroExpander) List(java.util.List) PackageType(com.facebook.buck.android.AndroidBinary.PackageType) RelinkerMode(com.facebook.buck.android.AndroidBinary.RelinkerMode) Hint(com.facebook.buck.rules.Hint) Optional(java.util.Optional) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Pattern(java.util.regex.Pattern) Description(com.facebook.buck.rules.Description) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) CxxBuckConfig(com.facebook.buck.cxx.CxxBuckConfig) CellPathResolver(com.facebook.buck.rules.CellPathResolver) ResourceCompressionMode(com.facebook.buck.android.ResourcesFilter.ResourceCompressionMode) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent) JavaOptions(com.facebook.buck.jvm.java.JavaOptions) SourcePath(com.facebook.buck.rules.SourcePath) Flavored(com.facebook.buck.model.Flavored) BuildRule(com.facebook.buck.rules.BuildRule) Tool(com.facebook.buck.rules.Tool) ImmutableList(com.google.common.collect.ImmutableList) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) ImplicitDepsInferringDescription(com.facebook.buck.rules.ImplicitDepsInferringDescription) BuildConfigFields(com.facebook.buck.rules.coercer.BuildConfigFields) Suppliers(com.google.common.base.Suppliers) RedexOptions(com.facebook.buck.android.redex.RedexOptions) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Logger(com.facebook.buck.log.Logger) MacroArg(com.facebook.buck.rules.args.MacroArg) ManifestEntries(com.facebook.buck.rules.coercer.ManifestEntries) JavacOptions(com.facebook.buck.jvm.java.JavacOptions) HumanReadableException(com.facebook.buck.util.HumanReadableException) PACKAGE_STRING_ASSETS_FLAVOR(com.facebook.buck.android.AndroidBinaryGraphEnhancer.PACKAGE_STRING_ASSETS_FLAVOR) AbstractDescriptionArg(com.facebook.buck.rules.AbstractDescriptionArg) Ordering(com.google.common.collect.Ordering) TargetCpuType(com.facebook.buck.android.NdkCxxPlatforms.TargetCpuType) ResourceFilter(com.facebook.buck.android.FilterResourcesStep.ResourceFilter) LocationMacroExpander(com.facebook.buck.rules.macros.LocationMacroExpander) Preconditions(com.google.common.base.Preconditions) Flavor(com.facebook.buck.model.Flavor) MacroHandler(com.facebook.buck.rules.macros.MacroHandler) RType(com.facebook.buck.android.aapt.RDotTxtEntry.RType) RedexOptions(com.facebook.buck.android.redex.RedexOptions) Keystore(com.facebook.buck.jvm.java.Keystore) ResourceFilter(com.facebook.buck.android.FilterResourcesStep.ResourceFilter) BuildTarget(com.facebook.buck.model.BuildTarget) PackageType(com.facebook.buck.android.AndroidBinary.PackageType) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) BuildRule(com.facebook.buck.rules.BuildRule) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) ResourceCompressionMode(com.facebook.buck.android.ResourcesFilter.ResourceCompressionMode) ExopackageMode(com.facebook.buck.android.AndroidBinary.ExopackageMode) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) HumanReadableException(com.facebook.buck.util.HumanReadableException)

Example 19 with JavaLibrary

use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.

the class AndroidInstrumentationApkDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
    BuildRule installableApk = resolver.getRule(args.apk);
    if (!(installableApk instanceof HasInstallableApk)) {
        throw new HumanReadableException("In %s, apk='%s' must be an android_binary() or apk_genrule() but was %s().", params.getBuildTarget(), installableApk.getFullyQualifiedName(), installableApk.getType());
    }
    AndroidBinary apkUnderTest = getUnderlyingApk((HasInstallableApk) installableApk);
    ImmutableSortedSet<JavaLibrary> rulesToExcludeFromDex = new ImmutableSortedSet.Builder<>(Ordering.<JavaLibrary>natural()).addAll(apkUnderTest.getRulesToExcludeFromDex()).addAll(getClasspathDeps(apkUnderTest.getClasspathDeps())).build();
    // TODO(natthu): Instrumentation APKs should also exclude native libraries and assets from the
    // apk under test.
    AndroidPackageableCollection.ResourceDetails resourceDetails = apkUnderTest.getAndroidPackageableCollection().getResourceDetails();
    ImmutableSet<BuildTarget> resourcesToExclude = ImmutableSet.copyOf(Iterables.concat(resourceDetails.getResourcesWithNonEmptyResDir(), resourceDetails.getResourcesWithEmptyResButNonEmptyAssetsDir()));
    Path primaryDexPath = AndroidBinary.getPrimaryDexPath(params.getBuildTarget(), params.getProjectFilesystem());
    AndroidBinaryGraphEnhancer graphEnhancer = new AndroidBinaryGraphEnhancer(params, resolver, ResourceCompressionMode.DISABLED, FilterResourcesStep.ResourceFilter.EMPTY_FILTER, /* bannedDuplicateResourceTypes */
    EnumSet.noneOf(RType.class), /* resourceUnionPackage */
    Optional.empty(), /* locales */
    ImmutableSet.of(), args.manifest, PackageType.INSTRUMENTED, apkUnderTest.getCpuFilters(), /* shouldBuildStringSourceMap */
    false, /* shouldPreDex */
    false, primaryDexPath, DexSplitMode.NO_SPLIT, rulesToExcludeFromDex.stream().map(BuildRule::getBuildTarget).collect(MoreCollectors.toImmutableSet()), resourcesToExclude, /* skipCrunchPngs */
    false, args.includesVectorDrawables.orElse(false), javacOptions, EnumSet.noneOf(ExopackageMode.class), /* buildConfigValues */
    BuildConfigFields.empty(), /* buildConfigValuesFile */
    Optional.empty(), /* xzCompressionLevel */
    Optional.empty(), /* trimResourceIds */
    false, /* keepResourcePattern */
    Optional.empty(), nativePlatforms, /* nativeLibraryMergeMap */
    Optional.empty(), /* nativeLibraryMergeGlue */
    Optional.empty(), /* nativeLibraryMergeCodeGenerator */
    Optional.empty(), AndroidBinary.RelinkerMode.DISABLED, dxExecutorService, apkUnderTest.getManifestEntries(), cxxBuckConfig, new APKModuleGraph(targetGraph, params.getBuildTarget(), Optional.empty()), dxConfig);
    AndroidGraphEnhancementResult enhancementResult = graphEnhancer.createAdditionalBuildables();
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    return new AndroidInstrumentationApk(params.copyReplacingExtraDeps(Suppliers.ofInstance(enhancementResult.getFinalDeps())).copyAppendingExtraDeps(rulesToExcludeFromDex), ruleFinder, proGuardConfig.getProguardJarOverride(), proGuardConfig.getProguardMaxHeapSize(), proGuardConfig.getProguardAgentPath(), apkUnderTest, rulesToExcludeFromDex, enhancementResult, dxExecutorService);
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) RType(com.facebook.buck.android.aapt.RDotTxtEntry.RType) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) ExopackageMode(com.facebook.buck.android.AndroidBinary.ExopackageMode) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) HumanReadableException(com.facebook.buck.util.HumanReadableException) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) BuildRule(com.facebook.buck.rules.BuildRule)

Example 20 with JavaLibrary

use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.

the class Project method createModuleForProjectConfig.

@SuppressWarnings("PMD.LooseCoupling")
private SerializableModule createModuleForProjectConfig(ProjectConfig projectConfig, Optional<Path> rJava) throws IOException {
    BuildRule projectRule = Preconditions.checkNotNull(projectConfig.getProjectRule());
    Preconditions.checkState(projectRule instanceof AndroidBinary || projectRule instanceof AndroidLibrary || projectRule instanceof AndroidResource || projectRule instanceof JavaBinary || projectRule instanceof JavaLibrary || projectRule instanceof JavaTest || projectRule instanceof CxxLibrary || projectRule instanceof NdkLibrary, "project_config() does not know how to process a src_target of type %s (%s).", projectRule.getType(), projectRule.getBuildTarget());
    LinkedHashSet<SerializableDependentModule> dependencies = Sets.newLinkedHashSet();
    final BuildTarget target = projectConfig.getBuildTarget();
    SerializableModule module = new SerializableModule(projectRule, target);
    module.name = getIntellijNameForRule(projectRule);
    module.isIntelliJPlugin = projectConfig.getIsIntelliJPlugin();
    Path relativePath = projectConfig.getBuildTarget().getBasePath();
    module.pathToImlFile = relativePath.resolve(module.name + ".iml");
    // List the module source as the first dependency.
    boolean includeSourceFolder = true;
    // Do the tests before the sources so they appear earlier in the classpath. When tests are run,
    // their classpath entries may be deliberately shadowing production classpath entries.
    // tests folder
    boolean hasSourceFoldersForTestRule = addSourceFolders(module, projectConfig.getTestRule(), projectConfig.getTestsSourceRoots(), true);
    // test dependencies
    BuildRule testRule = projectConfig.getTestRule();
    if (testRule != null) {
        walkRuleAndAdd(testRule, true, /* isForTests */
        dependencies, projectConfig.getSrcRule());
    }
    // src folder
    boolean hasSourceFoldersForSrcRule = addSourceFolders(module, projectConfig.getSrcRule(), projectConfig.getSourceRoots(), false);
    addRootExcludes(module, projectConfig.getSrcRule(), projectFilesystem);
    // non-library Android project with no source roots specified.
    if (!hasSourceFoldersForTestRule && !hasSourceFoldersForSrcRule) {
        includeSourceFolder = false;
    }
    // IntelliJ expects all Android projects to have a gen/ folder, even if there is no src/
    // directory specified.
    boolean isAndroidRule = projectRule.getProperties().is(ANDROID);
    if (isAndroidRule) {
        boolean hasSourceFolders = !module.sourceFolders.isEmpty();
        module.sourceFolders.add(SerializableModule.SourceFolder.GEN);
        if (!hasSourceFolders) {
            includeSourceFolder = true;
        }
    }
    // src dependencies
    // Note that isForTests is false even if projectRule is the project_config's test_target.
    walkRuleAndAdd(projectRule, false, /* isForTests */
    dependencies, projectConfig.getSrcRule());
    Path basePath = projectConfig.getBuildTarget().getBasePath();
    // Specify another path for intellij to generate gen/ for each android module,
    // so that it will not disturb our glob() rules.
    // To specify the location of gen, Intellij requires the relative path from
    // the base path of current build target.
    module.moduleGenPath = generateRelativeGenPath(projectFilesystem, basePath);
    if (turnOffAutoSourceGeneration && rJava.isPresent()) {
        module.moduleRJavaPath = basePath.relativize(Paths.get("")).resolve(rJava.get());
    }
    SerializableDependentModule jdkDependency;
    if (isAndroidRule) {
        // android details
        if (projectRule instanceof NdkLibrary) {
            NdkLibrary ndkLibrary = (NdkLibrary) projectRule;
            module.isAndroidLibraryProject = true;
            module.keystorePath = null;
            module.nativeLibs = relativePath.relativize(ndkLibrary.getLibraryPath());
        } else if (projectRule instanceof AndroidLibrary) {
            module.isAndroidLibraryProject = true;
            module.keystorePath = null;
            module.resFolder = intellijConfig.getAndroidResources().orElse(null);
            module.assetFolder = intellijConfig.getAndroidAssets().orElse(null);
        } else if (projectRule instanceof AndroidResource) {
            AndroidResource androidResource = (AndroidResource) projectRule;
            module.resFolder = createRelativeResourcesPath(Optional.ofNullable(androidResource.getRes()).map(resolver::getAbsolutePath).map(projectFilesystem::relativize).orElse(null), target);
            module.isAndroidLibraryProject = true;
            module.keystorePath = null;
        } else if (projectRule instanceof AndroidBinary) {
            AndroidBinary androidBinary = (AndroidBinary) projectRule;
            module.resFolder = intellijConfig.getAndroidResources().orElse(null);
            module.assetFolder = intellijConfig.getAndroidAssets().orElse(null);
            module.isAndroidLibraryProject = false;
            module.binaryPath = generateRelativeAPKPath(projectFilesystem, projectRule.getBuildTarget().getShortName(), basePath);
            KeystoreProperties keystoreProperties = KeystoreProperties.createFromPropertiesFile(resolver.getAbsolutePath(androidBinary.getKeystore().getPathToStore()), resolver.getRelativePath(androidBinary.getKeystore().getPathToPropertiesFile()), projectFilesystem);
            // getKeystore() returns an absolute path, but an IntelliJ module
            // expects the path to the keystore to be relative to the module root.
            // First, grab the aboslute path to the project config.
            BuildTarget projectTarget = projectConfig.getBuildTarget();
            Path modulePath = projectTarget.getCellPath().resolve(projectTarget.getBasePath());
            // Now relativize to the keystore path, which is absolute too.
            module.keystorePath = modulePath.relativize(keystoreProperties.getKeystore());
        } else {
            module.isAndroidLibraryProject = true;
            module.keystorePath = null;
        }
        module.hasAndroidFacet = true;
        module.proguardConfigPath = null;
        module.androidManifest = resolveAndroidManifestRelativePath(basePath);
        // List this last so that classes from modules can shadow classes in the JDK.
        jdkDependency = SerializableDependentModule.newInheritedJdk();
    } else {
        module.hasAndroidFacet = false;
        if (module.isIntelliJPlugin()) {
            jdkDependency = SerializableDependentModule.newIntelliJPluginJdk();
        } else {
            jdkDependency = SerializableDependentModule.newStandardJdk(intellijConfig.getJdkName(), intellijConfig.getJdkType());
        }
    }
    // Assign the dependencies.
    module.setModuleDependencies(createDependenciesInOrder(includeSourceFolder, dependencies, jdkDependency));
    // Annotation processing generates sources for IntelliJ to consume, but does so outside
    // the module directory to avoid messing up globbing.
    JavaLibrary javaLibrary = null;
    if (projectRule instanceof JavaLibrary) {
        javaLibrary = (JavaLibrary) projectRule;
    }
    if (javaLibrary != null) {
        Optional<Path> processingParams = javaLibrary.getGeneratedSourcePath();
        if (processingParams.isPresent()) {
            module.annotationGenPath = basePath.relativize(processingParams.get());
            module.annotationGenIsForTest = !hasSourceFoldersForSrcRule;
        }
    }
    return module;
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) CxxLibrary(com.facebook.buck.cxx.CxxLibrary) AndroidBinary(com.facebook.buck.android.AndroidBinary) JavaBinary(com.facebook.buck.jvm.java.JavaBinary) AndroidResource(com.facebook.buck.android.AndroidResource) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) AndroidLibrary(com.facebook.buck.android.AndroidLibrary) BuildTarget(com.facebook.buck.model.BuildTarget) JavaTest(com.facebook.buck.jvm.java.JavaTest) BuildRule(com.facebook.buck.rules.BuildRule) NdkLibrary(com.facebook.buck.android.NdkLibrary) KeystoreProperties(com.facebook.buck.android.KeystoreProperties)

Aggregations

JavaLibrary (com.facebook.buck.jvm.java.JavaLibrary)33 BuildTarget (com.facebook.buck.model.BuildTarget)24 BuildRule (com.facebook.buck.rules.BuildRule)18 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)18 Path (java.nio.file.Path)17 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)15 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)14 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)13 Test (org.junit.Test)13 SourcePath (com.facebook.buck.rules.SourcePath)12 DefaultJavaLibrary (com.facebook.buck.jvm.java.DefaultJavaLibrary)11 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)11 TargetGraph (com.facebook.buck.rules.TargetGraph)9 ImmutableSet (com.google.common.collect.ImmutableSet)8 Optional (java.util.Optional)8 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)7 DefaultJavaPackageFinder (com.facebook.buck.jvm.java.DefaultJavaPackageFinder)6 PathSourcePath (com.facebook.buck.rules.PathSourcePath)6 JavaLibraryDescription (com.facebook.buck.jvm.java.JavaLibraryDescription)5 JavaTest (com.facebook.buck.jvm.java.JavaTest)5