Add android platform build flag to lint

Bug: b/302055774
Change-Id: Ib15779a07685bda15e11a0ca9e760b32d7e640f6
diff --git a/src/main/java/com/android/tools/r8/BackportedMethodList.java b/src/main/java/com/android/tools/r8/BackportedMethodList.java
index 04c4886..f6644c9 100644
--- a/src/main/java/com/android/tools/r8/BackportedMethodList.java
+++ b/src/main/java/com/android/tools/r8/BackportedMethodList.java
@@ -51,13 +51,14 @@
       StringUtils.joinLines(
           "Usage: BackportedMethodList [options]",
           " Options are:",
-          "  --output <file>         # Output result in <file>.",
-          "  --min-api <number>      # Minimum Android API level for the application",
-          "  --desugared-lib <file>  # Desugared library configuration (JSON from the",
-          "                          # configuration)",
-          "  --lib <file>            # The compilation SDK library (android.jar)",
-          "  --version               # Print the version of BackportedMethodList.",
-          "  --help                  # Print this message.");
+          "  --output <file>          # Output result in <file>.",
+          "  --min-api <number>       # Minimum Android API level for the application",
+          "  --desugared-lib <file>   # Desugared library configuration (JSON from the",
+          "                           # configuration)",
+          "  --lib <file>             # The compilation SDK library (android.jar)",
+          "  --android-platform-build # Compilation of platform code",
+          "  --version                # Print the version of BackportedMethodList.",
+          "  --help                   # Print this message.");
 
   public static void run(BackportedMethodListCommand command) throws CompilationFailedException {
     if (command.isPrintHelp()) {
@@ -83,6 +84,9 @@
       builder.setDesugarLibrarySpecification(
           StringResource.fromString(jsonSource, Origin.unknown()));
     }
+    if (command.isAndroidPlatformBuild()) {
+      builder.setAndroidPlatformBuild();
+    }
     return builder
         .setMinApi(command.getMinApiLevel())
         .setOutputConsumer(command.getBackportedMethodListConsumer())
diff --git a/src/main/java/com/android/tools/r8/BackportedMethodListCommand.java b/src/main/java/com/android/tools/r8/BackportedMethodListCommand.java
index c79e8fe..1558357 100644
--- a/src/main/java/com/android/tools/r8/BackportedMethodListCommand.java
+++ b/src/main/java/com/android/tools/r8/BackportedMethodListCommand.java
@@ -42,6 +42,7 @@
   private final boolean printVersion;
   private final Reporter reporter;
   private final int minApiLevel;
+  private final boolean androidPlatformBuild;
   private final DesugaredLibrarySpecification desugaredLibrarySpecification;
   private final AndroidApp app;
   private final StringConsumer backportedMethodListConsumer;
@@ -55,6 +56,10 @@
     return printVersion;
   }
 
+  public boolean isAndroidPlatformBuild() {
+    return androidPlatformBuild;
+  }
+
   Reporter getReporter() {
     return reporter;
   }
@@ -80,6 +85,7 @@
     this.printVersion = printVersion;
     this.reporter = new Reporter();
     this.minApiLevel = -1;
+    this.androidPlatformBuild = false;
     this.desugaredLibrarySpecification = null;
     this.app = null;
     this.backportedMethodListConsumer = null;
