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 | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 16 | |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 17 | # 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 Tucci | 4c5efa4 | 2018-10-23 13:15:13 +0100 | [diff] [blame] | 40 | if (!defined(perfetto_protobuf_target_prefix)) { |
Primiano Tucci | 7e05fc1 | 2019-08-27 17:29:47 +0200 | [diff] [blame] | 41 | if (perfetto_root_path == "//") { |
Primiano Tucci | 4c5efa4 | 2018-10-23 13:15:13 +0100 | [diff] [blame] | 42 | perfetto_protobuf_target_prefix = "//buildtools" |
| 43 | } else { |
| 44 | perfetto_protobuf_target_prefix = "//third_party/protobuf" |
| 45 | } |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 46 | } |
Primiano Tucci | 4c5efa4 | 2018-10-23 13:15:13 +0100 | [diff] [blame] | 47 | if (!defined(perfetto_protobuf_gni)) { |
Primiano Tucci | 7e05fc1 | 2019-08-27 17:29:47 +0200 | [diff] [blame] | 48 | if (perfetto_root_path == "//") { |
Primiano Tucci | 4c5efa4 | 2018-10-23 13:15:13 +0100 | [diff] [blame] | 49 | perfetto_protobuf_gni = "//gn/standalone/proto_library.gni" |
| 50 | } else { |
| 51 | perfetto_protobuf_gni = "//third_party/protobuf/proto_library.gni" |
| 52 | } |
| 53 | } |
Primiano Tucci | 4c5efa4 | 2018-10-23 13:15:13 +0100 | [diff] [blame] | 54 | import(perfetto_protobuf_gni) |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 55 | |
| 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. |
| 61 | template("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", |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame^] | 93 | "propagate_imports_configs", |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 94 | ]) |
| 95 | } |
| 96 | } |
| 97 | |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 98 | # This template generates .gen.cc/h files from .proto files. The generated |
| 99 | # sources are actual C++ classes that can be moved and copied around, very |
| 100 | # similar to the libprotobuf generated ones API-wise, but use protozero under |
| 101 | # the hoods, without any zero-copy benefit though. |
| 102 | # They are mainly used for the perfetto IPC layer and tests. |
| 103 | template("protozero_cpp_library") { |
| 104 | proto_library(target_name) { |
| 105 | perfetto_root_path = invoker.perfetto_root_path |
| 106 | |
| 107 | generate_cc = false |
| 108 | generate_python = false |
| 109 | generator_plugin_label = |
| 110 | perfetto_root_path + "src/protozero/protoc_plugin:cppgen_plugin" |
| 111 | generator_plugin_suffix = ".gen" |
| 112 | if (build_with_chromium) { |
| 113 | component_build_force_source_set = true |
| 114 | } |
| 115 | |
| 116 | deps = [ |
| 117 | "$perfetto_root_path/gn:default_deps", |
| 118 | "$perfetto_root_path/include/perfetto/base", |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 119 | "$perfetto_root_path/src/protozero", |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 120 | ] |
| 121 | |
| 122 | if (defined(invoker.deps)) { |
| 123 | deps += invoker.deps |
| 124 | } |
| 125 | |
| 126 | forward_variables_from(invoker, |
| 127 | [ |
| 128 | "defines", |
| 129 | "generator_plugin_options", |
| 130 | "include_dirs", |
| 131 | "proto_in_dir", |
| 132 | "proto_out_dir", |
| 133 | "sources", |
| 134 | "testonly", |
| 135 | "visibility", |
| 136 | "generate_descriptor", |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame^] | 137 | "propagate_imports_configs", |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 138 | ]) |
| 139 | } |
| 140 | } |
| 141 | |
Primiano Tucci | 3aa027d | 2019-11-22 21:43:43 +0000 | [diff] [blame] | 142 | # Generates .ipc.{h,cc} stubs for IPC services defined in .proto files. |
| 143 | template("ipc_library") { |
| 144 | proto_library(target_name) { |
| 145 | perfetto_root_path = invoker.perfetto_root_path |
| 146 | generate_cc = false |
| 147 | generate_python = false |
| 148 | generator_plugin_label = |
| 149 | "$perfetto_root_path/src/ipc/protoc_plugin:ipc_plugin" |
| 150 | generator_plugin_suffix = ".ipc" |
| 151 | deps = [ |
| 152 | "$perfetto_root_path/src/ipc", |
| 153 | ] |
| 154 | if (defined(invoker.deps)) { |
| 155 | deps += invoker.deps |
| 156 | } |
| 157 | forward_variables_from(invoker, |
| 158 | [ |
| 159 | "defines", |
| 160 | "extra_configs", |
| 161 | "include_dirs", |
| 162 | "proto_in_dir", |
| 163 | "proto_out_dir", |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 164 | "generator_plugin_options", |
Primiano Tucci | 3aa027d | 2019-11-22 21:43:43 +0000 | [diff] [blame] | 165 | "sources", |
| 166 | "testonly", |
| 167 | "visibility", |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame^] | 168 | "propagate_imports_configs", |
Primiano Tucci | 3aa027d | 2019-11-22 21:43:43 +0000 | [diff] [blame] | 169 | ]) |
| 170 | } |
| 171 | } |
| 172 | |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 173 | # The template used everywhere in the codebase. |
| 174 | template("perfetto_proto_library") { |
| 175 | if (defined(invoker.proto_generators)) { |
| 176 | proto_generators = invoker.proto_generators |
| 177 | } else { |
| 178 | proto_generators = [ |
| 179 | "zero", |
| 180 | "lite", |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 181 | "cpp", |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 182 | ] |
| 183 | } |
| 184 | |
| 185 | # proto imports and C++ #includes are relative to this path. |
| 186 | if (defined(invoker.proto_path)) { |
| 187 | proto_path = invoker.proto_path |
| 188 | } else { |
Primiano Tucci | 355b8c8 | 2019-08-29 08:37:51 +0200 | [diff] [blame] | 189 | proto_path = perfetto_root_path |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | vars_to_forward = [ |
| 193 | "sources", |
| 194 | "visibility", |
| 195 | "testonly", |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 196 | ] |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 197 | expansion_token = "@TYPE@" |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 198 | |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame^] | 199 | # gn:public_config propagates the gen dir as include directory. We |
| 200 | # disable the proto_library's public_config to avoid duplicate include |
| 201 | # directory command line flags (crbug.com/1043279, crbug.com/gn/142). |
| 202 | propagate_imports_configs_ = false |
| 203 | |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 204 | foreach(gen_type, proto_generators) { |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 205 | target_name_ = string_replace(target_name, expansion_token, gen_type) |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 206 | |
| 207 | # Translate deps from xxx:@TYPE@ to xxx:lite/zero. |
| 208 | deps_ = [] |
| 209 | if (defined(invoker.deps)) { |
| 210 | foreach(dep, invoker.deps) { |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 211 | deps_ += [ string_replace(dep, expansion_token, gen_type) ] |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | if (gen_type == "zero") { |
| 216 | protozero_library(target_name_) { |
| 217 | proto_in_dir = proto_path |
| 218 | proto_out_dir = proto_path |
| 219 | generator_plugin_options = "wrapper_namespace=pbzero" |
| 220 | deps = deps_ |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame^] | 221 | propagate_imports_configs = propagate_imports_configs_ |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 222 | forward_variables_from(invoker, vars_to_forward) |
| 223 | } |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 224 | } else if (gen_type == "cpp") { |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 225 | protozero_cpp_library(target_name_) { |
| 226 | proto_in_dir = proto_path |
| 227 | proto_out_dir = proto_path |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 228 | generator_plugin_options = "wrapper_namespace=gen" |
| 229 | deps = deps_ |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame^] | 230 | propagate_imports_configs = propagate_imports_configs_ |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 231 | forward_variables_from(invoker, vars_to_forward) |
| 232 | } |
Primiano Tucci | 3aa027d | 2019-11-22 21:43:43 +0000 | [diff] [blame] | 233 | } else if (gen_type == "ipc") { |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 234 | cpp_target_name_ = string_replace(target_name, expansion_token, "cpp") |
Primiano Tucci | 3aa027d | 2019-11-22 21:43:43 +0000 | [diff] [blame] | 235 | ipc_library(target_name_) { |
| 236 | proto_in_dir = proto_path |
| 237 | proto_out_dir = proto_path |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 238 | generator_plugin_options = "wrapper_namespace=gen" |
| 239 | deps = deps_ + [ ":$cpp_target_name_" ] |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame^] | 240 | propagate_imports_configs = propagate_imports_configs_ |
Primiano Tucci | 3aa027d | 2019-11-22 21:43:43 +0000 | [diff] [blame] | 241 | forward_variables_from(invoker, vars_to_forward) |
| 242 | } |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 243 | } else if (gen_type == "lite") { |
| 244 | proto_library(target_name_) { |
| 245 | proto_in_dir = proto_path |
| 246 | proto_out_dir = proto_path |
| 247 | generate_python = false |
| 248 | deps = deps_ |
Primiano Tucci | 7b6a788 | 2020-01-20 22:34:31 +0000 | [diff] [blame] | 249 | cc_generator_options = "lite=true:" |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame^] | 250 | propagate_imports_configs = propagate_imports_configs_ |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 251 | forward_variables_from(invoker, vars_to_forward) |
| 252 | } |
| 253 | } else if (gen_type == "descriptor") { |
| 254 | proto_library(target_name_) { |
| 255 | proto_in_dir = proto_path |
| 256 | proto_out_dir = proto_path |
| 257 | generate_python = false |
| 258 | generate_cc = false |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 259 | generate_descriptor = invoker.generate_descriptor |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 260 | deps = deps_ |
| 261 | forward_variables_from(invoker, vars_to_forward) |
| 262 | } |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame^] | 263 | |
| 264 | # Not needed for descriptor proto_library target. |
| 265 | not_needed([ "propagate_imports_configs_" ]) |
Primiano Tucci | 2cf8b53 | 2019-08-29 01:14:01 +0200 | [diff] [blame] | 266 | } else { |
| 267 | assert(false, "Invalid 'proto_generators' value.") |
| 268 | } |
| 269 | } |
| 270 | } |