Skip to content

Commit 90e2ea6

Browse files
committed
Don't restrict the 'java' attr to a single file. This means that developers can create a java_runtime from a build target by constructing an appropriately laid-out jdk in a custom rule, and then point to that rule in the java attr.
1 parent 3f93d37 commit 90e2ea6

File tree

2 files changed

+90
-4
lines changed

2 files changed

+90
-4
lines changed

src/main/starlark/builtins_bzl/common/java/java_runtime.bzl

+1-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def _java_runtime_rule_impl(ctx):
100100
java_binary_exec_path = paths.get_relative(java_home, _get_bin_java(ctx))
101101
java_binary_runfiles_path = _get_runfiles_java_executable(ctx, java_home, ctx.label)
102102

103-
java = ctx.file.java
103+
java = ctx.executable.java.path
104104
if java:
105105
if paths.is_absolute(java_home):
106106
fail("'java_home' with an absolute path requires 'java' to be empty.")
@@ -204,7 +204,6 @@ The libraries that are statically linked with the launcher for hermetic deployme
204204
""",
205205
),
206206
"java": attr.label(
207-
allow_single_file = True,
208207
executable = True,
209208
cfg = "target",
210209
doc = """

src/test/shell/integration/bazel_java_test.sh

+89-2
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ EOF
336336

337337
# Test the genrule that specifically depends on :bar_runtime.
338338
bazel cquery --max_config_changes_to_show=0 --implicit_deps 'deps(//:with_java)' >& $TEST_log
339-
expect_not_log "foo"
340-
expect_log "bar"
339+
expect_log "dummy_java"
340+
expect_log "dummy_javac"
341341
expect_not_log "embedded_jdk"
342342
expect_not_log "remotejdk_"
343343
expect_not_log "remotejdk11_"
@@ -353,4 +353,91 @@ EOF
353353
expect_not_log "remotejdk11_"
354354
}
355355

356+
357+
function test_fully_generated_jdk() {
358+
cat << EOF >> WORKSPACE
359+
load("@bazel_tools//tools/jdk:local_java_repository.bzl", "local_java_repository")
360+
local_java_repository(
361+
name = "foo_javabase",
362+
java_home = "$PWD/foo",
363+
version = "11",
364+
)
365+
EOF
366+
367+
mkdir -p foo/bin bar/bin
368+
369+
cat << EOF > defs.bzl
370+
371+
def _make_jdk_impl(ctx):
372+
dummy_java = ctx.actions.declare_file(ctx.attr.name + "_dummy_java")
373+
ctx.actions.write(dummy_java, "dummy_java", is_executable = True)
374+
dummy_javac = ctx.actions.declare_file(ctx.attr.name + "_dummy_javac")
375+
ctx.actions.write(dummy_javac, "dummy_javac", is_executable = True)
376+
377+
return [DefaultInfo(
378+
executable = dummy_java,
379+
files = depset([dummy_java, dummy_javac])
380+
)]
381+
382+
make_jdk = rule(implementation = _make_jdk_impl, executable = True)
383+
384+
EOF
385+
386+
cat << EOF > BUILD
387+
load(":defs.bzl", "make_jdk")
388+
389+
make_jdk(name = "generated_jdk")
390+
391+
java_runtime(
392+
name = "bar_runtime",
393+
visibility = ["//visibility:public"],
394+
executable = "generated_jdk",
395+
)
396+
397+
genrule(
398+
name = "without_java",
399+
srcs = ["in"],
400+
outs = ["out_without"],
401+
cmd = "cat \$(SRCS) > \$(OUTS)",
402+
)
403+
404+
genrule(
405+
name = "with_java",
406+
srcs = ["in"],
407+
outs = ["out_with"],
408+
cmd = "echo \$(JAVA) > \$(OUTS)",
409+
toolchains = [":bar_runtime"],
410+
)
411+
EOF
412+
413+
# Use --max_config_changes_to_show=0, as changed option names may otherwise
414+
# erroneously match the expected regexes.
415+
416+
# Test the genrule with no java dependencies.
417+
bazel cquery --max_config_changes_to_show=0 --implicit_deps 'deps(//:without_java)' >& $TEST_log
418+
expect_not_log "dummy_java"
419+
expect_not_log "dummy_javac"
420+
expect_not_log "embedded_jdk"
421+
expect_not_log "remotejdk_"
422+
expect_not_log "remotejdk11_"
423+
424+
# Test the genrule that specifically depends on :bar_runtime.
425+
bazel cquery --max_config_changes_to_show=0 --implicit_deps 'deps(//:with_java)' >& $TEST_log
426+
expect_log "dummy_java"
427+
expect_log "dummy_javac"
428+
expect_not_log "embedded_jdk"
429+
expect_not_log "remotejdk_"
430+
expect_not_log "remotejdk11_"
431+
432+
# Setting the javabase should not change the use of :bar_runtime from the
433+
# roolchains attribute.
434+
bazel cquery --max_config_changes_to_show=0 --implicit_deps 'deps(//:with_java)' \
435+
--tool_java_runtime_version=foo_javabase >& $TEST_log
436+
expect_log "dummy_java"
437+
expect_log "dummy_javac"
438+
expect_not_log "embedded_jdk"
439+
expect_not_log "remotejdk_"
440+
expect_not_log "remotejdk11_"
441+
}
442+
356443
run_suite "Tests of specifying custom server_javabase/host_javabase and javabase."

0 commit comments

Comments
 (0)