Add a simple Proguard comaptibility tool

The tool will just read all proguard options from the command line and
place them into a Proguard configuration file. Then run R8 with that
file as the main Proguard configuration file.

Change-Id: Ia110e01db64543fa65a39aa0df7c77de2bd5d9a6
diff --git a/build.gradle b/build.gradle
index 7362238..b69be1f 100644
--- a/build.gradle
+++ b/build.gradle
@@ -379,6 +379,20 @@
     }
 }
 
+task CompatProguard(type: Jar) {
+    from sourceSets.main.output
+    baseName 'compatproguard'
+    manifest {
+        attributes 'Main-Class': 'com.android.tools.r8.compatproguard.CompatProguard'
+    }
+    // In order to build without dependencies, pass the exclude_deps property using:
+    // gradle -Pexclude_deps CompatProguard
+    if (!project.hasProperty('exclude_deps')) {
+        // Also include dependencies
+        from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
+    }
+}
+
 task D8Logger(type: Jar) {
     from sourceSets.main.output
     baseName 'd8logger'
diff --git a/src/main/java/com/android/tools/r8/compatproguard/CompatProguard.java b/src/main/java/com/android/tools/r8/compatproguard/CompatProguard.java
new file mode 100644
index 0000000..45eb6e4
--- /dev/null
+++ b/src/main/java/com/android/tools/r8/compatproguard/CompatProguard.java
@@ -0,0 +1,56 @@
+// 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.compatproguard;
+
+import com.android.tools.r8.CompilationException;
+import com.android.tools.r8.R8;
+import com.android.tools.r8.R8Command;
+import com.google.common.collect.ImmutableList;
+import java.io.IOException;
+import java.util.List;
+
+public class CompatProguard {
+  public static class CompatProguardOptions {
+    public final List<String> proguardConfig;
+
+    CompatProguardOptions(List<String> proguardConfig) {
+      this.proguardConfig = proguardConfig;
+    }
+
+    public static CompatProguardOptions parse(String[] args) {
+      ImmutableList.Builder<String> builder = ImmutableList.builder();
+      if (args.length > 0) {
+        StringBuilder currentLine = new StringBuilder(args[0]);
+        for (int i = 1; i < args.length; i++) {
+          String arg = args[i];
+          if (arg.charAt(0) == '-') {
+            builder.add(currentLine.toString());
+            currentLine = new StringBuilder(arg);
+          } else {
+            currentLine.append(' ').append(arg);
+          }
+        }
+        builder.add(currentLine.toString());
+      }
+      return new CompatProguardOptions(builder.build());
+    }
+  }
+
+  private static void run(String[] args) throws IOException, CompilationException {
+    System.out.println("CompatProguard " + String.join(" ", args));
+    // Run R8 passing all the options from the command line as a Proguard configuration.
+    CompatProguardOptions options = CompatProguardOptions.parse(args);
+    R8.run(R8Command.builder().addProguardConfiguration(options.proguardConfig).build());
+  }
+
+  public static void main(String[] args) throws IOException {
+    try {
+      run(args);
+    } catch (CompilationException e) {
+      System.err.println(e.getMessage());
+      System.exit(1);
+    }
+  }
+}