Skip to content

Commit

Permalink
Clean up default_java_toolchain
Browse files Browse the repository at this point in the history
* Upgrade default `java_toolchain.java_runtime` to JDK 17
* Reduce duplication between `_TOOLCHAIN_CONFIGURATION` constants
* Clean up obsolete JDK 8 handling in `bootclasspath` target

PiperOrigin-RevId: 494212843
Change-Id: I804a7a63d48a828c5e3a4e19b0a554bba219ca92
  • Loading branch information
cushon authored and copybara-github committed Dec 9, 2022
1 parent ae5dfe3 commit 75e0f7d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 47 deletions.
16 changes: 8 additions & 8 deletions src/test/shell/integration/bazel_java_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ EOF
# We expect the given host_javabase does not appear in the command line of
# java_library actions.
bazel aquery --output=text --tool_java_runtime_version='host_javabase' //java:javalib >& $TEST_log
expect_log "exec external/remotejdk11_.*/bin/java"
expect_log "exec external/remotejdk17_.*/bin/java"
expect_not_log "exec external/host_javabase/bin/java"

# If we don't specify anything, we expect the remote JDK to be used.
bazel aquery --output=text //java:javalib >& $TEST_log
expect_not_log "exec external/embedded_jdk/bin/java"
expect_log "exec external/remotejdk11_.*/bin/java"
expect_log "exec external/remotejdk17_.*/bin/java"

bazel aquery --output=text --java_runtime_version='host_javabase' //java:javalib >& $TEST_log
expect_log "exec external/remotejdk11_.*/bin/java"
expect_log "exec external/remotejdk17_.*/bin/java"
expect_not_log "exec external/host_javabase/bin/java"
}

Expand All @@ -102,10 +102,10 @@ EOF
touch foobar/bin/java

bazel aquery --output=text --java_language_version=8 //java:javalib >& $TEST_log
expect_log "exec external/remotejdk11_.*/bin/java"
expect_log "exec external/remotejdk17_.*/bin/java"

bazel aquery --output=text --java_language_version=11 //java:javalib >& $TEST_log
expect_log "exec external/remotejdk11_.*/bin/java"
expect_log "exec external/remotejdk17_.*/bin/java"

bazel aquery --output=text --java_language_version=17 //java:javalib >& $TEST_log
expect_log "exec external/remotejdk17_.*/bin/java"
Expand Down Expand Up @@ -155,17 +155,17 @@ EOF
# We expect the given host_javabase does not appear in the command line of
# java_library actions.
bazel aquery --output=text --tool_java_runtime_version='host_javabase' 'deps(//java:sample,1)' >& $TEST_log
expect_log "exec external/remotejdk11_.*/bin/java"
expect_log "exec external/remotejdk17_.*/bin/java"
expect_not_log "exec external/host_javabase/bin/java"

# If we don't specify anything, we expect the remote JDK to be used.
# Note that this will change in the future but is the current state.
bazel aquery --output=text 'deps(//java:sample,1)' >& $TEST_log
expect_not_log "exec external/embedded_jdk/bin/java"
expect_log "exec external/remotejdk11_.*/bin/java"
expect_log "exec external/remotejdk17_.*/bin/java"

bazel aquery --output=text --tool_java_runtime_version='host_javabase' 'deps(//java:sample,1)' >& $TEST_log
expect_log "exec external/remotejdk11_.*/bin/java"
expect_log "exec external/remotejdk17_.*/bin/java"
expect_not_log "exec external/host_javabase/bin/java"

bazel aquery --output=text --tool_java_language_version=17 --tool_java_runtime_version='host_javabase' 'deps(//java:sample,1)' >& $TEST_log
Expand Down
51 changes: 12 additions & 39 deletions tools/jdk/default_java_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ BASE_JDK9_JVM_OPTS = [
# TODO(b/64485048): Disable this option in persistent worker mode only.
# Disable symlinks resolution cache since symlinks in exec root change
"-Dsun.io.useCanonCaches=false",

# Compact strings make JavaBuilder slightly slower.
"-XX:-CompactStrings",
]

JDK9_JVM_OPTS = BASE_JDK9_JVM_OPTS
Expand All @@ -51,8 +54,7 @@ DEFAULT_JAVACOPTS = [
"-Xep:ReturnValueIgnored:OFF",
]

