1# Copyright 2020 The Pigweed Authors 2# 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 15import("//build_overrides/pigweed.gni") 16 17import("$dir_pw_build/error.gni") 18import("$dir_pw_build/input_group.gni") 19import("$dir_pw_build/mirror_tree.gni") 20import("$dir_pw_build/python.gni") 21import("$dir_pw_build/python_action.gni") 22import("$dir_pw_build/python_gn_args.gni") 23import("$dir_pw_build/target_types.gni") 24import("$dir_pw_third_party/nanopb/nanopb.gni") 25import("toolchain.gni") 26 27# Variables forwarded from the public pw_proto_library template to the final 28# pw_source_set. 29_forwarded_vars = [ 30 "testonly", 31 "visibility", 32] 33 34# Internal template that invokes protoc with a pw_python_action. This should not 35# be used outside of this file; use pw_proto_library instead. 36# 37# This creates the internal GN target $target_name.$language._gen that compiles 38# proto files with protoc. 39template("_pw_invoke_protoc") { 40 if (current_toolchain == pw_protobuf_compiler_TOOLCHAIN) { 41 if (defined(invoker.out_dir)) { 42 _out_dir = invoker.out_dir 43 } else { 44 _out_dir = "${invoker.base_out_dir}/${invoker.language}" 45 if (defined(invoker.module_as_package) && 46 invoker.module_as_package != "") { 47 assert(invoker.language == "python") 48 _out_dir = "$_out_dir/${invoker.module_as_package}" 49 } 50 } 51 52 _includes = 53 rebase_path(get_target_outputs(":${invoker.base_target}._includes"), 54 root_build_dir) 55 56 pw_python_action("$target_name._gen") { 57 script = 58 "$dir_pw_protobuf_compiler/py/pw_protobuf_compiler/generate_protos.py" 59 60 # NOTE: A python_dep on "$dir_pw_protobuf_compiler/py" should not be 61 # included building that Python package which requires the build venv to 62 # be created. Instead, use python_metadata_deps to only add 63 # pw_protobuf_compiler to the PYTHONPATH. 64 python_deps = [] 65 66 # Add pw_protobuf_compiler and its dependencies to the PYTHONPATH when 67 # running this action. 68 python_metadata_deps = [ "$dir_pw_protobuf_compiler/py" ] 69 70 python_deps = [] 71 if (defined(invoker.python_deps)) { 72 python_deps += invoker.python_deps 73 } 74 75 deps = [ 76 ":${invoker.base_target}._includes", 77 ":${invoker.base_target}._sources", 78 ] 79 80 foreach(dep, invoker.deps) { 81 deps += [ get_label_info(dep, "label_no_toolchain") + "._gen" ] 82 } 83 84 if (defined(invoker.other_deps)) { 85 deps += invoker.other_deps 86 } 87 88 args = [] 89 if (!pw_protobuf_compiler_GENERATE_LEGACY_ENUM_SNAKE_CASE_NAMES) { 90 args += [ "--exclude-pwpb-legacy-snake-case-field-name-enums" ] 91 } 92 args += [ 93 "--language", 94 invoker.language, 95 "--include-file", 96 _includes[0], 97 "--compile-dir", 98 rebase_path(invoker.compile_dir, root_build_dir), 99 "--out-dir", 100 rebase_path(_out_dir, root_build_dir), 101 "--sources", 102 ] + rebase_path(invoker.sources, root_build_dir) 103 104 if (defined(invoker.plugin)) { 105 inputs = [ invoker.plugin ] 106 args += 107 [ "--plugin-path=" + rebase_path(invoker.plugin, root_build_dir) ] 108 } 109 110 if (defined(invoker.outputs)) { 111 outputs = invoker.outputs 112 } else { 113 stamp = true 114 } 115 116 if (defined(invoker.metadata)) { 117 metadata = invoker.metadata 118 } 119 } 120 121 # Output a .json file with information about this proto library. 122 _proto_info = { 123 label = get_label_info(":${invoker.target_name}", "label_no_toolchain") 124 protoc_outputs = 125 rebase_path(get_target_outputs(":$target_name._gen"), root_build_dir) 126 root = rebase_path(_out_dir, root_build_dir) 127 package = invoker.package 128 129 nested_in_python_package = "" 130 if (defined(invoker.python_package)) { 131 nested_in_python_package = 132 get_label_info(invoker.python_package, "label_no_toolchain") 133 } 134 135 dependencies = [] 136 foreach(dep, invoker.deps) { 137 dependencies += 138 rebase_path([ get_label_info(dep, "target_gen_dir") + "/" + 139 get_label_info(dep, "name") + ".json" ], 140 root_build_dir) 141 } 142 } 143 write_file("$target_gen_dir/$target_name.json", _proto_info, "json") 144 } else { 145 # protoc is only ever invoked from pw_protobuf_compiler_TOOLCHAIN. 146 not_needed([ "target_name" ]) 147 not_needed(invoker, "*") 148 } 149} 150 151# Generates pw_protobuf C++ code for proto files, creating a source_set of the 152# generated files. This is internal and should not be used outside of this file. 153# Use pw_proto_library instead. 154template("_pw_pwpb_rpc_proto_library") { 155 # Create a target which runs protoc configured with the pwpb_rpc plugin to 156 # generate the C++ proto RPC headers. 157 _pw_invoke_protoc(target_name) { 158 forward_variables_from(invoker, "*", _forwarded_vars) 159 language = "pwpb_rpc" 160 plugin = "$dir_pw_rpc/py/pw_rpc/plugin_pwpb.py" 161 python_deps = [ "$dir_pw_rpc/py" ] 162 } 163 164 # Create a library with the generated source files. 165 config("$target_name._include_path") { 166 include_dirs = [ "${invoker.base_out_dir}/pwpb_rpc" ] 167 visibility = [ ":*" ] 168 } 169 170 pw_source_set(target_name) { 171 forward_variables_from(invoker, _forwarded_vars) 172 public_configs = [ ":$target_name._include_path" ] 173 deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ] 174 public_deps = [ 175 ":${invoker.base_target}.pwpb", 176 "$dir_pw_protobuf", 177 "$dir_pw_rpc:server", 178 "$dir_pw_rpc/pwpb:client_api", 179 "$dir_pw_rpc/pwpb:server_api", 180 ] + invoker.deps 181 public = invoker.outputs 182 check_includes = false 183 } 184} 185 186template("_pw_pwpb_proto_library") { 187 _pw_invoke_protoc(target_name) { 188 forward_variables_from(invoker, "*", _forwarded_vars) 189 language = "pwpb" 190 plugin = "$dir_pw_protobuf/py/pw_protobuf/plugin.py" 191 python_deps = [ "$dir_pw_protobuf/py" ] 192 } 193 194 # Create a library with the generated source files. 195 config("$target_name._include_path") { 196 include_dirs = [ "${invoker.base_out_dir}/pwpb" ] 197 visibility = [ ":*" ] 198 } 199 200 pw_source_set(target_name) { 201 forward_variables_from(invoker, _forwarded_vars) 202 public_configs = [ ":$target_name._include_path" ] 203 deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ] 204 public_deps = [ 205 "$dir_pw_containers:vector", 206 "$dir_pw_string:string", 207 dir_pw_assert, 208 dir_pw_function, 209 dir_pw_preprocessor, 210 dir_pw_protobuf, 211 dir_pw_result, 212 dir_pw_status, 213 ] + invoker.deps 214 sources = invoker.outputs 215 public = filter_include(sources, [ "*.pwpb.h" ]) 216 } 217} 218 219# Generates nanopb RPC code for proto files, creating a source_set of the 220# generated files. This is internal and should not be used outside of this file. 221# Use pw_proto_library instead. 222template("_pw_nanopb_rpc_proto_library") { 223 # Create a target which runs protoc configured with the nanopb_rpc plugin to 224 # generate the C++ proto RPC headers. 225 _pw_invoke_protoc(target_name) { 226 forward_variables_from(invoker, "*", _forwarded_vars) 227 language = "nanopb_rpc" 228 plugin = "$dir_pw_rpc/py/pw_rpc/plugin_nanopb.py" 229 python_deps = [ "$dir_pw_rpc/py" ] 230 } 231 232 # Create a library with the generated source files. 233 config("$target_name._include_path") { 234 include_dirs = [ "${invoker.base_out_dir}/nanopb_rpc" ] 235 visibility = [ ":*" ] 236 } 237 238 pw_source_set(target_name) { 239 forward_variables_from(invoker, _forwarded_vars) 240 public_configs = [ ":$target_name._include_path" ] 241 deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ] 242 public_deps = [ 243 ":${invoker.base_target}.nanopb", 244 "$dir_pw_rpc:server", 245 "$dir_pw_rpc/nanopb:client_api", 246 "$dir_pw_rpc/nanopb:server_api", 247 "$dir_pw_third_party/nanopb", 248 ] + invoker.deps 249 public = invoker.outputs 250 check_includes = false 251 } 252} 253 254# Generates nanopb code for proto files, creating a source_set of the generated 255# files. This is internal and should not be used outside of this file. Use 256# pw_proto_library instead. 257template("_pw_nanopb_proto_library") { 258 # When compiling with the Nanopb plugin, the nanopb.proto file is already 259 # compiled internally, so skip recompiling it with protoc. 260 if (rebase_path(invoker.sources, invoker.compile_dir) == [ "nanopb.proto" ]) { 261 group("$target_name._gen") { 262 deps = [ 263 ":${invoker.base_target}._sources($pw_protobuf_compiler_TOOLCHAIN)", 264 ] 265 } 266 267 group("$target_name") { 268 deps = invoker.deps + 269 [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ] 270 } 271 } else { 272 # Create a target which runs protoc configured with the nanopb plugin to 273 # generate the C proto sources. 274 _pw_invoke_protoc(target_name) { 275 forward_variables_from(invoker, "*", _forwarded_vars) 276 language = "nanopb" 277 plugin = "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb" 278 other_deps = [ "$dir_pw_third_party/nanopb:generate_nanopb_proto.action" ] 279 } 280 281 # Create a library with the generated source files. 282 config("$target_name._include_path") { 283 include_dirs = [ "${invoker.base_out_dir}/nanopb" ] 284 visibility = [ ":*" ] 285 } 286 287 pw_source_set(target_name) { 288 forward_variables_from(invoker, _forwarded_vars) 289 public_configs = [ ":$target_name._include_path" ] 290 deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ] 291 public_deps = [ "$dir_pw_third_party/nanopb" ] + invoker.deps 292 sources = invoker.outputs 293 public = filter_include(sources, [ "*.pb.h" ]) 294 } 295 } 296} 297 298# Generates raw RPC code for proto files, creating a source_set of the generated 299# files. This is internal and should not be used outside of this file. Use 300# pw_proto_library instead. 301template("_pw_raw_rpc_proto_library") { 302 # Create a target which runs protoc configured with the nanopb_rpc plugin to 303 # generate the C++ proto RPC headers. 304 _pw_invoke_protoc(target_name) { 305 forward_variables_from(invoker, "*", _forwarded_vars) 306 language = "raw_rpc" 307 plugin = "$dir_pw_rpc/py/pw_rpc/plugin_raw.py" 308 python_deps = [ "$dir_pw_rpc/py" ] 309 } 310 311 # Create a library with the generated source files. 312 config("$target_name._include_path") { 313 include_dirs = [ "${invoker.base_out_dir}/raw_rpc" ] 314 visibility = [ ":*" ] 315 } 316 317 pw_source_set(target_name) { 318 forward_variables_from(invoker, _forwarded_vars) 319 public_configs = [ ":$target_name._include_path" ] 320 deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ] 321 public_deps = [ 322 "$dir_pw_rpc:server", 323 "$dir_pw_rpc/raw:client_api", 324 "$dir_pw_rpc/raw:server_api", 325 ] + invoker.deps 326 public = invoker.outputs 327 check_includes = false 328 } 329} 330 331# Generates Go code for proto files, listing the proto output directory in the 332# metadata variable GOPATH. Internal use only. 333template("_pw_go_proto_library") { 334 _proto_gopath = "$root_gen_dir/go" 335 336 _pw_invoke_protoc(target_name) { 337 forward_variables_from(invoker, "*") 338 language = "go" 339 metadata = { 340 gopath = [ "GOPATH+=" + rebase_path(_proto_gopath) ] 341 external_deps = [ 342 "github.com/golang/protobuf/proto", 343 "google.golang.org/grpc", 344 ] 345 } 346 347 # Override the default "$base_out_dir/$language" output path. 348 out_dir = "$_proto_gopath/src" 349 } 350 351 group(target_name) { 352 deps = 353 invoker.deps + [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ] 354 } 355} 356 357# Generates Python code for proto files, creating a pw_python_package containing 358# the generated files. This is internal and should not be used outside of this 359# file. Use pw_proto_library instead. 360template("_pw_python_proto_library") { 361 _pw_invoke_protoc(target_name) { 362 forward_variables_from(invoker, "*", _forwarded_vars + [ "python_package" ]) 363 language = "python" 364 365 if (defined(invoker.python_package)) { 366 python_package = invoker.python_package 367 } 368 } 369 370 if (defined(invoker.python_package) && invoker.python_package != "") { 371 # This package is nested in another Python package. Depending on this 372 # its python subtarget is equivalent to depending on the Python package it 373 # is nested in. 374 pw_python_group(target_name) { 375 python_deps = [ invoker.python_package ] 376 } 377 378 # This proto library is merged into another package, but create a target to 379 # collect its dependencies that the other package can depend on. 380 pw_python_group(target_name + "._deps") { 381 python_deps = invoker.deps 382 other_deps = 383 [ ":${invoker.target_name}._gen($pw_protobuf_compiler_TOOLCHAIN)" ] 384 } 385 } else { 386 # Create a Python package with the generated source files. 387 pw_python_package(target_name) { 388 forward_variables_from(invoker, _forwarded_vars) 389 _target_dir = 390 get_path_info(get_label_info(":${invoker.base_target}", "dir"), 391 "abspath") 392 generate_setup = { 393 metadata = { 394 # Default to a package name that include the full source path to avoid 395 # conflicts with other packages when collecting all the .whl files 396 # with pw_python_wheels(). 397 name = 398 string_replace(string_replace(_target_dir, "//", ""), "/", "_") + 399 "_" + invoker.base_target 400 401 # The package name should match where the __init__.py lives. If 402 # module_as_package is specified use that for the Python package name. 403 if (defined(invoker.module_as_package) && 404 invoker.module_as_package != "") { 405 name = invoker.module_as_package 406 } 407 version = 408 "0.0.1" # TODO(hepler): Need to be able to set this verison. 409 } 410 } 411 sources = invoker.outputs 412 strip_prefix = "${invoker.base_out_dir}/python" 413 python_deps = invoker.deps 414 other_deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ] 415 static_analysis = [] 416 417 _pw_module_as_package = invoker.module_as_package != "" 418 } 419 } 420} 421 422# Generates protobuf code from .proto definitions for various languages. 423# For each supported generator, creates a sub-target named: 424# 425# <target_name>.<generator> 426# 427# GN permits using abbreviated labels when the target name matches the directory 428# name (e.g. //foo for //foo:foo). For consistency with this, the sub-targets 429# for each generator are aliased to the directory when the target name is the 430# same. For example, these two labels are equivalent: 431# 432# //path/to/my_protos:my_protos.pwpb 433# //path/to/my_protos:pwpb 434# 435# pw_protobuf_library targets generate Python packages. As such, they must have 436# globally unique package names. The first directory of the prefix or the first 437# common directory of the sources is used as the Python package. 438# 439# Args: 440# sources: List of input .proto files. 441# deps: List of other pw_proto_library dependencies. 442# other_deps: List of other non-proto dependencies. 443# inputs: Other files on which the protos depend (e.g. nanopb .options files). 444# prefix: A prefix to add to the source protos prior to compilation. For 445# example, a source called "foo.proto" with prefix = "nested" will be 446# compiled with protoc as "nested/foo.proto". 447# strip_prefix: Remove this prefix from the source protos. All source and 448# input files must be nested under this path. 449# python_package: Label of Python package to which to add the proto modules. 450# The .python subtarget will redirect to this package. 451# 452template("pw_proto_library") { 453 assert(defined(invoker.sources) && invoker.sources != [], 454 "pw_proto_library requires .proto source files") 455 456 if (defined(invoker.python_module_as_package)) { 457 _module_as_package = invoker.python_module_as_package 458 459 _must_be_one_source = invoker.sources 460 assert([ _must_be_one_source[0] ] == _must_be_one_source, 461 "'python_module_as_package' requires exactly one source file") 462 assert(_module_as_package != "", 463 "'python_module_as_package' cannot be be empty") 464 assert(string_split(_module_as_package, "/") == [ _module_as_package ], 465 "'python_module_as_package' cannot contain slashes") 466 assert(!defined(invoker.prefix), 467 "'prefix' cannot be provided with 'python_module_as_package'") 468 } else { 469 _module_as_package = "" 470 } 471 472 if (defined(invoker.strip_prefix)) { 473 _source_root = get_path_info(invoker.strip_prefix, "abspath") 474 } else { 475 _source_root = get_path_info(".", "abspath") 476 } 477 478 if (defined(invoker.prefix)) { 479 _prefix = invoker.prefix 480 } else { 481 _prefix = "" 482 } 483 484 _root_dir_name = "" 485 _source_names = [] 486 487 # Determine the Python package name to use for these protos. If there is no 488 # prefix, the first directory the sources are nested under is used. 489 foreach(source, rebase_path(invoker.sources, _source_root)) { 490 _path_components = [] 491 _path_components = string_split(source, "/") 492 493 if (_root_dir_name == "") { 494 _root_dir_name = _path_components[0] 495 } else { 496 assert(_prefix != "" || _path_components[0] == _root_dir_name, 497 "Unless 'prefix' is supplied, all .proto sources in a " + 498 "pw_proto_library must be in the same directory tree") 499 } 500 501 _source_names += 502 [ get_path_info(source, "dir") + "/" + get_path_info(source, "name") ] 503 } 504 505 # If the 'prefix' was supplied, use that for the package directory. 506 if (_prefix != "") { 507 _prefix_path_components = string_split(_prefix, "/") 508 _root_dir_name = _prefix_path_components[0] 509 } 510 511 assert( 512 _root_dir_name != "" && _root_dir_name != "." && _root_dir_name != "..", 513 "Either a 'prefix' must be specified or all sources must be nested " + 514 "under a common directory") 515 516 if (defined(invoker.deps)) { 517 _deps = invoker.deps 518 } else { 519 _deps = [] 520 } 521 522 _common = { 523 base_target = target_name 524 525 # This is the output directory for all files related to this proto library. 526 # Sources are mirrored to "$base_out_dir/sources" and protoc puts outputs in 527 # "$base_out_dir/$language" by default. 528 base_out_dir = 529 get_label_info(":$target_name($pw_protobuf_compiler_TOOLCHAIN)", 530 "target_gen_dir") + "/$target_name.proto_library" 531 532 compile_dir = "$base_out_dir/sources" 533 534 # Refer to the source files as the are mirrored to the output directory. 535 sources = [] 536 foreach(file, rebase_path(invoker.sources, _source_root)) { 537 sources += [ "$compile_dir/$_prefix/$file" ] 538 } 539 540 package = _root_dir_name 541 } 542 543 # For each proto target, create a file which collects the base directories of 544 # all of its dependencies to list as include paths to protoc. 545 generated_file("$target_name._includes") { 546 # Collect metadata from the include path files of each dependency. 547 548 deps = [] 549 foreach(dep, _deps) { 550 _base = get_label_info(dep, "label_no_toolchain") 551 deps += [ "$_base._includes(" + get_label_info(dep, "toolchain") + ")" ] 552 } 553 554 data_keys = [ "protoc_includes" ] 555 outputs = [ "${_common.base_out_dir}/includes.txt" ] 556 557 # Indicate this library's base directory for its dependents. 558 metadata = { 559 protoc_includes = [ rebase_path(_common.compile_dir, root_build_dir) ] 560 } 561 } 562 563 # Mirror the proto sources to the output directory with the prefix added. 564 if (current_toolchain == pw_protobuf_compiler_TOOLCHAIN) { 565 pw_mirror_tree("$target_name._sources") { 566 source_root = _source_root 567 sources = invoker.sources 568 if (defined(invoker.other_deps)) { 569 deps = invoker.other_deps 570 } 571 572 if (defined(invoker.inputs)) { 573 sources += invoker.inputs 574 } 575 576 directory = "${_common.compile_dir}/$_prefix" 577 } 578 } else { 579 not_needed(invoker, 580 [ 581 "inputs", 582 "other_deps", 583 ]) 584 } 585 586 # Enumerate all of the protobuf generator targets. 587 588 _pw_pwpb_rpc_proto_library("$target_name.pwpb_rpc") { 589 forward_variables_from(invoker, _forwarded_vars) 590 forward_variables_from(_common, "*") 591 592 deps = [] 593 foreach(dep, _deps) { 594 _base = get_label_info(dep, "label_no_toolchain") 595 deps += [ "$_base.pwpb_rpc(" + get_label_info(dep, "toolchain") + ")" ] 596 } 597 598 outputs = [] 599 foreach(name, _source_names) { 600 outputs += [ "$base_out_dir/pwpb_rpc/$_prefix/${name}.rpc.pwpb.h" ] 601 } 602 } 603 604 _pw_pwpb_proto_library("$target_name.pwpb") { 605 forward_variables_from(invoker, _forwarded_vars) 606 forward_variables_from(_common, "*") 607 608 deps = [] 609 foreach(dep, _deps) { 610 _base = get_label_info(dep, "label_no_toolchain") 611 deps += [ "$_base.pwpb(" + get_label_info(dep, "toolchain") + ")" ] 612 } 613 614 outputs = [] 615 foreach(name, _source_names) { 616 outputs += [ "$base_out_dir/pwpb/$_prefix/${name}.pwpb.h" ] 617 } 618 } 619 620 if (dir_pw_third_party_nanopb != "") { 621 _pw_nanopb_rpc_proto_library("$target_name.nanopb_rpc") { 622 forward_variables_from(invoker, _forwarded_vars) 623 forward_variables_from(_common, "*") 624 625 deps = [] 626 foreach(dep, _deps) { 627 _lbl = get_label_info(dep, "label_no_toolchain") 628 deps += [ "$_lbl.nanopb_rpc(" + get_label_info(dep, "toolchain") + ")" ] 629 } 630 631 outputs = [] 632 foreach(name, _source_names) { 633 outputs += [ "$base_out_dir/nanopb_rpc/$_prefix/${name}.rpc.pb.h" ] 634 } 635 } 636 637 _pw_nanopb_proto_library("$target_name.nanopb") { 638 forward_variables_from(invoker, _forwarded_vars) 639 forward_variables_from(_common, "*") 640 641 deps = [] 642 foreach(dep, _deps) { 643 _base = get_label_info(dep, "label_no_toolchain") 644 deps += [ "$_base.nanopb(" + get_label_info(dep, "toolchain") + ")" ] 645 } 646 647 outputs = [] 648 foreach(name, _source_names) { 649 outputs += [ 650 "$base_out_dir/nanopb/$_prefix/${name}.pb.h", 651 "$base_out_dir/nanopb/$_prefix/${name}.pb.c", 652 ] 653 } 654 } 655 } else { 656 pw_error("$target_name.nanopb_rpc") { 657 message = 658 "\$dir_pw_third_party_nanopb must be set to generate nanopb RPC code." 659 } 660 661 pw_error("$target_name.nanopb") { 662 message = 663 "\$dir_pw_third_party_nanopb must be set to compile nanopb protobufs." 664 } 665 } 666 667 _pw_raw_rpc_proto_library("$target_name.raw_rpc") { 668 forward_variables_from(invoker, _forwarded_vars) 669 forward_variables_from(_common, "*") 670 671 deps = [] 672 foreach(dep, _deps) { 673 _base = get_label_info(dep, "label_no_toolchain") 674 deps += [ "$_base.raw_rpc(" + get_label_info(dep, "toolchain") + ")" ] 675 } 676 677 outputs = [] 678 foreach(name, _source_names) { 679 outputs += [ "$base_out_dir/raw_rpc/$_prefix/${name}.raw_rpc.pb.h" ] 680 } 681 } 682 683 _pw_go_proto_library("$target_name.go") { 684 sources = _common.sources 685 686 deps = [] 687 foreach(dep, _deps) { 688 _base = get_label_info(dep, "label_no_toolchain") 689 deps += [ "$_base.go(" + get_label_info(dep, "toolchain") + ")" ] 690 } 691 692 forward_variables_from(_common, "*") 693 } 694 695 _pw_python_proto_library("$target_name.python") { 696 forward_variables_from(_common, "*") 697 forward_variables_from(invoker, [ "python_package" ]) 698 module_as_package = _module_as_package 699 700 deps = [] 701 foreach(dep, _deps) { 702 _base = get_label_info(dep, "label_no_toolchain") 703 deps += [ "$_base.python(" + get_label_info(dep, "toolchain") + ")" ] 704 } 705 706 if (module_as_package == "") { 707 _python_prefix = "$base_out_dir/python/$_prefix" 708 } else { 709 _python_prefix = "$base_out_dir/python/$module_as_package" 710 } 711 712 outputs = [] 713 foreach(name, _source_names) { 714 outputs += [ 715 "$_python_prefix/${name}_pb2.py", 716 "$_python_prefix/${name}_pb2.pyi", 717 ] 718 } 719 } 720 721 # All supported pw_protobuf generators. 722 _protobuf_generators = [ 723 "pwpb", 724 "pwpb_rpc", 725 "nanopb", 726 "nanopb_rpc", 727 "raw_rpc", 728 "go", 729 "python", 730 ] 731 732 # If the label matches the directory name, alias the subtargets to the 733 # directory (e.g. //foo:nanopb is an alias for //foo:foo.nanopb). 734 if (get_label_info(":$target_name", "name") == 735 get_path_info(get_label_info(":$target_name", "dir"), "name")) { 736 foreach(_generator, _protobuf_generators - [ "python" ]) { 737 group(_generator) { 738 public_deps = [ ":${invoker.target_name}.$_generator" ] 739 } 740 } 741 742 pw_python_group("python") { 743 python_deps = [ ":${invoker.target_name}.python" ] 744 } 745 } 746 747 # If the user attempts to use the target directly instead of one of the 748 # generator targets, run a script which prints a nice error message. 749 pw_python_action(target_name) { 750 script = string_join("/", 751 [ 752 dir_pw_protobuf_compiler, 753 "py", 754 "pw_protobuf_compiler", 755 "proto_target_invalid.py", 756 ]) 757 args = [ 758 "--target", 759 target_name, 760 "--dir", 761 get_path_info(".", "abspath"), 762 "--root", 763 "//", 764 ] + _protobuf_generators 765 stamp = true 766 } 767} 768