blob: 27c608fa1b51e32ee116b224f36c693460248d47 [file] [log] [blame]
Alexei Frolove19ebb82020-05-14 17:21:20 -07001# Copyright 2020 The Pigweed Authors
Alexei Frolov942adf02019-12-11 17:07:28 -08002#
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 Montanezfb3d3fb2020-06-09 18:12:12 -070015import("//build_overrides/pigweed.gni")
16
Alexei Frolovb499d3f2020-10-28 13:00:08 -070017import("$dir_pw_build/error.gni")
Alexei Frolov05d8ef22020-06-08 10:32:29 -070018import("$dir_pw_build/input_group.gni")
Wyatt Hepler752d7d32021-03-02 09:02:23 -080019import("$dir_pw_build/mirror_tree.gni")
Alexei Frolova4c0aee2020-12-01 13:48:48 -080020import("$dir_pw_build/python.gni")
Wyatt Hepler51ded742020-10-19 14:45:27 -070021import("$dir_pw_build/python_action.gni")
Alexei Frolovedd2f142020-06-09 19:11:27 -070022import("$dir_pw_build/target_types.gni")
Alexei Frolovdd6fa5c2020-08-11 10:04:01 -070023import("$dir_pw_third_party/nanopb/nanopb.gni")
Wyatt Heplerd49f8fe2020-10-15 10:13:47 -070024
Wyatt Heplerd9336a42020-11-10 09:47:30 -080025# Variables forwarded from the public pw_proto_library template to the final
26# pw_source_set.
Wyatt Heplere0c4fdc2020-05-29 17:26:19 -070027_forwarded_vars = [
28 "testonly",
29 "visibility",
30]
31
Wyatt Heplerd9336a42020-11-10 09:47:30 -080032# Internal template that invokes protoc with a pw_python_action. This should not
33# be used outside of this file; use pw_proto_library instead.
34#
35# This creates the internal GN target $target_name.$language._gen that compiles
36# proto files with protoc.
37template("_pw_invoke_protoc") {
Wyatt Hepler752d7d32021-03-02 09:02:23 -080038 if (defined(invoker.out_dir)) {
39 _out_dir = invoker.out_dir
40 } else {
41 _out_dir = "${invoker.base_out_dir}/${invoker.language}"
42 if (defined(invoker.module_as_package) && invoker.module_as_package != "") {
43 assert(invoker.language == "python")
44 _out_dir = "$_out_dir/${invoker.module_as_package}"
45 }
46 }
47
48 _includes =
49 rebase_path(get_target_outputs(":${invoker.base_target}._includes"))
Wyatt Heplerd9336a42020-11-10 09:47:30 -080050
51 pw_python_action("$target_name._gen") {
52 forward_variables_from(invoker, [ "metadata" ])
53 script =
54 "$dir_pw_protobuf_compiler/py/pw_protobuf_compiler/generate_protos.py"
55
Wyatt Hepler438caa02021-01-15 17:13:11 -080056 python_deps = [ "$dir_pw_protobuf_compiler/py" ]
57 if (defined(invoker.python_deps)) {
58 python_deps += invoker.python_deps
59 }
60
Wyatt Heplerd9336a42020-11-10 09:47:30 -080061 deps = [
Wyatt Hepler752d7d32021-03-02 09:02:23 -080062 ":${invoker.base_target}._includes",
63 ":${invoker.base_target}._sources",
64 ]
65
66 foreach(dep, invoker.deps) {
67 deps += [ get_label_info(dep, "label_no_toolchain") + "._gen" ]
68 }
69
Wyatt Heplerd9336a42020-11-10 09:47:30 -080070 args = [
71 "--language",
72 invoker.language,
Wyatt Heplerd9336a42020-11-10 09:47:30 -080073 "--include-file",
Wyatt Hepler752d7d32021-03-02 09:02:23 -080074 _includes[0],
75 "--compile-dir",
76 rebase_path(invoker.compile_dir),
Wyatt Heplerd9336a42020-11-10 09:47:30 -080077 "--out-dir",
Wyatt Hepler752d7d32021-03-02 09:02:23 -080078 rebase_path(_out_dir),
79 "--sources",
Wyatt Heplerd9336a42020-11-10 09:47:30 -080080 ] + rebase_path(invoker.sources)
81
Wyatt Heplerd9336a42020-11-10 09:47:30 -080082 if (defined(invoker.plugin)) {
Wyatt Hepler752d7d32021-03-02 09:02:23 -080083 inputs = [ invoker.plugin ]
Wyatt Heplerd9336a42020-11-10 09:47:30 -080084 args += [ "--plugin-path=" + rebase_path(invoker.plugin) ]
85 }
86
Wyatt Hepler752d7d32021-03-02 09:02:23 -080087 if (defined(invoker.outputs)) {
88 outputs = invoker.outputs
89 } else {
Wyatt Heplerd9336a42020-11-10 09:47:30 -080090 stamp = true
91 }
Wyatt Heplerd9336a42020-11-10 09:47:30 -080092 }
93}
94
Alexei Frolovf39cd8b2020-04-13 17:59:20 -070095# Generates pw_protobuf C++ code for proto files, creating a source_set of the
96# generated files. This is internal and should not be used outside of this file.
97# Use pw_proto_library instead.
Alexei Frolovf39cd8b2020-04-13 17:59:20 -070098template("_pw_pwpb_proto_library") {
Wyatt Heplerd9336a42020-11-10 09:47:30 -080099 _pw_invoke_protoc(target_name) {
100 forward_variables_from(invoker, "*", _forwarded_vars)
101 language = "pwpb"
102 plugin = "$dir_pw_protobuf/py/pw_protobuf/plugin.py"
Wyatt Hepler438caa02021-01-15 17:13:11 -0800103 python_deps = [ "$dir_pw_protobuf/py" ]
Alexei Frolov942adf02019-12-11 17:07:28 -0800104 }
105
106 # Create a library with the generated source files.
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800107 config("$target_name._include_path") {
108 include_dirs = [ "${invoker.base_out_dir}/pwpb" ]
109 visibility = [ ":*" ]
110 }
111
Alexei Frolovedd2f142020-06-09 19:11:27 -0700112 pw_source_set(target_name) {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800113 forward_variables_from(invoker, _forwarded_vars)
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800114 public_configs = [ ":$target_name._include_path" ]
115 deps = [ ":$target_name._gen($default_toolchain)" ]
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800116 public_deps = [ dir_pw_protobuf ] + invoker.deps
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800117 sources = invoker.outputs
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700118 public = filter_include(sources, [ "*.pwpb.h" ])
119 }
120}
121
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700122# Generates nanopb RPC code for proto files, creating a source_set of the
123# generated files. This is internal and should not be used outside of this file.
124# Use pw_proto_library instead.
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700125template("_pw_nanopb_rpc_proto_library") {
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700126 # Create a target which runs protoc configured with the nanopb_rpc plugin to
127 # generate the C++ proto RPC headers.
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800128 _pw_invoke_protoc(target_name) {
129 forward_variables_from(invoker, "*", _forwarded_vars)
130 language = "nanopb_rpc"
131 plugin = "$dir_pw_rpc/py/pw_rpc/plugin_nanopb.py"
Wyatt Hepler438caa02021-01-15 17:13:11 -0800132 python_deps = [ "$dir_pw_rpc/py" ]
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700133 }
134
135 # Create a library with the generated source files.
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800136 config("$target_name._include_path") {
137 include_dirs = [ "${invoker.base_out_dir}/nanopb_rpc" ]
138 visibility = [ ":*" ]
139 }
140
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700141 pw_source_set(target_name) {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800142 forward_variables_from(invoker, _forwarded_vars)
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800143 public_configs = [ ":$target_name._include_path" ]
144 deps = [ ":$target_name._gen($default_toolchain)" ]
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700145 public_deps = [
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800146 ":${invoker.base_target}.nanopb",
Wyatt Heplercbd09c22020-09-15 11:17:24 -0700147 "$dir_pw_rpc:server",
Alexei Frolova4d71502020-10-14 12:43:14 -0700148 "$dir_pw_rpc/nanopb:method_union",
Alexei Frolovdd6fa5c2020-08-11 10:04:01 -0700149 "$dir_pw_third_party/nanopb",
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800150 ] + invoker.deps
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800151 public = invoker.outputs
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700152 }
153}
154
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700155# Generates nanopb code for proto files, creating a source_set of the generated
156# files. This is internal and should not be used outside of this file. Use
157# pw_proto_library instead.
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700158template("_pw_nanopb_proto_library") {
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800159 # When compiling with the Nanopb plugin, the nanopb.proto file is already
160 # compiled internally, so skip recompiling it with protoc.
161 if (rebase_path(invoker.sources, invoker.compile_dir) == [ "nanopb.proto" ]) {
162 group("$target_name._gen") {
163 deps = [ ":${invoker.base_target}._sources" ]
164 }
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700165
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800166 group("$target_name") {
167 deps = invoker.deps + [ ":$target_name._gen($default_toolchain)" ]
168 }
169 } else {
170 # Create a target which runs protoc configured with the nanopb plugin to
171 # generate the C proto sources.
172 _pw_invoke_protoc(target_name) {
173 forward_variables_from(invoker, "*", _forwarded_vars)
174 language = "nanopb"
175 plugin = "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb"
176 }
177
178 # Create a library with the generated source files.
179 config("$target_name._include_path") {
180 include_dirs = [ "${invoker.base_out_dir}/nanopb" ]
181 visibility = [ ":*" ]
182 }
183
184 pw_source_set(target_name) {
185 forward_variables_from(invoker, _forwarded_vars)
186 public_configs = [ ":$target_name._include_path" ]
187 deps = [ ":$target_name._gen($default_toolchain)" ]
188 public_deps = [ "$dir_pw_third_party/nanopb" ] + invoker.deps
189 sources = invoker.outputs
190 public = filter_include(sources, [ "*.pb.h" ])
191 }
Alexei Frolov942adf02019-12-11 17:07:28 -0800192 }
193}
194
Alexei Frolovc912ea72020-10-26 08:43:27 -0700195# Generates raw RPC code for proto files, creating a source_set of the generated
196# files. This is internal and should not be used outside of this file. Use
197# pw_proto_library instead.
Alexei Frolovc912ea72020-10-26 08:43:27 -0700198template("_pw_raw_rpc_proto_library") {
Alexei Frolovc912ea72020-10-26 08:43:27 -0700199 # Create a target which runs protoc configured with the nanopb_rpc plugin to
200 # generate the C++ proto RPC headers.
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800201 _pw_invoke_protoc(target_name) {
202 forward_variables_from(invoker, "*", _forwarded_vars)
203 language = "raw_rpc"
204 plugin = "$dir_pw_rpc/py/pw_rpc/plugin_raw.py"
Wyatt Hepler438caa02021-01-15 17:13:11 -0800205 python_deps = [ "$dir_pw_rpc/py" ]
Alexei Frolovc912ea72020-10-26 08:43:27 -0700206 }
207
208 # Create a library with the generated source files.
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800209 config("$target_name._include_path") {
210 include_dirs = [ "${invoker.base_out_dir}/raw_rpc" ]
211 visibility = [ ":*" ]
212 }
213
Alexei Frolovc912ea72020-10-26 08:43:27 -0700214 pw_source_set(target_name) {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800215 forward_variables_from(invoker, _forwarded_vars)
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800216 public_configs = [ ":$target_name._include_path" ]
217 deps = [ ":$target_name._gen($default_toolchain)" ]
Alexei Frolovc912ea72020-10-26 08:43:27 -0700218 public_deps = [
219 "$dir_pw_rpc:server",
220 "$dir_pw_rpc/raw:method_union",
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800221 ] + invoker.deps
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800222 public = invoker.outputs
Alexei Frolovc912ea72020-10-26 08:43:27 -0700223 }
224}
225
Alexei Frolovdef14712019-12-23 13:03:32 -0800226# Generates Go code for proto files, listing the proto output directory in the
227# metadata variable GOPATH. Internal use only.
Alexei Frolovdef14712019-12-23 13:03:32 -0800228template("_pw_go_proto_library") {
229 _proto_gopath = "$root_gen_dir/go"
Alexei Frolovdef14712019-12-23 13:03:32 -0800230
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800231 _pw_invoke_protoc(target_name) {
232 forward_variables_from(invoker, "*")
233 language = "go"
Alexei Frolovdef14712019-12-23 13:03:32 -0800234 metadata = {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800235 gopath = [ "GOPATH+=" + rebase_path(_proto_gopath) ]
Alexei Frolovc15a9882019-12-23 14:29:02 -0800236 external_deps = [
237 "github.com/golang/protobuf/proto",
238 "google.golang.org/grpc",
239 ]
Alexei Frolovdef14712019-12-23 13:03:32 -0800240 }
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800241
242 # Override the default "$base_out_dir/$language" output path.
243 out_dir = "$_proto_gopath/src"
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800244 }
245
246 group(target_name) {
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800247 deps = invoker.deps + [ ":$target_name._gen($default_toolchain)" ]
Alexei Frolovdef14712019-12-23 13:03:32 -0800248 }
249}
250
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800251# Generates Python code for proto files, creating a pw_python_package containing
252# the generated files. This is internal and should not be used outside of this
253# file. Use pw_proto_library instead.
254template("_pw_python_proto_library") {
255 _target = target_name
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800256
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800257 _pw_invoke_protoc(target_name) {
258 forward_variables_from(invoker, "*", _forwarded_vars)
259 language = "python"
Wyatt Hepler4b921c12021-03-05 15:33:29 -0800260 python_deps = [ "$dir_pw_protobuf_compiler:protobuf_requirements" ]
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800261 }
262
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800263 _setup_py = "${invoker.base_out_dir}/python/setup.py"
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800264
265 # Create the setup and init files for the Python package.
Wyatt Hepler91741472021-02-03 08:45:10 -0800266 action(target_name + "._package_gen") {
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800267 script = "$dir_pw_protobuf_compiler/py/pw_protobuf_compiler/generate_python_package.py"
268 args = [
269 "--setup",
270 rebase_path(_setup_py),
271 "--package",
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800272 invoker._package_dir,
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800273 ] + rebase_path(invoker.outputs, "${invoker.base_out_dir}/python")
Wyatt Hepler32edf122020-12-11 17:21:34 -0800274
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800275 if (invoker.module_as_package != "") {
276 args += [ "--module-as-package" ]
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800277 }
278
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800279 public_deps = [ ":$_target._gen($default_toolchain)" ]
Wyatt Hepler91741472021-02-03 08:45:10 -0800280 outputs = [ _setup_py ]
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800281 }
282
283 # Create a Python package with the generated source files.
284 pw_python_package(target_name) {
285 forward_variables_from(invoker, _forwarded_vars)
286 setup = [ _setup_py ]
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800287 sources = invoker.outputs
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800288 python_deps = invoker.deps
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800289 other_deps = [ ":$_target._package_gen($default_toolchain)" ]
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800290 _pw_generated = true
291 }
292}
293
Alexei Frolov942adf02019-12-11 17:07:28 -0800294# Generates protobuf code from .proto definitions for various languages.
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700295# For each supported generator, creates a sub-target named:
Alexei Frolov942adf02019-12-11 17:07:28 -0800296#
Alexei Frolov8e30d462020-10-22 13:54:36 -0700297# <target_name>.<generator>
Alexei Frolov942adf02019-12-11 17:07:28 -0800298#
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800299# pw_protobuf_library targets generate Python packages. As such, they must have
300# globally unique package names. The first directory of the prefix or the first
301# common directory of the sources is used as the Python package.
302#
Alexei Frolov942adf02019-12-11 17:07:28 -0800303# Args:
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800304# sources: List of input .proto files.
305# deps: List of other pw_proto_library dependencies.
306# inputs: Other files on which the protos depend (e.g. nanopb .options files).
307# prefix: A prefix to add to the source protos prior to compilation. For
308# example, a source called "foo.proto" with prefix = "nested" will be
309# compiled with protoc as "nested/foo.proto".
310# strip_prefix: Remove this prefix from the source protos. All source and
311# input files must be nested under this path.
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700312#
Alexei Frolov942adf02019-12-11 17:07:28 -0800313template("pw_proto_library") {
314 assert(defined(invoker.sources) && invoker.sources != [],
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700315 "pw_proto_library requires .proto source files")
Alexei Frolov942adf02019-12-11 17:07:28 -0800316
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800317 if (defined(invoker.python_module_as_package)) {
318 _module_as_package = invoker.python_module_as_package
319
320 _must_be_one_source = invoker.sources
321 assert([ _must_be_one_source[0] ] == _must_be_one_source,
322 "'python_module_as_package' requires exactly one source file")
323 assert(_module_as_package != "",
324 "'python_module_as_package' cannot be be empty")
325 assert(string_split(_module_as_package, "/") == [ _module_as_package ],
326 "'python_module_as_package' cannot contain slashes")
327 assert(!defined(invoker.prefix),
328 "'prefix' cannot be provided with 'python_module_as_package'")
329 } else {
330 _module_as_package = ""
331 }
332
333 if (defined(invoker.strip_prefix)) {
334 _source_root = get_path_info(invoker.strip_prefix, "abspath")
335 } else {
336 _source_root = get_path_info(".", "abspath")
337 }
338
339 if (defined(invoker.prefix)) {
340 _prefix = invoker.prefix
341 } else {
342 _prefix = ""
343 }
344
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800345 _common = {
346 base_target = target_name
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800347
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800348 # This is the output directory for all files related to this proto library.
349 # Sources are mirrored to "$base_out_dir/sources" and protoc puts outputs in
350 # "$base_out_dir/$language" by default.
351 base_out_dir = get_label_info(":$target_name($default_toolchain)",
352 "target_gen_dir") + "/$target_name"
353
354 compile_dir = "$base_out_dir/sources"
355
356 # Refer to the source files as the are mirrored to the output directory.
357 sources = []
358 foreach(file, rebase_path(invoker.sources, _source_root)) {
359 sources += [ "$compile_dir/$_prefix/$file" ]
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800360 }
361 }
362
Wyatt Hepler91741472021-02-03 08:45:10 -0800363 _package_dir = ""
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800364 _source_names = []
Wyatt Hepler91741472021-02-03 08:45:10 -0800365
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800366 # Determine the Python package name to use for these protos. If there is no
367 # prefix, the first directory the sources are nested under is used.
368 foreach(source, rebase_path(invoker.sources, _source_root)) {
Wyatt Hepler91741472021-02-03 08:45:10 -0800369 _path_components = []
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800370 _path_components = string_split(source, "/")
Wyatt Hepler91741472021-02-03 08:45:10 -0800371
372 if (_package_dir == "") {
373 _package_dir = _path_components[0]
374 } else {
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800375 assert(_prefix != "" || _path_components[0] == _package_dir,
376 "Unless 'prefix' is supplied, all .proto sources in a " +
377 "pw_proto_library must be in the same directory tree")
Wyatt Hepler91741472021-02-03 08:45:10 -0800378 }
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800379
380 _source_names +=
381 [ get_path_info(source, "dir") + "/" + get_path_info(source, "name") ]
Wyatt Hepler91741472021-02-03 08:45:10 -0800382 }
383
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800384 # If the 'prefix' was supplied, use that for the package directory.
385 if (_prefix != "") {
386 _prefix_path_components = string_split(_prefix, "/")
387 _package_dir = _prefix_path_components[0]
388 }
389
390 assert(_package_dir != "" && _package_dir != "." && _package_dir != "..",
391 "Either a 'prefix' must be specified or all sources must be nested " +
392 "under a common directory")
393
394 # Define an action that is never executed to prevent duplicate proto packages
395 # from being declared. The target name and the output file include only the
396 # package directory, so different targets that use the same proto package name
397 # will conflict.
398 action("pw_proto_library.$_package_dir") {
399 script = "$dir_pw_build/py/pw_build/nop.py"
Wyatt Hepler91741472021-02-03 08:45:10 -0800400 visibility = []
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800401
402 # Place an error message in the output path (which is never created). If the
403 # package name conflicts occur in different BUILD.gn files, this results in
404 # an otherwise cryptic Ninja error, rather than a GN error.
405 outputs = [ "$root_out_dir/ " +
406 "ERROR - Multiple pw_proto_library targets create the " +
407 "'$_package_dir' package. Change the package name by setting " +
408 "the \"prefix\" arg or move the protos to a different " +
409 "directory, then re-run gn gen." ]
Wyatt Hepler91741472021-02-03 08:45:10 -0800410 }
411
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800412 if (defined(invoker.deps)) {
413 _deps = invoker.deps
414 } else {
415 _deps = []
416 }
417
Alexei Frolove19ebb82020-05-14 17:21:20 -0700418 # For each proto target, create a file which collects the base directories of
419 # all of its dependencies to list as include paths to protoc.
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800420 generated_file("$target_name._includes") {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800421 # Collect metadata from the include path files of each dependency.
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800422 deps = process_file_template(_deps, "{{source}}._includes")
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800423
Alexei Frolove19ebb82020-05-14 17:21:20 -0700424 data_keys = [ "protoc_includes" ]
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800425 outputs = [ "$target_gen_dir/${_common.base_target}/includes.txt" ]
Alexei Frolove19ebb82020-05-14 17:21:20 -0700426
427 # Indicate this library's base directory for its dependents.
428 metadata = {
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800429 protoc_includes = [ rebase_path(_common.compile_dir) ]
Alexei Frolove19ebb82020-05-14 17:21:20 -0700430 }
431 }
432
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800433 # Mirror the proto sources to the output directory with the prefix added.
434 pw_mirror_tree("$target_name._sources") {
435 source_root = _source_root
436 sources = invoker.sources
Alexei Frolov05d8ef22020-06-08 10:32:29 -0700437
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800438 if (defined(invoker.inputs)) {
439 sources += invoker.inputs
440 }
441
442 directory = "${_common.compile_dir}/$_prefix"
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700443 }
444
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700445 # Enumerate all of the protobuf generator targets.
Alexei Frolovc4b62ec2020-07-13 08:35:10 -0700446
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800447 _pw_pwpb_proto_library("$target_name.pwpb") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700448 forward_variables_from(invoker, _forwarded_vars)
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800449 forward_variables_from(_common, "*")
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800450
451 deps = []
452 foreach(dep, _deps) {
453 _base = get_label_info(dep, "label_no_toolchain")
454 deps += [ "$_base.pwpb(" + get_label_info(dep, "toolchain") + ")" ]
455 }
456
457 outputs = []
458 foreach(name, _source_names) {
459 outputs += [ "$base_out_dir/pwpb/$_prefix/${name}.pwpb.h" ]
460 }
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700461 }
462
463 if (dir_pw_third_party_nanopb != "") {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800464 _pw_nanopb_rpc_proto_library("$target_name.nanopb_rpc") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700465 forward_variables_from(invoker, _forwarded_vars)
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800466 forward_variables_from(_common, "*")
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800467
468 deps = []
469 foreach(dep, _deps) {
470 _lbl = get_label_info(dep, "label_no_toolchain")
471 deps += [ "$_lbl.nanopb_rpc(" + get_label_info(dep, "toolchain") + ")" ]
472 }
473
474 outputs = []
475 foreach(name, _source_names) {
476 outputs += [ "$base_out_dir/nanopb_rpc/$_prefix/${name}.rpc.pb.h" ]
477 }
Alexei Frolovc4b62ec2020-07-13 08:35:10 -0700478 }
479
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800480 _pw_nanopb_proto_library("$target_name.nanopb") {
481 forward_variables_from(invoker, _forwarded_vars)
482 forward_variables_from(_common, "*")
483
484 deps = []
485 foreach(dep, _deps) {
486 _base = get_label_info(dep, "label_no_toolchain")
487 deps += [ "$_base.nanopb(" + get_label_info(dep, "toolchain") + ")" ]
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800488 }
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800489
490 outputs = []
491 foreach(name, _source_names) {
492 outputs += [
493 "$base_out_dir/nanopb/$_prefix/${name}.pb.h",
494 "$base_out_dir/nanopb/$_prefix/${name}.pb.c",
495 ]
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800496 }
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700497 }
498 } else {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800499 pw_error("$target_name.nanopb_rpc") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700500 message =
501 "\$dir_pw_third_party_nanopb must be set to generate nanopb RPC code."
Alexei Frolov8185c822020-06-12 10:45:04 -0700502 }
Alexei Frolov942adf02019-12-11 17:07:28 -0800503
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800504 pw_error("$target_name.nanopb") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700505 message =
506 "\$dir_pw_third_party_nanopb must be set to compile nanopb protobufs."
Alexei Frolov942adf02019-12-11 17:07:28 -0800507 }
508 }
509
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800510 _pw_raw_rpc_proto_library("$target_name.raw_rpc") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700511 forward_variables_from(invoker, _forwarded_vars)
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800512 forward_variables_from(_common, "*")
513
514 deps = []
515 foreach(dep, _deps) {
516 _base = get_label_info(dep, "label_no_toolchain")
517 deps += [ "$_base.raw_rpc(" + get_label_info(dep, "toolchain") + ")" ]
518 }
519
520 outputs = []
521 foreach(name, _source_names) {
522 outputs += [ "$base_out_dir/raw_rpc/$_prefix/${name}.raw_rpc.pb.h" ]
523 }
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700524 }
525
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800526 _pw_go_proto_library("$target_name.go") {
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800527 sources = _common.sources
528
529 deps = []
530 foreach(dep, _deps) {
531 _base = get_label_info(dep, "label_no_toolchain")
532 deps += [ "$_base.go(" + get_label_info(dep, "toolchain") + ")" ]
533 }
534
535 forward_variables_from(_common, "*")
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700536 }
537
Alexei Frolov38cad0c2020-12-03 12:36:24 -0800538 _pw_python_proto_library("$target_name.python") {
Alexei Frolov38cad0c2020-12-03 12:36:24 -0800539 forward_variables_from(_common, "*")
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800540 module_as_package = _module_as_package
541
542 deps = []
543 foreach(dep, _deps) {
544 _base = get_label_info(dep, "label_no_toolchain")
545 deps += [ "$_base.python(" + get_label_info(dep, "toolchain") + ")" ]
546 }
547
548 if (module_as_package == "") {
549 _python_prefix = "$base_out_dir/python/$_prefix"
550 } else {
551 _python_prefix = "$base_out_dir/python/$module_as_package"
552 }
553
554 outputs = []
555 foreach(name, _source_names) {
556 outputs += [
557 "$_python_prefix/${name}_pb2.py",
558 "$_python_prefix/${name}_pb2.pyi",
559 ]
560 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800561 }
562
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700563 # All supported pw_protobuf generators.
564 _protobuf_generators = [
565 "pwpb",
566 "nanopb",
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700567 "nanopb_rpc",
Alexei Frolovc912ea72020-10-26 08:43:27 -0700568 "raw_rpc",
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700569 "go",
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800570 "python",
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700571 ]
572
Alexei Frolov942adf02019-12-11 17:07:28 -0800573 # If the user attempts to use the target directly instead of one of the
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700574 # generator targets, run a script which prints a nice error message.
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700575 pw_python_action(target_name) {
Alexei Frolov942adf02019-12-11 17:07:28 -0800576 script = string_join("/",
577 [
578 dir_pw_protobuf_compiler,
579 "py",
580 "pw_protobuf_compiler",
581 "proto_target_invalid.py",
582 ])
583 args = [
584 "--target",
585 target_name,
586 "--dir",
587 get_path_info(".", "abspath"),
588 "--root",
589 "//",
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700590 ] + _protobuf_generators
Alexei Frolov942adf02019-12-11 17:07:28 -0800591 stamp = true
592 }
593}