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