Skip to content

Commit

Permalink
Stop using --release in versioned java_toolchains
Browse files Browse the repository at this point in the history
Using --release to target versions that support modules and are not the
latest supported version (e.g. --release 9 on JDK 10) doesn't work:
https://bugs.openjdk.java.net/browse/JDK-8209865

Related: #5723

Closes #5982.

PiperOrigin-RevId: 210646978
  • Loading branch information
cushon authored and laurentlb committed Sep 7, 2018
1 parent e66b90f commit a5ac7e5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/java_tools/buildjar/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ java_toolchain(
# class path after -bootclasspath. For convenience, we currently have a
# single jar that contains the contents of both the bootclasspath and
# extdirs.
bootclasspath = ["//tools/jdk:platformclasspath.jar"],
bootclasspath = ["//tools/jdk:platformclasspath8.jar"],
extclasspath = [],
genclass = ["bootstrap_genclass_deploy.jar"],
ijar = ["//third_party/ijar"],
Expand Down
8 changes: 4 additions & 4 deletions tools/android/BUILD.tools
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ gen_java_lang_extras_jar_cmd = """
$(location %s) \
--exclude_build_data \
--dont_change_compression \
--sources $(location @bazel_tools//tools/jdk:platformclasspath) \
--sources $(location @bazel_tools//tools/jdk:platformclasspath8) \
--include_prefixes "java/lang/invoke/" \
--include_prefixes "java/lang/annotation/" \
--output $@
Expand All @@ -111,7 +111,7 @@ gen_java_lang_extras_jar_cmd = """
genrule(
name = "gen_java_lang_extras_jar",
srcs = [
"@bazel_tools//tools/jdk:platformclasspath"
"@bazel_tools//tools/jdk:platformclasspath8"
],
tools = select({
"//src/conditions:windows": [":singlejar_javabin"],
Expand Down Expand Up @@ -176,7 +176,7 @@ genrule(
$(location :desugar_java8) \
--input $< \
--output $@ \
--classpath_entry "$(location @bazel_tools//tools/jdk:platformclasspath)" \
--classpath_entry "$(location @bazel_tools//tools/jdk:platformclasspath8)" \
--core_library --allow_empty_bootclasspath \
--nobest_effort_tolerate_missing_deps \
--noemit_dependency_metadata_as_needed \
Expand Down Expand Up @@ -213,7 +213,7 @@ genrule(
--dont_rewrite_core_library_invocation "java/util/Iterator#remove" """,
tools = [
":desugar_java8",
"@bazel_tools//tools/jdk:platformclasspath",
"@bazel_tools//tools/jdk:platformclasspath8",
],
visibility = ["//visibility:private"],
)
Expand Down
62 changes: 32 additions & 30 deletions tools/jdk/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -169,57 +169,59 @@ alias(
actual = "@embedded_jdk//:jdk",
)

genrule(
name = "platformclasspath",
srcs = ["DumpPlatformClassPath.java"],
outs = ["platformclasspath.jar"],
cmd = """
RELEASES = (8, 9, 10)

# Create jars containing compile-time bootclasspaths for each Java version
# in RELEASES, using javac to read those APIs via the infrastructure added
# for the --release flag (see http://openjdk.java.net/jeps/247).
[
genrule(
name = "platformclasspath%d" % release,
srcs = ["DumpPlatformClassPath.java"],
outs = ["platformclasspath%d.jar" % release],
cmd = """
set -eu
TMPDIR=$$(mktemp -d -t tmp.XXXXXXXX)
$(JAVABASE)/bin/javac -source 8 -target 8 \
-cp $(JAVABASE)/lib/tools.jar \
-d $$TMPDIR $<
$(JAVA) -XX:+IgnoreUnrecognizedVMOptions \
--add-exports=jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED \
-cp $$TMPDIR DumpPlatformClassPath $@
-cp $$TMPDIR DumpPlatformClassPath %d $@
rm -rf $$TMPDIR
""",
toolchains = ["@bazel_tools//tools/jdk:current_host_java_runtime"],
tools = ["@bazel_tools//tools/jdk:current_host_java_runtime"],
)
""" % release,
toolchains = ["@bazel_tools//tools/jdk:current_host_java_runtime"],
tools = ["@bazel_tools//tools/jdk:current_host_java_runtime"],
)
for release in RELEASES
]

default_java_toolchain(
name = "toolchain_hostjdk8",
bootclasspath = [":platformclasspath"],
bootclasspath = [":platformclasspath8"],
forcibly_disable_header_compilation = True,
javabuilder = [":vanillajavabuilder"],
jvm_opts = [],
source_version = "8",
target_version = "8",
)

default_java_toolchain(
# Default to the Java 8 language level.
# TODO(cushon): consider if/when we should increment this?
alias(
name = "toolchain",
bootclasspath = [":platformclasspath"],
source_version = "8",
target_version = "8",
)

default_java_toolchain(
name = "toolchain_java9",
misc = DEFAULT_JAVACOPTS + [
"--release",
"9",
],
actual = "toolchain_java8",
)

default_java_toolchain(
name = "toolchain_java10",
misc = DEFAULT_JAVACOPTS + [
"--release",
"10",
],
)
[
default_java_toolchain(
name = "toolchain_java%d" % release,
bootclasspath = [":platformclasspath%d" % release],
source_version = "%s" % release,
target_version = "%s" % release,
)
for release in RELEASES
]

filegroup(
name = "srcs",
Expand Down
41 changes: 31 additions & 10 deletions tools/jdk/DumpPlatformClassPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,24 @@
import javax.tools.StandardLocation;

/**
* Output a jar file containing all classes on the platform classpath of the current JDK.
* Output a jar file containing all classes on the platform classpath of the given JDK release.
*
* <p>usage: DumpPlatformClassPath <output jar>
* <p>usage: DumpPlatformClassPath <release version> <output jar>
*/
public class DumpPlatformClassPath {

public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("usage: DumpPlatformClassPath <output jar>");
if (args.length != 2) {
System.err.println("usage: DumpPlatformClassPath <release version> <output jar>");
System.exit(1);
}
Path output = Paths.get(args[0]);
int release = Integer.parseInt(args[0]);
Path output = Paths.get(args[1]);

Map<String, byte[]> entries = new HashMap<>();

// JDK 8 bootclasspath handling
// Legacy JDK 8 bootclasspath handling.
// TODO(cushon): make sure this has test coverage.
Path javaHome = Paths.get(System.getProperty("java.home"));
if (javaHome.endsWith("jre")) {
javaHome = javaHome.getParent();
Expand All @@ -85,13 +87,32 @@ public static void main(String[] args) throws Exception {
}
}

if (entries.isEmpty()) {
// JDK > 8 bootclasspath handling
if (!entries.isEmpty()) {
// If we found a JDK 8 bootclasspath (rt.jar, etc.) then we're done.
//
// However JDK 8 only contains bootclasspath API information for the current release,
// so we're always going to get a JDK 8 API level regardless of what the user requested.
// Emit a warning if they wanted to target a different version.
if (release != 8) {
System.err.printf(
"warning: ignoring release %s on --host_javabase=%s\n",
release, System.getProperty("java.version"));
}
} else {
// JDK > 8 --host_javabase bootclasspath handling.
// The default --host_javabase is currently JDK 10.

// Set up a compilation with --release 8 to initialize a filemanager
// Set up a compilation with --release to initialize a filemanager
Context context = new Context();
JavacTool.create()
.getTask(null, null, null, Arrays.asList("--release", "8"), null, null, context);
.getTask(
/* out = */ null,
/* fileManager = */ null,
/* diagnosticListener = */ null,
/* options = */ Arrays.asList("--release", String.valueOf(release)),
/* classes = */ null,
/* compilationUnits = */ null,
context);
JavaFileManager fileManager = context.get(JavaFileManager.class);

for (JavaFileObject fileObject :
Expand Down

0 comments on commit a5ac7e5

Please sign in to comment.