blob: c659a20063b5da88894e1ffced2fee774b67aca6 [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" ]
84 }
85 script = _gen_script_path
86 args = [
87 "--language",
88 "go",
89 "--module-path",
90 "//",
91 "--out-dir",
92 _proto_gen_dir,
93 ] + get_path_info(invoker.protos, "abspath")
94 inputs = invoker.protos
95 deps = invoker.deps
96 stamp = true
97 }
98}
99
Alexei Frolov942adf02019-12-11 17:07:28 -0800100# Generates protobuf code from .proto definitions for various languages.
101#
102# The languages to generate are defined in the pw_protobuf_langs build variable.
103# Each listed language creates a generated code target called
104#
105# <target_name>_<language>
106#
107# For example, with the following definitions:
108#
109# pw_protobuf_langs = [ "cc", "py" ]
110#
111# pw_proto_library("my_protos") {
112# sources = [ "foo.proto" ]
113# }
114#
115# Two build targets will be created for the declared "my_protos" target.
116#
117# "my_protos_cc" <-- C++ source_set containing generated proto code
118# "my_protos_py" <-- Python module containing generated proto code
119#
120# Args:
121# sources: List of input .proto files.
122# deps: List of other pw_proto_library dependencies.
123#
124# TODO(frolv): Provide a way to set the protoc plugin for different languages.
125template("pw_proto_library") {
126 assert(defined(invoker.sources) && invoker.sources != [],
127 "pw_proto_codegen requires .proto source files")
128
129 foreach(lang, pw_protobuf_langs) {
130 if (defined(invoker.deps)) {
131 _lang_deps = process_file_template(invoker.deps, "{{source}}_${lang}")
132 } else {
133 _lang_deps = []
134 }
135 _lang_target = "${target_name}_${lang}"
136
137 if (lang == "cc") {
138 _pw_cc_proto_library(_lang_target) {
139 protos = invoker.sources
140 deps = _lang_deps
Alexei Frolovbbf164c2019-12-16 12:51:59 -0800141
142 # List the pw_protobuf plugin's files as a dependency to recompile
143 # generated code if they are modified.
144 #
145 # TODO(frolv): This check is currently true as pw_protobuf is the
146 # only supported plugin. It should be updated to support other proto
147 # libraries such as nanopb.
148 if (true) {
149 protoc_deps = [ "$dir_pw_protobuf:codegen_protoc_plugin" ]
150 }
Alexei Frolov942adf02019-12-11 17:07:28 -0800151 }
Alexei Frolovdef14712019-12-23 13:03:32 -0800152 } else if (lang == "go") {
153 _pw_go_proto_library(_lang_target) {
154 protos = invoker.sources
155 deps = _lang_deps
156 }
Alexei Frolov942adf02019-12-11 17:07:28 -0800157 } else {
158 assert(false,
159 string_join(
160 " ",
161 [
162 "pw_proto_codegen doesn't know how to generate code for",
163 "language '$lang'. Please add support if you require it.",
164 ]))
165 }
166 }
167
168 # If the user attempts to use the target directly instead of one of the
169 # language targets, run a script which prints a nice error message.
170 pw_python_script(target_name) {
171 script = string_join("/",
172 [
173 dir_pw_protobuf_compiler,
174 "py",
175 "pw_protobuf_compiler",
176 "proto_target_invalid.py",
177 ])
178 args = [
179 "--target",
180 target_name,
181 "--dir",
182 get_path_info(".", "abspath"),
183 "--root",
184 "//",
185 ] + pw_protobuf_langs
186 stamp = true
187 }
188}