Skip to content

Commit 28d6379

Browse files
authored
Fix associates compile classpath (#1271)
* Fix assoicate compile classpath * Maybe invalidate build cache * Add associate jars as input * Use class not compile jar in associates * Collect strict class jar only * Remove unused classpath entry * Add test for classpath order extracting fun under test * Flush and yield in test * Tidy ABI classpath test * Filter any abi jar from associates from classpath
1 parent 5219847 commit 28d6379

File tree

6 files changed

+313
-41
lines changed

6 files changed

+313
-41
lines changed

kotlin/internal/jvm/associates.bzl

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,56 @@ def _collect_associates(ctx, toolchains, associate):
2929
"""Collects the associate jars from the provided dependency and returns
3030
them as a depset.
3131
32-
There are two outcomes for this marco:
33-
1. When `experimental_strict_associate_dependencies` is enabled and the tag override has not been provided, only the
34-
direct java_output compile jars will be collected for each associate target.
35-
2. When `experimental_strict_associate_dependencies` is disabled, the complete transitive set of compile jars will
32+
There are three outcomes for this marco:
33+
1. When `experimental_remove_private_classes_in_abi_jars` is enabled and the tag override has not been provided, only the
34+
direct java_output CLASS jars will be collected for each associate target. Due to the stripping of internal and private
35+
symbols from the compile jar, class jar is the only one that will contain the internal symbols an associate has access to.
36+
2. When `experimental_strict_associate_dependencies` is enabled and the tag override has not been provided, only the
37+
direct java_output COMPILE jars will be collected for each associate target.
38+
3. When `experimental_strict_associate_dependencies` is disabled, the complete transitive set of compile jars will
3639
be collected for each assoicate target.
3740
"""
3841
jars_depset = None
39-
if (toolchains.kt.experimental_strict_associate_dependencies and
40-
"kt_experimental_strict_associate_dependencies_incompatible" not in ctx.attr.tags):
42+
abi_jars_set = _sets.new()
43+
if (not "kt_remove_private_classes_in_abi_plugin_incompatible" in ctx.attr.tags and
44+
toolchains.kt.experimental_remove_private_classes_in_abi_jars):
45+
jars_depset = depset(direct = [a.class_jar for a in associate[JavaInfo].java_outputs])
46+
_sets.add_all(abi_jars_set, [a.compile_jar for a in associate[JavaInfo].java_outputs])
47+
elif (toolchains.kt.experimental_strict_associate_dependencies and
48+
"kt_experimental_strict_associate_dependencies_incompatible" not in ctx.attr.tags):
4149
jars_depset = depset(direct = [a.compile_jar for a in associate[JavaInfo].java_outputs])
4250
else:
4351
jars_depset = depset(transitive = [associate[JavaInfo].compile_jars])
44-
return jars_depset
52+
return struct(
53+
jars = jars_depset,
54+
abi_jars_set = abi_jars_set,
55+
)
56+
57+
def _java_info(target):
58+
return target[JavaInfo] if JavaInfo in target else None
4559

4660
def _get_associates(ctx, toolchains, associates):
4761
"""Creates a struct of associates meta data"""
4862
if not associates:
4963
return struct(
5064
module_name = _utils.derive_module_name(ctx),
5165
jars = depset(),
66+
abi_jar_set = _sets.new(),
67+
dep_infos = [],
5268
)
5369
elif ctx.attr.module_name:
5470
fail("If associates have been set then module_name cannot be provided")
5571
else:
5672
jars = []
73+
abi_jar_set = {}
5774
module_names = []
75+
java_infos = []
5876
for a in associates:
59-
jars.append(_collect_associates(ctx = ctx, toolchains = toolchains, associate = a))
77+
jar_bundle = _collect_associates(ctx = ctx, toolchains = toolchains, associate = a)
78+
jars.append(jar_bundle.jars)
79+
abi_jar_set = jar_bundle.abi_jars_set
6080
module_names.append(a[_KtJvmInfo].module_name)
81+
java_infos.append(_java_info(a))
6182
module_names = list(_sets.copy_of(module_names))
6283

6384
if len(module_names) > 1:
@@ -69,7 +90,9 @@ def _get_associates(ctx, toolchains, associates):
6990
fail("Error in rules - a KtJvmInfo was found which did not have a module_name")
7091
return struct(
7192
jars = depset(transitive = jars),
93+
abi_jar_set = abi_jar_set,
7294
module_name = module_names[0],
95+
dep_infos = java_infos,
7396
)
7497

7598
associate_utils = struct(

kotlin/internal/jvm/compile.bzl

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ load(
3333
"kotlinc_options_to_flags",
3434
)
3535
load(
36-
"//kotlin/internal/jvm:associates.bzl",
37-
_associate_utils = "associate_utils",
36+
"//kotlin/internal/jvm:jvm_deps.bzl",
37+
_jvm_deps_utils = "jvm_deps_utils",
3838
)
3939
load(
4040
"//kotlin/internal/jvm:plugins.bzl",
@@ -102,8 +102,8 @@ def _compiler_toolchains(ctx):
102102
java_runtime = find_java_runtime_toolchain(ctx, ctx.attr._host_javabase),
103103
)
104104

105-
def _jvm_deps(ctx, toolchains, associate_deps, deps, exports = [], runtime_deps = []):
106-
"""Encapsulates jvm dependency metadata."""
105+
def _fail_if_invalid_associate_deps(associate_deps, deps):
106+
"""Verifies associates not included in target deps."""
107107
diff = _sets.intersection(
108108
_sets.copy_of([x.label for x in associate_deps]),
109109
_sets.copy_of([x.label for x in deps]),
@@ -113,34 +113,6 @@ def _jvm_deps(ctx, toolchains, associate_deps, deps, exports = [], runtime_deps
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 associate_deps + deps] + [toolchains.kt.jvm_stdlibs]
117-
118-
associates = _associate_utils.get_associates(ctx, toolchains = toolchains, associates = associate_deps)
119-
120-
# Reduced classpath, exclude transitive deps from compilation
121-
if (toolchains.kt.experimental_prune_transitive_deps and
122-
not "kt_experimental_prune_transitive_deps_incompatible" in ctx.attr.tags):
123-
transitive = [
124-
d.compile_jars
125-
for d in dep_infos
126-
]
127-
else:
128-
transitive = [
129-
d.compile_jars
130-
for d in dep_infos
131-
] + [
132-
d.transitive_compile_time_jars
133-
for d in dep_infos
134-
]
135-
136-
return struct(
137-
module_name = associates.module_name,
138-
deps = dep_infos,
139-
exports = [_java_info(d) for d in exports],
140-
associate_jars = associates.jars,
141-
compile_jars = depset(transitive = transitive),
142-
runtime_deps = [_java_info(d) for d in runtime_deps],
143-
)
144116

145117
def _java_infos_to_compile_jars(java_infos):
146118
return depset(transitive = [j.compile_jars for j in java_infos])
@@ -568,6 +540,7 @@ def _run_kt_builder_action(
568540
inputs = depset(
569541
srcs.all_srcs + srcs.src_jars + generated_src_jars,
570542
transitive = [
543+
compile_deps.associate_jars,
571544
compile_deps.compile_jars,
572545
transitive_runtime_jars,
573546
deps_artifacts,
@@ -608,7 +581,8 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
608581
"""
609582
toolchains = _compiler_toolchains(ctx)
610583
srcs = _partitioned_srcs(ctx.files.srcs)
611-
compile_deps = _jvm_deps(
584+
_fail_if_invalid_associate_deps(ctx.attr.associates, ctx.attr.deps)
585+
compile_deps = _jvm_deps_utils.jvm_deps(
612586
ctx,
613587
toolchains = toolchains,
614588
associate_deps = ctx.attr.associates,

kotlin/internal/jvm/jvm_deps.bzl

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright 2018 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
load(
15+
"@rules_java//java:defs.bzl",
16+
"JavaInfo",
17+
)
18+
load("//kotlin/internal/jvm:associates.bzl", _associate_utils = "associate_utils")
19+
load("//kotlin/internal/utils:sets.bzl", _sets = "sets")
20+
21+
def _java_info(target):
22+
return target[JavaInfo] if JavaInfo in target else None
23+
24+
def _jvm_deps(ctx, toolchains, associate_deps, deps, exports = [], runtime_deps = []):
25+
"""Encapsulates jvm dependency metadata."""
26+
dep_infos = [_java_info(d) for d in deps] + [toolchains.kt.jvm_stdlibs]
27+
28+
associates = _associate_utils.get_associates(ctx, toolchains = toolchains, associates = associate_deps)
29+
30+
# Reduced classpath, exclude transitive deps from compilation
31+
if (toolchains.kt.experimental_prune_transitive_deps and
32+
not "kt_experimental_prune_transitive_deps_incompatible" in ctx.attr.tags):
33+
transitive = [
34+
d.compile_jars
35+
for d in dep_infos
36+
]
37+
else:
38+
transitive = [
39+
d.compile_jars
40+
for d in dep_infos
41+
] + [
42+
d.transitive_compile_time_jars
43+
for d in dep_infos
44+
]
45+
46+
compile_depset_list = depset(transitive = transitive + [associates.jars]).to_list()
47+
compile_depset_list_filtered = [jar for jar in compile_depset_list if not _sets.contains(associates.abi_jar_set, jar)]
48+
49+
return struct(
50+
module_name = associates.module_name,
51+
deps = dep_infos + associates.dep_infos,
52+
exports = [_java_info(d) for d in exports],
53+
associate_jars = associates.jars,
54+
compile_jars = depset(direct = compile_depset_list_filtered),
55+
runtime_deps = [_java_info(d) for d in runtime_deps],
56+
)
57+
58+
jvm_deps_utils = struct(
59+
jvm_deps = _jvm_deps,
60+
)

src/test/kotlin/io/bazel/worker/WorkerEnvironmentTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ class WorkerEnvironmentTest {
3535
WorkerProtocol.WorkResponse.newBuilder().setRequestId(req.requestId).build()
3636
.also { println("sent $it") }
3737
.writeDelimitedTo(stdOut)
38+
stdOut.flush()
3839
}
3940
}
4041

4142
writeStdIn(give)
43+
Thread.sleep(10)
4244
closeStdIn()
4345

4446
return@inProcess readStdOut()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load(":jvm_deps_tests.bzl", "jvm_deps_test_suite")
2+
3+
jvm_deps_test_suite(name = "jvm_tests")

0 commit comments

Comments
 (0)