aboutsummaryrefslogtreecommitdiff
path: root/pw_protobuf_compiler/proto.gni
blob: f7b1a28a7f0afdd63b72bb7834bff2ff161ea18c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# Copyright 2020 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

import("//build_overrides/pigweed.gni")

import("$dir_pw_build/error.gni")
import("$dir_pw_build/input_group.gni")
import("$dir_pw_build/python_action.gni")
import("$dir_pw_build/target_types.gni")
import("$dir_pw_third_party/nanopb/nanopb.gni")

# Variables forwarded from the public pw_proto_library template to the final
# pw_source_set.
_forwarded_vars = [
  "testonly",
  "visibility",
]

# Internal template that invokes protoc with a pw_python_action. This should not
# be used outside of this file; use pw_proto_library instead.
#
# This creates the internal GN target $target_name.$language._gen that compiles
# proto files with protoc.
template("_pw_invoke_protoc") {
  _output = rebase_path(get_target_outputs(":${invoker.base_target}._metadata"))

  pw_python_action("$target_name._gen") {
    forward_variables_from(invoker, [ "metadata" ])
    script =
        "$dir_pw_protobuf_compiler/py/pw_protobuf_compiler/generate_protos.py"

    deps = [
             ":${invoker.base_target}._metadata",
             ":${invoker.base_target}._inputs",
           ] + invoker.deps

    args = [
             "--language",
             invoker.language,
             "--module-path",
             rebase_path("."),
             "--include-file",
             _output[0],
             "--out-dir",
             rebase_path(invoker.gen_dir),
           ] + rebase_path(invoker.sources)

    inputs = invoker.sources

    if (defined(invoker.plugin)) {
      inputs += [ invoker.plugin ]
      args += [ "--plugin-path=" + rebase_path(invoker.plugin) ]
    }

    if (defined(invoker.include_paths)) {
      args += [
        "--include-paths",
        string_join(";", rebase_path(invoker.include_paths)),
      ]
    }

    outputs = []
    foreach(extension, invoker.output_extensions) {
      foreach(proto,
              rebase_path(invoker.sources, get_path_info(".", "abspath"))) {
        _output = string_replace(proto, ".proto", extension)
        outputs += [ "${invoker.gen_dir}/$_output" ]
      }
    }

    if (outputs == []) {
      stamp = true
    }

    visibility = [ ":*" ]
  }
}

# Generates pw_protobuf C++ code for proto files, creating a source_set of the
# generated files. This is internal and should not be used outside of this file.
# Use pw_proto_library instead.
template("_pw_pwpb_proto_library") {
  _pw_invoke_protoc(target_name) {
    forward_variables_from(invoker, "*", _forwarded_vars)
    language = "pwpb"
    plugin = "$dir_pw_protobuf/py/pw_protobuf/plugin.py"
    deps += [ "$dir_pw_protobuf/py" ]
    output_extensions = [ ".pwpb.h" ]
  }

  # Create a library with the generated source files.
  pw_source_set(target_name) {
    forward_variables_from(invoker, _forwarded_vars)
    public_configs = [ ":${invoker.base_target}._include_path" ]
    deps = [ ":$target_name._gen" ]
    public_deps = [ dir_pw_protobuf ] + invoker.deps
    sources = get_target_outputs(":$target_name._gen")
    public = filter_include(sources, [ "*.pwpb.h" ])
  }
}

