diff options
author | 2015-11-20 22:01:46 +0000 | |
---|---|---|
committer | 2015-11-24 14:40:15 +0000 | |
commit | b0d1313e1ba86a0a7a061567879186cbf23b3744 (patch) | |
tree | 7437989ba1b8c9d169fa180cece9849006f58a1a /src/main/java/com/google/devtools/build/lib/rules | |
parent | 420804811ddb7798a02c5eb9fd2be1c253799f9d (diff) |
add optimization modes that require implicit proguard spec
--
MOS_MIGRATED_REVID=108376750
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java | 19 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java | 59 |
2 files changed, 71 insertions, 7 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java index 14d98ca2ad..301b509478 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java @@ -39,6 +39,7 @@ import com.google.devtools.build.lib.analysis.RunfilesProvider; import com.google.devtools.build.lib.analysis.TransitiveInfoCollection; import com.google.devtools.build.lib.analysis.actions.CommandLine; import com.google.devtools.build.lib.analysis.actions.CustomCommandLine; +import com.google.devtools.build.lib.analysis.actions.FileWriteAction; import com.google.devtools.build.lib.analysis.actions.SpawnAction; import com.google.devtools.build.lib.analysis.actions.SpawnAction.Builder; import com.google.devtools.build.lib.analysis.config.BuildConfiguration; @@ -749,8 +750,24 @@ public abstract class AndroidBinary implements RuleConfiguredTargetFactory { ruleContext.getPrerequisites("deps", Mode.TARGET, ProguardSpecProvider.class)) { builder.addAll(dep.getTransitiveProguardSpecs()); } + + // Include proguard spec generated from rule's resources Artifact output = resourceApk.getResourceProguardConfig(); builder.add(output); + + // Generate and include implicit Proguard spec for requested mode. + if (!optMode.getImplicitProguardDirectives().isEmpty()) { + Artifact implicitDirectives = + getProguardConfigArtifact(ruleContext, optMode.name().toLowerCase()); + ruleContext.registerAction( + new FileWriteAction( + ruleContext.getActionOwner(), + implicitDirectives, + optMode.getImplicitProguardDirectives(), + /*executable*/ false)); + builder.add(implicitDirectives); + } + return builder.build().asList(); } @@ -1381,7 +1398,7 @@ public abstract class AndroidBinary implements RuleConfiguredTargetFactory { } /** - * Returns an intermediate artifact used to support dex generation. + * Returns an intermediate artifact used to run Proguard. */ public static Artifact getProguardConfigArtifact(RuleContext ruleContext, String prefix) { // TODO(bazel-team): Remove the redundant inclusion of the rule name, as getUniqueDirectory diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java index ecc71af308..4da2550541 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java @@ -13,6 +13,8 @@ // limitations under the License. package com.google.devtools.build.lib.rules.java; +import static com.google.common.base.Preconditions.checkArgument; + import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap.Builder; import com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment; @@ -55,10 +57,49 @@ public final class JavaConfiguration extends Fragment { public static enum JavaOptimizationMode { /** Proguard is used iff top-level target has {@code proguard_specs} attribute. */ LEGACY, - /** No link-time optimizations are applied, regardless of the top-level target's attributes. */ - NOOP, - /** Produce fully optimized binary with short symbol names and unreachable code removed. */ - OPTIMIZE_MINIMIZE; + /** + * No link-time optimizations are applied, regardless of the top-level target's attributes. In + * practice this mode skips Proguard completely, rather than invoking Proguard as a no-op. + */ + NOOP("-dontshrink", "-dontoptimize", "-dontobfuscate"), + /** + * Symbols have different names except where configured not to rename. This mode is primarily + * intended to aid in identifying missing configuration directives that prevent symbols accessed + * reflectively etc. from being renamed or removed. + */ + RENAME("-dontshrink", "-dontoptimize"), + /** + * "Quickly" produce small binary typically without changing code structure. In practice this + * mode removes unreachable code and uses short symbol names except where configured not to + * rename or remove. This mode should build faster than {@link #OPTIMIZE_MINIFY} and may hence + * be preferable during development. + */ + FAST_MINIFY("-dontoptimize"), + /** + * Produce fully optimized binary with short symbol names and unreachable code removed. Unlike + * {@link #FAST_MINIFY}, this mode may apply code transformations, in addition to removing and + * renaming code as the configuration allows, to produce a more compact binary. This mode + * should be preferable for producing and testing release binaries. + */ + OPTIMIZE_MINIFY; + + private String proguardDirectives; + + private JavaOptimizationMode(String... donts) { + StringBuilder proguardDirectives = new StringBuilder(); + for (String dont : donts) { + checkArgument(dont.startsWith("-dont"), "invalid Proguard directive: %s", dont); + proguardDirectives.append(dont).append('\n'); + } + this.proguardDirectives = proguardDirectives.toString(); + } + + /** + * Returns additional Proguard directives necessary for this mode (can be empty). + */ + public String getImplicitProguardDirectives() { + return proguardDirectives; + } /** * Returns true if all affected targets should produce mappings from original to renamed symbol @@ -71,7 +112,9 @@ public final class JavaConfiguration extends Fragment { case LEGACY: case NOOP: return false; - case OPTIMIZE_MINIMIZE: + case RENAME: + case FAST_MINIFY: + case OPTIMIZE_MINIFY: return true; default: throw new AssertionError("Unexpected mode: " + this); @@ -283,7 +326,11 @@ public final class JavaConfiguration extends Fragment { return allowPrecompiledJarsInSrcs; } - /** Returns the --java_optimization_mode flag setting. */ + /** + * Returns the --java_optimization_mode flag setting. Note that running with a different mode over + * the same binary or test target typically invalidates the cached output Jar for that target, + * but since Proguard doesn't run on libraries, the outputs for library targets remain valid. + */ public JavaOptimizationMode getJavaOptimizationMode() { return javaOptimizationMode; } |