Search in sources :

Example 1 with JavaCommon

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

the class AndroidBinary method create.

@Override
public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException {
    JavaSemantics javaSemantics = createJavaSemantics();
    AndroidSemantics androidSemantics = createAndroidSemantics();
    if (!AndroidSdkProvider.verifyPresence(ruleContext)) {
        return null;
    }
    NestedSetBuilder<Artifact> filesBuilder = NestedSetBuilder.stableOrder();
    JavaCommon javaCommon = AndroidCommon.createJavaCommonWithAndroidDataBinding(ruleContext, javaSemantics, false);
    javaSemantics.checkRule(ruleContext, javaCommon);
    javaSemantics.checkForProtoLibraryAndJavaProtoLibraryOnSameProto(ruleContext, javaCommon);
    AndroidCommon androidCommon = new AndroidCommon(javaCommon, true, /* asNeverLink */
    true);
    ResourceDependencies resourceDeps = LocalResourceContainer.definesAndroidResources(ruleContext.attributes()) ? ResourceDependencies.fromRuleDeps(ruleContext, false) : ResourceDependencies.fromRuleResourceAndDeps(ruleContext, false);
    RuleConfiguredTargetBuilder builder = init(ruleContext, filesBuilder, resourceDeps, javaCommon, androidCommon, javaSemantics, androidSemantics);
    return builder.build();
}
Also used : RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) JavaSemantics(com.google.devtools.build.lib.rules.java.JavaSemantics) Artifact(com.google.devtools.build.lib.actions.Artifact) JavaCommon(com.google.devtools.build.lib.rules.java.JavaCommon)

Example 2 with JavaCommon

use of com.google.devtools.build.lib.rules.java.JavaCommon 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 3 with JavaCommon

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

the class AndroidLibrary method create.