# java_toolchain parameters without specifying javac, java.compiler,
# jdk.compiler module, and jvm_opts
# Default java_toolchain parameters
_BASE_TOOLCHAIN_CONFIGURATION = dict(
forcibly_disable_header_compilation = False,
genclass = ["@remote_java_tools//:GenClass"],
Expand All @@ -63,6 +65,10 @@ _BASE_TOOLCHAIN_CONFIGURATION = dict(
javac_supports_workers = True,
jacocorunner = "@remote_java_tools//:jacoco_coverage_runner_filegroup",
jvm_opts = BASE_JDK9_JVM_OPTS,
turbine_jvm_opts = [
# Turbine is not a worker and parallel GC is faster for short-lived programs.
"-XX:+UseParallelGC",
],
misc = DEFAULT_JAVACOPTS,
singlejar = ["@bazel_tools//tools/jdk:singlejar"],
# Code to enumerate target JVM boot classpath uses host JVM. Because
Expand All @@ -73,19 +79,10 @@ _BASE_TOOLCHAIN_CONFIGURATION = dict(
reduced_classpath_incompatible_processors = [
"dagger.hilt.processor.internal.root.RootProcessor", # see b/21307381
],
java_runtime = "@bazel_tools//tools/jdk:remotejdk_17",
)

DEFAULT_TOOLCHAIN_CONFIGURATION = dict(
jvm_opts = [
# Compact strings make JavaBuilder slightly slower.
"-XX:-CompactStrings",
] + JDK9_JVM_OPTS,
turbine_jvm_opts = [
# Turbine is not a worker and parallel GC is faster for short-lived programs.
"-XX:+UseParallelGC",
],
java_runtime = "@bazel_tools//tools/jdk:remote_jdk11",
)
DEFAULT_TOOLCHAIN_CONFIGURATION = _BASE_TOOLCHAIN_CONFIGURATION

# The 'vanilla' toolchain is an unsupported alternative to the default.
#
Expand All @@ -103,6 +100,7 @@ DEFAULT_TOOLCHAIN_CONFIGURATION = dict(
VANILLA_TOOLCHAIN_CONFIGURATION = dict(
javabuilder = ["@remote_java_tools//:VanillaJavaBuilder"],
jvm_opts = [],
java_runtime = None,
)

# The new toolchain is using all the pre-built tools, including
Expand All @@ -111,32 +109,14 @@ VANILLA_TOOLCHAIN_CONFIGURATION = dict(
# same, otherwise the binaries will not work on the execution
# platform.
PREBUILT_TOOLCHAIN_CONFIGURATION = dict(
jvm_opts = [
# Compact strings make JavaBuilder slightly slower.
"-XX:-CompactStrings",
] + JDK9_JVM_OPTS,
turbine_jvm_opts = [
# Turbine is not a worker and parallel GC is faster for short-lived programs.
"-XX:+UseParallelGC",
],
ijar = ["@bazel_tools//tools/jdk:ijar_prebuilt_binary"],
singlejar = ["@bazel_tools//tools/jdk:prebuilt_singlejar"],
java_runtime = "@bazel_tools//tools/jdk:remote_jdk11",
)

# The new toolchain is using all the tools from sources.
NONPREBUILT_TOOLCHAIN_CONFIGURATION = dict(
jvm_opts = [
# Compact strings make JavaBuilder slightly slower.
"-XX:-CompactStrings",
] + JDK9_JVM_OPTS,
turbine_jvm_opts = [
# Turbine is not a worker and parallel GC is faster for short-lived programs.
"-XX:+UseParallelGC",
],
ijar = ["@remote_java_tools//:ijar_cc_binary"],
singlejar = ["@remote_java_tools//:singlejar_cc_bin"],
java_runtime = "@bazel_tools//tools/jdk:remote_jdk11",
)

def default_java_toolchain(name, configuration = DEFAULT_TOOLCHAIN_CONFIGURATION, toolchain_definition = True, exec_compatible_with = [], target_compatible_with = [], **kwargs):
Expand Down Expand Up @@ -191,8 +171,6 @@ def _bootclasspath_impl(ctx):
args.add("-target")
args.add("8")
args.add("-Xlint:-options")
args.add("-cp")
args.add("%s/lib/tools.jar" % host_javabase.java_home)
args.add("-d")
args.add_all([class_dir], expand_directories = False)
args.add(ctx.file.src)
Expand All @@ -214,12 +192,7 @@ def _bootclasspath_impl(ctx):
args.add("--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED")
args.add("--add-exports=jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED")
args.add("--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED")
args.add_joined(
"-cp",
[class_dir, "%s/lib/tools.jar" % host_javabase.java_home],
join_with = ctx.configuration.host_path_separator,
expand_directories = False,
)
args.add("-cp", class_dir.path)
args.add("DumpPlatformClassPath")
args.add(bootclasspath)

Expand Down

0 comments on commit 75e0f7d

Please sign in to comment.