1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_protobuf_compiler: 2*61c4878aSAndroid Build Coastguard Worker 3*61c4878aSAndroid Build Coastguard Worker==================== 4*61c4878aSAndroid Build Coastguard Workerpw_protobuf_compiler 5*61c4878aSAndroid Build Coastguard Worker==================== 6*61c4878aSAndroid Build Coastguard WorkerThe Protobuf compiler module provides build system integration and wrapper 7*61c4878aSAndroid Build Coastguard Workerscripts for generating source code for Protobuf definitions. 8*61c4878aSAndroid Build Coastguard Worker 9*61c4878aSAndroid Build Coastguard Worker-------------------- 10*61c4878aSAndroid Build Coastguard WorkerProtobuf compilation 11*61c4878aSAndroid Build Coastguard Worker-------------------- 12*61c4878aSAndroid Build Coastguard Worker 13*61c4878aSAndroid Build Coastguard WorkerGenerator support 14*61c4878aSAndroid Build Coastguard Worker================= 15*61c4878aSAndroid Build Coastguard WorkerProtobuf code generation is currently supported for the following generators: 16*61c4878aSAndroid Build Coastguard Worker 17*61c4878aSAndroid Build Coastguard Worker+-------------+----------------+-----------------------------------------------+ 18*61c4878aSAndroid Build Coastguard Worker| Generator | Code | Notes | 19*61c4878aSAndroid Build Coastguard Worker+-------------+----------------+-----------------------------------------------+ 20*61c4878aSAndroid Build Coastguard Worker| pw_protobuf | ``pwpb`` | Compiles using ``pw_protobuf``. | 21*61c4878aSAndroid Build Coastguard Worker+-------------+----------------+-----------------------------------------------+ 22*61c4878aSAndroid Build Coastguard Worker| pw_protobuf | ``pwpb_rpc`` | Compiles pw_rpc service and client code for | 23*61c4878aSAndroid Build Coastguard Worker| RPC | | ``pw_protobuf``. | 24*61c4878aSAndroid Build Coastguard Worker+-------------+----------------+-----------------------------------------------+ 25*61c4878aSAndroid Build Coastguard Worker| Nanopb | ``nanopb`` | Compiles using Nanopb. The build argument | 26*61c4878aSAndroid Build Coastguard Worker| | | ``dir_pw_third_party_nanopb`` must be set to | 27*61c4878aSAndroid Build Coastguard Worker| | | point to a local nanopb installation. | 28*61c4878aSAndroid Build Coastguard Worker+-------------+----------------+-----------------------------------------------+ 29*61c4878aSAndroid Build Coastguard Worker| Nanopb RPC | ``nanopb_rpc`` | Compiles pw_rpc service and client code for | 30*61c4878aSAndroid Build Coastguard Worker| | | nanopb. Requires a nanopb installation. | 31*61c4878aSAndroid Build Coastguard Worker+-------------+----------------+-----------------------------------------------+ 32*61c4878aSAndroid Build Coastguard Worker| Raw RPC | ``raw_rpc`` | Compiles raw binary pw_rpc service code. | 33*61c4878aSAndroid Build Coastguard Worker+-------------+----------------+-----------------------------------------------+ 34*61c4878aSAndroid Build Coastguard Worker| Go | ``go`` | Compiles using the standard Go protobuf | 35*61c4878aSAndroid Build Coastguard Worker| | | plugin with gRPC service support. | 36*61c4878aSAndroid Build Coastguard Worker+-------------+----------------+-----------------------------------------------+ 37*61c4878aSAndroid Build Coastguard Worker| Python | ``python`` | Compiles using the standard Python protobuf | 38*61c4878aSAndroid Build Coastguard Worker| | | plugin, creating a ``pw_python_package``. | 39*61c4878aSAndroid Build Coastguard Worker+-------------+----------------+-----------------------------------------------+ 40*61c4878aSAndroid Build Coastguard Worker| Typescript | ``typescript`` | Compilation is supported in Bazel via | 41*61c4878aSAndroid Build Coastguard Worker| | | @rules_proto_grpc. ProtoCollection provides | 42*61c4878aSAndroid Build Coastguard Worker| | | convience methods for proto descriptors. | 43*61c4878aSAndroid Build Coastguard Worker+-------------+----------------+-----------------------------------------------+ 44*61c4878aSAndroid Build Coastguard Worker 45*61c4878aSAndroid Build Coastguard WorkerGN template 46*61c4878aSAndroid Build Coastguard Worker=========== 47*61c4878aSAndroid Build Coastguard WorkerThis module provides a ``pw_proto_library`` GN template that defines a 48*61c4878aSAndroid Build Coastguard Workercollection of protobuf files that should be compiled together. The template 49*61c4878aSAndroid Build Coastguard Workercreates a sub-target for each supported generator, named 50*61c4878aSAndroid Build Coastguard Worker``<target_name>.<generator>``. These sub-targets generate their respective 51*61c4878aSAndroid Build Coastguard Workerprotobuf code, and expose it to the build system appropriately (e.g. a 52*61c4878aSAndroid Build Coastguard Worker``pw_source_set`` for C/C++). 53*61c4878aSAndroid Build Coastguard Worker 54*61c4878aSAndroid Build Coastguard WorkerFor example, given the following target: 55*61c4878aSAndroid Build Coastguard Worker 56*61c4878aSAndroid Build Coastguard Worker.. code-block:: 57*61c4878aSAndroid Build Coastguard Worker 58*61c4878aSAndroid Build Coastguard Worker pw_proto_library("test_protos") { 59*61c4878aSAndroid Build Coastguard Worker sources = [ "my_test_protos/test.proto" ] 60*61c4878aSAndroid Build Coastguard Worker } 61*61c4878aSAndroid Build Coastguard Worker 62*61c4878aSAndroid Build Coastguard Worker``test_protos.pwpb`` compiles code for pw_protobuf, and ``test_protos.nanopb`` 63*61c4878aSAndroid Build Coastguard Workercompiles using Nanopb (if it's installed). 64*61c4878aSAndroid Build Coastguard Worker 65*61c4878aSAndroid Build Coastguard WorkerProtobuf code is only generated when a generator sub-target is listed as a 66*61c4878aSAndroid Build Coastguard Workerdependency of another GN target. 67*61c4878aSAndroid Build Coastguard Worker 68*61c4878aSAndroid Build Coastguard WorkerGN permits using abbreviated labels when the target name matches the directory 69*61c4878aSAndroid Build Coastguard Workername (e.g. ``//foo`` for ``//foo:foo``). For consistency with this, the 70*61c4878aSAndroid Build Coastguard Workersub-targets for each generator are aliased to the directory when the target name 71*61c4878aSAndroid Build Coastguard Workeris the same. For example, these two labels are equivalent: 72*61c4878aSAndroid Build Coastguard Worker 73*61c4878aSAndroid Build Coastguard Worker.. code-block:: 74*61c4878aSAndroid Build Coastguard Worker 75*61c4878aSAndroid Build Coastguard Worker //path/to/my_protos:my_protos.pwpb 76*61c4878aSAndroid Build Coastguard Worker //path/to/my_protos:pwpb 77*61c4878aSAndroid Build Coastguard Worker 78*61c4878aSAndroid Build Coastguard Worker``pw_python_package`` subtargets are also available on the ``python`` subtarget: 79*61c4878aSAndroid Build Coastguard Worker 80*61c4878aSAndroid Build Coastguard Worker.. code-block:: 81*61c4878aSAndroid Build Coastguard Worker 82*61c4878aSAndroid Build Coastguard Worker //path/to/my_protos:my_protos.python.lint 83*61c4878aSAndroid Build Coastguard Worker //path/to/my_protos:python.lint 84*61c4878aSAndroid Build Coastguard Worker 85*61c4878aSAndroid Build Coastguard Worker**Supported Codegen** 86*61c4878aSAndroid Build Coastguard Worker 87*61c4878aSAndroid Build Coastguard WorkerGN supports the following compiled proto libraries via the specified 88*61c4878aSAndroid Build Coastguard Workersub-targets generated by a ``pw_proto_library``. 89*61c4878aSAndroid Build Coastguard Worker 90*61c4878aSAndroid Build Coastguard Worker* ``${target_name}.pwpb`` - Generated C++ pw_protobuf code 91*61c4878aSAndroid Build Coastguard Worker* ``${target_name}.pwpb_rpc`` - Generated C++ pw_protobuf pw_rpc code 92*61c4878aSAndroid Build Coastguard Worker* ``${target_name}.nanopb`` - Generated C++ nanopb code (requires Nanopb) 93*61c4878aSAndroid Build Coastguard Worker* ``${target_name}.nanopb_rpc`` - Generated C++ Nanopb pw_rpc code (requires 94*61c4878aSAndroid Build Coastguard Worker Nanopb) 95*61c4878aSAndroid Build Coastguard Worker* ``${target_name}.raw_rpc`` - Generated C++ raw pw_rpc code (no protobuf 96*61c4878aSAndroid Build Coastguard Worker library) 97*61c4878aSAndroid Build Coastguard Worker* ``${target_name}.go`` - Generated GO protobuf libraries 98*61c4878aSAndroid Build Coastguard Worker* ``${target_name}.python`` - Generated Python protobuf libraries 99*61c4878aSAndroid Build Coastguard Worker 100*61c4878aSAndroid Build Coastguard Worker**Arguments** 101*61c4878aSAndroid Build Coastguard Worker 102*61c4878aSAndroid Build Coastguard Worker* ``sources``: List of input .proto files. 103*61c4878aSAndroid Build Coastguard Worker* ``deps``: List of other pw_proto_library dependencies. 104*61c4878aSAndroid Build Coastguard Worker* ``other_deps``: List of other non-proto dependencies. 105*61c4878aSAndroid Build Coastguard Worker* ``inputs``: Other files on which the protos depend (e.g. nanopb ``.options`` 106*61c4878aSAndroid Build Coastguard Worker files). 107*61c4878aSAndroid Build Coastguard Worker* ``prefix``: A prefix to add to the source protos prior to compilation. For 108*61c4878aSAndroid Build Coastguard Worker example, a source called ``"foo.proto"`` with ``prefix = "nested"`` will be 109*61c4878aSAndroid Build Coastguard Worker compiled with protoc as ``"nested/foo.proto"``. 110*61c4878aSAndroid Build Coastguard Worker* ``strip_prefix``: Remove this prefix from the source protos. All source and 111*61c4878aSAndroid Build Coastguard Worker input files must be nested under this path. 112*61c4878aSAndroid Build Coastguard Worker* ``python_package``: Label of Python package to which to add the proto modules. 113*61c4878aSAndroid Build Coastguard Worker The .python subtarget will redirect to this package. 114*61c4878aSAndroid Build Coastguard Worker* ``enabled_targets``: List of sub-targets to enable (see Supported Codegen), 115*61c4878aSAndroid Build Coastguard Worker e.g. ``["pwpb", "raw_rpc"]``. By default, all sub-targets are enabled. The 116*61c4878aSAndroid Build Coastguard Worker enabled sub-targets are built only as requested by the build system, but it 117*61c4878aSAndroid Build Coastguard Worker may be necessary to explicitly disable an unused sub-target if it conflicts 118*61c4878aSAndroid Build Coastguard Worker with another target in the same package. (For example, ``nanopb`` codegen can 119*61c4878aSAndroid Build Coastguard Worker conflict with the default C++ codegen provided by ``protoc``.) 120*61c4878aSAndroid Build Coastguard Worker TODO: b/235132083 - Remove this argument once we've removed the file-name 121*61c4878aSAndroid Build Coastguard Worker conflict between nanopb and protoc code generators. 122*61c4878aSAndroid Build Coastguard Worker 123*61c4878aSAndroid Build Coastguard Worker**Example** 124*61c4878aSAndroid Build Coastguard Worker 125*61c4878aSAndroid Build Coastguard Worker.. code-block:: 126*61c4878aSAndroid Build Coastguard Worker 127*61c4878aSAndroid Build Coastguard Worker import("$dir_pw_protobuf_compiler/proto.gni") 128*61c4878aSAndroid Build Coastguard Worker 129*61c4878aSAndroid Build Coastguard Worker pw_proto_library("my_protos") { 130*61c4878aSAndroid Build Coastguard Worker sources = [ 131*61c4878aSAndroid Build Coastguard Worker "my_protos/foo.proto", 132*61c4878aSAndroid Build Coastguard Worker "my_protos/bar.proto", 133*61c4878aSAndroid Build Coastguard Worker ] 134*61c4878aSAndroid Build Coastguard Worker } 135*61c4878aSAndroid Build Coastguard Worker 136*61c4878aSAndroid Build Coastguard Worker pw_proto_library("my_other_protos") { 137*61c4878aSAndroid Build Coastguard Worker sources = [ "some/other/path/baz.proto" ] # imports foo.proto 138*61c4878aSAndroid Build Coastguard Worker 139*61c4878aSAndroid Build Coastguard Worker # This removes the "some/other/path" prefix from the proto files. 140*61c4878aSAndroid Build Coastguard Worker strip_prefix = "some/other/path" 141*61c4878aSAndroid Build Coastguard Worker 142*61c4878aSAndroid Build Coastguard Worker # This adds the "my_other_protos/" prefix to the proto files. 143*61c4878aSAndroid Build Coastguard Worker prefix = "my_other_protos" 144*61c4878aSAndroid Build Coastguard Worker 145*61c4878aSAndroid Build Coastguard Worker # Proto libraries depend on other proto libraries directly. 146*61c4878aSAndroid Build Coastguard Worker deps = [ ":my_protos" ] 147*61c4878aSAndroid Build Coastguard Worker } 148*61c4878aSAndroid Build Coastguard Worker 149*61c4878aSAndroid Build Coastguard Worker source_set("my_cc_code") { 150*61c4878aSAndroid Build Coastguard Worker sources = [ 151*61c4878aSAndroid Build Coastguard Worker "foo.cc", 152*61c4878aSAndroid Build Coastguard Worker "bar.cc", 153*61c4878aSAndroid Build Coastguard Worker "baz.cc", 154*61c4878aSAndroid Build Coastguard Worker ] 155*61c4878aSAndroid Build Coastguard Worker 156*61c4878aSAndroid Build Coastguard Worker # When depending on protos in a source_set, specify the generator suffix. 157*61c4878aSAndroid Build Coastguard Worker deps = [ ":my_other_protos.pwpb" ] 158*61c4878aSAndroid Build Coastguard Worker } 159*61c4878aSAndroid Build Coastguard Worker 160*61c4878aSAndroid Build Coastguard WorkerFrom C++, ``baz.proto`` included as follows: 161*61c4878aSAndroid Build Coastguard Worker 162*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 163*61c4878aSAndroid Build Coastguard Worker 164*61c4878aSAndroid Build Coastguard Worker #include "my_other_protos/baz.pwpb.h" 165*61c4878aSAndroid Build Coastguard Worker 166*61c4878aSAndroid Build Coastguard WorkerFrom Python, ``baz.proto`` is imported as follows: 167*61c4878aSAndroid Build Coastguard Worker 168*61c4878aSAndroid Build Coastguard Worker.. code-block:: python 169*61c4878aSAndroid Build Coastguard Worker 170*61c4878aSAndroid Build Coastguard Worker from my_other_protos import baz_pb2 171*61c4878aSAndroid Build Coastguard Worker 172*61c4878aSAndroid Build Coastguard WorkerProto file structure 173*61c4878aSAndroid Build Coastguard Worker-------------------- 174*61c4878aSAndroid Build Coastguard WorkerProtobuf source files must be nested under another directory when they are 175*61c4878aSAndroid Build Coastguard Workercompiled. This ensures that they can be packaged properly in Python. 176*61c4878aSAndroid Build Coastguard Worker 177*61c4878aSAndroid Build Coastguard WorkerUsing ``prefix`` and ``strip_prefix`` together allows remapping proto files to 178*61c4878aSAndroid Build Coastguard Workera completely different path. This can be useful when working with protos defined 179*61c4878aSAndroid Build Coastguard Workerin external libraries. For example, consider this proto library: 180*61c4878aSAndroid Build Coastguard Worker 181*61c4878aSAndroid Build Coastguard Worker.. code-block:: 182*61c4878aSAndroid Build Coastguard Worker 183*61c4878aSAndroid Build Coastguard Worker pw_proto_library("external_protos") { 184*61c4878aSAndroid Build Coastguard Worker sources = [ 185*61c4878aSAndroid Build Coastguard Worker "//other/external/some_library/src/protos/alpha.proto", 186*61c4878aSAndroid Build Coastguard Worker "//other/external/some_library/src/protos/beta.proto, 187*61c4878aSAndroid Build Coastguard Worker "//other/external/some_library/src/protos/internal/gamma.proto", 188*61c4878aSAndroid Build Coastguard Worker ] 189*61c4878aSAndroid Build Coastguard Worker strip_prefix = "//other/external/some_library/src/protos" 190*61c4878aSAndroid Build Coastguard Worker prefix = "some_library" 191*61c4878aSAndroid Build Coastguard Worker } 192*61c4878aSAndroid Build Coastguard Worker 193*61c4878aSAndroid Build Coastguard WorkerThese protos will be compiled by protoc as if they were in this file structure: 194*61c4878aSAndroid Build Coastguard Worker 195*61c4878aSAndroid Build Coastguard Worker.. code-block:: 196*61c4878aSAndroid Build Coastguard Worker 197*61c4878aSAndroid Build Coastguard Worker some_library/ 198*61c4878aSAndroid Build Coastguard Worker ├── alpha.proto 199*61c4878aSAndroid Build Coastguard Worker ├── beta.proto 200*61c4878aSAndroid Build Coastguard Worker └── internal 201*61c4878aSAndroid Build Coastguard Worker └── gamma.proto 202*61c4878aSAndroid Build Coastguard Worker 203*61c4878aSAndroid Build Coastguard Worker.. _module-pw_protobuf_compiler-add-to-python-package: 204*61c4878aSAndroid Build Coastguard Worker 205*61c4878aSAndroid Build Coastguard WorkerAdding Python proto modules to an existing package 206*61c4878aSAndroid Build Coastguard Worker-------------------------------------------------- 207*61c4878aSAndroid Build Coastguard WorkerBy default, generated Python proto modules are organized into their own Python 208*61c4878aSAndroid Build Coastguard Workerpackage. These proto modules can instead be added to an existing Python package 209*61c4878aSAndroid Build Coastguard Workerdeclared with ``pw_python_package``. This is done by setting the 210*61c4878aSAndroid Build Coastguard Worker``python_package`` argument on the ``pw_proto_library`` and the 211*61c4878aSAndroid Build Coastguard Worker``proto_library`` argument on the ``pw_python_package``. 212*61c4878aSAndroid Build Coastguard Worker 213*61c4878aSAndroid Build Coastguard WorkerFor example, the protos declared in ``my_protos`` will be nested in the Python 214*61c4878aSAndroid Build Coastguard Workerpackage declared by ``my_package``. 215*61c4878aSAndroid Build Coastguard Worker 216*61c4878aSAndroid Build Coastguard Worker.. code-block:: 217*61c4878aSAndroid Build Coastguard Worker 218*61c4878aSAndroid Build Coastguard Worker pw_proto_library("my_protos") { 219*61c4878aSAndroid Build Coastguard Worker sources = [ "hello.proto ] 220*61c4878aSAndroid Build Coastguard Worker prefix = "foo" 221*61c4878aSAndroid Build Coastguard Worker python_package = ":my_package" 222*61c4878aSAndroid Build Coastguard Worker } 223*61c4878aSAndroid Build Coastguard Worker 224*61c4878aSAndroid Build Coastguard Worker pw_python_pacakge("my_package") { 225*61c4878aSAndroid Build Coastguard Worker generate_setup = { 226*61c4878aSAndroid Build Coastguard Worker metadata = { 227*61c4878aSAndroid Build Coastguard Worker name = "foo" 228*61c4878aSAndroid Build Coastguard Worker version = "1.0" 229*61c4878aSAndroid Build Coastguard Worker } 230*61c4878aSAndroid Build Coastguard Worker } 231*61c4878aSAndroid Build Coastguard Worker 232*61c4878aSAndroid Build Coastguard Worker sources = [ "foo/cool_module.py" ] 233*61c4878aSAndroid Build Coastguard Worker proto_library = ":my_protos" 234*61c4878aSAndroid Build Coastguard Worker } 235*61c4878aSAndroid Build Coastguard Worker 236*61c4878aSAndroid Build Coastguard WorkerThe ``hello_pb2.py`` proto module can be used alongside other files in the 237*61c4878aSAndroid Build Coastguard Worker``foo`` package. 238*61c4878aSAndroid Build Coastguard Worker 239*61c4878aSAndroid Build Coastguard Worker.. code-block:: python 240*61c4878aSAndroid Build Coastguard Worker 241*61c4878aSAndroid Build Coastguard Worker from foo import cool_module, hello_pb2 242*61c4878aSAndroid Build Coastguard Worker 243*61c4878aSAndroid Build Coastguard WorkerWorking with externally defined protos 244*61c4878aSAndroid Build Coastguard Worker-------------------------------------- 245*61c4878aSAndroid Build Coastguard Worker``pw_proto_library`` targets may be used to build ``.proto`` sources from 246*61c4878aSAndroid Build Coastguard Workerexisting projects. In these cases, it may be necessary to supply the 247*61c4878aSAndroid Build Coastguard Worker``strip_prefix`` argument, which specifies the protobuf include path to use for 248*61c4878aSAndroid Build Coastguard Worker``protoc``. If only a single external protobuf is being compiled, the 249*61c4878aSAndroid Build Coastguard Worker``python_module_as_package`` option can be used to override the requirement that 250*61c4878aSAndroid Build Coastguard Workerthe protobuf be nested under a directory. This option generates a Python package 251*61c4878aSAndroid Build Coastguard Workerwith the same name as the proto file, so that the generated proto can be 252*61c4878aSAndroid Build Coastguard Workerimported as if it were a standalone Python module. 253*61c4878aSAndroid Build Coastguard Worker 254*61c4878aSAndroid Build Coastguard WorkerFor example, the ``pw_proto_library`` target for Nanopb sets 255*61c4878aSAndroid Build Coastguard Worker``python_module_as_package`` to ``nanopb_pb2``. 256*61c4878aSAndroid Build Coastguard Worker 257*61c4878aSAndroid Build Coastguard Worker.. code-block:: 258*61c4878aSAndroid Build Coastguard Worker 259*61c4878aSAndroid Build Coastguard Worker pw_proto_library("proto") { 260*61c4878aSAndroid Build Coastguard Worker strip_prefix = "$dir_pw_third_party_nanopb/generator/proto" 261*61c4878aSAndroid Build Coastguard Worker sources = [ "$dir_pw_third_party_nanopb/generator/proto/nanopb.proto" ] 262*61c4878aSAndroid Build Coastguard Worker python_module_as_package = "nanopb_pb2" 263*61c4878aSAndroid Build Coastguard Worker } 264*61c4878aSAndroid Build Coastguard Worker 265*61c4878aSAndroid Build Coastguard WorkerIn Python, this makes ``nanopb.proto`` available as ``import nanopb_pb2`` via 266*61c4878aSAndroid Build Coastguard Workerthe ``nanopb_pb2`` Python package. In C++, ``nanopb.proto`` is accessed as 267*61c4878aSAndroid Build Coastguard Worker``#include "nanopb.pwpb.h"``. 268*61c4878aSAndroid Build Coastguard Worker 269*61c4878aSAndroid Build Coastguard WorkerThe ``python_module_as_package`` feature should only be used when absolutely 270*61c4878aSAndroid Build Coastguard Workernecessary --- for example, to support proto files that include 271*61c4878aSAndroid Build Coastguard Worker``import "nanopb.proto"``. 272*61c4878aSAndroid Build Coastguard Worker 273*61c4878aSAndroid Build Coastguard WorkerSpecifying a custom ``protoc`` 274*61c4878aSAndroid Build Coastguard Worker------------------------------ 275*61c4878aSAndroid Build Coastguard WorkerIf your build needs to use a custom build of ``protoc`` rather than the one 276*61c4878aSAndroid Build Coastguard Workersupplied by pigweed it can be specified by setting 277*61c4878aSAndroid Build Coastguard Worker``pw_protobuf_compiler_PROTOC_TARGET`` to a GN target that produces a ``protoc`` 278*61c4878aSAndroid Build Coastguard Workerexecutable and ``pw_protobuf_compiler_PROTOC_BINARY`` to the path, relative to 279*61c4878aSAndroid Build Coastguard Worker``root_build_dir``, of the ``protoc`` executable. 280*61c4878aSAndroid Build Coastguard Worker 281*61c4878aSAndroid Build Coastguard WorkerFor all ``protoc`` invocations, the build will add a dependency on that target 282*61c4878aSAndroid Build Coastguard Workerand will invoke that executable. 283*61c4878aSAndroid Build Coastguard Worker 284*61c4878aSAndroid Build Coastguard Worker.. _module-pw_protobuf_compiler-cmake: 285*61c4878aSAndroid Build Coastguard Worker 286*61c4878aSAndroid Build Coastguard WorkerCMake 287*61c4878aSAndroid Build Coastguard Worker===== 288*61c4878aSAndroid Build Coastguard WorkerCMake provides a ``pw_proto_library`` function with similar features as the 289*61c4878aSAndroid Build Coastguard WorkerGN template. The CMake build only supports building firmware code, so 290*61c4878aSAndroid Build Coastguard Worker``pw_proto_library`` does not generate a Python package. 291*61c4878aSAndroid Build Coastguard Worker 292*61c4878aSAndroid Build Coastguard Worker**Arguments** 293*61c4878aSAndroid Build Coastguard Worker 294*61c4878aSAndroid Build Coastguard Worker* ``NAME``: the base name of the libraries to create 295*61c4878aSAndroid Build Coastguard Worker* ``SOURCES``: .proto source files 296*61c4878aSAndroid Build Coastguard Worker* ``DEPS``: dependencies on other ``pw_proto_library`` targets 297*61c4878aSAndroid Build Coastguard Worker* ``PREFIX``: prefix add to the proto files 298*61c4878aSAndroid Build Coastguard Worker* ``STRIP_PREFIX``: prefix to remove from the proto files 299*61c4878aSAndroid Build Coastguard Worker* ``INPUTS``: files to include along with the .proto files (such as Nanopb 300*61c4878aSAndroid Build Coastguard Worker .options files) 301*61c4878aSAndroid Build Coastguard Worker 302*61c4878aSAndroid Build Coastguard Worker**Example** 303*61c4878aSAndroid Build Coastguard Worker 304*61c4878aSAndroid Build Coastguard Worker.. code-block:: cmake 305*61c4878aSAndroid Build Coastguard Worker 306*61c4878aSAndroid Build Coastguard Worker include($ENV{PW_ROOT}/pw_build/pigweed.cmake) 307*61c4878aSAndroid Build Coastguard Worker include($ENV{PW_ROOT}/pw_protobuf_compiler/proto.cmake) 308*61c4878aSAndroid Build Coastguard Worker 309*61c4878aSAndroid Build Coastguard Worker pw_proto_library(my_module.my_protos 310*61c4878aSAndroid Build Coastguard Worker SOURCES 311*61c4878aSAndroid Build Coastguard Worker my_protos/foo.proto 312*61c4878aSAndroid Build Coastguard Worker my_protos/bar.proto 313*61c4878aSAndroid Build Coastguard Worker ) 314*61c4878aSAndroid Build Coastguard Worker 315*61c4878aSAndroid Build Coastguard Worker pw_proto_library(my_module.my_protos 316*61c4878aSAndroid Build Coastguard Worker SOURCES 317*61c4878aSAndroid Build Coastguard Worker my_protos/foo.proto 318*61c4878aSAndroid Build Coastguard Worker my_protos/bar.proto 319*61c4878aSAndroid Build Coastguard Worker ) 320*61c4878aSAndroid Build Coastguard Worker 321*61c4878aSAndroid Build Coastguard Worker pw_proto_library(my_module.my_other_protos 322*61c4878aSAndroid Build Coastguard Worker SOURCES 323*61c4878aSAndroid Build Coastguard Worker some/other/path/baz.proto # imports foo.proto 324*61c4878aSAndroid Build Coastguard Worker 325*61c4878aSAndroid Build Coastguard Worker # This removes the "some/other/path" prefix from the proto files. 326*61c4878aSAndroid Build Coastguard Worker STRIP_PREFIX 327*61c4878aSAndroid Build Coastguard Worker some/other/path 328*61c4878aSAndroid Build Coastguard Worker 329*61c4878aSAndroid Build Coastguard Worker # This adds the "my_other_protos/" prefix to the proto files. 330*61c4878aSAndroid Build Coastguard Worker PREFIX 331*61c4878aSAndroid Build Coastguard Worker my_other_protos 332*61c4878aSAndroid Build Coastguard Worker 333*61c4878aSAndroid Build Coastguard Worker # Proto libraries depend on other proto libraries directly. 334*61c4878aSAndroid Build Coastguard Worker DEPS 335*61c4878aSAndroid Build Coastguard Worker my_module.my_protos 336*61c4878aSAndroid Build Coastguard Worker ) 337*61c4878aSAndroid Build Coastguard Worker 338*61c4878aSAndroid Build Coastguard Worker add_library(my_module.my_cc_code 339*61c4878aSAndroid Build Coastguard Worker foo.cc 340*61c4878aSAndroid Build Coastguard Worker bar.cc 341*61c4878aSAndroid Build Coastguard Worker baz.cc 342*61c4878aSAndroid Build Coastguard Worker ) 343*61c4878aSAndroid Build Coastguard Worker 344*61c4878aSAndroid Build Coastguard Worker # When depending on protos in a source_set, specify the generator suffix. 345*61c4878aSAndroid Build Coastguard Worker target_link_libraries(my_module.my_cc_code PUBLIC 346*61c4878aSAndroid Build Coastguard Worker my_module.my_other_protos.pwpb 347*61c4878aSAndroid Build Coastguard Worker ) 348*61c4878aSAndroid Build Coastguard Worker 349*61c4878aSAndroid Build Coastguard WorkerThese proto files are accessed in C++ the same as in the GN build: 350*61c4878aSAndroid Build Coastguard Worker 351*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 352*61c4878aSAndroid Build Coastguard Worker 353*61c4878aSAndroid Build Coastguard Worker #include "my_other_protos/baz.pwpb.h" 354*61c4878aSAndroid Build Coastguard Worker 355*61c4878aSAndroid Build Coastguard Worker**Supported Codegen** 356*61c4878aSAndroid Build Coastguard Worker 357*61c4878aSAndroid Build Coastguard WorkerCMake supports the following compiled proto libraries via the specified 358*61c4878aSAndroid Build Coastguard Workersub-targets generated by a ``pw_proto_library``. 359*61c4878aSAndroid Build Coastguard Worker 360*61c4878aSAndroid Build Coastguard Worker* ``${NAME}.pwpb`` - Generated C++ pw_protobuf code 361*61c4878aSAndroid Build Coastguard Worker* ``${NAME}.pwpb_rpc`` - Generated C++ pw_protobuf pw_rpc code 362*61c4878aSAndroid Build Coastguard Worker* ``${NAME}.nanopb`` - Generated C++ nanopb code (requires Nanopb) 363*61c4878aSAndroid Build Coastguard Worker* ``${NAME}.nanopb_rpc`` - Generated C++ Nanopb pw_rpc code (requires Nanopb) 364*61c4878aSAndroid Build Coastguard Worker* ``${NAME}.raw_rpc`` - Generated C++ raw pw_rpc code (no protobuf library) 365*61c4878aSAndroid Build Coastguard Worker 366*61c4878aSAndroid Build Coastguard WorkerBazel 367*61c4878aSAndroid Build Coastguard Worker===== 368*61c4878aSAndroid Build Coastguard WorkerIn Bazel we provide a set rules with similar features to the GN templates: 369*61c4878aSAndroid Build Coastguard Worker 370*61c4878aSAndroid Build Coastguard Worker* ``pwpb_proto_library`` - Generated C++ pw_protobuf code 371*61c4878aSAndroid Build Coastguard Worker* ``pwpb_rpc_proto_library`` - Generated C++ pw_protobuf pw_rpc code 372*61c4878aSAndroid Build Coastguard Worker* ``raw_rpc_proto_library`` - Generated C++ raw pw_rpc code (no protobuf library) 373*61c4878aSAndroid Build Coastguard Worker* ``nanopb_proto_library`` - Generated C++ nanopb code 374*61c4878aSAndroid Build Coastguard Worker* ``nanopb_rpc_proto_library`` - Generated C++ Nanopb pw_rpc code 375*61c4878aSAndroid Build Coastguard Worker 376*61c4878aSAndroid Build Coastguard WorkerThese rules build the corresponding firmware code; there are no rules for 377*61c4878aSAndroid Build Coastguard Workergenerating Python libraries. The Bazel rules differ slightly compared to the GN 378*61c4878aSAndroid Build Coastguard Workerbuild to be more in line with what would be considered idiomatic in Bazel. 379*61c4878aSAndroid Build Coastguard Worker 380*61c4878aSAndroid Build Coastguard WorkerTo use Pigweeds Protobuf rules you must first pull in the required dependencies 381*61c4878aSAndroid Build Coastguard Workerinto your Bazel WORKSPACE file. e.g. 382*61c4878aSAndroid Build Coastguard Worker 383*61c4878aSAndroid Build Coastguard Worker.. code-block:: python 384*61c4878aSAndroid Build Coastguard Worker 385*61c4878aSAndroid Build Coastguard Worker # WORKSPACE ... 386*61c4878aSAndroid Build Coastguard Worker load("@pigweed//pw_protobuf_compiler:deps.bzl", "pw_protobuf_dependencies") 387*61c4878aSAndroid Build Coastguard Worker pw_protobuf_dependencies() 388*61c4878aSAndroid Build Coastguard Worker 389*61c4878aSAndroid Build Coastguard WorkerBazel uses a different set of rules to manage proto files than it does to 390*61c4878aSAndroid Build Coastguard Workercompile them. e.g. 391*61c4878aSAndroid Build Coastguard Worker 392*61c4878aSAndroid Build Coastguard Worker.. code-block:: python 393*61c4878aSAndroid Build Coastguard Worker 394*61c4878aSAndroid Build Coastguard Worker # BUILD ... 395*61c4878aSAndroid Build Coastguard Worker load("@rules_proto//proto:defs.bzl", "proto_library") 396*61c4878aSAndroid Build Coastguard Worker load("@pigweed//pw_protobuf_compiler:pw_proto_library.bzl", 397*61c4878aSAndroid Build Coastguard Worker "nanopb_proto_library", 398*61c4878aSAndroid Build Coastguard Worker "nanopb_rpc_proto_library", 399*61c4878aSAndroid Build Coastguard Worker "pwpb_proto_library", 400*61c4878aSAndroid Build Coastguard Worker "raw_rpc_proto_library", 401*61c4878aSAndroid Build Coastguard Worker ) 402*61c4878aSAndroid Build Coastguard Worker 403*61c4878aSAndroid Build Coastguard Worker # Manages proto sources and dependencies. 404*61c4878aSAndroid Build Coastguard Worker proto_library( 405*61c4878aSAndroid Build Coastguard Worker name = "my_proto", 406*61c4878aSAndroid Build Coastguard Worker srcs = [ 407*61c4878aSAndroid Build Coastguard Worker "my_protos/foo.proto", 408*61c4878aSAndroid Build Coastguard Worker "my_protos/bar.proto", 409*61c4878aSAndroid Build Coastguard Worker ] 410*61c4878aSAndroid Build Coastguard Worker ) 411*61c4878aSAndroid Build Coastguard Worker 412*61c4878aSAndroid Build Coastguard Worker # Compiles dependent protos to C++. 413*61c4878aSAndroid Build Coastguard Worker pwpb_proto_library( 414*61c4878aSAndroid Build Coastguard Worker name = "my_proto_pwpb", 415*61c4878aSAndroid Build Coastguard Worker deps = [":my_proto"], 416*61c4878aSAndroid Build Coastguard Worker ) 417*61c4878aSAndroid Build Coastguard Worker 418*61c4878aSAndroid Build Coastguard Worker nanopb_proto_library( 419*61c4878aSAndroid Build Coastguard Worker name = "my_proto_nanopb", 420*61c4878aSAndroid Build Coastguard Worker deps = [":my_proto"], 421*61c4878aSAndroid Build Coastguard Worker ) 422*61c4878aSAndroid Build Coastguard Worker 423*61c4878aSAndroid Build Coastguard Worker raw_rpc_proto_library( 424*61c4878aSAndroid Build Coastguard Worker name = "my_proto_raw_rpc", 425*61c4878aSAndroid Build Coastguard Worker deps = [":my_proto"], 426*61c4878aSAndroid Build Coastguard Worker ) 427*61c4878aSAndroid Build Coastguard Worker 428*61c4878aSAndroid Build Coastguard Worker nanopb_rpc_proto_library( 429*61c4878aSAndroid Build Coastguard Worker name = "my_proto_nanopb_rpc", 430*61c4878aSAndroid Build Coastguard Worker nanopb_proto_library_deps = [":my_proto_nanopb"], 431*61c4878aSAndroid Build Coastguard Worker deps = [":my_proto"], 432*61c4878aSAndroid Build Coastguard Worker ) 433*61c4878aSAndroid Build Coastguard Worker 434*61c4878aSAndroid Build Coastguard Worker # Library that depends on only pw_protobuf generated proto targets. 435*61c4878aSAndroid Build Coastguard Worker cc_library( 436*61c4878aSAndroid Build Coastguard Worker name = "my_proto_only_lib", 437*61c4878aSAndroid Build Coastguard Worker srcs = ["my/proto_only.cc"], 438*61c4878aSAndroid Build Coastguard Worker deps = [":my_proto_pwpb"], 439*61c4878aSAndroid Build Coastguard Worker ) 440*61c4878aSAndroid Build Coastguard Worker 441*61c4878aSAndroid Build Coastguard Worker # Library that depends on only Nanopb generated proto targets. 442*61c4878aSAndroid Build Coastguard Worker cc_library( 443*61c4878aSAndroid Build Coastguard Worker name = "my_nanopb_only_lib", 444*61c4878aSAndroid Build Coastguard Worker srcs = ["my/nanopb_only.cc"], 445*61c4878aSAndroid Build Coastguard Worker deps = [":my_proto_nanopb"], 446*61c4878aSAndroid Build Coastguard Worker ) 447*61c4878aSAndroid Build Coastguard Worker 448*61c4878aSAndroid Build Coastguard Worker # Library that depends on pw_protobuf and pw_rpc/raw. 449*61c4878aSAndroid Build Coastguard Worker cc_library( 450*61c4878aSAndroid Build Coastguard Worker name = "my_raw_rpc_lib", 451*61c4878aSAndroid Build Coastguard Worker srcs = ["my/raw_rpc.cc"], 452*61c4878aSAndroid Build Coastguard Worker deps = [ 453*61c4878aSAndroid Build Coastguard Worker ":my_proto_pwpb", 454*61c4878aSAndroid Build Coastguard Worker ":my_proto_raw_rpc", 455*61c4878aSAndroid Build Coastguard Worker ], 456*61c4878aSAndroid Build Coastguard Worker ) 457*61c4878aSAndroid Build Coastguard Worker cc_library( 458*61c4878aSAndroid Build Coastguard Worker name = "my_nanopb_rpc_lib", 459*61c4878aSAndroid Build Coastguard Worker srcs = ["my/proto_only.cc"], 460*61c4878aSAndroid Build Coastguard Worker deps = [ 461*61c4878aSAndroid Build Coastguard Worker ":my_proto_nanopb_rpc", 462*61c4878aSAndroid Build Coastguard Worker ], 463*61c4878aSAndroid Build Coastguard Worker ) 464*61c4878aSAndroid Build Coastguard Worker 465*61c4878aSAndroid Build Coastguard WorkerFrom ``my/lib.cc`` you can now include the generated headers. 466*61c4878aSAndroid Build Coastguard Workere.g. 467*61c4878aSAndroid Build Coastguard Worker 468*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 469*61c4878aSAndroid Build Coastguard Worker 470*61c4878aSAndroid Build Coastguard Worker #include "my_protos/bar.pwpb.h" 471*61c4878aSAndroid Build Coastguard Worker // and/or RPC headers 472*61c4878aSAndroid Build Coastguard Worker #include "my_protos/bar.raw_rpc.pb.h 473*61c4878aSAndroid Build Coastguard Worker // or 474*61c4878aSAndroid Build Coastguard Worker #include "my_protos/bar.nanopb_rpc.pb.h" 475*61c4878aSAndroid Build Coastguard Worker 476*61c4878aSAndroid Build Coastguard Worker 477*61c4878aSAndroid Build Coastguard WorkerWhy isn't there one rule to generate all the code? 478*61c4878aSAndroid Build Coastguard Worker-------------------------------------------------- 479*61c4878aSAndroid Build Coastguard WorkerThere is! Like in GN, it's called ``pw_proto_library``, and has subtargets 480*61c4878aSAndroid Build Coastguard Workercorresponding to the different codegen flavors. However, new code **should not** 481*61c4878aSAndroid Build Coastguard Workeruse this. It is deprecated, and will be removed in the future. 482*61c4878aSAndroid Build Coastguard Worker 483*61c4878aSAndroid Build Coastguard WorkerThe ``pw_proto_library`` target has a number of disadvantages: 484*61c4878aSAndroid Build Coastguard Worker 485*61c4878aSAndroid Build Coastguard Worker#. As a general bazel style rule, macros should produce exactly one target for 486*61c4878aSAndroid Build Coastguard Worker external use, named according to the invocation's name argument. ``BUILD`` 487*61c4878aSAndroid Build Coastguard Worker files are easier to follow when the name specified in the macro call 488*61c4878aSAndroid Build Coastguard Worker actually matches the name of the generated target. This is not possible if a 489*61c4878aSAndroid Build Coastguard Worker single macro is generating multiple targets, as ``pw_proto_library`` does. 490*61c4878aSAndroid Build Coastguard Worker#. If you depend directly on the ``pw_proto_library``, rather than the 491*61c4878aSAndroid Build Coastguard Worker appropriate subtargets, you will build code you don't actually use. You may 492*61c4878aSAndroid Build Coastguard Worker even fetch dependencies you don't need, like nanopb. 493*61c4878aSAndroid Build Coastguard Worker#. The subtargets you don't depend on are still added to your BUILD files by 494*61c4878aSAndroid Build Coastguard Worker the ``pw_proto_library`` macro, and bazel will attempt to build them when 495*61c4878aSAndroid Build Coastguard Worker you run ``bazel build //...``. This may cause build breakages, and has 496*61c4878aSAndroid Build Coastguard Worker forced us to implement `awkward workarounds 497*61c4878aSAndroid Build Coastguard Worker <https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/96980>`_. 498*61c4878aSAndroid Build Coastguard Worker 499*61c4878aSAndroid Build Coastguard Worker---------------------- 500*61c4878aSAndroid Build Coastguard WorkerPython proto libraries 501*61c4878aSAndroid Build Coastguard Worker---------------------- 502*61c4878aSAndroid Build Coastguard Worker``pw_protobuf_compiler`` includes utilties for working with protocol buffers 503*61c4878aSAndroid Build Coastguard Workerin Python. The tools facilitate using protos from their package names 504*61c4878aSAndroid Build Coastguard Worker(``my.pkg.Message()``) rather than their generated module names 505*61c4878aSAndroid Build Coastguard Worker(``proto_source_file_pb2.Message()``). 506*61c4878aSAndroid Build Coastguard Worker 507*61c4878aSAndroid Build Coastguard Worker``python_protos`` module 508*61c4878aSAndroid Build Coastguard Worker======================== 509*61c4878aSAndroid Build Coastguard Worker.. automodule:: pw_protobuf_compiler.python_protos 510*61c4878aSAndroid Build Coastguard Worker :members: proto_repr, Library 511