diff options
author | mikaelpeltier <mikaelpeltier@google.com> | 2014-07-11 16:02:57 +0200 |
---|---|---|
committer | mikaelpeltier <mikaelpeltier@google.com> | 2014-07-18 11:08:26 +0200 |
commit | f9141d5433dcb74091d7f3121cc188fd754bb168 (patch) | |
tree | 2df025f451722125bcf21c6760be3a9a4944a686 | |
parent | 323c168999becffac28632b15bb97a093a06bef2 (diff) | |
download | toolchain_jack-f9141d5433dcb74091d7f3121cc188fd754bb168.zip toolchain_jack-f9141d5433dcb74091d7f3121cc188fd754bb168.tar.gz toolchain_jack-f9141d5433dcb74091d7f3121cc188fd754bb168.tar.bz2 |
Add command line for dex comparator
- DexComparator automatically call DexAnnotationsComparator when using
command line.
- Update build.xml and android.mk to build an executable jar of dex
comparator.
Change-Id: Ib69c697fd7c25e22d3569e92b9c581b5c57242b3
-rw-r--r-- | build.xml | 33 | ||||
-rw-r--r-- | dexcomparator/.classpath | 1 | ||||
-rw-r--r-- | dexcomparator/Android.mk | 20 | ||||
-rw-r--r-- | dexcomparator/etc/manifest.txt | 1 | ||||
-rw-r--r-- | dexcomparator/src/com/android/jack/DexComparator.java | 83 | ||||
-rw-r--r-- | dexcomparator/src/com/android/jack/DexComparatorOptions.java | 64 | ||||
-rw-r--r-- | dexcomparator/testing/com/android/jack/dexcomparator/test/BinaryCodeComparisonTest.java | 12 | ||||
-rw-r--r-- | jack/tests/com/android/jack/TestTools.java | 4 | ||||
-rw-r--r-- | jack/tests/com/android/jack/compile/androidtree/core/CoreCompilationTest.java | 6 | ||||
-rw-r--r-- | jack/tests/com/android/jack/tools/merger/MergerTestTools.java | 8 |
10 files changed, 181 insertions, 51 deletions
@@ -41,7 +41,7 @@ junit4-lib-clean, junit4-clean, hamcrest-core-lib-clean, - dexcomparator-lib-clean, + dexcomparator-clean, dex-lib-clean, dx-ref-lib-clean" /> @@ -889,22 +889,22 @@ </target> - <!-- ******************* --> - <!-- dexcomparator-lib --> - <!-- ******************* --> + <!-- *************** --> + <!-- dexcomparator --> + <!-- *************** --> + <property name="dexcomparator.execname" value="dexcomparator.jar" /> <property name="dexcomparator.build.dir" value="${dexcomparator.dir}/build" /> <property name="dexcomparator.libs.dir" value="${dexcomparator.dir}/libs" /> <property name="dexcomparator.build.outdir" value="${dexcomparator.build.dir}/classes" /> <property name="dexcomparator.dist.dir" value="${dexcomparator.dir}/dist" /> - <property name="dexcomparator.libname" value="dexcomparator-lib.jar" /> - <target name="dexcomparator-lib-clean"> + <target name="dexcomparator-clean"> <delete dir="${dexcomparator.build.dir}" /> <delete dir="${dexcomparator.libs.dir}" /> <delete dir="${dexcomparator.dist.dir}" /> </target> - <target name="dexcomparator-lib" depends="dx-lib,dex-lib,jsr305-lib,guava-lib"> + <target name="dexcomparator-compile" depends="dx-lib,dex-lib,jsr305-lib,guava-lib,args4j-lib"> <!-- project layout --> <mkdir dir="${dexcomparator.build.outdir}"/> <mkdir dir="${dexcomparator.libs.dir}"/> @@ -915,7 +915,8 @@ <file name="${dx.dist.dir}/${dx.libname}" /> <file name="${dexlib.dist.dir}/${dexlib.libname}" /> <file name="${jsr305.dist.dir}/${jsr305.libname}" /> - <file name="${guava.dist.dir}/${guava.libname}" /> + <file name="${guava.dist.dir}/${guava.libname}" /> + <file name="${args4j.dist.dir}/${args4j.libname}" /> </filelist> </copy> <!-- compile --> @@ -929,6 +930,22 @@ </fileset> </classpath> </javac> + </target> + + <target name="dexcomparator" depends="dexcomparator-compile"> + <jar destfile="${dexcomparator.dist.dir}/${dexcomparator.execname}" + manifest="${dexcomparator.dir}/etc/manifest.txt" + basedir="${dexcomparator.build.outdir}" includes="**"> + <zipgroupfileset dir="${dexcomparator.dir}/libs" includes="**/*.jar" /> + </jar> + </target> + + <!-- ******************* --> + <!-- dexcomparator-lib --> + <!-- ******************* --> + <property name="dexcomparator.libname" value="dexcomparator-lib.jar" /> + + <target name="dexcomparator-lib" depends="dexcomparator-compile"> <!-- package --> <jar destfile="${dexcomparator.dist.dir}/${dexcomparator.libname}" basedir="${dexcomparator.build.outdir}" diff --git a/dexcomparator/.classpath b/dexcomparator/.classpath index 0e81db1..c4ec233 100644 --- a/dexcomparator/.classpath +++ b/dexcomparator/.classpath @@ -12,6 +12,7 @@ <classpathentry kind="lib" path="libs/dex-lib.jar"/> <classpathentry kind="lib" path="libs/jsr305-lib.jar"/> <classpathentry kind="lib" path="libs/guava-lib.jar"/> + <classpathentry kind="lib" path="libs/args4j-lib.jar"/> <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> <classpathentry kind="output" path="bin"/> </classpath> diff --git a/dexcomparator/Android.mk b/dexcomparator/Android.mk index 651520f..1b6d234 100644 --- a/dexcomparator/Android.mk +++ b/dexcomparator/Android.mk @@ -9,13 +9,29 @@ include $(CLEAR_VARS) LOCAL_SRC_FILES := $(call all-java-files-under, src) LOCAL_MODULE := dexcomparator - LOCAL_MODULE_TAGS := optional LOCAL_JAVA_LIBRARIES := \ dx-jack \ dexlib-jack \ - jsr305lib-jack + jsr305lib-jack \ + args4j-jack include $(BUILD_HOST_JAVA_LIBRARY) + +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +LOCAL_MODULE := dexComparatorTool +LOCAL_JAR_MANIFEST := etc/manifest.txt +LOCAL_MODULE_TAGS := optional + +LOCAL_STATIC_JAVA_LIBRARIES := \ + dx-jack \ + dexlib-jack \ + jsr305lib-jack \ + args4j-jack + +include $(BUILD_HOST_JAVA_LIBRARY) diff --git a/dexcomparator/etc/manifest.txt b/dexcomparator/etc/manifest.txt new file mode 100644 index 0000000..902deab --- /dev/null +++ b/dexcomparator/etc/manifest.txt @@ -0,0 +1 @@ +Main-Class: com.android.jack.DexComparator diff --git a/dexcomparator/src/com/android/jack/DexComparator.java b/dexcomparator/src/com/android/jack/DexComparator.java index 5b0aeb7..f685049 100644 --- a/dexcomparator/src/com/android/jack/DexComparator.java +++ b/dexcomparator/src/com/android/jack/DexComparator.java @@ -31,6 +31,9 @@ import com.android.jack.dx.rop.code.AccessFlags; import com.android.jack.dx.rop.type.Prototype; import com.android.jack.dx.util.ByteInput; +import org.kohsuke.args4j.CmdLineException; +import org.kohsuke.args4j.CmdLineParser; + import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -48,14 +51,18 @@ import javax.annotation.Nonnull; */ public class DexComparator { + public static final int NO_DIFFERENCE = 0; + public static final int DIFFERENCE = 1; + public static final int PROBLEM = 2; + private final Logger logger; private DexBuffer referenceDexFile; private DexBuffer candidateDexFile; private static final Level ERROR_LEVEL = Level.SEVERE; private static final Level WARNING_LEVEL = Level.WARNING; private static final Level DEBUG_LEVEL = Level.FINE; - private boolean strict; - private boolean enableDebugInfoComparison; + private boolean strict = false; + private boolean enableDebugInfoComparison = false; private byte[] referenceData; private byte[] candidateData; private int refThisIndex; @@ -77,16 +84,9 @@ public class DexComparator { @Nonnull private static final String STATIC_INIT_NAME = "<clinit>"; - public DexComparator() { - logger = Logger.getLogger(this.getClass().getName()); - logger.setLevel(WARNING_LEVEL); - } - /** * Launch the comparison between a reference Dex {@code File} and a candidate Dex {@code File}. * - * @param referenceFile the reference Dex {@code File} - * @param candidateFile the candidate Dex {@code File} * @param compareDebugInfo also compare debug infos * @param strict if false, the candidate Dex must <i>at least<i/> contain all the structures of * the reference Dex; if true, the candidate Dex must <i>exactly<i/> contain all the @@ -95,31 +95,26 @@ public class DexComparator { * compareDebugInfo is enabled * @param compareInstructionNumber enable comparison of number of instructions * @param instructionNumberTolerance tolerance factor for comparison of number of instructions - * @throws DifferenceFoundException if a difference between the two Dex files is found - * @throws IOException if an error occurs while loading the dex files */ - public void compare(@Nonnull File referenceFile, - @Nonnull File candidateFile, + public DexComparator( boolean compareDebugInfo, boolean strict, boolean compareDebugInfoBinarily, boolean compareInstructionNumber, - float instructionNumberTolerance) throws DifferenceFoundException, IOException { + float instructionNumberTolerance) { + logger = Logger.getLogger(this.getClass().getName()); + logger.setLevel(WARNING_LEVEL); this.strict = strict; enableBinaryDebugInfoComparison = compareDebugInfoBinarily; enableInstructionNumberComparison = compareInstructionNumber; this.instructionNumberTolerance = instructionNumberTolerance; enableDebugInfoComparison = compareDebugInfo; - - compare(referenceFile, candidateFile); } /** * Launch the comparison between a reference Dex {@code File} and a candidate Dex {@code File}. * - * @param referenceFile the reference Dex {@code File} - * @param candidateFile the candidate Dex {@code File} * @param compareDebugInfo also compare debug infos * @param strict if false, the candidate Dex must <i>at least<i/> contain all the structures of * the reference Dex; if true, the candidate Dex must <i>exactly<i/> contain all the @@ -127,25 +122,22 @@ public class DexComparator { * @param compareDebugInfoBinarily enable binary comparison of debug infos, allowed only if * compareDebugInfo is enabled * @param compareCodeBinarily enable code binary comparison - * @throws DifferenceFoundException if a difference between the two Dex files is found - * @throws IOException if an error occurs while loading the dex files */ - public void compare(@Nonnull File referenceFile, - @Nonnull File candidateFile, + public DexComparator( boolean compareDebugInfo, boolean strict, boolean compareDebugInfoBinarily, - boolean compareCodeBinarily) throws DifferenceFoundException, IOException { + boolean compareCodeBinarily) { + logger = Logger.getLogger(this.getClass().getName()); + logger.setLevel(WARNING_LEVEL); this.strict = strict; enableBinaryDebugInfoComparison = compareDebugInfoBinarily; enableBinaryCodeComparison = compareCodeBinarily; enableDebugInfoComparison = compareDebugInfo; - - compare(referenceFile, candidateFile); } - private void compare(@Nonnull File referenceFile, @Nonnull File candidateFile) throws IOException, + public void compare(@Nonnull File referenceFile, @Nonnull File candidateFile) throws IOException, DifferenceFoundException { if (enableBinaryDebugInfoComparison && !enableDebugInfoComparison) { @@ -1021,4 +1013,43 @@ public class DexComparator { return position; } } + + public static void main(@Nonnull String[] args) { + DexComparatorOptions options = new DexComparatorOptions(); + CmdLineParser parser = new CmdLineParser(options); + parser.setUsageWidth(100); + + try { + parser.parseArgument(args); + DexComparator dc = null; + + if (options.compareInstructionNumber) { + if (options.enableBinaryCodeComparison) { + throw new CmdLineException(parser, + "Instruction number comparison is not allowed with binary code comparison"); + } + dc = + new DexComparator(options.enableDebugInfoComparison, options.strict, + options.enableBinaryDebugInfoComparison, options.compareInstructionNumber, + options.instructionNumberTolerance); + } else { + dc = new DexComparator(options.enableDebugInfoComparison, options.strict, + options.enableBinaryDebugInfoComparison, options.enableBinaryCodeComparison); + } + + dc.compare(options.referenceFile, options.candidateFile); + new DexAnnotationsComparator().compare(options.referenceFile, options.candidateFile); + System.exit(NO_DIFFERENCE); + } catch (CmdLineException e) { + System.err.println(e.getMessage()); + parser.printUsage(System.err); + System.exit(PROBLEM); + } catch (IOException e) { + System.err.println(e.getMessage()); + System.exit(PROBLEM); + } catch (DifferenceFoundException e) { + System.err.println(e.getMessage()); + System.exit(DIFFERENCE); + } + } } diff --git a/dexcomparator/src/com/android/jack/DexComparatorOptions.java b/dexcomparator/src/com/android/jack/DexComparatorOptions.java new file mode 100644 index 0000000..30fc38a --- /dev/null +++ b/dexcomparator/src/com/android/jack/DexComparatorOptions.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack; + +import org.kohsuke.args4j.Argument; +import org.kohsuke.args4j.Option; + +import java.io.File; + +/** + * Options used to configure a dex files comparator. + */ +class DexComparatorOptions { + @Argument(usage = "candidate dex file", required = true) + File candidateFile; + + @Option(name = "--referenceDexFile", aliases = "-ref", usage = "reference dex file", + required = true) + File referenceFile; + + @Option( + name = "--strict", + usage = "if false, the candidate Dex must at least contain all the structures of the " + + "reference Dex; if true, the candidate Dex must exactly contain all the " + + "structures of the reference Dex (default: false)") + boolean strict = false; + + @Option(name = "--compareDebugInfo", + usage = "enable comparison of debug infos (default: false)") + boolean enableDebugInfoComparison = false; + + @Option(name = "--compareDebugInfoBinarily", + usage = "enable binary comparison of debug infos, allowed only if " + + "compareDebugInfo is enabled (default: false)") + boolean enableBinaryDebugInfoComparison = false; + + @Option(name = "--compareCodeBinarily", + usage = "enable binary comparison of code (default: false)") + boolean enableBinaryCodeComparison = false; + + @Option(name = "--compareInstructionNumber", + usage = "enable comparison of number of instructions, not allowed " + + "with binary code comparison (default: false)") + boolean compareInstructionNumber = false; + + @Option(name = "--instructionNumberTolerance", + usage = "tolerance factor for comparison of number of instructions, allowed only if" + + " compareInstructionNumber is enabled (default: 0f)") + float instructionNumberTolerance = 0f; +}
\ No newline at end of file diff --git a/dexcomparator/testing/com/android/jack/dexcomparator/test/BinaryCodeComparisonTest.java b/dexcomparator/testing/com/android/jack/dexcomparator/test/BinaryCodeComparisonTest.java index 3441168..62a39be 100644 --- a/dexcomparator/testing/com/android/jack/dexcomparator/test/BinaryCodeComparisonTest.java +++ b/dexcomparator/testing/com/android/jack/dexcomparator/test/BinaryCodeComparisonTest.java @@ -47,14 +47,14 @@ public class BinaryCodeComparisonTest { dex2.deleteOnExit(); TestTools.compileToDexWithJack(a2, dex2); try { - new DexComparator().compare(dex1, dex2, false /* compareDebugInfo */, true /* strict */, - false /* compareDebugInfoBinarily */, true /* compareCodeBinarily */); + new DexComparator(false /* compareDebugInfo */, true /* strict */, + false /* compareDebugInfoBinarily */, true /* compareCodeBinarily */).compare(dex1, dex2); Assert.fail(); } catch (DifferenceFoundException e) { } try { - new DexComparator().compare(dex2, dex1, false /* compareDebugInfo */, true /* strict */, - false /* compareDebugInfoBinarily */, true /* compareCodeBinarily */); + new DexComparator(false /* compareDebugInfo */, true /* strict */, + false /* compareDebugInfoBinarily */, true /* compareCodeBinarily */).compare(dex2, dex1); Assert.fail(); } catch (DifferenceFoundException e) { } @@ -68,8 +68,8 @@ public class BinaryCodeComparisonTest { dex1.deleteOnExit(); TestTools.compileToDexWithJack(a1, dex1); try { - new DexComparator().compare(dex1, dex1, false /* compareDebugInfo */, true /* strict */, - false /* compareDebugInfoBinarily */, true /* compareCodeBinarily */); + new DexComparator(false /* compareDebugInfo */, true /* strict */, + false /* compareDebugInfoBinarily */, true /* compareCodeBinarily */).compare(dex1, dex1); } catch (DifferenceFoundException e) { Assert.fail(e.getMessage()); } diff --git a/jack/tests/com/android/jack/TestTools.java b/jack/tests/com/android/jack/TestTools.java index f41995b..59ffd25 100644 --- a/jack/tests/com/android/jack/TestTools.java +++ b/jack/tests/com/android/jack/TestTools.java @@ -846,8 +846,8 @@ public class TestTools { jarjarRules).dexFile; // Compare Jack Dex file to reference - new DexComparator().compare(refDex, jackDex, withDebugInfo, strict, - false /* compareDebugInfoBinary */, compareInstructionNumber, instructionNumberTolerance); + new DexComparator(withDebugInfo, strict, false /* compareDebugInfoBinary */, + compareInstructionNumber, instructionNumberTolerance).compare(refDex, jackDex); } private static void unzip(@Nonnull File jarfile, @Nonnull File outputFolder) { diff --git a/jack/tests/com/android/jack/compile/androidtree/core/CoreCompilationTest.java b/jack/tests/com/android/jack/compile/androidtree/core/CoreCompilationTest.java index 6415e5d..4f497c0 100644 --- a/jack/tests/com/android/jack/compile/androidtree/core/CoreCompilationTest.java +++ b/jack/tests/com/android/jack/compile/androidtree/core/CoreCompilationTest.java @@ -74,8 +74,8 @@ public class CoreCompilationTest { false); // Compare dex files structures and number of instructions - new DexComparator().compare(coreDexFromJava, coreDexFromJack, false /* withDebugInfo */, - false /* strict */, false /* compareDebugInfoBinary */, - true /* compareInstructionNumber */, 0); + new DexComparator(false /* withDebugInfo */, false /* strict */, + false /* compareDebugInfoBinary */, true /* compareInstructionNumber */, 0).compare( + coreDexFromJava, coreDexFromJack); } } diff --git a/jack/tests/com/android/jack/tools/merger/MergerTestTools.java b/jack/tests/com/android/jack/tools/merger/MergerTestTools.java index beed737..0144cf0 100644 --- a/jack/tests/com/android/jack/tools/merger/MergerTestTools.java +++ b/jack/tests/com/android/jack/tools/merger/MergerTestTools.java @@ -56,8 +56,8 @@ public class MergerTestTools { File oneDexPerTypeMerged = buildOneDexPerType(sourceFolder, withDebug); - new DexComparator().compare(monoDex, oneDexPerTypeMerged, false, true, - false /* compareDebugInfoBinary */, true, 0); + new DexComparator(false, true, false /* compareDebugInfoBinary */, true, 0).compare(monoDex, + oneDexPerTypeMerged); new DexAnnotationsComparator().compare(monoDex, oneDexPerTypeMerged); @@ -84,8 +84,8 @@ public class MergerTestTools { File oneDexPerTypeMerged = buildOneDexPerTypeFromJack(jackOutputFolder, true); - new DexComparator().compare(monoDex, oneDexPerTypeMerged, false, true, - false /* compareDebugInfoBinary */, true, 0); + new DexComparator(false, true, false /* compareDebugInfoBinary */, true, 0).compare(monoDex, + oneDexPerTypeMerged); new DexAnnotationsComparator().compare(monoDex, oneDexPerTypeMerged); |