Search in sources :

Example 16 with TransitiveInfoCollection

use of com.google.devtools.build.lib.analysis.TransitiveInfoCollection in project bazel by bazelbuild.

the class JavaToolchain method getArtifact.

private static Artifact getArtifact(String attributeName, RuleContext ruleContext) {
    TransitiveInfoCollection prerequisite = ruleContext.getPrerequisite(attributeName, Mode.HOST);
    if (prerequisite == null) {
        return null;
    }
    Iterable<Artifact> artifacts = prerequisite.getProvider(FileProvider.class).getFilesToBuild();
    if (Iterables.size(artifacts) != 1) {
        ruleContext.attributeError(attributeName, prerequisite.getLabel() + " expected a single artifact");
        return null;
    }
    return Iterables.getOnlyElement(artifacts);
}
Also used : FileProvider(com.google.devtools.build.lib.analysis.FileProvider) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 17 with TransitiveInfoCollection

use of com.google.devtools.build.lib.analysis.TransitiveInfoCollection in project bazel by bazelbuild.

the class JavaCommon method collectTransitiveExports.

/**
   * Collects labels of targets and artifacts reached transitively via the "exports" attribute.
   */
protected JavaExportsProvider collectTransitiveExports() {
    NestedSetBuilder<Label> builder = NestedSetBuilder.stableOrder();
    List<TransitiveInfoCollection> currentRuleExports = getExports(ruleContext);
    builder.addAll(Iterables.transform(currentRuleExports, GET_COLLECTION_LABEL));
    for (TransitiveInfoCollection dep : currentRuleExports) {
        JavaExportsProvider exportsProvider = dep.getProvider(JavaExportsProvider.class);
        if (exportsProvider != null) {
            builder.addTransitive(exportsProvider.getTransitiveExports());
        }
    }
    return new JavaExportsProvider(builder.build());
}
Also used : Label(com.google.devtools.build.lib.cmdline.Label) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection)

Example 18 with TransitiveInfoCollection

use of com.google.devtools.build.lib.analysis.TransitiveInfoCollection in project bazel by bazelbuild.

the class JavaCommon method checkRuntimeDeps.

/**
   * Sanity checks the given runtime dependencies, and emits errors if there is a problem.
   * Also called by {@link #initCommon()} for the current target's runtime dependencies.
   */
public static void checkRuntimeDeps(RuleContext ruleContext, List<TransitiveInfoCollection> runtimeDepInfo) {
    for (TransitiveInfoCollection c : runtimeDepInfo) {
        JavaNeverlinkInfoProvider neverLinkedness = c.getProvider(JavaNeverlinkInfoProvider.class);
        if (neverLinkedness == null) {
            continue;
        }
        boolean reportError = !ruleContext.getConfiguration().getAllowRuntimeDepsOnNeverLink();
        if (neverLinkedness.isNeverlink()) {
            String msg = String.format("neverlink dep %s not allowed in runtime deps", c.getLabel());
            if (reportError) {
                ruleContext.attributeError("runtime_deps", msg);
            } else {
                ruleContext.attributeWarning("runtime_deps", msg);
            }
        }
    }
}
Also used : TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection)

Example 19 with TransitiveInfoCollection

use of com.google.devtools.build.lib.analysis.TransitiveInfoCollection in project bazel by bazelbuild.

the class JavaCommon method collectTargetsTreatedAsDeps.

private static ImmutableList<TransitiveInfoCollection> collectTargetsTreatedAsDeps(RuleContext ruleContext, JavaSemantics semantics, ClasspathType type) {
    ImmutableList.Builder<TransitiveInfoCollection> builder = new Builder<>();
    if (!type.equals(ClasspathType.COMPILE_ONLY)) {
        builder.addAll(getRuntimeDeps(ruleContext));
        builder.addAll(getExports(ruleContext));
    }
    builder.addAll(ruleContext.getPrerequisites("deps", Mode.TARGET));
    semantics.collectTargetsTreatedAsDeps(ruleContext, builder);
    // Implicitly add dependency on java launcher cc_binary when --java_launcher= is enabled,
    // or when launcher attribute is specified in a build rule.
    TransitiveInfoCollection launcher = JavaHelper.launcherForTarget(semantics, ruleContext);
    if (launcher != null) {
        builder.add(launcher);
    }
    return builder.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) NestedSetBuilder(com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) Builder(com.google.common.collect.ImmutableList.Builder) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection)

