Extend compiledump to check for apimodeling and android platform build

Change-Id: I56c664b2c2c7d65e6304219ba6c6d7aca7cf00d2
diff --git a/src/main/java/com/android/tools/r8/utils/CompileDumpCompatR8.java b/src/main/java/com/android/tools/r8/utils/CompileDumpCompatR8.java
index 292f103..17b3673 100644
--- a/src/main/java/com/android/tools/r8/utils/CompileDumpCompatR8.java
+++ b/src/main/java/com/android/tools/r8/utils/CompileDumpCompatR8.java
@@ -12,6 +12,7 @@
 import com.android.tools.r8.R8Command;
 import com.android.tools.r8.R8Command.Builder;
 import java.io.IOException;
+import java.lang.reflect.Method;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -22,6 +23,7 @@
 import java.util.Map;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.function.Consumer;
 
 /**
  * Wrapper to make it easy to call R8 in compat mode when compiling a dump file.
@@ -36,7 +38,13 @@
 public class CompileDumpCompatR8 {
 
   private static final List<String> VALID_OPTIONS =
-      Arrays.asList("--classfile", "--compat", "--debug", "--release");
+      Arrays.asList(
+          "--classfile",
+          "--compat",
+          "--debug",
+          "--release",
+          "--enable-missing-library-api-modeling",
+          "--android-platform-build");
 
   private static final List<String> VALID_OPTIONS_WITH_SINGLE_OPERAND =
       Arrays.asList(
@@ -78,6 +86,8 @@
     List<Path> mainDexRulesFiles = new ArrayList<>();
     int minApi = 1;
     int threads = -1;
+    boolean enableMissingLibraryApiModeling = false;
+    boolean androidPlatformBuild = false;
     for (int i = 0; i < args.length; i++) {
       String option = args[i];
       if (VALID_OPTIONS.contains(option)) {
@@ -102,6 +112,12 @@
               compilationMode = CompilationMode.RELEASE;
               break;
             }
+          case "--enable-missing-library-api-modeling":
+            enableMissingLibraryApiModeling = true;
+            break;
+          case "--android-platform-build":
+            androidPlatformBuild = true;
+            break;
           default:
             throw new IllegalArgumentException("Unimplemented option: " + option);
         }
@@ -187,6 +203,11 @@
             .addMainDexRulesFiles(mainDexRulesFiles)
             .setOutput(outputPath, outputMode)
             .setMode(compilationMode);
+    getReflectiveBuilderMethod(
+            commandBuilder, "setEnableExperimentalMissingLibraryApiModeling", boolean.class)
+        .accept(new Object[] {enableMissingLibraryApiModeling});
+    getReflectiveBuilderMethod(commandBuilder, "setAndroidPlatformBuild", boolean.class)
+        .accept(new Object[] {androidPlatformBuild});
     if (desugaredLibJson != null) {
       commandBuilder.addDesugaredLibraryConfiguration(readAllBytesJava7(desugaredLibJson));
     }
@@ -217,6 +238,24 @@
     }
   }
 
+  private static Consumer<Object[]> getReflectiveBuilderMethod(
+      Builder builder, String setter, Class<?>... parameters) {
+    try {
+      Method declaredMethod = CompatProguardCommandBuilder.class.getMethod(setter, parameters);
+      return args -> {
+        try {
+          declaredMethod.invoke(builder, args);
+        } catch (Exception e) {
+          throw new RuntimeException(e);
+        }
+      };
+    } catch (NoSuchMethodException e) {
+      e.printStackTrace();
+      // The option is not available so we just return an empty consumer
+      return args -> {};
+    }
+  }
+
   // We cannot use StringResource since this class is added to the class path and has access only
   // to the public APIs.
   private static String readAllBytesJava7(Path filePath) {
diff --git a/tools/compiledump.py b/tools/compiledump.py
index 9f3d1b0..5962cc0 100755
--- a/tools/compiledump.py
+++ b/tools/compiledump.py
@@ -129,6 +129,16 @@
     help='Run the compilation in a loop',
     default=False,
     action='store_true')
+  parser.add_argument(
+    '--enable-missing-library-api-modeling',
+    help='Run with api modeling',
+    default=False,
+    action='store_true')
+  parser.add_argument(
+    '--android-platform-build',
+    help='Run as a platform build',
+    default=False,
+    action='store_true')
   return parser
 
 def error(msg):
@@ -271,6 +281,20 @@
     return True
   return None
 
+def determine_android_platform_build(args, build_properties):
+  if args.android_platform_build:
+    return args.android_platform_build
+  if 'android-platform-build=true' in build_properties:
+    return True
+  return None
+
+def determine_enable_missing_library_api_modeling(args, build_properties):
+  if args.enable_missing_library_api_modeling:
+    return args.enable_missing_library_api_modeling
+  if 'enable-missing-library-api-modeling=true' in build_properties:
+    return True
+  return None
+
 def determine_properties(build_properties):
   args = []
   for key, value in build_properties.items():
@@ -367,6 +391,8 @@
     out = determine_output(args, temp)
     min_api = determine_min_api(args, build_properties)
     classfile = determine_class_file(args, build_properties)
+    android_platform_build = determine_android_platform_build(args, build_properties)
+    enable_missing_library_api_modeling = determine_enable_missing_library_api_modeling(args, build_properties)
     jar = args.r8_jar if args.r8_jar else download_distribution(version, args.nolib, temp)
     if ':' not in jar and not os.path.exists(jar):
       error("Distribution does not exist: " + jar)
@@ -429,6 +455,10 @@
       cmd.extend(['--min-api', min_api])
     if classfile:
       cmd.extend(['--classfile'])
+    if android_platform_build:
+      cmd.extend(['--android-platform-build'])
+    if enable_missing_library_api_modeling:
+      cmd.extend(['--enable-missing-library-api-modeling'])
     if args.threads:
       cmd.extend(['--threads', args.threads])
     cmd.extend(otherargs)