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