Update desugared_library_jdk_11 deps

This updates to
https://github.com/google/desugar_jdk_libs/commit/4d875ab1e15e336557361f12c26183638597ef97

Also

  * add script for updating desugared library
  * store the hash of the desugared library with the dependency

Bug: 244156342
Change-Id: Ic9b643d9b78895f837de9ef42c5a02d35244ee89
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/specificationconversion/LegacyToHumanSpecificationConverter.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/specificationconversion/LegacyToHumanSpecificationConverter.java
index c0b124b..5eb2f7f 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/specificationconversion/LegacyToHumanSpecificationConverter.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/specificationconversion/LegacyToHumanSpecificationConverter.java
@@ -67,7 +67,7 @@
     Map<ApiLevelRange, HumanRewritingFlags> libraryFlags =
         convertRewritingFlagMap(legacySpec.getLibraryFlags(), app, origin);
 
-    legacyLibraryFlagHacks(libraryFlags, app, origin);
+    legacyLibraryFlagHacks(humanTopLevelFlags.getIdentifier(), libraryFlags, app, origin);
     reportWarnings(app.options.reporter);
 
     MultiAPILevelHumanDesugaredLibrarySpecification humanSpec =
@@ -94,12 +94,12 @@
     Origin origin = Origin.unknown();
     HumanRewritingFlags humanRewritingFlags =
         convertRewritingFlags(legacySpec.getRewritingFlags(), app, origin);
-    if (app.options.getMinApiLevel().isLessThanOrEqualTo(LEGACY_HACK_LEVEL)
-        && legacySpec.isLibraryCompilation()) {
+    if (legacySpec.isLibraryCompilation()) {
       timing.begin("Legacy hacks");
       HumanRewritingFlags.Builder builder =
           humanRewritingFlags.newBuilder(app.options.reporter, origin);
-      legacyLibraryFlagHacks(app.dexItemFactory(), builder);
+      legacyLibraryFlagHacks(
+          app.dexItemFactory(), app.options.getMinApiLevel(), legacySpec.getIdentifier(), builder);
       humanRewritingFlags = builder.build();
       timing.end();
     }
@@ -128,7 +128,10 @@
   }
 
   private void legacyLibraryFlagHacks(
-      Map<ApiLevelRange, HumanRewritingFlags> libraryFlags, DexApplication app, Origin origin) {
+      String identifier,
+      Map<ApiLevelRange, HumanRewritingFlags> libraryFlags,
+      DexApplication app,
+      Origin origin) {
     ApiLevelRange range = new ApiLevelRange(LEGACY_HACK_LEVEL.getLevel());
     HumanRewritingFlags humanRewritingFlags = libraryFlags.get(range);
     if (humanRewritingFlags == null) {
@@ -137,39 +140,52 @@
     }
     HumanRewritingFlags.Builder builder =
         humanRewritingFlags.newBuilder(app.options.reporter, origin);
-    legacyLibraryFlagHacks(app.dexItemFactory(), builder);
+    legacyLibraryFlagHacks(app.dexItemFactory(), LEGACY_HACK_LEVEL, identifier, builder);
     libraryFlags.put(range, builder.build());
   }
 
   private void legacyLibraryFlagHacks(
-      DexItemFactory itemFactory, HumanRewritingFlags.Builder builder) {
+      DexItemFactory itemFactory,
+      AndroidApiLevel apiLevel,
+      String identifier,
+      HumanRewritingFlags.Builder builder) {
 
-    // TODO(b/177977763): This is only a workaround rewriting invokes of j.u.Arrays.deepEquals0
-    // to j.u.DesugarArrays.deepEquals0.
-    DexString name = itemFactory.createString("deepEquals0");
-    DexProto proto =
-        itemFactory.createProto(
-            itemFactory.booleanType, itemFactory.objectType, itemFactory.objectType);
-    DexMethod source =
-        itemFactory.createMethod(itemFactory.createType(itemFactory.arraysDescriptor), proto, name);
-    DexType target = itemFactory.createType("Ljava/util/DesugarArrays;");
-    builder.retargetMethod(source, target);
+    if (apiLevel.isLessThanOrEqualTo(LEGACY_HACK_LEVEL)) {
+      // TODO(b/177977763): This is only a workaround rewriting invokes of j.u.Arrays.deepEquals0
+      // to j.u.DesugarArrays.deepEquals0.
+      DexString name = itemFactory.createString("deepEquals0");
+      DexProto proto =
+          itemFactory.createProto(
+              itemFactory.booleanType, itemFactory.objectType, itemFactory.objectType);
+      DexMethod source =
+          itemFactory.createMethod(
+              itemFactory.createType(itemFactory.arraysDescriptor), proto, name);
+      DexType target = itemFactory.createType("Ljava/util/DesugarArrays;");
+      builder.retargetMethod(source, target);
 
-    builder.amendLibraryMethod(
-        source,
-        MethodAccessFlags.fromSharedAccessFlags(
-            Constants.ACC_PRIVATE | Constants.ACC_STATIC, false));
+      builder.amendLibraryMethod(
+          source,
+          MethodAccessFlags.fromSharedAccessFlags(
+              Constants.ACC_PRIVATE | Constants.ACC_STATIC, false));
 
-    // TODO(b/181629049): This is only a workaround rewriting invokes of
-    //  j.u.TimeZone.getTimeZone taking a java.time.ZoneId.
-    name = itemFactory.createString("getTimeZone");
-    proto =
-        itemFactory.createProto(
-            itemFactory.createType("Ljava/util/TimeZone;"),
-            itemFactory.createType("Ljava/time/ZoneId;"));
-    source = itemFactory.createMethod(itemFactory.createType("Ljava/util/TimeZone;"), proto, name);
-    target = itemFactory.createType("Ljava/util/DesugarTimeZone;");
-    builder.retargetMethod(source, target);
+      // TODO(b/181629049): This is only a workaround rewriting invokes of
+      //  j.u.TimeZone.getTimeZone taking a java.time.ZoneId.
+      name = itemFactory.createString("getTimeZone");
+      proto =
+          itemFactory.createProto(
+              itemFactory.createType("Ljava/util/TimeZone;"),
+              itemFactory.createType("Ljava/time/ZoneId;"));
+      source =
+          itemFactory.createMethod(itemFactory.createType("Ljava/util/TimeZone;"), proto, name);
+      target = itemFactory.createType("Ljava/util/DesugarTimeZone;");
+      builder.retargetMethod(source, target);
+    }
+    // Required by
+    // https://github.com/google/desugar_jdk_libs/commit/485071cd09a3691549d065ba9e323d07edccf085.
+    if (identifier.contains(":1.2")) {
+      builder.putRewriteDerivedPrefix(
+          "sun.misc.Desugar", "jdk.internal.misc.", "j$.sun.misc.Desugar");
+    }
   }
 
   private Map<ApiLevelRange, HumanRewritingFlags> convertRewritingFlagMap(
diff --git a/third_party/openjdk/desugar_jdk_libs_11.tar.gz.sha1 b/third_party/openjdk/desugar_jdk_libs_11.tar.gz.sha1
index 6925eca..524abea 100644
--- a/third_party/openjdk/desugar_jdk_libs_11.tar.gz.sha1
+++ b/third_party/openjdk/desugar_jdk_libs_11.tar.gz.sha1
@@ -1 +1 @@
-b5f9ea4c2328d7654db2966acd6582f4c1f80081
\ No newline at end of file
+ae103f657ce219b839706397baac799745d46049
\ No newline at end of file
diff --git a/tools/archive_desugar_jdk_libs.py b/tools/archive_desugar_jdk_libs.py
index 06dbf63..63d02c6 100755
--- a/tools/archive_desugar_jdk_libs.py
+++ b/tools/archive_desugar_jdk_libs.py
@@ -75,6 +75,9 @@
   'jdk11_nio': BASE_LIBRARY_NAME + '_jdk11_nio.zip'
 }
 
