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/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)