Scaffolding for generation of intrinsics files.

This CL sets up the basis for the intrinsic file generation. All of
the GN and CMake pieces are setup, but they aren't hooked into the
main build yet. That will be a followup which just enables the
generation in order to allow easy reverting.

Change-Id: Iccac59377076ed6ac66eeaf0be965be2f49bc738
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/107981
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Reviewed-by: Ben Clayton <bclayton@google.com>
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
diff --git a/generator/tint_gen_template.py b/generator/tint_gen_template.py
new file mode 100755
index 0000000..d1df326
--- /dev/null
+++ b/generator/tint_gen_template.py
@@ -0,0 +1,53 @@
+#!/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 subprocess
+import sys
+
+
+def run_generator():
+    parser = argparse.ArgumentParser(
+        description=
+        "Tint template generator for GN build. Use tools/run gen for non-GN build.",
+        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
+    )
+    parser.add_argument('--output',
+                        default='',
+                        type=str,
+                        help='Base output directory.')
+    parser.add_argument('--template',
+                        default='',
+                        type=str,
+                        help='Template to generate.')
+    args = parser.parse_args()
+
+    root = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..')
+    go = os.path.join(root, "tools", "golang", "bin", "go")
+    if sys.platform == 'win32':
+        go += '.exe'
+
+    gen = os.path.join(root, "tools", "src", "cmd", "gen")
+    subprocess.check_call(
+        [go, "run", gen, "-o", args.output,
+         os.path.join(root, args.template)],
+        cwd=root)
+
+    return 0
+
+
+if __name__ == '__main__':
+    sys.exit(run_generator())
diff --git a/src/tint/BUILD.gn b/src/tint/BUILD.gn
index 92dfe06..54baaad 100644
--- a/src/tint/BUILD.gn
+++ b/src/tint/BUILD.gn
@@ -13,7 +13,6 @@
 # limitations under the License.
 
 import("//build_overrides/build.gni")
-
 import("../../tint_overrides_with_defaults.gni")
 
 if (tint_build_unittests) {
@@ -848,6 +847,140 @@
   public_deps = [ ":libtint_core_src" ]
 }
 
+# Template to help invoke code generator for tint template files.
+#
+# Variables:
+#   input: The prefix for the template to generate, does not include the .tmpl extension
+template("tint_intrinsic_generator") {
+  _sources = [
+    "${invoker.input}.tmpl",
+    "${tint_root_dir}/src/tint/intrinsics.def",
+  ]
+
+  _outputs = [ "${target_gen_dir}/${invoker.input}" ]
+
+  _script = "${tint_root_dir}/generator/tint_gen_template.py"
+  _args = [
+    "--output",
+    rebase_path(root_gen_dir),
+    "--template",
+    "src/tint/" + invoker.input + ".tmpl",
+  ]
+
+  action("${target_name}") {
+    script = _script
+    outputs = _outputs
+    sources = _sources
+    args = _args
+  }
+}
+
+# Template to help invoke code generator for tint template files for source files. By default this
+# will generate a `h` and `cc` build rule for the provided `input`
+#
+# Variables:
+#   input: The prefix for the template, minus the file extension and .tmpl
+#
+#   bench: set true to generate a bench template
+#
+#   test: set true to generate a test template
+template("tint_intrinsic_src_generator") {
+  tint_intrinsic_generator("${target_name}_h") {
+    input = "${invoker.input}.h"
+  }
+  tint_intrinsic_generator("${target_name}_cc") {
+    input = "${invoker.input}.cc"
+  }
+
+  if (defined(invoker.bench) && invoker.bench) {
+    tint_intrinsic_generator("${target_name}_bench") {
+      input = "${invoker.input}_bench.cc"
+    }
+  }
+  if (defined(invoker.test) && invoker.test) {
+    tint_intrinsic_generator("${target_name}_test") {
+      input = "${invoker.input}_test.cc"
+    }
+  }
+
+  source_set(target_name) {
+    deps = [
+      ":${target_name}_cc",
+      ":${target_name}_h",
+    ]
+
+    if (defined(invoker.bench) && invoker.bench) {
+      deps += [ ":${target_name}_bench" ]
+    }
+    if (defined(invoker.test) && invoker.test) {
+      deps += [ ":${target_name}_test" ]
+    }
+  }
+}
+
+tint_intrinsic_generator("tint_intrinsics_inl") {
+  input = "resolver/intrinsic_table.inl"
+}
+
+tint_intrinsic_src_generator("tint_init_conv_intrinsic") {
+  input = "resolver/init_conv_intrinsic"
+}
+
+tint_intrinsic_src_generator("tint_sem_builtin_type") {
+  input = "sem/builtin_type"
+}
+
+tint_intrinsic_src_generator("tint_sem_parameter_usage") {
+  input = "sem/parameter_usage"
+}
+
+tint_intrinsic_src_generator("tint_ast_extension") {
+  input = "ast/extension"
+  bench = true
+  test = true
+}
+
+tint_intrinsic_src_generator("tint_ast_access") {
+  input = "ast/access"
+}
+
+tint_intrinsic_src_generator("tint_ast_builtin_value") {
+  input = "ast/builtin_value"
+  bench = true
+  test = true
+}
+
+tint_intrinsic_src_generator("tint_ast_texel_format") {
+  input = "ast/texel_format"
+  bench = true
+  test = true
+}
+
+tint_intrinsic_src_generator("tint_ast_address_space") {
+  input = "ast/address_space"
+  bench = true
+  test = true
+}
+
+tint_intrinsic_src_generator("tint_ast_interpolate_attribute") {
+  input = "ast/interpolate_attribute"
+}
+
+libtint_source_set("tint_gen_deps") {
+  deps = [
+    ":tint_ast_access",
+    ":tint_ast_address_space",
+    ":tint_ast_builtin_value",
+    ":tint_ast_extension",
+    ":tint_ast_interpolate_attribute",
+    ":tint_ast_texel_format",
+    ":tint_init_conv_intrinsic",
+    ":tint_intrinsics_inl",
+    ":tint_sem_builtin_type",
+    ":tint_sem_parameter_usage",
+  ]
+}
+
 source_set("libtint") {
   public_deps = [ ":libtint_core_src" ]