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 | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 17 | import("$dir_pw_build/error.gni") |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 18 | import("$dir_pw_build/input_group.gni") |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 19 | import("$dir_pw_build/python.gni") |
Wyatt Hepler | 51ded74 | 2020-10-19 14:45:27 -0700 | [diff] [blame] | 20 | import("$dir_pw_build/python_action.gni") |
Alexei Frolov | edd2f14 | 2020-06-09 19:11:27 -0700 | [diff] [blame] | 21 | import("$dir_pw_build/target_types.gni") |
Alexei Frolov | dd6fa5c | 2020-08-11 10:04:01 -0700 | [diff] [blame] | 22 | import("$dir_pw_third_party/nanopb/nanopb.gni") |
Wyatt Hepler | d49f8fe | 2020-10-15 10:13:47 -0700 | [diff] [blame] | 23 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 24 | # Variables forwarded from the public pw_proto_library template to the final |
| 25 | # pw_source_set. |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 26 | _forwarded_vars = [ |
| 27 | "testonly", |
| 28 | "visibility", |
| 29 | ] |
| 30 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 31 | # Internal template that invokes protoc with a pw_python_action. This should not |
| 32 | # be used outside of this file; use pw_proto_library instead. |
| 33 | # |
| 34 | # This creates the internal GN target $target_name.$language._gen that compiles |
| 35 | # proto files with protoc. |
| 36 | template("_pw_invoke_protoc") { |
| 37 | _output = rebase_path(get_target_outputs(":${invoker.base_target}._metadata")) |
| 38 | |
| 39 | pw_python_action("$target_name._gen") { |
| 40 | forward_variables_from(invoker, [ "metadata" ]) |
| 41 | script = |
| 42 | "$dir_pw_protobuf_compiler/py/pw_protobuf_compiler/generate_protos.py" |
| 43 | |
| 44 | deps = [ |
| 45 | ":${invoker.base_target}._metadata", |
| 46 | ":${invoker.base_target}._inputs", |
| 47 | ] + invoker.deps |
| 48 | |
| 49 | args = [ |
| 50 | "--language", |
| 51 | invoker.language, |
| 52 | "--module-path", |
| 53 | rebase_path("."), |
| 54 | "--include-file", |
| 55 | _output[0], |
| 56 | "--out-dir", |
| 57 | rebase_path(invoker.gen_dir), |
| 58 | ] + rebase_path(invoker.sources) |
| 59 | |
| 60 | inputs = invoker.sources |
| 61 | |
| 62 | if (defined(invoker.plugin)) { |
| 63 | inputs += [ invoker.plugin ] |
| 64 | args += [ "--plugin-path=" + rebase_path(invoker.plugin) ] |
| 65 | } |
| 66 | |
| 67 | if (defined(invoker.include_paths)) { |
| 68 | args += [ |
| 69 | "--include-paths", |
| 70 | string_join(";", rebase_path(invoker.include_paths)), |
| 71 | ] |
| 72 | } |
| 73 | |
| 74 | outputs = [] |
| 75 | foreach(extension, invoker.output_extensions) { |
| 76 | foreach(proto, |
| 77 | rebase_path(invoker.sources, get_path_info(".", "abspath"))) { |
| 78 | _output = string_replace(proto, ".proto", extension) |
| 79 | outputs += [ "${invoker.gen_dir}/$_output" ] |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (outputs == []) { |
| 84 | stamp = true |
| 85 | } |
| 86 | |
| 87 | visibility = [ ":*" ] |
| 88 | } |
| 89 | } |
| 90 | |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 91 | # Generates pw_protobuf C++ code for proto files, creating a source_set of the |
| 92 | # generated files. This is internal and should not be used outside of this file. |
| 93 | # Use pw_proto_library instead. |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 94 | template("_pw_pwpb_proto_library") { |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 95 | _pw_invoke_protoc(target_name) { |
| 96 | forward_variables_from(invoker, "*", _forwarded_vars) |
| 97 | language = "pwpb" |
| 98 | plugin = "$dir_pw_protobuf/py/pw_protobuf/plugin.py" |
| 99 | deps += [ "$dir_pw_protobuf/py" ] |
| 100 | output_extensions = [ ".pwpb.h" ] |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | # Create a library with the generated source files. |
Alexei Frolov | edd2f14 | 2020-06-09 19:11:27 -0700 | [diff] [blame] | 104 | pw_source_set(target_name) { |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 105 | forward_variables_from(invoker, _forwarded_vars) |
| 106 | public_configs = [ ":${invoker.base_target}._include_path" ] |
| 107 | deps = [ ":$target_name._gen" ] |
| 108 | public_deps = [ dir_pw_protobuf ] + invoker.deps |
| 109 | sources = get_target_outputs(":$target_name._gen") |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 110 | public = filter_include(sources, [ "*.pwpb.h" ]) |
| 111 | } |
| 112 | } |
| 113 | |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 114 | # Generates nanopb RPC code for proto files, creating a source_set of the |
| 115 | # generated files. This is internal and should not be used outside of this file. |
| 116 | # Use pw_proto_library instead. |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 117 | template("_pw_nanopb_rpc_proto_library") { |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 118 | # Create a target which runs protoc configured with the nanopb_rpc plugin to |
| 119 | # generate the C++ proto RPC headers. |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 120 | _pw_invoke_protoc(target_name) { |
| 121 | forward_variables_from(invoker, "*", _forwarded_vars) |
| 122 | language = "nanopb_rpc" |
| 123 | plugin = "$dir_pw_rpc/py/pw_rpc/plugin_nanopb.py" |
| 124 | deps += [ "$dir_pw_rpc/py" ] |
| 125 | include_paths = [ "$dir_pw_third_party_nanopb/generator/proto" ] |
| 126 | output_extensions = [ ".rpc.pb.h" ] |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | # Create a library with the generated source files. |
| 130 | pw_source_set(target_name) { |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 131 | forward_variables_from(invoker, _forwarded_vars) |
| 132 | public_configs = [ ":${invoker.base_target}._include_path" ] |
| 133 | deps = [ ":$target_name._gen" ] |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 134 | public_deps = [ |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 135 | ":${invoker.base_target}.nanopb", |
Wyatt Hepler | cbd09c2 | 2020-09-15 11:17:24 -0700 | [diff] [blame] | 136 | "$dir_pw_rpc:server", |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 137 | "$dir_pw_rpc/nanopb:method_union", |
Alexei Frolov | dd6fa5c | 2020-08-11 10:04:01 -0700 | [diff] [blame] | 138 | "$dir_pw_third_party/nanopb", |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 139 | ] + invoker.deps |
| 140 | public = get_target_outputs(":$target_name._gen") |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 144 | # Generates nanopb code for proto files, creating a source_set of the generated |
| 145 | # files. This is internal and should not be used outside of this file. Use |
| 146 | # pw_proto_library instead. |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 147 | template("_pw_nanopb_proto_library") { |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 148 | # Create a target which runs protoc configured with the nanopb plugin to |
| 149 | # generate the C proto sources. |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 150 | _pw_invoke_protoc(target_name) { |
| 151 | forward_variables_from(invoker, "*", _forwarded_vars) |
| 152 | language = "nanopb" |
| 153 | plugin = "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb" |
| 154 | include_paths = [ "$dir_pw_third_party_nanopb/generator/proto" ] |
| 155 | output_extensions = [ |
| 156 | ".pb.h", |
| 157 | ".pb.c", |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 158 | ] |
| 159 | } |
| 160 | |
| 161 | # Create a library with the generated source files. |
Alexei Frolov | edd2f14 | 2020-06-09 19:11:27 -0700 | [diff] [blame] | 162 | pw_source_set(target_name) { |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 163 | forward_variables_from(invoker, _forwarded_vars) |
| 164 | public_configs = [ ":${invoker.base_target}._include_path" ] |
| 165 | deps = [ ":$target_name._gen" ] |
| 166 | public_deps = [ "$dir_pw_third_party/nanopb" ] + invoker.deps |
| 167 | sources = get_target_outputs(":$target_name._gen") |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 168 | public = filter_include(sources, [ "*.pb.h" ]) |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame] | 172 | # Generates raw RPC 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. |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame] | 175 | template("_pw_raw_rpc_proto_library") { |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame] | 176 | # Create a target which runs protoc configured with the nanopb_rpc plugin to |
| 177 | # generate the C++ proto RPC headers. |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 178 | _pw_invoke_protoc(target_name) { |
| 179 | forward_variables_from(invoker, "*", _forwarded_vars) |
| 180 | language = "raw_rpc" |
| 181 | plugin = "$dir_pw_rpc/py/pw_rpc/plugin_raw.py" |
| 182 | deps += [ "$dir_pw_rpc/py" ] |
| 183 | output_extensions = [ ".raw_rpc.pb.h" ] |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | # Create a library with the generated source files. |
| 187 | pw_source_set(target_name) { |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 188 | forward_variables_from(invoker, _forwarded_vars) |
| 189 | public_configs = [ ":${invoker.base_target}._include_path" ] |
| 190 | deps = [ ":$target_name._gen" ] |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame] | 191 | public_deps = [ |
| 192 | "$dir_pw_rpc:server", |
| 193 | "$dir_pw_rpc/raw:method_union", |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 194 | ] + invoker.deps |
| 195 | public = get_target_outputs(":$target_name._gen") |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 199 | # Generates Go code for proto files, listing the proto output directory in the |
| 200 | # metadata variable GOPATH. Internal use only. |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 201 | template("_pw_go_proto_library") { |
| 202 | _proto_gopath = "$root_gen_dir/go" |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 203 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 204 | _pw_invoke_protoc(target_name) { |
| 205 | forward_variables_from(invoker, "*") |
| 206 | language = "go" |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 207 | metadata = { |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 208 | gopath = [ "GOPATH+=" + rebase_path(_proto_gopath) ] |
Alexei Frolov | c15a988 | 2019-12-23 14:29:02 -0800 | [diff] [blame] | 209 | external_deps = [ |
| 210 | "github.com/golang/protobuf/proto", |
| 211 | "google.golang.org/grpc", |
| 212 | ] |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 213 | } |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 214 | output_extensions = [] # Don't enumerate the generated .go files. |
| 215 | gen_dir = "$_proto_gopath/src" |
| 216 | } |
| 217 | |
| 218 | group(target_name) { |
| 219 | deps = [ ":$target_name._gen" ] |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 223 | # Generates Python code for proto files, creating a pw_python_package containing |
| 224 | # the generated files. This is internal and should not be used outside of this |
| 225 | # file. Use pw_proto_library instead. |
| 226 | template("_pw_python_proto_library") { |
| 227 | _target = target_name |
| 228 | _package_dir = "" |
| 229 | |
| 230 | foreach(_rebased_proto_path, rebase_path(invoker.sources, ".")) { |
| 231 | _path_components = [] |
| 232 | _path_components = string_split(_rebased_proto_path, "/") |
| 233 | |
| 234 | assert(_path_components != [ _rebased_proto_path ] && |
| 235 | _path_components[0] != "..", |
| 236 | "Sources in a pw_proto_library must live in subdirectories " + |
| 237 | "of where it is defined") |
| 238 | |
| 239 | if (_package_dir == "") { |
| 240 | _package_dir = _path_components[0] |
| 241 | } else { |
| 242 | assert(_path_components[0] == _package_dir, |
| 243 | "All .proto sources in a pw_proto_library must live " + |
| 244 | "in the same directory tree") |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | _pw_invoke_protoc(target_name) { |
| 249 | forward_variables_from(invoker, "*", _forwarded_vars) |
| 250 | language = "python" |
| 251 | output_extensions = [ "_pb2.py" ] |
| 252 | } |
| 253 | |
| 254 | _setup_py = "${invoker.gen_dir}/setup.py" |
| 255 | |
| 256 | # Create the setup and init files for the Python package. |
| 257 | pw_python_action(target_name + "._package_gen") { |
| 258 | script = "$dir_pw_protobuf_compiler/py/pw_protobuf_compiler/generate_python_package.py" |
| 259 | args = [ |
| 260 | "--setup", |
| 261 | rebase_path(_setup_py), |
| 262 | "--package", |
| 263 | _package_dir, |
| 264 | ] + rebase_path(get_path_info(invoker.sources, "dir"), ".") |
| 265 | public_deps = [ ":$_target._gen" ] |
| 266 | stamp = true |
| 267 | } |
| 268 | |
| 269 | # Create a Python package with the generated source files. |
| 270 | pw_python_package(target_name) { |
| 271 | forward_variables_from(invoker, _forwarded_vars) |
| 272 | setup = [ _setup_py ] |
| 273 | sources = get_target_outputs(":$target_name._gen") |
| 274 | python_deps = invoker.deps |
| 275 | other_deps = [ ":$_target._package_gen" ] |
| 276 | _pw_generated = true |
| 277 | } |
| 278 | } |
| 279 | |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 280 | # Generates protobuf code from .proto definitions for various languages. |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 281 | # For each supported generator, creates a sub-target named: |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 282 | # |
Alexei Frolov | 8e30d46 | 2020-10-22 13:54:36 -0700 | [diff] [blame] | 283 | # <target_name>.<generator> |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 284 | # |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 285 | # Args: |
| 286 | # sources: List of input .proto files. |
| 287 | # deps: List of other pw_proto_library dependencies. |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 288 | # inputs: Other files on which the protos depend (e.g. nanopb .options files). |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 289 | # |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 290 | template("pw_proto_library") { |
| 291 | assert(defined(invoker.sources) && invoker.sources != [], |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 292 | "pw_proto_library requires .proto source files") |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 293 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 294 | _common = { |
| 295 | base_target = target_name |
| 296 | gen_dir = "$target_gen_dir/protos" |
| 297 | sources = invoker.sources |
| 298 | } |
| 299 | |
| 300 | if (defined(invoker.deps)) { |
| 301 | _deps = invoker.deps |
| 302 | } else { |
| 303 | _deps = [] |
| 304 | } |
| 305 | |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 306 | # For each proto target, create a file which collects the base directories of |
| 307 | # all of its dependencies to list as include paths to protoc. |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 308 | generated_file("$target_name._metadata") { |
| 309 | # Collect metadata from the include path files of each dependency. |
| 310 | deps = process_file_template(_deps, "{{source}}._metadata") |
| 311 | |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 312 | data_keys = [ "protoc_includes" ] |
Wyatt Hepler | 7c61caf | 2020-11-24 11:29:05 -0800 | [diff] [blame] | 313 | outputs = [ "$target_gen_dir/${_common.base_target}_includes.txt" ] |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 314 | |
| 315 | # Indicate this library's base directory for its dependents. |
| 316 | metadata = { |
Alexei Frolov | f79d227 | 2020-06-18 13:37:38 -0700 | [diff] [blame] | 317 | protoc_includes = [ rebase_path(".") ] |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 321 | # Toss any additional inputs into an input group dependency. |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 322 | if (defined(invoker.inputs)) { |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 323 | pw_input_group("$target_name._inputs") { |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 324 | inputs = invoker.inputs |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 325 | visibility = [ ":*" ] |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 326 | } |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 327 | } else { |
| 328 | group("$target_name._inputs") { |
| 329 | visibility = [ ":*" ] |
| 330 | } |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 333 | # Create a config with the generated proto directory, which is used for C++. |
| 334 | config("$target_name._include_path") { |
| 335 | include_dirs = [ _common.gen_dir ] |
| 336 | visibility = [ ":*" ] |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 339 | # Enumerate all of the protobuf generator targets. |
Alexei Frolov | c4b62ec | 2020-07-13 08:35:10 -0700 | [diff] [blame] | 340 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 341 | _pw_pwpb_proto_library("$target_name.pwpb") { |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 342 | forward_variables_from(invoker, _forwarded_vars) |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 343 | forward_variables_from(_common, "*") |
| 344 | deps = process_file_template(_deps, "{{source}}.pwpb") |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | if (dir_pw_third_party_nanopb != "") { |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 348 | _pw_nanopb_rpc_proto_library("$target_name.nanopb_rpc") { |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 349 | forward_variables_from(invoker, _forwarded_vars) |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 350 | forward_variables_from(_common, "*") |
| 351 | deps = process_file_template(_deps, "{{source}}.nanopb_rpc") |
Alexei Frolov | c4b62ec | 2020-07-13 08:35:10 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 354 | _pw_nanopb_proto_library("$target_name.nanopb") { |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 355 | forward_variables_from(invoker, _forwarded_vars) |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 356 | forward_variables_from(_common, "*") |
| 357 | deps = process_file_template(_deps, "{{source}}.nanopb") |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 358 | } |
| 359 | } else { |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 360 | pw_error("$target_name.nanopb_rpc") { |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 361 | message = |
| 362 | "\$dir_pw_third_party_nanopb must be set to generate nanopb RPC code." |
Alexei Frolov | 8185c82 | 2020-06-12 10:45:04 -0700 | [diff] [blame] | 363 | } |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 364 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 365 | pw_error("$target_name.nanopb") { |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 366 | message = |
| 367 | "\$dir_pw_third_party_nanopb must be set to compile nanopb protobufs." |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 371 | _pw_raw_rpc_proto_library("$target_name.raw_rpc") { |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 372 | forward_variables_from(invoker, _forwarded_vars) |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 373 | forward_variables_from(_common, "*", [ "deps" ]) |
| 374 | deps = process_file_template(_deps, "{{source}}.raw_rpc") |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 377 | _pw_go_proto_library("$target_name.go") { |
| 378 | sources = invoker.sources |
| 379 | deps = process_file_template(_deps, "{{source}}.go") |
| 380 | base_target = _common.base_target |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Alexei Frolov | 38cad0c | 2020-12-03 12:36:24 -0800 | [diff] [blame^] | 383 | _pw_python_proto_library("$target_name.python") { |
| 384 | sources = invoker.sources |
| 385 | forward_variables_from(_common, "*") |
| 386 | deps = process_file_template(_deps, "{{source}}.python") |
| 387 | base_target = _common.base_target |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 390 | # All supported pw_protobuf generators. |
| 391 | _protobuf_generators = [ |
| 392 | "pwpb", |
| 393 | "nanopb", |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 394 | "nanopb_rpc", |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame] | 395 | "raw_rpc", |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 396 | "go", |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 397 | "python", |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 398 | ] |
| 399 | |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 400 | # 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] | 401 | # generator targets, run a script which prints a nice error message. |
Wyatt Hepler | c8e05a4 | 2020-10-19 14:49:39 -0700 | [diff] [blame] | 402 | pw_python_action(target_name) { |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 403 | script = string_join("/", |
| 404 | [ |
| 405 | dir_pw_protobuf_compiler, |
| 406 | "py", |
| 407 | "pw_protobuf_compiler", |
| 408 | "proto_target_invalid.py", |
| 409 | ]) |
| 410 | args = [ |
| 411 | "--target", |
| 412 | target_name, |
| 413 | "--dir", |
| 414 | get_path_info(".", "abspath"), |
| 415 | "--root", |
| 416 | "//", |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 417 | ] + _protobuf_generators |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 418 | stamp = true |
| 419 | } |
| 420 | } |