@Override
public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException {
    validateRuleContext(ruleContext);
    JavaSemantics javaSemantics = createJavaSemantics();
    AndroidSemantics androidSemantics = createAndroidSemantics();
    if (!AndroidSdkProvider.verifyPresence(ruleContext)) {
        return null;
    }
    checkResourceInlining(ruleContext);
    NestedSetBuilder<Aar> transitiveAars = collectTransitiveAars(ruleContext);
    NestedSetBuilder<Artifact> proguardConfigsbuilder = NestedSetBuilder.stableOrder();
    proguardConfigsbuilder.addTransitive(new ProguardLibrary(ruleContext).collectProguardSpecs());
    AndroidIdlHelper.addSupportLibProguardConfigs(ruleContext, proguardConfigsbuilder);
    NestedSet<Artifact> transitiveProguardConfigs = proguardConfigsbuilder.build();
    JavaCommon javaCommon = AndroidCommon.createJavaCommonWithAndroidDataBinding(ruleContext, javaSemantics, true);
    javaSemantics.checkRule(ruleContext, javaCommon);
    AndroidCommon androidCommon = new AndroidCommon(javaCommon);
    boolean definesLocalResources = LocalResourceContainer.definesAndroidResources(ruleContext.attributes());
    if (definesLocalResources) {
        LocalResourceContainer.validateRuleContext(ruleContext);
    }
    final ResourceApk resourceApk;
    if (definesLocalResources) {
        ApplicationManifest applicationManifest = androidSemantics.getManifestForRule(ruleContext).renamePackage(ruleContext, AndroidCommon.getJavaPackage(ruleContext));
        resourceApk = applicationManifest.packWithDataAndResources(null, /* resourceApk -- not needed for library */
        ruleContext, true, /* isLibrary */
        ResourceDependencies.fromRuleDeps(ruleContext, JavaCommon.isNeverLink(ruleContext)), ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_R_TXT), ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_SYMBOLS), ResourceConfigurationFilter.empty(ruleContext), ImmutableList.<String>of(), /* uncompressedExtensions */
        false, /* crunchPng */
        ImmutableList.<String>of(), /* densities */
        false, /* incremental */
        null, /* proguardCfgOut */
        null, /* mainDexProguardCfg */
        ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_PROCESSED_MANIFEST), ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_RESOURCES_ZIP), DataBinding.isEnabled(ruleContext) ? DataBinding.getLayoutInfoFile(ruleContext) : null);
        if (ruleContext.hasErrors()) {
            return null;
        }
    } else {
        resourceApk = ResourceApk.fromTransitiveResources(ResourceDependencies.fromRuleResourceAndDeps(ruleContext, false));
    }
    if (!ruleContext.getFragment(AndroidConfiguration.class).allowSrcsLessAndroidLibraryDeps() && !definesLocalResources && ruleContext.attributes().get("srcs", BuildType.LABEL_LIST).isEmpty() && ruleContext.attributes().get("idl_srcs", BuildType.LABEL_LIST).isEmpty() && !ruleContext.attributes().get("deps", BuildType.LABEL_LIST).isEmpty()) {
        ruleContext.attributeError("deps", "deps not allowed without srcs; move to exports?");
    }
    JavaTargetAttributes javaTargetAttributes = androidCommon.init(javaSemantics, androidSemantics, resourceApk, false, /* addCoverageSupport */
    true, /* collectJavaCompilationArgs */
    false);
    if (javaTargetAttributes == null) {
        return null;
    }
    Artifact classesJar = ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_LIBRARY_CLASS_JAR);
    Artifact aarOut = ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_LIBRARY_AAR);
    final ResourceContainer primaryResources;
    final Aar aar;
    if (definesLocalResources) {
        primaryResources = resourceApk.getPrimaryResource();
        // applicationManifest has already been checked for nullness above in this method
        ApplicationManifest applicationManifest = androidSemantics.getManifestForRule(ruleContext);
        aar = Aar.create(aarOut, applicationManifest.getManifest());
        transitiveAars.add(aar);
    } else if (AndroidCommon.getAndroidResources(ruleContext) != null) {
        primaryResources = Iterables.getOnlyElement(AndroidCommon.getAndroidResources(ruleContext).getDirectAndroidResources());
        aar = Aar.create(aarOut, primaryResources.getManifest());
        transitiveAars.add(aar);
    } else {
        // there are no local resources and resources attribute was not specified either
        aar = null;
        ApplicationManifest applicationManifest = ApplicationManifest.generatedManifest(ruleContext).renamePackage(ruleContext, AndroidCommon.getJavaPackage(ruleContext));
        String javaPackage = AndroidCommon.getJavaPackage(ruleContext);
        ResourceContainer resourceContainer = ResourceContainer.builder().setLabel(ruleContext.getLabel()).setJavaPackageFromString(javaPackage).setManifest(applicationManifest.getManifest()).setJavaSourceJar(ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_JAVA_SOURCE_JAR)).setManifestExported(ruleContext.attributes().get("exports_manifest", Type.BOOLEAN)).setRTxt(ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_R_TXT)).build();
        primaryResources = new AndroidResourcesProcessorBuilder(ruleContext).setLibrary(true).setRTxtOut(resourceContainer.getRTxt()).setManifestOut(ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_PROCESSED_MANIFEST)).setSourceJarOut(resourceContainer.getJavaSourceJar()).setJavaPackage(resourceContainer.getJavaPackage()).withPrimary(resourceContainer).withDependencies(resourceApk.getResourceDependencies()).setDebug(ruleContext.getConfiguration().getCompilationMode() != CompilationMode.OPT).build(ruleContext);
    }
    new AarGeneratorBuilder(ruleContext).withPrimary(primaryResources).withManifest(aar != null ? aar.getManifest() : primaryResources.getManifest()).withRtxt(primaryResources.getRTxt()).withClasses(classesJar).setAAROut(aarOut).build(ruleContext);
    RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext);
    androidCommon.addTransitiveInfoProviders(builder, androidSemantics, aarOut, resourceApk, null, ImmutableList.<Artifact>of());
    androidSemantics.addTransitiveInfoProviders(builder, ruleContext, javaCommon, androidCommon, null);
    NestedSetBuilder<Artifact> transitiveResourcesJars = collectTransitiveResourceJars(ruleContext);
    if (androidCommon.getResourceClassJar() != null) {
        transitiveResourcesJars.add(androidCommon.getResourceClassJar());
    }
    builder.addProvider(NativeLibsZipsProvider.class, new NativeLibsZipsProvider(AndroidCommon.collectTransitiveNativeLibsZips(ruleContext).build())).add(JavaNeverlinkInfoProvider.class, new JavaNeverlinkInfoProvider(androidCommon.isNeverLink())).add(JavaSourceInfoProvider.class, JavaSourceInfoProvider.fromJavaTargetAttributes(javaTargetAttributes, javaSemantics)).add(AndroidCcLinkParamsProvider.class, AndroidCcLinkParamsProvider.create(androidCommon.getCcLinkParamsStore())).add(JavaPluginInfoProvider.class, JavaCommon.getTransitivePlugins(ruleContext)).add(ProguardSpecProvider.class, new ProguardSpecProvider(transitiveProguardConfigs)).addOutputGroup(OutputGroupProvider.HIDDEN_TOP_LEVEL, transitiveProguardConfigs).add(AndroidLibraryResourceClassJarProvider.class, AndroidLibraryResourceClassJarProvider.create(transitiveResourcesJars.build()));
    if (!JavaCommon.isNeverLink(ruleContext)) {
        builder.add(AndroidLibraryAarProvider.class, AndroidLibraryAarProvider.create(aar, transitiveAars.build()));
    }
    return builder.build();
}
Also used : JavaPluginInfoProvider(com.google.devtools.build.lib.rules.java.JavaPluginInfoProvider) ProguardLibrary(com.google.devtools.build.lib.rules.java.ProguardLibrary) ProguardSpecProvider(com.google.devtools.build.lib.rules.java.ProguardSpecProvider) JavaSemantics(com.google.devtools.build.lib.rules.java.JavaSemantics) Artifact(com.google.devtools.build.lib.actions.Artifact) JavaTargetAttributes(com.google.devtools.build.lib.rules.java.JavaTargetAttributes) Aar(com.google.devtools.build.lib.rules.android.AndroidLibraryAarProvider.Aar) JavaSourceInfoProvider(com.google.devtools.build.lib.rules.java.JavaSourceInfoProvider) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) JavaNeverlinkInfoProvider(com.google.devtools.build.lib.rules.java.JavaNeverlinkInfoProvider) JavaCommon(com.google.devtools.build.lib.rules.java.JavaCommon)

