Reland: [gn] Add support for generated sources Avoids use of absolute path to build directory when invoking the generation script, since this was causing failures on the bots that check for deterministic builds. Also disable CGO in run.py when invoking Go, since this causes problems when cross-compiling. Original commit message below. Add a custom action that invokes the source generator. The build file generator produces a list of dependencies for this custom command, so that source generation will automatically re-run whenever any .tmpl file or .def file is changed. Like with Bazel, we also pre-generate a list of generated sources because it needs to know what the outputs of the command are. Bug: 513255285, 516926043 Change-Id: I98656424052681b2682bdda5eb0a868db2f9b05a Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/312375 Reviewed-by: dan sinclair <dsinclair@chromium.org> Commit-Queue: James Price <jrprice@google.com>
diff --git a/src/tint/BUILD.gn b/src/tint/BUILD.gn index 1de9d47..82847b0 100644 --- a/src/tint/BUILD.gn +++ b/src/tint/BUILD.gn
@@ -37,6 +37,7 @@ import("//testing/test.gni") } +import("generated_sources.gni") import("tint.gni") ############################################################################### @@ -217,6 +218,17 @@ public_deps = [ "${dawn_root}/src/utils:gmock_and_gtest" ] } +action("generate_sources") { + script = "${dawn_root}/tools/run.py" + inputs = tint_generation_dependencies + outputs = tint_generated_sources + args = [ + "gen", + "sources", + rebase_path(root_gen_dir, dawn_root), + ] +} + if (tint_build_benchmarks) { group("google_benchmark") { testonly = true
diff --git a/src/tint/cmd/bench/BUILD.gn b/src/tint/cmd/bench/BUILD.gn index 8814657..de5fbb1 100644 --- a/src/tint/cmd/bench/BUILD.gn +++ b/src/tint/cmd/bench/BUILD.gn
@@ -46,14 +46,15 @@ if (tint_build_wgsl_reader) { tint_benchmarks_source_set("bench") { sources = [ + "${root_gen_dir}/src/tint/cmd/bench/enums_core_bench.cc", + "${root_gen_dir}/src/tint/cmd/bench/enums_wgsl_bench.cc", "bench.cc", "bench.h", - "enums_core_bench.cc", - "enums_wgsl_bench.cc", "validator_bench.cc", ] deps = [ "${dawn_root}/src/utils", + "${tint_src_dir}:generate_sources", "${tint_src_dir}:google_benchmark", "${tint_src_dir}/api/common", "${tint_src_dir}/lang/core",
diff --git a/src/tint/generated_sources.gni b/src/tint/generated_sources.gni new file mode 100644 index 0000000..143caed --- /dev/null +++ b/src/tint/generated_sources.gni
@@ -0,0 +1,104 @@ +# Copyright 2026 The Dawn & Tint Authors +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +################################################################################ +# File generated by 'tools/src/cmd/gen' +# +# To regenerate run: './tools/run gen' +# +# Do not modify this file directly +################################################################################ + +import("tint.gni") + +tint_generated_sources = [ + "${root_gen_dir}/src/tint/cmd/bench/enums_core_bench.cc", + "${root_gen_dir}/src/tint/cmd/bench/enums_wgsl_bench.cc", + "${root_gen_dir}/src/tint/lang/core/enums.cc", + "${root_gen_dir}/src/tint/lang/core/enums.h", + "${root_gen_dir}/src/tint/lang/core/enums_test.cc", + "${root_gen_dir}/src/tint/lang/core/intrinsic/ctor_conv.cc", + "${root_gen_dir}/src/tint/lang/core/intrinsic/ctor_conv.h", + "${root_gen_dir}/src/tint/lang/core/intrinsic/data.cc", + "${root_gen_dir}/src/tint/lang/glsl/builtin_fn.cc", + "${root_gen_dir}/src/tint/lang/glsl/builtin_fn.h", + "${root_gen_dir}/src/tint/lang/glsl/intrinsic/data.cc", + "${root_gen_dir}/src/tint/lang/hlsl/builtin_fn.cc", + "${root_gen_dir}/src/tint/lang/hlsl/builtin_fn.h", + "${root_gen_dir}/src/tint/lang/hlsl/intrinsic/data.cc", + "${root_gen_dir}/src/tint/lang/msl/builtin_fn.cc", + "${root_gen_dir}/src/tint/lang/msl/builtin_fn.h", + "${root_gen_dir}/src/tint/lang/msl/intrinsic/data.cc", + "${root_gen_dir}/src/tint/lang/spirv/builtin_fn.cc", + "${root_gen_dir}/src/tint/lang/spirv/builtin_fn.h", + "${root_gen_dir}/src/tint/lang/spirv/intrinsic/data.cc", + "${root_gen_dir}/src/tint/lang/wgsl/enums.cc", + "${root_gen_dir}/src/tint/lang/wgsl/enums.h", + "${root_gen_dir}/src/tint/lang/wgsl/enums_test.cc", + "${root_gen_dir}/src/tint/lang/wgsl/intrinsic/ctor_conv.cc", + "${root_gen_dir}/src/tint/lang/wgsl/intrinsic/ctor_conv.h", + "${root_gen_dir}/src/tint/lang/wgsl/intrinsic/data.cc", +] + +tint_generation_dependencies = [ + "lang/core/access.def", + "lang/core/address_space.def", + "lang/core/core.def", + "lang/core/subgroup_matrix_kind.def", + "lang/core/texel_format.def", + "lang/glsl/glsl.def", + "lang/hlsl/hlsl.def", + "lang/msl/msl.def", + "lang/spirv/spirv.def", + "lang/wgsl/wgsl.def", + "cmd/bench/enums_core_bench.cc.tmpl", + "cmd/bench/enums_wgsl_bench.cc.tmpl", + "lang/core/enums.cc.tmpl", + "lang/core/enums.h.tmpl", + "lang/core/enums_test.cc.tmpl", + "lang/core/intrinsic/ctor_conv.cc.tmpl", + "lang/core/intrinsic/ctor_conv.h.tmpl", + "lang/core/intrinsic/data.cc.tmpl", + "lang/glsl/builtin_fn.cc.tmpl", + "lang/glsl/builtin_fn.h.tmpl", + "lang/glsl/intrinsic/data.cc.tmpl", + "lang/hlsl/builtin_fn.cc.tmpl", + "lang/hlsl/builtin_fn.h.tmpl", + "lang/hlsl/intrinsic/data.cc.tmpl", + "lang/msl/builtin_fn.cc.tmpl", + "lang/msl/builtin_fn.h.tmpl", + "lang/msl/intrinsic/data.cc.tmpl", + "lang/spirv/builtin_fn.cc.tmpl", + "lang/spirv/builtin_fn.h.tmpl", + "lang/spirv/intrinsic/data.cc.tmpl", + "lang/wgsl/enums.cc.tmpl", + "lang/wgsl/enums.h.tmpl", + "lang/wgsl/enums_test.cc.tmpl", + "lang/wgsl/intrinsic/ctor_conv.cc.tmpl", + "lang/wgsl/intrinsic/ctor_conv.h.tmpl", + "lang/wgsl/intrinsic/data.cc.tmpl", +]
diff --git a/src/tint/lang/core/BUILD.gn b/src/tint/lang/core/BUILD.gn index 7c4053f..bd01b4a 100644 --- a/src/tint/lang/core/BUILD.gn +++ b/src/tint/lang/core/BUILD.gn
@@ -45,10 +45,10 @@ libtint_source_set("core") { sources = [ + "${root_gen_dir}/src/tint/lang/core/enums.cc", + "${root_gen_dir}/src/tint/lang/core/enums.h", "binary_op.cc", "binary_op.h", - "enums.cc", - "enums.h", "evaluation_stage.h", "fluent_types.h", "interpolation.h", @@ -60,6 +60,7 @@ ] deps = [ "${dawn_root}/src/utils", + "${tint_src_dir}:generate_sources", "${tint_src_dir}/api/common", "${tint_src_dir}/utils", "${tint_src_dir}/utils/containers", @@ -75,11 +76,12 @@ if (tint_build_unittests) { tint_unittests_source_set("unittests") { sources = [ - "enums_test.cc", + "${root_gen_dir}/src/tint/lang/core/enums_test.cc", "number_test.cc", ] deps = [ "${dawn_root}/src/utils", + "${tint_src_dir}:generate_sources", "${tint_src_dir}:gmock_and_gtest", "${tint_src_dir}/lang/core", "${tint_src_dir}/utils",
diff --git a/src/tint/lang/core/intrinsic/BUILD.gn b/src/tint/lang/core/intrinsic/BUILD.gn index 7653163..3e03f3b 100644 --- a/src/tint/lang/core/intrinsic/BUILD.gn +++ b/src/tint/lang/core/intrinsic/BUILD.gn
@@ -45,9 +45,9 @@ libtint_source_set("intrinsic") { sources = [ - "ctor_conv.cc", - "ctor_conv.h", - "data.cc", + "${root_gen_dir}/src/tint/lang/core/intrinsic/ctor_conv.cc", + "${root_gen_dir}/src/tint/lang/core/intrinsic/ctor_conv.h", + "${root_gen_dir}/src/tint/lang/core/intrinsic/data.cc", "dialect.h", "table.cc", "table.h", @@ -56,6 +56,7 @@ ] deps = [ "${dawn_root}/src/utils", + "${tint_src_dir}:generate_sources", "${tint_src_dir}/api/common", "${tint_src_dir}/lang/core", "${tint_src_dir}/lang/core/constant",
diff --git a/src/tint/lang/glsl/BUILD.gn b/src/tint/lang/glsl/BUILD.gn index 72d4344..7b02600 100644 --- a/src/tint/lang/glsl/BUILD.gn +++ b/src/tint/lang/glsl/BUILD.gn
@@ -41,11 +41,12 @@ libtint_source_set("glsl") { sources = [ - "builtin_fn.cc", - "builtin_fn.h", + "${root_gen_dir}/src/tint/lang/glsl/builtin_fn.cc", + "${root_gen_dir}/src/tint/lang/glsl/builtin_fn.h", ] deps = [ "${dawn_root}/src/utils", + "${tint_src_dir}:generate_sources", "${tint_src_dir}/lang/core/ir", "${tint_src_dir}/lang/core/type", "${tint_src_dir}/utils/containers",
diff --git a/src/tint/lang/glsl/intrinsic/BUILD.gn b/src/tint/lang/glsl/intrinsic/BUILD.gn index e3b6f7a..50c1e53 100644 --- a/src/tint/lang/glsl/intrinsic/BUILD.gn +++ b/src/tint/lang/glsl/intrinsic/BUILD.gn
@@ -41,11 +41,12 @@ libtint_source_set("intrinsic") { sources = [ - "data.cc", + "${root_gen_dir}/src/tint/lang/glsl/intrinsic/data.cc", "dialect.h", ] deps = [ "${dawn_root}/src/utils", + "${tint_src_dir}:generate_sources", "${tint_src_dir}/api/common", "${tint_src_dir}/lang/core", "${tint_src_dir}/lang/core/constant",
diff --git a/src/tint/lang/hlsl/BUILD.gn b/src/tint/lang/hlsl/BUILD.gn index 72746a7..e4f0d72 100644 --- a/src/tint/lang/hlsl/BUILD.gn +++ b/src/tint/lang/hlsl/BUILD.gn
@@ -41,11 +41,12 @@ libtint_source_set("hlsl") { sources = [ - "builtin_fn.cc", - "builtin_fn.h", + "${root_gen_dir}/src/tint/lang/hlsl/builtin_fn.cc", + "${root_gen_dir}/src/tint/lang/hlsl/builtin_fn.h", ] deps = [ "${dawn_root}/src/utils", + "${tint_src_dir}:generate_sources", "${tint_src_dir}/lang/core/ir", "${tint_src_dir}/lang/core/type", "${tint_src_dir}/utils/containers",
diff --git a/src/tint/lang/hlsl/intrinsic/BUILD.gn b/src/tint/lang/hlsl/intrinsic/BUILD.gn index 8d3e6e4..2ccf89c 100644 --- a/src/tint/lang/hlsl/intrinsic/BUILD.gn +++ b/src/tint/lang/hlsl/intrinsic/BUILD.gn
@@ -41,12 +41,13 @@ libtint_source_set("intrinsic") { sources = [ - "data.cc", + "${root_gen_dir}/src/tint/lang/hlsl/intrinsic/data.cc", "dialect.h", "type_matchers.h", ] deps = [ "${dawn_root}/src/utils", + "${tint_src_dir}:generate_sources", "${tint_src_dir}/api/common", "${tint_src_dir}/lang/core", "${tint_src_dir}/lang/core/constant",
diff --git a/src/tint/lang/msl/BUILD.gn b/src/tint/lang/msl/BUILD.gn index a194482..a0c3596 100644 --- a/src/tint/lang/msl/BUILD.gn +++ b/src/tint/lang/msl/BUILD.gn
@@ -41,12 +41,13 @@ libtint_source_set("msl") { sources = [ + "${root_gen_dir}/src/tint/lang/msl/builtin_fn.cc", + "${root_gen_dir}/src/tint/lang/msl/builtin_fn.h", "barrier_type.h", - "builtin_fn.cc", - "builtin_fn.h", ] deps = [ "${dawn_root}/src/utils", + "${tint_src_dir}:generate_sources", "${tint_src_dir}/lang/core/ir", "${tint_src_dir}/lang/core/type", "${tint_src_dir}/utils/containers",
diff --git a/src/tint/lang/msl/intrinsic/BUILD.gn b/src/tint/lang/msl/intrinsic/BUILD.gn index 327c3e5..95b693e 100644 --- a/src/tint/lang/msl/intrinsic/BUILD.gn +++ b/src/tint/lang/msl/intrinsic/BUILD.gn
@@ -41,12 +41,13 @@ libtint_source_set("intrinsic") { sources = [ - "data.cc", + "${root_gen_dir}/src/tint/lang/msl/intrinsic/data.cc", "dialect.h", "type_matchers.h", ] deps = [ "${dawn_root}/src/utils", + "${tint_src_dir}:generate_sources", "${tint_src_dir}/api/common", "${tint_src_dir}/lang/core", "${tint_src_dir}/lang/core/constant",
diff --git a/src/tint/lang/spirv/BUILD.gn b/src/tint/lang/spirv/BUILD.gn index 1ca18e1..1ac3d78 100644 --- a/src/tint/lang/spirv/BUILD.gn +++ b/src/tint/lang/spirv/BUILD.gn
@@ -41,11 +41,12 @@ libtint_source_set("spirv") { sources = [ - "builtin_fn.cc", - "builtin_fn.h", + "${root_gen_dir}/src/tint/lang/spirv/builtin_fn.cc", + "${root_gen_dir}/src/tint/lang/spirv/builtin_fn.h", ] deps = [ "${dawn_root}/src/utils", + "${tint_src_dir}:generate_sources", "${tint_src_dir}/lang/core/ir", "${tint_src_dir}/lang/core/type", "${tint_src_dir}/utils/containers",
diff --git a/src/tint/lang/spirv/intrinsic/BUILD.gn b/src/tint/lang/spirv/intrinsic/BUILD.gn index c3eae02..bcb518c 100644 --- a/src/tint/lang/spirv/intrinsic/BUILD.gn +++ b/src/tint/lang/spirv/intrinsic/BUILD.gn
@@ -41,12 +41,13 @@ libtint_source_set("intrinsic") { sources = [ - "data.cc", + "${root_gen_dir}/src/tint/lang/spirv/intrinsic/data.cc", "dialect.h", "type_matchers.h", ] deps = [ "${dawn_root}/src/utils", + "${tint_src_dir}:generate_sources", "${tint_src_dir}/api/common", "${tint_src_dir}/lang/core", "${tint_src_dir}/lang/core/constant",
diff --git a/src/tint/lang/wgsl/BUILD.gn b/src/tint/lang/wgsl/BUILD.gn index 4060c97..b6e527b 100644 --- a/src/tint/lang/wgsl/BUILD.gn +++ b/src/tint/lang/wgsl/BUILD.gn
@@ -45,9 +45,9 @@ libtint_source_set("wgsl") { sources = [ + "${root_gen_dir}/src/tint/lang/wgsl/enums.cc", + "${root_gen_dir}/src/tint/lang/wgsl/enums.h", "allowed_features.h", - "enums.cc", - "enums.h", "feature_status.cc", "feature_status.h", "reserved_words.cc", @@ -55,6 +55,7 @@ ] deps = [ "${dawn_root}/src/utils", + "${tint_src_dir}:generate_sources", "${tint_src_dir}/lang/core", "${tint_src_dir}/utils", "${tint_src_dir}/utils/containers", @@ -71,12 +72,13 @@ if (tint_build_unittests) { tint_unittests_source_set("unittests") { sources = [ + "${root_gen_dir}/src/tint/lang/wgsl/enums_test.cc", "allowed_features_test.cc", - "enums_test.cc", "language_feature_status_test.cc", ] deps = [ "${dawn_root}/src/utils", + "${tint_src_dir}:generate_sources", "${tint_src_dir}:gmock_and_gtest", "${tint_src_dir}/api/common", "${tint_src_dir}/lang/core",
diff --git a/src/tint/lang/wgsl/intrinsic/BUILD.gn b/src/tint/lang/wgsl/intrinsic/BUILD.gn index 1c9ca0c..590b750 100644 --- a/src/tint/lang/wgsl/intrinsic/BUILD.gn +++ b/src/tint/lang/wgsl/intrinsic/BUILD.gn
@@ -45,14 +45,15 @@ libtint_source_set("intrinsic") { sources = [ - "ctor_conv.cc", - "ctor_conv.h", - "data.cc", + "${root_gen_dir}/src/tint/lang/wgsl/intrinsic/ctor_conv.cc", + "${root_gen_dir}/src/tint/lang/wgsl/intrinsic/ctor_conv.h", + "${root_gen_dir}/src/tint/lang/wgsl/intrinsic/data.cc", "dialect.h", "type_matchers.h", ] deps = [ "${dawn_root}/src/utils", + "${tint_src_dir}:generate_sources", "${tint_src_dir}/api/common", "${tint_src_dir}/lang/core", "${tint_src_dir}/lang/core/constant",
diff --git a/tools/run.py b/tools/run.py index 29a3e27..e80d8d3 100644 --- a/tools/run.py +++ b/tools/run.py
@@ -66,6 +66,9 @@ ] cmd.extend(unknown_args) + # Disable CGO since it causes problems when cross-compiling (see https://crbug.com/516926043). + os.environ['CGO_ENABLED'] = '0' + proc = subprocess.run(cmd, check=False, cwd=DAWN_ROOT) return proc.returncode
diff --git a/tools/src/cmd/gen/build/BUILD.gn.tmpl b/tools/src/cmd/gen/build/BUILD.gn.tmpl index 47e4cdd..c138adc 100644 --- a/tools/src/cmd/gen/build/BUILD.gn.tmpl +++ b/tools/src/cmd/gen/build/BUILD.gn.tmpl
@@ -33,7 +33,7 @@ */ -}} {{- define "TargetIfNotEmpty"}} {{- if $}} -{{- if len $.SourceFiles}}{{Eval "Target" $}}{{end}} +{{- if or (len $.SourceFiles) (len $.GeneratedSourcePaths.List)}}{{Eval "Target" $}}{{end}} {{- end}} {{- end}} @@ -83,10 +83,18 @@ {{- end}} sources = [ {{- range $File := $.UnconditionalSourceFiles}} +{{- if not ($.IsGeneratedSource $File.Path)}} "{{TrimPrefix $File.Name $.Directory.Path}}", +{{- end}} +{{- end}} +{{- range $Path := $.GeneratedSourcePaths.List}} + "${root_gen_dir}/src/tint/{{$Path}}", {{- end}} ] deps = [ +{{- if and (not $.Kind.IsProto) $.GeneratedSourcePaths.List}} + "${tint_src_dir}:generate_sources", +{{- end}} {{- /* Emit unconditional internal dependencies */}} {{- range $Dep := $.UnconditionalInternalDependencies}}
diff --git a/tools/src/cmd/gen/build/build.go b/tools/src/cmd/gen/build/build.go index 4db0b26..958aac0 100644 --- a/tools/src/cmd/gen/build/build.go +++ b/tools/src/cmd/gen/build/build.go
@@ -794,6 +794,9 @@ if err := emitGeneratedSourceListForBazel(p, fsReaderWriter, generatedSources, depsForGeneratedSources); err != nil { return err } + if err := emitGeneratedSourceListForGN(p, fsReaderWriter, generatedSources, depsForGeneratedSources); err != nil { + return err + } return nil } @@ -858,6 +861,19 @@ return writeFileIfStale(p, fsReaderWriter, bzlPath, sb.String()) } +func emitGeneratedSourceListForGN(p *Project, fsReaderWriter oswrapper.FilesystemReaderWriter, generatedSources []string, depsForGeneratedSources []string) error { + gniPath := path.Join(p.Root, "generated_sources.gni") + sb := &strings.Builder{} + sb.WriteString(common.Header("", "", "#")) + sb.WriteString("\n") + sb.WriteString("import(\"tint.gni\")\n\n") + + emitList(sb, generatedSources, "tint_generated_sources = [\n", "]\n\n", " \"${root_gen_dir}/", "\",\n") + emitList(sb, depsForGeneratedSources, "tint_generation_dependencies = [\n", "]\n", " \"", "\",\n") + + return writeFileIfStale(p, fsReaderWriter, gniPath, sb.String()) +} + func writeFileIfStale(p *Project, fsReaderWriter oswrapper.FilesystemReaderWriter, filepath string, content string) error { existing, _ := fsReaderWriter.ReadFile(filepath) if string(existing) != content {