+DESUGAR_JDK_LIBS_HASH_FILE = os.path.join(
+    defines.THIRD_PARTY, 'openjdk', 'desugar_jdk_libs_11', 'desugar_jdk_libs_hash')
+
 
 def ParseOptions(argv):
   result = optparse.OptionParser()
@@ -131,11 +134,11 @@
     print('File available at: %s' %
         destination.replace('gs://', 'https://storage.googleapis.com/', 1))
 
-def CloneDesugaredLibrary(github_account, checkout_dir):
+def CloneDesugaredLibrary(github_account, checkout_dir, desugar_jdk_libs_hash):
   git_utils.GitClone(
     'https://github.com/'
         + github_account + '/' + GITHUB_REPRO, checkout_dir)
-  git_utils.GitCheckout('292df0eea1c2c1d6b8fe834c7b347ef0b0fdc11b', checkout_dir)
+  git_utils.GitCheckout(desugar_jdk_libs_hash, checkout_dir)
 
 def GetJavaEnv():
   java_env = dict(os.environ, JAVA_HOME = jdk.GetJdk11Home())
@@ -251,10 +254,13 @@
     raise Exception(path + ' does not exist or is not a directory')
 
 def BuildAndUpload(options, variant):
+  desugar_jdk_libs_hash = ''
+  with open(DESUGAR_JDK_LIBS_HASH_FILE, 'r') as input_hash:
+    desugar_jdk_libs_hash = input_hash.readline()
   if options.build_only:
     with utils.TempDir() as checkout_dir:
-      CloneDesugaredLibrary(options.github_account, checkout_dir)
-      (library_jar, maven_zip) = BuildDesugaredLibrary(checkout_dir, variant)
+      CloneDesugaredLibrary(options.github_account, checkout_dir, desugar_jdk_libs_hash)
+      (library_jar, maven_zip) = BuildDesugaredLibrary(checkout_dir, variant, desugar_jdk_libs_hash)
       shutil.copyfile(
         library_jar,
         os.path.join(options.build_only, os.path.basename(library_jar)))
@@ -267,7 +273,7 @@
   is_main = False
 
   with utils.TempDir() as checkout_dir:
