Add support for sharding tests

This allows you to specify the number of shards and the current shard to run when invoking test.py

Change-Id: I3981623c7cfc88d96178b615d9c4334c2a07a2ed
diff --git a/build.gradle b/build.gradle
index a247b4d..ac31d46 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1611,6 +1611,23 @@
     if (project.hasProperty('only_jctf')) {
         include "com/android/tools/r8/jctf/**"
     }
+    if (project.hasProperty('shard_count') ) {
+      assert project.hasProperty('shard_number')
+      int shard_count = project.getProperty('shard_count') as Integer
+      int shard_number = project.getProperty('shard_number') as Integer
+      assert shard_count < 65536
+      assert shard_number < shard_count
+      exclude {
+        entry ->
+          // Don't leave out directories. Leaving out a directory means all entries below.
+          if (entry.file.isDirectory()) {
+           return false
+          }
+          def first4 = entry.getRelativePath().toString().md5().substring(0, 4)
+          int hash = Integer.parseInt(first4, 16)
+          return hash % shard_count != shard_number
+      }
+    }
     if (project.hasProperty('jctf_compile_only')) {
         println "JCTF: compiling only"
         systemProperty 'jctf_compile_only', '1'
diff --git a/tools/test.py b/tools/test.py
index ef8fc30..05c5453 100755
--- a/tools/test.py
+++ b/tools/test.py
@@ -99,6 +99,10 @@
       help='Use a custom java version to run tests.')
   result.add_option('--java-max-memory-size', '--java_max_memory_size',
       help='Use a custom max memory size for the gradle java instance, eg, 4g')
+  result.add_option('--shard-count', '--shard_count',
+      help='We are running this many shards.')
+  result.add_option('--shard-number', '--shard_number',
+      help='We are running this shard.')
   result.add_option('--generate-golden-files-to', '--generate_golden_files_to',
       help='Store dex files produced by tests in the specified directory.'
            ' It is aimed to be read on platforms with no host runtime available'
@@ -130,6 +134,10 @@
 
   gradle_args = ['--stacktrace']
   # Set all necessary Gradle properties and options first.
+  if options.shard_count:
+    assert options.shard_number
+    gradle_args.append('-Pshard_count=%s' % options.shard_count)
+    gradle_args.append('-Pshard_number=%s' % options.shard_number)
   if options.verbose:
     gradle_args.append('-Pprint_test_stdout')
   if options.no_internal: