runtests.sh: build tests all together instead of one by one

Instead of building each test just before running it, build all
the tests together and then run them all together. This has two
advantages:

1. Building is faster because we don't have to start up soong and
   parse the ninja files for each test but only do it once at the
   beginning.
2. Test results appear all together and are easier to see in the
   scrollback buffer.

Test: ./system/netd/tests/runtests.sh
Test: ./system/netd/tests/runtests.sh unit
Test: ./system/netd/tests/runtests.sh utils
Change-Id: I3bb38aae5a1c05dca6a8bdbfbc98ed736854c96e
diff --git a/tests/runtests.sh b/tests/runtests.sh
index a221116..6f828fc 100755
--- a/tests/runtests.sh
+++ b/tests/runtests.sh
@@ -62,7 +62,6 @@
     echo "# $test"
     echo "###"
 
-    runCmd "$SOONG_BIN" --make-mode "$test" || return $?
     local prefix="$ANDROID_TARGET_OUT_TESTCASES/$test/$TARGET_ARCH/$test"
     for bits in '' 32 64; do
             local testbin="${prefix}${bits}"
@@ -118,9 +117,12 @@
 
     local failures=0
     local skipped=0
+
+    # Find out which tests to run.
+    local tests=""
     for testName in $DEFAULT_TESTS; do
         if [[ -z "$test_regex" || "$testName" =~ "$test_regex" ]]; then
-            runOneTest "$testName" || failures=$((failures + 1))
+            tests="$tests $testName"
         else
             logToStdErr "Skipping $testName"
             skipped=$((skipped + 1))
@@ -132,11 +134,24 @@
     if [[ -n "$test_regex" ]]; then
         for testName in $EXTENDED_TESTS; do
             if [[ "$testName" =~ "$test_regex" ]]; then
-                runOneTest "$testName" || failures=$((failures + 1))
+                tests="$tests $testName"
             fi
         done
     fi
 
+    if [[ -z "$tests" ]]; then
+        logToStdErr "No tests matched"
+        return 1
+    fi
+
+    # Build all the specified tests.
+    runCmd "$SOONG_BIN" --make-mode "$tests" || return $?
+
+    # Run all the specified tests.
+    for testName in $tests; do
+        runOneTest "$testName" || failures=$((failures + 1))
+    done
+
     echo "Number of tests failing: $failures"
     [[ $skipped -gt 0 ]] && echo "Number of tests skipped: $skipped"
     return $failures