-    CloneDesugaredLibrary(options.github_account, checkout_dir)
+    CloneDesugaredLibrary(options.github_account, checkout_dir, desugar_jdk_libs_hash)
     version = GetVersion(os.path.join(checkout_dir, VERSION_MAP[variant]))
 
     destination = archive.GetVersionDestination(
@@ -318,6 +324,7 @@
   utils.DownloadFromGoogleCloudStorage(utils.BAZEL_SHA_FILE)
   utils.DownloadFromGoogleCloudStorage(utils.JAVA8_SHA_FILE)
   utils.DownloadFromGoogleCloudStorage(utils.JAVA11_SHA_FILE)
+  utils.DownloadFromGoogleCloudStorage(utils.DESUGAR_JDK_LIBS_11_SHA_FILE)
 
   for v in options.variant:
     BuildAndUpload(options, v)
diff --git a/tools/desugar_jdk_libs_update.py b/tools/desugar_jdk_libs_update.py
new file mode 100755
index 0000000..2e751e9
--- /dev/null
+++ b/tools/desugar_jdk_libs_update.py
@@ -0,0 +1,93 @@
+#!/usr/bin/env python3
+# Copyright (c) 2022, 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.
+
+import argparse
+import os
+from os.path import join
+import shutil
+import subprocess
+import sys
+
+import utils
+
+def GetGitHash(checkout_dir):
+  return subprocess.check_output(
+      ['git', '-C', checkout_dir, 'rev-parse', 'HEAD']).decode('utf-8').strip()
+
+def run(args):
+  with utils.TempDir() as tmp_dir:
+    use_existing_checkout = args.desugar_jdk_libs_checkout != None
+    checkout_dir = (args.desugar_jdk_libs_checkout
+                    if use_existing_checkout
+                    else join(tmp_dir, 'desugar_jdk_libs'))
+    if (not use_existing_checkout):
+      subprocess.check_call(
+          ['git', 'clone', 'https://github.com/google/desugar_jdk_libs.git', checkout_dir])
+      if (args.desugar_jdk_libs_revision):
+        subprocess.check_call(
+            ['git', '-C', checkout_dir, 'checkout', args.desugar_jdk_libs_revision])
+    print("Building desugared library")
+    bazel = os.path.join(utils.BAZEL_TOOL, 'lib', 'bazel', 'bin', 'bazel')
+    with utils.ChangedWorkingDirectory(checkout_dir):
+      for target in [':desugar_jdk_libs_jdk11', '//jdk11/src:java_base_chm_only']:
+        subprocess.check_call([
+            bazel,
+            '--bazelrc=/dev/null',
+            'build',
+            '--spawn_strategy=local',
+            '--verbose_failures',
+            target])
+
+    openjdk_dir = join('third_party', 'openjdk')
+    openjdk_subdir = 'desugar_jdk_libs_11'
+    dest_dir = join(openjdk_dir, openjdk_subdir)
+    src_dir = join(checkout_dir, 'bazel-bin', 'jdk11', 'src')
+
+    metadata_files = ('LICENSE', 'README.google')
+    for f in metadata_files:
+      shutil.copyfile(join(dest_dir, f), join(tmp_dir, f))
+    shutil.rmtree(dest_dir)
+    os.remove(join(openjdk_dir, openjdk_subdir + '.tar.gz'))
+    os.remove(join(openjdk_dir, openjdk_subdir + '.tar.gz.sha1'))
+    os.mkdir(dest_dir)
+    for s in [
+        (join(src_dir, 'd8_java_base_selected_with_addon.jar'),
+          join(dest_dir, 'desugar_jdk_libs.jar')),
+        (join(src_dir, 'java_base_chm_only.jar'),
+          join(dest_dir, 'desugar_jdk_libs_chm_only.jar'))]:
+      shutil.copyfile(s[0], s[1])
+    for f in metadata_files:
+      shutil.copyfile(join(tmp_dir, f), join(dest_dir, f))
+    desugar_jdk_libs_hash = os.path.join(dest_dir, 'desugar_jdk_libs_hash')
+    with open(desugar_jdk_libs_hash, 'w') as desugar_jdk_libs_hash_writer:
+      desugar_jdk_libs_hash_writer.write(GetGitHash(checkout_dir))
+
+  print('Now run')
+  print('  (cd %s; upload_to_google_storage.py -a --bucket r8-deps %s)'
+      % (openjdk_dir, openjdk_subdir))
+
+
+
+def main():
+  args = parse_options()
+  run(args)
+
+def parse_options():
+  parser = argparse.ArgumentParser(
+    description='Script for updating third_party/openjdk/desugar_jdk_libs*')
+  parser.add_argument('--desugar-jdk-libs-checkout', '--desugar_jdk_libs_checkout',
+                      default=None,
+                      metavar=('<path>'),
+                      help='Use existing checkout of github.com/google/desugar_jdk_libs.')
+  parser.add_argument('--desugar-jdk-libs-revision', '--desugar_jdk_libs_revision',
+                      default=None,
+                      metavar=('<revision>'),
+                      help='Revision of github.com/google/desugar_jdk_libs to use.')
+  args = parser.parse_args()
+  return args
+
+
+if __name__ == '__main__':
+  sys.exit(main())