Skip to content

Commit 8b95028

Browse files
hvadehrarules_java Copybara
authored andcommitted
Add a test for java_common.compile() with a custom bootclasspath
PiperOrigin-RevId: 729519483 Change-Id: I75af66009265c26624a4846974807c279f576c8d
1 parent 2ffef4a commit 8b95028

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

java/test/common/java_common_tests.bzl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ load("//java/common:java_plugin_info.bzl", "JavaPluginInfo")
99
load("//java/test/testutil:java_info_subject.bzl", "java_info_subject")
1010
load("//java/test/testutil:rules/custom_library.bzl", "custom_library")
1111
load("//java/test/testutil:rules/custom_library_extended_compile_jdeps.bzl", "CompileJdepsInfo", "custom_library_extended_jdeps")
12+
load("//java/test/testutil:rules/custom_library_with_bootclasspath.bzl", "custom_bootclasspath", "custom_library_with_bootclasspath")
1213
load("//java/test/testutil:rules/custom_library_with_exports.bzl", "custom_library_with_exports")
1314
load("//java/test/testutil:rules/custom_library_with_sourcepaths.bzl", "custom_library_with_sourcepaths")
1415

@@ -161,6 +162,41 @@ def _test_compile_extend_compile_time_jdeps_rule_outputs_impl(env, targets):
161162
"{}/Baz.jdeps".format(baz.label.package),
162163
])
163164

165+
def _test_compile_bootclasspath(name):
166+
util.helper_target(
167+
custom_bootclasspath,
168+
name = name + "/bootclasspath",
169+
srcs = [
170+
"custom-system/lib/jrt-fs.jar",
171+
"custom-system/lib/modules",
172+
"custom-system/release",
173+
],
174+
)
175+
util.helper_target(
176+
custom_library_with_bootclasspath,
177+
name = name + "/custom",
178+
srcs = ["Main.java"],
179+
bootclasspath = name + "/bootclasspath",
180+
sourcepath = [":B.jar"],
181+
)
182+
183+
analysis_test(
184+
name = name,
185+
impl = _test_compile_bootclasspath_impl,
186+
target = name + "/custom",
187+
attr_values = {"tags": ["min_bazel_7"]},
188+
)
189+
190+
def _test_compile_bootclasspath_impl(env, target):
191+
assert_that_javac = env.expect.that_target(target).action_generating(
192+
target[JavaInfo].java_outputs[0].class_jar.short_path,
193+
)
194+
195+
assert_that_javac.contains_flag_values([(
196+
"--system",
197+
"{}/custom-system".format(target.label.package),
198+
)])
199+
164200
def java_common_tests(name):
165201
test_suite(
166202
name = name,
@@ -171,5 +207,6 @@ def java_common_tests(name):
171207
_test_java_plugin_info,
172208
_test_compile_extend_compile_time_jdeps,
173209
_test_compile_extend_compile_time_jdeps_rule_outputs,
210+
_test_compile_bootclasspath,
174211
],
175212
)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Helper rule to test custom bootclasspaths in java_common.compile()"""
2+
3+
load("//java/common:java_common.bzl", "java_common")
4+
load("//java/common:java_semantics.bzl", "semantics")
5+
6+
def _bootclasspath(ctx):
7+
files = ctx.files.srcs
8+
return [java_common.BootClassPathInfo(bootclasspath = files, system = files)]
9+
10+
custom_bootclasspath = rule(
11+
implementation = _bootclasspath,
12+
attrs = {
13+
"srcs": attr.label_list(allow_files = True),
14+
},
15+
)
16+
17+
def _impl(ctx):
18+
output_jar = ctx.actions.declare_file("lib" + ctx.label.name + ".jar")
19+
compilation_provider = java_common.compile(
20+
ctx,
21+
source_files = ctx.files.srcs,
22+
output = output_jar,
23+
deps = [],
24+
sourcepath = ctx.files.sourcepath,
25+
strict_deps = "ERROR",
26+
java_toolchain = semantics.find_java_toolchain(ctx),
27+
bootclasspath = ctx.attr.bootclasspath[java_common.BootClassPathInfo],
28+
)
29+
return [
30+
DefaultInfo(files = depset([output_jar])),
31+
compilation_provider,
32+
]
33+
34+
custom_library_with_bootclasspath = rule(
35+
implementation = _impl,
36+
outputs = {
37+
"my_output": "lib%{name}.jar",
38+
},
39+
attrs = {
40+
"srcs": attr.label_list(allow_files = [".java"]),
41+
"sourcepath": attr.label_list(allow_files = [".jar"]),
42+
"bootclasspath": attr.label(),
43+
},
44+
toolchains = [semantics.JAVA_TOOLCHAIN_TYPE],
45+
fragments = ["java"],
46+
)

0 commit comments

Comments
 (0)