Move vm-test-tf over to D8.

Bug: 69865358
Test: Test: m cts; cts-tradefed run cts -m vm-tests-tf

Change-Id: I4e277f8912800fc77a2f13a1808b128862090e28
diff --git a/tools/vm-tests-tf/src/util/build/BuildDalvikSuite.java b/tools/vm-tests-tf/src/util/build/BuildDalvikSuite.java
index 49a5a29..6fc43b6 100644
--- a/tools/vm-tests-tf/src/util/build/BuildDalvikSuite.java
+++ b/tools/vm-tests-tf/src/util/build/BuildDalvikSuite.java
@@ -445,7 +445,7 @@
 
         }
 
-        DxBuildStep dexBuildStep = new DxBuildStep(
+        D8BuildStep dexBuildStep = new D8BuildStep(
             new BuildStep.BuildFile(new File(CLASSES_OUTPUT_FOLDER)),
             new BuildStep.BuildFile(new File(mainsJar)),
             false);
@@ -571,7 +571,7 @@
                 OUTPUT_FOLDER,
                 classFileName + ".jar");
 
-        DxBuildStep dexBuildStep = new DxBuildStep(tmpJarFile,
+        D8BuildStep dexBuildStep = new D8BuildStep(tmpJarFile,
                 outputFile,
                 true);
 
diff --git a/tools/vm-tests-tf/src/util/build/D8BuildStep.java b/tools/vm-tests-tf/src/util/build/D8BuildStep.java
new file mode 100644
index 0000000..cd97dba
--- /dev/null
+++ b/tools/vm-tests-tf/src/util/build/D8BuildStep.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package util.build;
+
+import com.android.tools.r8.CompilationMode;
+import com.android.tools.r8.D8;
+import com.android.tools.r8.D8Command;
+import com.android.tools.r8.utils.OutputMode;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.attribute.BasicFileAttributes;
+
+public class D8BuildStep extends BuildStep {
+
+  private final boolean deleteInputFileAfterBuild;
+  private final D8Command.Builder builder;
+
+  D8BuildStep(BuildFile inputFile, BuildFile outputFile, boolean deleteInputFileAfterBuild) {
+    super(inputFile, outputFile);
+    this.deleteInputFileAfterBuild = deleteInputFileAfterBuild;
+    this.builder =
+        D8Command.builder()
+            .setMode(CompilationMode.DEBUG)
+            .setMinApiLevel(1000)
+            .setEnableDesugaring(false)
+            .setOutputMode(OutputMode.Indexed);
+  }
+
+  @Override
+  boolean build() {
+
+    if (super.build()) {
+      try {
+        builder.setOutputPath(Paths.get(outputFile.fileName.getAbsolutePath()));
+        Files.find(
+                Paths.get(inputFile.fileName.getAbsolutePath()),
+                1000,
+                D8BuildStep::isJarOrClassFile)
+            .forEach(
+                p -> {
+                  try {
+                    builder.addProgramFiles(p);
+                  } catch (Throwable e) {
+                    e.printStackTrace();
+                  }
+                });
+        D8.run(builder.build());
+      } catch (Throwable e) {
+        e.printStackTrace();
+        return false;
+      }
+      if (deleteInputFileAfterBuild) {
+        inputFile.fileName.delete();
+      }
+      return true;
+    }
+    return false;
+  }
+
+  @Override
+  public int hashCode() {
+    return inputFile.hashCode() ^ outputFile.hashCode();
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (super.equals(obj)) {
+      D8BuildStep other = (D8BuildStep) obj;
+
+      return inputFile.equals(other.inputFile) && outputFile.equals(other.outputFile);
+    }
+    return false;
+  }
+
+  private static boolean isJarOrClassFile(Path file, BasicFileAttributes attrs) {
+    if (!attrs.isRegularFile()) {
+      return false;
+    }
+    String name = file.getFileName().toString().toLowerCase();
+    return name.endsWith(".jar") || name.endsWith(".class");
+  }
+}
diff --git a/tools/vm-tests-tf/src/util/build/DxBuildStep.java b/tools/vm-tests-tf/src/util/build/DxBuildStep.java
deleted file mode 100644
index 6e347b2..0000000
--- a/tools/vm-tests-tf/src/util/build/DxBuildStep.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package util.build;
-
-import com.android.dx.command.dexer.Main;
-import java.io.IOException;
-
-public class DxBuildStep extends BuildStep {
-
-    private final boolean deleteInputFileAfterBuild;
-
-    DxBuildStep(BuildFile inputFile, BuildFile outputFile,
-            boolean deleteInputFileAfterBuild) {
-        super(inputFile, outputFile);
-        this.deleteInputFileAfterBuild = deleteInputFileAfterBuild;
-    }
-
-    @Override
-    boolean build() {
-
-        if (super.build()) {
-            Main.Arguments args = new Main.Arguments();
-
-            args.jarOutput = true;
-            args.fileNames = new String[] {inputFile.fileName.getAbsolutePath()};
-
-            args.outName = outputFile.fileName.getAbsolutePath();
-
-            int result = 0;
-            try {
-                result = Main.run(args);
-            } catch (IOException e) {
-                e.printStackTrace();
-                return false;
-            }
-
-            if (result == 0) {
-                if (deleteInputFileAfterBuild) {
-                    inputFile.fileName.delete();
-                }
-                return true;
-            } else {
-                System.err.println("exception while dexing "
-                        + inputFile.fileName.getAbsolutePath() + " to "
-                        + args.outName);
-                return false;
-            }
-        }
-        return false;
-    }
-
-    @Override
-    public int hashCode() {
-        return inputFile.hashCode() ^ outputFile.hashCode();
-    }
-
-    @Override
-    public boolean equals(Object obj) {
-        if (super.equals(obj)) {
-            DxBuildStep other = (DxBuildStep) obj;
-
-            return inputFile.equals(other.inputFile)
-                    && outputFile.equals(other.outputFile);
-        }
-        return false;
-    }
-
-
-}