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