blob: 30bb37a52b3d0a01969434d2a69577b54cd22b57 [file] [log] [blame]
Wyatt Heplerf9fb90f2020-09-30 18:59:33 -07001.. _module-pw_protobuf_compiler:
Alexei Frolov4a257c12020-03-02 14:09:42 -08002
Alexei Frolov942adf02019-12-11 17:07:28 -08003--------------------
4pw_protobuf_compiler
5--------------------
Alexei Frolov942adf02019-12-11 17:07:28 -08006The Protobuf compiler module provides build system integration and wrapper
7scripts for generating source code for Protobuf definitions.
8
Alexei Frolovf39cd8b2020-04-13 17:59:20 -07009Generator support
10=================
Alexei Frolovf39cd8b2020-04-13 17:59:20 -070011Protobuf code generation is currently supported for the following generators:
Alexei Frolov942adf02019-12-11 17:07:28 -080012
Alexei Frolov79b7cb02020-07-06 13:51:43 -070013+-------------+----------------+-----------------------------------------------+
14| Generator | Code | Notes |
15+-------------+----------------+-----------------------------------------------+
16| pw_protobuf | ``pwpb`` | Compiles using ``pw_protobuf``. |
17+-------------+----------------+-----------------------------------------------+
Alexei Frolov79b7cb02020-07-06 13:51:43 -070018| Nanopb | ``nanopb`` | Compiles using Nanopb. The build argument |
19| | | ``dir_pw_third_party_nanopb`` must be set to |
20| | | point to a local nanopb installation. |
21+-------------+----------------+-----------------------------------------------+
Alexei Frolovc912ea72020-10-26 08:43:27 -070022| Nanopb RPC | ``nanopb_rpc`` | Compiles pw_rpc service and client code for |
23| | | nanopb. Requires a nanopb installation. |
24+-------------+----------------+-----------------------------------------------+
25| Raw RPC | ``raw_rpc`` | Compiles raw binary pw_rpc service code. |
Alexei Frolov79b7cb02020-07-06 13:51:43 -070026+-------------+----------------+-----------------------------------------------+
Alexei Frolova4c0aee2020-12-01 13:48:48 -080027| Go | ``go`` | Compiles using the standard Go protobuf |
28| | | plugin with gRPC service support. |
29+-------------+----------------+-----------------------------------------------+
30| Python | ``python`` | Compiles using the standard Python protobuf |
31| | | plugin, creating a ``pw_python_package``. |
32+-------------+----------------+-----------------------------------------------+
Alexei Frolov942adf02019-12-11 17:07:28 -080033
Alexei Frolov942adf02019-12-11 17:07:28 -080034GN template
35===========
Alexei Frolov942adf02019-12-11 17:07:28 -080036The ``pw_proto_library`` GN template is provided by the module.
37
Alexei Frolovb499d3f2020-10-28 13:00:08 -070038It defines a collection of protobuf files that should be compiled together. The
39template creates a sub-target for each supported generator, named
40``<target_name>.<generator>``. These sub-targets generate their respective
41protobuf code, and expose it to the build system appropriately (e.g. a
42``pw_source_set`` for C/C++).
Alexei Frolov942adf02019-12-11 17:07:28 -080043
Alexei Frolovb499d3f2020-10-28 13:00:08 -070044For example, given the following target:
Alexei Frolov942adf02019-12-11 17:07:28 -080045
Alexei Frolovb499d3f2020-10-28 13:00:08 -070046.. code-block::
Alexei Frolov942adf02019-12-11 17:07:28 -080047
48 pw_proto_library("test_protos") {
Wyatt Heplerd517afc2021-02-03 19:40:08 -080049 sources = [ "my_test_protos/test.proto" ]
Alexei Frolov942adf02019-12-11 17:07:28 -080050 }
51
Alexei Frolovb499d3f2020-10-28 13:00:08 -070052``test_protos.pwpb`` compiles code for pw_protobuf, and ``test_protos.nanopb``
53compiles using Nanopb (if it's installed).
54
55Protobuf code is only generated when a generator sub-target is listed as a
56dependency of another GN target.
Alexei Frolov942adf02019-12-11 17:07:28 -080057
58**Arguments**
59
60* ``sources``: List of ``.proto`` files.
61* ``deps``: Other ``pw_proto_library`` targets that this one depends on.
62
63**Example**
64
65.. code::
66
67 import("$dir_pw_protobuf_compiler/proto.gni")
68
69 pw_proto_library("my_protos") {
70 sources = [
Wyatt Heplerd517afc2021-02-03 19:40:08 -080071 "my_protos/foo.proto",
72 "my_protos/bar.proto",
Alexei Frolov942adf02019-12-11 17:07:28 -080073 ]
74 }
75
76 pw_proto_library("my_other_protos") {
Wyatt Heplerd517afc2021-02-03 19:40:08 -080077 sources = [ "my_other_protos/baz.proto" ] # imports foo.proto
Alexei Frolov8e30d462020-10-22 13:54:36 -070078
79 # Proto libraries depend on other proto libraries directly.
80 deps = [ ":my_protos" ]
Alexei Frolov942adf02019-12-11 17:07:28 -080081 }
82
83 source_set("my_cc_code") {
84 sources = [
85 "foo.cc",
86 "bar.cc",
87 "baz.cc",
88 ]
Alexei Frolov8e30d462020-10-22 13:54:36 -070089
90 # When depending on protos in a source_set, specify the generator suffix.
91 deps = [ ":my_other_protos.pwpb" ]
Alexei Frolov942adf02019-12-11 17:07:28 -080092 }
Wyatt Heplerd517afc2021-02-03 19:40:08 -080093
94Proto file structure
95--------------------
96Protobuf source files must be nested under another directory when they are
97listed in sources. This ensures that they can be packaged properly in Python.
98The first directory is used as the Python package name.
99
100The requirements for proto file structure in the source tree will be relaxed in
101future updates.
102
103Working with externally defined protos
104--------------------------------------
105``pw_proto_library`` targets may be used to build ``.proto`` sources from
106existing projects. In these cases, it may be necessary to supply the
107``include_path`` argument, which specifies the protobuf include path to use for
108``protoc``. If only a single external protobuf is being compiled, the
109requirement that the protobuf be nested under a directory is waived. This
110exception should only be used when absolutely necessary -- for example, to
111support proto files that includes ``import "nanopb.proto"`` in them.