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 | # gn-format disable |
| 16 | import("//build_overrides/pigweed.gni") |
| 17 | |
| 18 | import("$dir_pigweed/legacy_target.gni") |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 19 | import("$dir_pw_build/input_group.gni") |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 20 | import("$dir_pw_build/python_script.gni") |
Alexei Frolov | edd2f14 | 2020-06-09 19:11:27 -0700 | [diff] [blame] | 21 | import("$dir_pw_build/target_types.gni") |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 22 | |
| 23 | # Python script that invokes protoc. |
| 24 | _gen_script_path = |
| 25 | "$dir_pw_protobuf_compiler/py/pw_protobuf_compiler/generate_protos.py" |
| 26 | |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 27 | _forwarded_vars = [ |
| 28 | "testonly", |
| 29 | "visibility", |
| 30 | ] |
| 31 | |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 32 | # Generates pw_protobuf C++ code for proto files, creating a source_set of the |
| 33 | # generated files. This is internal and should not be used outside of this file. |
| 34 | # Use pw_proto_library instead. |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 35 | # |
| 36 | # Args: |
| 37 | # protos: List of input .proto files. |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 38 | template("_pw_pwpb_proto_library") { |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 39 | _proto_gen_dir = "$root_gen_dir/protos" |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 40 | _module_path = get_path_info(".", "abspath") |
| 41 | _relative_proto_paths = rebase_path(invoker.protos, _module_path) |
| 42 | |
| 43 | _outputs = [] |
| 44 | foreach(_proto, _relative_proto_paths) { |
| 45 | _output = string_replace(_proto, ".proto", ".pwpb.h") |
| 46 | _outputs += [ "$_proto_gen_dir/$_output" ] |
| 47 | } |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 48 | |
| 49 | _gen_target = "${target_name}_gen" |
| 50 | pw_python_script(_gen_target) { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 51 | forward_variables_from(invoker, _forwarded_vars) |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 52 | script = _gen_script_path |
| 53 | args = [ |
| 54 | "--language", |
| 55 | "cc", |
| 56 | "--module-path", |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 57 | _module_path, |
| 58 | "--include-file", |
| 59 | invoker.include_file, |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 60 | "--out-dir", |
| 61 | _proto_gen_dir, |
| 62 | ] + get_path_info(invoker.protos, "abspath") |
| 63 | inputs = invoker.protos |
| 64 | outputs = _outputs |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 65 | deps = invoker.deps |
Alexei Frolov | bbf164c | 2019-12-16 12:51:59 -0800 | [diff] [blame] | 66 | if (defined(invoker.protoc_deps)) { |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 67 | deps += invoker.protoc_deps |
Alexei Frolov | bbf164c | 2019-12-16 12:51:59 -0800 | [diff] [blame] | 68 | } |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | # For C++ proto files, the generated proto directory is added as an include |
| 72 | # path for the code. This requires using "all_dependent_configs" to force the |
| 73 | # include on any code that transitively depends on the generated protos. |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 74 | _include_config_target = "${target_name}_includes" |
| 75 | config(_include_config_target) { |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 76 | include_dirs = [ "$_proto_gen_dir" ] |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | # Create a library with the generated source files. |
Alexei Frolov | edd2f14 | 2020-06-09 19:11:27 -0700 | [diff] [blame] | 80 | pw_source_set(target_name) { |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 81 | all_dependent_configs = [ ":$_include_config_target" ] |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 82 | deps = [ ":$_gen_target" ] |
| 83 | public_deps = [ dir_pw_protobuf ] + invoker.gen_deps |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 84 | sources = get_target_outputs(":$_gen_target") |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 85 | public = filter_include(sources, [ "*.pwpb.h" ]) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | # Generates nanopb code for proto files, creating a source_set of the generated |
| 90 | # files. This is internal and should not be used outside of this file. Use |
| 91 | # pw_proto_library instead. |
| 92 | # |
| 93 | # Args: |
| 94 | # protos: List of input .proto files. |
| 95 | template("_pw_nanopb_proto_library") { |
| 96 | assert(defined(dir_third_party_nanopb) && dir_third_party_nanopb != "", |
| 97 | "\$dir_third_party_nanopb must be set to compile nanopb protobufs") |
| 98 | |
| 99 | _proto_gen_dir = "$root_gen_dir/protos" |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 100 | _module_path = get_path_info(".", "abspath") |
| 101 | _relative_proto_paths = rebase_path(invoker.protos, _module_path) |
| 102 | |
| 103 | _outputs = [] |
| 104 | foreach(_proto, _relative_proto_paths) { |
| 105 | _output_h = string_replace(_proto, ".proto", ".pb.h") |
| 106 | _output_c = string_replace(_proto, ".proto", ".pb.c") |
| 107 | _outputs += [ |
| 108 | "$_proto_gen_dir/$_output_h", |
| 109 | "$_proto_gen_dir/$_output_c", |
| 110 | ] |
| 111 | } |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 112 | |
| 113 | _nanopb_plugin = "$dir_third_party_nanopb/generator/protoc-gen-nanopb" |
| 114 | if (host_os == "win") { |
| 115 | _nanopb_plugin += ".bat" |
| 116 | } |
| 117 | |
| 118 | # Create a target which runs protoc configured with the nanopb plugin to |
| 119 | # generate the C proto sources. |
| 120 | _gen_target = "${target_name}_gen" |
| 121 | pw_python_script(_gen_target) { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 122 | forward_variables_from(invoker, _forwarded_vars) |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 123 | script = _gen_script_path |
| 124 | args = [ |
| 125 | "--language", |
| 126 | "nanopb", |
| 127 | "--module-path", |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 128 | _module_path, |
Alexei Frolov | ac5b674 | 2020-05-01 13:28:14 -0700 | [diff] [blame] | 129 | "--include-paths", |
| 130 | "$dir_third_party_nanopb/generator/proto", |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 131 | "--include-file", |
| 132 | invoker.include_file, |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 133 | "--out-dir", |
| 134 | _proto_gen_dir, |
| 135 | "--custom-plugin", |
| 136 | get_path_info(_nanopb_plugin, "abspath"), |
| 137 | ] + get_path_info(invoker.protos, "abspath") |
| 138 | inputs = invoker.protos |
| 139 | outputs = _outputs |
| 140 | |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 141 | deps = invoker.deps |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 142 | if (defined(invoker.protoc_deps)) { |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 143 | deps += invoker.protoc_deps |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | # For C++ proto files, the generated proto directory is added as an include |
| 148 | # path for the code. This requires using "all_dependent_configs" to force the |
| 149 | # include on any code that transitively depends on the generated protos. |
| 150 | _include_root = rebase_path(get_path_info(".", "abspath"), "//") |
| 151 | _include_config_target = "${target_name}_includes" |
| 152 | config(_include_config_target) { |
| 153 | include_dirs = [ |
| 154 | "$_proto_gen_dir", |
| 155 | "$_proto_gen_dir/$_include_root", |
| 156 | ] |
| 157 | } |
| 158 | |
| 159 | # Create a library with the generated source files. |
Alexei Frolov | edd2f14 | 2020-06-09 19:11:27 -0700 | [diff] [blame] | 160 | pw_source_set(target_name) { |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 161 | all_dependent_configs = [ ":$_include_config_target" ] |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 162 | deps = [ ":$_gen_target" ] |
| 163 | public_deps = [ dir_third_party_nanopb ] + invoker.gen_deps |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 164 | sources = get_target_outputs(":$_gen_target") |
| 165 | public = filter_include(sources, [ "*.pb.h" ]) |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 169 | # Generates Go code for proto files, listing the proto output directory in the |
| 170 | # metadata variable GOPATH. Internal use only. |
| 171 | # |
| 172 | # Args: |
| 173 | # protos: List of input .proto files. |
| 174 | template("_pw_go_proto_library") { |
| 175 | _proto_gopath = "$root_gen_dir/go" |
| 176 | _proto_gen_dir = "$_proto_gopath/src" |
| 177 | _rebased_gopath = rebase_path(_proto_gopath) |
| 178 | |
| 179 | pw_python_script(target_name) { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 180 | forward_variables_from(invoker, _forwarded_vars) |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 181 | metadata = { |
| 182 | gopath = [ "GOPATH+=$_rebased_gopath" ] |
Alexei Frolov | c15a988 | 2019-12-23 14:29:02 -0800 | [diff] [blame] | 183 | external_deps = [ |
| 184 | "github.com/golang/protobuf/proto", |
| 185 | "google.golang.org/grpc", |
| 186 | ] |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 187 | } |
| 188 | script = _gen_script_path |
| 189 | args = [ |
| 190 | "--language", |
| 191 | "go", |
| 192 | "--module-path", |
| 193 | "//", |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 194 | "--include-file", |
| 195 | invoker.include_file, |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 196 | "--out-dir", |
| 197 | _proto_gen_dir, |
| 198 | ] + get_path_info(invoker.protos, "abspath") |
| 199 | inputs = invoker.protos |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 200 | deps = invoker.deps + invoker.gen_deps |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 201 | stamp = true |
| 202 | } |
| 203 | } |
| 204 | |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 205 | # Generates protobuf code from .proto definitions for various languages. |
| 206 | # |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 207 | # The generators to use are defined in the pw_protobuf_generators build |
| 208 | # variable. Each listed generator creates a generated code target called |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 209 | # |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 210 | # <target_name>_<generator> |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 211 | # |
| 212 | # For example, with the following definitions: |
| 213 | # |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 214 | # pw_protobuf_generators = [ "pwpb", "py" ] |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 215 | # |
| 216 | # pw_proto_library("my_protos") { |
| 217 | # sources = [ "foo.proto" ] |
| 218 | # } |
| 219 | # |
| 220 | # Two build targets will be created for the declared "my_protos" target. |
| 221 | # |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 222 | # "my_protos_pwpb" <-- C++ source_set containing generated proto code |
| 223 | # "my_protos_py" <-- Python module containing generated proto code |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 224 | # |
| 225 | # Args: |
| 226 | # sources: List of input .proto files. |
| 227 | # deps: List of other pw_proto_library dependencies. |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 228 | # 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] | 229 | template("pw_proto_library") { |
| 230 | assert(defined(invoker.sources) && invoker.sources != [], |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 231 | "pw_proto_library requires .proto source files") |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 232 | |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 233 | # For each proto target, create a file which collects the base directories of |
| 234 | # all of its dependencies to list as include paths to protoc. |
| 235 | _include_metadata_target = "${target_name}_include_paths" |
| 236 | _include_metadata_file = "${target_gen_dir}/${target_name}_includes.txt" |
| 237 | generated_file(_include_metadata_target) { |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 238 | if (defined(invoker.deps)) { |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 239 | # Collect metadata from the include path files of each dependency. |
| 240 | deps = process_file_template(invoker.deps, "{{source}}_include_paths") |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 241 | } else { |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 242 | deps = [] |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 243 | } |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 244 | data_keys = [ "protoc_includes" ] |
| 245 | outputs = [ _include_metadata_file ] |
| 246 | |
| 247 | # Indicate this library's base directory for its dependents. |
| 248 | metadata = { |
| 249 | protoc_includes = [ rebase_path(".", root_out_dir) ] |
| 250 | } |
| 251 | } |
| 252 | |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 253 | _deps = [ ":$_include_metadata_target" ] |
| 254 | |
| 255 | if (defined(invoker.inputs)) { |
| 256 | # Toss any additional inputs into an input group dependency. |
| 257 | _input_target_name = "${target_name}_inputs" |
| 258 | pw_input_group(_input_target_name) { |
| 259 | inputs = invoker.inputs |
| 260 | } |
| 261 | _deps += [ ":$_input_target_name" ] |
| 262 | } |
| 263 | |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 264 | foreach(_gen, pw_protobuf_generators) { |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 265 | _lang_target = "${target_name}_${_gen}" |
Alexei Frolov | 8185c82 | 2020-06-12 10:45:04 -0700 | [diff] [blame] | 266 | _gen_deps = [] |
| 267 | if (defined(invoker.deps)) { |
| 268 | _gen_deps = process_file_template(invoker.deps, "{{source}}_${_gen}") |
| 269 | } |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 270 | |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 271 | if (_gen == "pwpb") { |
| 272 | _pw_pwpb_proto_library(_lang_target) { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 273 | forward_variables_from(invoker, _forwarded_vars) |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 274 | protos = invoker.sources |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 275 | deps = _deps |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 276 | include_file = _include_metadata_file |
| 277 | gen_deps = _gen_deps |
Alexei Frolov | bbf164c | 2019-12-16 12:51:59 -0800 | [diff] [blame] | 278 | |
| 279 | # List the pw_protobuf plugin's files as a dependency to recompile |
| 280 | # generated code if they are modified. |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 281 | protoc_deps = [ "$dir_pw_protobuf:codegen_protoc_plugin" ] |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 282 | } |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 283 | } else if (_gen == "nanopb") { |
| 284 | _pw_nanopb_proto_library(_lang_target) { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 285 | forward_variables_from(invoker, _forwarded_vars) |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 286 | protos = invoker.sources |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 287 | deps = _deps |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 288 | include_file = _include_metadata_file |
| 289 | gen_deps = _gen_deps |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 290 | } |
| 291 | } else if (_gen == "go") { |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 292 | _pw_go_proto_library(_lang_target) { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 293 | forward_variables_from(invoker, _forwarded_vars) |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 294 | protos = invoker.sources |
Alexei Frolov | 05d8ef2 | 2020-06-08 10:32:29 -0700 | [diff] [blame] | 295 | deps = _deps |
Alexei Frolov | e19ebb8 | 2020-05-14 17:21:20 -0700 | [diff] [blame] | 296 | include_file = _include_metadata_file |
| 297 | gen_deps = _gen_deps |
Alexei Frolov | def1471 | 2019-12-23 13:03:32 -0800 | [diff] [blame] | 298 | } |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 299 | } else { |
| 300 | assert(false, |
| 301 | string_join( |
| 302 | " ", |
| 303 | [ |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 304 | "pw_proto_library doesn't know how to generate code for", |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 305 | "generator '$_gen'. Please add support if you require it.", |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 306 | ])) |
| 307 | } |
| 308 | } |
| 309 | |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 310 | # All supported pw_protobuf generators. |
| 311 | _protobuf_generators = [ |
| 312 | "pwpb", |
| 313 | "nanopb", |
| 314 | "go", |
| 315 | ] |
| 316 | |
| 317 | # Create stub versions of the proto library for other protobuf generators. |
| 318 | foreach(_gen, _protobuf_generators - pw_protobuf_generators) { |
| 319 | pw_python_script("${target_name}_${_gen}") { |
Wyatt Hepler | e0c4fdc | 2020-05-29 17:26:19 -0700 | [diff] [blame] | 320 | forward_variables_from(invoker, _forwarded_vars) |
Wyatt Hepler | b4b73a6 | 2020-05-27 15:17:27 -0700 | [diff] [blame] | 321 | script = string_join("/", |
| 322 | [ |
| 323 | dir_pw_protobuf_compiler, |
| 324 | "py", |
| 325 | "pw_protobuf_compiler", |
| 326 | "generator_not_selected.py", |
| 327 | ]) |
| 328 | args = [ |
| 329 | "--library", |
| 330 | "${target_name}_${_gen}", |
| 331 | "--generator", |
| 332 | _gen, |
| 333 | ] |
| 334 | inputs = invoker.sources |
| 335 | stamp = true |
| 336 | } |
| 337 | } |
| 338 | |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 339 | # 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] | 340 | # generator targets, run a script which prints a nice error message. |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 341 | pw_python_script(target_name) { |
| 342 | script = string_join("/", |
| 343 | [ |
| 344 | dir_pw_protobuf_compiler, |
| 345 | "py", |
| 346 | "pw_protobuf_compiler", |
| 347 | "proto_target_invalid.py", |
| 348 | ]) |
| 349 | args = [ |
| 350 | "--target", |
| 351 | target_name, |
| 352 | "--dir", |
| 353 | get_path_info(".", "abspath"), |
| 354 | "--root", |
| 355 | "//", |
Alexei Frolov | f39cd8b | 2020-04-13 17:59:20 -0700 | [diff] [blame] | 356 | ] + pw_protobuf_generators |
Alexei Frolov | 942adf0 | 2019-12-11 17:07:28 -0800 | [diff] [blame] | 357 | stamp = true |
| 358 | } |
| 359 | } |