Improve run-test tooling support

A useful thing to do with run-test tests is to run them in parallel to
reproduce low-probability flakes. We have a tool called
parallel_run.py to do this. Unfortunately one had to manually create
the script that parallel_run.py would execute, making it rather
difficult to use. This adds a --create-runner flag to run-test that
will cause it to write a script (called $DEX_LOCATION/runit.sh) that
is compatible with parallel_run.py

Test: % ./test/run-test --host --64 --prebuild 001-HelloWorld
      ...
      Runnable test script written to /tmp/allight/test-180018/runit.sh
      ...
      % ./tools/parallel_run.py /tmp/allight/test-180018/runit.sh -j80

Change-Id: I18d4ec72b51fb3c868ca4c6fe7f191e74f45b868
diff --git a/test/etc/run-test-jar b/test/etc/run-test-jar
index 596f9b4..eacb81d 100755
--- a/test/etc/run-test-jar
+++ b/test/etc/run-test-jar
@@ -95,6 +95,7 @@
 DEX2OAT_TIMEOUT="300" # 5 mins
 # The *hard* timeout where we really start trying to kill the dex2oat.
 DEX2OAT_RT_TIMEOUT="360" # 6 mins
+CREATE_RUNNER="n"
 
 # if "y", run 'sync' before dalvikvm to make sure all files from
 # build step (e.g. dex2oat) were finished writing.
@@ -172,6 +173,9 @@
         FLAGS="${FLAGS} -Xcompiler-option $option"
         COMPILE_FLAGS="${COMPILE_FLAGS} $option"
         shift
+    elif [ "x$1" = "x--create-runner" ]; then
+        CREATE_RUNNER="y"
+        shift
     elif [ "x$1" = "x--android-runtime-option" ]; then
         shift
         option="$1"
@@ -653,7 +657,15 @@
   if [ "$DEV_MODE" = "y" ]; then
     echo $cmdline
   fi
-  $cmdline
+  if [ "$CREATE_RUNNER" = "y" ]; then
+    echo "#!/bin/bash" > runit.sh
+    echo "export LD_LIBRARY_PATH=\"$LD_LIBRARY_PATH\""
+    echo $cmdline "2>&1" >> runit.sh
+    chmod u+x runit.sh
+    echo "Runnable test script written to $PWD/runit.sh"
+  else
+    $cmdline
+  fi
   exit
 fi
 
@@ -1252,6 +1264,15 @@
     $strip_cmdline || { echo "Strip failed." >&2 ; exit 3; }
     $sync_cmdline || { echo "Sync failed." >&2 ; exit 4; }
 
+    if [ "$CREATE_RUNNER" = "y" ]; then
+      echo "#!/bin/bash" > ${DEX_LOCATION}/runit.sh
+      for var in ANDROID_PRINTF_LOG ANDROID_DATA ANDROID_ROOT ANDROID_I18N_ROOT ANDROID_TZDATA_ROOT ANDROID_ART_ROOT LD_LIBRARY_PATH DYLD_LIBRARY_PATH PATH LD_USE_LOAD_BIAS; do
+        echo export $var="${!var}" >> ${DEX_LOCATION}/runit.sh
+      done
+      echo $cmdline "2>&1" >> ${DEX_LOCATION}/runit.sh
+      chmod u+x $DEX_LOCATION/runit.sh
+      echo "Runnable test script written to ${DEX_LOCATION}/runit.sh"
+    fi
     if [ "$DRY_RUN" = "y" ]; then
       exit 0
     fi
diff --git a/test/run-test b/test/run-test
index 72e7562..707d93f 100755
--- a/test/run-test
+++ b/test/run-test
@@ -138,6 +138,7 @@
 prebuild_mode="yes"
 target_mode="yes"
 dev_mode="no"
+create_runner="no"
 update_mode="no"
 debug_mode="no"
 relocate="no"
@@ -365,6 +366,13 @@
         fi
         run_args+=(--invoke-with "${what}")
         shift
+    elif [ "x$1" = "x--create-runner" ]; then
+        run_args+=(--create-runner --dry-run)
+        dev_mode="yes"
+        never_clean="yes"
+        create_runner="yes"
+        shift
+        shift
     elif [ "x$1" = "x--dev" ]; then
         run_args+=(--dev)
         dev_mode="yes"
@@ -697,6 +705,11 @@
     run_args+=(--no-image)
 fi
 
+if [ "$create_runner" = "yes" -a "$target_mode" = "yes" ]; then
+    err_echo "--create-runner does not function for non --host tests"
+    usage="yes"
+fi
+
 if [ "$dev_mode" = "yes" -a "$update_mode" = "yes" ]; then
     err_echo "--dev and --update are mutually exclusive"
     usage="yes"
@@ -746,6 +759,11 @@
         echo "  $prog [options] [test-name]           Run test normally."
         echo "  $prog --dev [options] [test-name]     Development mode" \
              "(dumps to stdout)."
+        echo "  $prog --create-runner [options] [test-name]"
+        echo "              Creates a runner script for use with other " \
+             "tools (e.g. parallel_run.py)."
+        echo "              The script will only run the test portion, and " \
+             "share oat and dex files."
         echo "  $prog --update [options] [test-name]  Update mode" \
              "(replaces expected.txt)."
         echo '  Omitting the test name or specifying "-" will use the' \