Skip to content

Commit 5b593c0

Browse files
authored
Clean up _get_associates creation (#1259)
* Clean up _get_associates creation * More friends into compile deps * More * Add exports * Update associates.bzl
1 parent 4b48424 commit 5b593c0

File tree

2 files changed

+35
-55
lines changed

2 files changed

+35
-55
lines changed

kotlin/internal/jvm/associates.bzl

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,24 @@ load(
2525
_utils = "utils",
2626
)
2727

28-
def _get_associates(ctx):
28+
def _get_associates(ctx, associates):
2929
"""Creates a struct of associates meta data"""
30-
31-
associates = getattr(ctx.attr, "associates", [])
32-
if not bool(associates):
30+
if not associates:
3331
return struct(
3432
targets = [],
3533
module_name = _utils.derive_module_name(ctx),
36-
jars = [],
34+
jars = depset(),
3735
)
3836
elif ctx.attr.module_name:
39-
fail("if associates have been set then module_name cannot be provided")
37+
fail("If associates have been set then module_name cannot be provided")
4038
else:
41-
jars = [depset([a], transitive = a[_KtJvmInfo].module_jars) for a in associates]
42-
module_names = _sets.copy_of([x[_KtJvmInfo].module_name for x in associates])
39+
jars = []
40+
module_names = []
41+
for a in associates:
42+
jars.append(depset(transitive = [a[JavaInfo].compile_jars, a[_KtJvmInfo].module_jars]))
43+
module_names.append(a[_KtJvmInfo].module_name)
44+
module_names = list(_sets.copy_of(module_names))
45+
4346
if len(module_names) > 1:
4447
fail("Dependencies from several different kotlin modules cannot be associated. " +
4548
"Associates can see each other's \"internal\" members, and so must only be " +
@@ -48,26 +51,10 @@ def _get_associates(ctx):
4851
# This should be impossible
4952
fail("Error in rules - a KtJvmInfo was found which did not have a module_name")
5053
return struct(
51-
targets = associates,
52-
jars = jars,
53-
module_name = list(module_names)[0],
54+
jars = depset(transitive = jars),
55+
module_name = module_names[0],
5456
)
5557

56-
def _flatten_jars(nested_jars_depset):
57-
"""Returns a list of strings containing the compile_jars for depset of targets.
58-
59-
This ends up unwinding the nesting of depsets, since compile_jars contains depsets inside
60-
the nested_jars targets, which themselves are depsets. This function is intended to be called
61-
lazily form within Args.add_all(map_each) as it collapses depsets.
62-
"""
63-
compile_jars_depsets = [
64-
target[JavaInfo].compile_jars
65-
for target in nested_jars_depset.to_list()
66-
if target[JavaInfo].compile_jars
67-
]
68-
return [file.path for file in depset(transitive = compile_jars_depsets).to_list()]
69-
7058
associate_utils = struct(
7159
get_associates = _get_associates,
72-
flatten_jars = _flatten_jars,
7360
)

kotlin/internal/jvm/compile.bzl

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,20 @@ def _compiler_toolchains(ctx):
102102
java_runtime = find_java_runtime_toolchain(ctx, ctx.attr._host_javabase),
103103
)
104104

105-
def _jvm_deps(ctx, toolchains, associated_targets, deps, runtime_deps = []):
105+
def _jvm_deps(ctx, toolchains, associate_deps, deps, exports = [], runtime_deps = []):
106106
"""Encapsulates jvm dependency metadata."""
107107
diff = _sets.intersection(
108-
_sets.copy_of([x.label for x in associated_targets]),
108+
_sets.copy_of([x.label for x in associate_deps]),
109109
_sets.copy_of([x.label for x in deps]),
110110
)
111111
if diff:
112112
fail(
113113
"\n------\nTargets should only be put in associates= or deps=, not both:\n%s" %
114114
",\n ".join([" %s" % x for x in list(diff)]),
115115
)
116-
dep_infos = [_java_info(d) for d in associated_targets + deps] + [toolchains.kt.jvm_stdlibs]
116+
dep_infos = [_java_info(d) for d in associate_deps + deps] + [toolchains.kt.jvm_stdlibs]
117+
118+
associates = _associate_utils.get_associates(ctx, associates = associate_deps)
117119

118120
# Reduced classpath, exclude transitive deps from compilation
119121
if (toolchains.kt.experimental_prune_transitive_deps and
@@ -132,10 +134,11 @@ def _jvm_deps(ctx, toolchains, associated_targets, deps, runtime_deps = []):
132134
]
133135

134136
return struct(
137+
module_name = associates.module_name,
135138
deps = dep_infos,
136-
compile_jars = depset(
137-
transitive = transitive,
138-
),
139+
exports = [_java_info(d) for d in exports],
140+
associate_jars = associates.jars,
141+
compile_jars = depset(transitive = transitive),
139142
runtime_deps = [_java_info(d) for d in runtime_deps],
140143
)
141144

@@ -378,7 +381,6 @@ def _run_kapt_builder_actions(
378381
rule_kind,
379382
toolchains,
380383
srcs,
381-
associates,
382384
compile_deps,
383385
deps_artifacts,
384386
annotation_processors,
@@ -398,7 +400,6 @@ def _run_kapt_builder_actions(
398400
toolchains = toolchains,
399401
srcs = srcs,
400402
generated_src_jars = [],
401-
associates = associates,
402403
compile_deps = compile_deps,
403404
deps_artifacts = deps_artifacts,
404405
annotation_processors = annotation_processors,
@@ -424,7 +425,6 @@ def _run_ksp_builder_actions(
424425
rule_kind,
425426
toolchains,
426427
srcs,
427-
associates,
428428
compile_deps,
429429
deps_artifacts,
430430
annotation_processors,
@@ -443,7 +443,6 @@ def _run_ksp_builder_actions(
443443
toolchains = toolchains,
444444
srcs = srcs,
445445
generated_src_jars = [],
446-
associates = associates,
447446
compile_deps = compile_deps,
448447
deps_artifacts = deps_artifacts,
449448
annotation_processors = annotation_processors,
@@ -464,7 +463,6 @@ def _run_kt_builder_action(
464463
toolchains,
465464
srcs,
466465
generated_src_jars,
467-
associates,
468466
compile_deps,
469467
deps_artifacts,
470468
annotation_processors,
@@ -477,7 +475,7 @@ def _run_kt_builder_action(
477475
kotlinc_options = ctx.attr.kotlinc_opts[KotlincOptions] if ctx.attr.kotlinc_opts else toolchains.kt.kotlinc_options
478476
javac_options = ctx.attr.javac_opts[JavacOptions] if ctx.attr.javac_opts else toolchains.kt.javac_options
479477

480-
args = _utils.init_args(ctx, rule_kind, associates.module_name, kotlinc_options)
478+
args = _utils.init_args(ctx, rule_kind, compile_deps.module_name, kotlinc_options)
481479

482480
for f, path in outputs.items():
483481
args.add("--" + f, path)
@@ -492,7 +490,7 @@ def _run_kt_builder_action(
492490
args.add_all("--sources", srcs.all_srcs, omit_if_empty = True)
493491
args.add_all("--source_jars", srcs.src_jars + generated_src_jars, omit_if_empty = True)
494492
args.add_all("--deps_artifacts", deps_artifacts, omit_if_empty = True)
495-
args.add_all("--kotlin_friend_paths", associates.jars, map_each = _associate_utils.flatten_jars)
493+
args.add_all("--kotlin_friend_paths", compile_deps.associate_jars, omit_if_empty = True)
496494
args.add("--instrument_coverage", ctx.coverage_instrumented())
497495

498496
# Collect and prepare plugin descriptor for the worker.
@@ -593,21 +591,21 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
593591
"""
594592
toolchains = _compiler_toolchains(ctx)
595593
srcs = _partitioned_srcs(ctx.files.srcs)
596-
associates = _associate_utils.get_associates(ctx)
597594
compile_deps = _jvm_deps(
598595
ctx,
599-
toolchains,
600-
associates.targets,
596+
toolchains = toolchains,
597+
associate_deps = ctx.attr.associates,
601598
deps = ctx.attr.deps,
602-
runtime_deps = ctx.attr.runtime_deps,
599+
exports = getattr(ctx.attr, "exports", []),
600+
runtime_deps = getattr(ctx.attr, "runtime_deps", []),
603601
)
604602

605603
annotation_processors = _plugin_mappers.targets_to_annotation_processors(ctx.attr.plugins + ctx.attr.deps)
606604
ksp_annotation_processors = _plugin_mappers.targets_to_ksp_annotation_processors(ctx.attr.plugins + ctx.attr.deps)
607605
transitive_runtime_jars = _plugin_mappers.targets_to_transitive_runtime_jars(ctx.attr.plugins + ctx.attr.deps)
608606
plugins = _new_plugins_from(ctx.attr.plugins + _exported_plugins(deps = ctx.attr.deps))
609607

610-
deps_artifacts = _deps_artifacts(toolchains, ctx.attr.deps + associates.targets)
608+
deps_artifacts = _deps_artifacts(toolchains, ctx.attr.deps + ctx.attr.associates)
611609

612610
generated_src_jars = []
613611
annotation_processing = None
@@ -623,7 +621,6 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
623621
srcs = srcs,
624622
generated_kapt_src_jars = [],
625623
generated_ksp_src_jars = [],
626-
associates = associates,
627624
compile_deps = compile_deps,
628625
deps_artifacts = deps_artifacts,
629626
annotation_processors = annotation_processors,
@@ -674,8 +671,8 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
674671
source_jar = source_jar,
675672
jdeps = output_jdeps,
676673
deps = compile_deps.deps,
677-
runtime_deps = [_java_info(d) for d in ctx.attr.runtime_deps],
678-
exports = [_java_info(d) for d in getattr(ctx.attr, "exports", [])],
674+
runtime_deps = compile_deps.runtime_deps,
675+
exports = compile_deps.exports,
679676
neverlink = getattr(ctx.attr, "neverlink", False),
680677
generated_source_jar = generated_source_jar,
681678
)
@@ -692,8 +689,8 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
692689
instrumented_files = instrumented_files,
693690
kt = _KtJvmInfo(
694691
srcs = ctx.files.srcs,
695-
module_name = associates.module_name,
696-
module_jars = associates.jars,
692+
module_name = compile_deps.module_name,
693+
module_jars = compile_deps.associate_jars,
697694
language_version = toolchains.kt.api_version,
698695
exported_compiler_plugins = _collect_plugins_for_export(
699696
getattr(ctx.attr, "exported_compiler_plugins", []),
@@ -723,7 +720,6 @@ def _run_kt_java_builder_actions(
723720
srcs,
724721
generated_kapt_src_jars,
725722
generated_ksp_src_jars,
726-
associates,
727723
compile_deps,
728724
deps_artifacts,
729725
annotation_processors,
@@ -749,7 +745,6 @@ def _run_kt_java_builder_actions(
749745
rule_kind = rule_kind,
750746
toolchains = toolchains,
751747
srcs = srcs,
752-
associates = associates,
753748
compile_deps = compile_deps,
754749
deps_artifacts = deps_artifacts,
755750
annotation_processors = annotation_processors,
@@ -773,7 +768,6 @@ def _run_kt_java_builder_actions(
773768
rule_kind = rule_kind,
774769
toolchains = toolchains,
775770
srcs = srcs,
776-
associates = associates,
777771
compile_deps = compile_deps,
778772
deps_artifacts = deps_artifacts,
779773
annotation_processors = ksp_annotation_processors,
@@ -810,7 +804,6 @@ def _run_kt_java_builder_actions(
810804
toolchains = toolchains,
811805
srcs = srcs,
812806
generated_src_jars = generated_kapt_src_jars + generated_ksp_src_jars,
813-
associates = associates,
814807
compile_deps = compile_deps,
815808
deps_artifacts = deps_artifacts,
816809
annotation_processors = [],
@@ -831,8 +824,8 @@ def _run_kt_java_builder_actions(
831824
compile_jar = kt_compile_jar,
832825
jdeps = kt_jdeps,
833826
deps = compile_deps.deps,
834-
runtime_deps = [d[JavaInfo] for d in ctx.attr.runtime_deps],
835-
exports = [d[JavaInfo] for d in getattr(ctx.attr, "exports", [])],
827+
runtime_deps = compile_deps.runtime_deps,
828+
exports = compile_deps.exports,
836829
neverlink = getattr(ctx.attr, "neverlink", False),
837830
)
838831
java_infos.append(kt_java_info)

0 commit comments

Comments
 (0)