aboutsummaryrefslogtreecommitdiff
path: root/gn/proto_library.gni
blob: f69d3ce74d03710c0f870c42e5f0e74ae2ecf158 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# Copyright (C) 2017 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("perfetto.gni")
import("perfetto_component.gni")

# This gni file defines rules for proto generation. There are various types of
# proto targets that can be defined in our codebase:
# "lite" targets: these use the standard libprotobuf library. They are used
#     mainly for tests and readback.
# "zero" targets: these use the protozero library and its protoc plugin. They
#     are used pretty much everywhere.
# "descriptor" targets: they are used to generate a proto-encoded reflection
#     descriptor that describes the schema of the proto using protobuf itself.
# All these targets can be generated using the perfetto_proto_library rule. It
# wraps the instantiation of several proto targets using a convenience template.
#
# For instance:
# perfetto_proto_library("xxx_@TYPE@") {
#   proto_generators = [ "lite", "zero" ]  # lite+zero+cpp is the default value.
#   sources = [ "one.proto", "two.proto" ]
#   deps = [ "dep:@TYPE@" ]
# }
#
# Is the equivalent of:
# proto_library("xxx_lite")     { sources = [...], deps = [ "dep:lite"] }
# protozero_library("xxx_zero") { sources = [...], deps = [ "dep:zero"] }

# Load the protobuf's proto_library() definition.
if (!defined(perfetto_protobuf_target_prefix)) {
  if (perfetto_root_path == "//") {
    perfetto_protobuf_target_prefix = "//buildtools"
  } else {
    perfetto_protobuf_target_prefix = "//third_party/protobuf"
  }
}
if (!defined(perfetto_protobuf_gni)) {
  if (perfetto_root_path == "//") {
    perfetto_protobuf_gni = "//gn/standalone/proto_library.gni"
  } else {
    perfetto_protobuf_gni = "//third_party/protobuf/proto_library.gni"
  }
}
if (!defined(perfetto_protobuf_src_dir)) {
  if (perfetto_root_path == "//") {
    perfetto_protobuf_src_dir = "//buildtools/protobuf/src"
  } else {
    perfetto_protobuf_src_dir = "//third_party/protobuf/src"
  }
}
import(perfetto_protobuf_gni)

# Equivalent to proto_library (generation of .h/.cc from .proto files) but
# enables also generation using the protozero plugin.
# The generated files will have the .pbzero.{cc,h} suffix, as opposed to the
# .pb.{cc,h} of the official proto library.
# DO NOT use this target directly, use perfetto_proto_library() below.
template("protozero_library") {
  proto_library(target_name) {
    perfetto_root_path = invoker.perfetto_root_path

    generate_cc = false
    generate_python = false
    generator_plugin_label =
        perfetto_root_path + "src/protozero/protoc_plugin:protozero_plugin"
    generator_plugin_suffix = ".pbzero"
    if (build_with_chromium) {
      component_build_force_source_set = true
    }

    if (defined(invoker.deps)) {
      deps = invoker.deps
    } else {
      deps = []
    }

    # omit_protozero_dep is intended to be used when protozero_library
    # is used in Chrome (for generation of code for proto extensions)
    # to avoid ODR violations in case of component builds. The embedder
    # (Chrome) is then responsible for adding the appropriate transitive
    # dependency on Protozero.
    #
    # TODO(b/173041866): use fine-grained components instead when available
    if (!(defined(invoker.omit_protozero_dep) && invoker.omit_protozero_dep)) {
      deps += [ perfetto_root_path + "src/protozero" ]
    }

    forward_variables_from(invoker,
                           [
                             "defines",
                             "generator_plugin_options",
                             "include_dirs",
                             "proto_in_dir",
                             "proto_out_dir",
                             "sources",
                             "testonly",
                             "visibility",
                             "generate_descriptor",
                             "propagate_imports_configs",
                             "import_dirs",
                           ])
  }
}

