blob: ad9f175581a3fc811a1258056e783c510014c7b0 [file] [log] [blame]
Alexei Frolove19ebb82020-05-14 17:21:20 -07001# Copyright 2020 The Pigweed Authors
Alexei Frolov942adf02019-12-11 17:07:28 -08002#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7# https://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, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
Armando Montanezfb3d3fb2020-06-09 18:12:12 -070015import("//build_overrides/pigweed.gni")
16
Alexei Frolovb499d3f2020-10-28 13:00:08 -070017import("$dir_pw_build/error.gni")
Alexei Frolov05d8ef22020-06-08 10:32:29 -070018import("$dir_pw_build/input_group.gni")
Wyatt Hepler51ded742020-10-19 14:45:27 -070019import("$dir_pw_build/python_action.gni")
Alexei Frolovedd2f142020-06-09 19:11:27 -070020import("$dir_pw_build/target_types.gni")
Alexei Frolovdd6fa5c2020-08-11 10:04:01 -070021import("$dir_pw_third_party/nanopb/nanopb.gni")
Wyatt Heplerd49f8fe2020-10-15 10:13:47 -070022
Alexei Frolov942adf02019-12-11 17:07:28 -080023# Python script that invokes protoc.
24_gen_script_path =
25 "$dir_pw_protobuf_compiler/py/pw_protobuf_compiler/generate_protos.py"
26
Wyatt Heplere0c4fdc2020-05-29 17:26:19 -070027_forwarded_vars = [
28 "testonly",
29 "visibility",
30]
31
Alexei Frolovf39cd8b2020-04-13 17:59:20 -070032# Generates pw_protobuf C++ code for proto files, creating a source_set of the
33# generated files. This is internal and should not be used outside of this file.
34# Use pw_proto_library instead.
Alexei Frolov942adf02019-12-11 17:07:28 -080035#
36# Args:
37# protos: List of input .proto files.
Alexei Frolovf39cd8b2020-04-13 17:59:20 -070038template("_pw_pwpb_proto_library") {
Alexei Frolov942adf02019-12-11 17:07:28 -080039 _proto_gen_dir = "$root_gen_dir/protos"
Alexei Frolove19ebb82020-05-14 17:21:20 -070040 _module_path = get_path_info(".", "abspath")
41 _relative_proto_paths = rebase_path(invoker.protos, _module_path)
42
43 _outputs = []
44 foreach(_proto, _relative_proto_paths) {
45 _output = string_replace(_proto, ".proto", ".pwpb.h")
46 _outputs += [ "$_proto_gen_dir/$_output" ]
47 }
Alexei Frolov942adf02019-12-11 17:07:28 -080048
49 _gen_target = "${target_name}_gen"
Wyatt Heplerc8e05a42020-10-19 14:49:39 -070050 pw_python_action(_gen_target) {
Wyatt Heplere0c4fdc2020-05-29 17:26:19 -070051 forward_variables_from(invoker, _forwarded_vars)
Alexei Frolov942adf02019-12-11 17:07:28 -080052 script = _gen_script_path
53 args = [
54 "--language",
55 "cc",
56 "--module-path",
Wyatt Hepler8224a642020-07-29 08:55:56 -070057 rebase_path(_module_path),
Alexei Frolove19ebb82020-05-14 17:21:20 -070058 "--include-file",
Wyatt Hepler8224a642020-07-29 08:55:56 -070059 rebase_path(invoker.include_file),
Alexei Frolov942adf02019-12-11 17:07:28 -080060 "--out-dir",
Wyatt Hepler8224a642020-07-29 08:55:56 -070061 rebase_path(_proto_gen_dir),
62 ] + rebase_path(invoker.protos)
Alexei Frolov942adf02019-12-11 17:07:28 -080063 inputs = invoker.protos
64 outputs = _outputs
Alexei Frolove19ebb82020-05-14 17:21:20 -070065 deps = invoker.deps
Alexei Frolovbbf164c2019-12-16 12:51:59 -080066 if (defined(invoker.protoc_deps)) {
Alexei Frolove19ebb82020-05-14 17:21:20 -070067 deps += invoker.protoc_deps
Alexei Frolovbbf164c2019-12-16 12:51:59 -080068 }
Alexei Frolov942adf02019-12-11 17:07:28 -080069 }
70
71 # For C++ proto files, the generated proto directory is added as an include
Alexei Frolovb499d3f2020-10-28 13:00:08 -070072 # path for the code.
Alexei Frolov942adf02019-12-11 17:07:28 -080073 _include_config_target = "${target_name}_includes"
74 config(_include_config_target) {
Alexei Frolove19ebb82020-05-14 17:21:20 -070075 include_dirs = [ "$_proto_gen_dir" ]
Alexei Frolov942adf02019-12-11 17:07:28 -080076 }
77
78 # Create a library with the generated source files.
Alexei Frolovedd2f142020-06-09 19:11:27 -070079 pw_source_set(target_name) {
Alexei Frolovb499d3f2020-10-28 13:00:08 -070080 public_configs = [ ":$_include_config_target" ]
Alexei Frolove19ebb82020-05-14 17:21:20 -070081 deps = [ ":$_gen_target" ]
82 public_deps = [ dir_pw_protobuf ] + invoker.gen_deps
Alexei Frolov942adf02019-12-11 17:07:28 -080083 sources = get_target_outputs(":$_gen_target")
Alexei Frolovf39cd8b2020-04-13 17:59:20 -070084 public = filter_include(sources, [ "*.pwpb.h" ])
85 }
86}
87
Alexei Frolov79b7cb02020-07-06 13:51:43 -070088# Generates nanopb RPC code for proto files, creating a source_set of the
89# generated files. This is internal and should not be used outside of this file.
90# Use pw_proto_library instead.
91#
92# Args:
93# protos: List of input .proto files.
94#
95template("_pw_nanopb_rpc_proto_library") {
Alexei Frolov79b7cb02020-07-06 13:51:43 -070096 _proto_gen_dir = "$root_gen_dir/protos"
97 _module_path = get_path_info(".", "abspath")
98 _relative_proto_paths = rebase_path(invoker.protos, _module_path)
99
100 _outputs = []
101 foreach(_proto, _relative_proto_paths) {
Alexei Frolov3ab26ff2020-07-21 10:44:58 -0700102 _output_h = string_replace(_proto, ".proto", ".rpc.pb.h")
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700103 _outputs += [ "$_proto_gen_dir/$_output_h" ]
104 }
105
106 # Create a target which runs protoc configured with the nanopb_rpc plugin to
107 # generate the C++ proto RPC headers.
108 _gen_target = "${target_name}_gen"
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700109 pw_python_action(_gen_target) {
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700110 forward_variables_from(invoker, _forwarded_vars)
111 script = _gen_script_path
112 args = [
113 "--language",
114 "nanopb_rpc",
115 "--module-path",
Wyatt Hepler8224a642020-07-29 08:55:56 -0700116 rebase_path(_module_path),
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700117 "--include-paths",
Wyatt Hepler8224a642020-07-29 08:55:56 -0700118 rebase_path("$dir_pw_third_party_nanopb/generator/proto"),
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700119 "--include-file",
Wyatt Hepler8224a642020-07-29 08:55:56 -0700120 rebase_path(invoker.include_file),
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700121 "--out-dir",
Wyatt Hepler8224a642020-07-29 08:55:56 -0700122 rebase_path(_proto_gen_dir),
123 ] + rebase_path(invoker.protos)
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700124 inputs = invoker.protos
125 outputs = _outputs
126
127 deps = invoker.deps
128 if (defined(invoker.protoc_deps)) {
129 deps += invoker.protoc_deps
130 }
131 }
132
133 # For C++ proto files, the generated proto directory is added as an include
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700134 # path for the code.
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700135 _include_root = rebase_path(get_path_info(".", "abspath"), "//")
136 _include_config_target = "${target_name}_includes"
137 config(_include_config_target) {
138 include_dirs = [
139 "$_proto_gen_dir",
140 "$_proto_gen_dir/$_include_root",
141 ]
142 }
143
144 # Create a library with the generated source files.
145 pw_source_set(target_name) {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700146 public_configs = [ ":$_include_config_target" ]
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700147 deps = [ ":$_gen_target" ]
148 public_deps = [
Wyatt Heplercbd09c22020-09-15 11:17:24 -0700149 "$dir_pw_rpc:server",
Alexei Frolova4d71502020-10-14 12:43:14 -0700150 "$dir_pw_rpc/nanopb:method_union",
Alexei Frolovdd6fa5c2020-08-11 10:04:01 -0700151 "$dir_pw_third_party/nanopb",
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700152 ] + invoker.gen_deps
153 public = get_target_outputs(":$_gen_target")
154 }
155}
156
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700157# Generates nanopb code for proto files, creating a source_set of the generated
158# files. This is internal and should not be used outside of this file. Use
159# pw_proto_library instead.
160#
161# Args:
162# protos: List of input .proto files.
163template("_pw_nanopb_proto_library") {
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700164 _proto_gen_dir = "$root_gen_dir/protos"
Alexei Frolove19ebb82020-05-14 17:21:20 -0700165 _module_path = get_path_info(".", "abspath")
166 _relative_proto_paths = rebase_path(invoker.protos, _module_path)
167
168 _outputs = []
169 foreach(_proto, _relative_proto_paths) {
170 _output_h = string_replace(_proto, ".proto", ".pb.h")
171 _output_c = string_replace(_proto, ".proto", ".pb.c")
172 _outputs += [
173 "$_proto_gen_dir/$_output_h",
174 "$_proto_gen_dir/$_output_c",
175 ]
176 }
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700177
Alexei Frolov4c0428a2020-06-10 10:46:04 -0700178 _nanopb_plugin = "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb"
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700179 if (host_os == "win") {
180 _nanopb_plugin += ".bat"
181 }
182
183 # Create a target which runs protoc configured with the nanopb plugin to
184 # generate the C proto sources.
185 _gen_target = "${target_name}_gen"
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700186 pw_python_action(_gen_target) {
Wyatt Heplere0c4fdc2020-05-29 17:26:19 -0700187 forward_variables_from(invoker, _forwarded_vars)
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700188 script = _gen_script_path
189 args = [
190 "--language",
191 "nanopb",
192 "--module-path",
Wyatt Hepler8224a642020-07-29 08:55:56 -0700193 rebase_path(_module_path),
Alexei Frolovac5b6742020-05-01 13:28:14 -0700194 "--include-paths",
Wyatt Hepler8224a642020-07-29 08:55:56 -0700195 rebase_path("$dir_pw_third_party_nanopb/generator/proto"),
Alexei Frolove19ebb82020-05-14 17:21:20 -0700196 "--include-file",
Wyatt Hepler8224a642020-07-29 08:55:56 -0700197 rebase_path(invoker.include_file),
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700198 "--out-dir",
Wyatt Hepler8224a642020-07-29 08:55:56 -0700199 rebase_path(_proto_gen_dir),
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700200 "--custom-plugin",
Wyatt Hepler8224a642020-07-29 08:55:56 -0700201 rebase_path(_nanopb_plugin),
202 ] + rebase_path(invoker.protos)
203
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700204 inputs = invoker.protos
205 outputs = _outputs
206
Alexei Frolove19ebb82020-05-14 17:21:20 -0700207 deps = invoker.deps
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700208 if (defined(invoker.protoc_deps)) {
Alexei Frolove19ebb82020-05-14 17:21:20 -0700209 deps += invoker.protoc_deps
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700210 }
211 }
212
213 # For C++ proto files, the generated proto directory is added as an include
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700214 # path for the code.
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700215 _include_root = rebase_path(get_path_info(".", "abspath"), "//")
216 _include_config_target = "${target_name}_includes"
217 config(_include_config_target) {
218 include_dirs = [
219 "$_proto_gen_dir",
220 "$_proto_gen_dir/$_include_root",
221 ]
Ewout van Bekkume4d7b692020-10-15 13:12:30 -0700222
223 # Nanopb uses __cplusplus with the implicit default of 0.
224 cflags = [ "-Wno-undef" ]
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700225 }
226
227 # Create a library with the generated source files.
Alexei Frolovedd2f142020-06-09 19:11:27 -0700228 pw_source_set(target_name) {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700229 public_configs = [ ":$_include_config_target" ]
Alexei Frolove19ebb82020-05-14 17:21:20 -0700230 deps = [ ":$_gen_target" ]
Alexei Frolovdd6fa5c2020-08-11 10:04:01 -0700231 public_deps = [ "$dir_pw_third_party/nanopb" ] + invoker.gen_deps
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700232 sources = get_target_outputs(":$_gen_target")
233 public = filter_include(sources, [ "*.pb.h" ])
Alexei Frolov942adf02019-12-11 17:07:28 -0800234 }
235}
236
Alexei Frolovc912ea72020-10-26 08:43:27 -0700237# Generates raw RPC code for proto files, creating a source_set of the generated
238# files. This is internal and should not be used outside of this file. Use
239# pw_proto_library instead.
240#
241# Args:
242# protos: List of input .proto files.
243#
244template("_pw_raw_rpc_proto_library") {
245 _proto_gen_dir = "$root_gen_dir/protos"
246 _module_path = get_path_info(".", "abspath")
247 _relative_proto_paths = rebase_path(invoker.protos, _module_path)
248
249 _outputs = []
250 foreach(_proto, _relative_proto_paths) {
251 _output_h = string_replace(_proto, ".proto", ".raw_rpc.pb.h")
252 _outputs += [ "$_proto_gen_dir/$_output_h" ]
253 }
254
255 # Create a target which runs protoc configured with the nanopb_rpc plugin to
256 # generate the C++ proto RPC headers.
257 _gen_target = "${target_name}_gen"
258 pw_python_action(_gen_target) {
259 forward_variables_from(invoker, _forwarded_vars)
260 script = _gen_script_path
261 args = [
262 "--language",
263 "raw_rpc",
264 "--module-path",
265 rebase_path(_module_path),
266 "--include-file",
267 rebase_path(invoker.include_file),
268 "--out-dir",
269 rebase_path(_proto_gen_dir),
270 ] + rebase_path(invoker.protos)
271 inputs = invoker.protos
272 outputs = _outputs
273
274 deps = invoker.deps
275 if (defined(invoker.protoc_deps)) {
276 deps += invoker.protoc_deps
277 }
278 }
279
280 # For C++ proto files, the generated proto directory is added as an include
281 # path for the code.
282 _include_root = rebase_path(get_path_info(".", "abspath"), "//")
283 _include_config_target = "${target_name}_includes"
284 config(_include_config_target) {
285 include_dirs = [
286 "$_proto_gen_dir",
287 "$_proto_gen_dir/$_include_root",
288 ]
289 }
290
291 # Create a library with the generated source files.
292 pw_source_set(target_name) {
293 public_configs = [ ":$_include_config_target" ]
294 deps = [ ":$_gen_target" ]
295 public_deps = [
296 "$dir_pw_rpc:server",
297 "$dir_pw_rpc/raw:method_union",
298 ] + invoker.gen_deps
299 public = get_target_outputs(":$_gen_target")
300 }
301}
302
Alexei Frolovdef14712019-12-23 13:03:32 -0800303# Generates Go code for proto files, listing the proto output directory in the
304# metadata variable GOPATH. Internal use only.
305#
306# Args:
307# protos: List of input .proto files.
308template("_pw_go_proto_library") {
309 _proto_gopath = "$root_gen_dir/go"
310 _proto_gen_dir = "$_proto_gopath/src"
311 _rebased_gopath = rebase_path(_proto_gopath)
312
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700313 pw_python_action(target_name) {
Wyatt Heplere0c4fdc2020-05-29 17:26:19 -0700314 forward_variables_from(invoker, _forwarded_vars)
Alexei Frolovdef14712019-12-23 13:03:32 -0800315 metadata = {
316 gopath = [ "GOPATH+=$_rebased_gopath" ]
Alexei Frolovc15a9882019-12-23 14:29:02 -0800317 external_deps = [
318 "github.com/golang/protobuf/proto",
319 "google.golang.org/grpc",
320 ]
Alexei Frolovdef14712019-12-23 13:03:32 -0800321 }
322 script = _gen_script_path
323 args = [
324 "--language",
325 "go",
326 "--module-path",
Wyatt Hepler8224a642020-07-29 08:55:56 -0700327 rebase_path("//"),
Alexei Frolove19ebb82020-05-14 17:21:20 -0700328 "--include-file",
Wyatt Hepler8224a642020-07-29 08:55:56 -0700329 rebase_path(invoker.include_file),
Alexei Frolovdef14712019-12-23 13:03:32 -0800330 "--out-dir",
Wyatt Hepler8224a642020-07-29 08:55:56 -0700331 rebase_path(_proto_gen_dir),
332 ] + rebase_path(invoker.protos)
Alexei Frolovdef14712019-12-23 13:03:32 -0800333 inputs = invoker.protos
Alexei Frolove19ebb82020-05-14 17:21:20 -0700334 deps = invoker.deps + invoker.gen_deps
Alexei Frolovdef14712019-12-23 13:03:32 -0800335 stamp = true
336 }
337}
338
Alexei Frolov942adf02019-12-11 17:07:28 -0800339# Generates protobuf code from .proto definitions for various languages.
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700340# For each supported generator, creates a sub-target named:
Alexei Frolov942adf02019-12-11 17:07:28 -0800341#
Alexei Frolov8e30d462020-10-22 13:54:36 -0700342# <target_name>.<generator>
Alexei Frolov942adf02019-12-11 17:07:28 -0800343#
Alexei Frolov942adf02019-12-11 17:07:28 -0800344# Args:
345# sources: List of input .proto files.
346# deps: List of other pw_proto_library dependencies.
Alexei Frolov05d8ef22020-06-08 10:32:29 -0700347# inputs: Other files on which the protos depend (e.g. nanopb .options files).
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700348#
Alexei Frolov942adf02019-12-11 17:07:28 -0800349template("pw_proto_library") {
350 assert(defined(invoker.sources) && invoker.sources != [],
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700351 "pw_proto_library requires .proto source files")
Alexei Frolov942adf02019-12-11 17:07:28 -0800352
Alexei Frolove19ebb82020-05-14 17:21:20 -0700353 # For each proto target, create a file which collects the base directories of
354 # all of its dependencies to list as include paths to protoc.
355 _include_metadata_target = "${target_name}_include_paths"
356 _include_metadata_file = "${target_gen_dir}/${target_name}_includes.txt"
357 generated_file(_include_metadata_target) {
Alexei Frolov942adf02019-12-11 17:07:28 -0800358 if (defined(invoker.deps)) {
Alexei Frolove19ebb82020-05-14 17:21:20 -0700359 # Collect metadata from the include path files of each dependency.
360 deps = process_file_template(invoker.deps, "{{source}}_include_paths")
Alexei Frolov942adf02019-12-11 17:07:28 -0800361 } else {
Alexei Frolove19ebb82020-05-14 17:21:20 -0700362 deps = []
Alexei Frolov942adf02019-12-11 17:07:28 -0800363 }
Alexei Frolove19ebb82020-05-14 17:21:20 -0700364 data_keys = [ "protoc_includes" ]
365 outputs = [ _include_metadata_file ]
366
367 # Indicate this library's base directory for its dependents.
368 metadata = {
Alexei Frolovf79d2272020-06-18 13:37:38 -0700369 protoc_includes = [ rebase_path(".") ]
Alexei Frolove19ebb82020-05-14 17:21:20 -0700370 }
371 }
372
Alexei Frolov05d8ef22020-06-08 10:32:29 -0700373 _deps = [ ":$_include_metadata_target" ]
374
375 if (defined(invoker.inputs)) {
376 # Toss any additional inputs into an input group dependency.
377 _input_target_name = "${target_name}_inputs"
378 pw_input_group(_input_target_name) {
379 inputs = invoker.inputs
380 }
381 _deps += [ ":$_input_target_name" ]
382 }
383
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700384 _base_target = target_name
385
386 if (defined(invoker.deps)) {
387 _invoker_deps = invoker.deps
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700388 } else {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700389 _invoker_deps = []
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700390 }
391
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700392 # Enumerate all of the protobuf generator targets.
Alexei Frolovc4b62ec2020-07-13 08:35:10 -0700393
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700394 _pw_pwpb_proto_library("${_base_target}.pwpb") {
395 forward_variables_from(invoker, _forwarded_vars)
396 protos = invoker.sources
397 deps = _deps
398 include_file = _include_metadata_file
399 gen_deps = process_file_template(_invoker_deps, "{{source}}.pwpb")
400 protoc_deps = [ "$dir_pw_protobuf/py" ]
401 }
402
403 if (dir_pw_third_party_nanopb != "") {
404 _pw_nanopb_rpc_proto_library("${_base_target}.nanopb_rpc") {
405 forward_variables_from(invoker, _forwarded_vars)
406 protos = invoker.sources
407 deps = _deps
408 include_file = _include_metadata_file
409 gen_deps = process_file_template(_invoker_deps, "{{source}}.nanopb") +
410 [ ":${_base_target}.nanopb" ]
411 protoc_deps = [ "$dir_pw_rpc/py" ]
Alexei Frolovc4b62ec2020-07-13 08:35:10 -0700412 }
413
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700414 _pw_nanopb_proto_library("${target_name}.nanopb") {
415 forward_variables_from(invoker, _forwarded_vars)
416 protos = invoker.sources
417 deps = _deps
418 include_file = _include_metadata_file
419 gen_deps = process_file_template(_invoker_deps, "{{source}}.nanopb")
420 }
421 } else {
422 pw_error("${_base_target}.nanopb_rpc") {
423 message =
424 "\$dir_pw_third_party_nanopb must be set to generate nanopb RPC code."
Alexei Frolov8185c822020-06-12 10:45:04 -0700425 }
Alexei Frolov942adf02019-12-11 17:07:28 -0800426
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700427 pw_error("${_base_target}.nanopb") {
428 message =
429 "\$dir_pw_third_party_nanopb must be set to compile nanopb protobufs."
Alexei Frolov942adf02019-12-11 17:07:28 -0800430 }
431 }
432
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700433 _pw_raw_rpc_proto_library("${target_name}.raw_rpc") {
434 forward_variables_from(invoker, _forwarded_vars)
435 protos = invoker.sources
436 deps = _deps
437 include_file = _include_metadata_file
438 gen_deps = []
439 protoc_deps = [ "$dir_pw_rpc/py" ]
440 }
441
442 _pw_go_proto_library("${target_name}.go") {
443 forward_variables_from(invoker, _forwarded_vars)
444 protos = invoker.sources
445 deps = _deps
446 include_file = _include_metadata_file
447 gen_deps = process_file_template(_invoker_deps, "{{source}}.go")
448 }
449
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700450 # All supported pw_protobuf generators.
451 _protobuf_generators = [
452 "pwpb",
453 "nanopb",
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700454 "nanopb_rpc",
Alexei Frolovc912ea72020-10-26 08:43:27 -0700455 "raw_rpc",
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700456 "go",
457 ]
458
Alexei Frolov942adf02019-12-11 17:07:28 -0800459 # If the user attempts to use the target directly instead of one of the
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700460 # generator targets, run a script which prints a nice error message.
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700461 pw_python_action(target_name) {
Alexei Frolov942adf02019-12-11 17:07:28 -0800462 script = string_join("/",
463 [
464 dir_pw_protobuf_compiler,
465 "py",
466 "pw_protobuf_compiler",
467 "proto_target_invalid.py",
468 ])
469 args = [
470 "--target",
471 target_name,
472 "--dir",
473 get_path_info(".", "abspath"),
474 "--root",
475 "//",
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700476 ] + _protobuf_generators
Alexei Frolov942adf02019-12-11 17:07:28 -0800477 stamp = true
478 }
479}