blob: 03d64554069d995c8257ad9d081d51cd09ce2b9b [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",
Andrew Shulaeve6c4a5c2021-01-29 11:07:39 +0000226 "exclude_imports",
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200227 ]
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100228 expansion_token = "@TYPE@"
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200229
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000230 # gn:public_config propagates the gen dir as include directory. We
231 # disable the proto_library's public_config to avoid duplicate include
232 # directory command line flags (crbug.com/1043279, crbug.com/gn/142).
233 propagate_imports_configs_ = false
234
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200235 foreach(gen_type, proto_generators) {
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100236 target_name_ = string_replace(target_name, expansion_token, gen_type)
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200237
238 # Translate deps from xxx:@TYPE@ to xxx:lite/zero.
239 deps_ = []
240 if (defined(invoker.deps)) {
241 foreach(dep, invoker.deps) {
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100242 deps_ += [ string_replace(dep, expansion_token, gen_type) ]
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200243 }
244 }
245
246 if (gen_type == "zero") {
247 protozero_library(target_name_) {
248 proto_in_dir = proto_path
249 proto_out_dir = proto_path
250 generator_plugin_options = "wrapper_namespace=pbzero"
251 deps = deps_
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000252 propagate_imports_configs = propagate_imports_configs_
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100253 import_dirs = import_dirs_
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200254 forward_variables_from(invoker, vars_to_forward)
255 }
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100256 } else if (gen_type == "cpp") {
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100257 protozero_cpp_library(target_name_) {
258 proto_in_dir = proto_path
259 proto_out_dir = proto_path
Primiano Tuccie8020f92019-11-26 13:24:01 +0000260 generator_plugin_options = "wrapper_namespace=gen"
261 deps = deps_
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000262 propagate_imports_configs = propagate_imports_configs_
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100263 import_dirs = import_dirs_
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100264 forward_variables_from(invoker, vars_to_forward)
265 }
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000266 } else if (gen_type == "ipc") {
Primiano Tuccie8020f92019-11-26 13:24:01 +0000267 cpp_target_name_ = string_replace(target_name, expansion_token, "cpp")
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000268 ipc_library(target_name_) {
269 proto_in_dir = proto_path
270 proto_out_dir = proto_path
Primiano Tuccie8020f92019-11-26 13:24:01 +0000271 generator_plugin_options = "wrapper_namespace=gen"
272 deps = deps_ + [ ":$cpp_target_name_" ]
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 Tucci3aa027d2019-11-22 21:43:43 +0000275 forward_variables_from(invoker, vars_to_forward)
276 }
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200277 } else if (gen_type == "lite") {
278 proto_library(target_name_) {
279 proto_in_dir = proto_path
280 proto_out_dir = proto_path
281 generate_python = false
282 deps = deps_
Primiano Tucci7b6a7882020-01-20 22:34:31 +0000283 cc_generator_options = "lite=true:"
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000284 propagate_imports_configs = propagate_imports_configs_
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100285 import_dirs = import_dirs_
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200286 forward_variables_from(invoker, vars_to_forward)
287 }
288 } else if (gen_type == "descriptor") {
289 proto_library(target_name_) {
290 proto_in_dir = proto_path
291 proto_out_dir = proto_path
292 generate_python = false
293 generate_cc = false
Lalit Maganti45d8eb82020-09-12 16:32:18 +0100294 generate_descriptor =
295 rebase_path(invoker.generate_descriptor, proto_path)
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200296 deps = deps_
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100297 import_dirs = import_dirs_
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200298 forward_variables_from(invoker, vars_to_forward)
299 }
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000300
301 # Not needed for descriptor proto_library target.
302 not_needed([ "propagate_imports_configs_" ])
Lalit Maganti117272f2020-09-11 14:01:18 +0100303 } else if (gen_type == "source_set") {
304 action(target_name_) {
305 out_path = "$target_gen_dir/" + target_name_
306 rebased_out_path =
307 rebase_path(target_gen_dir, root_build_dir) + "/" + target_name_
308
309 script = "$perfetto_root_path/tools/touch_file.py"
Lalit Maganti117272f2020-09-11 14:01:18 +0100310 args = [
311 "--output",
312 rebased_out_path,
313 ]
314 outputs = [ out_path ]
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100315 deps = deps_
Lalit Maganti117272f2020-09-11 14:01:18 +0100316
317 metadata = {
318 proto_library_sources = invoker.sources
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100319 import_dirs = import_dirs_
Lalit Maganti117272f2020-09-11 14:01:18 +0100320 }
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100321 forward_variables_from(invoker, vars_to_forward)
Lalit Maganti117272f2020-09-11 14:01:18 +0100322 }
Lalit Maganti3b09a3f2020-09-14 13:28:44 +0100323
324 # Not needed for source_set proto_library target.
Lalit Magantieeae1e02020-09-14 17:03:35 +0100325 not_needed([
326 "propagate_imports_configs_",
327 "proto_path",
328 ])
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200329 } else {
330 assert(false, "Invalid 'proto_generators' value.")
331 }
332 }
333}