# This template generates .gen.cc/h files from .proto files. The generated
# sources are actual C++ classes that can be moved and copied around, very
# similar to the libprotobuf generated ones API-wise, but use protozero under
# the hoods, without any zero-copy benefit though.
# They are mainly used for the perfetto IPC layer and tests.
template("protozero_cpp_library") {
  proto_library(target_name) {
    perfetto_root_path = invoker.perfetto_root_path

    generate_cc = false
    generate_python = false
    generator_plugin_label =
        perfetto_root_path + "src/protozero/protoc_plugin:cppgen_plugin"
    generator_plugin_suffix = ".gen"
    if (build_with_chromium) {
      component_build_force_source_set = true
    }

    deps = [
      "$perfetto_root_path/gn:default_deps",
      "$perfetto_root_path/include/perfetto/base",
      "$perfetto_root_path/src/protozero",
    ]

    if (defined(invoker.deps)) {
      deps += invoker.deps
    }

    forward_variables_from(invoker,
                           [
                             "defines",
                             "generator_plugin_options",
                             "include_dirs",
                             "proto_in_dir",
                             "proto_out_dir",
                             "sources",
                             "testonly",
                             "visibility",
                             "generate_descriptor",
                             "propagate_imports_configs",
                             "import_dirs",
                           ])
  }
}

# Generates .ipc.{h,cc} stubs for IPC services defined in .proto files.
template("ipc_library") {
  proto_library(target_name) {
    perfetto_root_path = invoker.perfetto_root_path
    generate_cc = false
    generate_python = false
    generator_plugin_label =
        "$perfetto_root_path/src/ipc/protoc_plugin:ipc_plugin"
    generator_plugin_suffix = ".ipc"
    deps = [ "$perfetto_root_path/gn:default_deps" ]
    if (perfetto_component_type == "static_library") {
      deps += [ "$perfetto_root_path/src/ipc:perfetto_ipc" ]
    } else {
      deps += [ "$perfetto_root_path/src/ipc:common" ]
    }
    if (is_win) {
      # TODO(primiano): investigate this. In Windows standalone builds, some
      # executable targets end up in a state where no code pulls a dep on the
      # ipc:client (in turn that seems a subtle consequence of not having
      # traced_probes on Windows). This dep here is effectively needed because
      # the client-side code in the generated .ipc.cc effectively depends on the
      # client-side IPC library. Perhaps we just should do this unconditionally
      # on all platforms?
      deps += [ "$perfetto_root_path/src/ipc:client" ]
    }

    if (defined(invoker.deps)) {
      deps += invoker.deps
    }
    forward_variables_from(invoker,
                           [
                             "defines",
                             "extra_configs",
                             "include_dirs",
                             "proto_in_dir",
                             "proto_out_dir",
                             "generator_plugin_options",
                             "sources",
                             "testonly",
                             "visibility",
                             "propagate_imports_configs",
                             "import_dirs",
                           ])
  }
}

# Generates .grpc.{h,cc} stubs for services defined in .proto files.
# We require explicit opt-in as gRPC is very heavyweight so we do not
# want accidental dependencies on this.
if (enable_perfetto_grpc) {
  template("perfetto_grpc_library") {
    proto_library(target_name) {
      proto_in_dir = perfetto_root_path
      proto_out_dir = perfetto_root_path
      propagate_imports_configs = false

      perfetto_root_path = perfetto_root_path
      generate_cc = false
      generate_python = false
      generator_plugin_label =
          "$perfetto_root_path/buildtools/grpc:grpc_cpp_plugin"
      generator_plugin_suffix = ".grpc.pb"
      deps = [ "$perfetto_root_path/buildtools/grpc:grpc++" ]
      public_configs = [ "$perfetto_root_path/buildtools:grpc_gen_config" ]
      if (defined(invoker.deps)) {
        deps += invoker.deps
      }
      forward_variables_from(invoker,
                             [
                               "defines",
                               "extra_configs",
                               "include_dirs",
                               "sources",
                               "testonly",
                               "visibility",
                             ])
    }
  }
}

