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