forked from LlsDimple/fidl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfidl.gni
562 lines (493 loc) · 18 KB
/
fidl.gni
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# TODO(vardhan): Currently, targets for each language bindings depend on a
# central generator target which generates bindings for /every/ language. Create
# separate generator targets for each language.
# TODO(vardhan): Currently, the compiler generates type info and calls the
# generators. Sever this, and have generators consume compiled output from an
# intermediate file.
import("//build/dart/dart_package.gni")
import("//build/rust/rust_library.gni")
# Generate C++/JavaScript/Java/Python/Dart/Go source files from fidl files. The
# output files will go under the generated file directory tree with the same
# path as each input file.
#
# Parameters:
#
# sources (optional if one of the deps sets listed below is present)
# List of source .fidl files to compile.
#
# deps (optional)
# Note: this can contain only other fidl targets.
#
# public_deps (optional)
# Note: this can contain only other fidl targets.
#
# import_dirs (optional)
# List of import directories that will get added when processing sources.
#
# testonly (optional)
#
# visibility (optional)
template("fidl") {
assert(
defined(invoker.sources) || defined(invoker.deps) ||
defined(invoker.public_deps),
"\"sources\" or \"deps\" must be defined for the $target_name template.")
cpp_sources_suffix = "cpp_sources"
cpp_sources_target_name = "${target_name}_${cpp_sources_suffix}"
cpp_sync_sources_target_name = "${target_name}_${cpp_sources_suffix}_sync"
if (defined(invoker.sources)) {
legacy_gen_path =
rebase_path("${root_out_dir}/legacy_generators/run_code_generators.py")
go_gen_path = rebase_path("${root_out_dir}/fidl-go")
rust_gen_path = rebase_path("${root_out_dir}/fidl-rust")
fidl_tool = rebase_path("${root_out_dir}/fidl")
generator_cpp_outputs = [
"{{source_gen_dir}}/{{source_file_part}}-common.cc",
"{{source_gen_dir}}/{{source_file_part}}-common.h",
"{{source_gen_dir}}/{{source_file_part}}-internal.h",
"{{source_gen_dir}}/{{source_file_part}}-sync.cc",
"{{source_gen_dir}}/{{source_file_part}}-sync.h",
"{{source_gen_dir}}/{{source_file_part}}.h",
"{{source_gen_dir}}/{{source_file_part}}.cc",
]
generator_dart_outputs = [ "{{source_gen_dir}}/{{source_file_part}}.dart" ]
generator_go_outputs = [ "${root_gen_dir}/go/src/{{source_dir}}/{{source_name_part}}/{{source_name_part}}.core.go" ]
# TODO(vardhan): We can't have both .mojom and .fidl coexist because of
# this hardcoded '_fidl' suffix. Has to do with python imports not handling
# '.fidl/.mojom' in names. Investigate putting in a package instead.
generator_python_outputs =
[ "{{source_gen_dir}}/{{source_name_part}}_fidl.py" ]
generator_rust_outputs =
# "{{source_gen_dir}}/${target_name}/src/{{source_name_part}}.rs" would be
# a cleaner directory structure for the generated crate, but would require
# changes to the generator
[ "{{source_gen_dir}}/{{source_name_part}}.rs" ]
}
generator_python_zip_output = "$target_out_dir/${target_name}_python.pyzip"
generator_rust_crate_output = "$target_gen_dir/${target_name}"
map_target = "${target_name}_map"
if (defined(invoker.sources)) {
generator_target_name = target_name + "__generator"
action_foreach(generator_target_name) {
script = "/usr/bin/env"
public_deps = [
"//lib/fidl/compiler:fidl",
]
deps = [
":$map_target",
"//lib/fidl/compiler:fidl-go",
"//lib/fidl/compiler:fidl-rust",
"//lib/fidl/compiler/legacy_generators",
]
if (defined(invoker.generate_type_info) && invoker.generate_type_info) {
deps += [ "//lib/fidl/compiler/interfaces:interfaces" ]
}
sources = invoker.sources
# In case one of our |sources| depends/imports another one of our |sources|:
inputs = invoker.sources
outputs = generator_cpp_outputs + generator_dart_outputs +
generator_python_outputs + generator_go_outputs +
generator_rust_outputs
args = [
fidl_tool,
"gen",
"--src-root-path",
rebase_path("//", root_build_dir),
"-I",
rebase_path("//", root_build_dir),
"--output-dir",
rebase_path(root_gen_dir),
"--no-gen-imports",
"--generators",
"${legacy_gen_path},${go_gen_path},${rust_gen_path}",
]
# Plumb map to generator (for Rust)
args += [
"--gen-arg",
"map-file=" + rebase_path("${generator_rust_crate_output}_map.txt",
root_build_dir),
]
if (defined(invoker.generate_type_info) && invoker.generate_type_info) {
args += [
"--gen-arg",
"generate-type-info",
]
}
if (defined(ignore_dart_package_annotations) &&
ignore_dart_package_annotations) {
args += [
"--gen-arg",
"dart_ignore-package-annotations",
]
}
if (defined(invoker.import_dirs)) {
foreach(import_dir, invoker.import_dirs) {
args += [
"-I",
rebase_path(import_dir, root_build_dir),
]
}
}
args += [ "{{source}}" ]
}
}
# Some re-usable variables for the C++ source sets:
cpp_public_deps = []
if (defined(invoker.public_deps)) {
cpp_public_deps += invoker.public_deps
}
if (defined(invoker.sources)) {
cpp_public_deps += [ ":$generator_target_name" ]
}
cpp_deps = []
if (defined(invoker.deps)) {
cpp_deps += invoker.deps
}
source_set(target_name) {
if (defined(invoker.visibility)) {
visibility = invoker.visibility
}
if (defined(invoker.testonly)) {
testonly = invoker.testonly
}
public_configs = [ "//lib/fidl/build/config:fidl_source" ]
public_deps = [
"//lib/fidl/cpp/bindings",
]
public_deps += cpp_public_deps
if (defined(invoker.sources)) {
public_deps += [ ":${cpp_sources_target_name}" ]
}
deps = cpp_deps
}
# This target provides C++ synchronous bindings support. The dependent may
# also depend on the non-sync C++ bindings target.
source_set("${target_name}_sync") {
if (defined(invoker.visibility)) {
visibility = invoker.visibility
}
if (defined(invoker.testonly)) {
testonly = invoker.testonly
}
public_configs = [ "//lib/fidl/build/config:fidl_source" ]
public_deps = [
"//lib/fidl/cpp/bindings",
]
public_deps += cpp_public_deps
if (defined(invoker.sources)) {
public_deps += [ ":${cpp_sync_sources_target_name}" ]
}
deps = cpp_deps
}
# This target provides C++ support for serialization without requiring any
# support from the OS. (The other C++ bindings assume message pipes, etc.)
source_set("${target_name}_data_only") {
if (defined(invoker.visibility)) {
visibility = invoker.visibility
}
if (defined(invoker.testonly)) {
testonly = invoker.testonly
}
public_configs = [ "//lib/fidl/build/config:fidl_source" ]
public_deps = [
"//lib/fidl/cpp/bindings:serialization",
]
public_deps += cpp_public_deps
if (defined(invoker.sources)) {
public_deps += [ ":${cpp_sources_target_name}" ]
}
deps = cpp_deps
}
all_deps = []
if (defined(invoker.deps)) {
all_deps += invoker.deps
}
if (defined(invoker.public_deps)) {
all_deps += invoker.public_deps
}
group("${target_name}__is_fidl") {
}
# Explicitly ensure that all dependencies (invoker.deps and
# invoker.public_deps) are fidl targets themselves.
group("${target_name}__check_deps_are_all_fidl") {
deps = []
foreach(d, all_deps) {
name = get_label_info(d, "label_no_toolchain")
toolchain = get_label_info(d, "toolchain")
deps += [ "${name}__is_fidl(${toolchain})" ]
}
}
action("${target_name}_python") {
script = "//lib/fidl/build/zip.py"
deps = []
zip_inputs = []
if (defined(invoker.sources)) {
inputs = process_file_template(invoker.sources, generator_python_outputs)
deps += [ ":$generator_target_name" ]
}
foreach(d, all_deps) {
# Resolve the name, so that a target //mojo/something becomes
# //mojo/something:something and we can append "_python" to get the python
# dependency name.
full_name = get_label_info(d, "label_no_toolchain")
dep_name = get_label_info(d, "name")
dep_target_out_dir = get_label_info(d, "target_out_dir")
deps += [ "${full_name}_python" ]
zip_inputs += [ "$dep_target_out_dir/${dep_name}_python.pyzip" ]
}
output = generator_python_zip_output
outputs = [
output,
]
rebase_base_dir = rebase_path(target_gen_dir, root_build_dir)
if (defined(invoker.sources)) {
rebase_inputs = rebase_path(inputs, root_build_dir)
}
rebase_zip_inputs = rebase_path(zip_inputs, root_build_dir)
rebase_output = rebase_path(output, root_build_dir)
args = [
"--base-dir=$rebase_base_dir",
"--zip-inputs=$rebase_zip_inputs",
"--output=$rebase_output",
]
if (defined(invoker.sources)) {
args += [ "--inputs=$rebase_inputs" ]
}
}
rust_crate_target_name = "${target_name}_rust"
action(rust_crate_target_name) {
script = "//lib/fidl/build/make_crate.py"
deps = []
dep_inputs = []
if (defined(invoker.sources)) {
inputs = process_file_template(invoker.sources, generator_rust_outputs)
deps += [ ":$generator_target_name" ]
}
foreach(d, all_deps) {
# Resolve the name, so that a target //fidl/something becomes
# //fidl/something:something and we can append "_rust" to get the rust
# dependency name.
full_name = get_label_info(d, "label_no_toolchain")
dep_name = get_label_info(d, "name")
dep_target_gen_dir = get_label_info(d, "target_gen_dir")
deps += [ "${full_name}_rust" ]
dep_inputs += [ "$dep_target_gen_dir/${dep_name}" ]
}
output_base = generator_rust_crate_output
outputs = [
"$output_base/Cargo.toml",
"$output_base/src/lib.rs",
]
rebase_gen_dir = rebase_path(root_gen_dir)
if (defined(invoker.sources)) {
rebase_inputs = rebase_path(inputs, root_build_dir)
}
rebase_dep_inputs = rebase_path(dep_inputs, root_gen_dir)
rebase_output = rebase_path(output_base, root_gen_dir)
rebase_srcroot = rebase_path("//", root_build_dir)
args = [
"--gen-dir=$rebase_gen_dir",
"--dep-inputs=$rebase_dep_inputs",
"--output=$rebase_output",
"--srcroot=$rebase_srcroot",
]
if (defined(invoker.sources)) {
args += [ "--inputs=$rebase_inputs" ]
}
}
rust_library("${target_name}_rust_library") {
name = exec_script("//lib/fidl/build/label_to_crate.py",
[ rebase_path(generator_rust_crate_output, root_gen_dir) ],
"trim string")
source_dir = generator_rust_crate_output
deps = []
foreach(d, all_deps) {
# Resolve the name, so that a target //fidl/something becomes
# //fidl/something:something and we can append "_rust_library" to get the
# rust dependency name.
full_name = get_label_info(d, "label_no_toolchain")
deps += [ "${full_name}_rust_library" ]
}
deps += [
"//lib/fidl/rust/fidl",
"//rust/magenta-rs:magenta",
]
non_rust_deps = [
":${rust_crate_target_name}"
]
}
# Creates a map from fidl file to GN build target which supplies it. Used primarily
# for resolving dependencies in the Rust binding generation.
save_target = target_name
action(map_target) {
script = "//lib/fidl/build/make_map.py"
deps = []
foreach(d, all_deps) {
# Resolve the name, so that a target //fidl/something becomes
# //fidl/something:something and we can append "_map" to get the map
# dependency name.
full_name = get_label_info(d, "label_no_toolchain")
deps += [ "${full_name}_map" ]
}
if (defined(invoker.deps)) {
map_deps = []
foreach(d, invoker.deps) {
dep_name = get_label_info(d, "name")
dep_target_gen_dir = get_label_info(d, "target_gen_dir")
map_deps += [ "$dep_target_gen_dir/${dep_name}_map.txt" ]
}
rebase_map_deps = rebase_path(map_deps, root_build_dir)
}
if (defined(invoker.public_deps)) {
map_public_deps = []
foreach(d, invoker.public_deps) {
dep_name = get_label_info(d, "name")
dep_target_gen_dir = get_label_info(d, "target_gen_dir")
map_public_deps += [ "$dep_target_gen_dir/${dep_name}_map.txt" ]
}
rebase_map_public_deps = rebase_path(map_public_deps, root_build_dir)
}
output_fn = "${generator_rust_crate_output}_map.txt"
outputs = [
output_fn,
]
if (defined(invoker.sources)) {
arg_sources = invoker.sources
}
rebase_output = rebase_path(output_fn, root_build_dir)
target_dir = get_label_info("${save_target}", "label_no_toolchain")
args = [
"--target=$target_dir",
"--output=$rebase_output",
]
if (defined(invoker.sources)) {
args += [ "--sources=$arg_sources" ]
}
if (defined(invoker.deps)) {
args += [ "--map-deps=$rebase_map_deps" ]
}
if (defined(invoker.public_deps)) {
args += [ "--map-public-deps=$rebase_map_public_deps" ]
}
}
if (defined(invoker.sources)) {
# The generated C++ source files. The main reason to introduce this target
# is so that lib/fidl/cpp/bindings can depend on fidl interfaces without
# circular dependencies. It means that the target is missing the dependency
# on lib/fidl/cpp/bindings. No external targets should depend directly on
# this target *except* lib/fidl/cpp/bindings and other *_cpp_sources
# targets.
source_set(cpp_sources_target_name + "__common") {
if (defined(invoker.testonly)) {
testonly = invoker.testonly
}
sources = process_file_template(
invoker.sources,
[
"{{source_gen_dir}}/{{source_file_part}}-common.cc",
"{{source_gen_dir}}/{{source_file_part}}-common.h",
"{{source_gen_dir}}/{{source_file_part}}-internal.h",
])
public_configs = [ "//lib/fidl/build/config:fidl_source" ]
deps = [
":$generator_target_name",
"//lib/fidl/compiler/interfaces:interfaces__generator",
"//lib/fidl/cpp/bindings",
]
foreach(d, all_deps) {
# Resolve the name, so that a target //mojo/something becomes
# //mojo/something:something and we can append cpp_sources_suffix to
# get the cpp dependency name.
full_name = get_label_info(d, "label_no_toolchain")
deps += [ "${full_name}_${cpp_sources_suffix}" ]
}
}
source_set(cpp_sources_target_name) {
if (defined(invoker.testonly)) {
testonly = invoker.testonly
}
sources =
process_file_template(invoker.sources,
[
"{{source_gen_dir}}/{{source_file_part}}.h",
"{{source_gen_dir}}/{{source_file_part}}.cc",
])
public_configs = [ "//lib/fidl/cpp/bindings:bindings_config" ]
deps = [
":$generator_target_name",
":${cpp_sources_target_name}__common",
"//lib/fidl/compiler/interfaces:interfaces__generator",
]
foreach(d, all_deps) {
# Resolve the name, so that a target //mojo/something becomes
# //mojo/something:something and we can append cpp_sources_suffix to
# get the cpp dependency name.
full_name = get_label_info(d, "label_no_toolchain")
deps += [ "${full_name}_${cpp_sources_suffix}" ]
}
}
source_set(cpp_sync_sources_target_name) {
if (defined(invoker.testonly)) {
testonly = invoker.testonly
}
sources = process_file_template(
invoker.sources,
[
"{{source_gen_dir}}/{{source_file_part}}-sync.cc",
"{{source_gen_dir}}/{{source_file_part}}-sync.h",
])
public_configs = [
"//lib/fidl/cpp/bindings:bindings_config",
"//lib/fidl/build/config:fidl_source",
]
deps = [
":$generator_target_name",
":${cpp_sources_target_name}__common",
"//lib/fidl/compiler/interfaces:interfaces__generator",
]
foreach(d, all_deps) {
# Resolve the name, so that a target //mojo/something becomes
# //mojo/something:something and we can append cpp_sources_suffix to
# get the cpp dependency name.
full_name = get_label_info(d, "label_no_toolchain")
deps += [ "${full_name}_${cpp_sources_suffix}" ]
}
}
}
# Compute the Dart package name in advance to ensure it does not contain the
# "_dart" suffix.
# See //build/dart/dart_package.gni for more details.
target_label = get_label_info(":$target_name", "label_no_toolchain")
dart_package_name = exec_script("//build/dart/label_to_package_name.py",
[ target_label ],
"trim string")
# This target is used to generate Dart package information
dart_package("${target_name}_dart") {
package_name = dart_package_name
source_dir = target_gen_dir
analysis_options = "//lib/fidl/dart/analysis_options_fidl"
deps = [
"//lib/fidl/dart",
]
fidl_deps = []
if (defined(invoker.deps)) {
fidl_deps += invoker.deps
}
if (defined(invoker.public_deps)) {
fidl_deps += invoker.public_deps
}
# Depend on the generated Dart package for all of our fidl dependencies.
foreach(d, fidl_deps) {
dep_label = get_label_info(d, "label_no_toolchain")
deps += [ "${dep_label}_dart" ]
}
non_dart_deps = []
if (defined(invoker.sources)) {
non_dart_deps +=
[ get_label_info(":$generator_target_name", "label_no_toolchain") ]
}
}
}