Пример #1
0
  public RuleConfiguredTargetBuilder addTransitiveInfoProviders(
      RuleConfiguredTargetBuilder builder,
      AndroidSemantics androidSemantics,
      ResourceApk resourceApk,
      Artifact zipAlignedApk,
      Iterable<Artifact> apksUnderTest) {

    javaCommon.addTransitiveInfoProviders(builder, filesToBuild, classJar, ANDROID_COLLECTION_SPEC);
    javaCommon.addGenJarsProvider(builder, genClassJar, genSourceJar);
    idlHelper.addTransitiveInfoProviders(builder, classJar, manifestProtoOutput);

    JavaRuleOutputJarsProvider.Builder outputJarsBuilder =
        JavaRuleOutputJarsProvider.builder()
            .addOutputJar(classJar, iJar, srcJar)
            .setJdeps(outputDepsProto);
    OutputJar resourceJar = null;
    if (resourceClassJar != null && resourceIJar != null && resourceSourceJar != null) {
      resourceJar = new OutputJar(resourceClassJar, resourceIJar, resourceSourceJar);
      outputJarsBuilder.addOutputJar(resourceJar);
    }

    return builder
        .setFilesToBuild(filesToBuild)
        .add(JavaRuleOutputJarsProvider.class, outputJarsBuilder.build())
        .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,
                resourceApk,
                zipAlignedApk,
                apksUnderTest))
        .add(
            JavaCompilationArgsProvider.class,
            new JavaCompilationArgsProvider(
                javaCompilationArgs,
                recursiveJavaCompilationArgs,
                compileTimeDependencyArtifacts,
                NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER)))
        .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, transitiveSourceJars);
  }
Пример #2
0
 private Runfiles getRunfiles() {
   // TODO(bazel-team): why return any Runfiles in the neverlink case?
   if (asNeverLink) {
     return new Runfiles.Builder(
             ruleContext.getWorkspaceName(),
             ruleContext.getConfiguration().legacyExternalRunfiles())
         .addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES)
         .build();
   }
   return JavaCommon.getRunfiles(
       ruleContext,
       javaCommon.getJavaSemantics(),
       javaCommon.getJavaCompilationArtifacts(),
       asNeverLink);
 }
Пример #3
0
  private JavaCompilationHelper initAttributes(
      JavaTargetAttributes.Builder attributes, JavaSemantics semantics) {
    JavaCompilationHelper helper =
        new JavaCompilationHelper(ruleContext, semantics, javaCommon.getJavacOpts(), attributes);

    helper.addLibrariesToAttributes(javaCommon.targetsTreatedAsDeps(ClasspathType.COMPILE_ONLY));
    helper.addProvidersToAttributes(
        JavaCommon.compilationArgsFromSources(ruleContext), asNeverLink);
    attributes.setStrictJavaDeps(getStrictAndroidDeps());
    attributes.setRuleKind(ruleContext.getRule().getRuleClass());
    attributes.setTargetLabel(ruleContext.getLabel());

    JavaCommon.validateConstraint(
        ruleContext, "android", javaCommon.targetsTreatedAsDeps(ClasspathType.BOTH));
    ruleContext.checkSrcsSamePackage(true);
    return helper;
  }
Пример #4
0
 /**
  * Collects Java compilation arguments for this target.
  *
  * @param recursive Whether to scan dependencies recursively.
  * @param isNeverLink Whether the target has the 'neverlink' attr.
  * @param hasSrcs If false, deps are exported (deprecated behaviour)
  */
 private JavaCompilationArgs collectJavaCompilationArgs(
     boolean recursive, boolean isNeverLink, boolean hasSrcs) {
   boolean exportDeps =
       !hasSrcs
           && ruleContext
               .getFragment(AndroidConfiguration.class)
               .allowSrcsLessAndroidLibraryDeps();
   Iterable<SourcesJavaCompilationArgsProvider> fromSrcs =
       ImmutableList.<SourcesJavaCompilationArgsProvider>of();
   return javaCommon.collectJavaCompilationArgs(recursive, isNeverLink, fromSrcs, exportDeps);
 }
Пример #5
0
 JackCompilationHelper initJack(JavaTargetAttributes attributes) throws InterruptedException {
   AndroidSdkProvider sdk = AndroidSdkProvider.fromRuleContext(ruleContext);
   return new JackCompilationHelper.Builder()
       // blaze infrastructure
       .setRuleContext(ruleContext)
       // configuration
       .setOutputArtifact(
           ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_LIBRARY_JACK_FILE))
       // tools
       .setJackBinary(sdk.getJack())
       .setJillBinary(sdk.getJill())
       .setResourceExtractorBinary(sdk.getResourceExtractor())
       .setJackBaseClasspath(sdk.getAndroidBaseClasspathForJack())
       // sources
       .addJavaSources(attributes.getSourceFiles())
       .addSourceJars(attributes.getSourceJars())
       .addResources(attributes.getResources())
       .addProcessorNames(attributes.getProcessorNames())
       .addProcessorClasspathJars(attributes.getProcessorPath())
       .addExports(JavaCommon.getExports(ruleContext))
       .addClasspathDeps(javaCommon.targetsTreatedAsDeps(ClasspathType.COMPILE_ONLY))
       .addRuntimeDeps(javaCommon.targetsTreatedAsDeps(ClasspathType.RUNTIME_ONLY))
       .build();
 }
Пример #6
0
  /**
   * Collects the transitive neverlink dependencies.
   *
   * @param ruleContext the context of the rule neverlink deps are to be computed for
   * @param deps the targets to be treated as dependencies
   * @param runtimeJars the runtime jars produced by the rule (non-transitive)
   * @return a nested set of the neverlink deps.
   */
  public static NestedSet<Artifact> collectTransitiveNeverlinkLibraries(
      RuleContext ruleContext,
      Iterable<? extends TransitiveInfoCollection> deps,
      ImmutableList<Artifact> runtimeJars) {
    NestedSetBuilder<Artifact> builder = NestedSetBuilder.naiveLinkOrder();

    for (AndroidNeverLinkLibrariesProvider provider :
        AnalysisUtils.getProviders(deps, AndroidNeverLinkLibrariesProvider.class)) {
      builder.addTransitive(provider.getTransitiveNeverLinkLibraries());
    }

    if (JavaCommon.isNeverLink(ruleContext)) {
      builder.addAll(runtimeJars);
      for (JavaCompilationArgsProvider provider :
          AnalysisUtils.getProviders(deps, JavaCompilationArgsProvider.class)) {
        builder.addTransitive(provider.getRecursiveJavaCompilationArgs().getRuntimeJars());
      }
    }

    return builder.build();
  }
Пример #7
0
 public CcLinkParamsStore getCcLinkParamsStore() {
   return getCcLinkParamsStore(javaCommon.targetsTreatedAsDeps(ClasspathType.BOTH));
 }
Пример #8
0
 public Artifact getInstrumentedJar() {
   return javaCommon.getJavaCompilationArtifacts().getInstrumentedJar();
 }
Пример #9
0
 public ImmutableList<Artifact> getRuntimeJars() {
   return javaCommon.getJavaCompilationArtifacts().getRuntimeJars();
 }
Пример #10
0
 public ImmutableList<String> getJavacOpts() {
   return javaCommon.getJavacOpts();
 }
Пример #11
0
  private void initJava(
      JavaCompilationHelper helper,
      JavaCompilationArtifacts.Builder javaArtifactsBuilder,
      boolean collectJavaCompilationArgs,
      NestedSetBuilder<Artifact> filesBuilder)
      throws InterruptedException {

    JavaTargetAttributes attributes = helper.getAttributes();
    if (ruleContext.hasErrors()) {
      // Avoid leaving filesToBuild set to null, otherwise we'll get a NullPointerException masking
      // the real error.
      filesToBuild = filesBuilder.build();
      return;
    }

    Artifact jar = null;
    if (attributes.hasSourceFiles() || attributes.hasSourceJars() || attributes.hasResources()) {
      // We only want to add a jar to the classpath of a dependent rule if it has content.
      javaArtifactsBuilder.addRuntimeJar(classJar);
      jar = classJar;
    }

    filesBuilder.add(classJar);

    manifestProtoOutput = helper.createManifestProtoOutput(classJar);

    // The gensrc jar is created only if the target uses annotation processing. Otherwise,
    // it is null, and the source jar action will not depend on the compile action.
    if (helper.usesAnnotationProcessing()) {
      genClassJar = helper.createGenJar(classJar);
      genSourceJar = helper.createGensrcJar(classJar);
      helper.createGenJarAction(classJar, manifestProtoOutput, genClassJar);
    }

    srcJar = ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_LIBRARY_SOURCE_JAR);
    helper.createSourceJarAction(srcJar, genSourceJar);

    outputDepsProto = helper.createOutputDepsProtoArtifact(classJar, javaArtifactsBuilder);
    helper.createCompileActionWithInstrumentation(
        classJar, manifestProtoOutput, genSourceJar, outputDepsProto, javaArtifactsBuilder);

    compileTimeDependencyArtifacts =
        javaCommon.collectCompileTimeDependencyArtifacts(outputDepsProto);
    filesToBuild = filesBuilder.build();

    if ((attributes.hasSourceFiles() || attributes.hasSourceJars()) && jar != null) {
      iJar = helper.createCompileTimeJarAction(jar, javaArtifactsBuilder);
    }
    javaCommon.setJavaCompilationArtifacts(javaArtifactsBuilder.build());

    javaCommon.setClassPathFragment(
        new ClasspathConfiguredFragment(
            javaCommon.getJavaCompilationArtifacts(),
            attributes,
            asNeverLink,
            helper.getBootclasspathOrDefault()));

    transitiveNeverlinkLibraries =
        collectTransitiveNeverlinkLibraries(
            ruleContext,
            javaCommon.getDependencies(),
            javaCommon.getJavaCompilationArtifacts().getRuntimeJars());
    topLevelSourceJars = ImmutableList.of(srcJar);
    transitiveSourceJars = javaCommon.collectTransitiveSourceJars(srcJar);

    if (collectJavaCompilationArgs) {
      boolean hasSources = attributes.hasSourceFiles() || attributes.hasSourceJars();
      this.javaCompilationArgs = collectJavaCompilationArgs(exportDeps, asNeverLink, hasSources);
      this.recursiveJavaCompilationArgs =
          collectJavaCompilationArgs(true, asNeverLink, /* hasSources */ true);
    }
  }
