1# 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 15import("perfetto.gni") 16import("perfetto_component.gni") 17 18# 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@") { 31# proto_generators = [ "lite", "zero" ] # lite+zero+cpp is the default value. 32# 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. 41if (!defined(perfetto_protobuf_target_prefix)) { 42 if (perfetto_root_path == "//") { 43 perfetto_protobuf_target_prefix = "//buildtools" 44 } else { 45 perfetto_protobuf_target_prefix = "//third_party/protobuf" 46 } 47} 48if (!defined(perfetto_protobuf_gni)) { 49 if (perfetto_root_path == "//") { 50 perfetto_protobuf_gni = "//gn/standalone/proto_library.gni" 51 } else { 52 perfetto_protobuf_gni = "//third_party/protobuf/proto_library.gni" 53 } 54} 55if (!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} 62import(perfetto_protobuf_gni) 63 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 88 # 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 } 98 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", 110 "propagate_imports_configs", 111 "import_dirs", 112 ]) 113 } 114} 115 116# 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", 137 "$perfetto_root_path/src/protozero", 138 ] 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", 155 "propagate_imports_configs", 156 "import_dirs", 157 ]) 158 } 159} 160 161# 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" 170 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 } 176 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 187 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", 197 "generator_plugin_options", 198 "sources", 199 "testonly", 200 "visibility", 201 "propagate_imports_configs", 202 "import_dirs", 203 ]) 204 } 205} 206 207# Generates .grpc.{h,cc} stubs for services defined in .proto files. 208# We require explicit opt-in as gRPC is very heavyweight so we do not 209# want accidental dependencies on this. 210if (enable_perfetto_grpc) { 211 template("perfetto_grpc_library") { 212 proto_library(target_name) { 213 proto_in_dir = perfetto_root_path 214 proto_out_dir = perfetto_root_path 215 propagate_imports_configs = false 216 217 perfetto_root_path = perfetto_root_path 218 generate_cc = false 219 generate_python = false 220 generator_plugin_label = 221 "$perfetto_root_path/buildtools/grpc:grpc_cpp_plugin" 222 generator_plugin_suffix = ".grpc.pb" 223 deps = [ "$perfetto_root_path/buildtools/grpc:grpc++" ] 224 public_configs = [ "$perfetto_root_path/buildtools:grpc_gen_config" ] 225 if (defined(invoker.deps)) { 226 deps += invoker.deps 227 } 228 forward_variables_from(invoker, 229 [ 230 "defines", 231 "extra_configs", 232 "include_dirs", 233 "sources", 234 "testonly", 235 "visibility", 236 ]) 237 } 238 } 239} 240 241# The template used everywhere in the codebase. 242template("perfetto_proto_library") { 243 if (defined(invoker.proto_generators)) { 244 proto_generators = invoker.proto_generators 245 } else { 246 proto_generators = [ 247 "zero", 248 "lite", 249 "cpp", 250 "source_set", 251 ] 252 } 253 254 # proto imports and C++ #includes are relative to this path. 255 if (defined(invoker.proto_path)) { 256 proto_path = invoker.proto_path 257 } else { 258 proto_path = perfetto_root_path 259 } 260 261 if (defined(invoker.import_dirs)) { 262 import_dirs_ = invoker.import_dirs 263 } else { 264 import_dirs_ = [] 265 } 266 267 vars_to_forward = [ 268 "sources", 269 "visibility", 270 "testonly", 271 "exclude_imports", 272 ] 273 expansion_token = "@TYPE@" 274 275 # gn:public_config propagates the gen dir as include directory. We 276 # disable the proto_library's public_config to avoid duplicate include 277 # directory command line flags (crbug.com/1043279, crbug.com/gn/142). 278 propagate_imports_configs_ = false 279 280 foreach(gen_type, proto_generators) { 281 target_name_ = string_replace(target_name, expansion_token, gen_type) 282 283 # Translate deps from xxx:@TYPE@ to xxx:lite/zero. 284 deps_ = [] 285 if (defined(invoker.deps)) { 286 foreach(dep, invoker.deps) { 287 deps_ += [ string_replace(dep, expansion_token, gen_type) ] 288 } 289 } 290 291 # The distinction between deps and public_deps does not matter for GN 292 # but Bazel cares about the difference so we distinguish between the two. 293 public_deps_ = [] 294 if (defined(invoker.public_deps)) { 295 foreach(dep, invoker.public_deps) { 296 public_deps_ = [ string_replace(dep, expansion_token, gen_type) ] 297 } 298 } 299 deps_ += public_deps_ 300 301 if (gen_type == "zero") { 302 protozero_library(target_name_) { 303 proto_in_dir = proto_path 304 proto_out_dir = proto_path 305 generator_plugin_options = "wrapper_namespace=pbzero" 306 deps = deps_ 307 propagate_imports_configs = propagate_imports_configs_ 308 import_dirs = import_dirs_ 309 forward_variables_from(invoker, vars_to_forward) 310 } 311 } else if (gen_type == "cpp") { 312 protozero_cpp_library(target_name_) { 313 proto_in_dir = proto_path 314 proto_out_dir = proto_path 315 generator_plugin_options = "wrapper_namespace=gen" 316 deps = deps_ 317 propagate_imports_configs = propagate_imports_configs_ 318 import_dirs = import_dirs_ 319 forward_variables_from(invoker, vars_to_forward) 320 } 321 } else if (gen_type == "ipc") { 322 cpp_target_name_ = string_replace(target_name, expansion_token, "cpp") 323 ipc_library(target_name_) { 324 proto_in_dir = proto_path 325 proto_out_dir = proto_path 326 generator_plugin_options = "wrapper_namespace=gen" 327 deps = deps_ + [ ":$cpp_target_name_" ] 328 propagate_imports_configs = propagate_imports_configs_ 329 import_dirs = import_dirs_ 330 forward_variables_from(invoker, vars_to_forward) 331 } 332 } else if (gen_type == "lite") { 333 proto_library(target_name_) { 334 proto_in_dir = proto_path 335 proto_out_dir = proto_path 336 generate_python = false 337 deps = deps_ 338 cc_generator_options = "lite=true:" 339 propagate_imports_configs = propagate_imports_configs_ 340 import_dirs = import_dirs_ 341 forward_variables_from(invoker, vars_to_forward) 342 } 343 } else if (gen_type == "descriptor") { 344 proto_library(target_name_) { 345 proto_in_dir = proto_path 346 proto_out_dir = proto_path 347 generate_python = false 348 generate_cc = false 349 generate_descriptor = 350 rebase_path(invoker.generate_descriptor, proto_path) 351 deps = deps_ 352 import_dirs = import_dirs_ 353 forward_variables_from(invoker, vars_to_forward) 354 } 355 356 # Not needed for descriptor proto_library target. 357 not_needed([ "propagate_imports_configs_" ]) 358 } else if (gen_type == "source_set") { 359 action(target_name_) { 360 out_path = "$target_gen_dir/" + target_name_ 361 rebased_out_path = 362 rebase_path(target_gen_dir, root_build_dir) + "/" + target_name_ 363 364 script = "$perfetto_root_path/tools/touch_file.py" 365 args = [ 366 "--output", 367 rebased_out_path, 368 ] 369 outputs = [ out_path ] 370 deps = deps_ 371 372 metadata = { 373 proto_library_sources = invoker.sources 374 proto_import_dirs = import_dirs_ 375 exports = [] 376 foreach(i, public_deps_) { 377 # Get the absolute target path 378 exports += 379 [ get_label_info(i, "dir") + ":" + get_label_info(i, "name") ] 380 } 381 } 382 forward_variables_from(invoker, vars_to_forward) 383 } 384 385 # Not needed for source_set proto_library target. 386 not_needed([ 387 "propagate_imports_configs_", 388 "proto_path", 389 ]) 390 } else { 391 assert(false, "Invalid 'proto_generators' value.") 392 } 393 } 394} 395