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