Search in sources :

Example 1 with JavaRuntimeJarProvider

use of com.google.devtools.build.lib.rules.java.JavaRuntimeJarProvider in project bazel by bazelbuild.

the class AarImport method create.

@Override
public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException {
    if (!AndroidSdkProvider.verifyPresence(ruleContext)) {
        return null;
    }
    RuleConfiguredTargetBuilder ruleBuilder = new RuleConfiguredTargetBuilder(ruleContext);
    Artifact aar = ruleContext.getPrerequisiteArtifact("aar", Mode.TARGET);
    Artifact allAarJars = createAarTreeArtifact(ruleContext, "jars");
    Artifact jarMergingParams = createAarArtifact(ruleContext, "jar_merging_params");
    ruleContext.registerAction(createAarEmbeddedJarsExtractorActions(ruleContext, aar, allAarJars, jarMergingParams));
    Artifact mergedJar = createAarArtifact(ruleContext, MERGED_JAR);
    ruleContext.registerAction(createAarJarsMergingActions(ruleContext, allAarJars, mergedJar, jarMergingParams));
    // AndroidManifest.xml is required in every AAR.
    Artifact androidManifestArtifact = createAarArtifact(ruleContext, ANDROID_MANIFEST);
    ruleContext.registerAction(createSingleFileExtractorActions(ruleContext, aar, ANDROID_MANIFEST, androidManifestArtifact));
    Artifact resourcesManifest = createAarArtifact(ruleContext, "resource_manifest");
    ruleContext.registerAction(createManifestExtractorActions(ruleContext, aar, "res/.*", resourcesManifest));
    Artifact resources = createAarTreeArtifact(ruleContext, "resources");
    ruleContext.registerAction(createManifestFileEntriesExtractorActions(ruleContext, aar, resourcesManifest, resources));
    ApplicationManifest androidManifest = ApplicationManifest.fromExplicitManifest(ruleContext, androidManifestArtifact);
    FileProvider resourcesProvider = new FileProvider(new NestedSetBuilder<Artifact>(Order.NAIVE_LINK_ORDER).add(resources).build());
    Artifact resourcesZip = ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_RESOURCES_ZIP);
    ResourceApk resourceApk = androidManifest.packWithDataAndResources(ruleContext, new LocalResourceContainer.Builder(ruleContext).withResources(ImmutableList.of(resourcesProvider)).build(), ResourceDependencies.fromRuleDeps(ruleContext, JavaCommon.isNeverLink(ruleContext)), ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_R_TXT), ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_SYMBOLS), ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_PROCESSED_MANIFEST), resourcesZip, /* alwaysExportManifest = */
    true);
    // There isn't really any use case for building an aar_import target on its own, so the files to
    // build could be empty. The resources zip and merged jars are added here as a sanity check for
    // Bazel developers so that `bazel build java/com/my_aar_import` will fail if the resource
    // processing or jar merging steps fail.
    NestedSetBuilder<Artifact> filesToBuildBuilder = NestedSetBuilder.<Artifact>stableOrder().add(resourcesZip).add(mergedJar);
    Artifact nativeLibs = createAarArtifact(ruleContext, "native_libs.zip");
    ruleContext.registerAction(createAarNativeLibsFilterActions(ruleContext, aar, nativeLibs));
    JavaRuleOutputJarsProvider.Builder jarProviderBuilder = new JavaRuleOutputJarsProvider.Builder().addOutputJar(mergedJar, null, ImmutableList.<Artifact>of());
    for (TransitiveInfoCollection export : ruleContext.getPrerequisites("exports", Mode.TARGET)) {
        for (OutputJar jar : JavaProvider.getProvider(JavaRuleOutputJarsProvider.class, export).getOutputJars()) {
            jarProviderBuilder.addOutputJar(jar);
            filesToBuildBuilder.add(jar.getClassJar());
        }
    }
    ImmutableList<TransitiveInfoCollection> targets = ImmutableList.<TransitiveInfoCollection>copyOf(ruleContext.getPrerequisites("exports", Mode.TARGET));
    JavaCommon common = new JavaCommon(ruleContext, javaSemantics, /* sources = */
    ImmutableList.<Artifact>of(), /* compileDeps = */
    targets, /* runtimeDeps = */
    targets, /* bothDeps = */
    targets);
    common.setJavaCompilationArtifacts(new JavaCompilationArtifacts.Builder().addRuntimeJar(mergedJar).addCompileTimeJar(mergedJar).build());
    return ruleBuilder.setFilesToBuild(filesToBuildBuilder.build()).addProvider(RunfilesProvider.class, RunfilesProvider.EMPTY).addProvider(AndroidResourcesProvider.class, resourceApk.toResourceProvider(ruleContext.getLabel())).addProvider(NativeLibsZipsProvider.class, new NativeLibsZipsProvider(AndroidCommon.collectTransitiveNativeLibsZips(ruleContext).add(nativeLibs).build())).addProvider(JavaRuntimeJarProvider.class, new JavaRuntimeJarProvider(ImmutableList.of(mergedJar))).addProvider(JavaCompilationArgsProvider.class, JavaCompilationArgsProvider.create(common.collectJavaCompilationArgs(/* recursive = */
    false, JavaCommon.isNeverLink(ruleContext), /* srcLessDepsExport = */
    false), common.collectJavaCompilationArgs(/* recursive = */
    true, JavaCommon.isNeverLink(ruleContext), /* srcLessDepsExport = */
    false))).addProvider(JavaRuleOutputJarsProvider.class, jarProviderBuilder.build()).build();
}
Also used : JavaRuleOutputJarsProvider(com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider) OutputJar(com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar) NestedSetBuilder(com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) JavaRuntimeJarProvider(com.google.devtools.build.lib.rules.java.JavaRuntimeJarProvider) Artifact(com.google.devtools.build.lib.actions.Artifact) FileProvider(com.google.devtools.build.lib.analysis.FileProvider) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) JavaCommon(com.google.devtools.build.lib.rules.java.JavaCommon)

