-
Notifications
You must be signed in to change notification settings - Fork 23
/
jsinterop_generator.bzl
485 lines (403 loc) · 16.8 KB
/
jsinterop_generator.bzl
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
"""jsinterop_generator build rule.
Takes closure extern files and generates java files annotated with JsInterop annotations for the types
defined in the extern files.
By default, this rule produces a java_library with the same name usable with gwt. This behavior
can be disabled by setting the parameter generate_gwt_library to False.
By default, this rule produces a j2cl_library with the same name suffixed by '-j2cl'. This
behavior can be disabled by setting the parameter generate_j2cl_library to False.
Examples:
jsinterop_generator(
name = "foo",
srcs = ["foo_extern.js"],
)
Generates:
java_library(
name = "foo"
# contains generated sources and related gwt module
}
j2cl_library(
name = "foo-j2cl",
# contains generated sources
)
"""
load("@bazel_common_javadoc//:javadoc.bzl", "javadoc_library")
load("@rules_java//java:defs.bzl", "java_library")
load("@com_google_j2cl//build_defs:rules.bzl", "j2cl_library")
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_library")
_is_bazel = not hasattr(native, "genmpm") # this_is_bazel
JS_INTEROP_RULE_NAME_PATTERN = "%s__internal_src_generated"
JsInteropGeneratorInfo = provider()
def _get_generator_files(deps):
transitive_srcs = [dep[JsInteropGeneratorInfo].transitive_sources for dep in deps]
transitive_types_mappings = [dep[JsInteropGeneratorInfo].transitive_types_mappings for dep in deps]
gwt_module_names = [name for dep in deps for name in dep[JsInteropGeneratorInfo].gwt_module_names]
return struct(
sources = depset(transitive = transitive_srcs),
types_mappings = depset(transitive = transitive_types_mappings),
gwt_module_names = gwt_module_names,
)
def _jsinterop_generator_export_impl(ctx):
""" Implementation of the _jsinterop_generator_export skylark rule.
This rule is used to export existing jsinterop_generator targets.
It collects the infos of the JsInteropGeneratorInfo provider of each target
of the exports attribute and reexpose everything in one provider.
"""
jsinterop_files = _get_generator_files(ctx.attr.exports)
# reexpose files and properties collected from all exported targets.
return [
JsInteropGeneratorInfo(
transitive_sources = jsinterop_files.sources,
transitive_types_mappings = jsinterop_files.types_mappings,
gwt_module_names = jsinterop_files.gwt_module_names,
),
]
_jsinterop_generator_export = rule(
attrs = {
"exports": attr.label_list(allow_files = True),
},
implementation = _jsinterop_generator_export_impl,
)
def _closure_impl(srcs, deps_files, types_mapping_file, ctx):
deps_srcs = deps_files.sources.to_list()
dep_types_mapping_files = deps_files.types_mappings.to_list()
names_mapping_files = ctx.files.name_mapping_files
arguments = [
"--output=%s" % ctx.outputs._generated_jar.path,
"--output_dependency_file=%s" % types_mapping_file.path,
"--package_prefix=%s" % ctx.attr.package_prefix,
"--extension_type_prefix=%s" % ctx.attr.extension_type_prefix,
"--global_scope_class_name=%s" % ctx.attr.global_scope_class_name,
]
arguments += ["--dependency=%s" % f.path for f in deps_srcs]
arguments += ["--dependency_mapping_file=%s" % f.path for f in dep_types_mapping_files]
arguments += ["--name_mapping_file=%s" % f.path for f in names_mapping_files]
arguments += ["--integer_entities_file=%s" % f.path for f in ctx.files.integer_entities_files]
arguments += ["--wildcard_types_file=%s" % f.path for f in ctx.files.wildcard_types_files]
if ctx.attr.debug:
arguments += ["--debug_mode"]
if ctx.attr.runtime_deps:
runtime_deps_expanded = [f.path for d in ctx.attr.runtime_deps for f in d.files.to_list()]
main_advice_classpath = "--main_advice_classpath=%s" % ":".join(runtime_deps_expanded)
# main_advice_classpath is a flag for the java launcher shell script used for prepending
# additional class path entries. If it isn't the first flag presented to the script, it
# won't be recognized unless it's wrapped like so:
# --wrapper_script_flag=--main_advice_classpath=...
arguments += ["--wrapper_script_flag=%s" % main_advice_classpath]
if ctx.attr.custom_preprocessing_pass:
arguments += [
"--custom_preprocessing_pass=%s" % v
for v in ctx.attr.custom_preprocessing_pass
]
arguments += ["%s" % f.path for f in srcs]
inputs = srcs + deps_srcs + dep_types_mapping_files + names_mapping_files
inputs += ctx.files.integer_entities_files + ctx.files.wildcard_types_files
inputs += [f for d in ctx.attr.runtime_deps for f in d.files.to_list()]
ctx.actions.run(
inputs = inputs,
outputs = [ctx.outputs._generated_jar, types_mapping_file],
executable = ctx.executable._closure_generator,
progress_message = "Generating JsInterop classes from externs",
arguments = arguments,
)
def _jsinterop_generator_impl(ctx):
srcs = ctx.files.srcs
deps_files = _get_generator_files(ctx.attr.deps)
types_mapping_file = ctx.actions.declare_file("%s_types" % ctx.attr.name)
if ctx.attr.conversion_mode == "closure":
_closure_impl(srcs, deps_files, types_mapping_file, ctx)
# generate the gwt.xml file by concatenating dependencies gwt inherits files
gwt_xml_file = ctx.outputs._gwt_xml_file
dep_gwt_module_names = deps_files.gwt_module_names
gwt_xml_content = [
"<module>",
"<source path=\"\"/>",
"<inherits name=\"jsinterop.base.Base\" />",
]
gwt_xml_content += ["<inherits name=\"%s\" />" % dep_module for dep_module in dep_gwt_module_names]
gwt_xml_content += ["</module>"]
ctx.actions.write(
output = gwt_xml_file,
content = "\n".join(gwt_xml_content),
)
# generate the gwt module name for dependency purpose
if not ctx.attr.package_prefix:
gwt_module_name = "%s" % ctx.attr.gwt_module_name
else:
gwt_module_name = "%s.%s" % (ctx.attr.package_prefix, ctx.attr.gwt_module_name)
# format output
arguments = [
ctx.outputs._generated_jar.path,
ctx.outputs._formatted_jar.path,
ctx.executable._google_java_formatter.path,
ctx.executable._zip.path,
]
tools = [
ctx.executable._google_java_formatter,
ctx.executable._zip,
]
ctx.actions.run(
inputs = [ctx.outputs._generated_jar],
tools = tools,
outputs = [ctx.outputs._formatted_jar],
executable = ctx.executable._format_jar_script,
progress_message = "Formatting java classes",
arguments = arguments,
)
return [
JsInteropGeneratorInfo(
transitive_sources = depset(srcs, transitive = [deps_files.sources]),
transitive_types_mappings = depset([types_mapping_file], transitive = [deps_files.types_mappings]),
gwt_module_names = [gwt_module_name],
),
]
_jsinterop_generator = rule(
attrs = {
"srcs": attr.label_list(
mandatory = True,
allow_files = [
".d.ts",
".js",
],
),
"deps": attr.label_list(allow_files = True),
"package_prefix": attr.string(),
"extension_type_prefix": attr.string(),
"global_scope_class_name": attr.string(),
"name_mapping_files": attr.label_list(allow_files = True),
"integer_entities_files": attr.label_list(allow_files = True),
"wildcard_types_files": attr.label_list(allow_files = True),
"debug": attr.bool(),
"conversion_mode": attr.string(),
"gwt_module_name": attr.string(),
"runtime_deps": attr.label_list(),
"custom_preprocessing_pass": attr.string_list(),
"_zip": attr.label(
cfg = "exec",
executable = True,
default = Label("@bazel_tools//tools/zip:zipper"),
),
"_google_java_formatter": attr.label(
cfg = "exec",
executable = True,
allow_files = True,
default = Label("//third_party:google_java_format"),
),
"_closure_generator": attr.label(
cfg = "exec",
executable = True,
default = Label("//java/jsinterop/generator/closure:ClosureJsinteropGenerator"),
),
"_format_jar_script": attr.label(
cfg = "exec",
executable = True,
allow_files = True,
default = Label(
"//internal_do_not_use:format_srcjar",
),
),
},
outputs = {
"_formatted_jar": "%{name}.srcjar",
"_generated_jar": "%{name}_non_formatted.jar",
"_gwt_xml_file": "%{gwt_module_name}.gwt.xml",
},
implementation = _jsinterop_generator_impl,
)
# Macro invoking the skylark rule
def jsinterop_generator(
name,
srcs = [],
exports = [],
deps = [],
gwt_java_deps = [],
extension_type_prefix = None,
global_scope_class_name = None,
name_mapping_files = [],
integer_entities_files = [],
wildcard_types_files = [],
package_prefix = None,
generate_j2cl_library = True,
generate_gwt_library = True,
conversion_mode = "closure",
generate_j2cl_build_test = None,
externs_deps = None, # Auto-populated from srcs by default.
runtime_deps = [],
custom_preprocessing_pass = [],
visibility = None,
testonly = None):
if not srcs and not exports:
fail("Empty rule. Nothing to generate or import.")
if not srcs and deps:
fail("deps cannot be used without srcs.")
if not generate_j2cl_library and not generate_gwt_library:
fail("either generate_j2cl_library or generate_gwt_library should be set to True")
exports_java = [_absolute_label(export) for export in exports]
exports_j2cl = ["%s-j2cl" % export for export in exports_java]
exports_js_interop_generator = [JS_INTEROP_RULE_NAME_PATTERN % export for export in exports_java]
deps_java = [_absolute_label(dep) for dep in deps]
deps_j2cl = ["%s-j2cl" % dep for dep in deps_java]
deps_js_interop_generator = [JS_INTEROP_RULE_NAME_PATTERN % dep for dep in deps_java]
deps_java += [_absolute_label(gwt_java_dep) for gwt_java_dep in gwt_java_deps]
jsinterop_generator_rule_name = JS_INTEROP_RULE_NAME_PATTERN % name
if srcs:
generator_srcs = srcs[:]
if not package_prefix:
package_prefix = _get_java_package(native.package_name())
if conversion_mode == "closure":
if externs_deps == None:
# Pass the extern files present in the srcs as deps of the j2cl_library
externs_deps = srcs
if externs_deps:
externs_lib_name = "%s-externs" % name
closure_js_library(
name = externs_lib_name,
srcs = externs_deps,
testonly = testonly,
)
deps_j2cl.append(":%s" % externs_lib_name)
else:
fail("Unknown conversion mode")
if not extension_type_prefix:
extension_type_prefix = name[0].upper() + name[1:]
if not global_scope_class_name:
global_scope_class_name = "%sGlobal" % extension_type_prefix
gwt_module_name = extension_type_prefix
_jsinterop_generator(
name = jsinterop_generator_rule_name,
srcs = generator_srcs,
deps = deps_js_interop_generator,
package_prefix = package_prefix,
extension_type_prefix = extension_type_prefix,
global_scope_class_name = global_scope_class_name,
name_mapping_files = name_mapping_files,
integer_entities_files = integer_entities_files,
wildcard_types_files = wildcard_types_files,
# TODO(dramaix): replace it by a blaze flag
debug = False,
conversion_mode = conversion_mode,
gwt_module_name = gwt_module_name,
runtime_deps = runtime_deps,
custom_preprocessing_pass = custom_preprocessing_pass,
testonly = testonly,
visibility = ["//visibility:public"],
)
generated_jars = [":%s" % jsinterop_generator_rule_name]
gwt_xml_file = ":%s.gwt.xml" % gwt_module_name
deps_java += [
Label("@com_google_j2cl//:jsinterop-annotations"),
Label("@com_google_jsinterop_base//:jsinterop-base"),
Label("//third_party:jspecify_annotations"),
]
deps_j2cl += [
Label("@com_google_j2cl//:jsinterop-annotations-j2cl"),
Label("@com_google_jsinterop_base//:jsinterop-base-j2cl"),
Label("//third_party:jspecify_annotations-j2cl"),
]
else:
# exporting existing generated libraries.
_jsinterop_generator_export(
name = jsinterop_generator_rule_name,
exports = exports_js_interop_generator,
)
generated_jars = None
gwt_xml_file = None
if not deps_java:
# Passing an empty array to java_library fails when we define an exports attribute
deps_java = None
deps_j2cl = None
if generate_j2cl_library:
j2cl_library(
name = "%s-j2cl" % name,
srcs = generated_jars,
generate_build_test = generate_j2cl_build_test,
deps = deps_j2cl,
exports = exports_j2cl,
testonly = testonly,
visibility = visibility,
)
if generate_gwt_library:
java_library_args = {
"name": name,
"srcs": generated_jars,
"deps": deps_java,
"exports": exports_java,
"visibility": visibility,
"testonly": testonly,
}
# bazel doesn't support constraint and gwtxml attributes
if _is_bazel:
if gwt_xml_file:
java_library_args["resources"] = [gwt_xml_file]
else:
java_library_args["gwtxml"] = gwt_xml_file
java_library_args["constraints"] = ["gwt", "public"]
java_library(**java_library_args)
_extract_srcjar(
name = name + "_generated_files",
srcjar = ":%s.srcjar" % jsinterop_generator_rule_name,
tags = ["manual", "notap"],
visibility = ["//visibility:private"],
)
javadoc_library(
name = name + "-javadoc",
srcs = [":" + name + "_generated_files"],
tags = ["manual", "notap"],
visibility = ["//visibility:private"],
deps = deps_java,
)
def _extract_srcjar_impl(ctx):
"""Extracts the generated java files from transpiled source jar.
Returns tree artifact outputs of the extracted java sources.
"""
output_dir = ctx.actions.declare_directory(ctx.label.name)
ctx.actions.run_shell(
command = "unzip -q %s *.java -d %s" % (ctx.file.srcjar.path, output_dir.path),
inputs = [ctx.file.srcjar],
outputs = [output_dir],
)
return [DefaultInfo(files = depset([output_dir]))]
# TODO(b/266158209): Change the jsinterop_generator rule to directly output the tree artifact
_extract_srcjar = rule(
attrs = {
"srcjar": attr.label(
allow_single_file = [".srcjar"],
mandatory = True,
),
},
implementation = _extract_srcjar_impl,
)
def _absolute_label(label):
"""Expand a label to be of the full form //package:foo.
Args:
label: string in relative or absolute form.
Returns:
Absolute form of the label as a string.
"""
if label.startswith("//"):
label = label[2:] # drop the leading //
colon_split = label.split(":")
if len(colon_split) == 1: # no ":" in label
pkg = label
_, _, target = label.rpartition("/")
else:
pkg, target = colon_split # fails if len(colon_split) != 2
else:
colon_split = label.split(":")
if len(colon_split) == 1: # no ":" in label
pkg, target = native.package_name(), label
else:
pkg2, target = colon_split # fails if len(colon_split) != 2
pkg = native.package_name() + ("/" + pkg2 if pkg2 else "")
return "//%s:%s" % (pkg, target)
def _get_java_package(path):
"""Extract the java package from path"""
segments = path.split("/")
# Find different root start indecies based on potential java roots
java_root_start_indecies = [_find(segments, root) for root in ["java", "javatests"]]
# Choose the root that starts earliest
start_index = min(java_root_start_indecies)
if start_index == len(segments):
fail("Cannot find java root: " + path)
return ".".join(segments[start_index + 1:])
def _find(segments, s):
return segments.index(s) if s in segments else len(segments)