Skip to content

Commit 4728e7d

Browse files
committed
Add neverlink and export support.
1 parent 5f6e0f2 commit 4728e7d

File tree

3 files changed

+102
-59
lines changed

3 files changed

+102
-59
lines changed

src/main/starlark/core/compile/rules.bzl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ _COMMON_ATTRS = {
1717
],
1818
allow_files = False,
1919
),
20+
"exports": attr.label_list(
21+
doc = """\
22+
Exported libraries.
23+
24+
Deps listed here will be made available to other rules, as if the parents explicitly depended on
25+
these deps. This is not true for regular (non-exported) deps.""",
26+
default = [],
27+
providers = [JavaInfo, KtJvmInfo],
28+
),
29+
"neverlink": attr.bool(
30+
doc = """If true only use this library for compilation and not at runtime.""",
31+
default = False,
32+
),
2033
"runtime_deps": attr.label_list(
2134
doc = """Libraries to make available to the final binary or test at runtime only. Like ordinary deps, these will
2235
appear on the runtime classpath, but unlike them, not on the compile-time classpath.""",
@@ -66,6 +79,8 @@ def _kt_jvm_library_impl(ctx):
6679
output_jar = class_jar,
6780
source_jar = source_jar,
6881
deps = java_info_deps,
82+
neverlink = ctx.attr.neverlink,
83+
exports = [e[JavaInfo] for e in ctx.attr.exports],
6984
)
7085
return [
7186
KtJvmInfo(

src/test/starlark/compile/common_tests.bzl

Lines changed: 86 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,49 @@ def _outputs(env, got):
1919
got_java_info = got_target.provider(JavaInfo, java_info_subject_factory)
2020
got_java_info.java_outputs().transform(**map_to("class_jar")).contains(env.ctx.file.class_jar)
2121

22-
want_jars = [
22+
want_compile_jars = [
2323
j
24-
for d in env.ctx.attr.transitive_deps
24+
for d in env.ctx.attr.transitive_compile_deps
2525
if JavaInfo in d
2626
for j in d[JavaInfo].compile_jars.to_list()
2727
]
2828

29-
got_java_info.transitive_compile_time_jars().contains_at_least(want_jars)
29+
want_runtime_jars = [
30+
j
31+
for d in env.ctx.attr.transitive_runtime_deps
32+
if JavaInfo in d
33+
for j in d[JavaInfo].transitive_runtime_jars.to_list()
34+
]
35+
36+
got_java_info.transitive_compile_time_jars().contains_exactly([j.short_path for j in want_compile_jars])
37+
got_java_info.transitive_runtime_jars().contains_exactly([j.short_path for j in want_runtime_jars])
3038
got_java_info.source_jars().contains(env.ctx.file.source_jar)
3139

3240
# get actions
3341
source = got_target.action_generating(env.ctx.file.source_jar.short_path)
3442

3543
compile = got_target.action_generating(env.ctx.file.class_jar.short_path)
36-
compile.contains_at_least_inputs(want_jars)
44+
compile.contains_at_least_inputs(
45+
[i[JavaInfo].compile_jars.to_list() for i in env.ctx.attr.inputs if JavaInfo in i] + env.ctx.files.inputs,
46+
)
3747

38-
def test_deps_core_kt_jvm_library(test):
48+
def _test_neverlink_deps(test, rule_under_test, **kwargs):
3949
have = test.have(
40-
core_kt_jvm_library,
50+
rule_under_test,
4151
name = "have",
42-
srcs = [
43-
test.artifact("hold.kt"),
44-
],
45-
deps = [
46-
],
52+
srcs = [test.artifact("hold.kt")],
53+
deps = [],
54+
neverlink = True,
55+
**kwargs
4756
)
4857

58+
got_src = test.artifact("gave.kt")
4959
got = test.got(
50-
core_kt_jvm_library,
60+
rule_under_test,
5161
name = "got",
52-
srcs = [
53-
test.artifact("gave.kt"),
54-
],
55-
deps = [
56-
have,
57-
],
62+
srcs = [got_src],
63+
deps = [have],
64+
**kwargs
5865
)
5966
test.claim(
6067
got = got,
@@ -68,35 +75,43 @@ def test_deps_core_kt_jvm_library(test):
6875
attr = attr.label(allow_single_file = True),
6976
value = got + "lib.srcjar",
7077
),
71-
"transitive_deps": Want(
78+
"inputs": Want(
79+
attr = attr.label_list(allow_empty = True, allow_files = True),
80+
value = [got_src, have + "lib.jar"],
81+
),
82+
"transitive_compile_deps": Want(
7283
attr = attr.label_list(providers = [[JavaInfo], [KtJvmInfo]]),
73-
value = [
74-
have,
75-
],
84+
value = [got, have],
85+
),
86+
"transitive_runtime_deps": Want(
87+
attr = attr.label_list(providers = [[JavaInfo], [KtJvmInfo]]),
88+
value = [got],
7689
),
7790
},
7891
)
7992

80-
def test_deps_core_kt_jvm_binary(test):
93+
def _test_deps_core(test, rule_under_test, **kwargs):
8194
have = test.have(
82-
core_kt_jvm_binary,
95+
rule_under_test,
8396
name = "have",
8497
srcs = [
8598
test.artifact("hold.kt"),
8699
],
87100
deps = [
88101
],
102+
**kwargs
89103
)
90104

91105
got = test.got(
92-
core_kt_jvm_binary,
106+
rule_under_test,
93107
name = "got",
94108
srcs = [
95109
test.artifact("gave.kt"),
96110
],
97111
deps = [
98112
have,
99113
],
114+
**kwargs
100115
)
101116
test.claim(
102117
got = got,
@@ -110,53 +125,35 @@ def test_deps_core_kt_jvm_binary(test):
110125
attr = attr.label(allow_single_file = True),
111126
value = got + "lib.srcjar",
112127
),
113-
"transitive_deps": Want(
128+
"inputs": Want(
129+
attr = attr.label_list(allow_empty = True, allow_files = True),
130+
value = [have + "lib.jar"],
131+
),
132+
"transitive_compile_deps": Want(
114133
attr = attr.label_list(providers = [[JavaInfo], [KtJvmInfo]]),
115134
value = [
116135
have,
136+
got,
117137
],
118138
),
119-
},
120-
)
121-
122-
def test_no_deps_core_kt_jvm_library(test):
123-
got = test.got(
124-
core_kt_jvm_library,
125-
name = "got",
126-
srcs = [
127-
test.artifact("a.kt"),
128-
],
129-
deps = [
130-
],
131-
)
132-
test.claim(
133-
got = got,
134-
what = _outputs,
135-
wants = {
136-
"class_jar": Want(
137-
attr = attr.label(allow_single_file = True),
138-
value = got + "lib.jar",
139-
),
140-
"source_jar": Want(
141-
attr = attr.label(allow_single_file = True),
142-
value = got + "lib.srcjar",
143-
),
144-
"transitive_deps": Want(
139+
"transitive_runtime_deps": Want(
145140
attr = attr.label_list(providers = [[JavaInfo], [KtJvmInfo]]),
146-
value = [],
141+
value = [got],
147142
),
148143
},
149144
)
150145

151-
def test_no_deps_core_kt_jvm_binary(test):
146+
def test_no_deps_core(test, rule_under_test, **kwargs):
147+
have_src = test.artifact("a.kt")
152148
got = test.got(
153-
core_kt_jvm_binary,
149+
rule_under_test,
154150
name = "got",
155151
srcs = [
156-
test.artifact("a.kt"),
152+
have_src,
157153
],
158154
deps = [
159155
],
156+
**kwargs
160157
)
161158
test.claim(
162159
got = got,
@@ -170,16 +167,46 @@ def test_no_deps_core_kt_jvm_binary(test):
170167
attr = attr.label(allow_single_file = True),
171168
value = got + "lib.srcjar",
172169
),
173-
"transitive_deps": Want(
170+
"transitive_compile_deps": Want(
171+
attr = attr.label_list(providers = [[JavaInfo], [KtJvmInfo]]),
172+
value = [got],
173+
),
174+
"transitive_runtime_deps": Want(
174175
attr = attr.label_list(providers = [[JavaInfo], [KtJvmInfo]]),
175-
value = [],
176+
value = [got],
177+
),
178+
"inputs": Want(
179+
attr = attr.label_list(allow_empty = True, allow_files = True),
180+
value = [have_src],
176181
),
177182
},
178183
)
179184

185+
def test_deps_core_kt_jvm_binary(test):
186+
_test_deps_core(test, core_kt_jvm_binary, main_class = "Foo")
187+
188+
def test_neverlink_deps_core_kt_jvm_binary(test):
189+
_test_neverlink_deps(test, core_kt_jvm_binary, main_class = "Foo")
190+
191+
def test_no_deps_core_kt_jvm_binary(test):
192+
test_no_deps_core(test, core_kt_jvm_binary, main_class = "Foo")
193+
194+
def test_deps_core_kt_jvm_library(test):
195+
_test_deps_core(test, core_kt_jvm_library)
196+
197+
def test_neverlink_deps_core_kt_jvm_library(test):
198+
_test_neverlink_deps(test, core_kt_jvm_library)
199+
200+
def test_no_deps_core_kt_jvm_library(test):
201+
test_no_deps_core(test, core_kt_jvm_library)
202+
180203
def test_suite(name):
181204
suite(
182205
name,
183-
test_no_deps_core_kt_jvm_library,
206+
test_deps_core_kt_jvm_binary,
184207
test_deps_core_kt_jvm_library,
208+
test_neverlink_deps_core_kt_jvm_library,
209+
test_neverlink_deps_core_kt_jvm_binary,
210+
test_no_deps_core_kt_jvm_binary,
211+
test_no_deps_core_kt_jvm_library,
185212
)

src/test/starlark/compile/subjects.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ def java_info_subject_factory(value, meta):
88
"java_outputs": subjects.collection,
99
"source_jars": subjects.collection,
1010
"transitive_compile_time_jars": subjects.depset_file,
11+
"transitive_runtime_jars": subjects.depset_file,
1112
},
1213
)

0 commit comments

Comments
 (0)