Example 20 with TransitiveInfoCollection

use of com.google.devtools.build.lib.analysis.TransitiveInfoCollection in project bazel by bazelbuild.

the class AndroidBinary method init.

private static RuleConfiguredTargetBuilder init(RuleContext ruleContext, NestedSetBuilder<Artifact> filesBuilder, ResourceDependencies resourceDeps, JavaCommon javaCommon, AndroidCommon androidCommon, JavaSemantics javaSemantics, AndroidSemantics androidSemantics) throws InterruptedException, RuleErrorException {
    validateRuleContext(ruleContext);
    // TODO(bazel-team): Find a way to simplify this code.
    // treeKeys() means that the resulting map sorts the entries by key, which is necessary to
    // ensure determinism.
    Multimap<String, TransitiveInfoCollection> depsByArchitecture = MultimapBuilder.treeKeys().arrayListValues().build();
    AndroidConfiguration androidConfig = ruleContext.getFragment(AndroidConfiguration.class);
    for (Map.Entry<Optional<String>, ? extends List<? extends TransitiveInfoCollection>> entry : ruleContext.getSplitPrerequisites("deps").entrySet()) {
        String cpu = entry.getKey().or(androidConfig.getCpu());
        depsByArchitecture.putAll(cpu, entry.getValue());
    }
    Map<String, BuildConfiguration> configurationMap = new LinkedHashMap<>();
    Map<String, CcToolchainProvider> toolchainMap = new LinkedHashMap<>();
    for (Map.Entry<Optional<String>, ? extends List<? extends TransitiveInfoCollection>> entry : ruleContext.getSplitPrerequisites(":cc_toolchain_split").entrySet()) {
        String cpu = entry.getKey().or(androidConfig.getCpu());
        TransitiveInfoCollection dep = Iterables.getOnlyElement(entry.getValue());
        CcToolchainProvider toolchain = CppHelper.getToolchain(ruleContext, dep);
        configurationMap.put(cpu, dep.getConfiguration());
        toolchainMap.put(cpu, toolchain);
    }
    NativeLibs nativeLibs = NativeLibs.fromLinkedNativeDeps(ruleContext, androidSemantics.getNativeDepsFileName(), depsByArchitecture, toolchainMap, configurationMap);
    // TODO(bazel-team): Resolve all the different cases of resource handling so this conditional
    // can go away: recompile from android_resources, and recompile from android_binary attributes.
    ApplicationManifest applicationManifest;
    ResourceApk resourceApk;
    ResourceApk incrementalResourceApk;
    ResourceApk instantRunResourceApk;
    ResourceApk splitResourceApk;
    if (LocalResourceContainer.definesAndroidResources(ruleContext.attributes())) {
        // Retrieve and compile the resources defined on the android_binary rule.
        LocalResourceContainer.validateRuleContext(ruleContext);
        ApplicationManifest ruleManifest = androidSemantics.getManifestForRule(ruleContext);
        applicationManifest = ruleManifest.mergeWith(ruleContext, resourceDeps);
        resourceApk = applicationManifest.packWithDataAndResources(ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_RESOURCES_APK), ruleContext, false, /* isLibrary */
        resourceDeps, ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_R_TXT), null, /* Artifact symbolsTxt */
        ResourceConfigurationFilter.fromRuleContext(ruleContext), ruleContext.getTokenizedStringListAttr("nocompress_extensions"), ruleContext.attributes().get("crunch_png", Type.BOOLEAN), ruleContext.getTokenizedStringListAttr("densities"), false, /* incremental */
        ProguardHelper.getProguardConfigArtifact(ruleContext, ""), createMainDexProguardSpec(ruleContext), ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_PROCESSED_MANIFEST), ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_RESOURCES_ZIP), DataBinding.isEnabled(ruleContext) ? DataBinding.getLayoutInfoFile(ruleContext) : null);
        ruleContext.assertNoErrors();
        incrementalResourceApk = applicationManifest.addMobileInstallStubApplication(ruleContext).packWithDataAndResources(ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_INCREMENTAL_RESOURCES_APK), ruleContext, false, /* isLibrary */
        resourceDeps, null, /* Artifact rTxt */
        null, /* Artifact symbolsTxt */
        ResourceConfigurationFilter.fromRuleContext(ruleContext), ruleContext.getTokenizedStringListAttr("nocompress_extensions"), ruleContext.attributes().get("crunch_png", Type.BOOLEAN), ruleContext.getTokenizedStringListAttr("densities"), true, /* incremental */
        ProguardHelper.getProguardConfigArtifact(ruleContext, "incremental"), null, /* mainDexProguardCfg */
        null, /* manifestOut */
        null, /* mergedResourcesOut */
        null);
        ruleContext.assertNoErrors();
        instantRunResourceApk = applicationManifest.addInstantRunStubApplication(ruleContext).packWithDataAndResources(getDxArtifact(ruleContext, "android_instant_run.ap_"), ruleContext, false, /* isLibrary */
        resourceDeps, null, /* Artifact rTxt */
        null, /* Artifact symbolsTxt */
        ResourceConfigurationFilter.fromRuleContext(ruleContext), ruleContext.getTokenizedStringListAttr("nocompress_extensions"), ruleContext.attributes().get("crunch_png", Type.BOOLEAN), ruleContext.getTokenizedStringListAttr("densities"), true, /* incremental */
        ProguardHelper.getProguardConfigArtifact(ruleContext, "instant_run"), null, /* mainDexProguardCfg */
        null, /* manifestOut */
        null, /* mergedResourcesOut */
        null);
        ruleContext.assertNoErrors();
        splitResourceApk = applicationManifest.createSplitManifest(ruleContext, "android_resources", false).packWithDataAndResources(getDxArtifact(ruleContext, "android_resources.ap_"), ruleContext, false, /* isLibrary */
        resourceDeps, null, /* Artifact rTxt */
        null, /* Artifact symbolsTxt */
        ResourceConfigurationFilter.fromRuleContext(ruleContext), ruleContext.getTokenizedStringListAttr("nocompress_extensions"), ruleContext.attributes().get("crunch_png", Type.BOOLEAN), ruleContext.getTokenizedStringListAttr("densities"), true, /* incremental */
        ProguardHelper.getProguardConfigArtifact(ruleContext, "incremental_split"), null, /* mainDexProguardCfg */
        null, /* manifestOut */
        null, /* mergedResourcesOut */
        null);
        ruleContext.assertNoErrors();
    } else {
        if (!ruleContext.attributes().get("crunch_png", Type.BOOLEAN)) {
            ruleContext.throwWithRuleError("Setting crunch_png = 0 is not supported for android_binary" + " rules which depend on android_resources rules.");
        }
        // Retrieve the resources from the resources attribute on the android_binary rule
        // and recompile them if necessary.
        ApplicationManifest resourcesManifest = ApplicationManifest.fromResourcesRule(ruleContext);
        if (resourcesManifest == null) {
            throw new RuleErrorException();
        }
        applicationManifest = resourcesManifest.mergeWith(ruleContext, resourceDeps);
        // Always recompiling resources causes AndroidTest to fail in certain circumstances.
        if (shouldRegenerate(ruleContext, resourceDeps)) {
            resourceApk = applicationManifest.packWithResources(ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_RESOURCES_APK), ruleContext, resourceDeps, true, /* createSource */
            ProguardHelper.getProguardConfigArtifact(ruleContext, ""), createMainDexProguardSpec(ruleContext));
            ruleContext.assertNoErrors();
        } else {
            resourceApk = applicationManifest.useCurrentResources(ruleContext, ProguardHelper.getProguardConfigArtifact(ruleContext, ""), createMainDexProguardSpec(ruleContext));
            ruleContext.assertNoErrors();
        }
        incrementalResourceApk = applicationManifest.addMobileInstallStubApplication(ruleContext).packWithResources(ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_INCREMENTAL_RESOURCES_APK), ruleContext, resourceDeps, false, /* createSource */
        ProguardHelper.getProguardConfigArtifact(ruleContext, "incremental"), null);
        ruleContext.assertNoErrors();
        instantRunResourceApk = applicationManifest.addInstantRunStubApplication(ruleContext).packWithResources(getDxArtifact(ruleContext, "android_instant_run.ap_"), ruleContext, resourceDeps, false, /* createSource */
        ProguardHelper.getProguardConfigArtifact(ruleContext, "instant_run"), null);
        ruleContext.assertNoErrors();
        splitResourceApk = applicationManifest.createSplitManifest(ruleContext, "android_resources", false).packWithResources(getDxArtifact(ruleContext, "android_resources.ap_"), ruleContext, resourceDeps, false, /* createSource */
        ProguardHelper.getProguardConfigArtifact(ruleContext, "incremental_split"), null);
        ruleContext.assertNoErrors();
    }
    boolean shrinkResources = shouldShrinkResources(ruleContext);
    JavaTargetAttributes resourceClasses = androidCommon.init(javaSemantics, androidSemantics, resourceApk, ruleContext.getConfiguration().isCodeCoverageEnabled(), true, /* collectJavaCompilationArgs */
    true);
    ruleContext.assertNoErrors();
    Function<Artifact, Artifact> derivedJarFunction = collectDesugaredJars(ruleContext, androidCommon, androidSemantics, resourceClasses);
    Artifact deployJar = createDeployJar(ruleContext, javaSemantics, androidCommon, resourceClasses, derivedJarFunction);
    Artifact proguardMapping = ruleContext.getPrerequisiteArtifact("proguard_apply_mapping", Mode.TARGET);
    return createAndroidBinary(ruleContext, filesBuilder, deployJar, derivedJarFunction, /* isBinaryJarFiltered */
    false, javaCommon, androidCommon, javaSemantics, androidSemantics, nativeLibs, applicationManifest, resourceApk, incrementalResourceApk, instantRunResourceApk, splitResourceApk, shrinkResources, resourceClasses, ImmutableList.<Artifact>of(), ImmutableList.<Artifact>of(), proguardMapping);
}
Also used : Optional(com.google.common.base.Optional) CcToolchainProvider(com.google.devtools.build.lib.rules.cpp.CcToolchainProvider) JavaTargetAttributes(com.google.devtools.build.lib.rules.java.JavaTargetAttributes) Artifact(com.google.devtools.build.lib.actions.Artifact) LinkedHashMap(java.util.LinkedHashMap) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)54 Artifact (com.google.devtools.build.lib.actions.Artifact)27 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)14 FileProvider (com.google.devtools.build.lib.analysis.FileProvider)11 NestedSetBuilder (com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder)11 Label (com.google.devtools.build.lib.cmdline.Label)10 ImmutableList (com.google.common.collect.ImmutableList)8 ArrayList (java.util.ArrayList)8 Runfiles (com.google.devtools.build.lib.analysis.Runfiles)7 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)5 Test (org.junit.Test)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 FilesToRunProvider (com.google.devtools.build.lib.analysis.FilesToRunProvider)4 RunfilesProvider (com.google.devtools.build.lib.analysis.RunfilesProvider)4 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)4 Pair (com.google.devtools.build.lib.util.Pair)4 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)4 Map (java.util.Map)4 CustomCommandLine (com.google.devtools.build.lib.analysis.actions.CustomCommandLine)3 CcLinkParamsProvider (com.google.devtools.build.lib.rules.cpp.CcLinkParamsProvider)3