blob: 07f4d52c10c4057628f03bff0adb188eb24e5ef5 [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 Heplerdcfcecf2021-03-01 08:36:19 -0800311 # If anyone attempts to depend on this Python package, print an error.
312 pw_error(target_name) {
313 _pkg = get_label_info(invoker.python_package, "label_no_toolchain")
314 message_lines = [
315 "This proto Python package is embedded in the $_pkg Python package.",
316 "It cannot be used directly; instead, depend on $_pkg.",
317 ]
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800318 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800319 foreach(subtarget, pw_python_package_subtargets) {
320 group("$target_name.$subtarget") {
321 deps = [ ":${invoker.target_name}" ]
322 }
323 }
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800324
Wyatt Heplercf184e82021-05-11 18:15:35 -0700325 # This proto library is merged into another package, but create a target to
326 # collect its dependencies that the other package can depend on.
327 pw_python_group(target_name + "._deps") {
328 python_deps = invoker.deps
329 other_deps =
330 [ ":${invoker.target_name}._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
331 }
332 } else {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800333 # Create a Python package with the generated source files.
334 pw_python_package(target_name) {
335 forward_variables_from(invoker, _forwarded_vars)
336 generate_setup = {
337 name = invoker._package_dir
338 version = "0.0.1" # TODO(hepler): Need to be able to set this verison.
339 }
340 sources = invoker.outputs
341 strip_prefix = "${invoker.base_out_dir}/python"
342 python_deps = invoker.deps
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700343 other_deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700344 static_analysis = []
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800345
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800346 _pw_module_as_package = invoker.module_as_package != ""
347 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800348 }
349}
350
Alexei Frolov942adf02019-12-11 17:07:28 -0800351# Generates protobuf code from .proto definitions for various languages.
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700352# For each supported generator, creates a sub-target named:
Alexei Frolov942adf02019-12-11 17:07:28 -0800353#
Alexei Frolov8e30d462020-10-22 13:54:36 -0700354# <target_name>.<generator>
Alexei Frolov942adf02019-12-11 17:07:28 -0800355#
Wyatt Hepler4cdc3e72021-03-24 08:41:08 -0700356# GN permits using abbreviated labels when the target name matches the directory
357# name (e.g. //foo for //foo:foo). For consistency with this, the sub-targets
358# for each generator are aliased to the directory when the target name is the
359# same. For example, these two labels are equivalent:
360#
361# //path/to/my_protos:my_protos.pwpb
362# //path/to/my_protos:pwpb
363#
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800364# pw_protobuf_library targets generate Python packages. As such, they must have
365# globally unique package names. The first directory of the prefix or the first
366# common directory of the sources is used as the Python package.
367#
Alexei Frolov942adf02019-12-11 17:07:28 -0800368# Args:
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800369# sources: List of input .proto files.
370# deps: List of other pw_proto_library dependencies.
371# inputs: Other files on which the protos depend (e.g. nanopb .options files).
372# prefix: A prefix to add to the source protos prior to compilation. For
373# example, a source called "foo.proto" with prefix = "nested" will be
374# compiled with protoc as "nested/foo.proto".
375# strip_prefix: Remove this prefix from the source protos. All source and
376# input files must be nested under this path.
Wyatt Hepler4cdc3e72021-03-24 08:41:08 -0700377# python_package: Label of Python package to which to add the proto modules.
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700378#
Alexei Frolov942adf02019-12-11 17:07:28 -0800379template("pw_proto_library") {
380 assert(defined(invoker.sources) && invoker.sources != [],
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700381 "pw_proto_library requires .proto source files")
Alexei Frolov942adf02019-12-11 17:07:28 -0800382
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800383 if (defined(invoker.python_module_as_package)) {
384 _module_as_package = invoker.python_module_as_package
385
386 _must_be_one_source = invoker.sources
387 assert([ _must_be_one_source[0] ] == _must_be_one_source,
388 "'python_module_as_package' requires exactly one source file")
389 assert(_module_as_package != "",
390 "'python_module_as_package' cannot be be empty")
391 assert(string_split(_module_as_package, "/") == [ _module_as_package ],
392 "'python_module_as_package' cannot contain slashes")
393 assert(!defined(invoker.prefix),
394 "'prefix' cannot be provided with 'python_module_as_package'")
395 } else {
396 _module_as_package = ""
397 }
398
399 if (defined(invoker.strip_prefix)) {
400 _source_root = get_path_info(invoker.strip_prefix, "abspath")
401 } else {
402 _source_root = get_path_info(".", "abspath")
403 }
404
405 if (defined(invoker.prefix)) {
406 _prefix = invoker.prefix
407 } else {
408 _prefix = ""
409 }
410
Wyatt Hepler91741472021-02-03 08:45:10 -0800411 _package_dir = ""
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800412 _source_names = []
Wyatt Hepler91741472021-02-03 08:45:10 -0800413
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800414 # Determine the Python package name to use for these protos. If there is no
415 # prefix, the first directory the sources are nested under is used.
416 foreach(source, rebase_path(invoker.sources, _source_root)) {
Wyatt Hepler91741472021-02-03 08:45:10 -0800417 _path_components = []
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800418 _path_components = string_split(source, "/")
Wyatt Hepler91741472021-02-03 08:45:10 -0800419
420 if (_package_dir == "") {
421 _package_dir = _path_components[0]
422 } else {
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800423 assert(_prefix != "" || _path_components[0] == _package_dir,
424 "Unless 'prefix' is supplied, all .proto sources in a " +
425 "pw_proto_library must be in the same directory tree")
Wyatt Hepler91741472021-02-03 08:45:10 -0800426 }
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800427
428 _source_names +=
429 [ get_path_info(source, "dir") + "/" + get_path_info(source, "name") ]
Wyatt Hepler91741472021-02-03 08:45:10 -0800430 }
431
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800432 # If the 'prefix' was supplied, use that for the package directory.
433 if (_prefix != "") {
434 _prefix_path_components = string_split(_prefix, "/")
435 _package_dir = _prefix_path_components[0]
436 }
437
438 assert(_package_dir != "" && _package_dir != "." && _package_dir != "..",
439 "Either a 'prefix' must be specified or all sources must be nested " +
440 "under a common directory")
441
442 # Define an action that is never executed to prevent duplicate proto packages
443 # from being declared. The target name and the output file include only the
444 # package directory, so different targets that use the same proto package name
445 # will conflict.
446 action("pw_proto_library.$_package_dir") {
447 script = "$dir_pw_build/py/pw_build/nop.py"
Wyatt Hepler91741472021-02-03 08:45:10 -0800448 visibility = []
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800449
450 # Place an error message in the output path (which is never created). If the
451 # package name conflicts occur in different BUILD.gn files, this results in
452 # an otherwise cryptic Ninja error, rather than a GN error.
453 outputs = [ "$root_out_dir/ " +
454 "ERROR - Multiple pw_proto_library targets create the " +
455 "'$_package_dir' package. Change the package name by setting " +
456 "the \"prefix\" arg or move the protos to a different " +
457 "directory, then re-run gn gen." ]
Wyatt Hepler91741472021-02-03 08:45:10 -0800458 }
459
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800460 if (defined(invoker.deps)) {
461 _deps = invoker.deps
462 } else {
463 _deps = []
464 }
465
Wyatt Heplercf184e82021-05-11 18:15:35 -0700466 _common = {
467 base_target = target_name
468
469 # This is the output directory for all files related to this proto library.
470 # Sources are mirrored to "$base_out_dir/sources" and protoc puts outputs in
471 # "$base_out_dir/$language" by default.
472 base_out_dir =
473 get_label_info(":$target_name($pw_protobuf_compiler_TOOLCHAIN)",
474 "target_gen_dir") + "/$target_name.proto_library"
475
476 compile_dir = "$base_out_dir/sources"
477
478 # Refer to the source files as the are mirrored to the output directory.
479 sources = []
480 foreach(file, rebase_path(invoker.sources, _source_root)) {
481 sources += [ "$compile_dir/$_prefix/$file" ]
482 }
483
484 package = _package_dir
485 }
486
Alexei Frolove19ebb82020-05-14 17:21:20 -0700487 # For each proto target, create a file which collects the base directories of
488 # all of its dependencies to list as include paths to protoc.
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800489 generated_file("$target_name._includes") {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800490 # Collect metadata from the include path files of each dependency.
Wyatt Hepler4cdc3e72021-03-24 08:41:08 -0700491
492 deps = []
493 foreach(dep, _deps) {
494 _base = get_label_info(dep, "label_no_toolchain")
495 deps += [ "$_base._includes(" + get_label_info(dep, "toolchain") + ")" ]
496 }
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800497
Alexei Frolove19ebb82020-05-14 17:21:20 -0700498 data_keys = [ "protoc_includes" ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800499 outputs = [ "${_common.base_out_dir}/includes.txt" ]
Alexei Frolove19ebb82020-05-14 17:21:20 -0700500
501 # Indicate this library's base directory for its dependents.
502 metadata = {
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800503 protoc_includes = [ rebase_path(_common.compile_dir) ]
Alexei Frolove19ebb82020-05-14 17:21:20 -0700504 }
505 }
506
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800507 # Mirror the proto sources to the output directory with the prefix added.
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700508 if (current_toolchain == pw_protobuf_compiler_TOOLCHAIN) {
509 pw_mirror_tree("$target_name._sources") {
510 source_root = _source_root
511 sources = invoker.sources
Alexei Frolov05d8ef22020-06-08 10:32:29 -0700512
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700513 if (defined(invoker.inputs)) {
514 sources += invoker.inputs
515 }
516
517 directory = "${_common.compile_dir}/$_prefix"
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800518 }
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700519 } else {
520 not_needed(invoker, [ "inputs" ])
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700521 }
522
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700523 # Enumerate all of the protobuf generator targets.
Alexei Frolovc4b62ec2020-07-13 08:35:10 -0700524
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800525 _pw_pwpb_proto_library("$target_name.pwpb") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700526 forward_variables_from(invoker, _forwarded_vars)
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800527 forward_variables_from(_common, "*")
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800528
529 deps = []
530 foreach(dep, _deps) {
531 _base = get_label_info(dep, "label_no_toolchain")
532 deps += [ "$_base.pwpb(" + get_label_info(dep, "toolchain") + ")" ]
533 }
534
535 outputs = []
536 foreach(name, _source_names) {
537 outputs += [ "$base_out_dir/pwpb/$_prefix/${name}.pwpb.h" ]
538 }
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700539 }
540
541 if (dir_pw_third_party_nanopb != "") {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800542 _pw_nanopb_rpc_proto_library("$target_name.nanopb_rpc") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700543 forward_variables_from(invoker, _forwarded_vars)
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800544 forward_variables_from(_common, "*")
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800545
546 deps = []
547 foreach(dep, _deps) {
548 _lbl = get_label_info(dep, "label_no_toolchain")
549 deps += [ "$_lbl.nanopb_rpc(" + get_label_info(dep, "toolchain") + ")" ]
550 }
551
552 outputs = []
553 foreach(name, _source_names) {
554 outputs += [ "$base_out_dir/nanopb_rpc/$_prefix/${name}.rpc.pb.h" ]
555 }
Alexei Frolovc4b62ec2020-07-13 08:35:10 -0700556 }
557
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800558 _pw_nanopb_proto_library("$target_name.nanopb") {
559 forward_variables_from(invoker, _forwarded_vars)
560 forward_variables_from(_common, "*")
561
562 deps = []
563 foreach(dep, _deps) {
564 _base = get_label_info(dep, "label_no_toolchain")
565 deps += [ "$_base.nanopb(" + get_label_info(dep, "toolchain") + ")" ]
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800566 }
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800567
568 outputs = []
569 foreach(name, _source_names) {
570 outputs += [
571 "$base_out_dir/nanopb/$_prefix/${name}.pb.h",
572 "$base_out_dir/nanopb/$_prefix/${name}.pb.c",
573 ]
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800574 }
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700575 }
576 } else {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800577 pw_error("$target_name.nanopb_rpc") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700578 message =
579 "\$dir_pw_third_party_nanopb must be set to generate nanopb RPC code."
Alexei Frolov8185c822020-06-12 10:45:04 -0700580 }
Alexei Frolov942adf02019-12-11 17:07:28 -0800581
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800582 pw_error("$target_name.nanopb") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700583 message =
584 "\$dir_pw_third_party_nanopb must be set to compile nanopb protobufs."
Alexei Frolov942adf02019-12-11 17:07:28 -0800585 }
586 }
587
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800588 _pw_raw_rpc_proto_library("$target_name.raw_rpc") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700589 forward_variables_from(invoker, _forwarded_vars)
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800590 forward_variables_from(_common, "*")
591
592 deps = []
593 foreach(dep, _deps) {
594 _base = get_label_info(dep, "label_no_toolchain")
595 deps += [ "$_base.raw_rpc(" + get_label_info(dep, "toolchain") + ")" ]
596 }
597
598 outputs = []
599 foreach(name, _source_names) {
600 outputs += [ "$base_out_dir/raw_rpc/$_prefix/${name}.raw_rpc.pb.h" ]
601 }
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700602 }
603
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800604 _pw_go_proto_library("$target_name.go") {
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800605 sources = _common.sources
606
607 deps = []
608 foreach(dep, _deps) {
609 _base = get_label_info(dep, "label_no_toolchain")
610 deps += [ "$_base.go(" + get_label_info(dep, "toolchain") + ")" ]
611 }
612
613 forward_variables_from(_common, "*")
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700614 }
615
Alexei Frolov38cad0c2020-12-03 12:36:24 -0800616 _pw_python_proto_library("$target_name.python") {
Alexei Frolov38cad0c2020-12-03 12:36:24 -0800617 forward_variables_from(_common, "*")
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800618 forward_variables_from(invoker, [ "python_package" ])
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800619 module_as_package = _module_as_package
620
621 deps = []
622 foreach(dep, _deps) {
623 _base = get_label_info(dep, "label_no_toolchain")
624 deps += [ "$_base.python(" + get_label_info(dep, "toolchain") + ")" ]
625 }
626
627 if (module_as_package == "") {
628 _python_prefix = "$base_out_dir/python/$_prefix"
629 } else {
630 _python_prefix = "$base_out_dir/python/$module_as_package"
631 }
632
633 outputs = []
634 foreach(name, _source_names) {
635 outputs += [
636 "$_python_prefix/${name}_pb2.py",
637 "$_python_prefix/${name}_pb2.pyi",
638 ]
639 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800640 }
641
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700642 # All supported pw_protobuf generators.
643 _protobuf_generators = [
644 "pwpb",
645 "nanopb",
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700646 "nanopb_rpc",
Alexei Frolovc912ea72020-10-26 08:43:27 -0700647 "raw_rpc",
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700648 "go",
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800649 "python",
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700650 ]
651
Wyatt Hepler4cdc3e72021-03-24 08:41:08 -0700652 # If the label matches the directory name, alias the subtargets to the
653 # directory (e.g. //foo:nanopb is an alias for //foo:foo.nanopb).
654 if (get_label_info(":$target_name", "name") ==
655 get_path_info(get_label_info(":$target_name", "dir"), "name")) {
Wyatt Hepler2c6c0ba2021-04-07 09:50:23 -0700656 foreach(_generator, _protobuf_generators - [ "python" ]) {
657 group(_generator) {
Wyatt Hepler4cdc3e72021-03-24 08:41:08 -0700658 public_deps = [ ":${invoker.target_name}.$_generator" ]
659 }
660 }
Wyatt Hepler2c6c0ba2021-04-07 09:50:23 -0700661
662 pw_python_group("python") {
663 python_deps = [ ":${invoker.target_name}.python" ]
664 }
Wyatt Hepler4cdc3e72021-03-24 08:41:08 -0700665 }
666
Alexei Frolov942adf02019-12-11 17:07:28 -0800667 # If the user attempts to use the target directly instead of one of the
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700668 # generator targets, run a script which prints a nice error message.
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700669 pw_python_action(target_name) {
Alexei Frolov942adf02019-12-11 17:07:28 -0800670 script = string_join("/",
671 [
672 dir_pw_protobuf_compiler,
673 "py",
674 "pw_protobuf_compiler",
675 "proto_target_invalid.py",
676 ])
677 args = [
678 "--target",
679 target_name,
680 "--dir",
681 get_path_info(".", "abspath"),
682 "--root",
683 "//",
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700684 ] + _protobuf_generators
Alexei Frolov942adf02019-12-11 17:07:28 -0800685 stamp = true
686 }
687}