Search in sources :

Example 1 with JavaExportsProvider

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

the class AndroidStudioInfoAspect method processDependencies.

private DependenciesResult processDependencies(ConfiguredTarget base, RuleContext ruleContext, AndroidStudioInfoFilesProvider.Builder providerBuilder) {
    // Calculate direct dependencies
    ImmutableList.Builder<TransitiveInfoCollection> directDepsBuilder = ImmutableList.builder();
    for (PrerequisiteAttr prerequisiteAttr : prerequisiteAttrs) {
        if (ruleContext.attributes().has(prerequisiteAttr.name, prerequisiteAttr.type)) {
            Mode mode = ruleContext.getAttributeMode(prerequisiteAttr.name);
            if (mode == Mode.TARGET || mode == Mode.SPLIT) {
                directDepsBuilder.addAll(ruleContext.getPrerequisites(prerequisiteAttr.name, Mode.TARGET));
            }
        }
    }
    List<TransitiveInfoCollection> directDeps = directDepsBuilder.build();
    // Add exports from direct dependencies
    NestedSetBuilder<Label> dependenciesBuilder = NestedSetBuilder.stableOrder();
    for (AndroidStudioInfoFilesProvider depProvider : AnalysisUtils.getProviders(directDeps, AndroidStudioInfoFilesProvider.class)) {
        dependenciesBuilder.addTransitive(depProvider.getExportedDeps());
    }
    for (TransitiveInfoCollection dep : directDeps) {
        dependenciesBuilder.add(dep.getLabel());
    }
    NestedSet<Label> dependencies = dependenciesBuilder.build();
    // Propagate my own exports
    JavaExportsProvider javaExportsProvider = base.getProvider(JavaExportsProvider.class);
    if (javaExportsProvider != null) {
        providerBuilder.exportedDepsBuilder().addTransitive(javaExportsProvider.getTransitiveExports());
    }
    // android_library without sources exports all its deps
    if (ruleContext.getRule().getRuleClass().equals("android_library")) {
        JavaSourceInfoProvider sourceInfoProvider = base.getProvider(JavaSourceInfoProvider.class);
        boolean hasSources = sourceInfoProvider != null && !sourceInfoProvider.getSourceFiles().isEmpty();
        if (!hasSources) {
            for (TransitiveInfoCollection dep : directDeps) {
                providerBuilder.exportedDepsBuilder().add(dep.getLabel());
            }
        }
    }
    // runtime_deps
    List<? extends TransitiveInfoCollection> runtimeDeps = ImmutableList.of();
    NestedSetBuilder<Label> runtimeDepsBuilder = NestedSetBuilder.stableOrder();
    if (ruleContext.attributes().has("runtime_deps", BuildType.LABEL_LIST)) {
        runtimeDeps = ruleContext.getPrerequisites("runtime_deps", Mode.TARGET);
        for (TransitiveInfoCollection dep : runtimeDeps) {
            runtimeDepsBuilder.add(dep.getLabel());
        }
    }
    // resources
    @Nullable TransitiveInfoCollection resources = ruleContext.attributes().has("resources", BuildType.LABEL) ? ruleContext.getPrerequisite("resources", Mode.TARGET) : null;
    // Propagate providers from all prerequisites (deps + runtime_deps)
    ImmutableList.Builder<TransitiveInfoCollection> prerequisitesBuilder = ImmutableList.builder();
    prerequisitesBuilder.addAll(directDeps);
    prerequisitesBuilder.addAll(runtimeDeps);
    if (resources != null) {
        prerequisitesBuilder.add(resources);
    }
    List<TransitiveInfoCollection> prerequisites = prerequisitesBuilder.build();
    for (AndroidStudioInfoFilesProvider depProvider : AnalysisUtils.getProviders(prerequisites, AndroidStudioInfoFilesProvider.class)) {
        providerBuilder.ideInfoFilesBuilder().addTransitive(depProvider.getIdeInfoFiles());
        providerBuilder.ideInfoTextFilesBuilder().addTransitive(depProvider.getIdeInfoTextFiles());
        providerBuilder.ideResolveFilesBuilder().addTransitive(depProvider.getIdeResolveFiles());
    }
    return new DependenciesResult(dependencies, runtimeDepsBuilder.build(), resources != null ? resources.getLabel() : null);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Mode(com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode) Label(com.google.devtools.build.lib.cmdline.Label) JavaExportsProvider(com.google.devtools.build.lib.rules.java.JavaExportsProvider) JavaSourceInfoProvider(com.google.devtools.build.lib.rules.java.JavaSourceInfoProvider) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) Nullable(javax.annotation.Nullable)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 Mode (com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode)1 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)1 Label (com.google.devtools.build.lib.cmdline.Label)1 JavaExportsProvider (com.google.devtools.build.lib.rules.java.JavaExportsProvider)1 JavaSourceInfoProvider (com.google.devtools.build.lib.rules.java.JavaSourceInfoProvider)1 Nullable (javax.annotation.Nullable)1