Skip to content

Commit

Permalink
Change plugins to use depsets, as opposed to lists. (bazelbuild#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
jongerrish authored Feb 28, 2020
1 parent a7fa973 commit 97d984c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
23 changes: 14 additions & 9 deletions kotlin/internal/jvm/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ load(
load(
"//kotlin/internal/jvm:plugins.bzl",
_merge_plugin_infos = "merge_plugin_infos",
_plugin_mappers = "mappers",
)
load(
"//kotlin/internal/utils:utils.bzl",
Expand Down Expand Up @@ -196,15 +197,19 @@ def kt_jvm_compile_action(ctx, rule_kind, output_jar):
args.add_all("--source_jars", srcs.src_jars, omit_if_empty = True)

# Collect and prepare plugin descriptor for the worker.
plugin_info = _merge_plugin_infos(ctx.attr.plugins + ctx.attr.deps)
if len(plugin_info.annotation_processors) > 0:
processors = []
processorpath = []
for p in plugin_info.annotation_processors:
processors += [p.processor_class]
processorpath += p.classpath
args.add_all("--processors", processors)
args.add_all("--processorpath", processorpath)
plugins = _plugin_mappers.targets_to_kt_plugins(ctx.attr.plugins + ctx.attr.deps)
args.add_all(
"--processors",
plugins,
map_each = _plugin_mappers.kt_plugin_to_processor,
omit_if_empty = True,
)
args.add_all(
"--processorpath",
plugins,
map_each = _plugin_mappers.kt_plugin_to_processorpath,
omit_if_empty = True,
)

progress_message = "Compiling Kotlin to JVM %s { kt: %d, java: %d, srcjars: %d }" % (
ctx.label,
Expand Down
36 changes: 23 additions & 13 deletions kotlin/internal/jvm/plugins.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,50 @@ load(
KtJvmPluginInfo = provider(
doc = "This provider contains the plugin info for the JVM aspect",
fields = {
"annotation_processors": "a serializeable list of structs containing annotation processor definitions",
"annotation_processors": "depset of structs containing annotation processor definitions",
},
)

_EMPTY_PLUGIN_INFO = [KtJvmPluginInfo(annotation_processors = [])]
_EMPTY_PLUGIN_INFO = [KtJvmPluginInfo(annotation_processors = depset())]

# Mapping functions for args.add_all.
# These preserve the transitive depsets until needed.
def _kt_plugin_to_processor(processor):
return processor.processor_class

def _kt_plugin_to_processorpath(processor):
return [j.path for j in processor.classpath.to_list()]

def _targets_to_kt_plugins(targets):
return depset(transitive = [t[KtJvmPluginInfo].annotation_processors for t in targets if t[KtJvmPluginInfo]])

mappers = struct(
targets_to_kt_plugins = _targets_to_kt_plugins,
kt_plugin_to_processor = _kt_plugin_to_processor,
kt_plugin_to_processorpath = _kt_plugin_to_processorpath,
)

def merge_plugin_infos(attrs):
"""Merge all of the plugin infos found in the provided sequence of attributes.
Returns:
A KtJvmPluginInfo provider, Each of the entries is serializable."""
tally = {}
annotation_processors = []
for info in [a[KtJvmPluginInfo] for a in attrs]:
for p in info.annotation_processors:
if p.label not in tally:
tally[p.label] = True
annotation_processors.append(p)
return KtJvmPluginInfo(
annotation_processors = annotation_processors,
annotation_processors = _targets_to_kt_plugins(attrs),
)

def _kt_jvm_plugin_aspect_impl(target, ctx):
if ctx.rule.kind == "java_plugin":
processor = ctx.rule.attr
merged_deps = java_common.merge([j[JavaInfo] for j in processor.deps])
return [KtJvmPluginInfo(
annotation_processors = [
annotation_processors = depset([
struct(
label = _utils.restore_label(ctx.label),
processor_class = processor.processor_class,
classpath = [cp.path for cp in merged_deps.transitive_runtime_jars.to_list()],
classpath = merged_deps.transitive_runtime_jars,
generates_api = processor.generates_api,
),
],
]),
)]
elif ctx.rule.kind == "java_library":
return [merge_plugin_infos(ctx.rule.attr.exported_plugins)]
Expand Down

0 comments on commit 97d984c

Please sign in to comment.