blob: 232e356a557b49ee7c4cefdd0260f75c7cd43ab8 [file] [log] [blame]
Alexei Frolov942adf02019-12-11 17:07:28 -08001.. _chapter-pw-protobuf-compiler:
2
3.. default-domain:: py
4
5.. highlight:: py
6
7--------------------
8pw_protobuf_compiler
9--------------------
10
11The Protobuf compiler module provides build system integration and wrapper
12scripts for generating source code for Protobuf definitions.
13
14Language support
15================
16
17Protobuf code generation is currently supported for the following languages:
18
19+----------+--------+----------------------------------------------------------+
20| Language | Code | Notes |
21+----------+--------+----------------------------------------------------------+
22| C++ | ``cc`` | Currently only supports using ``pw_protobuf``. |
23| | | |
24| | | Support for other libraries such as ``nanopb`` is |
25| | | planned. |
26+----------+--------+----------------------------------------------------------+
Alexei Frolov44d54732020-01-10 14:45:43 -080027| Go | ``go`` | Compiles using the standard Go protobuf plugin with gRPC |
28| | | service support. |
29+----------+--------+----------------------------------------------------------+
Alexei Frolov942adf02019-12-11 17:07:28 -080030
31The build variable ``pw_protobuf_langs`` tells the module the languages for
32which it should compile code. It is defined as a list of language codes.
33
34GN template
35===========
36
37The ``pw_proto_library`` GN template is provided by the module.
38
39It tells the build system to compile a set of source proto files to a library in
40each chosen language. A different target is created for each language, with the
41language's code appended as a suffix to the template's target name.
42
43For example, given the definitions:
44
45.. code::
46
47 pw_protobuf_langs = [ "cc", "py" ]
48
49 pw_proto_library("test_protos") {
50 sources = [ "test.proto" ]
51 }
52
53Two targets are created, named ``test_protos_cc`` and ``test_protos_py``,
54containing the generated code for their respective languages.
55
56**Arguments**
57
58* ``sources``: List of ``.proto`` files.
59* ``deps``: Other ``pw_proto_library`` targets that this one depends on.
60
61**Example**
62
63.. code::
64
65 import("$dir_pw_protobuf_compiler/proto.gni")
66
67 pw_proto_library("my_protos") {
68 sources = [
69 "foo.proto",
70 "bar.proto",
71 ]
72 }
73
74 pw_proto_library("my_other_protos") {
75 sources = [
76 "baz.proto", # imports foo.proto
77 ]
78 deps = [
79 ":my_protos",
80 ]
81 }
82
83 source_set("my_cc_code") {
84 sources = [
85 "foo.cc",
86 "bar.cc",
87 "baz.cc",
88 ]
89 deps = [
90 ":my_other_protos_cc",
91 ]
92 }