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