xref: /aosp_15_r20/external/perfetto/gn/proto_library.gni (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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    # Convert deps to link_deps: the proto_library rule requires that C++ files
83    # are passed in as link_deps wheras in Perfetto, we always works with deps.
84    link_deps = []
85    if (defined(invoker.deps)) {
86      link_deps += invoker.deps
87    }
88
89    # omit_protozero_dep is intended to be used when protozero_library
90    # is used in Chrome (for generation of code for proto extensions)
91    # to avoid ODR violations in case of component builds. The embedder
92    # (Chrome) is then responsible for adding the appropriate transitive
93    # dependency on Protozero.
94    #
95    # TODO(b/173041866): use fine-grained components instead when available
96    if (!(defined(invoker.omit_protozero_dep) && invoker.omit_protozero_dep)) {
97      link_deps += [ perfetto_root_path + "src/protozero" ]
98    }
99
100    if (defined(invoker.link_deps)) {
101      link_deps += invoker.link_deps
102    }
103
104    forward_variables_from(invoker,
105                           [
106                             "defines",
107                             "generator_plugin_options",
108                             "include_dirs",
109                             "proto_data_sources",
110                             "proto_deps",
111                             "proto_in_dir",
112                             "proto_out_dir",
113                             "sources",
114                             "testonly",
115                             "visibility",
116                             "generate_descriptor",
117                             "propagate_imports_configs",
118                             "import_dirs",
119                           ])
120  }
121}
122
123# This template generates .gen.cc/h files from .proto files. The generated
124# sources are actual C++ classes that can be moved and copied around, very
125# similar to the libprotobuf generated ones API-wise, but use protozero under
126# the hoods, without any zero-copy benefit though.
127# They are mainly used for the perfetto IPC layer and tests.
128template("protozero_cpp_library") {
129  proto_library(target_name) {
130    perfetto_root_path = invoker.perfetto_root_path
131
132    generate_cc = false
133    generate_python = false
134    generator_plugin_label =
135        perfetto_root_path + "src/protozero/protoc_plugin:cppgen_plugin"
136    generator_plugin_suffix = ".gen"
137    if (build_with_chromium) {
138      component_build_force_source_set = true
139    }
140
141    link_deps = [
142      "$perfetto_root_path/gn:default_deps",
143      "$perfetto_root_path/include/perfetto/base",
144      "$perfetto_root_path/src/protozero",
145    ]
146
147    # Convert deps to link_deps: the proto_library rule requires that C++ files
148    # are passed in as link_deps wheras in Perfetto, we always works with deps.
149    if (defined(invoker.deps)) {
150      link_deps += invoker.deps
151    }
152    forward_variables_from(invoker,
153                           [
154                             "defines",
155                             "generator_plugin_options",
156                             "include_dirs",
157                             "proto_deps",
158                             "proto_in_dir",
159                             "proto_out_dir",
160                             "sources",
161                             "testonly",
162                             "visibility",
163                             "generate_descriptor",
164                             "propagate_imports_configs",
165                             "import_dirs",
166                           ])
167  }
168}
169
170# Generates .ipc.{h,cc} stubs for IPC services defined in .proto files.
171template("ipc_library") {
172  proto_library(target_name) {
173    perfetto_root_path = invoker.perfetto_root_path
174    generate_cc = false
175    generate_python = false
176    generator_plugin_label =
177        "$perfetto_root_path/src/ipc/protoc_plugin:ipc_plugin"
178    generator_plugin_suffix = ".ipc"
179    link_deps = [ "$perfetto_root_path/gn:default_deps" ]
180    if (perfetto_component_type == "static_library") {
181      link_deps += [ "$perfetto_root_path/src/ipc:perfetto_ipc" ]
182    } else {
183      link_deps += [ "$perfetto_root_path/src/ipc:common" ]
184    }
185    if (is_win) {
186      # TODO(primiano): investigate this. In Windows standalone builds, some
187      # executable targets end up in a state where no code pulls a dep on the
188      # ipc:client (in turn that seems a subtle consequence of not having
189      # traced_probes on Windows). This dep here is effectively needed because
190      # the client-side code in the generated .ipc.cc effectively depends on the
191      # client-side IPC library. Perhaps we just should do this unconditionally
192      # on all platforms?
193      if (perfetto_component_type == "static_library") {
194        link_deps += [ "$perfetto_root_path/src/ipc:perfetto_ipc" ]
195      } else {
196        link_deps += [ "$perfetto_root_path/src/ipc:client" ]
197      }
198    }
199
200    # Convert deps to link_deps: the proto_library rule requires that C++ files
201    # are passed in as link_deps wheras in Perfetto, we always works with deps.
202    if (defined(invoker.deps)) {
203      link_deps += invoker.deps
204    }
205    forward_variables_from(invoker,
206                           [
207                             "defines",
208                             "extra_configs",
209                             "include_dirs",
210                             "proto_deps",
211                             "proto_in_dir",
212                             "proto_out_dir",
213                             "generator_plugin_options",
214                             "sources",
215                             "testonly",
216                             "visibility",
217                             "propagate_imports_configs",
218                             "import_dirs",
219                           ])
220  }
221}
222
223# Generates .grpc.{h,cc} stubs for services defined in .proto files.
224# We require explicit opt-in as gRPC is very heavyweight so we do not
225# want accidental dependencies on this.
226if (enable_perfetto_grpc) {
227  template("perfetto_grpc_library") {
228    proto_library(target_name) {
229      proto_in_dir = perfetto_root_path
230      proto_out_dir = perfetto_root_path
231      propagate_imports_configs = false
232
233      perfetto_root_path = perfetto_root_path
234      generate_cc = false
235      generate_python = false
236      generator_plugin_label =
237          "$perfetto_root_path/buildtools/grpc:grpc_cpp_plugin"
238      generator_plugin_suffix = ".grpc.pb"
239      link_deps = [ "$perfetto_root_path/buildtools/grpc:grpc++" ]
240      public_configs = [ "$perfetto_root_path/buildtools:grpc_gen_config" ]
241
242      # Convert deps to link_deps: the proto_library rule requires that C++
243      # files are passed in as link_deps wheras in Perfetto, we always works
244      # with deps.
245      if (defined(invoker.deps)) {
246        link_deps += invoker.deps
247      }
248      forward_variables_from(invoker,
249                             [
250                               "defines",
251                               "extra_configs",
252                               "include_dirs",
253                               "sources",
254                               "testonly",
255                               "visibility",
256                             ])
257    }
258  }
259}
260
261# The template used everywhere in the codebase.
262template("perfetto_proto_library") {
263  if (defined(invoker.proto_generators)) {
264    proto_generators = invoker.proto_generators
265  } else {
266    proto_generators = [
267      "zero",
268      "lite",
269      "cpp",
270    ]
271  }
272
273  # proto imports and C++ #includes are relative to this path.
274  if (defined(invoker.proto_path)) {
275    proto_path = invoker.proto_path
276  } else {
277    proto_path = perfetto_root_path
278  }
279
280  if (defined(invoker.import_dirs)) {
281    import_dirs_ = invoker.import_dirs
282  } else {
283    import_dirs_ = []
284  }
285
286  expansion_token = "@TYPE@"
287
288  # The source set target should always be generated as it is used by the
289  # build generators and for generating descriptors.
290  source_set_target_name =
291      string_replace(target_name, expansion_token, "source_set")
292
293  # This config is necessary for Chrome proto_library build rule to work
294  # correctly.
295  source_set_input_config_name = "${source_set_target_name}_input_config"
296  config(source_set_input_config_name) {
297    inputs = invoker.sources
298  }
299
300  group(source_set_target_name) {
301    # To propagate indirect inputs dependencies to descendant tareget, we use
302    # public_deps and public_configs in this target.
303    public_deps = []
304    exports_ = []
305    if (defined(invoker.public_deps)) {
306      foreach(dep, invoker.public_deps) {
307        # Get the absolute target path
308        mapped_dep = string_replace(dep, expansion_token, "source_set")
309        public_deps += [ mapped_dep ]
310        exports_ += [ get_label_info(mapped_dep, "dir") + ":" +
311                      get_label_info(mapped_dep, "name") ]
312      }
313    }
314
315    if (defined(invoker.deps)) {
316      foreach(dep, invoker.deps) {
317        mapped_dep = string_replace(dep, expansion_token, "source_set")
318        public_deps += [ mapped_dep ]
319      }
320    }
321
322    sources = []
323    foreach(source, invoker.sources) {
324      sources += [ get_path_info(source, "abspath") ]
325    }
326
327    public_configs = [ ":${source_set_input_config_name}" ]
328
329    metadata = {
330      proto_library_sources = sources
331      proto_import_dirs = import_dirs_
332      exports = exports_
333    }
334  }
335
336  # Generate the descriptor if the option is set.
337  if (defined(invoker.generate_descriptor)) {
338    target_name_ = string_replace(target_name, expansion_token, "descriptor")
339    proto_library(target_name_) {
340      proto_in_dir = proto_path
341      proto_out_dir = proto_path
342      generate_python = false
343      generate_cc = false
344      generate_descriptor = rebase_path(invoker.generate_descriptor, proto_path)
345      sources = [ invoker.descriptor_root_source ]
346      import_dirs = import_dirs_
347      deps = [ ":${source_set_target_name}" ]
348      forward_variables_from(invoker,
349                             [
350                               "visibility",
351                               "testonly",
352                               "exclude_imports",
353                             ])
354    }
355  }
356
357  foreach(gen_type, proto_generators) {
358    target_name_ = string_replace(target_name, expansion_token, gen_type)
359
360    # Translate deps from xxx:@TYPE@ to xxx:lite/zero.
361    all_deps_ = []
362    if (defined(invoker.deps)) {
363      foreach(dep, invoker.deps) {
364        all_deps_ += [ string_replace(dep, expansion_token, gen_type) ]
365      }
366    }
367
368    # The distinction between deps and public_deps does not matter for GN
369    # but Bazel cares about the difference so we distinguish between the two.
370    if (defined(invoker.public_deps)) {
371      foreach(dep, invoker.public_deps) {
372        all_deps_ += [ string_replace(dep, expansion_token, gen_type) ]
373      }
374    }
375
376    # gn:public_config propagates the gen dir as include directory. We
377    # disable the proto_library's public_config to avoid duplicate include
378    # directory command line flags (crbug.com/1043279, crbug.com/gn/142).
379    propagate_imports_configs_ = false
380    vars_to_forward = []
381    vars_to_forward += [
382      "sources",
383      "visibility",
384      "testonly",
385    ]
386
387    if (gen_type == "zero") {
388      protozero_library(target_name_) {
389        proto_in_dir = proto_path
390        proto_out_dir = proto_path
391        generator_plugin_options = "wrapper_namespace=pbzero"
392        deps = all_deps_
393        proto_deps = [ ":$source_set_target_name" ]
394        propagate_imports_configs = propagate_imports_configs_
395        import_dirs = import_dirs_
396        forward_variables_from(invoker, vars_to_forward)
397      }
398    } else if (gen_type == "cpp") {
399      protozero_cpp_library(target_name_) {
400        proto_in_dir = proto_path
401        proto_out_dir = proto_path
402        generator_plugin_options = "wrapper_namespace=gen"
403        deps = all_deps_
404        proto_deps = [ ":$source_set_target_name" ]
405        propagate_imports_configs = propagate_imports_configs_
406        import_dirs = import_dirs_
407        forward_variables_from(invoker, vars_to_forward)
408      }
409    } else if (gen_type == "ipc") {
410      cpp_target_name_ = string_replace(target_name, expansion_token, "cpp")
411      ipc_library(target_name_) {
412        proto_in_dir = proto_path
413        proto_out_dir = proto_path
414        generator_plugin_options = "wrapper_namespace=gen"
415        proto_deps = [ ":$source_set_target_name" ]
416        deps = all_deps_ + [ ":$cpp_target_name_" ]
417        propagate_imports_configs = propagate_imports_configs_
418        import_dirs = import_dirs_
419        forward_variables_from(invoker, vars_to_forward)
420      }
421    } else if (gen_type == "lite") {
422      proto_library(target_name_) {
423        proto_in_dir = proto_path
424        proto_out_dir = proto_path
425        generate_python = false
426        link_deps = all_deps_
427        cc_generator_options = "lite=true:"
428        propagate_imports_configs = propagate_imports_configs_
429        import_dirs = import_dirs_
430        proto_deps = [ ":${source_set_target_name}" ]
431        forward_variables_from(invoker, vars_to_forward)
432      }
433    } else {
434      assert(false, "Invalid 'proto_generators' value.")
435    }
436  }
437}
438