@@ -89,6 +95,7 @@
   private BackportedMethodListCommand(
       Reporter reporter,
       int minApiLevel,
+      boolean androidPlatformBuild,
       DesugaredLibrarySpecification desugaredLibrarySpecification,
       AndroidApp app,
       StringConsumer backportedMethodListConsumer,
@@ -97,6 +104,7 @@
     this.printVersion = false;
     this.reporter = reporter;
     this.minApiLevel = minApiLevel;
+    this.androidPlatformBuild = androidPlatformBuild;
     this.desugaredLibrarySpecification = desugaredLibrarySpecification;
     this.app = app;
     this.backportedMethodListConsumer = backportedMethodListConsumer;
@@ -139,6 +147,8 @@
         builder.setPrintHelp(true);
       } else if (arg.equals("--version")) {
         builder.setPrintVersion(true);
+      } else if (arg.equals("--android-platform-build")) {
+        builder.setAndroidPlatformBuild(true);
       } else if (arg.equals("--min-api")) {
         if (hasDefinedApiLevel) {
           builder.error(new StringDiagnostic("Cannot set multiple --min-api options"));
@@ -184,6 +194,7 @@
     private StringConsumer backportedMethodListConsumer;
     private boolean printHelp = false;
     private boolean printVersion = false;
+    private boolean androidPlatformBuild = false;
 
     private Builder() {
       this(new DiagnosticsHandler() {});
@@ -289,6 +300,11 @@
       return this;
     }
 
+    public Builder setAndroidPlatformBuild(boolean androidPlatformBuild) {
+      this.androidPlatformBuild = androidPlatformBuild;
+      return this;
+    }
+
     /** True if the print-version flag is enabled. */
     public boolean isPrintVersion() {
       return printVersion;
@@ -332,6 +348,7 @@
       return new BackportedMethodListCommand(
           reporter,
           minApiLevel,
+          androidPlatformBuild,
           getDesugaredLibraryConfiguration(factory),
           library,
           backportedMethodListConsumer,
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/lint/DesugaredMethodsList.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/lint/DesugaredMethodsList.java
index 169dcfe..09f727b 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/lint/DesugaredMethodsList.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/lint/DesugaredMethodsList.java
@@ -31,10 +31,12 @@
 public class DesugaredMethodsList extends GenerateDesugaredLibraryLintFiles {
 
   private final AndroidApiLevel minApi;
+  private final boolean androidPlatformBuild;
   private final StringConsumer outputConsumer;
 
   DesugaredMethodsList(
       int minApi,
+      boolean androidPlatformBuild,
       Reporter reporter,
       StringResource desugarConfiguration,
       Collection<ProgramResourceProvider> desugarImplementation,
@@ -42,6 +44,7 @@
       Collection<ClassFileResourceProvider> androidJar) {
     super(reporter, desugarConfiguration, desugarImplementation, null, androidJar);
     this.minApi = AndroidApiLevel.getAndroidApiLevel(minApi);
+    this.androidPlatformBuild = androidPlatformBuild;
     this.outputConsumer = outputConsumer;
   }
 
@@ -61,6 +64,7 @@
           () -> {
             new DesugaredMethodsList(
                     command.getMinApi(),
+                    command.isAndroidPlatformBuild(),
                     command.getReporter(),
                     command.getDesugarLibrarySpecification(),
                     command.getDesugarLibraryImplementation(),
@@ -78,7 +82,7 @@
     AndroidApiLevel compilationLevel =
         desugaredLibrarySpecification.getRequiredCompilationApiLevel();
     SupportedClasses supportedMethods =
-        new SupportedClassesGenerator(options, androidJar, minApi, true)
+        new SupportedClassesGenerator(options, androidJar, minApi, androidPlatformBuild, true)
             .run(desugaredLibraryImplementation, desugaredLibrarySpecificationResource);
     System.out.println(
         "Generating lint files for "
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/lint/DesugaredMethodsListCommand.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/lint/DesugaredMethodsListCommand.java
index 726d445..d69cf6b 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/lint/DesugaredMethodsListCommand.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/lint/DesugaredMethodsListCommand.java
@@ -15,6 +15,7 @@
 import com.android.tools.r8.ProgramResourceProvider;
 import com.android.tools.r8.StringConsumer;
 import com.android.tools.r8.StringResource;
+import com.android.tools.r8.errors.Unreachable;
 import com.android.tools.r8.utils.AndroidApiLevel;
 import com.android.tools.r8.utils.Reporter;
 import com.android.tools.r8.utils.StringUtils;
@@ -38,6 +39,7 @@
   private final Collection<ProgramResourceProvider> desugarLibraryImplementation;
   private final StringConsumer outputConsumer;
   private final Collection<ClassFileResourceProvider> library;
+  private final boolean androidPlatformBuild;
 
   DesugaredMethodsListCommand(
       int minApi,
@@ -45,7 +47,8 @@
       StringResource desugarLibrarySpecification,
       Collection<ProgramResourceProvider> desugarLibraryImplementation,
       StringConsumer outputConsumer,
-      Collection<ClassFileResourceProvider> library) {
+      Collection<ClassFileResourceProvider> library,
+      boolean androidPlatformBuild) {
     this.help = false;
     this.version = false;
     this.minApi = minApi;
@@ -54,6 +57,7 @@
     this.desugarLibraryImplementation = desugarLibraryImplementation;
     this.outputConsumer = outputConsumer;
     this.library = library;
+    this.androidPlatformBuild = androidPlatformBuild;
   }
 
   DesugaredMethodsListCommand(boolean help, boolean version) {
@@ -65,12 +69,17 @@
     this.desugarLibraryImplementation = null;
     this.outputConsumer = null;
     this.library = null;
+    this.androidPlatformBuild = false;
   }
 
   public int getMinApi() {
     return minApi;
   }
 
+  public boolean isAndroidPlatformBuild() {
+    return androidPlatformBuild;
+  }
+
   public StringResource getDesugarLibrarySpecification() {
     return desugarLibrarySpecification;
   }
@@ -124,6 +133,7 @@
 
     private boolean help = false;
     private boolean version = false;
+    private boolean androidPlatformBuild = false;
 
     public Builder(DiagnosticsHandler diagnosticsHandler) {
       this.reporter = new Reporter(diagnosticsHandler);
@@ -177,6 +187,11 @@
       return this;
     }
 
+    public Builder setAndroidPlatformBuild() {
+      this.androidPlatformBuild = true;
+      return this;
+    }
+
     public DesugaredMethodsListCommand build() {
       // The min-api level defaults to 1, it's always present.
       // If desugarLibraryImplementation is empty, this generates only the backported method list.
@@ -185,8 +200,12 @@
         return new DesugaredMethodsListCommand(help, version);
       }
 
+      if (androidPlatformBuild && !desugarLibraryImplementation.isEmpty()) {
+        reporter.error("With platform build desugared library is not allowed.");
+      }
+
       if (desugarLibrarySpecification != null && library.isEmpty()) {
-        reporter.error("With desugared library configuration a library is required");
+        reporter.error("With desugared library configuration a library is required.");
       }
 
       if (!desugarLibraryImplementation.isEmpty() && desugarLibrarySpecification == null) {
@@ -213,7 +232,8 @@
           desugarLibrarySpecification,
           desugarLibraryImplementation,
           outputConsumer,
-          library);
+          library,
+          androidPlatformBuild);
     }
   }
 
@@ -227,6 +247,7 @@
           .add(ParseFlagInfoImpl.getVersion("DesugaredMethods"))
           .add(ParseFlagInfoImpl.getHelp())
           .add(ParseFlagInfoImpl.getDesugaredLib())
+          .add(ParseFlagInfoImpl.getAndroidPlatformBuild())
           .add(
               ParseFlagInfoImpl.flag1(
                   "--desugared-lib-jar", "<file>", "Specify desugared library jar."))
@@ -246,6 +267,9 @@
         } else if (arg.equals("--version")) {
           builder.setVersion();
           continue;
+        } else if (arg.equals("--android-platform-build")) {
+          builder.setAndroidPlatformBuild();
+          continue;
         }
         String argValue = args[i + 1].trim();
         if (arg.equals("--min-api")) {
@@ -259,6 +283,8 @@
           builder.setOutputPath(Paths.get(argValue));
         } else if (arg.equals("--lib")) {
           builder.addLibrary(new ArchiveClassFileProvider(Paths.get(argValue)));
+        } else {
+          throw new Unreachable("Unsupported argument " + arg);
         }
       }
 
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/lint/SupportedClassesGenerator.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/lint/SupportedClassesGenerator.java
index 595092b..5bdf2e6 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/lint/SupportedClassesGenerator.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/lint/SupportedClassesGenerator.java
@@ -43,6 +43,7 @@
 import com.android.tools.r8.utils.InternalOptions;
 import com.android.tools.r8.utils.ThreadUtils;
 import com.android.tools.r8.utils.Timing;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Sets;
 import java.io.IOException;
@@ -59,23 +60,26 @@
   private final DirectMappedDexApplication appForMax;
   private final AndroidApiLevel minApi;
   private final SupportedClasses.Builder builder = SupportedClasses.builder();
+  private final boolean androidPlatformBuild;
   private final boolean addBackports;
 
   public SupportedClassesGenerator(
       InternalOptions options, Collection<ClassFileResourceProvider> androidJar)
       throws IOException {
-    this(options, androidJar, AndroidApiLevel.B, false);
+    this(options, androidJar, AndroidApiLevel.B, false, false);
   }
 
   public SupportedClassesGenerator(
       InternalOptions options,
       Collection<ClassFileResourceProvider> androidJar,
       AndroidApiLevel minApi,
+      boolean androidPlatformBuild,
       boolean addBackports)
       throws IOException {
     this.options = options;
     this.appForMax = createAppForMax(androidJar);
     this.minApi = minApi;
+    this.androidPlatformBuild = androidPlatformBuild;
     this.addBackports = addBackports;
   }
 
@@ -155,8 +159,7 @@
       AppInfoWithClassHierarchy appInfo = appView.appInfoForDesugaring();
 
       // This should depend only on machine specification and min api.
-      List<DexMethod> backports =
-          BackportedMethodRewriter.generateListOfBackportedMethods(appForMax, options);
+      List<DexMethod> backports = generateListOfBackportedMethods();
 
       int finalApi = api;
       builder.forEachClassAndMethod(
@@ -279,8 +282,7 @@
     DirectMappedDexApplication implementationApplication =
         new ApplicationReader(implementation, options, Timing.empty()).read().toDirect();
 
-    List<DexMethod> backports =
-        BackportedMethodRewriter.generateListOfBackportedMethods(appForMax, options);
+    List<DexMethod> backports = generateListOfBackportedMethods();
 
     for (DexProgramClass clazz : implementationApplication.classes()) {
       // All emulated interfaces static and default methods are supported.
@@ -366,6 +368,13 @@
     }
   }
 
+  private List<DexMethod> generateListOfBackportedMethods() throws IOException {
+    if (androidPlatformBuild) {
+      return ImmutableList.of();
+    }
+    return BackportedMethodRewriter.generateListOfBackportedMethods(appForMax, options);
+  }
+
   private void registerMethod(
       DexMethod method, DexApplication implementationApplication, List<DexMethod> backports) {
     DexClass dexClass = implementationApplication.definitionFor(method.getHolderType());