Update AOSP build script to support R8

* Add --tool r8 option
* Generate the script external/proguard/bin/proguard.sh either
  calling proguard or calling R8.

Change-Id: I7d4ed796aa2ad7b4951ab9081ae57d6ae3d5bd44
diff --git a/tools/build_aosp.py b/tools/build_aosp.py
index a8ccbaf..d284a14 100755
--- a/tools/build_aosp.py
+++ b/tools/build_aosp.py
@@ -6,9 +6,11 @@
 from os.path import join
 from glob import glob
 from itertools import chain
+from stat import S_IRWXU
 import argparse
 import multiprocessing
 import os
+import re
 import sys
 
 import gradle
@@ -24,7 +26,7 @@
       description = 'Checkout the AOSP source tree.')
   utils_aosp.add_common_arguments(parser)
   parser.add_argument('--tool',
-      choices = ['jack', 'd8', 'default'],
+      choices = ['jack', 'd8', 'r8', 'default'],
       default = 'd8',
       help='Compiler tool to use. Defaults to d8.')
   parser.add_argument('--clean-dex',
@@ -58,6 +60,51 @@
   if counter > 0:
     print('Removed {} dex files.'.format(counter))
 
+PROGUARD_SCRIPT = """#!/bin/sh
+#
+# Start-up script for ProGuard -- free class file shrinker, optimizer,
+# obfuscator, and preverifier for Java bytecode.
+#
+# Note: when passing file names containing spaces to this script,
+#       you\'ll have to add escaped quotes around them, e.g.
+#       "\"/My Directory/My File.txt\""
+
+# Account for possibly missing/basic readlink.
+# POSIX conformant (dash/ksh/zsh/bash).
+PROGUARD=`readlink -f "$0" 2>/dev/null`
+if test "$PROGUARD" = \'\'
+then
+  PROGUARD=`readlink "$0" 2>/dev/null`
+  if test "$PROGUARD" = \'\'
+  then
+    PROGUARD="$0"
+  fi
+fi
+
+PROGUARD_HOME=`dirname "$PROGUARD"`/..
+
+# BEGIN android-changed Added -Xmx2G for Mac builds
+java -Xmx2G -jar "$PROGUARD_HOME/lib/proguard.jar" "$@"
+# END android-changed
+"""
+
+def prepare_for_proguard(aosp_root):
+  # Write the default proguard.sh script.
+  proguard_script = join(aosp_root, 'external', 'proguard', 'bin', 'proguard.sh')
+  with open(proguard_script, 'w') as f:
+    f.write(PROGUARD_SCRIPT)
+
+  os.chmod(proguard_script, S_IRWXU)
+
+def prepare_for_r8(aosp_root):
+  # Write the proguard.sh script invoking R8.
+  compat_proguard_jar = join(
+      utils.REPO_ROOT, 'build', 'libs', 'compatproguard.jar')
+  proguard_script = join(aosp_root, 'external', 'proguard', 'bin', 'proguard.sh')
+  with open(proguard_script, 'w') as f:
+    f.write('java -jar ' + compat_proguard_jar + ' "$@"')
+  os.chmod(proguard_script, S_IRWXU)
+
 def build_aosp(aosp_root, lunch, tool, concurrency):
   jack_option = 'ANDROID_COMPILE_WITH_JACK=' \
       + ('true' if tool == 'jack' else 'false')
@@ -67,9 +114,17 @@
   if tool == 'd8':
     alt_jar_option += utils.COMPATDX_JAR
 
+  if tool == 'r8':
+    prepare_for_r8(aosp_root)
+    # Use D8 compatdx dexer with hack for forwarding the R8 dex file.
+    alt_jar_option += utils.COMPATDX_JAR
+  else:
+    prepare_for_proguard(aosp_root)
+
   j_option = '-j' + str(concurrency);
   print("-- Building Android image with 'make {} {} {}'." \
     .format(j_option, jack_option, alt_jar_option))
+
   utils_aosp.run_through_aosp_helper(lunch,
       ['make', j_option, jack_option, alt_jar_option], aosp_root)
 
@@ -77,7 +132,7 @@
   args = parse_arguments()
 
   # Build the required tools.
-  if args.tool == 'd8':
+  if args.tool == 'd8' or args.tool == 'r8':
     gradle.RunGradle(['d8', 'r8', 'compatdx', 'compatproguard'])
 
   setup_and_clean_dex(args.aosp_root, args.tool, args.clean_dex)