Add more build properties and flags to dump infrastructure.

This adds compilation mode as well as the top-level tree-shaking and
minification settings to build.properties. The compiledump.py script
is amended with support for setting max heap and thread count.

Change-Id: I20fb03625c10e48b62274d316691805f32f14d09
diff --git a/src/main/java/com/android/tools/r8/utils/AndroidApp.java b/src/main/java/com/android/tools/r8/utils/AndroidApp.java
index 8599b02..9dd4865 100644
--- a/src/main/java/com/android/tools/r8/utils/AndroidApp.java
+++ b/src/main/java/com/android/tools/r8/utils/AndroidApp.java
@@ -472,7 +472,13 @@
   }
 
   private String getBuildPropertiesContents(InternalOptions options) {
-    return "min-api=" + options.minApiLevel;
+    return String.join(
+        "\n",
+        ImmutableList.of(
+            "mode=" + (options.debug ? "debug" : "release"),
+            "min-api=" + options.minApiLevel,
+            "tree-shaking=" + options.isTreeShakingEnabled(),
+            "minification=" + options.isMinificationEnabled()));
   }
 
   private int dumpLibraryResources(int nextDexIndex, ZipOutputStream out)
diff --git a/src/main/java/com/android/tools/r8/utils/CompileDumpCompatR8.java b/src/main/java/com/android/tools/r8/utils/CompileDumpCompatR8.java
index 936395f..e349c9a 100644
--- a/src/main/java/com/android/tools/r8/utils/CompileDumpCompatR8.java
+++ b/src/main/java/com/android/tools/r8/utils/CompileDumpCompatR8.java
@@ -9,6 +9,7 @@
 import com.android.tools.r8.DexIndexedConsumer.ArchiveConsumer;
 import com.android.tools.r8.OutputMode;
 import com.android.tools.r8.R8;
+import com.android.tools.r8.R8Command;
 import com.android.tools.r8.R8Command.Builder;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -17,6 +18,8 @@
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 /**
  * Wrapper to make it easy to call R8 in compat mode when compiling a dump file.
@@ -44,7 +47,8 @@
           "--main-dex-list-output",
           "--pg-conf",
           "--pg-map-output",
-          "--desugared-lib");
+          "--desugared-lib",
+          "--threads");
 
   private static final List<String> VALID_OPTIONS_WITH_TWO_OPERANDS =
       Arrays.asList("--feature-jar");
@@ -69,6 +73,7 @@
     List<Path> classpath = new ArrayList<>();
     List<Path> config = new ArrayList<>();
     int minApi = 1;
+    int threads = -1;
     for (int i = 0; i < args.length; i++) {
       String option = args[i];
       if (VALID_OPTIONS.contains(option)) {
@@ -129,6 +134,11 @@
               pgMapOutput = Paths.get(operand);
               break;
             }
+          case "--threads":
+            {
+              threads = Integer.parseInt(operand);
+              break;
+            }
           default:
             throw new IllegalArgumentException("Unimplemented option: " + option);
         }
@@ -174,6 +184,16 @@
     if (pgMapOutput != null) {
       commandBuilder.setProguardMapOutputPath(pgMapOutput);
     }
-    R8.run(commandBuilder.build());
+    R8Command command = commandBuilder.build();
+    if (threads != -1) {
+      ExecutorService executor = Executors.newWorkStealingPool(threads);
+      try {
+        R8.run(command, executor);
+      } finally {
+        executor.shutdown();
+      }
+    } else {
+      R8.run(command);
+    }
   }
 }
diff --git a/tools/compiledump.py b/tools/compiledump.py
index 511ec8d..962bc23 100755
--- a/tools/compiledump.py
+++ b/tools/compiledump.py
@@ -3,15 +3,15 @@
 # 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.
 
+import archive
 import argparse
+import jdk
 import os
+import retrace
 import subprocess
 import sys
 import zipfile
 
-import archive
-import jdk
-import retrace
 import utils
 
 
@@ -65,6 +65,18 @@
       help='Enable Java debug agent and suspend compilation (default disabled)',
       default=False,
       action='store_true')
+  parser.add_argument(
+      '--xmx',
+      help='Set JVM max heap size (-Xmx)',
+      default=None)
+  parser.add_argument(
+      '--threads',
+      help='Set the number of threads to use',
+      default=None)
+  parser.add_argument(
+      '--min-api',
+      help='Set min-api (default read from dump properties file)',
+      default=None)
   return parser
 
 def error(msg):
@@ -155,6 +167,13 @@
 def determine_output(args, temp):
   return os.path.join(temp, 'out.jar')
 
+def determine_min_api(args, build_properties):
+  if args.min_api:
+    return args.min_api
+  if 'min-api' in build_properties:
+    return build_properties.get('min-api')
+  return None
+
 def determine_feature_output(feature_jar, temp):
   return os.path.join(temp, os.path.basename(feature_jar)[:-4] + ".out.jar")
 
@@ -193,6 +212,7 @@
     version = determine_version(args, dump)
     compiler = determine_compiler(args, dump)
     out = determine_output(args, temp)
+    min_api = determine_min_api(args, build_properties)
     jar = args.r8_jar if args.r8_jar else download_distribution(args, version, temp)
     wrapper_dir = prepare_wrapper(jar, temp)
     cmd = [jdk.GetJavaExecutable()]
@@ -201,6 +221,8 @@
         print "WARNING: Running debugging agent on r8lib is questionable..."
       cmd.append(
           '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005')
+    if args.xmx:
+      cmd.append('-Xmx' + args.xmx)
     if args.ea:
       cmd.append('-ea')
     if args.printtimes:
@@ -225,8 +247,10 @@
       cmd.extend(['--pg-conf', dump.config_file()])
     if compiler != 'd8':
       cmd.extend(['--pg-map-output', '%s.map' % out])
-    if 'min-api' in build_properties:
-      cmd.extend(['--min-api', build_properties.get('min-api')])
+    if min_api:
+      cmd.extend(['--min-api', min_api])
+    if args.threads:
+      cmd.extend(['--threads', args.threads])
     cmd.extend(otherargs)
     utils.PrintCmd(cmd)
     try: