Skip to content

Fix and verify Java version compatibility of Java tools #26126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ build:windows --per_file_copt=external/.*@/w
build:windows --host_per_file_copt=external/.*@/w

# Enable Java 21 language features
build --java_runtime_version=21
build --java_language_version=21
build --tool_java_language_version=21
build --tool_java_runtime_version=21
common --java_runtime_version=21
common --java_language_version=21
common --tool_java_language_version=21
common --tool_java_runtime_version=21
# Ensure that Java language level and bootclasspath version are the same.
# This avoids accidental usage of more recent Java APIs.
common --@rules_java//toolchains:incompatible_language_version_bootclasspath

# User-specific .bazelrc
try-import %workspace%/user.bazelrc
Expand Down
7 changes: 6 additions & 1 deletion MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ DIST_ARCHIVE_REPOS = [get_canonical_repo_name(repo) for repo in [
"rules_proto",
"rules_python",
"rules_shell",
"with_cfg.bzl",
"zlib",
"zstd-jni",
]] + [(get_canonical_repo_name("com_github_grpc_grpc") + "+grpc_repo_deps_ext+" + suffix) for suffix in [
Expand Down
54 changes: 25 additions & 29 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_java//java:java_binary.bzl", "java_binary")
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
load("//src:build_defs.bzl", "transition_java_language_8_archive")
load("//src:release_archive.bzl", "release_archive")
load("//src:release_archive.bzl", "minimum_java_compilation_runtime_filegroup", "minimum_java_runtime_filegroup", "release_archive")
load(":embedded_tools.bzl", "srcsfile")
load(":rule_size_test.bzl", "rule_size_test")

Expand Down Expand Up @@ -512,19 +511,33 @@ genrule(
)

# Following targets build java_tools.zip - platform independent part of java_tools
JAVA_TOOLS_DEPLOY_JARS = [
"//src/java_tools/buildjar:JavaBuilder_deploy.jar",
"//src/java_tools/buildjar:VanillaJavaBuilder_deploy.jar",
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/genclass:GenClass_deploy.jar",
"//src/java_tools/buildjar/java/com/google/devtools/build/java/turbine:turbine_direct_binary_deploy.jar",
"//src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps:ImportDepsChecker_deploy.jar",
"//src/java_tools/junitrunner/java/com/google/testing/coverage:JacocoCoverage_jarjar_deploy.jar",
"//src/java_tools/junitrunner/java/com/google/testing/junit/runner:Runner_deploy.jar",
]
minimum_java_runtime_filegroup(
name = "minimum_java_runtime_tools",
srcs = [
"//src/java_tools/junitrunner/java/com/google/testing/coverage:JacocoCoverage_jarjar_deploy.jar",
"//src/java_tools/junitrunner/java/com/google/testing/junit/runner:Runner_deploy.jar",
],
visibility = ["//visibility:private"],
)

minimum_java_compilation_runtime_filegroup(
name = "minimum_java_compilation_runtime_tools",
srcs = [
"//src/java_tools/buildjar:JavaBuilder_deploy.jar",
"//src/java_tools/buildjar:VanillaJavaBuilder_deploy.jar",
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/genclass:GenClass_deploy.jar",
"//src/java_tools/buildjar/java/com/google/devtools/build/java/turbine:turbine_direct_binary_deploy.jar",
"//src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps:ImportDepsChecker_deploy.jar",
],
visibility = ["//visibility:private"],
)

release_archive(
name = "jars_java_tools_zip",
srcs = JAVA_TOOLS_DEPLOY_JARS,
srcs = [
":minimum_java_compilation_runtime_tools",
":minimum_java_runtime_tools",
],
package_dir = "java_tools",
visibility = ["//visibility:private"],
)
Expand All @@ -545,12 +558,6 @@ release_archive(
],
)

transition_java_language_8_archive(
name = "java_tools_java8.zip",
archive_zip = ":java_tools.zip",
visibility = ["//src/test/shell/bazel:__pkg__"],
)

release_archive(
name = "turbine_direct_graal_zip",
srcs = ["//src/java_tools/buildjar/java/com/google/devtools/build/java/turbine:turbine_direct_graal"],
Expand All @@ -573,12 +580,6 @@ release_archive(
],
)

transition_java_language_8_archive(
name = "java_tools_prebuilt_java8.zip",
archive_zip = ":java_tools_prebuilt.zip",
visibility = ["//src/test/shell/bazel:__pkg__"],
)

# Following targets used by the java_tools_binaries Buildkite pipeline to upload
# the java_tools_*.zip to either tmp/sources or tmp/build directories in GCS.
sh_binary(
Expand Down Expand Up @@ -664,11 +665,6 @@ filegroup(
],
)

bzl_library(
name = "build_defs_bzl",
srcs = ["build_defs.bzl"],
)

java_binary(
name = "CheckSunJnuEncoding",
srcs = ["CheckSunJnuEncoding.java"],
Expand Down
58 changes: 0 additions & 58 deletions src/build_defs.bzl

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,7 @@ private static void setLocations(JavacFileManager fileManager, BlazeJavacArgumen
try {
fileManager.setLocationFromPaths(StandardLocation.CLASS_PATH, arguments.classPath());
// modular dependencies must be on the module path, not the classpath
fileManager.setLocationFromPaths(
StandardLocation.locationFor("MODULE_PATH"), arguments.classPath());
fileManager.setLocationFromPaths(StandardLocation.MODULE_PATH, arguments.classPath());

fileManager.setLocationFromPaths(
StandardLocation.CLASS_OUTPUT, ImmutableList.of(arguments.classOutput()));
Expand Down Expand Up @@ -326,7 +325,7 @@ private static void setLocations(JavacFileManager fileManager, BlazeJavacArgumen
Path system = arguments.system();
if (system != null) {
fileManager.setLocationFromPaths(
StandardLocation.locationFor("SYSTEM_MODULES"), ImmutableList.of(system));
StandardLocation.SYSTEM_MODULES, ImmutableList.of(system));
}
// The bootclasspath may legitimately be empty if --release is being used.
Collection<Path> bootClassPath = arguments.bootClassPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@

package com.google.testing.junit.runner.internal;

import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toList;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -28,40 +26,41 @@
*/
public class SystemExitDetectingShutdownHook {
public static Thread newShutdownHook(PrintStream testRunnerOut) {
Runnable hook = () -> {
boolean foundRuntimeExit = false;
for (StackTraceElement[] stack : Thread.getAllStackTraces().values()) {
@SuppressWarnings("JdkCollectors") // can't use ImmutableList here
List<String> framesStartingWithRuntimeExit =
stream(stack)
.dropWhile(
frame ->
!frame.getClassName().equals("java.lang.Runtime")
|| !frame.getMethodName().equals("exit"))
.map(SystemExitDetectingShutdownHook::frameString)
.collect(toList());
if (!framesStartingWithRuntimeExit.isEmpty()) {
foundRuntimeExit = true;
testRunnerOut.println("\nSystem.exit or Runtime.exit was called!");
testRunnerOut.println(String.join("\n", framesStartingWithRuntimeExit));
}
}
if (foundRuntimeExit) {
// We must call halt rather than exit, because exit would lead to a deadlock. We use a
// hopefully unique exit code to make it easier to identify this case.
Runtime.getRuntime().halt(121);
}
};
Runnable hook =
() -> {
boolean foundRuntimeExit = false;
for (StackTraceElement[] stack : Thread.getAllStackTraces().values()) {
List<String> framesStartingWithRuntimeExit = new ArrayList<>();
boolean foundRuntimeExitInThisThread = false;
for (StackTraceElement frame : stack) {
if (!foundRuntimeExitInThisThread
&& frame.getClassName().equals("java.lang.Runtime")
&& frame.getMethodName().equals("exit")) {
foundRuntimeExitInThisThread = true;
}
if (foundRuntimeExitInThisThread) {
framesStartingWithRuntimeExit.add(frameString(frame));
}
}
if (foundRuntimeExitInThisThread) {
foundRuntimeExit = true;
testRunnerOut.println("\nSystem.exit or Runtime.exit was called!");
testRunnerOut.println(String.join("\n", framesStartingWithRuntimeExit));
}
}
if (foundRuntimeExit) {
// We must call halt rather than exit, because exit would lead to a deadlock. We use a
// hopefully unique exit code to make it easier to identify this case.
Runtime.getRuntime().halt(121);
}
};
return new Thread(hook, "SystemExitDetectingShutdownHook");
}

private static String frameString(StackTraceElement frame) {
return String.format(
" at %s.%s(%s:%d)",
frame.getClassName(),
frame.getMethodName(),
frame.getFileName(),
frame.getLineNumber());
frame.getClassName(), frame.getMethodName(), frame.getFileName(), frame.getLineNumber());
}

private SystemExitDetectingShutdownHook() {}
Expand Down
31 changes: 31 additions & 0 deletions src/release_archive.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,37 @@

"""Rules to create a release archive"""

load("@rules_java//java:java_binary.bzl", "java_binary")
load("@with_cfg.bzl", "with_cfg")

# The minimum --java_{tool_,}runtime_version supported by prebuilt Java tools.
_MINIMUM_JAVA_RUNTIME_VERSION = 8

# The minimum version of a java_toolchain's java_runtime supported by prebuilt Java tools.
_MINIMUM_JAVA_COMPILATION_RUNTIME_VERSION = 17

minimum_java_runtime_java_binary, _minimum_java_runtime_java_binary = (
# Don't warn about targeting very old Java versions.
with_cfg(java_binary)
.set("java_language_version", str(_MINIMUM_JAVA_RUNTIME_VERSION))
.extend("javacopt", ["-Xlint:-options"])
.build()
)

minimum_java_runtime_filegroup, _minimum_java_runtime_filegroup = (
# Don't warn about targeting very old Java versions.
with_cfg(native.filegroup)
.set("java_language_version", str(_MINIMUM_JAVA_RUNTIME_VERSION))
.extend("javacopt", ["-Xlint:-options"])
.build()
)

minimum_java_compilation_runtime_filegroup, _minimum_java_compilation_runtime_filegroup = (
with_cfg(native.filegroup)
.set("java_language_version", str(_MINIMUM_JAVA_COMPILATION_RUNTIME_VERSION))
.build()
)

def release_archive(name, srcs = [], src_map = {}, package_dir = "-", deps = [], **kwargs):
""" Creates an zip of the srcs, and renamed label artifacts.

Expand Down
Loading
Loading