blob: d1794d565b942a884b343a580ae8263f2c625546 [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 Tucci5f3008e2021-05-19 21:34:45 +0100176 if (is_win) {
177 # TODO(primiano): investigate this. In Windows standalone builds, some
178 # executable targets end up in a state where no code pulls a dep on the
179 # ipc:client (in turn that seems a subtle consequence of not having
180 # traced_probes on Windows). This dep here is effectively needed because
181 # the client-side code in the generated .ipc.cc effectively depends on the
182 # client-side IPC library. Perhaps we just should do this unconditionally
183 # on all platforms?
184 deps += [ "$perfetto_root_path/src/ipc:client" ]
185 }
186
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000187 if (defined(invoker.deps)) {
188 deps += invoker.deps
189 }
190 forward_variables_from(invoker,
191 [
192 "defines",
193 "extra_configs",
194 "include_dirs",
195 "proto_in_dir",
196 "proto_out_dir",
Primiano Tuccie8020f92019-11-26 13:24:01 +0000197 "generator_plugin_options",
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000198 "sources",
199 "testonly",
200 "visibility",
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000201 "propagate_imports_configs",
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100202 "import_dirs",
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000203 ])
204 }
205}
206
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200207# The template used everywhere in the codebase.
208template("perfetto_proto_library") {
209 if (defined(invoker.proto_generators)) {
210 proto_generators = invoker.proto_generators
211 } else {
212 proto_generators = [
213 "zero",
214 "lite",
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100215 "cpp",
Lalit Maganti117272f2020-09-11 14:01:18 +0100216 "source_set",
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200217 ]
218 }
219
220 # proto imports and C++ #includes are relative to this path.
221 if (defined(invoker.proto_path)) {
222 proto_path = invoker.proto_path
223 } else {
Primiano Tucci355b8c82019-08-29 08:37:51 +0200224 proto_path = perfetto_root_path
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200225 }
226
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100227 if (defined(invoker.import_dirs)) {
228 import_dirs_ = invoker.import_dirs
229 } else {
230 import_dirs_ = []
231 }
232
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200233 vars_to_forward = [
234 "sources",
235 "visibility",
236 "testonly",
Andrew Shulaeve6c4a5c2021-01-29 11:07:39 +0000237 "exclude_imports",
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200238 ]
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100239 expansion_token = "@TYPE@"
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200240
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000241 # gn:public_config propagates the gen dir as include directory. We
242 # disable the proto_library's public_config to avoid duplicate include
243 # directory command line flags (crbug.com/1043279, crbug.com/gn/142).
244 propagate_imports_configs_ = false
245
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200246 foreach(gen_type, proto_generators) {
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100247 target_name_ = string_replace(target_name, expansion_token, gen_type)
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200248
249 # Translate deps from xxx:@TYPE@ to xxx:lite/zero.
250 deps_ = []
251 if (defined(invoker.deps)) {
252 foreach(dep, invoker.deps) {
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100253 deps_ += [ string_replace(dep, expansion_token, gen_type) ]
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200254 }
255 }
256
Lalit Magantiba364ec2022-02-15 19:53:20 +0000257 # The distinction between deps and public_deps does not matter for GN
258 # but Bazel cares about the difference so we distinguish between the two.
259 public_deps_ = []
260 if (defined(invoker.public_deps)) {
261 foreach(dep, invoker.public_deps) {
262 public_deps_ = [ string_replace(dep, expansion_token, gen_type) ]
263 }
264 }
265 deps_ += public_deps_
266
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200267 if (gen_type == "zero") {
268 protozero_library(target_name_) {
269 proto_in_dir = proto_path
270 proto_out_dir = proto_path
271 generator_plugin_options = "wrapper_namespace=pbzero"
272 deps = deps_
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000273 propagate_imports_configs = propagate_imports_configs_
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100274 import_dirs = import_dirs_
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200275 forward_variables_from(invoker, vars_to_forward)
276 }
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100277 } else if (gen_type == "cpp") {
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100278 protozero_cpp_library(target_name_) {
279 proto_in_dir = proto_path
280 proto_out_dir = proto_path
Primiano Tuccie8020f92019-11-26 13:24:01 +0000281 generator_plugin_options = "wrapper_namespace=gen"
282 deps = deps_
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 Tucci57dd66b2019-10-15 23:09:04 +0100285 forward_variables_from(invoker, vars_to_forward)
286 }
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000287 } else if (gen_type == "ipc") {
Primiano Tuccie8020f92019-11-26 13:24:01 +0000288 cpp_target_name_ = string_replace(target_name, expansion_token, "cpp")
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000289 ipc_library(target_name_) {
290 proto_in_dir = proto_path
291 proto_out_dir = proto_path
Primiano Tuccie8020f92019-11-26 13:24:01 +0000292 generator_plugin_options = "wrapper_namespace=gen"
293 deps = deps_ + [ ":$cpp_target_name_" ]
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000294 propagate_imports_configs = propagate_imports_configs_
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100295 import_dirs = import_dirs_
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000296 forward_variables_from(invoker, vars_to_forward)
297 }
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200298 } else if (gen_type == "lite") {
299 proto_library(target_name_) {
300 proto_in_dir = proto_path
301 proto_out_dir = proto_path
302 generate_python = false
303 deps = deps_
Primiano Tucci7b6a7882020-01-20 22:34:31 +0000304 cc_generator_options = "lite=true:"
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000305 propagate_imports_configs = propagate_imports_configs_
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100306 import_dirs = import_dirs_
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200307 forward_variables_from(invoker, vars_to_forward)
308 }
309 } else if (gen_type == "descriptor") {
310 proto_library(target_name_) {
311 proto_in_dir = proto_path
312 proto_out_dir = proto_path
313 generate_python = false
314 generate_cc = false
Lalit Maganti45d8eb82020-09-12 16:32:18 +0100315 generate_descriptor =
316 rebase_path(invoker.generate_descriptor, proto_path)
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200317 deps = deps_
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100318 import_dirs = import_dirs_
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200319 forward_variables_from(invoker, vars_to_forward)
320 }
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000321
322 # Not needed for descriptor proto_library target.
323 not_needed([ "propagate_imports_configs_" ])
Lalit Maganti117272f2020-09-11 14:01:18 +0100324 } else if (gen_type == "source_set") {
325 action(target_name_) {
326 out_path = "$target_gen_dir/" + target_name_
327 rebased_out_path =
328 rebase_path(target_gen_dir, root_build_dir) + "/" + target_name_
329
330 script = "$perfetto_root_path/tools/touch_file.py"
Lalit Maganti117272f2020-09-11 14:01:18 +0100331 args = [
332 "--output",
333 rebased_out_path,
334 ]
335 outputs = [ out_path ]
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100336 deps = deps_
Lalit Maganti117272f2020-09-11 14:01:18 +0100337
338 metadata = {
339 proto_library_sources = invoker.sources
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100340 import_dirs = import_dirs_
Lalit Magantiba364ec2022-02-15 19:53:20 +0000341 exports = get_path_info(public_deps_, "abspath")
Lalit Maganti117272f2020-09-11 14:01:18 +0100342 }
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100343 forward_variables_from(invoker, vars_to_forward)
Lalit Maganti117272f2020-09-11 14:01:18 +0100344 }
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100345
346 # Not needed for source_set proto_library target.
Lalit Magantieeae1e02020-09-14 17:03:35 +0100347 not_needed([
348 "propagate_imports_configs_",
349 "proto_path",
350 ])
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200351 } else {
352 assert(false, "Invalid 'proto_generators' value.")
353 }
354 }
355}