• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 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")
16import("generator_lib.gni")
17
18# 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).
26#  2. src/include and dawn_gen_root/src/include has to match the structure of
27#    the source tree too.
28#  3. Dawn files must use include relative to src/ or src/include such as
29#    "dawn/dawn.h" or "dawn_native/backend/BackendStuff.h".
30#
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
40dawn_allowed_gen_output_dirs = [
41  "src/dawn/",
42  "src/dawn_native/",
43  "src/dawn_native/opengl/",
44  "src/dawn_wire/client/",
45  "src/dawn_wire/server/",
46  "src/dawn_wire/",
47  "src/include/dawn/",
48  "emscripten-bits/",
49  "webgpu-headers/",
50]
51
52# Template to help invoking Dawn code generators based on generator_lib
53#
54#   dawn_generator("my_target_gen") {
55#     # The script and generator specific arguments
56#     script = [ "my_awesome_generator.py" ]
57#     args = [
58#       "--be-awesome",
59#       "yes"
60#     ]
61#
62#     # The list of expected outputs, generation fails if there's a mismatch
63#     outputs = [
64#       "MyAwesomeTarget.cpp",
65#       "MyAwesomeTarget.h",
66#     ]
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") {
77  generator_lib_action(target_name) {
78    forward_variables_from(invoker, "*")
79
80    # Set arguments required to find the python libraries for the generator
81    generator_lib_dir = "${dawn_root}/generator"
82    jinja2_path = dawn_jinja2_dir
83
84    # Force Dawn's autogenerated file structure to mirror exactly the source
85    # tree but start at ${dawn_gen_root} instead of ${dawn_root}
86    allowed_output_dirs = dawn_allowed_gen_output_dirs
87    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" ]
92  }
93}
94
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) {
105    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),
114      "--targets",
115      invoker.target,
116    ]
117
118    forward_variables_from(invoker, "*", [ "target" ])
119  }
120}
121