Search in sources :

Example 1 with OutputJar

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

the class AarImportTest method testClassesJarProvided.

@Test
public void testClassesJarProvided() throws Exception {
    ConfiguredTarget aarImportTarget = getConfiguredTarget("//a:foo");
    Iterable<OutputJar> outputJars = aarImportTarget.getProvider(JavaRuleOutputJarsProvider.class).getOutputJars();
    assertThat(outputJars).hasSize(1);
    Artifact classesJar = outputJars.iterator().next().getClassJar();
    assertThat(classesJar.getFilename()).isEqualTo("classes_and_libs_merged.jar");
    SpawnAction jarMergingAction = ((SpawnAction) getGeneratingAction(classesJar));
    assertThat(jarMergingAction.getArguments()).contains("--dont_change_compression");
}
Also used : SpawnAction(com.google.devtools.build.lib.analysis.actions.SpawnAction) JavaRuleOutputJarsProvider(com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider) OutputJar(com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) FileConfiguredTarget(com.google.devtools.build.lib.analysis.FileConfiguredTarget) Artifact(com.google.devtools.build.lib.actions.Artifact) Test(org.junit.Test)

Example 2 with OutputJar

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

the class AndroidStudioInfoAspect method makeAndroidIdeInfo.

private static AndroidIdeInfo makeAndroidIdeInfo(AndroidIdeInfoProvider androidIdeInfoProvider, DependenciesResult dependenciesResult, NestedSetBuilder<Artifact> ideResolveArtifacts) {
    AndroidIdeInfo.Builder builder = AndroidIdeInfo.newBuilder();
    if (androidIdeInfoProvider.getSignedApk() != null) {
        builder.setApk(makeArtifactLocation(androidIdeInfoProvider.getSignedApk()));
    }
    Artifact manifest = androidIdeInfoProvider.getManifest();
    if (manifest != null) {
        builder.setManifest(makeArtifactLocation(manifest));
        addResolveArtifact(ideResolveArtifacts, manifest);
    }
    for (Artifact artifact : androidIdeInfoProvider.getApksUnderTest()) {
        builder.addDependencyApk(makeArtifactLocation(artifact));
    }
    for (SourceDirectory resourceDir : androidIdeInfoProvider.getResourceDirs()) {
        ArtifactLocation artifactLocation = makeArtifactLocation(resourceDir);
        builder.addResources(artifactLocation);
    }
    if (androidIdeInfoProvider.getJavaPackage() != null) {
        builder.setJavaPackage(androidIdeInfoProvider.getJavaPackage());
    }
    String idlImportRoot = androidIdeInfoProvider.getIdlImportRoot();
    if (idlImportRoot != null) {
        builder.setIdlImportRoot(idlImportRoot);
    }
    boolean hasIdlSources = !androidIdeInfoProvider.getIdlSrcs().isEmpty();
    builder.setHasIdlSources(hasIdlSources);
    if (hasIdlSources) {
        LibraryArtifact idlLibraryArtifact = makeLibraryArtifact(ideResolveArtifacts, androidIdeInfoProvider.getIdlClassJar(), null, androidIdeInfoProvider.getIdlSourceJar());
        if (idlLibraryArtifact != null) {
            builder.setIdlJar(idlLibraryArtifact);
        }
    }
    builder.setGenerateResourceClass(androidIdeInfoProvider.definesAndroidResources());
    if (dependenciesResult.resources != null) {
        builder.setLegacyResources(dependenciesResult.resources.toString());
    }
    OutputJar resourceJar = androidIdeInfoProvider.getResourceJar();
    if (resourceJar != null) {
        LibraryArtifact resourceLibraryArtifact = makeLibraryArtifact(ideResolveArtifacts, resourceJar.getClassJar(), resourceJar.getIJar(), resourceJar.getSrcJar());
        if (resourceLibraryArtifact != null) {
            builder.setResourceJar(resourceLibraryArtifact);
        }
    }
    return builder.build();
}
Also used : OutputJar(com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar) ArtifactLocation(com.google.devtools.intellij.ideinfo.IntellijIdeInfo.ArtifactLocation) SourceDirectory(com.google.devtools.build.lib.rules.android.AndroidIdeInfoProvider.SourceDirectory) AndroidIdeInfo(com.google.devtools.intellij.ideinfo.IntellijIdeInfo.AndroidIdeInfo) LibraryArtifact(com.google.devtools.intellij.ideinfo.IntellijIdeInfo.LibraryArtifact) Artifact(com.google.devtools.build.lib.actions.Artifact) LibraryArtifact(com.google.devtools.intellij.ideinfo.IntellijIdeInfo.LibraryArtifact)

Example 3 with OutputJar

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

the class AndroidStudioInfoAspect method makeJavaIdeInfo.

