blob: fb7e639c587ebdba084bcc7c47047704d8122559 [file] [log] [blame]
Corentin Wallezabc753c2019-03-06 23:17:39 +00001# Copyright 2019 The Dawn Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import("../scripts/dawn_overrides_with_defaults.gni")
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +000016import("generator_lib.gni")
Corentin Wallezabc753c2019-03-06 23:17:39 +000017
Corentin Walleza9a84df2019-09-19 23:30:42 +000018# Dawn used to put autogenerated files in a lot of different places. When we
19# started to move them around, some compilation issues arised because some
20# stale include files stayed in the build directory and were picked up.
21# To counter this, now Dawn does the following:
22#
23# 1. The generated output file directory structure has to match the structure
24# of the source tree, starting at dawn_gen_root (gen/ or
25# gen/third_party/dawn depending on where we are).
Ben Clayton9fb7a512022-02-04 18:18:18 +000026# 2. include and dawn_gen_root/include has to match the structure of
Corentin Walleza9a84df2019-09-19 23:30:42 +000027# the source tree too.
Ben Clayton9fb7a512022-02-04 18:18:18 +000028# 3. Dawn files must use include relative to src/ or include such as
Ben Clayton818001d2022-02-04 17:07:46 +000029# "dawn/dawn.h" or "dawn/native/backend/BackendStuff.h".
Corentin Walleza9a84df2019-09-19 23:30:42 +000030#
31# The allowed list below ensure 1). Include directory rules for Dawn ensure 3)
32# and 2) is something we need to enforce in code review.
33#
34# However GN's toolchains automatically add some include directories for us
35# which breaks 3) slightly. To avoid stale headers in for example
36# dawn_gen_root/src/dawn/dawn/ to be picked up (instead of
37# dawn_gen_root/src/dawn), we have a special action that removes files in
38# disallowed gen directories.
39
Corentin Wallez05623df2019-09-18 23:19:31 +000040dawn_allowed_gen_output_dirs = [
41 "src/dawn/",
Ben Clayton818001d2022-02-04 17:07:46 +000042 "src/dawn/native/",
43 "src/dawn/native/opengl/",
Ben Clayton20cbe6d2022-02-04 12:51:25 +000044 "src/dawn/wire/client/",
45 "src/dawn/wire/server/",
46 "src/dawn/wire/",
Ben Clayton9fb7a512022-02-04 18:18:18 +000047 "include/dawn/",
Kai Ninomiya930e9182021-09-17 19:44:43 +000048 "emscripten-bits/",
49 "webgpu-headers/",
Corentin Wallez05623df2019-09-18 23:19:31 +000050]
51
Corentin Wallez031fbbb2019-06-11 18:03:05 +000052# Template to help invoking Dawn code generators based on generator_lib
Corentin Wallezabc753c2019-03-06 23:17:39 +000053#
54# dawn_generator("my_target_gen") {
Corentin Wallez031fbbb2019-06-11 18:03:05 +000055# # The script and generator specific arguments
56# script = [ "my_awesome_generator.py" ]
57# args = [
58# "--be-awesome",
59# "yes"
60# ]
61#
Corentin Wallezabc753c2019-03-06 23:17:39 +000062# # The list of expected outputs, generation fails if there's a mismatch
63# outputs = [
Corentin Wallez031fbbb2019-06-11 18:03:05 +000064# "MyAwesomeTarget.cpp",
65# "MyAwesomeTarget.h",
Corentin Wallezabc753c2019-03-06 23:17:39 +000066# ]
67# }
68#
69# Using the generated files is done like so:
70#
71# shared_library("my_target") {
72# deps = [ ":my_target_gen "]
73# sources = get_target_outputs(":my_target_gen")
74# }
75#
76template("dawn_generator") {
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +000077 generator_lib_action(target_name) {
78 forward_variables_from(invoker, "*")
Corentin Walleza9a84df2019-09-19 23:30:42 +000079
80 # Set arguments required to find the python libraries for the generator
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +000081 generator_lib_dir = "${dawn_root}/generator"
82 jinja2_path = dawn_jinja2_dir
Corentin Walleza9a84df2019-09-19 23:30:42 +000083
84 # Force Dawn's autogenerated file structure to mirror exactly the source
85 # tree but start at ${dawn_gen_root} instead of ${dawn_root}
Corentin Wallez05623df2019-09-18 23:19:31 +000086 allowed_output_dirs = dawn_allowed_gen_output_dirs
Corentin Walleza9a84df2019-09-19 23:30:42 +000087 custom_gen_dir = dawn_gen_root
88
89 # Make sure that we delete stale autogenerated file in directories that are
90 # no longer used by code generation to avoid include conflicts.
91 deps = [ "${dawn_root}/generator:remove_stale_autogen_files" ]
Corentin Wallezabc753c2019-03-06 23:17:39 +000092 }
93}
Corentin Wallez031fbbb2019-06-11 18:03:05 +000094
95# Helper generator for calling the generator from dawn.json
96#
97# dawn_json_generator("my_target_gen") {
98# # Which generator target to output
99# target = "my_target"
100#
101# # Also supports `outputs` and `custom_gen_dir` like dawn_generator.
102# }
103template("dawn_json_generator") {
104 dawn_generator(target_name) {
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000105 script = "${dawn_root}/generator/dawn_json_generator.py"
106
107 # The base arguments for the generator: from this dawn.json, generate this
108 # target using templates in this directory.
109 args = [
110 "--dawn-json",
111 rebase_path("${dawn_root}/dawn.json", root_build_dir),
112 "--wire-json",
113 rebase_path("${dawn_root}/dawn_wire.json", root_build_dir),
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000114 "--targets",
115 invoker.target,
116 ]
117
Corentin Wallezdf69f242019-06-13 10:22:32 +0000118 forward_variables_from(invoker, "*", [ "target" ])
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000119 }
120}