Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [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 | |
Joel Fernandes | 80fc3cd | 2020-04-01 18:02:57 -0400 | [diff] [blame] | 15 | import("../perfetto.gni") |
| 16 | |
Primiano Tucci | 42433ab | 2020-11-30 18:42:01 +0100 | [diff] [blame] | 17 | if (host_os == "win") { |
| 18 | _host_executable_suffix = ".exe" |
| 19 | } else { |
| 20 | _host_executable_suffix = "" |
| 21 | } |
| 22 | |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 23 | template("proto_library") { |
| 24 | assert(defined(invoker.sources)) |
| 25 | proto_sources = invoker.sources |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 26 | |
Primiano Tucci | 2ee254a | 2017-11-15 00:38:48 +0000 | [diff] [blame] | 27 | # All the proto imports should be relative to the project root. |
| 28 | proto_in_dir = "//" |
| 29 | if (defined(invoker.proto_in_dir)) { |
| 30 | proto_in_dir = invoker.proto_in_dir |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 31 | } |
Primiano Tucci | 782374f | 2017-11-27 16:25:20 +0000 | [diff] [blame] | 32 | assert(defined(invoker.proto_out_dir), |
| 33 | "proto_out_dir must be explicitly defined") |
| 34 | proto_out_dir = invoker.proto_out_dir |
Oystein Eftevaag | 51e06e5 | 2018-01-18 11:28:49 -0800 | [diff] [blame] | 35 | |
| 36 | # We don't support generate_python in the standalone build, but still must |
| 37 | # check that the caller sets this to false. This is because when building in |
| 38 | # the chromium tree, chromium's proto_library.gni in chrome (!= this) defaults |
| 39 | # generate_python = true. |
| 40 | assert(defined(invoker.generate_python) && !invoker.generate_python) |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 41 | |
Lalit Maganti | 3b09a3f | 2020-09-14 13:28:44 +0100 | [diff] [blame] | 42 | import_dirs = [] |
| 43 | if (defined(invoker.import_dirs)) { |
| 44 | import_dirs = invoker.import_dirs |
| 45 | } |
| 46 | |
Hector Dearman | 37e0553 | 2017-11-14 15:35:22 +0000 | [diff] [blame] | 47 | # If false will not generate the default .pb.{cc,h} files. Used for custom |
| 48 | # codegen plugins. |
| 49 | generate_cc = true |
| 50 | if (defined(invoker.generate_cc)) { |
| 51 | generate_cc = invoker.generate_cc |
| 52 | } |
| 53 | |
Florian Mayer | ab55e3d | 2018-04-19 11:56:01 +0100 | [diff] [blame] | 54 | generate_descriptor = "" |
| 55 | if (defined(invoker.generate_descriptor)) { |
| 56 | generate_descriptor = invoker.generate_descriptor |
Andrew Shulaev | e6c4a5c | 2021-01-29 11:07:39 +0000 | [diff] [blame] | 57 | |
| 58 | # Include imports to descriptor by default, but use exclude_imports to omit |
| 59 | # them if needed. |
| 60 | if (defined(invoker.exclude_imports)) { |
| 61 | exclude_imports = invoker.exclude_imports |
| 62 | } else { |
| 63 | exclude_imports = false |
| 64 | } |
Florian Mayer | ab55e3d | 2018-04-19 11:56:01 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 67 | if (defined(invoker.generator_plugin_label)) { |
| 68 | plugin_host_label = invoker.generator_plugin_label + "($host_toolchain)" |
Primiano Tucci | 42433ab | 2020-11-30 18:42:01 +0100 | [diff] [blame] | 69 | plugin_path = |
| 70 | get_label_info(plugin_host_label, "root_out_dir") + "/" + |
| 71 | get_label_info(plugin_host_label, "name") + _host_executable_suffix |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 72 | generate_with_plugin = true |
| 73 | } else if (defined(invoker.generator_plugin_script)) { |
| 74 | plugin_path = invoker.generator_plugin_script |
| 75 | generate_with_plugin = true |
| 76 | } else { |
| 77 | generate_with_plugin = false |
| 78 | } |
| 79 | |
| 80 | if (generate_with_plugin) { |
| 81 | if (defined(invoker.generator_plugin_suffix)) { |
| 82 | generator_plugin_suffixes = [ |
| 83 | "${invoker.generator_plugin_suffix}.h", |
| 84 | "${invoker.generator_plugin_suffix}.cc", |
| 85 | ] |
| 86 | } else { |
| 87 | generator_plugin_suffixes = invoker.generator_plugin_suffixes |
| 88 | } |
| 89 | } |
| 90 | |
Lalit Maganti | 45d8eb8 | 2020-09-12 16:32:18 +0100 | [diff] [blame] | 91 | out_dir = "$root_gen_dir/" + proto_out_dir |
| 92 | rel_out_dir = rebase_path(out_dir, root_build_dir) |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 93 | |
Lalit Maganti | fc7b058 | 2019-05-22 23:52:08 +0100 | [diff] [blame] | 94 | # Prevent unused errors when generating descriptor only. |
| 95 | if (generate_descriptor != "") { |
Lalit Maganti | 45d8eb8 | 2020-09-12 16:32:18 +0100 | [diff] [blame] | 96 | not_needed([ "rel_out_dir" ]) |
Lalit Maganti | fc7b058 | 2019-05-22 23:52:08 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 99 | protos = rebase_path(proto_sources, proto_in_dir) |
| 100 | protogens = [] |
| 101 | |
Florian Mayer | ab55e3d | 2018-04-19 11:56:01 +0100 | [diff] [blame] | 102 | if (generate_descriptor != "") { |
Lalit Maganti | 45d8eb8 | 2020-09-12 16:32:18 +0100 | [diff] [blame] | 103 | protogens += [ "$out_dir/${generate_descriptor}" ] |
Florian Mayer | ab55e3d | 2018-04-19 11:56:01 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 106 | foreach(proto, protos) { |
| 107 | proto_dir = get_path_info(proto, "dir") |
| 108 | proto_name = get_path_info(proto, "name") |
| 109 | proto_path = proto_dir + "/" + proto_name |
| 110 | |
Lalit Maganti | fc7b058 | 2019-05-22 23:52:08 +0100 | [diff] [blame] | 111 | # Prevent unused errors when generating descriptor only. |
| 112 | if (generate_descriptor != "") { |
| 113 | not_needed([ "proto_path" ]) |
| 114 | } |
| 115 | |
Hector Dearman | 37e0553 | 2017-11-14 15:35:22 +0000 | [diff] [blame] | 116 | if (generate_cc) { |
| 117 | protogens += [ |
Lalit Maganti | 45d8eb8 | 2020-09-12 16:32:18 +0100 | [diff] [blame] | 118 | "$out_dir/$proto_path.pb.h", |
| 119 | "$out_dir/$proto_path.pb.cc", |
Hector Dearman | 37e0553 | 2017-11-14 15:35:22 +0000 | [diff] [blame] | 120 | ] |
| 121 | } |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 122 | if (generate_with_plugin) { |
| 123 | foreach(suffix, generator_plugin_suffixes) { |
Lalit Maganti | 45d8eb8 | 2020-09-12 16:32:18 +0100 | [diff] [blame] | 124 | protogens += [ "$out_dir/${proto_path}${suffix}" ] |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | config_name = "${target_name}_config" |
Lalit Maganti | f9799c6 | 2019-05-28 20:01:08 +0100 | [diff] [blame] | 130 | if (generate_descriptor == "") { |
| 131 | action_name = "${target_name}_gen" |
| 132 | source_set_name = target_name |
| 133 | } else { |
| 134 | action_name = target_name |
| 135 | } |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 136 | |
| 137 | config(config_name) { |
Lalit Maganti | 45d8eb8 | 2020-09-12 16:32:18 +0100 | [diff] [blame] | 138 | include_dirs = [ out_dir ] |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | # The XXX_gen action that generates the .pb.{cc,h} files. |
| 142 | action(action_name) { |
Lalit Maganti | f9799c6 | 2019-05-28 20:01:08 +0100 | [diff] [blame] | 143 | if (generate_descriptor == "") { |
| 144 | visibility = [ ":$source_set_name" ] |
| 145 | } |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 146 | sources = proto_sources |
| 147 | outputs = get_path_info(protogens, "abspath") |
| 148 | |
Joel Fernandes | 80fc3cd | 2020-04-01 18:02:57 -0400 | [diff] [blame] | 149 | if (perfetto_use_system_protobuf) { |
Primiano Tucci | 42433ab | 2020-11-30 18:42:01 +0100 | [diff] [blame] | 150 | protoc_rebased_path = "protoc" + _host_executable_suffix # from PATH |
Joel Fernandes | 80fc3cd | 2020-04-01 18:02:57 -0400 | [diff] [blame] | 151 | } else { |
| 152 | protoc_label = "//gn:protoc($host_toolchain)" |
Primiano Tucci | 42433ab | 2020-11-30 18:42:01 +0100 | [diff] [blame] | 153 | protoc_path = get_label_info(protoc_label, "root_out_dir") + "/protoc" + |
| 154 | _host_executable_suffix |
Joel Fernandes | 80fc3cd | 2020-04-01 18:02:57 -0400 | [diff] [blame] | 155 | protoc_rebased_path = "./" + rebase_path(protoc_path, root_build_dir) |
| 156 | } |
Primiano Tucci | 42433ab | 2020-11-30 18:42:01 +0100 | [diff] [blame] | 157 | script = "//gn/standalone/protoc.py" |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 158 | args = [ |
| 159 | # Path should be rebased because |root_build_dir| for current toolchain |
| 160 | # may be different from |root_out_dir| of protoc built on host toolchain. |
Joel Fernandes | 80fc3cd | 2020-04-01 18:02:57 -0400 | [diff] [blame] | 161 | protoc_rebased_path, |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 162 | "--proto_path", |
| 163 | rebase_path(proto_in_dir, root_build_dir), |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 164 | ] |
Lalit Maganti | 3b09a3f | 2020-09-14 13:28:44 +0100 | [diff] [blame] | 165 | |
| 166 | foreach(path, import_dirs) { |
| 167 | args += [ |
| 168 | "--proto_path", |
| 169 | rebase_path(path, root_build_dir), |
| 170 | ] |
| 171 | } |
| 172 | |
Daniele Di Proietto | f6f6e78 | 2024-04-29 14:21:48 +0000 | [diff] [blame] | 173 | metadata = { |
| 174 | proto_import_dirs = import_dirs |
| 175 | } |
| 176 | |
Hector Dearman | 37e0553 | 2017-11-14 15:35:22 +0000 | [diff] [blame] | 177 | if (generate_cc) { |
Primiano Tucci | 7b6a788 | 2020-01-20 22:34:31 +0000 | [diff] [blame] | 178 | cc_generator_options_ = "" |
| 179 | if (defined(invoker.cc_generator_options)) { |
| 180 | cc_generator_options_ = invoker.cc_generator_options |
| 181 | } |
Hector Dearman | 37e0553 | 2017-11-14 15:35:22 +0000 | [diff] [blame] | 182 | args += [ |
| 183 | "--cpp_out", |
Lalit Maganti | 45d8eb8 | 2020-09-12 16:32:18 +0100 | [diff] [blame] | 184 | cc_generator_options_ + rel_out_dir, |
Hector Dearman | 37e0553 | 2017-11-14 15:35:22 +0000 | [diff] [blame] | 185 | ] |
| 186 | } |
Florian Mayer | ab55e3d | 2018-04-19 11:56:01 +0100 | [diff] [blame] | 187 | if (generate_descriptor != "") { |
Lalit Maganti | 45d8eb8 | 2020-09-12 16:32:18 +0100 | [diff] [blame] | 188 | depfile = "$root_gen_dir/$generate_descriptor.d" |
Andrew Shulaev | e6c4a5c | 2021-01-29 11:07:39 +0000 | [diff] [blame] | 189 | |
| 190 | if (!exclude_imports) { |
| 191 | args += [ "--include_imports" ] |
| 192 | } |
Florian Mayer | ab55e3d | 2018-04-19 11:56:01 +0100 | [diff] [blame] | 193 | args += [ |
| 194 | "--descriptor_set_out", |
Lalit Maganti | 45d8eb8 | 2020-09-12 16:32:18 +0100 | [diff] [blame] | 195 | rebase_path("$root_gen_dir/$generate_descriptor", root_build_dir), |
Lalit Maganti | e87838f | 2019-06-25 18:31:49 +0100 | [diff] [blame] | 196 | "--dependency_out", |
Hector Dearman | 7a9ffa7 | 2019-08-13 16:00:10 +0100 | [diff] [blame] | 197 | rebase_path(depfile, root_build_dir), |
Florian Mayer | ab55e3d | 2018-04-19 11:56:01 +0100 | [diff] [blame] | 198 | ] |
| 199 | } |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 200 | |
| 201 | if (generate_with_plugin) { |
| 202 | plugin_path_rebased = rebase_path(plugin_path, root_build_dir) |
| 203 | plugin_out_args = "" |
| 204 | if (defined(invoker.generator_plugin_options)) { |
| 205 | plugin_out_args += invoker.generator_plugin_options |
| 206 | } |
Lalit Maganti | 45d8eb8 | 2020-09-12 16:32:18 +0100 | [diff] [blame] | 207 | plugin_out_args += ":$rel_out_dir" |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 208 | |
| 209 | args += [ |
| 210 | "--plugin=protoc-gen-plugin=$plugin_path_rebased", |
| 211 | "--plugin_out=$plugin_out_args", |
| 212 | ] |
| 213 | } |
| 214 | |
Primiano Tucci | e8a7760 | 2018-01-16 00:14:08 +0000 | [diff] [blame] | 215 | args += rebase_path(proto_sources, root_build_dir) |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 216 | |
Joel Fernandes | 80fc3cd | 2020-04-01 18:02:57 -0400 | [diff] [blame] | 217 | if (!perfetto_use_system_protobuf) { |
| 218 | inputs = [ protoc_path ] |
| 219 | deps = [ protoc_label ] |
| 220 | } else { |
Primiano Tucci | 5cbeadf | 2020-04-03 22:28:40 +0100 | [diff] [blame] | 221 | inputs = [] |
| 222 | deps = [] |
Joel Fernandes | 80fc3cd | 2020-04-01 18:02:57 -0400 | [diff] [blame] | 223 | } |
Hector Dearman | 7125663 | 2019-03-11 16:39:34 +0000 | [diff] [blame] | 224 | |
| 225 | # TODO(hjd): Avoid adding to deps here this. |
| 226 | # When we generate BUILD files we need find the transitive proto, |
| 227 | # dependencies, so also add link_deps to actual deps so they show up |
| 228 | # in gn desc. |
| 229 | if (defined(invoker.link_deps)) { |
| 230 | deps += invoker.link_deps |
| 231 | } |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 232 | if (generate_with_plugin) { |
| 233 | inputs += [ plugin_path ] |
| 234 | if (defined(plugin_host_label)) { |
| 235 | # Action depends on native generator plugin but for host toolchain only. |
| 236 | deps += [ plugin_host_label ] |
| 237 | } |
| 238 | } |
Eric Seckler | e0e1914 | 2019-03-25 10:14:18 +0000 | [diff] [blame] | 239 | |
| 240 | if (defined(invoker.deps)) { |
| 241 | deps += invoker.deps |
| 242 | } |
Lalit Maganti | f9799c6 | 2019-05-28 20:01:08 +0100 | [diff] [blame] | 243 | } # action(action_name) |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 244 | |
| 245 | # The source_set that builds the generated .pb.cc files. |
Lalit Maganti | f9799c6 | 2019-05-28 20:01:08 +0100 | [diff] [blame] | 246 | if (generate_descriptor == "") { |
| 247 | source_set(source_set_name) { |
| 248 | forward_variables_from(invoker, |
| 249 | [ |
| 250 | "defines", |
| 251 | "include_dirs", |
| 252 | "public_configs", |
| 253 | "testonly", |
| 254 | "visibility", |
| 255 | ]) |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 256 | |
Lalit Maganti | f9799c6 | 2019-05-28 20:01:08 +0100 | [diff] [blame] | 257 | sources = get_target_outputs(":$action_name") |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 258 | |
Lalit Maganti | f9799c6 | 2019-05-28 20:01:08 +0100 | [diff] [blame] | 259 | configs -= [ "//gn/standalone:extra_warnings" ] |
| 260 | if (defined(invoker.extra_configs)) { |
| 261 | configs += invoker.extra_configs |
| 262 | } |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 263 | |
Lalit Maganti | f9799c6 | 2019-05-28 20:01:08 +0100 | [diff] [blame] | 264 | if (!defined(invoker.public_configs)) { |
| 265 | public_configs = [] |
| 266 | } |
Primiano Tucci | 4e313b8 | 2017-11-02 12:16:58 +0000 | [diff] [blame] | 267 | |
Lalit Maganti | 7690522 | 2022-05-20 14:24:14 +0100 | [diff] [blame] | 268 | public_configs += [ ":$config_name" ] |
| 269 | |
| 270 | # Only include the protobuf_gen_config when generating .pb.cc files. |
| 271 | # Note that |generate_cc| is false for .{pbzero,ipc,gen etc.}.cc |
| 272 | if (generate_cc) { |
| 273 | public_configs += [ "//gn:protobuf_gen_config" ] |
| 274 | } |
Primiano Tucci | 782374f | 2017-11-27 16:25:20 +0000 | [diff] [blame] | 275 | |
Eric Seckler | d7fd6c2 | 2020-01-22 18:46:35 +0000 | [diff] [blame] | 276 | # By default, propagate the config for |include_dirs| to dependent |
| 277 | # targets, so that public imports can be resolved to corresponding header |
| 278 | # files. In some cases, the embedder target handles include directory |
| 279 | # propagation itself, e.g. via a common config. |
| 280 | propagate_imports_configs = !defined(invoker.propagate_imports_configs) || |
| 281 | invoker.propagate_imports_configs |
| 282 | if (propagate_imports_configs) { |
| 283 | public_configs += [ ":$config_name" ] |
| 284 | } else { |
| 285 | configs += [ ":$config_name" ] |
| 286 | } |
| 287 | |
Lalit Maganti | f9799c6 | 2019-05-28 20:01:08 +0100 | [diff] [blame] | 288 | # Use protobuf_full only for tests. |
| 289 | if (defined(invoker.use_protobuf_full) && |
| 290 | invoker.use_protobuf_full == true) { |
Primiano Tucci | 2925e9d | 2020-01-27 10:15:58 +0000 | [diff] [blame] | 291 | deps = [ "//gn:protobuf_full" ] |
Lalit Maganti | 4e7b577 | 2019-06-13 13:36:33 +0100 | [diff] [blame] | 292 | } else if (generate_cc) { |
Primiano Tucci | 2925e9d | 2020-01-27 10:15:58 +0000 | [diff] [blame] | 293 | deps = [ "//gn:protobuf_lite" ] |
Lalit Maganti | 4e7b577 | 2019-06-13 13:36:33 +0100 | [diff] [blame] | 294 | } else { |
| 295 | deps = [] |
Lalit Maganti | f9799c6 | 2019-05-28 20:01:08 +0100 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | deps += [ ":$action_name" ] |
| 299 | if (defined(invoker.deps)) { |
| 300 | deps += invoker.deps |
| 301 | } |
| 302 | } # source_set(source_set_name) |
| 303 | } |
Primiano Tucci | fd48423 | 2017-10-25 00:15:39 +0100 | [diff] [blame] | 304 | } # template |