Generate and load CTS cache

Cache .json files are generated at build time. This relies on a
list of the expected cache outputs.

The path to the cache is hardcoded in the test_runner and the
.json files are loaded at runtime.

Change-Id: Icc125125df7e9c338a243526dbc4950a2517039f
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/110441
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Austin Eng <enga@chromium.org>
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/third_party/gn/webgpu-cts/BUILD.gn b/third_party/gn/webgpu-cts/BUILD.gn
index 8cc8c4d..df3b229 100644
--- a/third_party/gn/webgpu-cts/BUILD.gn
+++ b/third_party/gn/webgpu-cts/BUILD.gn
@@ -22,6 +22,7 @@
   public_deps = [
     ":compile_src",
     ":copy_resources",
+    ":gen_cache",
     ":verify_gen_ts_dep_list",
   ]
   data = [ "test_list.txt" ]
@@ -104,3 +105,23 @@
     rebase_path(outputs[0], root_build_dir),
   ]
 }
+
+action("gen_cache") {
+  script = "${dawn_root}/webgpu-cts/scripts/gen_cache.py"
+
+  deps = [ ":compile_src" ]
+  _gen_cache_js =
+      "$target_gen_dir/../../webgpu-cts/src-node/common/tools/gen_cache.js"
+  inputs = get_target_outputs(":compile_src") + [ _gen_cache_js ]
+  args = [
+    rebase_path(_gen_cache_js, root_build_dir),
+    rebase_path("$target_gen_dir/../../webgpu-cts/cache", root_build_dir),
+  ]
+
+  _outputs = read_file("cache_list.txt", "list lines")
+  outputs = []
+  foreach(file, _outputs) {
+    outputs += [ "$target_gen_dir/../../webgpu-cts/cache/$file" ]
+  }
+  data = outputs
+}
diff --git a/webgpu-cts/scripts/gen_cache.py b/webgpu-cts/scripts/gen_cache.py
new file mode 100644
index 0000000..4f3f276
--- /dev/null
+++ b/webgpu-cts/scripts/gen_cache.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python3
+#
+# Copyright 2022 The Dawn Authors
+#
+# 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.
+
+import argparse
+import os
+import sys
+
+from dir_paths import node_dir
+
+
+def gen_cache(js_script, out_dir):
+    old_sys_path = sys.path
+    try:
+        sys.path = old_sys_path + [node_dir]
+        from node import RunNode
+    finally:
+        sys.path = old_sys_path
+
+    # Save the cwd. gen_cache.js needs to be run from a specific directory.
+    cwd = os.getcwd()
+    cts_dir = os.path.realpath(
+        os.path.join(cwd, os.path.dirname(js_script), '..', '..', '..'))
+    os.chdir(cts_dir)
+    RunNode([
+        os.path.join(cwd, js_script),
+        os.path.join(cwd, out_dir),
+        os.path.join('src-node', 'webgpu')
+    ])
+
+
+# Generate a cache for CTS runs.
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser()
+    parser.add_argument('js_script', help='Path to gen_cache.js')
+    parser.add_argument('out_dir', help='Output directory for the cache')
+    args = parser.parse_args()
+
+    gen_cache(args.js_script, args.out_dir)
diff --git a/webgpu-cts/test_runner.js b/webgpu-cts/test_runner.js
index ff4645c..eeb38d6 100644
--- a/webgpu-cts/test_runner.js
+++ b/webgpu-cts/test_runner.js
@@ -13,6 +13,7 @@
 // limitations under the License.
 
 import { globalTestConfig } from '../third_party/webgpu-cts/src/common/framework/test_config.js';
+import { dataCache } from '../third_party/webgpu-cts/src/common/framework/data_cache.js';
 import { DefaultTestFileLoader } from '../third_party/webgpu-cts/src/common/internal/file_loader.js';
 import { prettyPrintLog } from '../third_party/webgpu-cts/src/common/internal/logging/log_message.js';
 import { Logger } from '../third_party/webgpu-cts/src/common/internal/logging/logger.js';
@@ -98,6 +99,12 @@
   runCtsTest(input['q'], input['w']);
 }
 
+dataCache.setStore({
+  load: async (path) => {
+    return await (await fetch(`/third_party/webgpu-cts/cache/data/${path}`)).text();
+  }
+});
+
 // Make a rate-limited version `sendMessageTestHeartbeat` that executes
 // at most once every 500 ms.
 const [sendHeartbeat, {