Search in sources :

Example 6 with Builder

use of com.google.devtools.build.lib.analysis.actions.SpawnAction.Builder in project bazel by bazelbuild.

the class AndroidBinary method singleJarSpawnActionBuilder.

// Adds the appropriate SpawnAction options depending on if SingleJar is a jar or not.
private static SpawnAction.Builder singleJarSpawnActionBuilder(RuleContext ruleContext) {
    Artifact singleJar = JavaToolchainProvider.fromRuleContext(ruleContext).getSingleJar();
    SpawnAction.Builder builder = new SpawnAction.Builder();
    if (singleJar.getFilename().endsWith(".jar")) {
        builder.setJarExecutable(ruleContext.getHostConfiguration().getFragment(Jvm.class).getJavaExecutable(), singleJar, JavaToolchainProvider.fromRuleContext(ruleContext).getJvmOptions()).addTransitiveInputs(JavaHelper.getHostJavabaseInputs(ruleContext));
    } else {
        builder.setExecutable(singleJar);
    }
    return builder;
}
Also used : Jvm(com.google.devtools.build.lib.rules.java.Jvm) SpawnAction(com.google.devtools.build.lib.analysis.actions.SpawnAction) Builder(com.google.devtools.build.lib.analysis.actions.SpawnAction.Builder) MultimapBuilder(com.google.common.collect.MultimapBuilder) Builder(com.google.devtools.build.lib.analysis.actions.SpawnAction.Builder) NestedSetBuilder(com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) DeployArchiveBuilder(com.google.devtools.build.lib.rules.java.DeployArchiveBuilder) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 7 with Builder

use of com.google.devtools.build.lib.analysis.actions.SpawnAction.Builder in project bazel by bazelbuild.

the class ProguardHelper method createProguardAction.

/**
   * Creates an action to run Proguard over the given {@code programJar} with various other given
   * inputs to produce {@code proguardOutputJar}.  If requested explicitly, or implicitly with
   * --java_optimization_mode, the action also produces a mapping file (which shows what methods and
   * classes in the output Jar correspond to which methods and classes in the input).  The "pair"
   * returned by this method indicates whether a mapping is being produced.
   *
   * <p>See the Proguard manual for the meaning of the various artifacts in play.
   *
   * @param proguard Proguard executable to use
   * @param proguardSpecs Proguard specification files to pass to Proguard
   * @param proguardMapping optional mapping file for Proguard to apply
   * @param libraryJars any other Jar files that the {@code programJar} will run against
   * @param mappingRequested whether to ask Proguard to output a mapping file (a mapping will be
   *        produced anyway if --java_optimization_mode includes obfuscation)
   * @param optimizationPasses if not null specifies to break proguard up into multiple passes with
   *        the given number of optimization passes.
   * @param proguardOutputMap mapping generated by Proguard if requested. could be null.
   * @param useSingleJarForProguardLibraryJars whether to combine all library jars into a single jar
   *        file before processing with Proguard
   */
public static ProguardOutput createProguardAction(RuleContext ruleContext, FilesToRunProvider proguard, Artifact programJar, ImmutableList<Artifact> proguardSpecs, @Nullable Artifact proguardSeeds, @Nullable Artifact proguardUsage, @Nullable Artifact proguardMapping, Iterable<Artifact> libraryJars, Artifact proguardOutputJar, JavaSemantics semantics, @Nullable Integer optimizationPasses, @Nullable Artifact proguardOutputMap, boolean useSingleJarForProguardLibraryJars) throws InterruptedException {
    JavaOptimizationMode optMode = getJavaOptimizationMode(ruleContext);
    Preconditions.checkArgument(optMode != JavaOptimizationMode.NOOP);
    Preconditions.checkArgument(optMode != JavaOptimizationMode.LEGACY || !proguardSpecs.isEmpty());
    ProguardOutput output = getProguardOutputs(proguardOutputJar, proguardSeeds, proguardUsage, ruleContext, semantics, proguardOutputMap);
    if (useSingleJarForProguardLibraryJars && Iterables.size(libraryJars) > 1) {
        JavaTargetAttributes attributes = new JavaTargetAttributes.Builder(semantics).build();
        Artifact combinedLibraryJar = getProguardTempArtifact(ruleContext, optMode.name().toLowerCase(), "combined_library_jars.jar");
        new DeployArchiveBuilder(semantics, ruleContext).setOutputJar(combinedLibraryJar).setAttributes(attributes).addRuntimeJars(libraryJars).build();
        libraryJars = ImmutableList.of(combinedLibraryJar);
    }
    if (optimizationPasses == null) {
        // Run proguard as a single step.
        Builder builder = makeBuilder(proguard, programJar, proguardSpecs, proguardMapping, libraryJars, output.getOutputJar(), output.getMapping(), output.getProtoMapping(), output.getSeeds(), output.getUsage(), output.getConstantStringObfuscatedMapping(), output.getConfig()).setProgressMessage("Trimming binary with Proguard").addOutput(proguardOutputJar);
        ruleContext.registerAction(builder.build(ruleContext));
    } else {
        // Optimization passes have been specified, so run proguard in multiple phases.
        Artifact lastStageOutput = getProguardTempArtifact(ruleContext, optMode.name().toLowerCase(), "proguard_preoptimization.jar");
        ruleContext.registerAction(makeBuilder(proguard, programJar, proguardSpecs, proguardMapping, libraryJars, output.getOutputJar(), /* proguardOutputMap */
        null, /* proguardOutputProtoMap */
        null, // ProGuard only prints seeds during INITIAL and NORMAL runtypes.
        output.getSeeds(), /* proguardUsage */
        null, /* constantStringObfuscatedMapping */
        null, /* proguardConfigOutput */
        null).setProgressMessage("Trimming binary with Proguard: Verification/Shrinking Pass").addArgument("-runtype INITIAL").addArgument("-nextstageoutput").addOutputArgument(lastStageOutput).build(ruleContext));
        for (int i = 0; i < optimizationPasses; i++) {
            Artifact optimizationOutput = getProguardTempArtifact(ruleContext, optMode.name().toLowerCase(), "proguard_optimization_" + (i + 1) + ".jar");
            ruleContext.registerAction(makeBuilder(proguard, programJar, proguardSpecs, proguardMapping, libraryJars, output.getOutputJar(), /* proguardOutputMap */
            null, /* proguardOutputProtoMap */
            null, /* proguardSeeds */
            null, /* proguardUsage */
            null, /* constantStringObfuscatedMapping */
            null, /* proguardConfigOutput */
            null).setProgressMessage("Trimming binary with Proguard: Optimization Pass " + (i + 1)).addArgument("-runtype OPTIMIZATION").addArgument("-laststageoutput").addInputArgument(lastStageOutput).addArgument("-nextstageoutput").addOutputArgument(optimizationOutput).build(ruleContext));
            lastStageOutput = optimizationOutput;
        }
        Builder builder = makeBuilder(proguard, programJar, proguardSpecs, proguardMapping, libraryJars, output.getOutputJar(), output.getMapping(), output.getProtoMapping(), // runtype FINAL does not produce seeds.
        null, /* proguardSeeds */
        output.getUsage(), output.getConstantStringObfuscatedMapping(), output.getConfig()).setProgressMessage("Trimming binary with Proguard: Obfuscation and Final Output Pass").addArgument("-runtype FINAL").addArgument("-laststageoutput").addInputArgument(lastStageOutput).addOutput(proguardOutputJar);
        ruleContext.registerAction(builder.build(ruleContext));
    }
    return output;
}
Also used : JavaOptimizationMode(com.google.devtools.build.lib.rules.java.JavaConfiguration.JavaOptimizationMode) NestedSetBuilder(com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder) Builder(com.google.devtools.build.lib.analysis.actions.SpawnAction.Builder) Artifact(com.google.devtools.build.lib.actions.Artifact)

Aggregations

Builder (com.google.devtools.build.lib.analysis.actions.SpawnAction.Builder)7 Artifact (com.google.devtools.build.lib.actions.Artifact)6 SpawnAction (com.google.devtools.build.lib.analysis.actions.SpawnAction)5 NestedSetBuilder (com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder)5 MultimapBuilder (com.google.common.collect.MultimapBuilder)4 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)4 DeployArchiveBuilder (com.google.devtools.build.lib.rules.java.DeployArchiveBuilder)4 FilesToRunProvider (com.google.devtools.build.lib.analysis.FilesToRunProvider)2 ArrayList (java.util.ArrayList)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 RunfilesProvider (com.google.devtools.build.lib.analysis.RunfilesProvider)1 ApkSigningMethod (com.google.devtools.build.lib.rules.android.AndroidConfiguration.ApkSigningMethod)1 JavaOptimizationMode (com.google.devtools.build.lib.rules.java.JavaConfiguration.JavaOptimizationMode)1 Jvm (com.google.devtools.build.lib.rules.java.Jvm)1 ProguardOutput (com.google.devtools.build.lib.rules.java.ProguardHelper.ProguardOutput)1 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1