blob: c438f5b948076a420d51c02f2a449260c7f2784c [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#
40# root_dir: Optional root source dir for Python dependencies
41# computation. Defaults to "${generator_lib_dir}/..". Any dependency
42# outside of this directory is considered a system file and will be
43# omitted.
44#
45template("generator_lib_action") {
46 _generator_args = []
47 if (defined(invoker.args)) {
48 _generator_args += invoker.args
49 }
50
51 assert(defined(invoker.generator_lib_dir),
52 "generator_lib_dir must be defined before calling this action!")
53
54 _template_dir = "${invoker.generator_lib_dir}/templates"
55 if (defined(invoker.template_dir)) {
56 _template_dir = invoker.template_dir
57 }
58 _generator_args += [
59 "--template-dir",
Corentin Wallezc94696a2022-11-22 11:07:27 +000060 rebase_path(_template_dir, root_build_dir),
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +000061 ]
62
63 if (defined(invoker.root_dir)) {
64 _generator_args += [
65 "--root-dir",
66 rebase_path(_root_dir, root_build_dir),
67 ]
68 }
69
70 if (defined(invoker.jinja2_path)) {
71 _generator_args += [
72 "--jinja2-path",
Corentin Wallezc94696a2022-11-22 11:07:27 +000073 rebase_path(invoker.jinja2_path, root_build_dir),
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +000074 ]
75 }
76
77 # Chooses either the default gen_dir or the custom one required by the
78 # invoker. This allows moving the definition of code generators in different
79 # BUILD.gn files without changing the location of generated file. Without
80 # this generated headers could cause issues when old headers aren't removed.
81 _gen_dir = target_gen_dir
82 if (defined(invoker.custom_gen_dir)) {
83 _gen_dir = invoker.custom_gen_dir
84 }
85
86 # For build parallelism GN wants to know the exact inputs and outputs of
87 # action targets like we use for our code generator. We avoid asking the
88 # generator about its inputs by using the "depfile" feature of GN/Ninja.
89 #
90 # A ninja limitation is that the depfile is a subset of Makefile that can
91 # contain a single target, so we output a single "JSON-tarball" instead.
92 _json_tarball = "${_gen_dir}/${target_name}.json_tarball"
93 _json_tarball_target = "${target_name}__json_tarball"
94 _json_tarball_depfile = "${_json_tarball}.d"
95
96 _generator_args += [
97 "--output-json-tarball",
98 rebase_path(_json_tarball, root_build_dir),
99 "--depfile",
100 rebase_path(_json_tarball_depfile, root_build_dir),
101 ]
102
103 # After the JSON tarball is created we need an action target to extract it
104 # with a list of its outputs. The invoker provided a list of expected
105 # outputs. To make sure the list is in sync between the generator and the
106 # build files, we write it to a file and ask the generator to assert it is
107 # correct.
108 _expected_outputs_file = "${_gen_dir}/${target_name}.expected_outputs"
109 write_file(_expected_outputs_file, invoker.outputs)
110
111 _generator_args += [
112 "--expected-outputs-file",
113 rebase_path(_expected_outputs_file, root_build_dir),
114 ]
115
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +0000116 # The code generator invocation that will write the JSON tarball, check the
117 # outputs are what's expected and write a depfile for Ninja.
118 action(_json_tarball_target) {
119 script = invoker.script
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000120 outputs = [ _json_tarball ]
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +0000121 depfile = _json_tarball_depfile
122 args = _generator_args
Corentin Walleza9a84df2019-09-19 23:30:42 +0000123 if (defined(invoker.deps)) {
124 deps = invoker.deps
125 }
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +0000126 }
127
128 # Extract the JSON tarball into the gen_dir
129 action(target_name) {
130 script = "${invoker.generator_lib_dir}/extract_json.py"
131 args = [
132 rebase_path(_json_tarball, root_build_dir),
133 rebase_path(_gen_dir, root_build_dir),
134 ]
135
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000136 deps = [ ":${_json_tarball_target}" ]
137 inputs = [ _json_tarball ]
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +0000138
139 # The expected output list is relative to the gen_dir but action
140 # target outputs are from the root dir so we need to rebase them.
141 outputs = []
142 foreach(source, invoker.outputs) {
143 outputs += [ "${_gen_dir}/${source}" ]
144 }
145 }
146}