Aggregations

Artifact (com.google.devtools.build.lib.actions.Artifact)3 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)3 JavaCommon (com.google.devtools.build.lib.rules.java.JavaCommon)3 JavaSemantics (com.google.devtools.build.lib.rules.java.JavaSemantics)2 FileProvider (com.google.devtools.build.lib.analysis.FileProvider)1 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)1 NestedSetBuilder (com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder)1 Aar (com.google.devtools.build.lib.rules.android.AndroidLibraryAarProvider.Aar)1 JavaNeverlinkInfoProvider (com.google.devtools.build.lib.rules.java.JavaNeverlinkInfoProvider)1 JavaPluginInfoProvider (com.google.devtools.build.lib.rules.java.JavaPluginInfoProvider)1 JavaRuleOutputJarsProvider (com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider)1 OutputJar (com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar)1 JavaRuntimeJarProvider (com.google.devtools.build.lib.rules.java.JavaRuntimeJarProvider)1 JavaSourceInfoProvider (com.google.devtools.build.lib.rules.java.JavaSourceInfoProvider)1 JavaTargetAttributes (com.google.devtools.build.lib.rules.java.JavaTargetAttributes)1 ProguardLibrary (com.google.devtools.build.lib.rules.java.ProguardLibrary)1 ProguardSpecProvider (com.google.devtools.build.lib.rules.java.ProguardSpecProvider)1