Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 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 | |
Primiano Tucci | 4c5efa4 | 2018-10-23 13:15:13 +0100 | [diff] [blame] | 15 | import("perfetto.gni") |
Primiano Tucci | 916f4e5 | 2020-10-16 20:40:33 +0200 | [diff] [blame] | 16 | import("perfetto_component.gni") |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 17 | |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 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@") { |
Andrew Shulaev | a7e60fe | 2020-06-09 10:33:06 +0100 | [diff] [blame] | 31 | # proto_generators = [ "lite", "zero" ] # lite+zero+cpp is the default value. |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 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. |
Primiano Tucci | 4c5efa4 | 2018-10-23 13:15:13 +0100 | [diff] [blame] | 41 | if (!defined(perfetto_protobuf_target_prefix)) { |
Primiano Tucci | 7e05fc1 | 2019-08-27 17:29:47 +0200 | [diff] [blame] | 42 | if (perfetto_root_path == "//") { |
Primiano Tucci | 4c5efa4 | 2018-10-23 13:15:13 +0100 | [diff] [blame] | 43 | perfetto_protobuf_target_prefix = "//buildtools" |
| 44 | } else { |
| 45 | perfetto_protobuf_target_prefix = "//third_party/protobuf" |
| 46 | } |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 47 | } |
Primiano Tucci | 4c5efa4 | 2018-10-23 13:15:13 +0100 | [diff] [blame] | 48 | if (!defined(perfetto_protobuf_gni)) { |
Primiano Tucci | 7e05fc1 | 2019-08-27 17:29:47 +0200 | [diff] [blame] | 49 | if (perfetto_root_path == "//") { |
Primiano Tucci | 4c5efa4 | 2018-10-23 13:15:13 +0100 | [diff] [blame] | 50 | perfetto_protobuf_gni = "//gn/standalone/proto_library.gni" |
| 51 | } else { |
| 52 | perfetto_protobuf_gni = "//third_party/protobuf/proto_library.gni" |
| 53 | } |
| 54 | } |
Lalit Maganti | 3b09a3f | 2020-09-14 13:28:44 +0100 | [diff] [blame] | 55 | if (!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 | } |
Primiano Tucci | 4c5efa4 | 2018-10-23 13:15:13 +0100 | [diff] [blame] | 62 | import(perfetto_protobuf_gni) |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 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. |
| 69 | template("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 | |
Andrew Shulaev | 292a92c | 2020-11-12 14:32:04 +0000 | [diff] [blame] | 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 | } |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 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", |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame] | 110 | "propagate_imports_configs", |
Lalit Maganti | 3b09a3f | 2020-09-14 13:28:44 +0100 | [diff] [blame] | 111 | "import_dirs", |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 112 | ]) |
| 113 | } |
| 114 | } |
| 115 | |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 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. |
| 121 | template("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", |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 137 | "$perfetto_root_path/src/protozero", |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 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", |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame] | 155 | "propagate_imports_configs", |
Lalit Maganti | 3b09a3f | 2020-09-14 13:28:44 +0100 | [diff] [blame] | 156 | "import_dirs", |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 157 | ]) |
| 158 | } |
| 159 | } |
| 160 | |
Primiano Tucci | 3aa027d | 2019-11-22 21:43:43 +0000 | [diff] [blame] | 161 | # Generates .ipc.{h,cc} stubs for IPC services defined in .proto files. |
| 162 | template("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" |
Primiano Tucci | 916f4e5 | 2020-10-16 20:40:33 +0200 | [diff] [blame] | 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 | } |
Primiano Tucci | 3aa027d | 2019-11-22 21:43:43 +0000 | [diff] [blame] | 176 | if (defined(invoker.deps)) { |
| 177 | deps += invoker.deps |
| 178 | } |
| 179 | forward_variables_from(invoker, |
| 180 | [ |
| 181 | "defines", |
| 182 | "extra_configs", |
| 183 | "include_dirs", |
| 184 | "proto_in_dir", |
| 185 | "proto_out_dir", |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 186 | "generator_plugin_options", |
Primiano Tucci | 3aa027d | 2019-11-22 21:43:43 +0000 | [diff] [blame] | 187 | "sources", |
| 188 | "testonly", |
| 189 | "visibility", |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame] | 190 | "propagate_imports_configs", |
Lalit Maganti | 3b09a3f | 2020-09-14 13:28:44 +0100 | [diff] [blame] | 191 | "import_dirs", |
Primiano Tucci | 3aa027d | 2019-11-22 21:43:43 +0000 | [diff] [blame] | 192 | ]) |
| 193 | } |
| 194 | } |
| 195 | |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 196 | # The template used everywhere in the codebase. |
| 197 | template("perfetto_proto_library") { |
| 198 | if (defined(invoker.proto_generators)) { |
| 199 | proto_generators = invoker.proto_generators |
| 200 | } else { |
| 201 | proto_generators = [ |
| 202 | "zero", |
| 203 | "lite", |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 204 | "cpp", |
Lalit Maganti | 117272f | 2020-09-11 14:01:18 +0100 | [diff] [blame] | 205 | "source_set", |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 206 | ] |
| 207 | } |
| 208 | |
| 209 | # proto imports and C++ #includes are relative to this path. |
| 210 | if (defined(invoker.proto_path)) { |
| 211 | proto_path = invoker.proto_path |
| 212 | } else { |
Primiano Tucci | 355b8c8 | 2019-08-29 08:37:51 +0200 | [diff] [blame] | 213 | proto_path = perfetto_root_path |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Lalit Maganti | 3b09a3f | 2020-09-14 13:28:44 +0100 | [diff] [blame] | 216 | if (defined(invoker.import_dirs)) { |
| 217 | import_dirs_ = invoker.import_dirs |
| 218 | } else { |
| 219 | import_dirs_ = [] |
| 220 | } |
| 221 | |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 222 | vars_to_forward = [ |
| 223 | "sources", |
| 224 | "visibility", |
| 225 | "testonly", |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 226 | ] |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 227 | expansion_token = "@TYPE@" |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 228 | |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame] | 229 | # gn:public_config propagates the gen dir as include directory. We |
| 230 | # disable the proto_library's public_config to avoid duplicate include |
| 231 | # directory command line flags (crbug.com/1043279, crbug.com/gn/142). |
| 232 | propagate_imports_configs_ = false |
| 233 | |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 234 | foreach(gen_type, proto_generators) { |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 235 | target_name_ = string_replace(target_name, expansion_token, gen_type) |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 236 | |
| 237 | # Translate deps from xxx:@TYPE@ to xxx:lite/zero. |
| 238 | deps_ = [] |
| 239 | if (defined(invoker.deps)) { |
| 240 | foreach(dep, invoker.deps) { |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 241 | deps_ += [ string_replace(dep, expansion_token, gen_type) ] |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
| 245 | if (gen_type == "zero") { |
| 246 | protozero_library(target_name_) { |
| 247 | proto_in_dir = proto_path |
| 248 | proto_out_dir = proto_path |
| 249 | generator_plugin_options = "wrapper_namespace=pbzero" |
| 250 | deps = deps_ |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame] | 251 | propagate_imports_configs = propagate_imports_configs_ |
Lalit Maganti | 3b09a3f | 2020-09-14 13:28:44 +0100 | [diff] [blame] | 252 | import_dirs = import_dirs_ |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 253 | forward_variables_from(invoker, vars_to_forward) |
| 254 | } |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 255 | } else if (gen_type == "cpp") { |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 256 | protozero_cpp_library(target_name_) { |
| 257 | proto_in_dir = proto_path |
| 258 | proto_out_dir = proto_path |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 259 | generator_plugin_options = "wrapper_namespace=gen" |
| 260 | deps = deps_ |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame] | 261 | propagate_imports_configs = propagate_imports_configs_ |
Lalit Maganti | 3b09a3f | 2020-09-14 13:28:44 +0100 | [diff] [blame] | 262 | import_dirs = import_dirs_ |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 263 | forward_variables_from(invoker, vars_to_forward) |
| 264 | } |
Primiano Tucci | 3aa027d | 2019-11-22 21:43:43 +0000 | [diff] [blame] | 265 | } else if (gen_type == "ipc") { |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 266 | cpp_target_name_ = string_replace(target_name, expansion_token, "cpp") |
Primiano Tucci | 3aa027d | 2019-11-22 21:43:43 +0000 | [diff] [blame] | 267 | ipc_library(target_name_) { |
| 268 | proto_in_dir = proto_path |
| 269 | proto_out_dir = proto_path |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 270 | generator_plugin_options = "wrapper_namespace=gen" |
| 271 | deps = deps_ + [ ":$cpp_target_name_" ] |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame] | 272 | propagate_imports_configs = propagate_imports_configs_ |
Lalit Maganti | 3b09a3f | 2020-09-14 13:28:44 +0100 | [diff] [blame] | 273 | import_dirs = import_dirs_ |
Primiano Tucci | 3aa027d | 2019-11-22 21:43:43 +0000 | [diff] [blame] | 274 | forward_variables_from(invoker, vars_to_forward) |
| 275 | } |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 276 | } else if (gen_type == "lite") { |
| 277 | proto_library(target_name_) { |
| 278 | proto_in_dir = proto_path |
| 279 | proto_out_dir = proto_path |
| 280 | generate_python = false |
| 281 | deps = deps_ |
Primiano Tucci | 7b6a788 | 2020-01-20 22:34:31 +0000 | [diff] [blame] | 282 | cc_generator_options = "lite=true:" |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame] | 283 | propagate_imports_configs = propagate_imports_configs_ |
Lalit Maganti | 3b09a3f | 2020-09-14 13:28:44 +0100 | [diff] [blame] | 284 | import_dirs = import_dirs_ |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 285 | forward_variables_from(invoker, vars_to_forward) |
| 286 | } |
| 287 | } else if (gen_type == "descriptor") { |
| 288 | proto_library(target_name_) { |
| 289 | proto_in_dir = proto_path |
| 290 | proto_out_dir = proto_path |
| 291 | generate_python = false |
| 292 | generate_cc = false |
Lalit Maganti | 45d8eb8 | 2020-09-12 16:32:18 +0100 | [diff] [blame] | 293 | generate_descriptor = |
| 294 | rebase_path(invoker.generate_descriptor, proto_path) |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 295 | deps = deps_ |
Lalit Maganti | 3b09a3f | 2020-09-14 13:28:44 +0100 | [diff] [blame] | 296 | import_dirs = import_dirs_ |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 297 | forward_variables_from(invoker, vars_to_forward) |
| 298 | } |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame] | 299 | |
| 300 | # Not needed for descriptor proto_library target. |
| 301 | not_needed([ "propagate_imports_configs_" ]) |
Lalit Maganti | 117272f | 2020-09-11 14:01:18 +0100 | [diff] [blame] | 302 | } else if (gen_type == "source_set") { |
| 303 | action(target_name_) { |
| 304 | out_path = "$target_gen_dir/" + target_name_ |
| 305 | rebased_out_path = |
| 306 | rebase_path(target_gen_dir, root_build_dir) + "/" + target_name_ |
| 307 | |
| 308 | script = "$perfetto_root_path/tools/touch_file.py" |
Lalit Maganti | 117272f | 2020-09-11 14:01:18 +0100 | [diff] [blame] | 309 | args = [ |
| 310 | "--output", |
| 311 | rebased_out_path, |
| 312 | ] |
| 313 | outputs = [ out_path ] |
Lalit Maganti | 3b09a3f | 2020-09-14 13:28:44 +0100 | [diff] [blame] | 314 | deps = deps_ |
Lalit Maganti | 117272f | 2020-09-11 14:01:18 +0100 | [diff] [blame] | 315 | |
| 316 | metadata = { |
| 317 | proto_library_sources = invoker.sources |
Lalit Maganti | 3b09a3f | 2020-09-14 13:28:44 +0100 | [diff] [blame] | 318 | import_dirs = import_dirs_ |
Lalit Maganti | 117272f | 2020-09-11 14:01:18 +0100 | [diff] [blame] | 319 | } |
Lalit Maganti | 3b09a3f | 2020-09-14 13:28:44 +0100 | [diff] [blame] | 320 | forward_variables_from(invoker, vars_to_forward) |
Lalit Maganti | 117272f | 2020-09-11 14:01:18 +0100 | [diff] [blame] | 321 | } |
Lalit Maganti | 3b09a3f | 2020-09-14 13:28:44 +0100 | [diff] [blame] | 322 | |
| 323 | # Not needed for source_set proto_library target. |
Lalit Maganti | eeae1e0 | 2020-09-14 17:03:35 +0100 | [diff] [blame] | 324 | not_needed([ |
| 325 | "propagate_imports_configs_", |
| 326 | "proto_path", |
| 327 | ]) |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 328 | } else { |
| 329 | assert(false, "Invalid 'proto_generators' value.") |
| 330 | } |
| 331 | } |
| 332 | } |