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