blob: 4c5064c64f5d468e2cc96599221c43733400413f [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001# Copyright 2019 The Dawn & Tint Authors
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +00002#
Austin Engcc2516a2023-10-17 20:57:54 +00003# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are met:
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +00005#
Austin Engcc2516a2023-10-17 20:57:54 +00006# 1. Redistributions of source code must retain the above copyright notice, this
7# list of conditions and the following disclaimer.
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +00008#
Austin Engcc2516a2023-10-17 20:57:54 +00009# 2. Redistributions in binary form must reproduce the above copyright notice,
10# this list of conditions and the following disclaimer in the documentation
11# and/or other materials provided with the distribution.
12#
13# 3. Neither the name of the copyright holder nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +000027
28# Template to help invoking code generators based on generator_lib.py
29# Internal use only, this should only be called from templates implementing
30# generator-specific actions.
31#
32# Variables:
33# script: Path to generator script.
34#
35# args: List of extra command-line arguments passed to the generator.
36#
37# outputs: List of expected outputs, generation will fail if there is a
38# mistmatch.
39#
Corentin Walleza9a84df2019-09-19 23:30:42 +000040# deps: additional deps for the code generation targets.
41#
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +000042# generator_lib_dir: directory where generator_lib.py is located.
43#
44# custom_gen_dir: Optional custom target gen dir. Defaults to $target_gen_dir
45# but allows output files to not depend on the location of the BUILD.gn
46# that generates them.
47#
48# template_dir: Optional template root directory. Defaults to
49# "${generator_lib_dir}/templates".
50#
51# jinja2_path: Optional Jinja2 installation path.
52#
53# root_dir: Optional root source dir for Python dependencies
54# computation. Defaults to "${generator_lib_dir}/..". Any dependency
55# outside of this directory is considered a system file and will be
56# omitted.
57#
58template("generator_lib_action") {
59 _generator_args = []
60 if (defined(invoker.args)) {
61 _generator_args += invoker.args
62 }
63
64 assert(defined(invoker.generator_lib_dir),
65 "generator_lib_dir must be defined before calling this action!")
66
67 _template_dir = "${invoker.generator_lib_dir}/templates"
68 if (defined(invoker.template_dir)) {
69 _template_dir = invoker.template_dir
70 }
71 _generator_args += [
72 "--template-dir",
Corentin Wallezc94696a2022-11-22 11:07:27 +000073 rebase_path(_template_dir, root_build_dir),
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +000074 ]
75
76 if (defined(invoker.root_dir)) {
77 _generator_args += [
78 "--root-dir",
79 rebase_path(_root_dir, root_build_dir),
80 ]
81 }
82
83 if (defined(invoker.jinja2_path)) {
84 _generator_args += [
85 "--jinja2-path",
Corentin Wallezc94696a2022-11-22 11:07:27 +000086 rebase_path(invoker.jinja2_path, root_build_dir),
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +000087 ]
88 }
89
90 # Chooses either the default gen_dir or the custom one required by the
91 # invoker. This allows moving the definition of code generators in different
92 # BUILD.gn files without changing the location of generated file. Without
93 # this generated headers could cause issues when old headers aren't removed.
94 _gen_dir = target_gen_dir
95 if (defined(invoker.custom_gen_dir)) {
96 _gen_dir = invoker.custom_gen_dir
97 }
98
99 # For build parallelism GN wants to know the exact inputs and outputs of
100 # action targets like we use for our code generator. We avoid asking the
101 # generator about its inputs by using the "depfile" feature of GN/Ninja.
102 #
103 # A ninja limitation is that the depfile is a subset of Makefile that can
104 # contain a single target, so we output a single "JSON-tarball" instead.
105 _json_tarball = "${_gen_dir}/${target_name}.json_tarball"
106 _json_tarball_target = "${target_name}__json_tarball"
107 _json_tarball_depfile = "${_json_tarball}.d"
108
109 _generator_args += [
110 "--output-json-tarball",
111 rebase_path(_json_tarball, root_build_dir),
112 "--depfile",
113 rebase_path(_json_tarball_depfile, root_build_dir),
114 ]
115
116 # After the JSON tarball is created we need an action target to extract it
117 # with a list of its outputs. The invoker provided a list of expected
118 # outputs. To make sure the list is in sync between the generator and the
119 # build files, we write it to a file and ask the generator to assert it is
120 # correct.
121 _expected_outputs_file = "${_gen_dir}/${target_name}.expected_outputs"
122 write_file(_expected_outputs_file, invoker.outputs)
123
124 _generator_args += [
125 "--expected-outputs-file",
126 rebase_path(_expected_outputs_file, root_build_dir),
127 ]
128
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +0000129 # The code generator invocation that will write the JSON tarball, check the
130 # outputs are what's expected and write a depfile for Ninja.
131 action(_json_tarball_target) {
132 script = invoker.script
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000133 outputs = [ _json_tarball ]
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +0000134 depfile = _json_tarball_depfile
135 args = _generator_args
Corentin Walleza9a84df2019-09-19 23:30:42 +0000136 if (defined(invoker.deps)) {
137 deps = invoker.deps
138 }
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +0000139 }
140
141 # Extract the JSON tarball into the gen_dir
142 action(target_name) {
143 script = "${invoker.generator_lib_dir}/extract_json.py"
144 args = [
145 rebase_path(_json_tarball, root_build_dir),
146 rebase_path(_gen_dir, root_build_dir),
147 ]
148
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000149 deps = [ ":${_json_tarball_target}" ]
150 inputs = [ _json_tarball ]
David 'Digit' Turner5dee3e82019-06-24 14:31:06 +0000151
152 # The expected output list is relative to the gen_dir but action
153 # target outputs are from the root dir so we need to rebase them.
154 outputs = []
155 foreach(source, invoker.outputs) {
156 outputs += [ "${_gen_dir}/${source}" ]
157 }
158 }
159}