blob: b6118bfd870f716d5413206086cde4c22bacc085 [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 Tucci4f9b6d72017-12-05 20:59:16 +000016
Primiano Tucci2cf8b532019-08-29 01:14:01 +020017# This gni file defines rules for proto generation. There are various types of
18# proto targets that can be defined in our codebase:
19# "lite" targets: these use the standard libprotobuf library. They are used
20# mainly for tests and readback.
21# "zero" targets: these use the protozero library and its protoc plugin. They
22# are used pretty much everywhere.
23# "descriptor" targets: they are used to generate a proto-encoded reflection
24# descriptor that describes the schema of the proto using protobuf itself.
25# All these targets can be generated using the perfetto_proto_library rule. It
26# wraps the instantiation of several proto targets using a convenience template.
27#
28# For instance:
29# perfetto_proto_library("xxx_@TYPE@") {
30# proto_generators = [ "lite", "zero" ] # lite+zero is the default value.
31# sources = [ "one.proto", "two.proto" ]
32# deps = [ "dep:@TYPE@" ]
33# }
34#
35# Is the equivalent of:
36# proto_library("xxx_lite") { sources = [...], deps = [ "dep:lite"] }
37# protozero_library("xxx_zero") { sources = [...], deps = [ "dep:zero"] }
38
39# Load the protobuf's proto_library() definition.
Primiano Tucci4c5efa42018-10-23 13:15:13 +010040if (!defined(perfetto_protobuf_target_prefix)) {
Primiano Tucci7e05fc12019-08-27 17:29:47 +020041 if (perfetto_root_path == "//") {
Primiano Tucci4c5efa42018-10-23 13:15:13 +010042 perfetto_protobuf_target_prefix = "//buildtools"
43 } else {
44 perfetto_protobuf_target_prefix = "//third_party/protobuf"
45 }
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000046}
Primiano Tucci4c5efa42018-10-23 13:15:13 +010047if (!defined(perfetto_protobuf_gni)) {
Primiano Tucci7e05fc12019-08-27 17:29:47 +020048 if (perfetto_root_path == "//") {
Primiano Tucci4c5efa42018-10-23 13:15:13 +010049 perfetto_protobuf_gni = "//gn/standalone/proto_library.gni"
50 } else {
51 perfetto_protobuf_gni = "//third_party/protobuf/proto_library.gni"
52 }
53}
Primiano Tucci4c5efa42018-10-23 13:15:13 +010054import(perfetto_protobuf_gni)
Primiano Tucci2cf8b532019-08-29 01:14:01 +020055
56# Equivalent to proto_library (generation of .h/.cc from .proto files) but
57# enables also generation using the protozero plugin.
58# The generated files will have the .pbzero.{cc,h} suffix, as opposed to the
59# .pb.{cc,h} of the official proto library.
60# DO NOT use this target directly, use perfetto_proto_library() below.
61template("protozero_library") {
62 proto_library(target_name) {
63 perfetto_root_path = invoker.perfetto_root_path
64
65 generate_cc = false
66 generate_python = false
67 generator_plugin_label =
68 perfetto_root_path + "src/protozero/protoc_plugin:protozero_plugin"
69 generator_plugin_suffix = ".pbzero"
70 if (build_with_chromium) {
71 component_build_force_source_set = true
72 }
73
74 if (defined(invoker.deps)) {
75 deps = invoker.deps
76 } else {
77 deps = []
78 }
79
80 deps += [ perfetto_root_path + "src/protozero" ]
81
82 forward_variables_from(invoker,
83 [
84 "defines",
85 "generator_plugin_options",
86 "include_dirs",
87 "proto_in_dir",
88 "proto_out_dir",
89 "sources",
90 "testonly",
91 "visibility",
92 "generate_descriptor",
Eric Secklerd7fd6c22020-01-22 18:46:35 +000093 "propagate_imports_configs",
Primiano Tucci2cf8b532019-08-29 01:14:01 +020094 ])
95 }
96}
97
Primiano Tucci57dd66b2019-10-15 23:09:04 +010098# This template generates .gen.cc/h files from .proto files. The generated
99# sources are actual C++ classes that can be moved and copied around, very
100# similar to the libprotobuf generated ones API-wise, but use protozero under
101# the hoods, without any zero-copy benefit though.
102# They are mainly used for the perfetto IPC layer and tests.
103template("protozero_cpp_library") {
104 proto_library(target_name) {
105 perfetto_root_path = invoker.perfetto_root_path
106
107 generate_cc = false
108 generate_python = false
109 generator_plugin_label =
110 perfetto_root_path + "src/protozero/protoc_plugin:cppgen_plugin"
111 generator_plugin_suffix = ".gen"
112 if (build_with_chromium) {
113 component_build_force_source_set = true
114 }
115
116 deps = [
117 "$perfetto_root_path/gn:default_deps",
118 "$perfetto_root_path/include/perfetto/base",
Primiano Tuccie8020f92019-11-26 13:24:01 +0000119 "$perfetto_root_path/src/protozero",
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100120 ]
121
122 if (defined(invoker.deps)) {
123 deps += invoker.deps
124 }
125
126 forward_variables_from(invoker,
127 [
128 "defines",
129 "generator_plugin_options",
130 "include_dirs",
131 "proto_in_dir",
132 "proto_out_dir",
133 "sources",
134 "testonly",
135 "visibility",
136 "generate_descriptor",
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000137 "propagate_imports_configs",
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100138 ])
139 }
140}
141
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000142# Generates .ipc.{h,cc} stubs for IPC services defined in .proto files.
143template("ipc_library") {
144 proto_library(target_name) {
145 perfetto_root_path = invoker.perfetto_root_path
146 generate_cc = false
147 generate_python = false
148 generator_plugin_label =
149 "$perfetto_root_path/src/ipc/protoc_plugin:ipc_plugin"
150 generator_plugin_suffix = ".ipc"
151 deps = [
152 "$perfetto_root_path/src/ipc",
153 ]
154 if (defined(invoker.deps)) {
155 deps += invoker.deps
156 }
157 forward_variables_from(invoker,
158 [
159 "defines",
160 "extra_configs",
161 "include_dirs",
162 "proto_in_dir",
163 "proto_out_dir",
Primiano Tuccie8020f92019-11-26 13:24:01 +0000164 "generator_plugin_options",
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000165 "sources",
166 "testonly",
167 "visibility",
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000168 "propagate_imports_configs",
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000169 ])
170 }
171}
172
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200173# The template used everywhere in the codebase.
174template("perfetto_proto_library") {
175 if (defined(invoker.proto_generators)) {
176 proto_generators = invoker.proto_generators
177 } else {
178 proto_generators = [
179 "zero",
180 "lite",
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100181 "cpp",
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200182 ]
183 }
184
185 # proto imports and C++ #includes are relative to this path.
186 if (defined(invoker.proto_path)) {
187 proto_path = invoker.proto_path
188 } else {
Primiano Tucci355b8c82019-08-29 08:37:51 +0200189 proto_path = perfetto_root_path
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200190 }
191
192 vars_to_forward = [
193 "sources",
194 "visibility",
195 "testonly",
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200196 ]
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100197 expansion_token = "@TYPE@"
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200198
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000199 # gn:public_config propagates the gen dir as include directory. We
200 # disable the proto_library's public_config to avoid duplicate include
201 # directory command line flags (crbug.com/1043279, crbug.com/gn/142).
202 propagate_imports_configs_ = false
203
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200204 foreach(gen_type, proto_generators) {
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100205 target_name_ = string_replace(target_name, expansion_token, gen_type)
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200206
207 # Translate deps from xxx:@TYPE@ to xxx:lite/zero.
208 deps_ = []
209 if (defined(invoker.deps)) {
210 foreach(dep, invoker.deps) {
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100211 deps_ += [ string_replace(dep, expansion_token, gen_type) ]
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200212 }
213 }
214
215 if (gen_type == "zero") {
216 protozero_library(target_name_) {
217 proto_in_dir = proto_path
218 proto_out_dir = proto_path
219 generator_plugin_options = "wrapper_namespace=pbzero"
220 deps = deps_
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000221 propagate_imports_configs = propagate_imports_configs_
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200222 forward_variables_from(invoker, vars_to_forward)
223 }
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100224 } else if (gen_type == "cpp") {
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100225 protozero_cpp_library(target_name_) {
226 proto_in_dir = proto_path
227 proto_out_dir = proto_path
Primiano Tuccie8020f92019-11-26 13:24:01 +0000228 generator_plugin_options = "wrapper_namespace=gen"
229 deps = deps_
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000230 propagate_imports_configs = propagate_imports_configs_
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100231 forward_variables_from(invoker, vars_to_forward)
232 }
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000233 } else if (gen_type == "ipc") {
Primiano Tuccie8020f92019-11-26 13:24:01 +0000234 cpp_target_name_ = string_replace(target_name, expansion_token, "cpp")
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000235 ipc_library(target_name_) {
236 proto_in_dir = proto_path
237 proto_out_dir = proto_path
Primiano Tuccie8020f92019-11-26 13:24:01 +0000238 generator_plugin_options = "wrapper_namespace=gen"
239 deps = deps_ + [ ":$cpp_target_name_" ]
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000240 propagate_imports_configs = propagate_imports_configs_
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000241 forward_variables_from(invoker, vars_to_forward)
242 }
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200243 } else if (gen_type == "lite") {
244 proto_library(target_name_) {
245 proto_in_dir = proto_path
246 proto_out_dir = proto_path
247 generate_python = false
248 deps = deps_
Primiano Tucci7b6a7882020-01-20 22:34:31 +0000249 cc_generator_options = "lite=true:"
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000250 propagate_imports_configs = propagate_imports_configs_
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200251 forward_variables_from(invoker, vars_to_forward)
252 }
253 } else if (gen_type == "descriptor") {
254 proto_library(target_name_) {
255 proto_in_dir = proto_path
256 proto_out_dir = proto_path
257 generate_python = false
258 generate_cc = false
Primiano Tucci02c11762019-08-30 00:57:59 +0200259 generate_descriptor = invoker.generate_descriptor
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200260 deps = deps_
261 forward_variables_from(invoker, vars_to_forward)
262 }
Eric Secklerd7fd6c22020-01-22 18:46:35 +0000263
264 # Not needed for descriptor proto_library target.
265 not_needed([ "propagate_imports_configs_" ])
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200266 } else {
267 assert(false, "Invalid 'proto_generators' value.")
268 }
269 }
270}