Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 1 | .. _chapter-pw-protobuf-compiler: |
| 2 | |
| 3 | .. default-domain:: py |
| 4 | |
| 5 | .. highlight:: py |
| 6 | |
| 7 | -------------------- |
| 8 | pw_protobuf_compiler |
| 9 | -------------------- |
| 10 | |
| 11 | The Protobuf compiler module provides build system integration and wrapper |
| 12 | scripts for generating source code for Protobuf definitions. |
| 13 | |
| 14 | Language support |
| 15 | ================ |
| 16 | |
| 17 | Protobuf 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 Frolov | 44d5473 | 2020-01-10 14:45:43 -0800 | [diff] [blame] | 27 | | Go | ``go`` | Compiles using the standard Go protobuf plugin with gRPC | |
| 28 | | | | service support. | |
| 29 | +----------+--------+----------------------------------------------------------+ |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 30 | |
| 31 | The build variable ``pw_protobuf_langs`` tells the module the languages for |
| 32 | which it should compile code. It is defined as a list of language codes. |
| 33 | |
| 34 | GN template |
| 35 | =========== |
| 36 | |
| 37 | The ``pw_proto_library`` GN template is provided by the module. |
| 38 | |
| 39 | It tells the build system to compile a set of source proto files to a library in |
| 40 | each chosen language. A different target is created for each language, with the |
| 41 | language's code appended as a suffix to the template's target name. |
| 42 | |
| 43 | For 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 | |
| 53 | Two targets are created, named ``test_protos_cc`` and ``test_protos_py``, |
| 54 | containing 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 | } |