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