blob: 8b9e04cc955d36f18e7bac9a55c7f01db675e634 [file] [log] [blame]
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +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
15# Template to help invoking code generators based on generator_lib.py
16# Internal use only, this should only be called from templates implementing
17# generator-specific actions.
18#
19# Variables:
20# script: Path to generator script.
21#
22# args: List of extra command-line arguments passed to the generator.
23#
24# outputs: List of expected outputs, generation will fail if there is a
25# mistmatch.
26#
Corentin Walleza9a84df2019-09-19 23:30:42 +000027# deps: additional deps for the code generation targets.
28#
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +000029# generator_lib_dir: directory where generator_lib.py is located.
30#
31# custom_gen_dir: Optional custom target gen dir. Defaults to $target_gen_dir
32# but allows output files to not depend on the location of the BUILD.gn
33# that generates them.
34#
35# template_dir: Optional template root directory. Defaults to
36# "${generator_lib_dir}/templates".
37#
38# jinja2_path: Optional Jinja2 installation path.
39#
Corentin Wallez05623df2019-09-18 23:19:31 +000040# allowed_output_dirs: Optional list of directories that are the only
41# directories in which files of `outputs` are allowed to be (and not
42# in children directories). Generation will fail if an output isn't
43# in a directory in the list.
44#
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +000045# root_dir: Optional root source dir for Python dependencies
46# computation. Defaults to "${generator_lib_dir}/..". Any dependency
47# outside of this directory is considered a system file and will be
48# omitted.
49#
50template("generator_lib_action") {
51 _generator_args = []
52 if (defined(invoker.args)) {
53 _generator_args += invoker.args
54 }
55
56 assert(defined(invoker.generator_lib_dir),
57 "generator_lib_dir must be defined before calling this action!")
58
59 _template_dir = "${invoker.generator_lib_dir}/templates"
60 if (defined(invoker.template_dir)) {
61 _template_dir = invoker.template_dir
62 }
63 _generator_args += [
64 "--template-dir",
65 rebase_path(_template_dir),
66 ]
67
68 if (defined(invoker.root_dir)) {
69 _generator_args += [
70 "--root-dir",
71 rebase_path(_root_dir, root_build_dir),
72 ]
73 }
74
75 if (defined(invoker.jinja2_path)) {
76 _generator_args += [
77 "--jinja2-path",
78 rebase_path(invoker.jinja2_path),
79 ]
80 }
81
82 # Chooses either the default gen_dir or the custom one required by the
83 # invoker. This allows moving the definition of code generators in different
84 # BUILD.gn files without changing the location of generated file. Without
85 # this generated headers could cause issues when old headers aren't removed.
86 _gen_dir = target_gen_dir
87 if (defined(invoker.custom_gen_dir)) {
88 _gen_dir = invoker.custom_gen_dir
89 }
90
91 # For build parallelism GN wants to know the exact inputs and outputs of
92 # action targets like we use for our code generator. We avoid asking the
93 # generator about its inputs by using the "depfile" feature of GN/Ninja.
94 #
95 # A ninja limitation is that the depfile is a subset of Makefile that can
96 # contain a single target, so we output a single "JSON-tarball" instead.
97 _json_tarball = "${_gen_dir}/${target_name}.json_tarball"
98 _json_tarball_target = "${target_name}__json_tarball"
99 _json_tarball_depfile = "${_json_tarball}.d"
100
101 _generator_args += [
102 "--output-json-tarball",
103 rebase_path(_json_tarball, root_build_dir),
104 "--depfile",
105 rebase_path(_json_tarball_depfile, root_build_dir),
106 ]
107
108 # After the JSON tarball is created we need an action target to extract it
109 # with a list of its outputs. The invoker provided a list of expected
110 # outputs. To make sure the list is in sync between the generator and the
111 # build files, we write it to a file and ask the generator to assert it is
112 # correct.
113 _expected_outputs_file = "${_gen_dir}/${target_name}.expected_outputs"
114 write_file(_expected_outputs_file, invoker.outputs)
115
116 _generator_args += [
117 "--expected-outputs-file",
118 rebase_path(_expected_outputs_file, root_build_dir),
119 ]
120
Corentin Wallez05623df2019-09-18 23:19:31 +0000121 # Check that all of the outputs are in a directory that's allowed. This is
122 # useful to keep the list of directories in sink with other parts of the
123 # build.
124 if (defined(invoker.allowed_output_dirs)) {
125 _allowed_output_dirs_file = "${_gen_dir}/${target_name}.allowed_output_dirs"
126 write_file(_allowed_output_dirs_file, invoker.allowed_output_dirs)
127
128 _generator_args += [
129 "--allowed-output-dirs-file",
130 rebase_path(_allowed_output_dirs_file, root_build_dir),
131 ]
132 }
133
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +0000134 # The code generator invocation that will write the JSON tarball, check the
135 # outputs are what's expected and write a depfile for Ninja.
136 action(_json_tarball_target) {
137 script = invoker.script
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000138 outputs = [ _json_tarball ]
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +0000139 depfile = _json_tarball_depfile
140 args = _generator_args
Corentin Walleza9a84df2019-09-19 23:30:42 +0000141 if (defined(invoker.deps)) {
142 deps = invoker.deps
143 }
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +0000144 }
145
146 # Extract the JSON tarball into the gen_dir
147 action(target_name) {
148 script = "${invoker.generator_lib_dir}/extract_json.py"
149 args = [
150 rebase_path(_json_tarball, root_build_dir),
151 rebase_path(_gen_dir, root_build_dir),
152 ]
153
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000154 deps = [ ":${_json_tarball_target}" ]
155 inputs = [ _json_tarball ]
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +0000156
157 # The expected output list is relative to the gen_dir but action
158 # target outputs are from the root dir so we need to rebase them.
159 outputs = []
160 foreach(source, invoker.outputs) {
161 outputs += [ "${_gen_dir}/${source}" ]
162 }
163 }
164}