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