Пример #12
0
  public JavaTargetAttributes init(
      JavaSemantics javaSemantics,
      AndroidSemantics androidSemantics,
      ResourceApk resourceApk,
      boolean addCoverageSupport,
      boolean collectJavaCompilationArgs)
      throws InterruptedException {

    classJar = ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_LIBRARY_CLASS_JAR);
    idlHelper = new AndroidIdlHelper(ruleContext, classJar);

    JavaTargetAttributes.Builder attributes =
        javaCommon
            .initCommon(
                idlHelper.getIdlGeneratedJavaSources(), androidSemantics.getJavacArguments())
            .setBootClassPath(
                ImmutableList.of(AndroidSdkProvider.fromRuleContext(ruleContext).getAndroidJar()));

    JavaCompilationArtifacts.Builder artifactsBuilder = new JavaCompilationArtifacts.Builder();
    ImmutableList.Builder<Artifact> jarsProducedForRuntime = ImmutableList.builder();
    NestedSetBuilder<Artifact> filesBuilder = NestedSetBuilder.<Artifact>stableOrder();

    Artifact resourcesJar = resourceApk.getResourceJavaSrcJar();
    if (resourcesJar != null) {
      filesBuilder.add(resourcesJar);
      compileResources(
          javaSemantics,
          resourcesJar,
          artifactsBuilder,
          attributes,
          filesBuilder,
          jarsProducedForRuntime);
      if (resourceApk.isLegacy()) {
        // Repackages the R.java for each dependency package and places the resultant jars before
        // the dependency libraries to ensure that the generated resource ids are correct.
        createJarJarActions(
            attributes,
            jarsProducedForRuntime,
            resourceApk.getResourceDependencies().getResources(),
            resourceApk.getPrimaryResource().getJavaPackage(),
            resourceClassJar);
      }
    }

    JavaCompilationHelper helper = initAttributes(attributes, javaSemantics);
    if (ruleContext.hasErrors()) {
      return null;
    }

    if (addCoverageSupport) {
      androidSemantics.addCoverageSupport(
          ruleContext, this, javaSemantics, true, attributes, artifactsBuilder);
      if (ruleContext.hasErrors()) {
        return null;
      }
    }

    jackCompilationHelper = initJack(helper.getAttributes());
    if (ruleContext.hasErrors()) {
      return null;
    }

    initJava(helper, artifactsBuilder, collectJavaCompilationArgs, filesBuilder);
    if (ruleContext.hasErrors()) {
      return null;
    }
    this.jarsProducedForRuntime = jarsProducedForRuntime.add(classJar).build();
    return helper.getAttributes();
  }
Пример #13
0
 /**
  * Creates a new AndroidCommon.
  *
  * @param common the JavaCommon instance
  * @param asNeverLink Boolean to indicate if this rule should be treated as a compile time dep by
  *     consuming rules.
  * @param exportDeps Boolean to indicate if the dependencies should be treated as "exported" deps.
  */
 public AndroidCommon(JavaCommon common, boolean asNeverLink, boolean exportDeps) {
   this.ruleContext = common.getRuleContext();
   this.asNeverLink = asNeverLink;
   this.exportDeps = exportDeps;
   this.javaCommon = common;
 }
Пример #14
0
 public AndroidCommon(JavaCommon javaCommon) {
   this(javaCommon, JavaCommon.isNeverLink(javaCommon.getRuleContext()), false);
 }