# The template used everywhere in the codebase.
template("perfetto_proto_library") {
  if (defined(invoker.proto_generators)) {
    proto_generators = invoker.proto_generators
  } else {
    proto_generators = [
      "zero",
      "lite",
      "cpp",
      "source_set",
    ]
  }

  # proto imports and C++ #includes are relative to this path.
  if (defined(invoker.proto_path)) {
    proto_path = invoker.proto_path
  } else {
    proto_path = perfetto_root_path
  }

  if (defined(invoker.import_dirs)) {
    import_dirs_ = invoker.import_dirs
  } else {
    import_dirs_ = []
  }

  vars_to_forward = [
    "sources",
    "visibility",
    "testonly",
    "exclude_imports",
  ]
  expansion_token = "@TYPE@"

  # gn:public_config propagates the gen dir as include directory. We
  # disable the proto_library's public_config to avoid duplicate include
  # directory command line flags (crbug.com/1043279, crbug.com/gn/142).
  propagate_imports_configs_ = false

  foreach(gen_type, proto_generators) {
    target_name_ = string_replace(target_name, expansion_token, gen_type)

    # Translate deps from xxx:@TYPE@ to xxx:lite/zero.
    deps_ = []
    if (defined(invoker.deps)) {
      foreach(dep, invoker.deps) {
        deps_ += [ string_replace(dep, expansion_token, gen_type) ]
      }
    }

    # The distinction between deps and public_deps does not matter for GN
    # but Bazel cares about the difference so we distinguish between the two.
    public_deps_ = []
    if (defined(invoker.public_deps)) {
      foreach(dep, invoker.public_deps) {
        public_deps_ = [ string_replace(dep, expansion_token, gen_type) ]
      }
    }
    deps_ += public_deps_

    if (gen_type == "zero") {
      protozero_library(target_name_) {
        proto_in_dir = proto_path
        proto_out_dir = proto_path
        generator_plugin_options = "wrapper_namespace=pbzero"
        deps = deps_
        propagate_imports_configs = propagate_imports_configs_
        import_dirs = import_dirs_
        forward_variables_from(invoker, vars_to_forward)
      }
    } else if (gen_type == "cpp") {
      protozero_cpp_library(target_name_) {
        proto_in_dir = proto_path
        proto_out_dir = proto_path
        generator_plugin_options = "wrapper_namespace=gen"
        deps = deps_
        propagate_imports_configs = propagate_imports_configs_
        import_dirs = import_dirs_
        forward_variables_from(invoker, vars_to_forward)
      }
    } else if (gen_type == "ipc") {
      cpp_target_name_ = string_replace(target_name, expansion_token, "cpp")
      ipc_library(target_name_) {
        proto_in_dir = proto_path
        proto_out_dir = proto_path
        generator_plugin_options = "wrapper_namespace=gen"
        deps = deps_ + [ ":$cpp_target_name_" ]
        propagate_imports_configs = propagate_imports_configs_
        import_dirs = import_dirs_
        forward_variables_from(invoker, vars_to_forward)
      }
    } else if (gen_type == "lite") {
      proto_library(target_name_) {
        proto_in_dir = proto_path
        proto_out_dir = proto_path
        generate_python = false
        deps = deps_
        cc_generator_options = "lite=true:"
        propagate_imports_configs = propagate_imports_configs_
        import_dirs = import_dirs_
        forward_variables_from(invoker, vars_to_forward)
      }
    } else if (gen_type == "descriptor") {
      proto_library(target_name_) {
        proto_in_dir = proto_path
        proto_out_dir = proto_path
        generate_python = false
        generate_cc = false
        generate_descriptor =
            rebase_path(invoker.generate_descriptor, proto_path)
        deps = deps_
        import_dirs = import_dirs_
        forward_variables_from(invoker, vars_to_forward)
      }

      # Not needed for descriptor proto_library target.
      not_needed([ "propagate_imports_configs_" ])
    } else if (gen_type == "source_set") {
      action(target_name_) {
        out_path = "$target_gen_dir/" + target_name_
        rebased_out_path =
            rebase_path(target_gen_dir, root_build_dir) + "/" + target_name_

        script = "$perfetto_root_path/tools/touch_file.py"
        args = [
          "--output",
          rebased_out_path,
        ]
        outputs = [ out_path ]
        deps = deps_

        metadata = {
          proto_library_sources = invoker.sources
          import_dirs = import_dirs_
          exports = get_path_info(public_deps_, "abspath")
        }
        forward_variables_from(invoker, vars_to_forward)
      }

      # Not needed for source_set proto_library target.
      not_needed([
                   "propagate_imports_configs_",
                   "proto_path",
                 ])
    } else {
      assert(false, "Invalid 'proto_generators' value.")
    }
  }
}