blob: d20679a218bb370c5f8874f7303f9f6496973265 [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,
31 "$root_gen_dir/protos/{{source_root_relative_dir}}/{{source_name_part}}.pb.h")
32
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
46 }
47
48 # For C++ proto files, the generated proto directory is added as an include
49 # path for the code. This requires using "all_dependent_configs" to force the
50 # include on any code that transitively depends on the generated protos.
51 _include_root = rebase_path(get_path_info(".", "abspath"), "//")
52 _include_config_target = "${target_name}_includes"
53 config(_include_config_target) {
54 include_dirs = [ "$_proto_gen_dir/$_include_root" ]
55 }
56
57 # Create a library with the generated source files.
58 # TODO(frolv): This currently only supports pw_protobuf, which is header-only.
59 # Figure out how to support .cc files.
60 source_set(target_name) {
61 all_dependent_configs = [ ":$_include_config_target" ]
62 deps = [ ":$_gen_target" ] + invoker.deps
63 sources = get_target_outputs(":$_gen_target")
64 }
65}
66
67# Generates protobuf code from .proto definitions for various languages.
68#
69# The languages to generate are defined in the pw_protobuf_langs build variable.
70# Each listed language creates a generated code target called
71#
72# <target_name>_<language>
73#
74# For example, with the following definitions:
75#
76# pw_protobuf_langs = [ "cc", "py" ]
77#
78# pw_proto_library("my_protos") {
79# sources = [ "foo.proto" ]
80# }
81#
82# Two build targets will be created for the declared "my_protos" target.
83#
84# "my_protos_cc" <-- C++ source_set containing generated proto code
85# "my_protos_py" <-- Python module containing generated proto code
86#
87# Args:
88# sources: List of input .proto files.
89# deps: List of other pw_proto_library dependencies.
90#
91# TODO(frolv): Provide a way to set the protoc plugin for different languages.
92template("pw_proto_library") {
93 assert(defined(invoker.sources) && invoker.sources != [],
94 "pw_proto_codegen requires .proto source files")
95
96 foreach(lang, pw_protobuf_langs) {
97 if (defined(invoker.deps)) {
98 _lang_deps = process_file_template(invoker.deps, "{{source}}_${lang}")
99 } else {
100 _lang_deps = []
101 }
102 _lang_target = "${target_name}_${lang}"
103
104 if (lang == "cc") {
105 _pw_cc_proto_library(_lang_target) {
106 protos = invoker.sources
107 deps = _lang_deps
108 }
109 } else {
110 assert(false,
111 string_join(
112 " ",
113 [
114 "pw_proto_codegen doesn't know how to generate code for",
115 "language '$lang'. Please add support if you require it.",
116 ]))
117 }
118 }
119
120 # If the user attempts to use the target directly instead of one of the
121 # language targets, run a script which prints a nice error message.
122 pw_python_script(target_name) {
123 script = string_join("/",
124 [
125 dir_pw_protobuf_compiler,
126 "py",
127 "pw_protobuf_compiler",
128 "proto_target_invalid.py",
129 ])
130 args = [
131 "--target",
132 target_name,
133 "--dir",
134 get_path_info(".", "abspath"),
135 "--root",
136 "//",
137 ] + pw_protobuf_langs
138 stamp = true
139 }
140}