Add option to test.py to use desugared library from GitHub HEAD

Bug: 179896028
Change-Id: I7968308ae93398bc508ac1f9735d8a061287fd78
diff --git a/build.gradle b/build.gradle
index 09bd946..f4881c2 100644
--- a/build.gradle
+++ b/build.gradle
@@ -2007,6 +2007,10 @@
         systemProperty 'slow_tests', project.property('slow_tests')
     }
 
+    if (project.hasProperty('desugar_jdk_libs')) {
+        systemProperty 'desugar_jdk_libs', project.property('desugar_jdk_libs')
+    }
+
     if (project.hasProperty('one_line_per_test')) {
         beforeTest { desc ->
             println "Start executing test ${desc.name} [${desc.className}]"
diff --git a/src/test/java/com/android/tools/r8/ToolHelper.java b/src/test/java/com/android/tools/r8/ToolHelper.java
index f873336..6c395bf 100644
--- a/src/test/java/com/android/tools/r8/ToolHelper.java
+++ b/src/test/java/com/android/tools/r8/ToolHelper.java
@@ -137,7 +137,8 @@
   public static final String DEFAULT_PROGUARD_MAP_FILE = "proguard.map";
 
   public static final String JAVA_8_RUNTIME = "third_party/openjdk/openjdk-rt-1.8/rt.jar";
-  public static final String DESUGAR_JDK_LIBS = "third_party/openjdk/desugar_jdk_libs/libjava.jar";
+  public static final String DESUGAR_JDK_LIBS =
+      System.getProperty("desugar_jdk_libs", "third_party/openjdk/desugar_jdk_libs/libjava.jar");
   public static final String CORE_LAMBDA_STUBS =
       "third_party/core-lambda-stubs/core-lambda-stubs.jar";
   public static final String JSR223_RI_JAR = "third_party/jsr223-api-1.0/jsr223-api-1.0.jar";
diff --git a/tools/test.py b/tools/test.py
index dffc4565..546a25f 100755
--- a/tools/test.py
+++ b/tools/test.py
@@ -16,6 +16,7 @@
 import time
 import uuid
 
+import archive_desugar_jdk_libs
 import gradle
 import notify
 import utils
@@ -161,6 +162,8 @@
       help='Enable Java debug agent and suspend compilation (default disabled)',
       default=False,
       action='store_true')
+  result.add_option('--desugared-library', '--desugared_library',
+      help='Build and use desugared library from GitHub.')
   return result.parse_args()
 
 def archive_failures():
@@ -178,6 +181,23 @@
   if utils.is_bot():
     gradle.RunGradle(['--no-daemon', 'clean'])
 
+  desugar_jdk_libs = None
+  if options.desugared_library:
+    if options.desugared_library != 'HEAD':
+      print("Only value supported for --desugared-library is 'HEAD'")
+      exit(1)
+    desugar_jdk_libs_dir = 'build/desugar_jdk_libs'
+    shutil.rmtree(desugar_jdk_libs_dir, ignore_errors=True)
+    os.makedirs(desugar_jdk_libs_dir)
+    print('Building desugared library.')
+    with utils.TempDir() as checkout_dir:
+      archive_desugar_jdk_libs.CloneDesugaredLibrary('google', checkout_dir)
+      (library_jar, maven_zip) = archive_desugar_jdk_libs.BuildDesugaredLibrary(checkout_dir)
+      desugar_jdk_libs = os.path.join(desugar_jdk_libs_dir, os.path.basename(library_jar))
+      shutil.copyfile(library_jar, desugar_jdk_libs)
+      print('Desugared library for test in ' + desugar_jdk_libs)
+
+
   gradle_args = ['--stacktrace']
   if utils.is_bot():
     # Bots don't like dangling processes.
@@ -261,6 +281,9 @@
     gradle_args.append('--no-daemon')
   if options.debug_agent:
     gradle_args.append('--no-daemon')
+  if desugar_jdk_libs:
+    gradle_args.append('-Pdesugar_jdk_libs=' + desugar_jdk_libs)
+
 
   # Build an R8 with dependencies for bootstrapping tests before adding test sources.
   gradle_args.append('r8WithDeps')