private JavaIdeInfo makeJavaIdeInfo(ConfiguredTarget base, RuleContext ruleContext, JavaRuleOutputJarsProvider outputJarsProvider, AndroidStudioInfoFilesProvider.Builder providerBuilder) {
    NestedSetBuilder<Artifact> ideResolveArtifacts = providerBuilder.ideResolveFilesBuilder();
    JavaIdeInfo.Builder builder = JavaIdeInfo.newBuilder();
    List<Artifact> javaSources = Lists.newArrayList();
    List<Artifact> generatedJavaSources = Lists.newArrayList();
    List<Artifact> srcjars = Lists.newArrayList();
    divideJavaSources(ruleContext, javaSources, generatedJavaSources, srcjars);
    if (!javaSources.isEmpty()) {
        Artifact packageManifest = derivedArtifact(base, ruleContext, ".manifest");
        providerBuilder.ideInfoFilesBuilder().add(packageManifest);
        ruleContext.registerAction(makePackageManifestAction(ruleContext, packageManifest, javaSources));
        builder.setPackageManifest(makeArtifactLocation(packageManifest));
    }
    // This can be removed once android_resources is deleted
    if (ruleContext.attributes().has("resources", BuildType.LABEL) && ruleContext.getRule().getRuleClass().startsWith("android_")) {
        srcjars = ImmutableList.of();
    }
    if (!javaSources.isEmpty() && (!generatedJavaSources.isEmpty() || !srcjars.isEmpty())) {
        Artifact filteredGenJar = derivedArtifact(base, ruleContext, "-filtered-gen.jar");
        Artifact filteredGenSrcJar = derivedArtifact(base, ruleContext, "-filtered-gen-src.jar");
        List<Artifact> jars = Lists.newArrayList();
        List<Artifact> sourceJars = Lists.newArrayList();
        for (OutputJar outputJar : outputJarsProvider.getOutputJars()) {
            Artifact jar = outputJar.getIJar();
            if (jar == null) {
                jar = outputJar.getClassJar();
            }
            if (jar != null) {
                jars.add(jar);
            }
            if (outputJar.getSrcJar() != null) {
                sourceJars.add(outputJar.getSrcJar());
            }
        }
        ruleContext.registerAction(makeFilteredJarAction(ruleContext, jars, sourceJars, generatedJavaSources, srcjars, filteredGenJar, filteredGenSrcJar));
        ideResolveArtifacts.add(filteredGenJar);
        ideResolveArtifacts.add(filteredGenSrcJar);
        builder.setFilteredGenJar(makeLibraryArtifact(ideResolveArtifacts, filteredGenJar, null, filteredGenSrcJar));
    }
    collectJarsFromOutputJarsProvider(builder, ideResolveArtifacts, outputJarsProvider);
    Artifact jdeps = outputJarsProvider.getJdeps();
    if (jdeps != null) {
        builder.setJdeps(makeArtifactLocation(jdeps));
    }
    JavaGenJarsProvider genJarsProvider = base.getProvider(JavaGenJarsProvider.class);
    if (genJarsProvider != null) {
        collectGenJars(builder, ideResolveArtifacts, genJarsProvider);
    }
    Collection<Artifact> sourceFiles = getSources(ruleContext);
    for (Artifact sourceFile : sourceFiles) {
        builder.addSources(makeArtifactLocation(sourceFile));
    }
    return builder.build();
}
Also used : OutputJar(com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar) JavaGenJarsProvider(com.google.devtools.build.lib.rules.java.JavaGenJarsProvider) JavaIdeInfo(com.google.devtools.intellij.ideinfo.IntellijIdeInfo.JavaIdeInfo) Artifact(com.google.devtools.build.lib.actions.Artifact) LibraryArtifact(com.google.devtools.intellij.ideinfo.IntellijIdeInfo.LibraryArtifact)

Example 4 with OutputJar

use of com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar 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 5 with OutputJar

use of com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar 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)

Aggregations

OutputJar (com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar)5 Artifact (com.google.devtools.build.lib.actions.Artifact)4 JavaRuleOutputJarsProvider (com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider)3 JavaRuntimeJarProvider (com.google.devtools.build.lib.rules.java.JavaRuntimeJarProvider)2 LibraryArtifact (com.google.devtools.intellij.ideinfo.IntellijIdeInfo.LibraryArtifact)2 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 FileConfiguredTarget (com.google.devtools.build.lib.analysis.FileConfiguredTarget)1 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 SpawnAction (com.google.devtools.build.lib.analysis.actions.SpawnAction)1 NestedSetBuilder (com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder)1 SourceDirectory (com.google.devtools.build.lib.rules.android.AndroidIdeInfoProvider.SourceDirectory)1 JavaCommon (com.google.devtools.build.lib.rules.java.JavaCommon)1 JavaCompilationArgsProvider (com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider)1 JavaGenJarsProvider (com.google.devtools.build.lib.rules.java.JavaGenJarsProvider)1 JavaSkylarkApiProvider (com.google.devtools.build.lib.rules.java.JavaSkylarkApiProvider)1 JavaSourceJarsProvider (com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider)1 AndroidIdeInfo (com.google.devtools.intellij.ideinfo.IntellijIdeInfo.AndroidIdeInfo)1