Example 2 with JavaRuntimeJarProvider

use of com.google.devtools.build.lib.rules.java.JavaRuntimeJarProvider in project bazel by bazelbuild.

the class AndroidCommon method addTransitiveInfoProviders.

public RuleConfiguredTargetBuilder addTransitiveInfoProviders(RuleConfiguredTargetBuilder builder, AndroidSemantics androidSemantics, Artifact aar, ResourceApk resourceApk, Artifact zipAlignedApk, Iterable<Artifact> apksUnderTest) {
    idlHelper.addTransitiveInfoProviders(builder, classJar, manifestProtoOutput);
    if (generatedExtensionRegistryProvider != null) {
        builder.add(GeneratedExtensionRegistryProvider.class, generatedExtensionRegistryProvider);
    }
    OutputJar resourceJar = null;
    if (resourceClassJar != null && resourceSourceJar != null) {
        resourceJar = new OutputJar(resourceClassJar, null, ImmutableList.of(resourceSourceJar));
        javaRuleOutputJarsProviderBuilder.addOutputJar(resourceJar);
    }
    JavaRuleOutputJarsProvider ruleOutputJarsProvider = javaRuleOutputJarsProviderBuilder.addOutputJar(classJar, iJar, ImmutableList.of(srcJar)).setJdeps(outputDepsProto).build();
    JavaSourceJarsProvider sourceJarsProvider = javaSourceJarsProviderBuilder.build();
    JavaCompilationArgsProvider compilationArgsProvider = JavaCompilationArgsProvider.create(javaCompilationArgs, recursiveJavaCompilationArgs, compileTimeDependencyArtifacts, NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER));
    JavaSkylarkApiProvider.Builder skylarkApiProvider = JavaSkylarkApiProvider.builder().setRuleOutputJarsProvider(ruleOutputJarsProvider).setSourceJarsProvider(sourceJarsProvider).setCompilationArgsProvider(compilationArgsProvider);
    javaCommon.addTransitiveInfoProviders(builder, skylarkApiProvider, filesToBuild, classJar, ANDROID_COLLECTION_SPEC);
    javaCommon.addGenJarsProvider(builder, skylarkApiProvider, genClassJar, genSourceJar);
    return builder.setFilesToBuild(filesToBuild).addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, skylarkApiProvider.build()).add(JavaRuleOutputJarsProvider.class, ruleOutputJarsProvider).add(JavaSourceJarsProvider.class, sourceJarsProvider).add(JavaRuntimeJarProvider.class, new JavaRuntimeJarProvider(javaCommon.getJavaCompilationArtifacts().getRuntimeJars())).add(RunfilesProvider.class, RunfilesProvider.simple(getRunfiles())).add(AndroidResourcesProvider.class, resourceApk.toResourceProvider(ruleContext.getLabel())).add(AndroidIdeInfoProvider.class, createAndroidIdeInfoProvider(ruleContext, androidSemantics, idlHelper, resourceJar, aar, resourceApk, zipAlignedApk, apksUnderTest)).add(JavaCompilationArgsProvider.class, compilationArgsProvider).add(JackLibraryProvider.class, asNeverLink ? jackCompilationHelper.compileAsNeverlinkLibrary() : jackCompilationHelper.compileAsLibrary()).addSkylarkTransitiveInfo(AndroidSkylarkApiProvider.NAME, new AndroidSkylarkApiProvider()).addOutputGroup(OutputGroupProvider.HIDDEN_TOP_LEVEL, collectHiddenTopLevelArtifacts(ruleContext)).addOutputGroup(JavaSemantics.SOURCE_JARS_OUTPUT_GROUP, sourceJarsProvider.getTransitiveSourceJars());
}
Also used : JavaRuleOutputJarsProvider(com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider) JavaCompilationArgsProvider(com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider) OutputJar(com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar) JavaSourceJarsProvider(com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider) JavaSkylarkApiProvider(com.google.devtools.build.lib.rules.java.JavaSkylarkApiProvider) JavaRuntimeJarProvider(com.google.devtools.build.lib.rules.java.JavaRuntimeJarProvider) RunfilesProvider(com.google.devtools.build.lib.analysis.RunfilesProvider)

