blob: 241c6662645c28cee48167137f27a8b327b462ca [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 Heplerf6f74f42021-04-13 19:26:20 -070024import("toolchain.gni")
Wyatt Heplerd49f8fe2020-10-15 10:13:47 -070025
Wyatt Heplerd9336a42020-11-10 09:47:30 -080026# Variables forwarded from the public pw_proto_library template to the final
27# pw_source_set.
Wyatt Heplere0c4fdc2020-05-29 17:26:19 -070028_forwarded_vars = [
29 "testonly",
30 "visibility",
31]
32
Wyatt Heplerd9336a42020-11-10 09:47:30 -080033# Internal template that invokes protoc with a pw_python_action. This should not
34# be used outside of this file; use pw_proto_library instead.
35#
36# This creates the internal GN target $target_name.$language._gen that compiles
37# proto files with protoc.
38template("_pw_invoke_protoc") {
Wyatt Heplerf6f74f42021-04-13 19:26:20 -070039 if (current_toolchain == pw_protobuf_compiler_TOOLCHAIN) {
Wyatt Heplera0377642021-03-09 08:37:10 -080040 if (defined(invoker.out_dir)) {
41 _out_dir = invoker.out_dir
Wyatt Hepler752d7d32021-03-02 09:02:23 -080042 } else {
Wyatt Heplera0377642021-03-09 08:37:10 -080043 _out_dir = "${invoker.base_out_dir}/${invoker.language}"
44 if (defined(invoker.module_as_package) &&
45 invoker.module_as_package != "") {
46 assert(invoker.language == "python")
47 _out_dir = "$_out_dir/${invoker.module_as_package}"
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -080048 }
49 }
Wyatt Heplera0377642021-03-09 08:37:10 -080050
51 _includes =
52 rebase_path(get_target_outputs(":${invoker.base_target}._includes"))
53
54 pw_python_action("$target_name._gen") {
55 script =
56 "$dir_pw_protobuf_compiler/py/pw_protobuf_compiler/generate_protos.py"
57
58 python_deps = [ "$dir_pw_protobuf_compiler/py" ]
59 if (defined(invoker.python_deps)) {
60 python_deps += invoker.python_deps
61 }
62
63 deps = [
64 ":${invoker.base_target}._includes",
65 ":${invoker.base_target}._sources",
66 ]
67
68 foreach(dep, invoker.deps) {
69 deps += [ get_label_info(dep, "label_no_toolchain") + "._gen" ]
70 }
71
Wyatt Heplera3ca62a2021-05-04 16:21:43 -070072 if (defined(invoker.other_deps)) {
73 deps += invoker.other_deps
74 }
75
Wyatt Heplera0377642021-03-09 08:37:10 -080076 args = [
77 "--language",
78 invoker.language,
79 "--include-file",
80 _includes[0],
81 "--compile-dir",
82 rebase_path(invoker.compile_dir),
83 "--out-dir",
84 rebase_path(_out_dir),
85 "--sources",
86 ] + rebase_path(invoker.sources)
87
88 if (defined(invoker.plugin)) {
89 inputs = [ invoker.plugin ]
90 args += [ "--plugin-path=" + rebase_path(invoker.plugin) ]
91 }
92
93 if (defined(invoker.outputs)) {
94 outputs = invoker.outputs
95 } else {
96 stamp = true
97 }
98
99 if (defined(invoker.metadata)) {
100 metadata = invoker.metadata
Wyatt Heplera0377642021-03-09 08:37:10 -0800101 }
102 }
Wyatt Heplercf184e82021-05-11 18:15:35 -0700103
104 # Output a .json file with information about this proto library.
105 _proto_info = {
106 label = get_label_info(":${invoker.target_name}", "label_no_toolchain")
107 protoc_outputs = rebase_path(get_target_outputs(":$target_name._gen"))
108 root = rebase_path(_out_dir)
109 package = invoker.package
110
111 nested_in_python_package = ""
112 if (defined(invoker.python_package)) {
113 nested_in_python_package =
114 get_label_info(invoker.python_package, "label_no_toolchain")
115 }
116
117 dependencies = []
118 foreach(dep, invoker.deps) {
119 dependencies +=
120 rebase_path([ get_label_info(dep, "target_gen_dir") + "/" +
121 get_label_info(dep, "name") + ".json" ])
122 }
123 }
124 write_file("$target_gen_dir/$target_name.json", _proto_info, "json")
Wyatt Heplera0377642021-03-09 08:37:10 -0800125 } else {
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700126 # protoc is only ever invoked from pw_protobuf_compiler_TOOLCHAIN.
Wyatt Heplera0377642021-03-09 08:37:10 -0800127 not_needed([ "target_name" ])
128 not_needed(invoker, "*")
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800129 }
130}
131
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700132# Generates pw_protobuf C++ code for proto files, creating a source_set of the
133# generated files. This is internal and should not be used outside of this file.
134# Use pw_proto_library instead.
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700135template("_pw_pwpb_proto_library") {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800136 _pw_invoke_protoc(target_name) {
137 forward_variables_from(invoker, "*", _forwarded_vars)
138 language = "pwpb"
139 plugin = "$dir_pw_protobuf/py/pw_protobuf/plugin.py"
Wyatt Hepler438caa02021-01-15 17:13:11 -0800140 python_deps = [ "$dir_pw_protobuf/py" ]
Alexei Frolov942adf02019-12-11 17:07:28 -0800141 }
142
143 # Create a library with the generated source files.
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800144 config("$target_name._include_path") {
145 include_dirs = [ "${invoker.base_out_dir}/pwpb" ]
146 visibility = [ ":*" ]
147 }
148
Alexei Frolovedd2f142020-06-09 19:11:27 -0700149 pw_source_set(target_name) {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800150 forward_variables_from(invoker, _forwarded_vars)
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800151 public_configs = [ ":$target_name._include_path" ]
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700152 deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800153 public_deps = [ dir_pw_protobuf ] + invoker.deps
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800154 sources = invoker.outputs
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700155 public = filter_include(sources, [ "*.pwpb.h" ])
156 }
157}
158
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700159# Generates nanopb RPC code for proto files, creating a source_set of the
160# generated files. This is internal and should not be used outside of this file.
161# Use pw_proto_library instead.
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700162template("_pw_nanopb_rpc_proto_library") {
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700163 # Create a target which runs protoc configured with the nanopb_rpc plugin to
164 # generate the C++ proto RPC headers.
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800165 _pw_invoke_protoc(target_name) {
166 forward_variables_from(invoker, "*", _forwarded_vars)
167 language = "nanopb_rpc"
168 plugin = "$dir_pw_rpc/py/pw_rpc/plugin_nanopb.py"
Wyatt Hepler438caa02021-01-15 17:13:11 -0800169 python_deps = [ "$dir_pw_rpc/py" ]
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700170 }
171
172 # Create a library with the generated source files.
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800173 config("$target_name._include_path") {
174 include_dirs = [ "${invoker.base_out_dir}/nanopb_rpc" ]
175 visibility = [ ":*" ]
176 }
177
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700178 pw_source_set(target_name) {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800179 forward_variables_from(invoker, _forwarded_vars)
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800180 public_configs = [ ":$target_name._include_path" ]
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700181 deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700182 public_deps = [
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800183 ":${invoker.base_target}.nanopb",
Wyatt Heplercbd09c22020-09-15 11:17:24 -0700184 "$dir_pw_rpc:server",
Alexei Frolov2d737bc2021-04-27 23:03:09 -0700185 "$dir_pw_rpc/nanopb:client",
Alexei Frolova4d71502020-10-14 12:43:14 -0700186 "$dir_pw_rpc/nanopb:method_union",
Alexei Frolovdd6fa5c2020-08-11 10:04:01 -0700187 "$dir_pw_third_party/nanopb",
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800188 ] + invoker.deps
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800189 public = invoker.outputs
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700190 }
191}
192
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700193# Generates nanopb code for proto files, creating a source_set of the generated
194# files. This is internal and should not be used outside of this file. Use
195# pw_proto_library instead.
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700196template("_pw_nanopb_proto_library") {
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800197 # When compiling with the Nanopb plugin, the nanopb.proto file is already
198 # compiled internally, so skip recompiling it with protoc.
199 if (rebase_path(invoker.sources, invoker.compile_dir) == [ "nanopb.proto" ]) {
200 group("$target_name._gen") {
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700201 deps = [
202 ":${invoker.base_target}._sources($pw_protobuf_compiler_TOOLCHAIN)",
203 ]
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800204 }
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700205
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800206 group("$target_name") {
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700207 deps = invoker.deps +
208 [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800209 }
210 } else {
211 # Create a target which runs protoc configured with the nanopb plugin to
212 # generate the C proto sources.
213 _pw_invoke_protoc(target_name) {
214 forward_variables_from(invoker, "*", _forwarded_vars)
215 language = "nanopb"
216 plugin = "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb"
Wyatt Heplera3ca62a2021-05-04 16:21:43 -0700217 other_deps = [ "$dir_pw_third_party/nanopb:generate_nanopb_proto.action" ]
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800218 }
219
220 # Create a library with the generated source files.
221 config("$target_name._include_path") {
222 include_dirs = [ "${invoker.base_out_dir}/nanopb" ]
223 visibility = [ ":*" ]
224 }
225
226 pw_source_set(target_name) {
227 forward_variables_from(invoker, _forwarded_vars)
228 public_configs = [ ":$target_name._include_path" ]
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700229 deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800230 public_deps = [ "$dir_pw_third_party/nanopb" ] + invoker.deps
231 sources = invoker.outputs
232 public = filter_include(sources, [ "*.pb.h" ])
Wyatt Heplerb93749b2021-05-11 09:38:22 -0700233 check_includes = false
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800234 }
Alexei Frolov942adf02019-12-11 17:07:28 -0800235 }
236}
237
Alexei Frolovc912ea72020-10-26 08:43:27 -0700238# Generates raw RPC code for proto files, creating a source_set of the generated
239# files. This is internal and should not be used outside of this file. Use
240# pw_proto_library instead.
Alexei Frolovc912ea72020-10-26 08:43:27 -0700241template("_pw_raw_rpc_proto_library") {
Alexei Frolovc912ea72020-10-26 08:43:27 -0700242 # Create a target which runs protoc configured with the nanopb_rpc plugin to
243 # generate the C++ proto RPC headers.
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800244 _pw_invoke_protoc(target_name) {
245 forward_variables_from(invoker, "*", _forwarded_vars)
246 language = "raw_rpc"
247 plugin = "$dir_pw_rpc/py/pw_rpc/plugin_raw.py"
Wyatt Hepler438caa02021-01-15 17:13:11 -0800248 python_deps = [ "$dir_pw_rpc/py" ]
Alexei Frolovc912ea72020-10-26 08:43:27 -0700249 }
250
251 # Create a library with the generated source files.
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800252 config("$target_name._include_path") {
253 include_dirs = [ "${invoker.base_out_dir}/raw_rpc" ]
254 visibility = [ ":*" ]
255 }
256
Alexei Frolovc912ea72020-10-26 08:43:27 -0700257 pw_source_set(target_name) {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800258 forward_variables_from(invoker, _forwarded_vars)
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800259 public_configs = [ ":$target_name._include_path" ]
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700260 deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
Alexei Frolovc912ea72020-10-26 08:43:27 -0700261 public_deps = [
262 "$dir_pw_rpc:server",
263 "$dir_pw_rpc/raw:method_union",
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800264 ] + invoker.deps
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800265 public = invoker.outputs
Wyatt Heplerb93749b2021-05-11 09:38:22 -0700266 check_includes = false
Alexei Frolovc912ea72020-10-26 08:43:27 -0700267 }
268}
269
Alexei Frolovdef14712019-12-23 13:03:32 -0800270# Generates Go code for proto files, listing the proto output directory in the
271# metadata variable GOPATH. Internal use only.
Alexei Frolovdef14712019-12-23 13:03:32 -0800272template("_pw_go_proto_library") {
273 _proto_gopath = "$root_gen_dir/go"
Alexei Frolovdef14712019-12-23 13:03:32 -0800274
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800275 _pw_invoke_protoc(target_name) {
276 forward_variables_from(invoker, "*")
277 language = "go"
Alexei Frolovdef14712019-12-23 13:03:32 -0800278 metadata = {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800279 gopath = [ "GOPATH+=" + rebase_path(_proto_gopath) ]
Alexei Frolovc15a9882019-12-23 14:29:02 -0800280 external_deps = [
281 "github.com/golang/protobuf/proto",
282 "google.golang.org/grpc",
283 ]
Alexei Frolovdef14712019-12-23 13:03:32 -0800284 }
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800285
286 # Override the default "$base_out_dir/$language" output path.
287 out_dir = "$_proto_gopath/src"
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800288 }
289
290 group(target_name) {
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700291 deps =
292 invoker.deps + [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
Alexei Frolovdef14712019-12-23 13:03:32 -0800293 }
294}
295
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800296# Generates Python code for proto files, creating a pw_python_package containing
297# the generated files. This is internal and should not be used outside of this
298# file. Use pw_proto_library instead.
299template("_pw_python_proto_library") {
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800300 _pw_invoke_protoc(target_name) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800301 forward_variables_from(invoker, "*", _forwarded_vars + [ "python_package" ])
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800302 language = "python"
Wyatt Hepler4b921c12021-03-05 15:33:29 -0800303 python_deps = [ "$dir_pw_protobuf_compiler:protobuf_requirements" ]
Wyatt Heplercf184e82021-05-11 18:15:35 -0700304
305 if (defined(invoker.python_package)) {
306 python_package = invoker.python_package
307 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800308 }
309
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800310 if (defined(invoker.python_package) && invoker.python_package != "") {
Wyatt Heplerb2062fd2021-06-01 21:33:07 -0700311 # This package is nested in another Python package. Depending on this
312 # its python subtarget is equivalent to depending on the Python package it
313 # is nested in.
314 pw_python_group(target_name) {
315 python_deps = [ invoker.python_package ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800316 }
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800317
Wyatt Heplercf184e82021-05-11 18:15:35 -0700318 # This proto library is merged into another package, but create a target to
319 # collect its dependencies that the other package can depend on.
320 pw_python_group(target_name + "._deps") {
321 python_deps = invoker.deps
322 other_deps =
323 [ ":${invoker.target_name}._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
324 }
325 } else {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800326 # Create a Python package with the generated source files.
327 pw_python_package(target_name) {
328 forward_variables_from(invoker, _forwarded_vars)
329 generate_setup = {
330 name = invoker._package_dir
331 version = "0.0.1" # TODO(hepler): Need to be able to set this verison.
332 }
333 sources = invoker.outputs
334 strip_prefix = "${invoker.base_out_dir}/python"
335 python_deps = invoker.deps
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700336 other_deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700337 static_analysis = []
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800338
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800339 _pw_module_as_package = invoker.module_as_package != ""
340 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800341 }
342}
343
Alexei Frolov942adf02019-12-11 17:07:28 -0800344# Generates protobuf code from .proto definitions for various languages.
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700345# For each supported generator, creates a sub-target named:
Alexei Frolov942adf02019-12-11 17:07:28 -0800346#
Alexei Frolov8e30d462020-10-22 13:54:36 -0700347# <target_name>.<generator>
Alexei Frolov942adf02019-12-11 17:07:28 -0800348#
Wyatt Hepler4cdc3e72021-03-24 08:41:08 -0700349# GN permits using abbreviated labels when the target name matches the directory
350# name (e.g. //foo for //foo:foo). For consistency with this, the sub-targets
351# for each generator are aliased to the directory when the target name is the
352# same. For example, these two labels are equivalent:
353#
354# //path/to/my_protos:my_protos.pwpb
355# //path/to/my_protos:pwpb
356#
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800357# pw_protobuf_library targets generate Python packages. As such, they must have
358# globally unique package names. The first directory of the prefix or the first
359# common directory of the sources is used as the Python package.
360#
Alexei Frolov942adf02019-12-11 17:07:28 -0800361# Args:
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800362# sources: List of input .proto files.
363# deps: List of other pw_proto_library dependencies.
364# inputs: Other files on which the protos depend (e.g. nanopb .options files).
365# prefix: A prefix to add to the source protos prior to compilation. For
366# example, a source called "foo.proto" with prefix = "nested" will be
367# compiled with protoc as "nested/foo.proto".
368# strip_prefix: Remove this prefix from the source protos. All source and
369# input files must be nested under this path.
Wyatt Hepler4cdc3e72021-03-24 08:41:08 -0700370# python_package: Label of Python package to which to add the proto modules.
Wyatt Heplerb2062fd2021-06-01 21:33:07 -0700371# The .python subtarget will redirect to this package.
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700372#
Alexei Frolov942adf02019-12-11 17:07:28 -0800373template("pw_proto_library") {
374 assert(defined(invoker.sources) && invoker.sources != [],
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700375 "pw_proto_library requires .proto source files")
Alexei Frolov942adf02019-12-11 17:07:28 -0800376
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800377 if (defined(invoker.python_module_as_package)) {
378 _module_as_package = invoker.python_module_as_package
379
380 _must_be_one_source = invoker.sources
381 assert([ _must_be_one_source[0] ] == _must_be_one_source,
382 "'python_module_as_package' requires exactly one source file")
383 assert(_module_as_package != "",
384 "'python_module_as_package' cannot be be empty")
385 assert(string_split(_module_as_package, "/") == [ _module_as_package ],
386 "'python_module_as_package' cannot contain slashes")
387 assert(!defined(invoker.prefix),
388 "'prefix' cannot be provided with 'python_module_as_package'")
389 } else {
390 _module_as_package = ""
391 }
392
393 if (defined(invoker.strip_prefix)) {
394 _source_root = get_path_info(invoker.strip_prefix, "abspath")
395 } else {
396 _source_root = get_path_info(".", "abspath")
397 }
398
399 if (defined(invoker.prefix)) {
400 _prefix = invoker.prefix
401 } else {
402 _prefix = ""
403 }
404
Wyatt Hepler91741472021-02-03 08:45:10 -0800405 _package_dir = ""
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800406 _source_names = []
Wyatt Hepler91741472021-02-03 08:45:10 -0800407
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800408 # Determine the Python package name to use for these protos. If there is no
409 # prefix, the first directory the sources are nested under is used.
410 foreach(source, rebase_path(invoker.sources, _source_root)) {
Wyatt Hepler91741472021-02-03 08:45:10 -0800411 _path_components = []
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800412 _path_components = string_split(source, "/")
Wyatt Hepler91741472021-02-03 08:45:10 -0800413
414 if (_package_dir == "") {
415 _package_dir = _path_components[0]
416 } else {
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800417 assert(_prefix != "" || _path_components[0] == _package_dir,
418 "Unless 'prefix' is supplied, all .proto sources in a " +
419 "pw_proto_library must be in the same directory tree")
Wyatt Hepler91741472021-02-03 08:45:10 -0800420 }
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800421
422 _source_names +=
423 [ get_path_info(source, "dir") + "/" + get_path_info(source, "name") ]
Wyatt Hepler91741472021-02-03 08:45:10 -0800424 }
425
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800426 # If the 'prefix' was supplied, use that for the package directory.
427 if (_prefix != "") {
428 _prefix_path_components = string_split(_prefix, "/")
429 _package_dir = _prefix_path_components[0]
430 }
431
432 assert(_package_dir != "" && _package_dir != "." && _package_dir != "..",
433 "Either a 'prefix' must be specified or all sources must be nested " +
434 "under a common directory")
435
436 # Define an action that is never executed to prevent duplicate proto packages
437 # from being declared. The target name and the output file include only the
438 # package directory, so different targets that use the same proto package name
439 # will conflict.
440 action("pw_proto_library.$_package_dir") {
441 script = "$dir_pw_build/py/pw_build/nop.py"
Wyatt Hepler91741472021-02-03 08:45:10 -0800442 visibility = []
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800443
444 # Place an error message in the output path (which is never created). If the
445 # package name conflicts occur in different BUILD.gn files, this results in
446 # an otherwise cryptic Ninja error, rather than a GN error.
447 outputs = [ "$root_out_dir/ " +
448 "ERROR - Multiple pw_proto_library targets create the " +
449 "'$_package_dir' package. Change the package name by setting " +
450 "the \"prefix\" arg or move the protos to a different " +
451 "directory, then re-run gn gen." ]
Wyatt Hepler91741472021-02-03 08:45:10 -0800452 }
453
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800454 if (defined(invoker.deps)) {
455 _deps = invoker.deps
456 } else {
457 _deps = []
458 }
459
Wyatt Heplercf184e82021-05-11 18:15:35 -0700460 _common = {
461 base_target = target_name
462
463 # This is the output directory for all files related to this proto library.
464 # Sources are mirrored to "$base_out_dir/sources" and protoc puts outputs in
465 # "$base_out_dir/$language" by default.
466 base_out_dir =
467 get_label_info(":$target_name($pw_protobuf_compiler_TOOLCHAIN)",
468 "target_gen_dir") + "/$target_name.proto_library"
469
470 compile_dir = "$base_out_dir/sources"
471
472 # Refer to the source files as the are mirrored to the output directory.
473 sources = []
474 foreach(file, rebase_path(invoker.sources, _source_root)) {
475 sources += [ "$compile_dir/$_prefix/$file" ]
476 }
477
478 package = _package_dir
479 }
480
Alexei Frolove19ebb82020-05-14 17:21:20 -0700481 # For each proto target, create a file which collects the base directories of
482 # all of its dependencies to list as include paths to protoc.
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800483 generated_file("$target_name._includes") {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800484 # Collect metadata from the include path files of each dependency.
Wyatt Hepler4cdc3e72021-03-24 08:41:08 -0700485
486 deps = []
487 foreach(dep, _deps) {
488 _base = get_label_info(dep, "label_no_toolchain")
489 deps += [ "$_base._includes(" + get_label_info(dep, "toolchain") + ")" ]
490 }
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800491
Alexei Frolove19ebb82020-05-14 17:21:20 -0700492 data_keys = [ "protoc_includes" ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800493 outputs = [ "${_common.base_out_dir}/includes.txt" ]
Alexei Frolove19ebb82020-05-14 17:21:20 -0700494
495 # Indicate this library's base directory for its dependents.
496 metadata = {
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800497 protoc_includes = [ rebase_path(_common.compile_dir) ]
Alexei Frolove19ebb82020-05-14 17:21:20 -0700498 }
499 }
500
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800501 # Mirror the proto sources to the output directory with the prefix added.
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700502 if (current_toolchain == pw_protobuf_compiler_TOOLCHAIN) {
503 pw_mirror_tree("$target_name._sources") {
504 source_root = _source_root
505 sources = invoker.sources
Alexei Frolov05d8ef22020-06-08 10:32:29 -0700506
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700507 if (defined(invoker.inputs)) {
508 sources += invoker.inputs
509 }
510
511 directory = "${_common.compile_dir}/$_prefix"
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800512 }
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700513 } else {
514 not_needed(invoker, [ "inputs" ])
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700515 }
516
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700517 # Enumerate all of the protobuf generator targets.
Alexei Frolovc4b62ec2020-07-13 08:35:10 -0700518
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800519 _pw_pwpb_proto_library("$target_name.pwpb") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700520 forward_variables_from(invoker, _forwarded_vars)
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800521 forward_variables_from(_common, "*")
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800522
523 deps = []
524 foreach(dep, _deps) {
525 _base = get_label_info(dep, "label_no_toolchain")
526 deps += [ "$_base.pwpb(" + get_label_info(dep, "toolchain") + ")" ]
527 }
528
529 outputs = []
530 foreach(name, _source_names) {
531 outputs += [ "$base_out_dir/pwpb/$_prefix/${name}.pwpb.h" ]
532 }
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700533 }
534
535 if (dir_pw_third_party_nanopb != "") {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800536 _pw_nanopb_rpc_proto_library("$target_name.nanopb_rpc") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700537 forward_variables_from(invoker, _forwarded_vars)
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800538 forward_variables_from(_common, "*")
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800539
540 deps = []
541 foreach(dep, _deps) {
542 _lbl = get_label_info(dep, "label_no_toolchain")
543 deps += [ "$_lbl.nanopb_rpc(" + get_label_info(dep, "toolchain") + ")" ]
544 }
545
546 outputs = []
547 foreach(name, _source_names) {
548 outputs += [ "$base_out_dir/nanopb_rpc/$_prefix/${name}.rpc.pb.h" ]
549 }
Alexei Frolovc4b62ec2020-07-13 08:35:10 -0700550 }
551
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800552 _pw_nanopb_proto_library("$target_name.nanopb") {
553 forward_variables_from(invoker, _forwarded_vars)
554 forward_variables_from(_common, "*")
555
556 deps = []
557 foreach(dep, _deps) {
558 _base = get_label_info(dep, "label_no_toolchain")
559 deps += [ "$_base.nanopb(" + get_label_info(dep, "toolchain") + ")" ]
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800560 }
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800561
562 outputs = []
563 foreach(name, _source_names) {
564 outputs += [
565 "$base_out_dir/nanopb/$_prefix/${name}.pb.h",
566 "$base_out_dir/nanopb/$_prefix/${name}.pb.c",
567 ]
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800568 }
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700569 }
570 } else {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800571 pw_error("$target_name.nanopb_rpc") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700572 message =
573 "\$dir_pw_third_party_nanopb must be set to generate nanopb RPC code."
Alexei Frolov8185c822020-06-12 10:45:04 -0700574 }
Alexei Frolov942adf02019-12-11 17:07:28 -0800575
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800576 pw_error("$target_name.nanopb") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700577 message =
578 "\$dir_pw_third_party_nanopb must be set to compile nanopb protobufs."
Alexei Frolov942adf02019-12-11 17:07:28 -0800579 }
580 }
581
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800582 _pw_raw_rpc_proto_library("$target_name.raw_rpc") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700583 forward_variables_from(invoker, _forwarded_vars)
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800584 forward_variables_from(_common, "*")
585
586 deps = []
587 foreach(dep, _deps) {
588 _base = get_label_info(dep, "label_no_toolchain")
589 deps += [ "$_base.raw_rpc(" + get_label_info(dep, "toolchain") + ")" ]
590 }
591
592 outputs = []
593 foreach(name, _source_names) {
594 outputs += [ "$base_out_dir/raw_rpc/$_prefix/${name}.raw_rpc.pb.h" ]
595 }
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700596 }
597
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800598 _pw_go_proto_library("$target_name.go") {
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800599 sources = _common.sources
600
601 deps = []
602 foreach(dep, _deps) {
603 _base = get_label_info(dep, "label_no_toolchain")
604 deps += [ "$_base.go(" + get_label_info(dep, "toolchain") + ")" ]
605 }
606
607 forward_variables_from(_common, "*")
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700608 }
609
Alexei Frolov38cad0c2020-12-03 12:36:24 -0800610 _pw_python_proto_library("$target_name.python") {
Alexei Frolov38cad0c2020-12-03 12:36:24 -0800611 forward_variables_from(_common, "*")
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800612 forward_variables_from(invoker, [ "python_package" ])
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800613 module_as_package = _module_as_package
614
615 deps = []
616 foreach(dep, _deps) {
617 _base = get_label_info(dep, "label_no_toolchain")
618 deps += [ "$_base.python(" + get_label_info(dep, "toolchain") + ")" ]
619 }
620
621 if (module_as_package == "") {
622 _python_prefix = "$base_out_dir/python/$_prefix"
623 } else {
624 _python_prefix = "$base_out_dir/python/$module_as_package"
625 }
626
627 outputs = []
628 foreach(name, _source_names) {
629 outputs += [
630 "$_python_prefix/${name}_pb2.py",
631 "$_python_prefix/${name}_pb2.pyi",
632 ]
633 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800634 }
635
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700636 # All supported pw_protobuf generators.
637 _protobuf_generators = [
638 "pwpb",
639 "nanopb",
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700640 "nanopb_rpc",
Alexei Frolovc912ea72020-10-26 08:43:27 -0700641 "raw_rpc",
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700642 "go",
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800643 "python",
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700644 ]
645
Wyatt Hepler4cdc3e72021-03-24 08:41:08 -0700646 # If the label matches the directory name, alias the subtargets to the
647 # directory (e.g. //foo:nanopb is an alias for //foo:foo.nanopb).
648 if (get_label_info(":$target_name", "name") ==
649 get_path_info(get_label_info(":$target_name", "dir"), "name")) {
Wyatt Hepler2c6c0ba2021-04-07 09:50:23 -0700650 foreach(_generator, _protobuf_generators - [ "python" ]) {
651 group(_generator) {
Wyatt Hepler4cdc3e72021-03-24 08:41:08 -0700652 public_deps = [ ":${invoker.target_name}.$_generator" ]
653 }
654 }
Wyatt Hepler2c6c0ba2021-04-07 09:50:23 -0700655
656 pw_python_group("python") {
657 python_deps = [ ":${invoker.target_name}.python" ]
658 }
Wyatt Hepler4cdc3e72021-03-24 08:41:08 -0700659 }
660
Alexei Frolov942adf02019-12-11 17:07:28 -0800661 # If the user attempts to use the target directly instead of one of the
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700662 # generator targets, run a script which prints a nice error message.
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700663 pw_python_action(target_name) {
Alexei Frolov942adf02019-12-11 17:07:28 -0800664 script = string_join("/",
665 [
666 dir_pw_protobuf_compiler,
667 "py",
668 "pw_protobuf_compiler",
669 "proto_target_invalid.py",
670 ])
671 args = [
672 "--target",
673 target_name,
674 "--dir",
675 get_path_info(".", "abspath"),
676 "--root",
677 "//",
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700678 ] + _protobuf_generators
Alexei Frolov942adf02019-12-11 17:07:28 -0800679 stamp = true
680 }
681}