Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 1 | # Copyright 2020 The Pigweed Authors |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 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 | |
Armando Montanez | fb3d3fb | 2020-06-09 18:12:12 -0700 | [diff] [blame] | 15 | # gn-format disable |
| 16 | import("//build_overrides/pigweed.gni") |
| 17 | |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 18 | import("$dir_pw_build/input_group.gni") |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 19 | import("$dir_pw_build/python_script.gni") |
Alexei Frolov | edd2f14 | 2020-06-09 19:11:27 -0700 | [diff] [blame] | 20 | import("$dir_pw_build/target_types.gni") |
Alexei Frolov | e4b6f6c | 2020-06-12 15:08:27 -0700 | [diff] [blame] | 21 | import("nanopb.gni") |
Alexei Frolov | 4c0428a | 2020-06-10 10:46:04 -0700 | [diff] [blame] | 22 | declare_args() { |
| 23 | # Generators with which to compile protobuf code. These are used by the |
| 24 | # pw_proto_library template to determine which build targets to create. |
| 25 | # |
| 26 | # Supported generators: |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame^] | 27 | # "pwpb", "nanopb", "nanopb_rpc", "go" |
Alexei Frolov | 4c0428a | 2020-06-10 10:46:04 -0700 | [diff] [blame] | 28 | pw_protobuf_GENERATORS = [ |
| 29 | "pwpb", |
| 30 | "go", |
| 31 | ] |
| 32 | } |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 33 | |
| 34 | # Python script that invokes protoc. |
| 35 | _gen_script_path = |
| 36 | "$dir_pw_protobuf_compiler/py/pw_protobuf_compiler/generate_protos.py" |
| 37 | |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 38 | _forwarded_vars = [ |
| 39 | "testonly", |
| 40 | "visibility", |
| 41 | ] |
| 42 | |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 43 | # Generates pw_protobuf C++ code for proto files, creating a source_set of the |
| 44 | # generated files. This is internal and should not be used outside of this file. |
| 45 | # Use pw_proto_library instead. |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 46 | # |
| 47 | # Args: |
| 48 | # protos: List of input .proto files. |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 49 | template("_pw_pwpb_proto_library") { |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 50 | _proto_gen_dir = "$root_gen_dir/protos" |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 51 | _module_path = get_path_info(".", "abspath") |
| 52 | _relative_proto_paths = rebase_path(invoker.protos, _module_path) |
| 53 | |
| 54 | _outputs = [] |
| 55 | foreach(_proto, _relative_proto_paths) { |
| 56 | _output = string_replace(_proto, ".proto", ".pwpb.h") |
| 57 | _outputs += [ "$_proto_gen_dir/$_output" ] |
| 58 | } |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 59 | |
| 60 | _gen_target = "${target_name}_gen" |
| 61 | pw_python_script(_gen_target) { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 62 | forward_variables_from(invoker, _forwarded_vars) |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 63 | script = _gen_script_path |
| 64 | args = [ |
| 65 | "--language", |
| 66 | "cc", |
| 67 | "--module-path", |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 68 | _module_path, |
| 69 | "--include-file", |
| 70 | invoker.include_file, |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 71 | "--out-dir", |
| 72 | _proto_gen_dir, |
| 73 | ] + get_path_info(invoker.protos, "abspath") |
| 74 | inputs = invoker.protos |
| 75 | outputs = _outputs |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 76 | deps = invoker.deps |
Alexei Frolov | bbf164c | 2019-12-16 12:51:59 -0800 | [diff] [blame] | 77 | if (defined(invoker.protoc_deps)) { |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 78 | deps += invoker.protoc_deps |
Alexei Frolov | bbf164c | 2019-12-16 12:51:59 -0800 | [diff] [blame] | 79 | } |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | # For C++ proto files, the generated proto directory is added as an include |
| 83 | # path for the code. This requires using "all_dependent_configs" to force the |
| 84 | # include on any code that transitively depends on the generated protos. |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 85 | _include_config_target = "${target_name}_includes" |
| 86 | config(_include_config_target) { |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 87 | include_dirs = [ "$_proto_gen_dir" ] |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | # Create a library with the generated source files. |
Alexei Frolov | edd2f14 | 2020-06-09 19:11:27 -0700 | [diff] [blame] | 91 | pw_source_set(target_name) { |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 92 | all_dependent_configs = [ ":$_include_config_target" ] |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 93 | deps = [ ":$_gen_target" ] |
| 94 | public_deps = [ dir_pw_protobuf ] + invoker.gen_deps |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 95 | sources = get_target_outputs(":$_gen_target") |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 96 | public = filter_include(sources, [ "*.pwpb.h" ]) |
| 97 | } |
| 98 | } |
| 99 | |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame^] | 100 | # Generates nanopb RPC code for proto files, creating a source_set of the |
| 101 | # generated files. This is internal and should not be used outside of this file. |
| 102 | # Use pw_proto_library instead. |
| 103 | # |
| 104 | # Args: |
| 105 | # protos: List of input .proto files. |
| 106 | # |
| 107 | template("_pw_nanopb_rpc_proto_library") { |
| 108 | assert(defined(dir_pw_third_party_nanopb) && dir_pw_third_party_nanopb != "", |
| 109 | "\$dir_pw_third_party_nanopb must be set to compile nanopb protobufs") |
| 110 | |
| 111 | _proto_gen_dir = "$root_gen_dir/protos" |
| 112 | _module_path = get_path_info(".", "abspath") |
| 113 | _relative_proto_paths = rebase_path(invoker.protos, _module_path) |
| 114 | |
| 115 | _outputs = [] |
| 116 | foreach(_proto, _relative_proto_paths) { |
| 117 | _output_h = string_replace(_proto, ".proto", "_rpc.pb.h") |
| 118 | _outputs += [ "$_proto_gen_dir/$_output_h" ] |
| 119 | } |
| 120 | |
| 121 | # Create a target which runs protoc configured with the nanopb_rpc plugin to |
| 122 | # generate the C++ proto RPC headers. |
| 123 | _gen_target = "${target_name}_gen" |
| 124 | pw_python_script(_gen_target) { |
| 125 | forward_variables_from(invoker, _forwarded_vars) |
| 126 | script = _gen_script_path |
| 127 | args = [ |
| 128 | "--language", |
| 129 | "nanopb_rpc", |
| 130 | "--module-path", |
| 131 | _module_path, |
| 132 | "--include-paths", |
| 133 | "$dir_pw_third_party_nanopb/generator/proto", |
| 134 | "--include-file", |
| 135 | invoker.include_file, |
| 136 | "--out-dir", |
| 137 | _proto_gen_dir, |
| 138 | ] + get_path_info(invoker.protos, "abspath") |
| 139 | inputs = invoker.protos |
| 140 | outputs = _outputs |
| 141 | |
| 142 | deps = invoker.deps |
| 143 | if (defined(invoker.protoc_deps)) { |
| 144 | deps += invoker.protoc_deps |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | # For C++ proto files, the generated proto directory is added as an include |
| 149 | # path for the code. This requires using "all_dependent_configs" to force the |
| 150 | # include on any code that transitively depends on the generated protos. |
| 151 | _include_root = rebase_path(get_path_info(".", "abspath"), "//") |
| 152 | _include_config_target = "${target_name}_includes" |
| 153 | config(_include_config_target) { |
| 154 | include_dirs = [ |
| 155 | "$_proto_gen_dir", |
| 156 | "$_proto_gen_dir/$_include_root", |
| 157 | ] |
| 158 | } |
| 159 | |
| 160 | # Create a library with the generated source files. |
| 161 | pw_source_set(target_name) { |
| 162 | all_dependent_configs = [ ":$_include_config_target" ] |
| 163 | deps = [ ":$_gen_target" ] |
| 164 | public_deps = [ |
| 165 | dir_pw_third_party_nanopb, |
| 166 | "$dir_pw_rpc:nanopb_server", |
| 167 | ] + invoker.gen_deps |
| 168 | public = get_target_outputs(":$_gen_target") |
| 169 | } |
| 170 | } |
| 171 | |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 172 | # Generates nanopb code for proto files, creating a source_set of the generated |
| 173 | # files. This is internal and should not be used outside of this file. Use |
| 174 | # pw_proto_library instead. |
| 175 | # |
| 176 | # Args: |
| 177 | # protos: List of input .proto files. |
| 178 | template("_pw_nanopb_proto_library") { |
Alexei Frolov | 4c0428a | 2020-06-10 10:46:04 -0700 | [diff] [blame] | 179 | assert(defined(dir_pw_third_party_nanopb) && dir_pw_third_party_nanopb != "", |
| 180 | "\$dir_pw_third_party_nanopb must be set to compile nanopb protobufs") |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 181 | |
| 182 | _proto_gen_dir = "$root_gen_dir/protos" |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 183 | _module_path = get_path_info(".", "abspath") |
| 184 | _relative_proto_paths = rebase_path(invoker.protos, _module_path) |
| 185 | |
| 186 | _outputs = [] |
| 187 | foreach(_proto, _relative_proto_paths) { |
| 188 | _output_h = string_replace(_proto, ".proto", ".pb.h") |
| 189 | _output_c = string_replace(_proto, ".proto", ".pb.c") |
| 190 | _outputs += [ |
| 191 | "$_proto_gen_dir/$_output_h", |
| 192 | "$_proto_gen_dir/$_output_c", |
| 193 | ] |
| 194 | } |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 195 | |
Alexei Frolov | 4c0428a | 2020-06-10 10:46:04 -0700 | [diff] [blame] | 196 | _nanopb_plugin = "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb" |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 197 | if (host_os == "win") { |
| 198 | _nanopb_plugin += ".bat" |
| 199 | } |
| 200 | |
| 201 | # Create a target which runs protoc configured with the nanopb plugin to |
| 202 | # generate the C proto sources. |
| 203 | _gen_target = "${target_name}_gen" |
| 204 | pw_python_script(_gen_target) { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 205 | forward_variables_from(invoker, _forwarded_vars) |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 206 | script = _gen_script_path |
| 207 | args = [ |
| 208 | "--language", |
| 209 | "nanopb", |
| 210 | "--module-path", |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 211 | _module_path, |
Alexei Frolov | ac5b674 | 2020-05-01 13:28:14 -0700 | [diff] [blame] | 212 | "--include-paths", |
Alexei Frolov | 4c0428a | 2020-06-10 10:46:04 -0700 | [diff] [blame] | 213 | "$dir_pw_third_party_nanopb/generator/proto", |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 214 | "--include-file", |
| 215 | invoker.include_file, |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 216 | "--out-dir", |
| 217 | _proto_gen_dir, |
| 218 | "--custom-plugin", |
| 219 | get_path_info(_nanopb_plugin, "abspath"), |
| 220 | ] + get_path_info(invoker.protos, "abspath") |
| 221 | inputs = invoker.protos |
| 222 | outputs = _outputs |
| 223 | |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 224 | deps = invoker.deps |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 225 | if (defined(invoker.protoc_deps)) { |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 226 | deps += invoker.protoc_deps |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | # For C++ proto files, the generated proto directory is added as an include |
| 231 | # path for the code. This requires using "all_dependent_configs" to force the |
| 232 | # include on any code that transitively depends on the generated protos. |
| 233 | _include_root = rebase_path(get_path_info(".", "abspath"), "//") |
| 234 | _include_config_target = "${target_name}_includes" |
| 235 | config(_include_config_target) { |
| 236 | include_dirs = [ |
| 237 | "$_proto_gen_dir", |
| 238 | "$_proto_gen_dir/$_include_root", |
| 239 | ] |
| 240 | } |
| 241 | |
| 242 | # Create a library with the generated source files. |
Alexei Frolov | edd2f14 | 2020-06-09 19:11:27 -0700 | [diff] [blame] | 243 | pw_source_set(target_name) { |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 244 | all_dependent_configs = [ ":$_include_config_target" ] |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 245 | deps = [ ":$_gen_target" ] |
Alexei Frolov | 4c0428a | 2020-06-10 10:46:04 -0700 | [diff] [blame] | 246 | public_deps = [ dir_pw_third_party_nanopb ] + invoker.gen_deps |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 247 | sources = get_target_outputs(":$_gen_target") |
| 248 | public = filter_include(sources, [ "*.pb.h" ]) |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 252 | # Generates Go code for proto files, listing the proto output directory in the |
| 253 | # metadata variable GOPATH. Internal use only. |
| 254 | # |
| 255 | # Args: |
| 256 | # protos: List of input .proto files. |
| 257 | template("_pw_go_proto_library") { |
| 258 | _proto_gopath = "$root_gen_dir/go" |
| 259 | _proto_gen_dir = "$_proto_gopath/src" |
| 260 | _rebased_gopath = rebase_path(_proto_gopath) |
| 261 | |
| 262 | pw_python_script(target_name) { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 263 | forward_variables_from(invoker, _forwarded_vars) |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 264 | metadata = { |
| 265 | gopath = [ "GOPATH+=$_rebased_gopath" ] |
Alexei Frolov | c15a988 | 2019-12-23 14:29:02 -0800 | [diff] [blame] | 266 | external_deps = [ |
| 267 | "github.com/golang/protobuf/proto", |
| 268 | "google.golang.org/grpc", |
| 269 | ] |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 270 | } |
| 271 | script = _gen_script_path |
| 272 | args = [ |
| 273 | "--language", |
| 274 | "go", |
| 275 | "--module-path", |
| 276 | "//", |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 277 | "--include-file", |
| 278 | invoker.include_file, |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 279 | "--out-dir", |
| 280 | _proto_gen_dir, |
| 281 | ] + get_path_info(invoker.protos, "abspath") |
| 282 | inputs = invoker.protos |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 283 | deps = invoker.deps + invoker.gen_deps |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 284 | stamp = true |
| 285 | } |
| 286 | } |
| 287 | |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 288 | # Generates protobuf code from .proto definitions for various languages. |
| 289 | # |
Alexei Frolov | 4c0428a | 2020-06-10 10:46:04 -0700 | [diff] [blame] | 290 | # The generators to use are defined in the pw_protobuf_GENERATORS build |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 291 | # variable. Each listed generator creates a generated code target called |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 292 | # |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 293 | # <target_name>_<generator> |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 294 | # |
| 295 | # For example, with the following definitions: |
| 296 | # |
Alexei Frolov | 4c0428a | 2020-06-10 10:46:04 -0700 | [diff] [blame] | 297 | # pw_protobuf_GENERATORS = [ "pwpb", "py" ] |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 298 | # |
| 299 | # pw_proto_library("my_protos") { |
| 300 | # sources = [ "foo.proto" ] |
| 301 | # } |
| 302 | # |
| 303 | # Two build targets will be created for the declared "my_protos" target. |
| 304 | # |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 305 | # "my_protos_pwpb" <-- C++ source_set containing generated proto code |
| 306 | # "my_protos_py" <-- Python module containing generated proto code |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 307 | # |
| 308 | # Args: |
| 309 | # sources: List of input .proto files. |
| 310 | # deps: List of other pw_proto_library dependencies. |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 311 | # inputs: Other files on which the protos depend (e.g. nanopb .options files). |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 312 | template("pw_proto_library") { |
| 313 | assert(defined(invoker.sources) && invoker.sources != [], |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 314 | "pw_proto_library requires .proto source files") |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 315 | |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 316 | # For each proto target, create a file which collects the base directories of |
| 317 | # all of its dependencies to list as include paths to protoc. |
| 318 | _include_metadata_target = "${target_name}_include_paths" |
| 319 | _include_metadata_file = "${target_gen_dir}/${target_name}_includes.txt" |
| 320 | generated_file(_include_metadata_target) { |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 321 | if (defined(invoker.deps)) { |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 322 | # Collect metadata from the include path files of each dependency. |
| 323 | deps = process_file_template(invoker.deps, "{{source}}_include_paths") |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 324 | } else { |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 325 | deps = [] |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 326 | } |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 327 | data_keys = [ "protoc_includes" ] |
| 328 | outputs = [ _include_metadata_file ] |
| 329 | |
| 330 | # Indicate this library's base directory for its dependents. |
| 331 | metadata = { |
Alexei Frolov | f79d227 | 2020-06-18 13:37:38 -0700 | [diff] [blame] | 332 | protoc_includes = [ rebase_path(".") ] |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 336 | _deps = [ ":$_include_metadata_target" ] |
| 337 | |
| 338 | if (defined(invoker.inputs)) { |
| 339 | # Toss any additional inputs into an input group dependency. |
| 340 | _input_target_name = "${target_name}_inputs" |
| 341 | pw_input_group(_input_target_name) { |
| 342 | inputs = invoker.inputs |
| 343 | } |
| 344 | _deps += [ ":$_input_target_name" ] |
| 345 | } |
| 346 | |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame^] | 347 | # If the nanopb_rpc generator is selected, make sure that nanopb is also |
| 348 | # selected. |
| 349 | has_nanopb_rpc = pw_protobuf_GENERATORS + [ "nanopb_rpc" ] - |
| 350 | [ "nanopb_rpc" ] != pw_protobuf_GENERATORS |
| 351 | if (has_nanopb_rpc) { |
| 352 | _generators = |
| 353 | pw_protobuf_GENERATORS + [ "nanopb" ] - [ "nanopb" ] + [ "nanopb" ] |
| 354 | } else { |
| 355 | _generators = pw_protobuf_GENERATORS |
| 356 | } |
| 357 | |
| 358 | foreach(_gen, _generators) { |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 359 | _lang_target = "${target_name}_${_gen}" |
Alexei Frolov | 8185c82 | 2020-06-12 10:45:04 -0700 | [diff] [blame] | 360 | _gen_deps = [] |
| 361 | if (defined(invoker.deps)) { |
| 362 | _gen_deps = process_file_template(invoker.deps, "{{source}}_${_gen}") |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame^] | 363 | |
| 364 | if (_gen == "nanopb_rpc") { |
| 365 | # Generated RPC code also depends on the core generated protos. |
| 366 | _gen_deps += process_file_template(invoker.deps, "{{source}}_nanopb") |
| 367 | } |
Alexei Frolov | 8185c82 | 2020-06-12 10:45:04 -0700 | [diff] [blame] | 368 | } |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 369 | |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 370 | if (_gen == "pwpb") { |
| 371 | _pw_pwpb_proto_library(_lang_target) { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 372 | forward_variables_from(invoker, _forwarded_vars) |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 373 | protos = invoker.sources |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 374 | deps = _deps |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 375 | include_file = _include_metadata_file |
| 376 | gen_deps = _gen_deps |
Alexei Frolov | bbf164c | 2019-12-16 12:51:59 -0800 | [diff] [blame] | 377 | |
| 378 | # List the pw_protobuf plugin's files as a dependency to recompile |
| 379 | # generated code if they are modified. |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 380 | protoc_deps = [ "$dir_pw_protobuf:codegen_protoc_plugin" ] |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 381 | } |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame^] | 382 | } else if (_gen == "nanopb_rpc") { |
| 383 | _pw_nanopb_rpc_proto_library(_lang_target) { |
| 384 | forward_variables_from(invoker, _forwarded_vars) |
| 385 | protos = invoker.sources |
| 386 | deps = _deps |
| 387 | include_file = _include_metadata_file |
| 388 | gen_deps = _gen_deps |
| 389 | |
| 390 | # List the pw_protobuf plugin's files as a dependency to recompile |
| 391 | # generated code if they are modified. |
| 392 | protoc_deps = [ "$dir_pw_rpc:nanopb_protoc_plugin" ] |
| 393 | } |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 394 | } else if (_gen == "nanopb") { |
| 395 | _pw_nanopb_proto_library(_lang_target) { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 396 | forward_variables_from(invoker, _forwarded_vars) |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 397 | protos = invoker.sources |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 398 | deps = _deps |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 399 | include_file = _include_metadata_file |
| 400 | gen_deps = _gen_deps |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 401 | } |
| 402 | } else if (_gen == "go") { |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 403 | _pw_go_proto_library(_lang_target) { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 404 | forward_variables_from(invoker, _forwarded_vars) |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 405 | protos = invoker.sources |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 406 | deps = _deps |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 407 | include_file = _include_metadata_file |
| 408 | gen_deps = _gen_deps |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 409 | } |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 410 | } else { |
| 411 | assert(false, |
| 412 | string_join( |
| 413 | " ", |
| 414 | [ |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 415 | "pw_proto_library doesn't know how to generate code for", |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 416 | "generator '$_gen'. Please add support if you require it.", |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 417 | ])) |
| 418 | } |
| 419 | } |
| 420 | |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 421 | # All supported pw_protobuf generators. |
| 422 | _protobuf_generators = [ |
| 423 | "pwpb", |
| 424 | "nanopb", |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame^] | 425 | "nanopb_rpc", |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 426 | "go", |
| 427 | ] |
| 428 | |
| 429 | # Create stub versions of the proto library for other protobuf generators. |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame^] | 430 | foreach(_gen, _protobuf_generators - _generators) { |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 431 | pw_python_script("${target_name}_${_gen}") { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 432 | forward_variables_from(invoker, _forwarded_vars) |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 433 | script = string_join("/", |
| 434 | [ |
| 435 | dir_pw_protobuf_compiler, |
| 436 | "py", |
| 437 | "pw_protobuf_compiler", |
| 438 | "generator_not_selected.py", |
| 439 | ]) |
| 440 | args = [ |
| 441 | "--library", |
| 442 | "${target_name}_${_gen}", |
| 443 | "--generator", |
| 444 | _gen, |
| 445 | ] |
| 446 | inputs = invoker.sources |
| 447 | stamp = true |
| 448 | } |
| 449 | } |
| 450 | |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 451 | # If the user attempts to use the target directly instead of one of the |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 452 | # generator targets, run a script which prints a nice error message. |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 453 | pw_python_script(target_name) { |
| 454 | script = string_join("/", |
| 455 | [ |
| 456 | dir_pw_protobuf_compiler, |
| 457 | "py", |
| 458 | "pw_protobuf_compiler", |
| 459 | "proto_target_invalid.py", |
| 460 | ]) |
| 461 | args = [ |
| 462 | "--target", |
| 463 | target_name, |
| 464 | "--dir", |
| 465 | get_path_info(".", "abspath"), |
| 466 | "--root", |
| 467 | "//", |
Alexei Frolov | 4c0428a | 2020-06-10 10:46:04 -0700 | [diff] [blame] | 468 | ] + pw_protobuf_GENERATORS |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 469 | stamp = true |
| 470 | } |
| 471 | } |