Example 3 with JavaRuntimeJarProvider

use of com.google.devtools.build.lib.rules.java.JavaRuntimeJarProvider in project bazel by bazelbuild.

the class DexArchiveAspect method desugarJarsIfRequested.

/**
   * Runs Jars in {@link JavaRuntimeJarProvider} through desugaring action if flag is set and adds
   * the result to {@code result}. Note that this cannot happen in a separate aspect because aspects
   * don't see providers added by other aspects executed on the same target.
   */
private Function<Artifact, Artifact> desugarJarsIfRequested(ConfiguredTarget base, RuleContext ruleContext, ConfiguredAspect.Builder result) {
    if (!getAndroidConfig(ruleContext).desugarJava8()) {
        return Functions.identity();
    }
    Map<Artifact, Artifact> newlyDesugared = new HashMap<>();
    if (JavaCommon.isNeverLink(ruleContext)) {
        result.addProvider(AndroidRuntimeJarProvider.NEVERLINK);
        return Functions.forMap(newlyDesugared);
    }
    AndroidRuntimeJarProvider.Builder desugaredJars = new AndroidRuntimeJarProvider.Builder().addTransitiveProviders(collectPrerequisites(ruleContext, AndroidRuntimeJarProvider.class));
    if (isProtoLibrary(ruleContext)) {
        // TODO(b/33557068): Desugar protos if needed instead of assuming they don't need desugaring
        result.addProvider(desugaredJars.build());
        return Functions.identity();
    }
    JavaRuntimeJarProvider jarProvider = base.getProvider(JavaRuntimeJarProvider.class);
    if (jarProvider != null) {
        // These are all transitive hjars of dependencies and hjar of the jar itself
        NestedSet<Artifact> compileTimeClasspath = getJavaCompilationArgsProvider(base, ruleContext).getRecursiveJavaCompilationArgs().getCompileTimeJars();
        // For android_* targets we need to honor their bootclasspath (nicer in general to do so)
        ImmutableList<Artifact> bootclasspath = getBootclasspath(base, ruleContext);
        boolean basenameClash = checkBasenameClash(jarProvider.getRuntimeJars());
        for (Artifact jar : jarProvider.getRuntimeJars()) {
            Artifact desugared = createDesugarAction(ruleContext, basenameClash, jar, bootclasspath, compileTimeClasspath);
            newlyDesugared.put(jar, desugared);
            desugaredJars.addDesugaredJar(jar, desugared);
        }
    }
    result.addProvider(desugaredJars.build());
    return Functions.forMap(newlyDesugared);
}
Also used : HashMap(java.util.HashMap) JavaRuntimeJarProvider(com.google.devtools.build.lib.rules.java.JavaRuntimeJarProvider) Artifact(com.google.devtools.build.lib.actions.Artifact)

Aggregations

JavaRuntimeJarProvider (com.google.devtools.build.lib.rules.java.JavaRuntimeJarProvider)3 Artifact (com.google.devtools.build.lib.actions.Artifact)2 JavaRuleOutputJarsProvider (com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider)2 OutputJar (com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar)2 FileProvider (com.google.devtools.build.lib.analysis.FileProvider)1 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)1 RunfilesProvider (com.google.devtools.build.lib.analysis.RunfilesProvider)1 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)1 NestedSetBuilder (com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder)1 JavaCommon (com.google.devtools.build.lib.rules.java.JavaCommon)1 JavaCompilationArgsProvider (com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider)1 JavaSkylarkApiProvider (com.google.devtools.build.lib.rules.java.JavaSkylarkApiProvider)1 JavaSourceJarsProvider (com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider)1 HashMap (java.util.HashMap)1