blob: a52982bc518ab26bd043a7756acc8e9c6c4a3ba0 [file] [log] [blame]
Primiano Tucci4f9b6d72017-12-05 20:59:16 +00001# Copyright (C) 2017 The Android Open Source Project
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
Primiano Tucci4c5efa42018-10-23 13:15:13 +010015import("perfetto.gni")
Primiano Tucci916f4e52020-10-16 20:40:33 +020016import("perfetto_component.gni")
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000017
Primiano Tucci2cf8b532019-08-29 01:14:01 +020018# This gni file defines rules for proto generation. There are various types of
19# proto targets that can be defined in our codebase:
20# "lite" targets: these use the standard libprotobuf library. They are used
21# mainly for tests and readback.
22# "zero" targets: these use the protozero library and its protoc plugin. They
23# are used pretty much everywhere.
24# "descriptor" targets: they are used to generate a proto-encoded reflection
25# descriptor that describes the schema of the proto using protobuf itself.
26# All these targets can be generated using the perfetto_proto_library rule. It
27# wraps the instantiation of several proto targets using a convenience template.
28#
29# For instance:
30# perfetto_proto_library("xxx_@TYPE@") {
Andrew Shulaeva7e60fe2020-06-09 10:33:06 +010031# proto_generators = [ "lite", "zero" ] # lite+zero+cpp is the default value.
Primiano Tucci2cf8b532019-08-29 01:14:01 +020032# sources = [ "one.proto", "two.proto" ]
33# deps = [ "dep:@TYPE@" ]
34# }
35#
36# Is the equivalent of:
37# proto_library("xxx_lite") { sources = [...], deps = [ "dep:lite"] }
38# protozero_library("xxx_zero") { sources = [...], deps = [ "dep:zero"] }
39
40# Load the protobuf's proto_library() definition.
Primiano Tucci4c5efa42018-10-23 13:15:13 +010041if (!defined(perfetto_protobuf_target_prefix)) {
Primiano Tucci7e05fc12019-08-27 17:29:47 +020042 if (perfetto_root_path == "//") {
Primiano Tucci4c5efa42018-10-23 13:15:13 +010043 perfetto_protobuf_target_prefix = "//buildtools"
44 } else {
45 perfetto_protobuf_target_prefix = "//third_party/protobuf"
46 }
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000047}
Primiano Tucci4c5efa42018-10-23 13:15:13 +010048if (!defined(perfetto_protobuf_gni)) {
Primiano Tucci7e05fc12019-08-27 17:29:47 +020049 if (perfetto_root_path == "//") {
Primiano Tucci4c5efa42018-10-23 13:15:13 +010050 perfetto_protobuf_gni = "//gn/standalone/proto_library.gni"
51 } else {
52 perfetto_protobuf_gni = "//third_party/protobuf/proto_library.gni"
53 }
54}
Lalit Maganti3b09a3f2020-09-14 13:28:44 +010055if (!defined(perfetto_protobuf_src_dir)) {
56 if (perfetto_root_path == "//") {
57 perfetto_protobuf_src_dir = "//buildtools/protobuf/src"
58 } else {
59 perfetto_protobuf_src_dir = "//third_party/protobuf/src"
60 }
61}
Primiano Tucci4c5efa42018-10-23 13:15:13 +010062import(perfetto_protobuf_gni)
Primiano Tucci2cf8b532019-08-29 01:14:01 +020063
64# Equivalent to proto_library (generation of .h/.cc from .proto files) but
65# enables also generation using the protozero plugin.
66# The generated files will have the .pbzero.{cc,h} suffix, as opposed to the
67# .pb.{cc,h} of the official proto library.
68# DO NOT use this target directly, use perfetto_proto_library() below.
69template("protozero_library") {
70 proto_library(target_name) {
71 perfetto_root_path = invoker.perfetto_root_path
72
73 generate_cc = false
74 generate_python = false
75 generator_plugin_label =
76 perfetto_root_path + "src/protozero/protoc_plugin:protozero_plugin"
77 generator_plugin_suffix = ".pbzero"
78 if (build_with_chromium) {
79 component_build_force_source_set = true
80 }
81
82 if (defined(invoker.deps)) {
83 deps = invoker.deps
84 } else {
85 deps = []
86 }
87
Andrew Shulaev292a92c2020-11-12 14:32:04 +000088 # omit_protozero_dep is intended to be used when protozero_library
89 # is used in Chrome (for generation of code for proto extensions)
90 # to avoid ODR violations in case of component builds. The embedder
91 # (Chrome) is then responsible for adding the appropriate transitive
92 # dependency on Protozero.
93 #
94 # TODO(b/173041866): use fine-grained components instead when available
95 if (!(defined(invoker.omit_protozero_dep) && invoker.omit_protozero_dep)) {
96 deps += [ perfetto_root_path + "src/protozero" ]
97 }
Primiano Tucci2cf8b532019-08-29 01:14:01 +020098
99 forward_variables_from(invoker,
100 [
101 "defines",
102 "generator_plugin_options",
103 "include_dirs",
104 "proto_in_dir",
105 "proto_out_dir",
106 "sources",
107 "testonly",
108 "visibility",
109 "generate_descriptor",
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000110 "propagate_imports_configs",
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100111 "import_dirs",
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200112 ])
113 }
114}
115
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100116# This template generates .gen.cc/h files from .proto files. The generated
117# sources are actual C++ classes that can be moved and copied around, very
118# similar to the libprotobuf generated ones API-wise, but use protozero under
119# the hoods, without any zero-copy benefit though.
120# They are mainly used for the perfetto IPC layer and tests.
121template("protozero_cpp_library") {
122 proto_library(target_name) {
123 perfetto_root_path = invoker.perfetto_root_path
124
125 generate_cc = false
126 generate_python = false
127 generator_plugin_label =
128 perfetto_root_path + "src/protozero/protoc_plugin:cppgen_plugin"
129 generator_plugin_suffix = ".gen"
130 if (build_with_chromium) {
131 component_build_force_source_set = true
132 }
133
134 deps = [
135 "$perfetto_root_path/gn:default_deps",
136 "$perfetto_root_path/include/perfetto/base",
Primiano Tuccie8020f92019-11-26 13:24:01 +0000137 "$perfetto_root_path/src/protozero",
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100138 ]
139
140 if (defined(invoker.deps)) {
141 deps += invoker.deps
142 }
143
144 forward_variables_from(invoker,
145 [
146 "defines",
147 "generator_plugin_options",
148 "include_dirs",
149 "proto_in_dir",
150 "proto_out_dir",
151 "sources",
152 "testonly",
153 "visibility",
154 "generate_descriptor",
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000155 "propagate_imports_configs",
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100156 "import_dirs",
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100157 ])
158 }
159}
160
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000161# Generates .ipc.{h,cc} stubs for IPC services defined in .proto files.
162template("ipc_library") {
163 proto_library(target_name) {
164 perfetto_root_path = invoker.perfetto_root_path
165 generate_cc = false
166 generate_python = false
167 generator_plugin_label =
168 "$perfetto_root_path/src/ipc/protoc_plugin:ipc_plugin"
169 generator_plugin_suffix = ".ipc"
Primiano Tucci916f4e52020-10-16 20:40:33 +0200170 deps = [ "$perfetto_root_path/gn:default_deps" ]
171 if (perfetto_component_type == "static_library") {
172 deps += [ "$perfetto_root_path/src/ipc:perfetto_ipc" ]
173 } else {
174 deps += [ "$perfetto_root_path/src/ipc:common" ]
175 }
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000176 if (defined(invoker.deps)) {
177 deps += invoker.deps
178 }
179 forward_variables_from(invoker,
180 [
181 "defines",
182 "extra_configs",
183 "include_dirs",
184 "proto_in_dir",
185 "proto_out_dir",
Primiano Tuccie8020f92019-11-26 13:24:01 +0000186 "generator_plugin_options",
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000187 "sources",
188 "testonly",
189 "visibility",
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000190 "propagate_imports_configs",
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100191 "import_dirs",
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000192 ])
193 }
194}
195
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200196# The template used everywhere in the codebase.
197template("perfetto_proto_library") {
198 if (defined(invoker.proto_generators)) {
199 proto_generators = invoker.proto_generators
200 } else {
201 proto_generators = [
202 "zero",
203 "lite",
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100204 "cpp",
Lalit Maganti117272f2020-09-11 14:01:18 +0100205 "source_set",
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200206 ]
207 }
208
209 # proto imports and C++ #includes are relative to this path.
210 if (defined(invoker.proto_path)) {
211 proto_path = invoker.proto_path
212 } else {
Primiano Tucci355b8c82019-08-29 08:37:51 +0200213 proto_path = perfetto_root_path
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200214 }
215
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100216 if (defined(invoker.import_dirs)) {
217 import_dirs_ = invoker.import_dirs
218 } else {
219 import_dirs_ = []
220 }
221
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200222 vars_to_forward = [
223 "sources",
224 "visibility",
225 "testonly",
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200226 ]
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100227 expansion_token = "@TYPE@"
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200228
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000229 # gn:public_config propagates the gen dir as include directory. We
230 # disable the proto_library's public_config to avoid duplicate include
231 # directory command line flags (crbug.com/1043279, crbug.com/gn/142).
232 propagate_imports_configs_ = false
233
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200234 foreach(gen_type, proto_generators) {
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100235 target_name_ = string_replace(target_name, expansion_token, gen_type)
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200236
237 # Translate deps from xxx:@TYPE@ to xxx:lite/zero.
238 deps_ = []
239 if (defined(invoker.deps)) {
240 foreach(dep, invoker.deps) {
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100241 deps_ += [ string_replace(dep, expansion_token, gen_type) ]
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200242 }
243 }
244
245 if (gen_type == "zero") {
246 protozero_library(target_name_) {
247 proto_in_dir = proto_path
248 proto_out_dir = proto_path
249 generator_plugin_options = "wrapper_namespace=pbzero"
250 deps = deps_
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000251 propagate_imports_configs = propagate_imports_configs_
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100252 import_dirs = import_dirs_
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200253 forward_variables_from(invoker, vars_to_forward)
254 }
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100255 } else if (gen_type == "cpp") {
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100256 protozero_cpp_library(target_name_) {
257 proto_in_dir = proto_path
258 proto_out_dir = proto_path
Primiano Tuccie8020f92019-11-26 13:24:01 +0000259 generator_plugin_options = "wrapper_namespace=gen"
260 deps = deps_
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000261 propagate_imports_configs = propagate_imports_configs_
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100262 import_dirs = import_dirs_
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100263 forward_variables_from(invoker, vars_to_forward)
264 }
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000265 } else if (gen_type == "ipc") {
Primiano Tuccie8020f92019-11-26 13:24:01 +0000266 cpp_target_name_ = string_replace(target_name, expansion_token, "cpp")
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000267 ipc_library(target_name_) {
268 proto_in_dir = proto_path
269 proto_out_dir = proto_path
Primiano Tuccie8020f92019-11-26 13:24:01 +0000270 generator_plugin_options = "wrapper_namespace=gen"
271 deps = deps_ + [ ":$cpp_target_name_" ]
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000272 propagate_imports_configs = propagate_imports_configs_
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100273 import_dirs = import_dirs_
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000274 forward_variables_from(invoker, vars_to_forward)
275 }
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200276 } else if (gen_type == "lite") {
277 proto_library(target_name_) {
278 proto_in_dir = proto_path
279 proto_out_dir = proto_path
280 generate_python = false
281 deps = deps_
Primiano Tucci7b6a7882020-01-20 22:34:31 +0000282 cc_generator_options = "lite=true:"
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000283 propagate_imports_configs = propagate_imports_configs_
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100284 import_dirs = import_dirs_
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200285 forward_variables_from(invoker, vars_to_forward)
286 }
287 } else if (gen_type == "descriptor") {
288 proto_library(target_name_) {
289 proto_in_dir = proto_path
290 proto_out_dir = proto_path
291 generate_python = false
292 generate_cc = false
Lalit Maganti45d8eb82020-09-12 16:32:18 +0100293 generate_descriptor =
294 rebase_path(invoker.generate_descriptor, proto_path)
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200295 deps = deps_
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100296 import_dirs = import_dirs_
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200297 forward_variables_from(invoker, vars_to_forward)
298 }
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000299
300 # Not needed for descriptor proto_library target.
301 not_needed([ "propagate_imports_configs_" ])
Lalit Maganti117272f2020-09-11 14:01:18 +0100302 } else if (gen_type == "source_set") {
303 action(target_name_) {
304 out_path = "$target_gen_dir/" + target_name_
305 rebased_out_path =
306 rebase_path(target_gen_dir, root_build_dir) + "/" + target_name_
307
308 script = "$perfetto_root_path/tools/touch_file.py"
Lalit Maganti117272f2020-09-11 14:01:18 +0100309 args = [
310 "--output",
311 rebased_out_path,
312 ]
313 outputs = [ out_path ]
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100314 deps = deps_
Lalit Maganti117272f2020-09-11 14:01:18 +0100315
316 metadata = {
317 proto_library_sources = invoker.sources
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100318 import_dirs = import_dirs_
Lalit Maganti117272f2020-09-11 14:01:18 +0100319 }
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100320 forward_variables_from(invoker, vars_to_forward)
Lalit Maganti117272f2020-09-11 14:01:18 +0100321 }
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100322
323 # Not needed for source_set proto_library target.
Lalit Magantieeae1e02020-09-14 17:03:35 +0100324 not_needed([
325 "propagate_imports_configs_",
326 "proto_path",
327 ])
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200328 } else {
329 assert(false, "Invalid 'proto_generators' value.")
330 }
331 }
332}