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") |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 19 | import("$dir_pw_build/mirror_tree.gni") |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 20 | import("$dir_pw_build/python.gni") |
Wyatt Hepler | 51ded74 | 2020-10-19 14:45:27 -0700 | [diff] [blame] | 21 | import("$dir_pw_build/python_action.gni") |
Alexei Frolov | edd2f14 | 2020-06-09 19:11:27 -0700 | [diff] [blame] | 22 | import("$dir_pw_build/target_types.gni") |
Alexei Frolov | dd6fa5c | 2020-08-11 10:04:01 -0700 | [diff] [blame] | 23 | import("$dir_pw_third_party/nanopb/nanopb.gni") |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 24 | import("toolchain.gni") |
Wyatt Hepler | d49f8fe | 2020-10-15 10:13:47 -0700 | [diff] [blame] | 25 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 26 | # Variables forwarded from the public pw_proto_library template to the final |
| 27 | # pw_source_set. |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 28 | _forwarded_vars = [ |
| 29 | "testonly", |
| 30 | "visibility", |
| 31 | ] |
| 32 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 33 | # Internal template that invokes protoc with a pw_python_action. This should not |
| 34 | # be used outside of this file; use pw_proto_library instead. |
| 35 | # |
| 36 | # This creates the internal GN target $target_name.$language._gen that compiles |
| 37 | # proto files with protoc. |
| 38 | template("_pw_invoke_protoc") { |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 39 | if (current_toolchain == pw_protobuf_compiler_TOOLCHAIN) { |
Wyatt Hepler | a037764 | 2021-03-09 08:37:10 -0800 | [diff] [blame] | 40 | if (defined(invoker.out_dir)) { |
| 41 | _out_dir = invoker.out_dir |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 42 | } else { |
Wyatt Hepler | a037764 | 2021-03-09 08:37:10 -0800 | [diff] [blame] | 43 | _out_dir = "${invoker.base_out_dir}/${invoker.language}" |
| 44 | if (defined(invoker.module_as_package) && |
| 45 | invoker.module_as_package != "") { |
| 46 | assert(invoker.language == "python") |
| 47 | _out_dir = "$_out_dir/${invoker.module_as_package}" |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 48 | } |
| 49 | } |
Wyatt Hepler | a037764 | 2021-03-09 08:37:10 -0800 | [diff] [blame] | 50 | |
| 51 | _includes = |
| 52 | rebase_path(get_target_outputs(":${invoker.base_target}._includes")) |
| 53 | |
| 54 | pw_python_action("$target_name._gen") { |
| 55 | script = |
| 56 | "$dir_pw_protobuf_compiler/py/pw_protobuf_compiler/generate_protos.py" |
| 57 | |
| 58 | python_deps = [ "$dir_pw_protobuf_compiler/py" ] |
| 59 | if (defined(invoker.python_deps)) { |
| 60 | python_deps += invoker.python_deps |
| 61 | } |
| 62 | |
| 63 | deps = [ |
| 64 | ":${invoker.base_target}._includes", |
| 65 | ":${invoker.base_target}._sources", |
| 66 | ] |
| 67 | |
| 68 | foreach(dep, invoker.deps) { |
| 69 | deps += [ get_label_info(dep, "label_no_toolchain") + "._gen" ] |
| 70 | } |
| 71 | |
Wyatt Hepler | a3ca62a | 2021-05-04 16:21:43 -0700 | [diff] [blame] | 72 | if (defined(invoker.other_deps)) { |
| 73 | deps += invoker.other_deps |
| 74 | } |
| 75 | |
Wyatt Hepler | a037764 | 2021-03-09 08:37:10 -0800 | [diff] [blame] | 76 | args = [ |
| 77 | "--language", |
| 78 | invoker.language, |
| 79 | "--include-file", |
| 80 | _includes[0], |
| 81 | "--compile-dir", |
| 82 | rebase_path(invoker.compile_dir), |
| 83 | "--out-dir", |
| 84 | rebase_path(_out_dir), |
| 85 | "--sources", |
| 86 | ] + rebase_path(invoker.sources) |
| 87 | |
| 88 | if (defined(invoker.plugin)) { |
| 89 | inputs = [ invoker.plugin ] |
| 90 | args += [ "--plugin-path=" + rebase_path(invoker.plugin) ] |
| 91 | } |
| 92 | |
| 93 | if (defined(invoker.outputs)) { |
| 94 | outputs = invoker.outputs |
| 95 | } else { |
| 96 | stamp = true |
| 97 | } |
| 98 | |
| 99 | if (defined(invoker.metadata)) { |
| 100 | metadata = invoker.metadata |
Wyatt Hepler | a037764 | 2021-03-09 08:37:10 -0800 | [diff] [blame] | 101 | } |
| 102 | } |
Wyatt Hepler | cf184e8 | 2021-05-11 18:15:35 -0700 | [diff] [blame] | 103 | |
| 104 | # Output a .json file with information about this proto library. |
| 105 | _proto_info = { |
| 106 | label = get_label_info(":${invoker.target_name}", "label_no_toolchain") |
| 107 | protoc_outputs = rebase_path(get_target_outputs(":$target_name._gen")) |
| 108 | root = rebase_path(_out_dir) |
| 109 | package = invoker.package |
| 110 | |
| 111 | nested_in_python_package = "" |
| 112 | if (defined(invoker.python_package)) { |
| 113 | nested_in_python_package = |
| 114 | get_label_info(invoker.python_package, "label_no_toolchain") |
| 115 | } |
| 116 | |
| 117 | dependencies = [] |
| 118 | foreach(dep, invoker.deps) { |
| 119 | dependencies += |
| 120 | rebase_path([ get_label_info(dep, "target_gen_dir") + "/" + |
| 121 | get_label_info(dep, "name") + ".json" ]) |
| 122 | } |
| 123 | } |
| 124 | write_file("$target_gen_dir/$target_name.json", _proto_info, "json") |
Wyatt Hepler | a037764 | 2021-03-09 08:37:10 -0800 | [diff] [blame] | 125 | } else { |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 126 | # protoc is only ever invoked from pw_protobuf_compiler_TOOLCHAIN. |
Wyatt Hepler | a037764 | 2021-03-09 08:37:10 -0800 | [diff] [blame] | 127 | not_needed([ "target_name" ]) |
| 128 | not_needed(invoker, "*") |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 132 | # Generates pw_protobuf C++ code for proto files, creating a source_set of the |
| 133 | # generated files. This is internal and should not be used outside of this file. |
| 134 | # Use pw_proto_library instead. |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 135 | template("_pw_pwpb_proto_library") { |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 136 | _pw_invoke_protoc(target_name) { |
| 137 | forward_variables_from(invoker, "*", _forwarded_vars) |
| 138 | language = "pwpb" |
| 139 | plugin = "$dir_pw_protobuf/py/pw_protobuf/plugin.py" |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 140 | python_deps = [ "$dir_pw_protobuf/py" ] |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | # Create a library with the generated source files. |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 144 | config("$target_name._include_path") { |
| 145 | include_dirs = [ "${invoker.base_out_dir}/pwpb" ] |
| 146 | visibility = [ ":*" ] |
| 147 | } |
| 148 | |
Alexei Frolov | edd2f14 | 2020-06-09 19:11:27 -0700 | [diff] [blame] | 149 | pw_source_set(target_name) { |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 150 | forward_variables_from(invoker, _forwarded_vars) |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 151 | public_configs = [ ":$target_name._include_path" ] |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 152 | deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ] |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 153 | public_deps = [ dir_pw_protobuf ] + invoker.deps |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 154 | sources = invoker.outputs |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 155 | public = filter_include(sources, [ "*.pwpb.h" ]) |
| 156 | } |
| 157 | } |
| 158 | |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 159 | # Generates nanopb RPC code for proto files, creating a source_set of the |
| 160 | # generated files. This is internal and should not be used outside of this file. |
| 161 | # Use pw_proto_library instead. |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 162 | template("_pw_nanopb_rpc_proto_library") { |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 163 | # Create a target which runs protoc configured with the nanopb_rpc plugin to |
| 164 | # generate the C++ proto RPC headers. |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 165 | _pw_invoke_protoc(target_name) { |
| 166 | forward_variables_from(invoker, "*", _forwarded_vars) |
| 167 | language = "nanopb_rpc" |
| 168 | plugin = "$dir_pw_rpc/py/pw_rpc/plugin_nanopb.py" |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 169 | python_deps = [ "$dir_pw_rpc/py" ] |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | # Create a library with the generated source files. |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 173 | config("$target_name._include_path") { |
| 174 | include_dirs = [ "${invoker.base_out_dir}/nanopb_rpc" ] |
| 175 | visibility = [ ":*" ] |
| 176 | } |
| 177 | |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 178 | pw_source_set(target_name) { |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 179 | forward_variables_from(invoker, _forwarded_vars) |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 180 | public_configs = [ ":$target_name._include_path" ] |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 181 | deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ] |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 182 | public_deps = [ |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 183 | ":${invoker.base_target}.nanopb", |
Wyatt Hepler | cbd09c2 | 2020-09-15 11:17:24 -0700 | [diff] [blame] | 184 | "$dir_pw_rpc:server", |
Alexei Frolov | 2d737bc | 2021-04-27 23:03:09 -0700 | [diff] [blame] | 185 | "$dir_pw_rpc/nanopb:client", |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 186 | "$dir_pw_rpc/nanopb:method_union", |
Alexei Frolov | dd6fa5c | 2020-08-11 10:04:01 -0700 | [diff] [blame] | 187 | "$dir_pw_third_party/nanopb", |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 188 | ] + invoker.deps |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 189 | public = invoker.outputs |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 193 | # Generates nanopb code for proto files, creating a source_set of the generated |
| 194 | # files. This is internal and should not be used outside of this file. Use |
| 195 | # pw_proto_library instead. |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 196 | template("_pw_nanopb_proto_library") { |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 197 | # When compiling with the Nanopb plugin, the nanopb.proto file is already |
| 198 | # compiled internally, so skip recompiling it with protoc. |
| 199 | if (rebase_path(invoker.sources, invoker.compile_dir) == [ "nanopb.proto" ]) { |
| 200 | group("$target_name._gen") { |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 201 | deps = [ |
| 202 | ":${invoker.base_target}._sources($pw_protobuf_compiler_TOOLCHAIN)", |
| 203 | ] |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 204 | } |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 205 | |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 206 | group("$target_name") { |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 207 | deps = invoker.deps + |
| 208 | [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ] |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 209 | } |
| 210 | } else { |
| 211 | # Create a target which runs protoc configured with the nanopb plugin to |
| 212 | # generate the C proto sources. |
| 213 | _pw_invoke_protoc(target_name) { |
| 214 | forward_variables_from(invoker, "*", _forwarded_vars) |
| 215 | language = "nanopb" |
| 216 | plugin = "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb" |
Wyatt Hepler | a3ca62a | 2021-05-04 16:21:43 -0700 | [diff] [blame] | 217 | other_deps = [ "$dir_pw_third_party/nanopb:generate_nanopb_proto.action" ] |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | # Create a library with the generated source files. |
| 221 | config("$target_name._include_path") { |
| 222 | include_dirs = [ "${invoker.base_out_dir}/nanopb" ] |
| 223 | visibility = [ ":*" ] |
| 224 | } |
| 225 | |
| 226 | pw_source_set(target_name) { |
| 227 | forward_variables_from(invoker, _forwarded_vars) |
| 228 | public_configs = [ ":$target_name._include_path" ] |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 229 | deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ] |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 230 | public_deps = [ "$dir_pw_third_party/nanopb" ] + invoker.deps |
| 231 | sources = invoker.outputs |
| 232 | public = filter_include(sources, [ "*.pb.h" ]) |
Wyatt Hepler | b93749b | 2021-05-11 09:38:22 -0700 | [diff] [blame] | 233 | check_includes = false |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 234 | } |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame] | 238 | # Generates raw RPC code for proto files, creating a source_set of the generated |
| 239 | # files. This is internal and should not be used outside of this file. Use |
| 240 | # pw_proto_library instead. |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame] | 241 | template("_pw_raw_rpc_proto_library") { |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame] | 242 | # Create a target which runs protoc configured with the nanopb_rpc plugin to |
| 243 | # generate the C++ proto RPC headers. |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 244 | _pw_invoke_protoc(target_name) { |
| 245 | forward_variables_from(invoker, "*", _forwarded_vars) |
| 246 | language = "raw_rpc" |
| 247 | plugin = "$dir_pw_rpc/py/pw_rpc/plugin_raw.py" |
Wyatt Hepler | 438caa0 | 2021-01-15 17:13:11 -0800 | [diff] [blame] | 248 | python_deps = [ "$dir_pw_rpc/py" ] |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | # Create a library with the generated source files. |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 252 | config("$target_name._include_path") { |
| 253 | include_dirs = [ "${invoker.base_out_dir}/raw_rpc" ] |
| 254 | visibility = [ ":*" ] |
| 255 | } |
| 256 | |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame] | 257 | pw_source_set(target_name) { |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 258 | forward_variables_from(invoker, _forwarded_vars) |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 259 | public_configs = [ ":$target_name._include_path" ] |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 260 | deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ] |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame] | 261 | public_deps = [ |
| 262 | "$dir_pw_rpc:server", |
| 263 | "$dir_pw_rpc/raw:method_union", |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 264 | ] + invoker.deps |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 265 | public = invoker.outputs |
Wyatt Hepler | b93749b | 2021-05-11 09:38:22 -0700 | [diff] [blame] | 266 | check_includes = false |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 270 | # Generates Go code for proto files, listing the proto output directory in the |
| 271 | # metadata variable GOPATH. Internal use only. |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 272 | template("_pw_go_proto_library") { |
| 273 | _proto_gopath = "$root_gen_dir/go" |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 274 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 275 | _pw_invoke_protoc(target_name) { |
| 276 | forward_variables_from(invoker, "*") |
| 277 | language = "go" |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 278 | metadata = { |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 279 | gopath = [ "GOPATH+=" + rebase_path(_proto_gopath) ] |
Alexei Frolov | c15a988 | 2019-12-23 14:29:02 -0800 | [diff] [blame] | 280 | external_deps = [ |
| 281 | "github.com/golang/protobuf/proto", |
| 282 | "google.golang.org/grpc", |
| 283 | ] |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 284 | } |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 285 | |
| 286 | # Override the default "$base_out_dir/$language" output path. |
| 287 | out_dir = "$_proto_gopath/src" |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | group(target_name) { |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 291 | deps = |
| 292 | invoker.deps + [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ] |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 296 | # Generates Python code for proto files, creating a pw_python_package containing |
| 297 | # the generated files. This is internal and should not be used outside of this |
| 298 | # file. Use pw_proto_library instead. |
| 299 | template("_pw_python_proto_library") { |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 300 | _pw_invoke_protoc(target_name) { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 301 | forward_variables_from(invoker, "*", _forwarded_vars + [ "python_package" ]) |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 302 | language = "python" |
Wyatt Hepler | 4b921c1 | 2021-03-05 15:33:29 -0800 | [diff] [blame] | 303 | python_deps = [ "$dir_pw_protobuf_compiler:protobuf_requirements" ] |
Wyatt Hepler | cf184e8 | 2021-05-11 18:15:35 -0700 | [diff] [blame] | 304 | |
| 305 | if (defined(invoker.python_package)) { |
| 306 | python_package = invoker.python_package |
| 307 | } |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 310 | if (defined(invoker.python_package) && invoker.python_package != "") { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 311 | # If anyone attempts to depend on this Python package, print an error. |
| 312 | pw_error(target_name) { |
| 313 | _pkg = get_label_info(invoker.python_package, "label_no_toolchain") |
| 314 | message_lines = [ |
| 315 | "This proto Python package is embedded in the $_pkg Python package.", |
| 316 | "It cannot be used directly; instead, depend on $_pkg.", |
| 317 | ] |
Wyatt Hepler | d517afc | 2021-02-03 19:40:08 -0800 | [diff] [blame] | 318 | } |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 319 | foreach(subtarget, pw_python_package_subtargets) { |
| 320 | group("$target_name.$subtarget") { |
| 321 | deps = [ ":${invoker.target_name}" ] |
| 322 | } |
| 323 | } |
Wyatt Hepler | d517afc | 2021-02-03 19:40:08 -0800 | [diff] [blame] | 324 | |
Wyatt Hepler | cf184e8 | 2021-05-11 18:15:35 -0700 | [diff] [blame] | 325 | # This proto library is merged into another package, but create a target to |
| 326 | # collect its dependencies that the other package can depend on. |
| 327 | pw_python_group(target_name + "._deps") { |
| 328 | python_deps = invoker.deps |
| 329 | other_deps = |
| 330 | [ ":${invoker.target_name}._gen($pw_protobuf_compiler_TOOLCHAIN)" ] |
| 331 | } |
| 332 | } else { |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 333 | # Create a Python package with the generated source files. |
| 334 | pw_python_package(target_name) { |
| 335 | forward_variables_from(invoker, _forwarded_vars) |
| 336 | generate_setup = { |
| 337 | name = invoker._package_dir |
| 338 | version = "0.0.1" # TODO(hepler): Need to be able to set this verison. |
| 339 | } |
| 340 | sources = invoker.outputs |
| 341 | strip_prefix = "${invoker.base_out_dir}/python" |
| 342 | python_deps = invoker.deps |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 343 | other_deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ] |
Wyatt Hepler | c2ce524 | 2021-03-17 08:42:14 -0700 | [diff] [blame] | 344 | static_analysis = [] |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 345 | |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 346 | _pw_module_as_package = invoker.module_as_package != "" |
| 347 | } |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 351 | # Generates protobuf code from .proto definitions for various languages. |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 352 | # For each supported generator, creates a sub-target named: |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 353 | # |
Alexei Frolov | 8e30d46 | 2020-10-22 13:54:36 -0700 | [diff] [blame] | 354 | # <target_name>.<generator> |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 355 | # |
Wyatt Hepler | 4cdc3e7 | 2021-03-24 08:41:08 -0700 | [diff] [blame] | 356 | # GN permits using abbreviated labels when the target name matches the directory |
| 357 | # name (e.g. //foo for //foo:foo). For consistency with this, the sub-targets |
| 358 | # for each generator are aliased to the directory when the target name is the |
| 359 | # same. For example, these two labels are equivalent: |
| 360 | # |
| 361 | # //path/to/my_protos:my_protos.pwpb |
| 362 | # //path/to/my_protos:pwpb |
| 363 | # |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 364 | # pw_protobuf_library targets generate Python packages. As such, they must have |
| 365 | # globally unique package names. The first directory of the prefix or the first |
| 366 | # common directory of the sources is used as the Python package. |
| 367 | # |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 368 | # Args: |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 369 | # sources: List of input .proto files. |
| 370 | # deps: List of other pw_proto_library dependencies. |
| 371 | # inputs: Other files on which the protos depend (e.g. nanopb .options files). |
| 372 | # prefix: A prefix to add to the source protos prior to compilation. For |
| 373 | # example, a source called "foo.proto" with prefix = "nested" will be |
| 374 | # compiled with protoc as "nested/foo.proto". |
| 375 | # strip_prefix: Remove this prefix from the source protos. All source and |
| 376 | # input files must be nested under this path. |
Wyatt Hepler | 4cdc3e7 | 2021-03-24 08:41:08 -0700 | [diff] [blame] | 377 | # python_package: Label of Python package to which to add the proto modules. |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 378 | # |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 379 | template("pw_proto_library") { |
| 380 | assert(defined(invoker.sources) && invoker.sources != [], |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 381 | "pw_proto_library requires .proto source files") |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 382 | |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 383 | if (defined(invoker.python_module_as_package)) { |
| 384 | _module_as_package = invoker.python_module_as_package |
| 385 | |
| 386 | _must_be_one_source = invoker.sources |
| 387 | assert([ _must_be_one_source[0] ] == _must_be_one_source, |
| 388 | "'python_module_as_package' requires exactly one source file") |
| 389 | assert(_module_as_package != "", |
| 390 | "'python_module_as_package' cannot be be empty") |
| 391 | assert(string_split(_module_as_package, "/") == [ _module_as_package ], |
| 392 | "'python_module_as_package' cannot contain slashes") |
| 393 | assert(!defined(invoker.prefix), |
| 394 | "'prefix' cannot be provided with 'python_module_as_package'") |
| 395 | } else { |
| 396 | _module_as_package = "" |
| 397 | } |
| 398 | |
| 399 | if (defined(invoker.strip_prefix)) { |
| 400 | _source_root = get_path_info(invoker.strip_prefix, "abspath") |
| 401 | } else { |
| 402 | _source_root = get_path_info(".", "abspath") |
| 403 | } |
| 404 | |
| 405 | if (defined(invoker.prefix)) { |
| 406 | _prefix = invoker.prefix |
| 407 | } else { |
| 408 | _prefix = "" |
| 409 | } |
| 410 | |
Wyatt Hepler | 9174147 | 2021-02-03 08:45:10 -0800 | [diff] [blame] | 411 | _package_dir = "" |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 412 | _source_names = [] |
Wyatt Hepler | 9174147 | 2021-02-03 08:45:10 -0800 | [diff] [blame] | 413 | |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 414 | # Determine the Python package name to use for these protos. If there is no |
| 415 | # prefix, the first directory the sources are nested under is used. |
| 416 | foreach(source, rebase_path(invoker.sources, _source_root)) { |
Wyatt Hepler | 9174147 | 2021-02-03 08:45:10 -0800 | [diff] [blame] | 417 | _path_components = [] |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 418 | _path_components = string_split(source, "/") |
Wyatt Hepler | 9174147 | 2021-02-03 08:45:10 -0800 | [diff] [blame] | 419 | |
| 420 | if (_package_dir == "") { |
| 421 | _package_dir = _path_components[0] |
| 422 | } else { |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 423 | assert(_prefix != "" || _path_components[0] == _package_dir, |
| 424 | "Unless 'prefix' is supplied, all .proto sources in a " + |
| 425 | "pw_proto_library must be in the same directory tree") |
Wyatt Hepler | 9174147 | 2021-02-03 08:45:10 -0800 | [diff] [blame] | 426 | } |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 427 | |
| 428 | _source_names += |
| 429 | [ get_path_info(source, "dir") + "/" + get_path_info(source, "name") ] |
Wyatt Hepler | 9174147 | 2021-02-03 08:45:10 -0800 | [diff] [blame] | 430 | } |
| 431 | |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 432 | # If the 'prefix' was supplied, use that for the package directory. |
| 433 | if (_prefix != "") { |
| 434 | _prefix_path_components = string_split(_prefix, "/") |
| 435 | _package_dir = _prefix_path_components[0] |
| 436 | } |
| 437 | |
| 438 | assert(_package_dir != "" && _package_dir != "." && _package_dir != "..", |
| 439 | "Either a 'prefix' must be specified or all sources must be nested " + |
| 440 | "under a common directory") |
| 441 | |
| 442 | # Define an action that is never executed to prevent duplicate proto packages |
| 443 | # from being declared. The target name and the output file include only the |
| 444 | # package directory, so different targets that use the same proto package name |
| 445 | # will conflict. |
| 446 | action("pw_proto_library.$_package_dir") { |
| 447 | script = "$dir_pw_build/py/pw_build/nop.py" |
Wyatt Hepler | 9174147 | 2021-02-03 08:45:10 -0800 | [diff] [blame] | 448 | visibility = [] |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 449 | |
| 450 | # Place an error message in the output path (which is never created). If the |
| 451 | # package name conflicts occur in different BUILD.gn files, this results in |
| 452 | # an otherwise cryptic Ninja error, rather than a GN error. |
| 453 | outputs = [ "$root_out_dir/ " + |
| 454 | "ERROR - Multiple pw_proto_library targets create the " + |
| 455 | "'$_package_dir' package. Change the package name by setting " + |
| 456 | "the \"prefix\" arg or move the protos to a different " + |
| 457 | "directory, then re-run gn gen." ] |
Wyatt Hepler | 9174147 | 2021-02-03 08:45:10 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 460 | if (defined(invoker.deps)) { |
| 461 | _deps = invoker.deps |
| 462 | } else { |
| 463 | _deps = [] |
| 464 | } |
| 465 | |
Wyatt Hepler | cf184e8 | 2021-05-11 18:15:35 -0700 | [diff] [blame] | 466 | _common = { |
| 467 | base_target = target_name |
| 468 | |
| 469 | # This is the output directory for all files related to this proto library. |
| 470 | # Sources are mirrored to "$base_out_dir/sources" and protoc puts outputs in |
| 471 | # "$base_out_dir/$language" by default. |
| 472 | base_out_dir = |
| 473 | get_label_info(":$target_name($pw_protobuf_compiler_TOOLCHAIN)", |
| 474 | "target_gen_dir") + "/$target_name.proto_library" |
| 475 | |
| 476 | compile_dir = "$base_out_dir/sources" |
| 477 | |
| 478 | # Refer to the source files as the are mirrored to the output directory. |
| 479 | sources = [] |
| 480 | foreach(file, rebase_path(invoker.sources, _source_root)) { |
| 481 | sources += [ "$compile_dir/$_prefix/$file" ] |
| 482 | } |
| 483 | |
| 484 | package = _package_dir |
| 485 | } |
| 486 | |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 487 | # For each proto target, create a file which collects the base directories of |
| 488 | # all of its dependencies to list as include paths to protoc. |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 489 | generated_file("$target_name._includes") { |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 490 | # Collect metadata from the include path files of each dependency. |
Wyatt Hepler | 4cdc3e7 | 2021-03-24 08:41:08 -0700 | [diff] [blame] | 491 | |
| 492 | deps = [] |
| 493 | foreach(dep, _deps) { |
| 494 | _base = get_label_info(dep, "label_no_toolchain") |
| 495 | deps += [ "$_base._includes(" + get_label_info(dep, "toolchain") + ")" ] |
| 496 | } |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 497 | |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 498 | data_keys = [ "protoc_includes" ] |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 499 | outputs = [ "${_common.base_out_dir}/includes.txt" ] |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 500 | |
| 501 | # Indicate this library's base directory for its dependents. |
| 502 | metadata = { |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 503 | protoc_includes = [ rebase_path(_common.compile_dir) ] |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 507 | # Mirror the proto sources to the output directory with the prefix added. |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 508 | if (current_toolchain == pw_protobuf_compiler_TOOLCHAIN) { |
| 509 | pw_mirror_tree("$target_name._sources") { |
| 510 | source_root = _source_root |
| 511 | sources = invoker.sources |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 512 | |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 513 | if (defined(invoker.inputs)) { |
| 514 | sources += invoker.inputs |
| 515 | } |
| 516 | |
| 517 | directory = "${_common.compile_dir}/$_prefix" |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 518 | } |
Wyatt Hepler | f6f74f4 | 2021-04-13 19:26:20 -0700 | [diff] [blame] | 519 | } else { |
| 520 | not_needed(invoker, [ "inputs" ]) |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 523 | # Enumerate all of the protobuf generator targets. |
Alexei Frolov | c4b62ec | 2020-07-13 08:35:10 -0700 | [diff] [blame] | 524 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 525 | _pw_pwpb_proto_library("$target_name.pwpb") { |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 526 | forward_variables_from(invoker, _forwarded_vars) |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 527 | forward_variables_from(_common, "*") |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 528 | |
| 529 | deps = [] |
| 530 | foreach(dep, _deps) { |
| 531 | _base = get_label_info(dep, "label_no_toolchain") |
| 532 | deps += [ "$_base.pwpb(" + get_label_info(dep, "toolchain") + ")" ] |
| 533 | } |
| 534 | |
| 535 | outputs = [] |
| 536 | foreach(name, _source_names) { |
| 537 | outputs += [ "$base_out_dir/pwpb/$_prefix/${name}.pwpb.h" ] |
| 538 | } |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | if (dir_pw_third_party_nanopb != "") { |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 542 | _pw_nanopb_rpc_proto_library("$target_name.nanopb_rpc") { |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 543 | forward_variables_from(invoker, _forwarded_vars) |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 544 | forward_variables_from(_common, "*") |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 545 | |
| 546 | deps = [] |
| 547 | foreach(dep, _deps) { |
| 548 | _lbl = get_label_info(dep, "label_no_toolchain") |
| 549 | deps += [ "$_lbl.nanopb_rpc(" + get_label_info(dep, "toolchain") + ")" ] |
| 550 | } |
| 551 | |
| 552 | outputs = [] |
| 553 | foreach(name, _source_names) { |
| 554 | outputs += [ "$base_out_dir/nanopb_rpc/$_prefix/${name}.rpc.pb.h" ] |
| 555 | } |
Alexei Frolov | c4b62ec | 2020-07-13 08:35:10 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 558 | _pw_nanopb_proto_library("$target_name.nanopb") { |
| 559 | forward_variables_from(invoker, _forwarded_vars) |
| 560 | forward_variables_from(_common, "*") |
| 561 | |
| 562 | deps = [] |
| 563 | foreach(dep, _deps) { |
| 564 | _base = get_label_info(dep, "label_no_toolchain") |
| 565 | deps += [ "$_base.nanopb(" + get_label_info(dep, "toolchain") + ")" ] |
Wyatt Hepler | d517afc | 2021-02-03 19:40:08 -0800 | [diff] [blame] | 566 | } |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 567 | |
| 568 | outputs = [] |
| 569 | foreach(name, _source_names) { |
| 570 | outputs += [ |
| 571 | "$base_out_dir/nanopb/$_prefix/${name}.pb.h", |
| 572 | "$base_out_dir/nanopb/$_prefix/${name}.pb.c", |
| 573 | ] |
Wyatt Hepler | d517afc | 2021-02-03 19:40:08 -0800 | [diff] [blame] | 574 | } |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 575 | } |
| 576 | } else { |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 577 | pw_error("$target_name.nanopb_rpc") { |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 578 | message = |
| 579 | "\$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] | 580 | } |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 581 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 582 | pw_error("$target_name.nanopb") { |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 583 | message = |
| 584 | "\$dir_pw_third_party_nanopb must be set to compile nanopb protobufs." |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 588 | _pw_raw_rpc_proto_library("$target_name.raw_rpc") { |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 589 | forward_variables_from(invoker, _forwarded_vars) |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 590 | forward_variables_from(_common, "*") |
| 591 | |
| 592 | deps = [] |
| 593 | foreach(dep, _deps) { |
| 594 | _base = get_label_info(dep, "label_no_toolchain") |
| 595 | deps += [ "$_base.raw_rpc(" + get_label_info(dep, "toolchain") + ")" ] |
| 596 | } |
| 597 | |
| 598 | outputs = [] |
| 599 | foreach(name, _source_names) { |
| 600 | outputs += [ "$base_out_dir/raw_rpc/$_prefix/${name}.raw_rpc.pb.h" ] |
| 601 | } |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 604 | _pw_go_proto_library("$target_name.go") { |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 605 | sources = _common.sources |
| 606 | |
| 607 | deps = [] |
| 608 | foreach(dep, _deps) { |
| 609 | _base = get_label_info(dep, "label_no_toolchain") |
| 610 | deps += [ "$_base.go(" + get_label_info(dep, "toolchain") + ")" ] |
| 611 | } |
| 612 | |
| 613 | forward_variables_from(_common, "*") |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Alexei Frolov | 38cad0c | 2020-12-03 12:36:24 -0800 | [diff] [blame] | 616 | _pw_python_proto_library("$target_name.python") { |
Alexei Frolov | 38cad0c | 2020-12-03 12:36:24 -0800 | [diff] [blame] | 617 | forward_variables_from(_common, "*") |
Wyatt Hepler | dcfcecf | 2021-03-01 08:36:19 -0800 | [diff] [blame] | 618 | forward_variables_from(invoker, [ "python_package" ]) |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 619 | module_as_package = _module_as_package |
| 620 | |
| 621 | deps = [] |
| 622 | foreach(dep, _deps) { |
| 623 | _base = get_label_info(dep, "label_no_toolchain") |
| 624 | deps += [ "$_base.python(" + get_label_info(dep, "toolchain") + ")" ] |
| 625 | } |
| 626 | |
| 627 | if (module_as_package == "") { |
| 628 | _python_prefix = "$base_out_dir/python/$_prefix" |
| 629 | } else { |
| 630 | _python_prefix = "$base_out_dir/python/$module_as_package" |
| 631 | } |
| 632 | |
| 633 | outputs = [] |
| 634 | foreach(name, _source_names) { |
| 635 | outputs += [ |
| 636 | "$_python_prefix/${name}_pb2.py", |
| 637 | "$_python_prefix/${name}_pb2.pyi", |
| 638 | ] |
| 639 | } |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 640 | } |
| 641 | |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 642 | # All supported pw_protobuf generators. |
| 643 | _protobuf_generators = [ |
| 644 | "pwpb", |
| 645 | "nanopb", |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 646 | "nanopb_rpc", |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame] | 647 | "raw_rpc", |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 648 | "go", |
Alexei Frolov | a4c0aee | 2020-12-01 13:48:48 -0800 | [diff] [blame] | 649 | "python", |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 650 | ] |
| 651 | |
Wyatt Hepler | 4cdc3e7 | 2021-03-24 08:41:08 -0700 | [diff] [blame] | 652 | # If the label matches the directory name, alias the subtargets to the |
| 653 | # directory (e.g. //foo:nanopb is an alias for //foo:foo.nanopb). |
| 654 | if (get_label_info(":$target_name", "name") == |
| 655 | get_path_info(get_label_info(":$target_name", "dir"), "name")) { |
Wyatt Hepler | 2c6c0ba | 2021-04-07 09:50:23 -0700 | [diff] [blame] | 656 | foreach(_generator, _protobuf_generators - [ "python" ]) { |
| 657 | group(_generator) { |
Wyatt Hepler | 4cdc3e7 | 2021-03-24 08:41:08 -0700 | [diff] [blame] | 658 | public_deps = [ ":${invoker.target_name}.$_generator" ] |
| 659 | } |
| 660 | } |
Wyatt Hepler | 2c6c0ba | 2021-04-07 09:50:23 -0700 | [diff] [blame] | 661 | |
| 662 | pw_python_group("python") { |
| 663 | python_deps = [ ":${invoker.target_name}.python" ] |
| 664 | } |
Wyatt Hepler | 4cdc3e7 | 2021-03-24 08:41:08 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 667 | # 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] | 668 | # generator targets, run a script which prints a nice error message. |
Wyatt Hepler | c8e05a4 | 2020-10-19 14:49:39 -0700 | [diff] [blame] | 669 | pw_python_action(target_name) { |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 670 | script = string_join("/", |
| 671 | [ |
| 672 | dir_pw_protobuf_compiler, |
| 673 | "py", |
| 674 | "pw_protobuf_compiler", |
| 675 | "proto_target_invalid.py", |
| 676 | ]) |
| 677 | args = [ |
| 678 | "--target", |
| 679 | target_name, |
| 680 | "--dir", |
| 681 | get_path_info(".", "abspath"), |
| 682 | "--root", |
| 683 | "//", |
Alexei Frolov | b499d3f | 2020-10-28 13:00:08 -0700 | [diff] [blame] | 684 | ] + _protobuf_generators |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 685 | stamp = true |
| 686 | } |
| 687 | } |