blob: 28c53010cb9a7fcf745feca4fbb3b0453b0127bb [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/",
Loko Kungf5786722022-03-31 05:09:04 +000042 "src/dawn/common/",
Ben Clayton818001d2022-02-04 17:07:46 +000043 "src/dawn/native/",
44 "src/dawn/native/opengl/",
Ben Clayton20cbe6d2022-02-04 12:51:25 +000045 "src/dawn/wire/client/",
46 "src/dawn/wire/server/",
47 "src/dawn/wire/",
Ben Clayton9fb7a512022-02-04 18:18:18 +000048 "include/dawn/",
Kai Ninomiya930e9182021-09-17 19:44:43 +000049 "emscripten-bits/",
50 "webgpu-headers/",
Corentin Wallez05623df2019-09-18 23:19:31 +000051]
52
Corentin Wallez031fbbb2019-06-11 18:03:05 +000053# Template to help invoking Dawn code generators based on generator_lib
Corentin Wallezabc753c2019-03-06 23:17:39 +000054#
55# dawn_generator("my_target_gen") {
Corentin Wallez031fbbb2019-06-11 18:03:05 +000056# # The script and generator specific arguments
57# script = [ "my_awesome_generator.py" ]
58# args = [
59# "--be-awesome",
60# "yes"
61# ]
62#
Corentin Wallezabc753c2019-03-06 23:17:39 +000063# # The list of expected outputs, generation fails if there's a mismatch
64# outputs = [
Corentin Wallez031fbbb2019-06-11 18:03:05 +000065# "MyAwesomeTarget.cpp",
66# "MyAwesomeTarget.h",
Corentin Wallezabc753c2019-03-06 23:17:39 +000067# ]
68# }
69#
70# Using the generated files is done like so:
71#
72# shared_library("my_target") {
73# deps = [ ":my_target_gen "]
74# sources = get_target_outputs(":my_target_gen")
75# }
76#
77template("dawn_generator") {
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +000078 generator_lib_action(target_name) {
79 forward_variables_from(invoker, "*")
Corentin Walleza9a84df2019-09-19 23:30:42 +000080
81 # Set arguments required to find the python libraries for the generator
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +000082 generator_lib_dir = "${dawn_root}/generator"
83 jinja2_path = dawn_jinja2_dir
Corentin Walleza9a84df2019-09-19 23:30:42 +000084
85 # Force Dawn's autogenerated file structure to mirror exactly the source
86 # tree but start at ${dawn_gen_root} instead of ${dawn_root}
Corentin Wallez05623df2019-09-18 23:19:31 +000087 allowed_output_dirs = dawn_allowed_gen_output_dirs
Corentin Walleza9a84df2019-09-19 23:30:42 +000088 custom_gen_dir = dawn_gen_root
89
90 # Make sure that we delete stale autogenerated file in directories that are
91 # no longer used by code generation to avoid include conflicts.
92 deps = [ "${dawn_root}/generator:remove_stale_autogen_files" ]
Corentin Wallezabc753c2019-03-06 23:17:39 +000093 }
94}
Corentin Wallez031fbbb2019-06-11 18:03:05 +000095
96# Helper generator for calling the generator from dawn.json
97#
98# dawn_json_generator("my_target_gen") {
99# # Which generator target to output
100# target = "my_target"
101#
102# # Also supports `outputs` and `custom_gen_dir` like dawn_generator.
103# }
104template("dawn_json_generator") {
105 dawn_generator(target_name) {
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000106 script = "${dawn_root}/generator/dawn_json_generator.py"
107
108 # The base arguments for the generator: from this dawn.json, generate this
109 # target using templates in this directory.
110 args = [
111 "--dawn-json",
112 rebase_path("${dawn_root}/dawn.json", root_build_dir),
113 "--wire-json",
114 rebase_path("${dawn_root}/dawn_wire.json", root_build_dir),
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000115 "--targets",
116 invoker.target,
117 ]
118
Corentin Wallezdf69f242019-06-13 10:22:32 +0000119 forward_variables_from(invoker, "*", [ "target" ])
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000120 }
121}