blob: 59637ee2052636349e248f81ee547de89a955a9c [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
21# Generates C++ code for proto files, creating a source_set of the generated
22# files. This is internal and should not be used outside of this file. Use
23# pw_proto_library instead.
24#
25# Args:
26# protos: List of input .proto files.
27template("_pw_cc_proto_library") {
28 _proto_gen_dir = "$root_gen_dir/protos"
29 _outputs = process_file_template(
30 invoker.protos,
Alexei Frolovdef14712019-12-23 13:03:32 -080031 "$_proto_gen_dir/{{source_root_relative_dir}}/{{source_name_part}}.pb.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")
68 }
69}
70
Alexei Frolovdef14712019-12-23 13:03:32 -080071# Generates Go code for proto files, listing the proto output directory in the
72# metadata variable GOPATH. Internal use only.
73#
74# Args:
75# protos: List of input .proto files.
76template("_pw_go_proto_library") {
77 _proto_gopath = "$root_gen_dir/go"
78 _proto_gen_dir = "$_proto_gopath/src"
79 _rebased_gopath = rebase_path(_proto_gopath)
80
81 pw_python_script(target_name) {
82 metadata = {
83 gopath = [ "GOPATH+=$_rebased_gopath" ]
Alexei Frolovc15a9882019-12-23 14:29:02 -080084 external_deps = [
85 "github.com/golang/protobuf/proto",
86 "google.golang.org/grpc",
87 ]
Alexei Frolovdef14712019-12-23 13:03:32 -080088 }
89 script = _gen_script_path
90 args = [
91 "--language",
92 "go",
93 "--module-path",
94 "//",
95 "--out-dir",
96 _proto_gen_dir,
97 ] + get_path_info(invoker.protos, "abspath")
98 inputs = invoker.protos
99 deps = invoker.deps
100 stamp = true
101 }
102}
103
Alexei Frolov942adf02019-12-11 17:07:28 -0800104# Generates protobuf code from .proto definitions for various languages.
105#
106# The languages to generate are defined in the pw_protobuf_langs build variable.
107# Each listed language creates a generated code target called
108#
109# <target_name>_<language>
110#
111# For example, with the following definitions:
112#
113# pw_protobuf_langs = [ "cc", "py" ]
114#
115# pw_proto_library("my_protos") {
116# sources = [ "foo.proto" ]
117# }
118#
119# Two build targets will be created for the declared "my_protos" target.
120#
121# "my_protos_cc" <-- C++ source_set containing generated proto code
122# "my_protos_py" <-- Python module containing generated proto code
123#
124# Args:
125# sources: List of input .proto files.
126# deps: List of other pw_proto_library dependencies.
127#
128# TODO(frolv): Provide a way to set the protoc plugin for different languages.
129template("pw_proto_library") {
130 assert(defined(invoker.sources) && invoker.sources != [],
131 "pw_proto_codegen requires .proto source files")
132
133 foreach(lang, pw_protobuf_langs) {
134 if (defined(invoker.deps)) {
135 _lang_deps = process_file_template(invoker.deps, "{{source}}_${lang}")
136 } else {
137 _lang_deps = []
138 }
139 _lang_target = "${target_name}_${lang}"
140
141 if (lang == "cc") {
142 _pw_cc_proto_library(_lang_target) {
143 protos = invoker.sources
144 deps = _lang_deps
Alexei Frolovbbf164c2019-12-16 12:51:59 -0800145
146 # List the pw_protobuf plugin's files as a dependency to recompile
147 # generated code if they are modified.
148 #
149 # TODO(frolv): This check is currently true as pw_protobuf is the
150 # only supported plugin. It should be updated to support other proto
151 # libraries such as nanopb.
152 if (true) {
153 protoc_deps = [ "$dir_pw_protobuf:codegen_protoc_plugin" ]
154 }
Alexei Frolov942adf02019-12-11 17:07:28 -0800155 }
Alexei Frolovdef14712019-12-23 13:03:32 -0800156 } else if (lang == "go") {
157 _pw_go_proto_library(_lang_target) {
158 protos = invoker.sources
159 deps = _lang_deps
160 }
Alexei Frolov942adf02019-12-11 17:07:28 -0800161 } else {
162 assert(false,
163 string_join(
164 " ",
165 [
166 "pw_proto_codegen doesn't know how to generate code for",
167 "language '$lang'. Please add support if you require it.",
168 ]))
169 }
170 }
171
172 # If the user attempts to use the target directly instead of one of the
173 # language targets, run a script which prints a nice error message.
174 pw_python_script(target_name) {
175 script = string_join("/",
176 [
177 dir_pw_protobuf_compiler,
178 "py",
179 "pw_protobuf_compiler",
180 "proto_target_invalid.py",
181 ])
182 args = [
183 "--target",
184 target_name,
185 "--dir",
186 get_path_info(".", "abspath"),
187 "--root",
188 "//",
189 ] + pw_protobuf_langs
190 stamp = true
191 }
192}