Update CompatProguard

* Accept and check --multi-dex
* Turn off desugar

Bug: 67922556
Change-Id: I4fd7ea3719f9cc2b51ef30407822efb9052729d7
diff --git a/src/main/java/com/android/tools/r8/R8.java b/src/main/java/com/android/tools/r8/R8.java
index 62e3413..2cd6b77 100644
--- a/src/main/java/com/android/tools/r8/R8.java
+++ b/src/main/java/com/android/tools/r8/R8.java
@@ -403,7 +403,7 @@
    * @param command R8 command.
    * @return the compilation result.
    */
-  public static AndroidApp run(R8Command command) throws IOException, CompilationException {
+  public static R8Output run(R8Command command) throws IOException, CompilationException {
     InternalOptions options = command.getInternalOptions();
     ExecutorService executorService = ThreadUtils.getExecutorService(options);
     try {
@@ -413,6 +413,19 @@
     }
   }
 
+  /**
+   * TODO(sgjesse): Get rid of this.
+   */
+  public static AndroidApp runInternal(R8Command command) throws IOException, CompilationException {
+    InternalOptions options = command.getInternalOptions();
+    ExecutorService executorService = ThreadUtils.getExecutorService(options);
+    try {
+      return runInternal(command, executorService);
+    } finally {
+      executorService.shutdown();
+    }
+  }
+
   private static void writeProguardMapToPath(Path path, AndroidApp outputApp) throws IOException {
     try (Closer closer = Closer.create()) {
       OutputStream mapOut = FileUtils.openPathWithDefault(
@@ -484,7 +497,20 @@
    * @param executor executor service from which to get threads for multi-threaded processing.
    * @return the compilation result.
    */
-  public static AndroidApp run(R8Command command, ExecutorService executor)
+  public static R8Output run(R8Command command, ExecutorService executor)
+      throws IOException, CompilationException {
+    InternalOptions options = command.getInternalOptions();
+    AndroidApp outputApp =
+        runForTesting(command.getInputApp(), options, executor).androidApp;
+    R8Output output = new R8Output(outputApp, command.getOutputMode());
+    writeOutputs(command, options, outputApp);
+    return output;
+  }
+
+  /**
+   * TODO(sgjesse): Get rid of this.
+   */
+  public static AndroidApp runInternal(R8Command command, ExecutorService executor)
       throws IOException, CompilationException {
     InternalOptions options = command.getInternalOptions();
     AndroidApp outputApp =
diff --git a/src/main/java/com/android/tools/r8/R8Output.java b/src/main/java/com/android/tools/r8/R8Output.java
new file mode 100644
index 0000000..5134a39
--- /dev/null
+++ b/src/main/java/com/android/tools/r8/R8Output.java
@@ -0,0 +1,22 @@
+// Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package com.android.tools.r8;
+
+import com.android.tools.r8.utils.AndroidApp;
+import com.android.tools.r8.utils.OutputMode;
+import java.io.IOException;
+import java.nio.file.Path;
+
+/** Represents the output of a R8 compilation. */
+public class R8Output extends BaseOutput {
+
+  R8Output(AndroidApp app, OutputMode outputMode) {
+    super(app, outputMode);
+  }
+
+  @Override
+  public void write(Path output) throws IOException {
+    getAndroidApp().write(output, getOutputMode());
+  }
+}
diff --git a/src/main/java/com/android/tools/r8/compatproguard/CompatProguard.java b/src/main/java/com/android/tools/r8/compatproguard/CompatProguard.java
index 75c28d1..34798e4 100644
--- a/src/main/java/com/android/tools/r8/compatproguard/CompatProguard.java
+++ b/src/main/java/com/android/tools/r8/compatproguard/CompatProguard.java
@@ -7,20 +7,25 @@
 import com.android.tools.r8.CompilationException;
 import com.android.tools.r8.R8;
 import com.android.tools.r8.R8Command;
+import com.android.tools.r8.R8Output;
 import com.android.tools.r8.Version;
+import com.android.tools.r8.errors.CompilationError;
 import com.google.common.collect.ImmutableList;
 import java.io.IOException;
 import java.nio.file.Paths;
 import java.util.List;
 
 /**
- * Proguard + Dx compatibility interface for r8.
+ * Proguard + dx compatibility interface for r8.
  *
- * This should become a mostly drop-in replacement for uses of Proguard followed by Dx.
+ * This should become a mostly drop-in replacement for uses of Proguard followed by dx for
+ * use with the Android Platform build.
  *
  * It accepts all Proguard flags supported by r8, except -outjars.
  *
- * The flag -outjars does not make sense as r8 (like Proguard + Dx) produces Dex output.
+ * It accepts a few dx flags which are known to be used in the Android Platform build.
+ *
+ * The flag -outjars does not make sense as r8 (like Proguard + dx) produces Dex output.
  * For output use --output as for R8 proper.
  */
 public class CompatProguard {
@@ -28,13 +33,15 @@
     public final String output;
     public final int minApi;
     public final boolean forceProguardCompatibility;
+    public final boolean multiDex;
     public final List<String> proguardConfig;
 
     CompatProguardOptions(List<String> proguardConfig, String output, int minApi,
-        boolean forceProguardCompatibility) {
+        boolean multiDex, boolean forceProguardCompatibility) {
       this.output = output;
       this.minApi = minApi;
       this.forceProguardCompatibility = forceProguardCompatibility;
+      this.multiDex = multiDex;
       this.proguardConfig = proguardConfig;
     }
 
@@ -42,6 +49,7 @@
       String output = null;
       int minApi = 1;
       boolean forceProguardCompatibility = false;
+      boolean multiDex = false;
       ImmutableList.Builder<String> builder = ImmutableList.builder();
       if (args.length > 0) {
         StringBuilder currentLine = new StringBuilder(args[0]);
@@ -54,6 +62,8 @@
               forceProguardCompatibility = true;
             } else if (arg.equals("--output")) {
               output = args[++i];
+            } else if (arg.equals("--multi-dex")) {
+              multiDex = true;
             } else if (arg.equals("-outjars")) {
               throw new CompilationException(
                   "Proguard argument -outjar is not supported. Use R8 compatible --output flag");
@@ -67,7 +77,8 @@
         }
         builder.add(currentLine.toString());
       }
-      return new CompatProguardOptions(builder.build(), output, minApi, forceProguardCompatibility);
+      return new CompatProguardOptions(builder.build(), output, minApi, multiDex,
+          forceProguardCompatibility);
     }
   }
 
@@ -84,7 +95,15 @@
     builder.setOutputPath(Paths.get(options.output))
         .addProguardConfiguration(options.proguardConfig)
         .setMinApiLevel(options.minApi);
-    R8.run(builder.build());
+    R8Output result = R8.run(builder.build());
+
+    if (!options.multiDex) {
+      if (result.getDexResources().size() > 1) {
+        throw new CompilationError(
+            "Compilation result could not fit into a single dex file. "
+                + "Reduce the input-program size or run with --multi-dex enabled");
+      }
+    }
   }
 
   public static void main(String[] args) throws IOException {
diff --git a/src/main/java/com/android/tools/r8/compatproguard/CompatProguardCommandBuilder.java b/src/main/java/com/android/tools/r8/compatproguard/CompatProguardCommandBuilder.java
index 145d0e6..2c7c43b 100644
--- a/src/main/java/com/android/tools/r8/compatproguard/CompatProguardCommandBuilder.java
+++ b/src/main/java/com/android/tools/r8/compatproguard/CompatProguardCommandBuilder.java
@@ -9,5 +9,6 @@
 public class CompatProguardCommandBuilder extends R8Command.Builder {
   CompatProguardCommandBuilder(boolean forceProguardCompatibility) {
     super(true, forceProguardCompatibility);
+    setEnableDesugaring(false);
   }
 }
diff --git a/src/test/java/com/android/tools/r8/ToolHelper.java b/src/test/java/com/android/tools/r8/ToolHelper.java
index 0a26131..bb35911 100644
--- a/src/test/java/com/android/tools/r8/ToolHelper.java
+++ b/src/test/java/com/android/tools/r8/ToolHelper.java
@@ -672,7 +672,7 @@
 
   public static AndroidApp runR8(Collection<String> fileNames, String out)
       throws IOException, CompilationException {
-    return R8.run(
+    return R8.runInternal(
         R8Command.builder()
             .addProgramFiles(ListUtils.map(fileNames, Paths::get))
             .setOutputPath(Paths.get(out))
diff --git a/src/test/java/com/android/tools/r8/jsr45/JSR45Tests.java b/src/test/java/com/android/tools/r8/jsr45/JSR45Tests.java
index c15b716..f253a12 100644
--- a/src/test/java/com/android/tools/r8/jsr45/JSR45Tests.java
+++ b/src/test/java/com/android/tools/r8/jsr45/JSR45Tests.java
@@ -60,7 +60,7 @@
   void compileWithR8(Path inputPath, Path outputPath, Path keepRulesPath)
       throws IOException, CompilationException {
     AndroidApp androidApp =
-        R8.run(
+        R8.runInternal(
             R8Command.builder()
                 .addProgramFiles(inputPath)
                 .addLibraryFiles(Paths.get(ToolHelper.getDefaultAndroidJar()))