blob: 33fe5d79216cb601563da05599d378049badb362 [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
101 } else {
102 metadata = {
103 protoc_outputs = rebase_path(outputs)
104 root = [ rebase_path(_out_dir) ]
105 }
106 }
107 }
108 } else {
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700109 # protoc is only ever invoked from pw_protobuf_compiler_TOOLCHAIN.
Wyatt Heplera0377642021-03-09 08:37:10 -0800110 not_needed([ "target_name" ])
111 not_needed(invoker, "*")
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800112 }
113}
114
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700115# Generates pw_protobuf C++ code for proto files, creating a source_set of the
116# generated files. This is internal and should not be used outside of this file.
117# Use pw_proto_library instead.
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700118template("_pw_pwpb_proto_library") {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800119 _pw_invoke_protoc(target_name) {
120 forward_variables_from(invoker, "*", _forwarded_vars)
121 language = "pwpb"
122 plugin = "$dir_pw_protobuf/py/pw_protobuf/plugin.py"
Wyatt Hepler438caa02021-01-15 17:13:11 -0800123 python_deps = [ "$dir_pw_protobuf/py" ]
Alexei Frolov942adf02019-12-11 17:07:28 -0800124 }
125
126 # Create a library with the generated source files.
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800127 config("$target_name._include_path") {
128 include_dirs = [ "${invoker.base_out_dir}/pwpb" ]
129 visibility = [ ":*" ]
130 }
131
Alexei Frolovedd2f142020-06-09 19:11:27 -0700132 pw_source_set(target_name) {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800133 forward_variables_from(invoker, _forwarded_vars)
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800134 public_configs = [ ":$target_name._include_path" ]
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700135 deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800136 public_deps = [ dir_pw_protobuf ] + invoker.deps
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800137 sources = invoker.outputs
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700138 public = filter_include(sources, [ "*.pwpb.h" ])
139 }
140}
141
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700142# Generates nanopb RPC code for proto files, creating a source_set of the
143# generated files. This is internal and should not be used outside of this file.
144# Use pw_proto_library instead.
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700145template("_pw_nanopb_rpc_proto_library") {
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700146 # Create a target which runs protoc configured with the nanopb_rpc plugin to
147 # generate the C++ proto RPC headers.
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800148 _pw_invoke_protoc(target_name) {
149 forward_variables_from(invoker, "*", _forwarded_vars)
150 language = "nanopb_rpc"
151 plugin = "$dir_pw_rpc/py/pw_rpc/plugin_nanopb.py"
Wyatt Hepler438caa02021-01-15 17:13:11 -0800152 python_deps = [ "$dir_pw_rpc/py" ]
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700153 }
154
155 # Create a library with the generated source files.
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800156 config("$target_name._include_path") {
157 include_dirs = [ "${invoker.base_out_dir}/nanopb_rpc" ]
158 visibility = [ ":*" ]
159 }
160
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700161 pw_source_set(target_name) {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800162 forward_variables_from(invoker, _forwarded_vars)
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800163 public_configs = [ ":$target_name._include_path" ]
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700164 deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700165 public_deps = [
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800166 ":${invoker.base_target}.nanopb",
Wyatt Heplercbd09c22020-09-15 11:17:24 -0700167 "$dir_pw_rpc:server",
Alexei Frolova4d71502020-10-14 12:43:14 -0700168 "$dir_pw_rpc/nanopb:method_union",
Alexei Frolovdd6fa5c2020-08-11 10:04:01 -0700169 "$dir_pw_third_party/nanopb",
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800170 ] + invoker.deps
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800171 public = invoker.outputs
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700172 }
173}
174
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700175# Generates nanopb code for proto files, creating a source_set of the generated
176# files. This is internal and should not be used outside of this file. Use
177# pw_proto_library instead.
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700178template("_pw_nanopb_proto_library") {
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800179 # When compiling with the Nanopb plugin, the nanopb.proto file is already
180 # compiled internally, so skip recompiling it with protoc.
181 if (rebase_path(invoker.sources, invoker.compile_dir) == [ "nanopb.proto" ]) {
182 group("$target_name._gen") {
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700183 deps = [
184 ":${invoker.base_target}._sources($pw_protobuf_compiler_TOOLCHAIN)",
185 ]
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800186 }
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700187
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800188 group("$target_name") {
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700189 deps = invoker.deps +
190 [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800191 }
192 } else {
193 # Create a target which runs protoc configured with the nanopb plugin to
194 # generate the C proto sources.
195 _pw_invoke_protoc(target_name) {
196 forward_variables_from(invoker, "*", _forwarded_vars)
197 language = "nanopb"
198 plugin = "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb"
Wyatt Heplera3ca62a2021-05-04 16:21:43 -0700199 other_deps = [ "$dir_pw_third_party/nanopb:generate_nanopb_proto.action" ]
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800200 }
201
202 # Create a library with the generated source files.
203 config("$target_name._include_path") {
204 include_dirs = [ "${invoker.base_out_dir}/nanopb" ]
205 visibility = [ ":*" ]
206 }
207
208 pw_source_set(target_name) {
209 forward_variables_from(invoker, _forwarded_vars)
210 public_configs = [ ":$target_name._include_path" ]
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700211 deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800212 public_deps = [ "$dir_pw_third_party/nanopb" ] + invoker.deps
213 sources = invoker.outputs
214 public = filter_include(sources, [ "*.pb.h" ])
Wyatt Heplerb93749b2021-05-11 09:38:22 -0700215 check_includes = false
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800216 }
Alexei Frolov942adf02019-12-11 17:07:28 -0800217 }
218}
219
Alexei Frolovc912ea72020-10-26 08:43:27 -0700220# Generates raw RPC code for proto files, creating a source_set of the generated
221# files. This is internal and should not be used outside of this file. Use
222# pw_proto_library instead.
Alexei Frolovc912ea72020-10-26 08:43:27 -0700223template("_pw_raw_rpc_proto_library") {
Alexei Frolovc912ea72020-10-26 08:43:27 -0700224 # Create a target which runs protoc configured with the nanopb_rpc plugin to
225 # generate the C++ proto RPC headers.
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800226 _pw_invoke_protoc(target_name) {
227 forward_variables_from(invoker, "*", _forwarded_vars)
228 language = "raw_rpc"
229 plugin = "$dir_pw_rpc/py/pw_rpc/plugin_raw.py"
Wyatt Hepler438caa02021-01-15 17:13:11 -0800230 python_deps = [ "$dir_pw_rpc/py" ]
Alexei Frolovc912ea72020-10-26 08:43:27 -0700231 }
232
233 # Create a library with the generated source files.
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800234 config("$target_name._include_path") {
235 include_dirs = [ "${invoker.base_out_dir}/raw_rpc" ]
236 visibility = [ ":*" ]
237 }
238
Alexei Frolovc912ea72020-10-26 08:43:27 -0700239 pw_source_set(target_name) {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800240 forward_variables_from(invoker, _forwarded_vars)
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800241 public_configs = [ ":$target_name._include_path" ]
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700242 deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
Alexei Frolovc912ea72020-10-26 08:43:27 -0700243 public_deps = [
244 "$dir_pw_rpc:server",
245 "$dir_pw_rpc/raw:method_union",
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800246 ] + invoker.deps
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800247 public = invoker.outputs
Wyatt Heplerb93749b2021-05-11 09:38:22 -0700248 check_includes = false
Alexei Frolovc912ea72020-10-26 08:43:27 -0700249 }
250}
251
Alexei Frolovdef14712019-12-23 13:03:32 -0800252# Generates Go code for proto files, listing the proto output directory in the
253# metadata variable GOPATH. Internal use only.
Alexei Frolovdef14712019-12-23 13:03:32 -0800254template("_pw_go_proto_library") {
255 _proto_gopath = "$root_gen_dir/go"
Alexei Frolovdef14712019-12-23 13:03:32 -0800256
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800257 _pw_invoke_protoc(target_name) {
258 forward_variables_from(invoker, "*")
259 language = "go"
Alexei Frolovdef14712019-12-23 13:03:32 -0800260 metadata = {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800261 gopath = [ "GOPATH+=" + rebase_path(_proto_gopath) ]
Alexei Frolovc15a9882019-12-23 14:29:02 -0800262 external_deps = [
263 "github.com/golang/protobuf/proto",
264 "google.golang.org/grpc",
265 ]
Alexei Frolovdef14712019-12-23 13:03:32 -0800266 }
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800267
268 # Override the default "$base_out_dir/$language" output path.
269 out_dir = "$_proto_gopath/src"
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800270 }
271
272 group(target_name) {
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700273 deps =
274 invoker.deps + [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
Alexei Frolovdef14712019-12-23 13:03:32 -0800275 }
276}
277
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800278# Generates Python code for proto files, creating a pw_python_package containing
279# the generated files. This is internal and should not be used outside of this
280# file. Use pw_proto_library instead.
281template("_pw_python_proto_library") {
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800282 _pw_invoke_protoc(target_name) {
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800283 forward_variables_from(invoker, "*", _forwarded_vars + [ "python_package" ])
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800284 language = "python"
Wyatt Hepler4b921c12021-03-05 15:33:29 -0800285 python_deps = [ "$dir_pw_protobuf_compiler:protobuf_requirements" ]
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800286 }
287
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800288 if (defined(invoker.python_package) && invoker.python_package != "") {
289 # If nested in a Python package, write the package's name to a file so
290 # pw_python_package can check that the dependencies are correct.
291 write_file("${invoker.base_out_dir}/python_package.txt",
292 get_label_info(invoker.python_package, "label_no_toolchain"))
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800293
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800294 # If anyone attempts to depend on this Python package, print an error.
295 pw_error(target_name) {
296 _pkg = get_label_info(invoker.python_package, "label_no_toolchain")
297 message_lines = [
298 "This proto Python package is embedded in the $_pkg Python package.",
299 "It cannot be used directly; instead, depend on $_pkg.",
300 ]
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800301 }
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800302 foreach(subtarget, pw_python_package_subtargets) {
303 group("$target_name.$subtarget") {
304 deps = [ ":${invoker.target_name}" ]
305 }
306 }
307 } else {
308 write_file("${invoker.base_out_dir}/python_package.txt", "")
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800309
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800310 # Create a Python package with the generated source files.
311 pw_python_package(target_name) {
312 forward_variables_from(invoker, _forwarded_vars)
313 generate_setup = {
314 name = invoker._package_dir
315 version = "0.0.1" # TODO(hepler): Need to be able to set this verison.
316 }
317 sources = invoker.outputs
318 strip_prefix = "${invoker.base_out_dir}/python"
319 python_deps = invoker.deps
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700320 other_deps = [ ":$target_name._gen($pw_protobuf_compiler_TOOLCHAIN)" ]
Wyatt Heplerc2ce5242021-03-17 08:42:14 -0700321 static_analysis = []
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800322
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800323 _pw_module_as_package = invoker.module_as_package != ""
324 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800325 }
326}
327
Alexei Frolov942adf02019-12-11 17:07:28 -0800328# Generates protobuf code from .proto definitions for various languages.
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700329# For each supported generator, creates a sub-target named:
Alexei Frolov942adf02019-12-11 17:07:28 -0800330#
Alexei Frolov8e30d462020-10-22 13:54:36 -0700331# <target_name>.<generator>
Alexei Frolov942adf02019-12-11 17:07:28 -0800332#
Wyatt Hepler4cdc3e72021-03-24 08:41:08 -0700333# GN permits using abbreviated labels when the target name matches the directory
334# name (e.g. //foo for //foo:foo). For consistency with this, the sub-targets
335# for each generator are aliased to the directory when the target name is the
336# same. For example, these two labels are equivalent:
337#
338# //path/to/my_protos:my_protos.pwpb
339# //path/to/my_protos:pwpb
340#
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800341# pw_protobuf_library targets generate Python packages. As such, they must have
342# globally unique package names. The first directory of the prefix or the first
343# common directory of the sources is used as the Python package.
344#
Alexei Frolov942adf02019-12-11 17:07:28 -0800345# Args:
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800346# sources: List of input .proto files.
347# deps: List of other pw_proto_library dependencies.
348# inputs: Other files on which the protos depend (e.g. nanopb .options files).
349# prefix: A prefix to add to the source protos prior to compilation. For
350# example, a source called "foo.proto" with prefix = "nested" will be
351# compiled with protoc as "nested/foo.proto".
352# strip_prefix: Remove this prefix from the source protos. All source and
353# input files must be nested under this path.
Wyatt Hepler4cdc3e72021-03-24 08:41:08 -0700354# python_package: Label of Python package to which to add the proto modules.
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700355#
Alexei Frolov942adf02019-12-11 17:07:28 -0800356template("pw_proto_library") {
357 assert(defined(invoker.sources) && invoker.sources != [],
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700358 "pw_proto_library requires .proto source files")
Alexei Frolov942adf02019-12-11 17:07:28 -0800359
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800360 if (defined(invoker.python_module_as_package)) {
361 _module_as_package = invoker.python_module_as_package
362
363 _must_be_one_source = invoker.sources
364 assert([ _must_be_one_source[0] ] == _must_be_one_source,
365 "'python_module_as_package' requires exactly one source file")
366 assert(_module_as_package != "",
367 "'python_module_as_package' cannot be be empty")
368 assert(string_split(_module_as_package, "/") == [ _module_as_package ],
369 "'python_module_as_package' cannot contain slashes")
370 assert(!defined(invoker.prefix),
371 "'prefix' cannot be provided with 'python_module_as_package'")
372 } else {
373 _module_as_package = ""
374 }
375
376 if (defined(invoker.strip_prefix)) {
377 _source_root = get_path_info(invoker.strip_prefix, "abspath")
378 } else {
379 _source_root = get_path_info(".", "abspath")
380 }
381
382 if (defined(invoker.prefix)) {
383 _prefix = invoker.prefix
384 } else {
385 _prefix = ""
386 }
387
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800388 _common = {
389 base_target = target_name
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800390
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800391 # This is the output directory for all files related to this proto library.
392 # Sources are mirrored to "$base_out_dir/sources" and protoc puts outputs in
393 # "$base_out_dir/$language" by default.
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800394 base_out_dir =
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700395 get_label_info(":$target_name($pw_protobuf_compiler_TOOLCHAIN)",
396 "target_gen_dir") + "/$target_name.proto_library"
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800397
398 compile_dir = "$base_out_dir/sources"
399
400 # Refer to the source files as the are mirrored to the output directory.
401 sources = []
402 foreach(file, rebase_path(invoker.sources, _source_root)) {
403 sources += [ "$compile_dir/$_prefix/$file" ]
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800404 }
405 }
406
Wyatt Hepler91741472021-02-03 08:45:10 -0800407 _package_dir = ""
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800408 _source_names = []
Wyatt Hepler91741472021-02-03 08:45:10 -0800409
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800410 # Determine the Python package name to use for these protos. If there is no
411 # prefix, the first directory the sources are nested under is used.
412 foreach(source, rebase_path(invoker.sources, _source_root)) {
Wyatt Hepler91741472021-02-03 08:45:10 -0800413 _path_components = []
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800414 _path_components = string_split(source, "/")
Wyatt Hepler91741472021-02-03 08:45:10 -0800415
416 if (_package_dir == "") {
417 _package_dir = _path_components[0]
418 } else {
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800419 assert(_prefix != "" || _path_components[0] == _package_dir,
420 "Unless 'prefix' is supplied, all .proto sources in a " +
421 "pw_proto_library must be in the same directory tree")
Wyatt Hepler91741472021-02-03 08:45:10 -0800422 }
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800423
424 _source_names +=
425 [ get_path_info(source, "dir") + "/" + get_path_info(source, "name") ]
Wyatt Hepler91741472021-02-03 08:45:10 -0800426 }
427
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800428 # If the 'prefix' was supplied, use that for the package directory.
429 if (_prefix != "") {
430 _prefix_path_components = string_split(_prefix, "/")
431 _package_dir = _prefix_path_components[0]
432 }
433
434 assert(_package_dir != "" && _package_dir != "." && _package_dir != "..",
435 "Either a 'prefix' must be specified or all sources must be nested " +
436 "under a common directory")
437
438 # Define an action that is never executed to prevent duplicate proto packages
439 # from being declared. The target name and the output file include only the
440 # package directory, so different targets that use the same proto package name
441 # will conflict.
442 action("pw_proto_library.$_package_dir") {
443 script = "$dir_pw_build/py/pw_build/nop.py"
Wyatt Hepler91741472021-02-03 08:45:10 -0800444 visibility = []
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800445
446 # Place an error message in the output path (which is never created). If the
447 # package name conflicts occur in different BUILD.gn files, this results in
448 # an otherwise cryptic Ninja error, rather than a GN error.
449 outputs = [ "$root_out_dir/ " +
450 "ERROR - Multiple pw_proto_library targets create the " +
451 "'$_package_dir' package. Change the package name by setting " +
452 "the \"prefix\" arg or move the protos to a different " +
453 "directory, then re-run gn gen." ]
Wyatt Hepler91741472021-02-03 08:45:10 -0800454 }
455
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800456 if (defined(invoker.deps)) {
457 _deps = invoker.deps
458 } else {
459 _deps = []
460 }
461
Alexei Frolove19ebb82020-05-14 17:21:20 -0700462 # For each proto target, create a file which collects the base directories of
463 # all of its dependencies to list as include paths to protoc.
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800464 generated_file("$target_name._includes") {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800465 # Collect metadata from the include path files of each dependency.
Wyatt Hepler4cdc3e72021-03-24 08:41:08 -0700466
467 deps = []
468 foreach(dep, _deps) {
469 _base = get_label_info(dep, "label_no_toolchain")
470 deps += [ "$_base._includes(" + get_label_info(dep, "toolchain") + ")" ]
471 }
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800472
Alexei Frolove19ebb82020-05-14 17:21:20 -0700473 data_keys = [ "protoc_includes" ]
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800474 outputs = [ "${_common.base_out_dir}/includes.txt" ]
Alexei Frolove19ebb82020-05-14 17:21:20 -0700475
476 # Indicate this library's base directory for its dependents.
477 metadata = {
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800478 protoc_includes = [ rebase_path(_common.compile_dir) ]
Alexei Frolove19ebb82020-05-14 17:21:20 -0700479 }
480 }
481
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800482 # Mirror the proto sources to the output directory with the prefix added.
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700483 if (current_toolchain == pw_protobuf_compiler_TOOLCHAIN) {
484 pw_mirror_tree("$target_name._sources") {
485 source_root = _source_root
486 sources = invoker.sources
Alexei Frolov05d8ef22020-06-08 10:32:29 -0700487
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700488 if (defined(invoker.inputs)) {
489 sources += invoker.inputs
490 }
491
492 directory = "${_common.compile_dir}/$_prefix"
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800493 }
Wyatt Heplerf6f74f42021-04-13 19:26:20 -0700494 } else {
495 not_needed(invoker, [ "inputs" ])
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700496 }
497
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700498 # Enumerate all of the protobuf generator targets.
Alexei Frolovc4b62ec2020-07-13 08:35:10 -0700499
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800500 _pw_pwpb_proto_library("$target_name.pwpb") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700501 forward_variables_from(invoker, _forwarded_vars)
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800502 forward_variables_from(_common, "*")
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800503
504 deps = []
505 foreach(dep, _deps) {
506 _base = get_label_info(dep, "label_no_toolchain")
507 deps += [ "$_base.pwpb(" + get_label_info(dep, "toolchain") + ")" ]
508 }
509
510 outputs = []
511 foreach(name, _source_names) {
512 outputs += [ "$base_out_dir/pwpb/$_prefix/${name}.pwpb.h" ]
513 }
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700514 }
515
516 if (dir_pw_third_party_nanopb != "") {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800517 _pw_nanopb_rpc_proto_library("$target_name.nanopb_rpc") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700518 forward_variables_from(invoker, _forwarded_vars)
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800519 forward_variables_from(_common, "*")
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800520
521 deps = []
522 foreach(dep, _deps) {
523 _lbl = get_label_info(dep, "label_no_toolchain")
524 deps += [ "$_lbl.nanopb_rpc(" + get_label_info(dep, "toolchain") + ")" ]
525 }
526
527 outputs = []
528 foreach(name, _source_names) {
529 outputs += [ "$base_out_dir/nanopb_rpc/$_prefix/${name}.rpc.pb.h" ]
530 }
Alexei Frolovc4b62ec2020-07-13 08:35:10 -0700531 }
532
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800533 _pw_nanopb_proto_library("$target_name.nanopb") {
534 forward_variables_from(invoker, _forwarded_vars)
535 forward_variables_from(_common, "*")
536
537 deps = []
538 foreach(dep, _deps) {
539 _base = get_label_info(dep, "label_no_toolchain")
540 deps += [ "$_base.nanopb(" + get_label_info(dep, "toolchain") + ")" ]
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800541 }
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800542
543 outputs = []
544 foreach(name, _source_names) {
545 outputs += [
546 "$base_out_dir/nanopb/$_prefix/${name}.pb.h",
547 "$base_out_dir/nanopb/$_prefix/${name}.pb.c",
548 ]
Wyatt Heplerd517afc2021-02-03 19:40:08 -0800549 }
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700550 }
551 } else {
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800552 pw_error("$target_name.nanopb_rpc") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700553 message =
554 "\$dir_pw_third_party_nanopb must be set to generate nanopb RPC code."
Alexei Frolov8185c822020-06-12 10:45:04 -0700555 }
Alexei Frolov942adf02019-12-11 17:07:28 -0800556
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800557 pw_error("$target_name.nanopb") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700558 message =
559 "\$dir_pw_third_party_nanopb must be set to compile nanopb protobufs."
Alexei Frolov942adf02019-12-11 17:07:28 -0800560 }
561 }
562
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800563 _pw_raw_rpc_proto_library("$target_name.raw_rpc") {
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700564 forward_variables_from(invoker, _forwarded_vars)
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800565 forward_variables_from(_common, "*")
566
567 deps = []
568 foreach(dep, _deps) {
569 _base = get_label_info(dep, "label_no_toolchain")
570 deps += [ "$_base.raw_rpc(" + get_label_info(dep, "toolchain") + ")" ]
571 }
572
573 outputs = []
574 foreach(name, _source_names) {
575 outputs += [ "$base_out_dir/raw_rpc/$_prefix/${name}.raw_rpc.pb.h" ]
576 }
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700577 }
578
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800579 _pw_go_proto_library("$target_name.go") {
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800580 sources = _common.sources
581
582 deps = []
583 foreach(dep, _deps) {
584 _base = get_label_info(dep, "label_no_toolchain")
585 deps += [ "$_base.go(" + get_label_info(dep, "toolchain") + ")" ]
586 }
587
588 forward_variables_from(_common, "*")
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700589 }
590
Alexei Frolov38cad0c2020-12-03 12:36:24 -0800591 _pw_python_proto_library("$target_name.python") {
Alexei Frolov38cad0c2020-12-03 12:36:24 -0800592 forward_variables_from(_common, "*")
Wyatt Heplerdcfcecf2021-03-01 08:36:19 -0800593 forward_variables_from(invoker, [ "python_package" ])
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800594 module_as_package = _module_as_package
595
596 deps = []
597 foreach(dep, _deps) {
598 _base = get_label_info(dep, "label_no_toolchain")
599 deps += [ "$_base.python(" + get_label_info(dep, "toolchain") + ")" ]
600 }
601
602 if (module_as_package == "") {
603 _python_prefix = "$base_out_dir/python/$_prefix"
604 } else {
605 _python_prefix = "$base_out_dir/python/$module_as_package"
606 }
607
608 outputs = []
609 foreach(name, _source_names) {
610 outputs += [
611 "$_python_prefix/${name}_pb2.py",
612 "$_python_prefix/${name}_pb2.pyi",
613 ]
614 }
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800615 }
616
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700617 # All supported pw_protobuf generators.
618 _protobuf_generators = [
619 "pwpb",
620 "nanopb",
Alexei Frolov79b7cb02020-07-06 13:51:43 -0700621 "nanopb_rpc",
Alexei Frolovc912ea72020-10-26 08:43:27 -0700622 "raw_rpc",
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700623 "go",
Alexei Frolova4c0aee2020-12-01 13:48:48 -0800624 "python",
Wyatt Heplerb4b73a62020-05-27 15:17:27 -0700625 ]
626
Wyatt Hepler4cdc3e72021-03-24 08:41:08 -0700627 # If the label matches the directory name, alias the subtargets to the
628 # directory (e.g. //foo:nanopb is an alias for //foo:foo.nanopb).
629 if (get_label_info(":$target_name", "name") ==
630 get_path_info(get_label_info(":$target_name", "dir"), "name")) {
Wyatt Hepler2c6c0ba2021-04-07 09:50:23 -0700631 foreach(_generator, _protobuf_generators - [ "python" ]) {
632 group(_generator) {
Wyatt Hepler4cdc3e72021-03-24 08:41:08 -0700633 public_deps = [ ":${invoker.target_name}.$_generator" ]
634 }
635 }
Wyatt Hepler2c6c0ba2021-04-07 09:50:23 -0700636
637 pw_python_group("python") {
638 python_deps = [ ":${invoker.target_name}.python" ]
639 }
Wyatt Hepler4cdc3e72021-03-24 08:41:08 -0700640 }
641
Alexei Frolov942adf02019-12-11 17:07:28 -0800642 # If the user attempts to use the target directly instead of one of the
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700643 # generator targets, run a script which prints a nice error message.
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700644 pw_python_action(target_name) {
Alexei Frolov942adf02019-12-11 17:07:28 -0800645 script = string_join("/",
646 [
647 dir_pw_protobuf_compiler,
648 "py",
649 "pw_protobuf_compiler",
650 "proto_target_invalid.py",
651 ])
652 args = [
653 "--target",
654 target_name,
655 "--dir",
656 get_path_info(".", "abspath"),
657 "--root",
658 "//",
Alexei Frolovb499d3f2020-10-28 13:00:08 -0700659 ] + _protobuf_generators
Alexei Frolov942adf02019-12-11 17:07:28 -0800660 stamp = true
661 }
662}