# Generates nanopb RPC code for proto files, creating a source_set of the
# generated files. This is internal and should not be used outside of this file.
# Use pw_proto_library instead.
template("_pw_nanopb_rpc_proto_library") {
  # Create a target which runs protoc configured with the nanopb_rpc plugin to
  # generate the C++ proto RPC headers.
  _pw_invoke_protoc(target_name) {
    forward_variables_from(invoker, "*", _forwarded_vars)
    language = "nanopb_rpc"
    plugin = "$dir_pw_rpc/py/pw_rpc/plugin_nanopb.py"
    deps += [ "$dir_pw_rpc/py" ]
    include_paths = [ "$dir_pw_third_party_nanopb/generator/proto" ]
    output_extensions = [ ".rpc.pb.h" ]
  }

  # Create a library with the generated source files.
  pw_source_set(target_name) {
    forward_variables_from(invoker, _forwarded_vars)
    public_configs = [ ":${invoker.base_target}._include_path" ]
    deps = [ ":$target_name._gen" ]
    public_deps = [
                    ":${invoker.base_target}.nanopb",
                    "$dir_pw_rpc:server",
                    "$dir_pw_rpc/nanopb:method_union",
                    "$dir_pw_third_party/nanopb",
                  ] + invoker.deps
    public = get_target_outputs(":$target_name._gen")
  }
}

# Generates nanopb code for proto files, creating a source_set of the generated
# files. This is internal and should not be used outside of this file. Use
# pw_proto_library instead.
template("_pw_nanopb_proto_library") {
  # Create a target which runs protoc configured with the nanopb plugin to
  # generate the C proto sources.
  _pw_invoke_protoc(target_name) {
    forward_variables_from(invoker, "*", _forwarded_vars)
    language = "nanopb"
    plugin = "$dir_pw_third_party_nanopb/generator/protoc-gen-nanopb"
    include_paths = [ "$dir_pw_third_party_nanopb/generator/proto" ]
    output_extensions = [
      ".pb.h",
      ".pb.c",
    ]
  }

  # Create a library with the generated source files.
  pw_source_set(target_name) {
    forward_variables_from(invoker, _forwarded_vars)
    public_configs = [ ":${invoker.base_target}._include_path" ]
    deps = [ ":$target_name._gen" ]
    public_deps = [ "$dir_pw_third_party/nanopb" ] + invoker.deps
    sources = get_target_outputs(":$target_name._gen")
    public = filter_include(sources, [ "*.pb.h" ])
  }
}

# Generates raw RPC code for proto files, creating a source_set of the generated
# files. This is internal and should not be used outside of this file. Use
# pw_proto_library instead.
template("_pw_raw_rpc_proto_library") {
  # Create a target which runs protoc configured with the nanopb_rpc plugin to
  # generate the C++ proto RPC headers.
  _pw_invoke_protoc(target_name) {
    forward_variables_from(invoker, "*", _forwarded_vars)
    language = "raw_rpc"
    plugin = "$dir_pw_rpc/py/pw_rpc/plugin_raw.py"
    deps += [ "$dir_pw_rpc/py" ]
    output_extensions = [ ".raw_rpc.pb.h" ]
  }

  # Create a library with the generated source files.
  pw_source_set(target_name) {
    forward_variables_from(invoker, _forwarded_vars)
    public_configs = [ ":${invoker.base_target}._include_path" ]
    deps = [ ":$target_name._gen" ]
    public_deps = [
                    "$dir_pw_rpc:server",
                    "$dir_pw_rpc/raw:method_union",
                  ] + invoker.deps
    public = get_target_outputs(":$target_name._gen")
  }
}

# Generates Go code for proto files, listing the proto output directory in the
# metadata variable GOPATH. Internal use only.
template("_pw_go_proto_library") {
  _proto_gopath = "$root_gen_dir/go"

  _pw_invoke_protoc(target_name) {
    forward_variables_from(invoker, "*")
    language = "go"
    metadata = {
      gopath = [ "GOPATH+=" + rebase_path(_proto_gopath) ]
      external_deps = [
        "github.com/golang/protobuf/proto",
        "google.golang.org/grpc",
      ]
    }
    output_extensions = []  # Don't enumerate the generated .go files.
    gen_dir = "$_proto_gopath/src"
  }

  group(target_name) {
    deps = [ ":$target_name._gen" ]
  }
}

