Simulate the golem environment when using run_benchmarks.py

This fixes the TiviIncremental benchmark such that its library
is obtained from the existing dependency.

Bug: b/221813621
Change-Id: I80084612ae28cb51dddb15a4b1adcacb5ca1f487
diff --git a/src/test/java/com/android/tools/r8/benchmarks/BenchmarkEnvironment.java b/src/test/java/com/android/tools/r8/benchmarks/BenchmarkEnvironment.java
index f558932..fe36c52 100644
--- a/src/test/java/com/android/tools/r8/benchmarks/BenchmarkEnvironment.java
+++ b/src/test/java/com/android/tools/r8/benchmarks/BenchmarkEnvironment.java
@@ -29,7 +29,11 @@
 
   public Path translateDependencyPath(String directoryName, Path location) {
     return isGolem
-        ? Paths.get("benchmarks", config.getDependencyDirectoryName(), directoryName)
+        ? getGolemDependencyRoot().resolve(directoryName)
         : location.resolve(directoryName);
   }
+
+  public Path getGolemDependencyRoot() {
+    return Paths.get("benchmarks", config.getDependencyDirectoryName());
+  }
 }
diff --git a/src/test/java/com/android/tools/r8/benchmarks/BenchmarkMainEntryRunner.java b/src/test/java/com/android/tools/r8/benchmarks/BenchmarkMainEntryRunner.java
index 3151343..d5f84a8 100644
--- a/src/test/java/com/android/tools/r8/benchmarks/BenchmarkMainEntryRunner.java
+++ b/src/test/java/com/android/tools/r8/benchmarks/BenchmarkMainEntryRunner.java
@@ -3,6 +3,12 @@
 // BSD-style license that can be found in the LICENSE file.
 package com.android.tools.r8.benchmarks;
 
+import com.android.tools.r8.ToolHelper;
+import com.android.tools.r8.ToolHelper.ProcessResult;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import org.junit.rules.TemporaryFolder;
 
 public class BenchmarkMainEntryRunner {
@@ -19,18 +25,40 @@
       throw new RuntimeException("Invalid identifier identifier: " + benchmarkName);
     }
     boolean isGolem = isGolemArg.equals("golem");
-    if (!isGolem && !isGolemArg.equals("local")) {
-      throw new RuntimeException(
-          "Invalid value for arg 3, expected 'golem' or 'local', got '" + isGolemArg + "'");
-    }
     BenchmarkCollection collection = BenchmarkCollection.computeCollection();
     BenchmarkConfig config = collection.getBenchmark(identifier);
     if (config == null) {
       throw new RuntimeException("Unknown identifier: " + identifier);
     }
+
     TemporaryFolder temp = new TemporaryFolder();
     temp.create();
-    config.run(new BenchmarkEnvironment(config, temp, isGolem));
-    temp.delete();
+    try {
+      // When running locally we still setup a "golem" environment and manually unpack dependencies.
+      BenchmarkEnvironment environment = new BenchmarkEnvironment(config, temp, true /* isGolem */);
+      if (!isGolem) {
+        // When not running with golem, the python wrapper will run the benchmark in a temp
+        // directory.
+        // In this case the argument is the absolute path to the R8 repo.
+        Path repoRoot = Paths.get(isGolemArg);
+        Path dependencyDirectory = Files.createDirectories(environment.getGolemDependencyRoot());
+        for (BenchmarkDependency dependency : config.getDependencies()) {
+          untar(repoRoot.resolve(dependency.getTarball()), dependencyDirectory);
+        }
+      }
+      System.out.println("Running benchmark");
+      config.run(environment);
+    } finally {
+      temp.delete();
+    }
+  }
+
+  private static void untar(Path tarball, Path target) throws IOException {
+    ProcessBuilder builder =
+        new ProcessBuilder("tar", "zxf", tarball.toString(), "-C", target.toString());
+    ProcessResult result = ToolHelper.runProcess(builder);
+    if (result.exitCode != 0) {
+      throw new IOException(result.toString());
+    }
   }
 }
diff --git a/src/test/java/com/android/tools/r8/benchmarks/appdumps/AppDumpBenchmarkBuilder.java b/src/test/java/com/android/tools/r8/benchmarks/appdumps/AppDumpBenchmarkBuilder.java
index f8b16df..ecfb059 100644
--- a/src/test/java/com/android/tools/r8/benchmarks/appdumps/AppDumpBenchmarkBuilder.java
+++ b/src/test/java/com/android/tools/r8/benchmarks/appdumps/AppDumpBenchmarkBuilder.java
@@ -224,6 +224,7 @@
 
                   TestBase.testForD8(environment.getTemp(), Backend.DEX)
                       .addProgramFiles(programOutputs)
+                      .addLibraryFiles(dump.getLibraryArchive())
                       .setMinApi(dumpProperties.getMinApi())
                       .benchmarkCompile(results.getSubResults(builder.nameForMergePart()));
                 });
diff --git a/tools/run_benchmark.py b/tools/run_benchmark.py
index 904b509..41da451 100755
--- a/tools/run_benchmark.py
+++ b/tools/run_benchmark.py
@@ -8,10 +8,10 @@
 import subprocess
 import sys
 
+import compiledump
 import gradle
 import jdk
 import utils
-import compiledump
 
 NONLIB_BUILD_TARGET = 'R8WithRelocatedDeps'
 NONLIB_TEST_BUILD_TARGETS = [utils.R8_TESTS_TARGET, utils.R8_TESTS_DEPS_TARGET]
@@ -99,7 +99,13 @@
   if not options.no_build:
     gradle.RunGradle(buildTargets + ['-Pno_internal'])
 
-  return run(options, r8jar, testjars)
+  if not options.golem:
+    # When running locally, change the working directory to be in 'temp'.
+    # This is hard to do properly within the JVM so we do it here.
+    with utils.ChangedWorkingDirectory(temp):
+      return run(options, r8jar, testjars)
+  else:
+    return run(options, r8jar, testjars)
 
 def run(options, r8jar, testjars):
   jdkhome = get_jdk_home(options, options.benchmark)
@@ -113,7 +119,9 @@
     'com.android.tools.r8.benchmarks.BenchmarkMainEntryRunner',
     options.benchmark,
     options.target,
-    'golem' if options.golem else 'local',
+    # When running locally the working directory is moved and we pass the
+    # repository root as an argument. The runner can then setup dependencies.
+    'golem' if options.golem else utils.REPO_ROOT,
     ])
   return subprocess.check_call(cmd)