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 | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 17 | import("$dir_pw_build/input_group.gni") |
Wyatt Hepler | 51ded74 | 2020-10-19 14:45:27 -0700 | [diff] [blame] | 18 | import("$dir_pw_build/python_action.gni") |
Alexei Frolov | edd2f14 | 2020-06-09 19:11:27 -0700 | [diff] [blame] | 19 | import("$dir_pw_build/target_types.gni") |
Alexei Frolov | dd6fa5c | 2020-08-11 10:04:01 -0700 | [diff] [blame] | 20 | import("$dir_pw_third_party/nanopb/nanopb.gni") |
Wyatt Hepler | d49f8fe | 2020-10-15 10:13:47 -0700 | [diff] [blame] | 21 | |
Alexei Frolov | 4c0428a | 2020-06-10 10:46:04 -0700 | [diff] [blame] | 22 | declare_args() { |
| 23 | # Generators with which to compile protobuf code. These are used by the |
| 24 | # pw_proto_library template to determine which build targets to create. |
| 25 | # |
| 26 | # Supported generators: |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame^] | 27 | # "pwpb", "nanopb", "nanopb_rpc", "raw_rpc", "go" |
Alexei Frolov | 4c0428a | 2020-06-10 10:46:04 -0700 | [diff] [blame] | 28 | pw_protobuf_GENERATORS = [ |
| 29 | "pwpb", |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame^] | 30 | "raw_rpc", |
Alexei Frolov | 4c0428a | 2020-06-10 10:46:04 -0700 | [diff] [blame] | 31 | "go", |
| 32 | ] |
| 33 | } |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 34 | |
| 35 | # Python script that invokes protoc. |
| 36 | _gen_script_path = |
| 37 | "$dir_pw_protobuf_compiler/py/pw_protobuf_compiler/generate_protos.py" |
| 38 | |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 39 | _forwarded_vars = [ |
| 40 | "testonly", |
| 41 | "visibility", |
| 42 | ] |
| 43 | |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 44 | # Generates pw_protobuf C++ code for proto files, creating a source_set of the |
| 45 | # generated files. This is internal and should not be used outside of this file. |
| 46 | # Use pw_proto_library instead. |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 47 | # |
| 48 | # Args: |
| 49 | # protos: List of input .proto files. |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 50 | template("_pw_pwpb_proto_library") { |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 51 | _proto_gen_dir = "$root_gen_dir/protos" |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 52 | _module_path = get_path_info(".", "abspath") |
| 53 | _relative_proto_paths = rebase_path(invoker.protos, _module_path) |
| 54 | |
| 55 | _outputs = [] |
| 56 | foreach(_proto, _relative_proto_paths) { |
| 57 | _output = string_replace(_proto, ".proto", ".pwpb.h") |
| 58 | _outputs += [ "$_proto_gen_dir/$_output" ] |
| 59 | } |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 60 | |
| 61 | _gen_target = "${target_name}_gen" |
Wyatt Hepler | c8e05a4 | 2020-10-19 14:49:39 -0700 | [diff] [blame] | 62 | pw_python_action(_gen_target) { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 63 | forward_variables_from(invoker, _forwarded_vars) |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 64 | script = _gen_script_path |
| 65 | args = [ |
| 66 | "--language", |
| 67 | "cc", |
| 68 | "--module-path", |
Wyatt Hepler | 8224a64 | 2020-07-29 08:55:56 -0700 | [diff] [blame] | 69 | rebase_path(_module_path), |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 70 | "--include-file", |
Wyatt Hepler | 8224a64 | 2020-07-29 08:55:56 -0700 | [diff] [blame] | 71 | rebase_path(invoker.include_file), |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 72 | "--out-dir", |
Wyatt Hepler | 8224a64 | 2020-07-29 08:55:56 -0700 | [diff] [blame] | 73 | rebase_path(_proto_gen_dir), |
| 74 | ] + rebase_path(invoker.protos) |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 75 | inputs = invoker.protos |
| 76 | outputs = _outputs |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 77 | deps = invoker.deps |
Alexei Frolov | bbf164c | 2019-12-16 12:51:59 -0800 | [diff] [blame] | 78 | if (defined(invoker.protoc_deps)) { |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 79 | deps += invoker.protoc_deps |
Alexei Frolov | bbf164c | 2019-12-16 12:51:59 -0800 | [diff] [blame] | 80 | } |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | # For C++ proto files, the generated proto directory is added as an include |
| 84 | # path for the code. This requires using "all_dependent_configs" to force the |
| 85 | # include on any code that transitively depends on the generated protos. |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 86 | _include_config_target = "${target_name}_includes" |
| 87 | config(_include_config_target) { |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 88 | include_dirs = [ "$_proto_gen_dir" ] |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | # Create a library with the generated source files. |
Alexei Frolov | edd2f14 | 2020-06-09 19:11:27 -0700 | [diff] [blame] | 92 | pw_source_set(target_name) { |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 93 | all_dependent_configs = [ ":$_include_config_target" ] |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 94 | deps = [ ":$_gen_target" ] |
| 95 | public_deps = [ dir_pw_protobuf ] + invoker.gen_deps |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 96 | sources = get_target_outputs(":$_gen_target") |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 97 | public = filter_include(sources, [ "*.pwpb.h" ]) |
| 98 | } |
| 99 | } |
| 100 | |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 101 | # Generates nanopb RPC code for proto files, creating a source_set of the |
| 102 | # generated files. This is internal and should not be used outside of this file. |
| 103 | # Use pw_proto_library instead. |
| 104 | # |
| 105 | # Args: |
| 106 | # protos: List of input .proto files. |
| 107 | # |
| 108 | template("_pw_nanopb_rpc_proto_library") { |
| 109 | assert(defined(dir_pw_third_party_nanopb) && dir_pw_third_party_nanopb != "", |
| 110 | "\$dir_pw_third_party_nanopb must be set to compile nanopb protobufs") |
| 111 | |
| 112 | _proto_gen_dir = "$root_gen_dir/protos" |
| 113 | _module_path = get_path_info(".", "abspath") |
| 114 | _relative_proto_paths = rebase_path(invoker.protos, _module_path) |
| 115 | |
| 116 | _outputs = [] |
| 117 | foreach(_proto, _relative_proto_paths) { |
Alexei Frolov | 3ab26ff | 2020-07-21 10:44:58 -0700 | [diff] [blame] | 118 | _output_h = string_replace(_proto, ".proto", ".rpc.pb.h") |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 119 | _outputs += [ "$_proto_gen_dir/$_output_h" ] |
| 120 | } |
| 121 | |
| 122 | # Create a target which runs protoc configured with the nanopb_rpc plugin to |
| 123 | # generate the C++ proto RPC headers. |
| 124 | _gen_target = "${target_name}_gen" |
Wyatt Hepler | c8e05a4 | 2020-10-19 14:49:39 -0700 | [diff] [blame] | 125 | pw_python_action(_gen_target) { |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 126 | forward_variables_from(invoker, _forwarded_vars) |
| 127 | script = _gen_script_path |
| 128 | args = [ |
| 129 | "--language", |
| 130 | "nanopb_rpc", |
| 131 | "--module-path", |
Wyatt Hepler | 8224a64 | 2020-07-29 08:55:56 -0700 | [diff] [blame] | 132 | rebase_path(_module_path), |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 133 | "--include-paths", |
Wyatt Hepler | 8224a64 | 2020-07-29 08:55:56 -0700 | [diff] [blame] | 134 | rebase_path("$dir_pw_third_party_nanopb/generator/proto"), |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 135 | "--include-file", |
Wyatt Hepler | 8224a64 | 2020-07-29 08:55:56 -0700 | [diff] [blame] | 136 | rebase_path(invoker.include_file), |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 137 | "--out-dir", |
Wyatt Hepler | 8224a64 | 2020-07-29 08:55:56 -0700 | [diff] [blame] | 138 | rebase_path(_proto_gen_dir), |
| 139 | ] + rebase_path(invoker.protos) |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 140 | inputs = invoker.protos |
| 141 | outputs = _outputs |
| 142 | |
| 143 | deps = invoker.deps |
| 144 | if (defined(invoker.protoc_deps)) { |
| 145 | deps += invoker.protoc_deps |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | # For C++ proto files, the generated proto directory is added as an include |
| 150 | # path for the code. This requires using "all_dependent_configs" to force the |
| 151 | # include on any code that transitively depends on the generated protos. |
| 152 | _include_root = rebase_path(get_path_info(".", "abspath"), "//") |
| 153 | _include_config_target = "${target_name}_includes" |
| 154 | config(_include_config_target) { |
| 155 | include_dirs = [ |
| 156 | "$_proto_gen_dir", |
| 157 | "$_proto_gen_dir/$_include_root", |
| 158 | ] |
| 159 | } |
| 160 | |
| 161 | # Create a library with the generated source files. |
| 162 | pw_source_set(target_name) { |
| 163 | all_dependent_configs = [ ":$_include_config_target" ] |
| 164 | deps = [ ":$_gen_target" ] |
| 165 | public_deps = [ |
Wyatt Hepler | cbd09c2 | 2020-09-15 11:17:24 -0700 | [diff] [blame] | 166 | "$dir_pw_rpc:server", |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 167 | "$dir_pw_rpc/nanopb:method_union", |
Alexei Frolov | dd6fa5c | 2020-08-11 10:04:01 -0700 | [diff] [blame] | 168 | "$dir_pw_third_party/nanopb", |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 169 | ] + invoker.gen_deps |
| 170 | public = get_target_outputs(":$_gen_target") |
| 171 | } |
| 172 | } |
| 173 | |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 174 | # Generates nanopb code for proto files, creating a source_set of the generated |
| 175 | # files. This is internal and should not be used outside of this file. Use |
| 176 | # pw_proto_library instead. |
| 177 | # |
| 178 | # Args: |
| 179 | # protos: List of input .proto files. |
| 180 | template("_pw_nanopb_proto_library") { |
Alexei Frolov | 4c0428a | 2020-06-10 10:46:04 -0700 | [diff] [blame] | 181 | assert(defined(dir_pw_third_party_nanopb) && dir_pw_third_party_nanopb != "", |
| 182 | "\$dir_pw_third_party_nanopb must be set to compile nanopb protobufs") |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 183 | |
| 184 | _proto_gen_dir = "$root_gen_dir/protos" |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 185 | _module_path = get_path_info(".", "abspath") |
| 186 | _relative_proto_paths = rebase_path(invoker.protos, _module_path) |
| 187 | |
| 188 | _outputs = [] |
| 189 | foreach(_proto, _relative_proto_paths) { |
| 190 | _output_h = string_replace(_proto, ".proto", ".pb.h") |
| 191 | _output_c = string_replace(_proto, ".proto", ".pb.c") |
| 192 | _outputs += [ |
| 193 | "$_proto_gen_dir/$_output_h", |
| 194 | "$_proto_gen_dir/$_output_c", |
| 195 | ] |
| 196 | } |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 197 | |
Alexei Frolov | 4c0428a | 2020-06-10 10:46:04 -0700 | [diff] [blame] | 198 | _nanopb_plugin = "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb" |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 199 | if (host_os == "win") { |
| 200 | _nanopb_plugin += ".bat" |
| 201 | } |
| 202 | |
| 203 | # Create a target which runs protoc configured with the nanopb plugin to |
| 204 | # generate the C proto sources. |
| 205 | _gen_target = "${target_name}_gen" |
Wyatt Hepler | c8e05a4 | 2020-10-19 14:49:39 -0700 | [diff] [blame] | 206 | pw_python_action(_gen_target) { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 207 | forward_variables_from(invoker, _forwarded_vars) |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 208 | script = _gen_script_path |
| 209 | args = [ |
| 210 | "--language", |
| 211 | "nanopb", |
| 212 | "--module-path", |
Wyatt Hepler | 8224a64 | 2020-07-29 08:55:56 -0700 | [diff] [blame] | 213 | rebase_path(_module_path), |
Alexei Frolov | ac5b674 | 2020-05-01 13:28:14 -0700 | [diff] [blame] | 214 | "--include-paths", |
Wyatt Hepler | 8224a64 | 2020-07-29 08:55:56 -0700 | [diff] [blame] | 215 | rebase_path("$dir_pw_third_party_nanopb/generator/proto"), |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 216 | "--include-file", |
Wyatt Hepler | 8224a64 | 2020-07-29 08:55:56 -0700 | [diff] [blame] | 217 | rebase_path(invoker.include_file), |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 218 | "--out-dir", |
Wyatt Hepler | 8224a64 | 2020-07-29 08:55:56 -0700 | [diff] [blame] | 219 | rebase_path(_proto_gen_dir), |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 220 | "--custom-plugin", |
Wyatt Hepler | 8224a64 | 2020-07-29 08:55:56 -0700 | [diff] [blame] | 221 | rebase_path(_nanopb_plugin), |
| 222 | ] + rebase_path(invoker.protos) |
| 223 | |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 224 | inputs = invoker.protos |
| 225 | outputs = _outputs |
| 226 | |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 227 | deps = invoker.deps |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 228 | if (defined(invoker.protoc_deps)) { |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 229 | deps += invoker.protoc_deps |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | # For C++ proto files, the generated proto directory is added as an include |
| 234 | # path for the code. This requires using "all_dependent_configs" to force the |
| 235 | # include on any code that transitively depends on the generated protos. |
| 236 | _include_root = rebase_path(get_path_info(".", "abspath"), "//") |
| 237 | _include_config_target = "${target_name}_includes" |
| 238 | config(_include_config_target) { |
| 239 | include_dirs = [ |
| 240 | "$_proto_gen_dir", |
| 241 | "$_proto_gen_dir/$_include_root", |
| 242 | ] |
Ewout van Bekkum | e4d7b69 | 2020-10-15 13:12:30 -0700 | [diff] [blame] | 243 | |
| 244 | # Nanopb uses __cplusplus with the implicit default of 0. |
| 245 | cflags = [ "-Wno-undef" ] |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | # Create a library with the generated source files. |
Alexei Frolov | edd2f14 | 2020-06-09 19:11:27 -0700 | [diff] [blame] | 249 | pw_source_set(target_name) { |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 250 | all_dependent_configs = [ ":$_include_config_target" ] |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 251 | deps = [ ":$_gen_target" ] |
Alexei Frolov | dd6fa5c | 2020-08-11 10:04:01 -0700 | [diff] [blame] | 252 | public_deps = [ "$dir_pw_third_party/nanopb" ] + invoker.gen_deps |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 253 | sources = get_target_outputs(":$_gen_target") |
| 254 | public = filter_include(sources, [ "*.pb.h" ]) |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame^] | 258 | # Generates raw RPC code for proto files, creating a source_set of the generated |
| 259 | # files. This is internal and should not be used outside of this file. Use |
| 260 | # pw_proto_library instead. |
| 261 | # |
| 262 | # Args: |
| 263 | # protos: List of input .proto files. |
| 264 | # |
| 265 | template("_pw_raw_rpc_proto_library") { |
| 266 | _proto_gen_dir = "$root_gen_dir/protos" |
| 267 | _module_path = get_path_info(".", "abspath") |
| 268 | _relative_proto_paths = rebase_path(invoker.protos, _module_path) |
| 269 | |
| 270 | _outputs = [] |
| 271 | foreach(_proto, _relative_proto_paths) { |
| 272 | _output_h = string_replace(_proto, ".proto", ".raw_rpc.pb.h") |
| 273 | _outputs += [ "$_proto_gen_dir/$_output_h" ] |
| 274 | } |
| 275 | |
| 276 | # Create a target which runs protoc configured with the nanopb_rpc plugin to |
| 277 | # generate the C++ proto RPC headers. |
| 278 | _gen_target = "${target_name}_gen" |
| 279 | pw_python_action(_gen_target) { |
| 280 | forward_variables_from(invoker, _forwarded_vars) |
| 281 | script = _gen_script_path |
| 282 | args = [ |
| 283 | "--language", |
| 284 | "raw_rpc", |
| 285 | "--module-path", |
| 286 | rebase_path(_module_path), |
| 287 | "--include-file", |
| 288 | rebase_path(invoker.include_file), |
| 289 | "--out-dir", |
| 290 | rebase_path(_proto_gen_dir), |
| 291 | ] + rebase_path(invoker.protos) |
| 292 | inputs = invoker.protos |
| 293 | outputs = _outputs |
| 294 | |
| 295 | deps = invoker.deps |
| 296 | if (defined(invoker.protoc_deps)) { |
| 297 | deps += invoker.protoc_deps |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | # For C++ proto files, the generated proto directory is added as an include |
| 302 | # path for the code. |
| 303 | _include_root = rebase_path(get_path_info(".", "abspath"), "//") |
| 304 | _include_config_target = "${target_name}_includes" |
| 305 | config(_include_config_target) { |
| 306 | include_dirs = [ |
| 307 | "$_proto_gen_dir", |
| 308 | "$_proto_gen_dir/$_include_root", |
| 309 | ] |
| 310 | } |
| 311 | |
| 312 | # Create a library with the generated source files. |
| 313 | pw_source_set(target_name) { |
| 314 | public_configs = [ ":$_include_config_target" ] |
| 315 | deps = [ ":$_gen_target" ] |
| 316 | public_deps = [ |
| 317 | "$dir_pw_rpc:server", |
| 318 | "$dir_pw_rpc/raw:method_union", |
| 319 | ] + invoker.gen_deps |
| 320 | public = get_target_outputs(":$_gen_target") |
| 321 | } |
| 322 | } |
| 323 | |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 324 | # Generates Go code for proto files, listing the proto output directory in the |
| 325 | # metadata variable GOPATH. Internal use only. |
| 326 | # |
| 327 | # Args: |
| 328 | # protos: List of input .proto files. |
| 329 | template("_pw_go_proto_library") { |
| 330 | _proto_gopath = "$root_gen_dir/go" |
| 331 | _proto_gen_dir = "$_proto_gopath/src" |
| 332 | _rebased_gopath = rebase_path(_proto_gopath) |
| 333 | |
Wyatt Hepler | c8e05a4 | 2020-10-19 14:49:39 -0700 | [diff] [blame] | 334 | pw_python_action(target_name) { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 335 | forward_variables_from(invoker, _forwarded_vars) |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 336 | metadata = { |
| 337 | gopath = [ "GOPATH+=$_rebased_gopath" ] |
Alexei Frolov | c15a988 | 2019-12-23 14:29:02 -0800 | [diff] [blame] | 338 | external_deps = [ |
| 339 | "github.com/golang/protobuf/proto", |
| 340 | "google.golang.org/grpc", |
| 341 | ] |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 342 | } |
| 343 | script = _gen_script_path |
| 344 | args = [ |
| 345 | "--language", |
| 346 | "go", |
| 347 | "--module-path", |
Wyatt Hepler | 8224a64 | 2020-07-29 08:55:56 -0700 | [diff] [blame] | 348 | rebase_path("//"), |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 349 | "--include-file", |
Wyatt Hepler | 8224a64 | 2020-07-29 08:55:56 -0700 | [diff] [blame] | 350 | rebase_path(invoker.include_file), |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 351 | "--out-dir", |
Wyatt Hepler | 8224a64 | 2020-07-29 08:55:56 -0700 | [diff] [blame] | 352 | rebase_path(_proto_gen_dir), |
| 353 | ] + rebase_path(invoker.protos) |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 354 | inputs = invoker.protos |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 355 | deps = invoker.deps + invoker.gen_deps |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 356 | stamp = true |
| 357 | } |
| 358 | } |
| 359 | |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 360 | # Generates protobuf code from .proto definitions for various languages. |
| 361 | # |
Alexei Frolov | 4c0428a | 2020-06-10 10:46:04 -0700 | [diff] [blame] | 362 | # The generators to use are defined in the pw_protobuf_GENERATORS build |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 363 | # variable. Each listed generator creates a generated code target called |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 364 | # |
Alexei Frolov | 8e30d46 | 2020-10-22 13:54:36 -0700 | [diff] [blame] | 365 | # <target_name>.<generator> |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 366 | # |
| 367 | # For example, with the following definitions: |
| 368 | # |
Alexei Frolov | 4c0428a | 2020-06-10 10:46:04 -0700 | [diff] [blame] | 369 | # pw_protobuf_GENERATORS = [ "pwpb", "py" ] |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 370 | # |
| 371 | # pw_proto_library("my_protos") { |
| 372 | # sources = [ "foo.proto" ] |
| 373 | # } |
| 374 | # |
| 375 | # Two build targets will be created for the declared "my_protos" target. |
| 376 | # |
Alexei Frolov | 8e30d46 | 2020-10-22 13:54:36 -0700 | [diff] [blame] | 377 | # "my_protos.pwpb" <-- C++ source_set containing generated proto code |
| 378 | # "my_protos.py" <-- Python module containing generated proto code |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 379 | # |
| 380 | # Args: |
| 381 | # sources: List of input .proto files. |
| 382 | # deps: List of other pw_proto_library dependencies. |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 383 | # inputs: Other files on which the protos depend (e.g. nanopb .options files). |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 384 | template("pw_proto_library") { |
| 385 | assert(defined(invoker.sources) && invoker.sources != [], |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 386 | "pw_proto_library requires .proto source files") |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 387 | |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 388 | # For each proto target, create a file which collects the base directories of |
| 389 | # all of its dependencies to list as include paths to protoc. |
| 390 | _include_metadata_target = "${target_name}_include_paths" |
| 391 | _include_metadata_file = "${target_gen_dir}/${target_name}_includes.txt" |
| 392 | generated_file(_include_metadata_target) { |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 393 | if (defined(invoker.deps)) { |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 394 | # Collect metadata from the include path files of each dependency. |
| 395 | deps = process_file_template(invoker.deps, "{{source}}_include_paths") |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 396 | } else { |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 397 | deps = [] |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 398 | } |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 399 | data_keys = [ "protoc_includes" ] |
| 400 | outputs = [ _include_metadata_file ] |
| 401 | |
| 402 | # Indicate this library's base directory for its dependents. |
| 403 | metadata = { |
Alexei Frolov | f79d227 | 2020-06-18 13:37:38 -0700 | [diff] [blame] | 404 | protoc_includes = [ rebase_path(".") ] |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 408 | _deps = [ ":$_include_metadata_target" ] |
| 409 | |
| 410 | if (defined(invoker.inputs)) { |
| 411 | # Toss any additional inputs into an input group dependency. |
| 412 | _input_target_name = "${target_name}_inputs" |
| 413 | pw_input_group(_input_target_name) { |
| 414 | inputs = invoker.inputs |
| 415 | } |
| 416 | _deps += [ ":$_input_target_name" ] |
| 417 | } |
| 418 | |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 419 | # If the nanopb_rpc generator is selected, make sure that nanopb is also |
| 420 | # selected. |
| 421 | has_nanopb_rpc = pw_protobuf_GENERATORS + [ "nanopb_rpc" ] - |
| 422 | [ "nanopb_rpc" ] != pw_protobuf_GENERATORS |
| 423 | if (has_nanopb_rpc) { |
| 424 | _generators = |
| 425 | pw_protobuf_GENERATORS + [ "nanopb" ] - [ "nanopb" ] + [ "nanopb" ] |
| 426 | } else { |
| 427 | _generators = pw_protobuf_GENERATORS |
| 428 | } |
| 429 | |
| 430 | foreach(_gen, _generators) { |
Alexei Frolov | 8e30d46 | 2020-10-22 13:54:36 -0700 | [diff] [blame] | 431 | _lang_target = "${target_name}.${_gen}" |
Alexei Frolov | 8185c82 | 2020-06-12 10:45:04 -0700 | [diff] [blame] | 432 | _gen_deps = [] |
Alexei Frolov | c4b62ec | 2020-07-13 08:35:10 -0700 | [diff] [blame] | 433 | |
| 434 | if (_gen == "nanopb_rpc") { |
| 435 | # Generated RPC code depends on the library's core protos. |
Alexei Frolov | 8e30d46 | 2020-10-22 13:54:36 -0700 | [diff] [blame] | 436 | _gen_deps += [ ":${target_name}.nanopb" ] |
Alexei Frolov | c4b62ec | 2020-07-13 08:35:10 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Alexei Frolov | 8185c82 | 2020-06-12 10:45:04 -0700 | [diff] [blame] | 439 | if (defined(invoker.deps)) { |
Alexei Frolov | 8e30d46 | 2020-10-22 13:54:36 -0700 | [diff] [blame] | 440 | _gen_deps += process_file_template(invoker.deps, "{{source}}.${_gen}") |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 441 | |
| 442 | if (_gen == "nanopb_rpc") { |
Alexei Frolov | c4b62ec | 2020-07-13 08:35:10 -0700 | [diff] [blame] | 443 | # RPC dependencies also depend on their core generated protos. |
Alexei Frolov | 8e30d46 | 2020-10-22 13:54:36 -0700 | [diff] [blame] | 444 | _gen_deps += process_file_template(invoker.deps, "{{source}}.nanopb") |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 445 | } |
Alexei Frolov | 8185c82 | 2020-06-12 10:45:04 -0700 | [diff] [blame] | 446 | } |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 447 | |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 448 | if (_gen == "pwpb") { |
| 449 | _pw_pwpb_proto_library(_lang_target) { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 450 | forward_variables_from(invoker, _forwarded_vars) |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 451 | protos = invoker.sources |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 452 | deps = _deps |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 453 | include_file = _include_metadata_file |
| 454 | gen_deps = _gen_deps |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame^] | 455 | protoc_deps = [ "$dir_pw_protobuf/py" ] |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 456 | } |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 457 | } else if (_gen == "nanopb_rpc") { |
| 458 | _pw_nanopb_rpc_proto_library(_lang_target) { |
| 459 | forward_variables_from(invoker, _forwarded_vars) |
| 460 | protos = invoker.sources |
| 461 | deps = _deps |
| 462 | include_file = _include_metadata_file |
| 463 | gen_deps = _gen_deps |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame^] | 464 | protoc_deps = [ "$dir_pw_rpc/py" ] |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 465 | } |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 466 | } else if (_gen == "nanopb") { |
| 467 | _pw_nanopb_proto_library(_lang_target) { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 468 | forward_variables_from(invoker, _forwarded_vars) |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 469 | protos = invoker.sources |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 470 | deps = _deps |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 471 | include_file = _include_metadata_file |
| 472 | gen_deps = _gen_deps |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 473 | } |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame^] | 474 | } else if (_gen == "raw_rpc") { |
| 475 | _pw_raw_rpc_proto_library(_lang_target) { |
| 476 | forward_variables_from(invoker, _forwarded_vars) |
| 477 | protos = invoker.sources |
| 478 | deps = _deps |
| 479 | include_file = _include_metadata_file |
| 480 | gen_deps = _gen_deps |
| 481 | protoc_deps = [ "$dir_pw_rpc/py" ] |
| 482 | } |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 483 | } else if (_gen == "go") { |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 484 | _pw_go_proto_library(_lang_target) { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 485 | forward_variables_from(invoker, _forwarded_vars) |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 486 | protos = invoker.sources |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 487 | deps = _deps |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 488 | include_file = _include_metadata_file |
| 489 | gen_deps = _gen_deps |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 490 | } |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 491 | } else { |
| 492 | assert(false, |
| 493 | string_join( |
| 494 | " ", |
| 495 | [ |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 496 | "pw_proto_library doesn't know how to generate code for", |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 497 | "generator '$_gen'. Please add support if you require it.", |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 498 | ])) |
| 499 | } |
Alexei Frolov | 8e30d46 | 2020-10-22 13:54:36 -0700 | [diff] [blame] | 500 | |
| 501 | # TODO(frolv): Remove this after migrating existing code. |
| 502 | _legacy_target = "${target_name}_${_gen}" |
| 503 | group(_legacy_target) { |
| 504 | public_deps = [ ":$_lang_target" ] |
| 505 | } |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 508 | # All supported pw_protobuf generators. |
| 509 | _protobuf_generators = [ |
| 510 | "pwpb", |
| 511 | "nanopb", |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 512 | "nanopb_rpc", |
Alexei Frolov | c912ea7 | 2020-10-26 08:43:27 -0700 | [diff] [blame^] | 513 | "raw_rpc", |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 514 | "go", |
| 515 | ] |
| 516 | |
| 517 | # Create stub versions of the proto library for other protobuf generators. |
Alexei Frolov | 79b7cb0 | 2020-07-06 13:51:43 -0700 | [diff] [blame] | 518 | foreach(_gen, _protobuf_generators - _generators) { |
Alexei Frolov | 8e30d46 | 2020-10-22 13:54:36 -0700 | [diff] [blame] | 519 | pw_python_action("${target_name}.${_gen}") { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 520 | forward_variables_from(invoker, _forwarded_vars) |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 521 | script = string_join("/", |
| 522 | [ |
| 523 | dir_pw_protobuf_compiler, |
| 524 | "py", |
| 525 | "pw_protobuf_compiler", |
| 526 | "generator_not_selected.py", |
| 527 | ]) |
| 528 | args = [ |
| 529 | "--library", |
Alexei Frolov | 8e30d46 | 2020-10-22 13:54:36 -0700 | [diff] [blame] | 530 | target_name, |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 531 | "--generator", |
| 532 | _gen, |
| 533 | ] |
| 534 | inputs = invoker.sources |
| 535 | stamp = true |
| 536 | } |
| 537 | } |
| 538 | |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 539 | # 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] | 540 | # generator targets, run a script which prints a nice error message. |
Wyatt Hepler | c8e05a4 | 2020-10-19 14:49:39 -0700 | [diff] [blame] | 541 | pw_python_action(target_name) { |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 542 | script = string_join("/", |
| 543 | [ |
| 544 | dir_pw_protobuf_compiler, |
| 545 | "py", |
| 546 | "pw_protobuf_compiler", |
| 547 | "proto_target_invalid.py", |
| 548 | ]) |
| 549 | args = [ |
| 550 | "--target", |
| 551 | target_name, |
| 552 | "--dir", |
| 553 | get_path_info(".", "abspath"), |
| 554 | "--root", |
| 555 | "//", |
Alexei Frolov | 4c0428a | 2020-06-10 10:46:04 -0700 | [diff] [blame] | 556 | ] + pw_protobuf_GENERATORS |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 557 | stamp = true |
| 558 | } |
| 559 | } |