Update AOSP tools

* Share common code
* Make the --aosp-root default to build/aosp in the R8 checkout

Change-Id: Ia76aad590f079b852bfa8f581f73082da571de68
diff --git a/tools/build_aosp.py b/tools/build_aosp.py
index 8bb9e11..a8ccbaf 100755
--- a/tools/build_aosp.py
+++ b/tools/build_aosp.py
@@ -6,7 +6,6 @@
 from os.path import join
 from glob import glob
 from itertools import chain
-from subprocess import check_call
 import argparse
 import multiprocessing
 import os
@@ -14,10 +13,7 @@
 
 import gradle
 import utils
-
-AOSP_HELPER_SH = join(utils.REPO_ROOT, 'scripts', 'aosp_helper.sh')
-
-DEFAULT_LUNCH = 'aosp_x86-eng'
+import utils_aosp
 
 J_DEFAULT = multiprocessing.cpu_count() - 2
 
@@ -26,30 +22,25 @@
 def parse_arguments():
   parser = argparse.ArgumentParser(
       description = 'Checkout the AOSP source tree.')
-  parser.add_argument('--aosp-root',
-                      help='Root of the AOSP checkout. ' +
-                           'Defaults to current working directory.',
-                      default=os.getcwd())
-  parser.add_argument('--lunch',
-                      help='Build menu. ' +
-                           'Defaults to ' + DEFAULT_LUNCH + '.',
-                      default=DEFAULT_LUNCH)
+  utils_aosp.add_common_arguments(parser)
   parser.add_argument('--tool',
-                      choices = ['jack', 'd8', 'default'],
-                      default = 'd8',
-                      help='Compiler tool to use. Defaults to d8.')
+      choices = ['jack', 'd8', 'default'],
+      default = 'd8',
+      help='Compiler tool to use. Defaults to d8.')
   parser.add_argument('--clean-dex',
       action = 'store_true',
       help = 'Remove all dex files before the build. By default they'
       " are removed only if '--tool=d8' and if they are older than the D8 tool")
   parser.add_argument('-j',
-                      help='Projects to fetch simultaneously. ' +
-                           'Defaults to ' + str(J_DEFAULT) + '.',
-                      type=int,
-                      default=J_DEFAULT)
+      help='Projects to fetch simultaneously. ' +
+      'Defaults to ' + str(J_DEFAULT) + '.',
+      type=int,
+      default=J_DEFAULT)
   return parser.parse_args()
 
 def setup_and_clean_dex(aosp_root, tool, clean_dex):
+  print "Running AOSP build in " + aosp_root
+
   out = join(aosp_root, 'out')
   utils.makedirs_if_needed(out)
 
@@ -79,15 +70,15 @@
   j_option = '-j' + str(concurrency);
   print("-- Building Android image with 'make {} {} {}'." \
     .format(j_option, jack_option, alt_jar_option))
-  check_call([AOSP_HELPER_SH, lunch, 'make', j_option,
-              jack_option, alt_jar_option], cwd = aosp_root)
+  utils_aosp.run_through_aosp_helper(lunch,
+      ['make', j_option, jack_option, alt_jar_option], aosp_root)
 
 def Main():
   args = parse_arguments()
 
   # Build the required tools.
   if args.tool == 'd8':
-    gradle.RunGradle(['d8', 'd8logger', 'compatdx'])
+    gradle.RunGradle(['d8', 'r8', 'compatdx', 'compatproguard'])
 
   setup_and_clean_dex(args.aosp_root, args.tool, args.clean_dex)
 
diff --git a/tools/emulator_aosp.py b/tools/emulator_aosp.py
index 90a6000..f63c673 100755
--- a/tools/emulator_aosp.py
+++ b/tools/emulator_aosp.py
@@ -3,34 +3,23 @@
 # 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.
 
-from os.path import join
-from subprocess import check_call
 import argparse
-import os
 import sys
 
 import utils
-
-AOSP_HELPER_SH = join(utils.REPO_ROOT, 'scripts', 'aosp_helper.sh')
-
-DEFAULT_LUNCH = 'aosp_x86-eng'
+import utils_aosp
 
 def parse_arguments():
   parser = argparse.ArgumentParser(
       description = 'Checkout the AOSP source tree.')
-  parser.add_argument('--aosp-root',
-                      help='Root of the AOSP checkout. ' +
-                           'Defaults to current working directory.',
-                      default=os.getcwd())
-  parser.add_argument('--lunch',
-                      help='Build menu. ' +
-                           'Defaults to ' + DEFAULT_LUNCH + '.',
-                      default=DEFAULT_LUNCH)
+  utils_aosp.add_common_arguments(parser)
   return parser.parse_args()
 
 def emulator_aosp(aosp_root, lunch):
-  check_call([AOSP_HELPER_SH, lunch,
-      'emulator_fg', '-partition-size', '4096', '-wipe-data'], cwd = aosp_root)
+  print "Running AOSP emulator in " + aosp_root
+
+  utils_aosp.run_through_aosp_helper(lunch, ['emulator_fg',
+      '-partition-size', '4096', '-wipe-data'], cwd = aosp_root)
 
 def Main():
   args = parse_arguments()
diff --git a/tools/utils_aosp.py b/tools/utils_aosp.py
new file mode 100644
index 0000000..8f20c2f
--- /dev/null
+++ b/tools/utils_aosp.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python
+# 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.
+
+
+from os.path import join
+from subprocess import check_call
+
+import os
+
+import utils
+
+AOSP_HELPER_SH = join(utils.REPO_ROOT, 'scripts', 'aosp_helper.sh')
+
+DEFAULT_LUNCH = 'aosp_x86-eng'
+
+DEFAULT_ROOT = join(utils.REPO_ROOT, 'build', 'aosp')
+
+def add_common_arguments(parser):
+  parser.add_argument('--aosp-root',
+      help='Root of the AOSP checkout. ' +
+           'Defaults to ' +  DEFAULT_ROOT +'.',
+      default=DEFAULT_ROOT)
+  parser.add_argument('--lunch',
+      help='Build menu. ' +
+           'Defaults to ' + DEFAULT_LUNCH + '.',
+      default=DEFAULT_LUNCH)
+
+def run_through_aosp_helper(lunch, args, cwd):
+  args[0:0] = [AOSP_HELPER_SH, lunch]
+  check_call(args, cwd = cwd)