aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/android/java/com/google/devtools/build/android/dexer
diff options
context:
space:
mode:
authorGravatar kmb <kmb@google.com>2017-09-28 15:14:24 -0400
committerGravatar John Cater <jcater@google.com>2017-09-29 12:14:10 -0400
commit98fbf51a30adc225a1900ca13db835244aeb0588 (patch)
tree8eb6c8da426c3903d4e739485662a88da1d4a011 /src/tools/android/java/com/google/devtools/build/android/dexer
parent64cb229b324444ed24c067dce42cdeea787e4728 (diff)
Support --positions flag in DexBuilder, for consistency with dx
RELNOTES: none PiperOrigin-RevId: 170379445
Diffstat (limited to 'src/tools/android/java/com/google/devtools/build/android/dexer')
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/dexer/Dexing.java54
1 files changed, 50 insertions, 4 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/dexer/Dexing.java b/src/tools/android/java/com/google/devtools/build/android/dexer/Dexing.java
index ca5dba24ae..d5583e79b1 100644
--- a/src/tools/android/java/com/google/devtools/build/android/dexer/Dexing.java
+++ b/src/tools/android/java/com/google/devtools/build/android/dexer/Dexing.java
@@ -26,11 +26,14 @@ import com.android.dx.util.ByteArray;
import com.google.auto.value.AutoValue;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.io.ByteStreams;
+import com.google.devtools.common.options.Converter;
import com.google.devtools.common.options.Option;
import com.google.devtools.common.options.OptionDocumentationCategory;
import com.google.devtools.common.options.OptionEffectTag;
import com.google.devtools.common.options.OptionsBase;
+import com.google.devtools.common.options.OptionsParsingException;
import java.io.PrintStream;
+import java.lang.reflect.Field;
/**
* Common helper class that encodes Java classes into {@link DexFile}s.
@@ -40,6 +43,30 @@ class Dexing {
static final PrintStream nullout = new PrintStream(ByteStreams.nullOutputStream());
/**
+ * Parser for positions options based on the integer field names in {@link PositionList}.
+ */
+ public static class PositionGranularityConverter implements Converter<Integer> {
+ @Override
+ public Integer convert(String input) throws OptionsParsingException {
+ for (Field field : PositionList.class.getFields()) {
+ if (field.getName().equalsIgnoreCase(input)) {
+ try {
+ return field.getInt(null);
+ } catch (RuntimeException | IllegalAccessException e) {
+ throw new OptionsParsingException("Can't parse positions option", input, e);
+ }
+ }
+ }
+ throw new OptionsParsingException("Unknown positions option", input);
+ }
+
+ @Override
+ public String getTypeDescription() {
+ return "One of the options from dx's --positions flag";
+ }
+ }
+
+ /**
* Common command line options for use with {@link Dexing}.
*/
public static class DexingOptions extends OptionsBase {
@@ -67,6 +94,20 @@ class Dexing {
public boolean optimize;
@Option(
+ name = "positions",
+ defaultValue = "lines", // dx's default
+ category = "semantics",
+ documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
+ effectTags = {OptionEffectTag.UNKNOWN},
+ allowMultiple = false,
+ converter = PositionGranularityConverter.class,
+ help = "How densely to emit line number information."
+ )
+ // Note this field must be initialized (usually done by an options parser) since the implicit
+ // initial value 0 is not valid.
+ public int positionInfo;
+
+ @Option(
name = "warning",
defaultValue = "true", // dx's default
category = "misc",
@@ -85,7 +126,7 @@ class Dexing {
// Use dx's defaults
result.optimizeListFile = null;
result.dontOptimizeListFile = null;
- result.positionInfo = PositionList.LINES;
+ result.positionInfo = positionInfo;
result.strictNameCheck = true;
result.statistics = false; // we're not supporting statistics anyways
return result;
@@ -103,9 +144,10 @@ class Dexing {
*/
@AutoValue
abstract static class DexingKey {
- static DexingKey create(boolean localInfo, boolean optimize, byte[] classfileContent) {
+ static DexingKey create(
+ boolean localInfo, boolean optimize, int positionInfo, byte[] classfileContent) {
// TODO(bazel-team): Maybe we can use a minimal collision hash instead of full content
- return new AutoValue_Dexing_DexingKey(localInfo, optimize, classfileContent);
+ return new AutoValue_Dexing_DexingKey(localInfo, optimize, positionInfo, classfileContent);
}
/** Returns whether {@link CfOptions#localInfo local variable information} is included. */
@@ -114,6 +156,9 @@ class Dexing {
/** Returns whether {@link CfOptions#optimize SSA/register optimization} is performed. */
abstract boolean optimize();
+ /** Returns how much line number information is emitted as a {@link PositionList} constant. */
+ abstract int positionInfo();
+
/** Returns the class file to dex, <b>not</b> the dexed class. Don't modify the return value! */
@SuppressWarnings("mutable") abstract byte[] classfileContent();
}
@@ -162,6 +207,7 @@ class Dexing {
}
public DexingKey getDexingKey(byte[] classfile) {
- return DexingKey.create(cfOptions.localInfo, cfOptions.optimize, classfile);
+ return DexingKey.create(
+ cfOptions.localInfo, cfOptions.optimize, cfOptions.positionInfo, classfile);
}
}