blob: c03f11a16149f0b5012ce991ac43c40892c160e5 [file] [log] [blame]
Alexei Frolov942adf02019-12-11 17:07:28 -08001# Copyright 2019 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7# https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15import("$dir_pw_build/python_script.gni")
16
17# Python script that invokes protoc.
18_gen_script_path =
19 "$dir_pw_protobuf_compiler/py/pw_protobuf_compiler/generate_protos.py"
20
Alexei Frolovf39cd8b2020-04-13 17:59:20 -070021# Generates pw_protobuf C++ code for proto files, creating a source_set of the
22# generated files. This is internal and should not be used outside of this file.
23# Use pw_proto_library instead.
Alexei Frolov942adf02019-12-11 17:07:28 -080024#
25# Args:
26# protos: List of input .proto files.
Alexei Frolovf39cd8b2020-04-13 17:59:20 -070027template("_pw_pwpb_proto_library") {
Alexei Frolov942adf02019-12-11 17:07:28 -080028 _proto_gen_dir = "$root_gen_dir/protos"
29 _outputs = process_file_template(
30 invoker.protos,
Alexei Frolovf39cd8b2020-04-13 17:59:20 -070031 "$_proto_gen_dir/{{source_root_relative_dir}}/{{source_name_part}}.pwpb.h")
Alexei Frolov942adf02019-12-11 17:07:28 -080032
33 _gen_target = "${target_name}_gen"
34 pw_python_script(_gen_target) {
35 script = _gen_script_path
36 args = [
37 "--language",
38 "cc",
39 "--module-path",
40 "//",
41 "--out-dir",
42 _proto_gen_dir,
43 ] + get_path_info(invoker.protos, "abspath")
44 inputs = invoker.protos
45 outputs = _outputs
Alexei Frolovbbf164c2019-12-16 12:51:59 -080046
47 if (defined(invoker.protoc_deps)) {
48 deps = invoker.protoc_deps
49 }
Alexei Frolov942adf02019-12-11 17:07:28 -080050 }
51
52 # For C++ proto files, the generated proto directory is added as an include
53 # path for the code. This requires using "all_dependent_configs" to force the
54 # include on any code that transitively depends on the generated protos.
55 _include_root = rebase_path(get_path_info(".", "abspath"), "//")
56 _include_config_target = "${target_name}_includes"
57 config(_include_config_target) {
58 include_dirs = [ "$_proto_gen_dir/$_include_root" ]
59 }
60
61 # Create a library with the generated source files.
62 # TODO(frolv): This currently only supports pw_protobuf, which is header-only.
63 # Figure out how to support .cc files.
64 source_set(target_name) {
65 all_dependent_configs = [ ":$_include_config_target" ]
66 deps = [ ":$_gen_target" ] + invoker.deps
67 sources = get_target_outputs(":$_gen_target")
Alexei Frolovf39cd8b2020-04-13 17:59:20 -070068 public = filter_include(sources, [ "*.pwpb.h" ])
69 }
70}
71
72# Generates nanopb code for proto files, creating a source_set of the generated
73# files. This is internal and should not be used outside of this file. Use
74# pw_proto_library instead.
75#
76# Args:
77# protos: List of input .proto files.
78template("_pw_nanopb_proto_library") {
79 assert(defined(dir_third_party_nanopb) && dir_third_party_nanopb != "",
80 "\$dir_third_party_nanopb must be set to compile nanopb protobufs")
81
82 _proto_gen_dir = "$root_gen_dir/protos"
83 _outputs = process_file_template(
84 invoker.protos,
85 [
86 "$_proto_gen_dir/{{source_root_relative_dir}}/{{source_name_part}}.pb.h",
87 "$_proto_gen_dir/{{source_root_relative_dir}}/{{source_name_part}}.pb.c",
88 ])
89
90 _nanopb_plugin = "$dir_third_party_nanopb/generator/protoc-gen-nanopb"
91 if (host_os == "win") {
92 _nanopb_plugin += ".bat"
93 }
94
95 # Create a target which runs protoc configured with the nanopb plugin to
96 # generate the C proto sources.
97 _gen_target = "${target_name}_gen"
98 pw_python_script(_gen_target) {
99 script = _gen_script_path
100 args = [
101 "--language",
102 "nanopb",
103 "--module-path",
104 "//",
105 "--out-dir",
106 _proto_gen_dir,
107 "--custom-plugin",
108 get_path_info(_nanopb_plugin, "abspath"),
109 ] + get_path_info(invoker.protos, "abspath")
110 inputs = invoker.protos
111 outputs = _outputs
112
113 if (defined(invoker.protoc_deps)) {
114 deps = invoker.protoc_deps
115 }
116 }
117
118 # For C++ proto files, the generated proto directory is added as an include
119 # path for the code. This requires using "all_dependent_configs" to force the
120 # include on any code that transitively depends on the generated protos.
121 _include_root = rebase_path(get_path_info(".", "abspath"), "//")
122 _include_config_target = "${target_name}_includes"
123 config(_include_config_target) {
124 include_dirs = [
125 "$_proto_gen_dir",
126 "$_proto_gen_dir/$_include_root",
127 ]
128 }
129
130 # Create a library with the generated source files.
131 source_set(target_name) {
132 all_dependent_configs = [ ":$_include_config_target" ]
133 deps = invoker.deps
134 public_deps = [
135 ":$_gen_target",
136 dir_third_party_nanopb,
137 ]
138 sources = get_target_outputs(":$_gen_target")
139 public = filter_include(sources, [ "*.pb.h" ])
Alexei Frolov942adf02019-12-11 17:07:28 -0800140 }
141}
142
Alexei Frolovdef14712019-12-23 13:03:32 -0800143# Generates Go code for proto files, listing the proto output directory in the
144# metadata variable GOPATH. Internal use only.
145#
146# Args:
147# protos: List of input .proto files.
148template("_pw_go_proto_library") {
149 _proto_gopath = "$root_gen_dir/go"
150 _proto_gen_dir = "$_proto_gopath/src"
151 _rebased_gopath = rebase_path(_proto_gopath)
152
153 pw_python_script(target_name) {
154 metadata = {
155 gopath = [ "GOPATH+=$_rebased_gopath" ]
Alexei Frolovc15a9882019-12-23 14:29:02 -0800156 external_deps = [
157 "github.com/golang/protobuf/proto",
158 "google.golang.org/grpc",
159 ]
Alexei Frolovdef14712019-12-23 13:03:32 -0800160 }
161 script = _gen_script_path
162 args = [
163 "--language",
164 "go",
165 "--module-path",
166 "//",
167 "--out-dir",
168 _proto_gen_dir,
169 ] + get_path_info(invoker.protos, "abspath")
170 inputs = invoker.protos
171 deps = invoker.deps
172 stamp = true
173 }
174}
175
Alexei Frolov942adf02019-12-11 17:07:28 -0800176# Generates protobuf code from .proto definitions for various languages.
177#
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700178# The generators to use are defined in the pw_protobuf_generators build
179# variable. Each listed generator creates a generated code target called
Alexei Frolov942adf02019-12-11 17:07:28 -0800180#
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700181# <target_name>_<generator>
Alexei Frolov942adf02019-12-11 17:07:28 -0800182#
183# For example, with the following definitions:
184#
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700185# pw_protobuf_generators = [ "pwpb", "py" ]
Alexei Frolov942adf02019-12-11 17:07:28 -0800186#
187# pw_proto_library("my_protos") {
188# sources = [ "foo.proto" ]
189# }
190#
191# Two build targets will be created for the declared "my_protos" target.
192#
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700193# "my_protos_pwpb" <-- C++ source_set containing generated proto code
194# "my_protos_py" <-- Python module containing generated proto code
Alexei Frolov942adf02019-12-11 17:07:28 -0800195#
196# Args:
197# sources: List of input .proto files.
198# deps: List of other pw_proto_library dependencies.
Alexei Frolov942adf02019-12-11 17:07:28 -0800199template("pw_proto_library") {
200 assert(defined(invoker.sources) && invoker.sources != [],
201 "pw_proto_codegen requires .proto source files")
202
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700203 foreach(_gen, pw_protobuf_generators) {
Alexei Frolov942adf02019-12-11 17:07:28 -0800204 if (defined(invoker.deps)) {
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700205 _gen_deps = process_file_template(invoker.deps, "{{source}}_${_gen}")
Alexei Frolov942adf02019-12-11 17:07:28 -0800206 } else {
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700207 _gen_deps = []
Alexei Frolov942adf02019-12-11 17:07:28 -0800208 }
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700209 _lang_target = "${target_name}_${_gen}"
Alexei Frolov942adf02019-12-11 17:07:28 -0800210
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700211 if (_gen == "pwpb") {
212 _pw_pwpb_proto_library(_lang_target) {
Alexei Frolov942adf02019-12-11 17:07:28 -0800213 protos = invoker.sources
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700214 deps = _gen_deps
Alexei Frolovbbf164c2019-12-16 12:51:59 -0800215
216 # List the pw_protobuf plugin's files as a dependency to recompile
217 # generated code if they are modified.
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700218 protoc_deps = [ "$dir_pw_protobuf:codegen_protoc_plugin" ]
Alexei Frolov942adf02019-12-11 17:07:28 -0800219 }
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700220 } else if (_gen == "nanopb") {
221 _pw_nanopb_proto_library(_lang_target) {
222 protos = invoker.sources
223 deps = _gen_deps
224 }
225 } else if (_gen == "go") {
Alexei Frolovdef14712019-12-23 13:03:32 -0800226 _pw_go_proto_library(_lang_target) {
227 protos = invoker.sources
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700228 deps = _gen_deps
Alexei Frolovdef14712019-12-23 13:03:32 -0800229 }
Alexei Frolov942adf02019-12-11 17:07:28 -0800230 } else {
231 assert(false,
232 string_join(
233 " ",
234 [
235 "pw_proto_codegen doesn't know how to generate code for",
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700236 "generator '$_gen'. Please add support if you require it.",
Alexei Frolov942adf02019-12-11 17:07:28 -0800237 ]))
238 }
239 }
240
241 # If the user attempts to use the target directly instead of one of the
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700242 # generator targets, run a script which prints a nice error message.
Alexei Frolov942adf02019-12-11 17:07:28 -0800243 pw_python_script(target_name) {
244 script = string_join("/",
245 [
246 dir_pw_protobuf_compiler,
247 "py",
248 "pw_protobuf_compiler",
249 "proto_target_invalid.py",
250 ])
251 args = [
252 "--target",
253 target_name,
254 "--dir",
255 get_path_info(".", "abspath"),
256 "--root",
257 "//",
Alexei Frolovf39cd8b2020-04-13 17:59:20 -0700258 ] + pw_protobuf_generators
Alexei Frolov942adf02019-12-11 17:07:28 -0800259 stamp = true
260 }
261}