blob: 85b0e57ae3d709d411d1f18c266aea42cb5401a4 [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",
93 ])
94 }
95}
96
Primiano Tucci57dd66b2019-10-15 23:09:04 +010097# This template generates .gen.cc/h files from .proto files. The generated
98# sources are actual C++ classes that can be moved and copied around, very
99# similar to the libprotobuf generated ones API-wise, but use protozero under
100# the hoods, without any zero-copy benefit though.
101# They are mainly used for the perfetto IPC layer and tests.
102template("protozero_cpp_library") {
103 proto_library(target_name) {
104 perfetto_root_path = invoker.perfetto_root_path
105
106 generate_cc = false
107 generate_python = false
108 generator_plugin_label =
109 perfetto_root_path + "src/protozero/protoc_plugin:cppgen_plugin"
110 generator_plugin_suffix = ".gen"
111 if (build_with_chromium) {
112 component_build_force_source_set = true
113 }
114
115 deps = [
116 "$perfetto_root_path/gn:default_deps",
117 "$perfetto_root_path/include/perfetto/base",
Primiano Tuccie8020f92019-11-26 13:24:01 +0000118 "$perfetto_root_path/src/protozero",
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100119 ]
120
121 if (defined(invoker.deps)) {
122 deps += invoker.deps
123 }
124
125 forward_variables_from(invoker,
126 [
127 "defines",
128 "generator_plugin_options",
129 "include_dirs",
130 "proto_in_dir",
131 "proto_out_dir",
132 "sources",
133 "testonly",
134 "visibility",
135 "generate_descriptor",
136 ])
137 }
138}
139
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000140# Generates .ipc.{h,cc} stubs for IPC services defined in .proto files.
141template("ipc_library") {
142 proto_library(target_name) {
143 perfetto_root_path = invoker.perfetto_root_path
144 generate_cc = false
145 generate_python = false
146 generator_plugin_label =
147 "$perfetto_root_path/src/ipc/protoc_plugin:ipc_plugin"
148 generator_plugin_suffix = ".ipc"
149 deps = [
150 "$perfetto_root_path/src/ipc",
151 ]
152 if (defined(invoker.deps)) {
153 deps += invoker.deps
154 }
155 forward_variables_from(invoker,
156 [
157 "defines",
158 "extra_configs",
159 "include_dirs",
160 "proto_in_dir",
161 "proto_out_dir",
Primiano Tuccie8020f92019-11-26 13:24:01 +0000162 "generator_plugin_options",
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000163 "sources",
164 "testonly",
165 "visibility",
166 ])
167 }
168}
169
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200170# The template used everywhere in the codebase.
171template("perfetto_proto_library") {
172 if (defined(invoker.proto_generators)) {
173 proto_generators = invoker.proto_generators
174 } else {
175 proto_generators = [
176 "zero",
177 "lite",
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100178 "cpp",
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200179 ]
180 }
181
182 # proto imports and C++ #includes are relative to this path.
183 if (defined(invoker.proto_path)) {
184 proto_path = invoker.proto_path
185 } else {
Primiano Tucci355b8c82019-08-29 08:37:51 +0200186 proto_path = perfetto_root_path
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200187 }
188
189 vars_to_forward = [
190 "sources",
191 "visibility",
192 "testonly",
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200193 ]
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100194 expansion_token = "@TYPE@"
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200195
196 foreach(gen_type, proto_generators) {
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100197 target_name_ = string_replace(target_name, expansion_token, gen_type)
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200198
199 # Translate deps from xxx:@TYPE@ to xxx:lite/zero.
200 deps_ = []
201 if (defined(invoker.deps)) {
202 foreach(dep, invoker.deps) {
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100203 deps_ += [ string_replace(dep, expansion_token, gen_type) ]
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200204 }
205 }
206
207 if (gen_type == "zero") {
208 protozero_library(target_name_) {
209 proto_in_dir = proto_path
210 proto_out_dir = proto_path
211 generator_plugin_options = "wrapper_namespace=pbzero"
212 deps = deps_
213 forward_variables_from(invoker, vars_to_forward)
214 }
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100215 } else if (gen_type == "cpp") {
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100216 protozero_cpp_library(target_name_) {
217 proto_in_dir = proto_path
218 proto_out_dir = proto_path
Primiano Tuccie8020f92019-11-26 13:24:01 +0000219 generator_plugin_options = "wrapper_namespace=gen"
220 deps = deps_
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100221 forward_variables_from(invoker, vars_to_forward)
222 }
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000223 } else if (gen_type == "ipc") {
Primiano Tuccie8020f92019-11-26 13:24:01 +0000224 cpp_target_name_ = string_replace(target_name, expansion_token, "cpp")
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000225 ipc_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_ + [ ":$cpp_target_name_" ]
Primiano Tucci3aa027d2019-11-22 21:43:43 +0000230 forward_variables_from(invoker, vars_to_forward)
231 }
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200232 } else if (gen_type == "lite") {
233 proto_library(target_name_) {
234 proto_in_dir = proto_path
235 proto_out_dir = proto_path
236 generate_python = false
237 deps = deps_
Primiano Tucci7b6a7882020-01-20 22:34:31 +0000238 cc_generator_options = "lite=true:"
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200239 forward_variables_from(invoker, vars_to_forward)
240 }
241 } else if (gen_type == "descriptor") {
242 proto_library(target_name_) {
243 proto_in_dir = proto_path
244 proto_out_dir = proto_path
245 generate_python = false
246 generate_cc = false
Primiano Tucci02c11762019-08-30 00:57:59 +0200247 generate_descriptor = invoker.generate_descriptor
Primiano Tucci2cf8b532019-08-29 01:14:01 +0200248 deps = deps_
249 forward_variables_from(invoker, vars_to_forward)
250 }
251 } else {
252 assert(false, "Invalid 'proto_generators' value.")
253 }
254 }
255}