# Generates protobuf code from .proto definitions for various languages.
# For each supported generator, creates a sub-target named:
#
#   <target_name>.<generator>
#
# Args:
#  sources: List of input .proto files.
#  deps: List of other pw_proto_library dependencies.
#  inputs: Other files on which the protos depend (e.g. nanopb .options files).
#
template("pw_proto_library") {
  assert(defined(invoker.sources) && invoker.sources != [],
         "pw_proto_library requires .proto source files")

  _common = {
    base_target = target_name
    gen_dir = "$target_gen_dir/protos"
    sources = invoker.sources
  }

  if (defined(invoker.deps)) {
    _deps = invoker.deps
  } else {
    _deps = []
  }

  # For each proto target, create a file which collects the base directories of
  # all of its dependencies to list as include paths to protoc.
  generated_file("$target_name._metadata") {
    # Collect metadata from the include path files of each dependency.
    deps = process_file_template(_deps, "{{source}}._metadata")

    data_keys = [ "protoc_includes" ]
    outputs = [ "$target_gen_dir/${target_name}_includes.txt" ]

    # Indicate this library's base directory for its dependents.
    metadata = {
      protoc_includes = [ rebase_path(".") ]
    }
  }

  # Toss any additional inputs into an input group dependency.
  if (defined(invoker.inputs)) {
    pw_input_group("$target_name._inputs") {
      inputs = invoker.inputs
      visibility = [ ":*" ]
    }
  } else {
    group("$target_name._inputs") {
      visibility = [ ":*" ]
    }
  }

  # Create a config with the generated proto directory, which is used for C++.
  config("$target_name._include_path") {
    include_dirs = [ _common.gen_dir ]
    visibility = [ ":*" ]
  }

  # Enumerate all of the protobuf generator targets.

  _pw_pwpb_proto_library("$target_name.pwpb") {
    forward_variables_from(invoker, _forwarded_vars)
    forward_variables_from(_common, "*")
    deps = process_file_template(_deps, "{{source}}.pwpb")
  }

  if (dir_pw_third_party_nanopb != "") {
    _pw_nanopb_rpc_proto_library("$target_name.nanopb_rpc") {
      forward_variables_from(invoker, _forwarded_vars)
      forward_variables_from(_common, "*")
      deps = process_file_template(_deps, "{{source}}.nanopb_rpc")
    }

    _pw_nanopb_proto_library("$target_name.nanopb") {
      forward_variables_from(invoker, _forwarded_vars)
      forward_variables_from(_common, "*")
      deps = process_file_template(_deps, "{{source}}.nanopb")
    }
  } else {
    pw_error("$target_name.nanopb_rpc") {
      message =
          "\$dir_pw_third_party_nanopb must be set to generate nanopb RPC code."
    }

    pw_error("$target_name.nanopb") {
      message =
          "\$dir_pw_third_party_nanopb must be set to compile nanopb protobufs."
    }
  }

  _pw_raw_rpc_proto_library("$target_name.raw_rpc") {
    forward_variables_from(invoker, _forwarded_vars)
    forward_variables_from(_common, "*", [ "deps" ])
    deps = process_file_template(_deps, "{{source}}.raw_rpc")
  }

  _pw_go_proto_library("$target_name.go") {
    sources = invoker.sources
    deps = process_file_template(_deps, "{{source}}.go")
    base_target = _common.base_target
  }

  # All supported pw_protobuf generators.
  _protobuf_generators = [
    "pwpb",
    "nanopb",
    "nanopb_rpc",
    "raw_rpc",
    "go",
  ]

  # If the user attempts to use the target directly instead of one of the
  # generator targets, run a script which prints a nice error message.
  pw_python_action(target_name) {
    script = string_join("/",
                         [
                           dir_pw_protobuf_compiler,
                           "py",
                           "pw_protobuf_compiler",
                           "proto_target_invalid.py",
                         ])
    args = [
             "--target",
             target_name,
             "--dir",
             get_path_info(".", "abspath"),
             "--root",
             "//",
           ] + _protobuf_generators
    stamp = true
  }
}