Skip to content

Commit 9b7fa9f

Browse files
committed
Add support for JDK 14
Closes bazelbuild#11017. Test Plan: 1. Create java_tools from this commit: $ bazel build src:java_tools_java14.zip 2. Switch to using the java_tools from the above step in WORKSPACE file: load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "remote_java_tools_linux", urls = [ "file:///<path to the java_tools_java14.zip>", ], ) 3. Add Zulu OpenJDK14 to use as javabase in WORKSPACE file: http_archive( name = "openjdk14_linux_archive", build_file_content = """ java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public']) exports_files(["WORKSPACE"], visibility = ["//visibility:public"]) """, sha256 = "48bb8947034cd079ad1ef83335e7634db4b12a26743a0dc314b6b861480777aa", strip_prefix = "zulu14.28.21-ca-jdk14.0.1-linux_x64", urls = ["https://cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-linux_x64.tar.gz"], ) 4. Activate custom java toolchain and javabase in .bazelrc file: build --java_toolchain=@remote_java_tools_linux//:toolchain_jdk_14 build --host_java_toolchain=@remote_java_tools_linux//:toolchain_jdk_14 build --javabase=@openjdk14_linux_archive//:runtime build --host_javabase=@openjdk14_linux_archive//:runtime 5. Create Java 14 example class file: public class Javac14Example { record Point(int x, int y) {} public static void main(String[] args) { Point point = new Point(0, 1); System.out.println(point.x); } } 6. Add Bazel file to build Java 14 syntax class with activated preview features: java_binary( name = "Javac14Example", srcs = ["Javac14Example.java"], javacopts = ["--enable-preview"], jvm_flags = ["--enable-preview"], main_class = "Javac14Example", ) 7. Test that it works as expected: $ bazel run java:Javac14Example INFO: Analyzed target //java:Javac14Example (1 packages loaded, 2 targets configured). INFO: Found 1 target... INFO: From Building java/Javac14Example.jar (1 source file): Note: java/Javac14Example.java uses preview language features. Note: Recompile with -Xlint:preview for details. Target //java:Javac14Example up-to-date: bazel-bin/java/Javac14Example.jar bazel-bin/java/Javac14Example INFO: Elapsed time: 1.502s, Critical Path: 1.30s INFO: 1 process: 1 worker. INFO: Build completed successfully, 2 total actions INFO: Build completed successfully, 2 total actions 0
1 parent 870a19b commit 9b7fa9f

File tree

7 files changed

+220
-21
lines changed

7 files changed

+220
-21
lines changed

WORKSPACE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,15 @@ http_archive(
645645
],
646646
)
647647

648+
# TODO(davido): This is not needed, because jdk compiler module patching is disabled for JDK 14
649+
http_archive(
650+
name = "java_tools_langtools_javac14",
651+
sha256 = "99b107105165a91df82cd7cf82a8efb930d803fb7de1663cf7f780142104cd14",
652+
urls = [
653+
"https://mirror.bazel.build/bazel_java_tools/jdk_langtools/langtools_jdk12.zip",
654+
],
655+
)
656+
648657
# This must be kept in sync with src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_remote_tools.WORKSPACE
649658
http_archive(
650659
name = "android_tools_for_testing",
@@ -857,6 +866,18 @@ exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
857866
urls = ["https://mirror.bazel.build/openjdk/azul-zulu12.2.3-ca-jdk12.0.1/zulu12.2.3-ca-jdk12.0.1-linux_x64.tar.gz"],
858867
)
859868

869+
# This must be kept in sync with src/test/shell/bazel/testdata/jdk_http_archives.
870+
http_archive(
871+
name = "openjdk14_linux_archive",
872+
build_file_content = """
873+
java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public'])
874+
exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
875+
""",
876+
sha256 = "48bb8947034cd079ad1ef83335e7634db4b12a26743a0dc314b6b861480777aa",
877+
strip_prefix = "zulu14.28.21-ca-jdk14.0.1-linux_x64",
878+
urls = ["https://cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-linux_x64.tar.gz"],
879+
)
880+
860881
# This must be kept in sync with src/test/shell/bazel/testdata/jdk_http_archives.
861882
http_archive(
862883
name = "openjdk12_darwin_archive",

src/BUILD

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ JAVA_TOOLS_DEPLOY_JARS = [
562562
"//conditions:default": [],
563563
})
564564

565-
JAVA_VERSIONS = ("11", "12")
565+
JAVA_VERSIONS = ("11", "14")
566566

567567
[
568568
genrule(
@@ -633,8 +633,8 @@ JAVA_VERSIONS = ("11", "12")
633633
[
634634
# The java_tools releases can have BUILD files that vary depending on the
635635
# javac version they embed. Currently the only difference is in the
636-
# java_toolchain source version which has to be 12 for javac 12 to be able
637-
# to build new Java 12 features.
636+
# java_toolchain source version which has to be 14 for javac 14 to be able
637+
# to build new Java 14 features.
638638
genrule(
639639
name = "create_java_tools_build_java" + java_version,
640640
srcs = ["//tools/jdk:BUILD.java_tools"],
@@ -739,9 +739,9 @@ filegroup(
739739
"@openjdk11_darwin_archive//:WORKSPACE",
740740
"@openjdk11_linux_archive//:WORKSPACE",
741741
"@openjdk11_windows_archive//:WORKSPACE",
742-
"@openjdk12_darwin_archive//:WORKSPACE",
743-
"@openjdk12_linux_archive//:WORKSPACE",
744-
"@openjdk12_windows_archive//:WORKSPACE",
742+
"@openjdk14_darwin_archive//:WORKSPACE",
743+
"@openjdk14_linux_archive//:WORKSPACE",
744+
"@openjdk14_windows_archive//:WORKSPACE",
745745
"@openjdk_linux_aarch64_minimal//file",
746746
"@openjdk_linux_minimal//file",
747747
"@openjdk_macos_minimal//file",
@@ -751,9 +751,9 @@ filegroup(
751751
"@remote_java_tools_javac11_test_darwin//:WORKSPACE",
752752
"@remote_java_tools_javac11_test_linux//:WORKSPACE",
753753
"@remote_java_tools_javac11_test_windows//:WORKSPACE",
754-
"@remote_java_tools_javac12_test_darwin//:WORKSPACE",
755-
"@remote_java_tools_javac12_test_linux//:WORKSPACE",
756-
"@remote_java_tools_javac12_test_windows//:WORKSPACE",
754+
"@remote_java_tools_javac14_test_darwin//:WORKSPACE",
755+
"@remote_java_tools_javac14_test_linux//:WORKSPACE",
756+
"@remote_java_tools_javac14_test_windows//:WORKSPACE",
757757
"@remote_java_tools_linux_for_testing//:WORKSPACE",
758758
"@remote_java_tools_windows_for_testing//:WORKSPACE",
759759
"@remotejdk11_linux_aarch64_for_testing//:WORKSPACE",

src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,25 @@ bind(
9494
# Bazel's WORKSPACE file, but they don't have to be the same.
9595

9696
# This must be kept in sync with the top-level WORKSPACE file.
97+
#maybe(
98+
# http_archive,
99+
# name = "remotejdk11_linux",
100+
# build_file = "@local_jdk//:BUILD.bazel",
101+
# sha256 = "360626cc19063bc411bfed2914301b908a8f77a7919aaea007a977fa8fb3cde1",
102+
# strip_prefix = "zulu11.37.17-ca-jdk11.0.6-linux_x64",
103+
# urls = [
104+
# "https://mirror.bazel.build/openjdk/azul-zulu11.37.17-ca-jdk11.0.6/zulu11.37.17-ca-jdk11.0.6-linux_x64.tar.gz",
105+
# ],
106+
#)
107+
97108
maybe(
98109
http_archive,
99110
name = "remotejdk11_linux",
100111
build_file = "@local_jdk//:BUILD.bazel",
101-
sha256 = "360626cc19063bc411bfed2914301b908a8f77a7919aaea007a977fa8fb3cde1",
102-
strip_prefix = "zulu11.37.17-ca-jdk11.0.6-linux_x64",
112+
sha256 = "48bb8947034cd079ad1ef83335e7634db4b12a26743a0dc314b6b861480777aa",
113+
strip_prefix = "zulu14.28.21-ca-jdk14.0.1-linux_x64",
103114
urls = [
104-
"https://mirror.bazel.build/openjdk/azul-zulu11.37.17-ca-jdk11.0.6/zulu11.37.17-ca-jdk11.0.6-linux_x64.tar.gz",
115+
"https://cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-linux_x64.tar.gz",
105116
],
106117
)
107118

src/test/shell/bazel/BUILD

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,23 +179,24 @@ sh_test(
179179
)
180180

181181
sh_test(
182-
name = "bazel_java12_test",
183-
srcs = ["bazel_java12_test.sh"],
182+
name = "bazel_java14_test",
183+
srcs = ["bazel_java14_test.sh"],
184+
tags = ["local"],
184185
args = [
185186
# --java_toolchain and --host_java_toolchain values
186-
"@local_java_tools//:toolchain_jdk_12",
187+
"@local_java_tools//:toolchain_jdk_14",
187188
# java_tools zip to test
188-
"src/java_tools_java12.zip",
189+
"src/java_tools_java14.zip",
189190
] + select({
190191
# --javabase and --host_javabase values
191192
"//src/conditions:darwin": ["@openjdk12_darwin_archive//:runtime"],
192193
"//src/conditions:darwin_x86_64": ["@openjdk12_darwin_archive//:runtime"],
193194
"//src/conditions:windows": ["@openjdk12_windows_archive//:runtime"],
194-
"//src/conditions:linux_x86_64": ["@openjdk12_linux_archive//:runtime"],
195+
"//src/conditions:linux_x86_64": ["@openjdk14_linux_archive//:runtime"],
195196
}),
196197
data = [
197198
":test-deps",
198-
"//src:java_tools_java12_zip",
199+
"//src:java_tools_java14_zip",
199200
"//src/test/shell/bazel/testdata:jdk_http_archives_filegroup",
200201
"@bazel_tools//tools/bash/runfiles",
201202
],
@@ -219,7 +220,7 @@ sh_test(
219220
exec_compatible_with = ["//:highcpu_machine"],
220221
)
221222

222-
JAVA_VERSIONS = ("11", "12")
223+
JAVA_VERSIONS = ("11", "14")
223224

224225
[
225226
sh_test(
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2020 The Bazel Authors. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
# Tests that bazel runs projects with Java 14 features.
18+
19+
# --- begin runfiles.bash initialization ---
20+
if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
21+
if [[ -f "$0.runfiles_manifest" ]]; then
22+
export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
23+
elif [[ -f "$0.runfiles/MANIFEST" ]]; then
24+
export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
25+
elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
26+
export RUNFILES_DIR="$0.runfiles"
27+
fi
28+
fi
29+
if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
30+
source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
31+
elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
32+
source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
33+
"$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
34+
else
35+
echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
36+
exit 1
37+
fi
38+
# --- end runfiles.bash initialization ---
39+
40+
source "$(rlocation "io_bazel/src/test/shell/integration_test_setup.sh")" \
41+
|| { echo "integration_test_setup.sh not found!" >&2; exit 1; }
42+
43+
case "$(uname -s | tr [:upper:] [:lower:])" in
44+
msys*|mingw*|cygwin*)
45+
declare -r is_windows=true
46+
;;
47+
*)
48+
declare -r is_windows=false
49+
;;
50+
esac
51+
52+
if "$is_windows"; then
53+
export MSYS_NO_PATHCONV=1
54+
export MSYS2_ARG_CONV_EXCL="*"
55+
fi
56+
57+
JAVA_TOOLCHAIN="$1"; shift
58+
JAVA_TOOLS_ZIP="$1"; shift
59+
JAVA_RUNTIME="$1"; shift
60+
61+
echo "JAVA_TOOLS_ZIP=$JAVA_TOOLS_ZIP"
62+
63+
64+
JAVA_TOOLS_RLOCATION=$(rlocation io_bazel/$JAVA_TOOLS_ZIP)
65+
66+
if "$is_windows"; then
67+
JAVA_TOOLS_ZIP_FILE_URL="file:///${JAVA_TOOLS_RLOCATION}"
68+
else
69+
JAVA_TOOLS_ZIP_FILE_URL="file://${JAVA_TOOLS_RLOCATION}"
70+
fi
71+
JAVA_TOOLS_ZIP_FILE_URL=${JAVA_TOOLS_ZIP_FILE_URL:-}
72+
73+
add_to_bazelrc "build --java_toolchain=${JAVA_TOOLCHAIN}"
74+
add_to_bazelrc "build --host_java_toolchain=${JAVA_TOOLCHAIN}"
75+
add_to_bazelrc "build --javabase=${JAVA_RUNTIME}"
76+
add_to_bazelrc "build --host_javabase=${JAVA_RUNTIME}"
77+
78+
function set_up() {
79+
cat >>WORKSPACE <<EOF
80+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
81+
# java_tools versions only used to test Bazel with various JDK toolchains.
82+
83+
http_archive(
84+
name = "local_java_tools",
85+
urls = ["${JAVA_TOOLS_ZIP_FILE_URL}"]
86+
)
87+
EOF
88+
cat $(rlocation io_bazel/src/test/shell/bazel/testdata/jdk_http_archives) >> WORKSPACE
89+
}
90+
91+
function test_java14_record_type() {
92+
mkdir -p java/main
93+
cat >java/main/BUILD <<EOF
94+
java_binary(
95+
name = 'Javac14Example',
96+
srcs = ['Javac14Example.java'],
97+
main_class = 'Javac14Example',
98+
javacopts = ["--enable-preview"],
99+
jvm_flags = ["--enable-preview"],
100+
)
101+
EOF
102+
103+
cat >java/main/Javac14Example.java <<EOF
104+
public class Javac14Example {
105+
record Point(int x, int y) {}
106+
public static void main(String[] args) {
107+
Point point = new Point(0, 1);
108+
System.out.println(point.x);
109+
}
110+
}
111+
EOF
112+
bazel run java/main:Javac14Example --test_output=all --verbose_failures &>"${TEST_log}"
113+
expect_log "0"
114+
}
115+
116+
run_suite "Tests new Java 14 language features"

src/test/shell/bazel/testdata/jdk_http_archives

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ http_archive(
4848
],
4949
)
5050

51+
################### Remote java_tools with embedded javac 14 ###################
52+
# This must be kept in sync with the top-level WORKSPACE file.
53+
http_archive(
54+
name = "remote_java_tools_javac14_test_linux",
55+
sha256 = "3997ee9a57b095748f1c0d084839fab2fbc72504aeb7b37b1f71c31738d330e3",
56+
urls = ["https://mirror.bazel.build/bazel_java_tools/releases/javac14/v1.0/java_tools_javac14_linux-v1.0.zip"],
57+
)
58+
# This must be kept in sync with the top-level WORKSPACE file.
59+
http_archive(
60+
name = "remote_java_tools_javac14_test_windows",
61+
sha256 = "cfad1718dad1fed12816748eed27ab30b9ea1268c8ce9940acf3b5b7d82d483d",
62+
urls = [
63+
"https://mirror.bazel.build/bazel_java_tools/releases/javac14/v1.0/java_tools_javac14_windows-v1.0.zip",
64+
],
65+
)
66+
# This must be kept in sync with the top-level WORKSPACE file.
67+
http_archive(
68+
name = "remote_java_tools_javac14_test_darwin",
69+
sha256 = "54df966e7583bafe659e39b4103a4ce934201d969de638d071ada07d8e0c1a3a",
70+
urls = [
71+
"https://mirror.bazel.build/bazel_java_tools/releases/javac14/v1.0/java_tools_javac14_darwin-v1.0.zip",
72+
],
73+
)
74+
5175
#################################### JDK 11 ####################################
5276
# This must be kept in sync with the top-level WORKSPACE file.
5377
http_archive(
@@ -99,3 +123,29 @@ http_archive(
99123
strip_prefix = "zulu12.2.3-ca-jdk12.0.1-win_x64",
100124
urls = ["https://mirror.bazel.build/openjdk/azul-zulu12.2.3-ca-jdk12.0.1/zulu12.2.3-ca-jdk12.0.1-win_x64.zip"],
101125
)
126+
127+
#################################### JDK 14 ####################################
128+
# This must be kept in sync with the top-level WORKSPACE file.
129+
http_archive(
130+
name = "openjdk14_linux_archive",
131+
build_file_content = "java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public'])",
132+
sha256 = "48bb8947034cd079ad1ef83335e7634db4b12a26743a0dc314b6b861480777aa",
133+
strip_prefix = "zulu14.28.21-ca-jdk14.0.1-linux_x64",
134+
urls = ["https://cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-linux_x64.tar.gz"],
135+
)
136+
# This must be kept in sync with the top-level WORKSPACE file.
137+
http_archive(
138+
name = "openjdk14_darwin_archive",
139+
build_file_content = "java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public'])",
140+
sha256 = "67ca9d285056132ebb19fa237a14affda52132142e1171fe1c20e18974b3b8a5",
141+
strip_prefix = "zulu12.2.3-ca-jdk12.0.1-macosx_x64",
142+
urls = ["https://mirror.bazel.build/openjdk/azul-zulu12.2.3-ca-jdk12.0.1/zulu12.2.3-ca-jdk12.0.1-macosx_x64.tar.gz"],
143+
)
144+
# This must be kept in sync with the top-level WORKSPACE file.
145+
http_archive(
146+
name = "openjdk14_windows_archive",
147+
build_file_content = "java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public'])",
148+
sha256 = "cf28404c23c3aa1115363ba6e796c30580a768e1d7d6681a7d053e516008e00d",
149+
strip_prefix = "zulu12.2.3-ca-jdk12.0.1-win_x64",
150+
urls = ["https://mirror.bazel.build/openjdk/azul-zulu12.2.3-ca-jdk12.0.1/zulu12.2.3-ca-jdk12.0.1-win_x64.zip"],
151+
)

tools/jdk/BUILD.java_tools

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ java_toolchain(
9595
"--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
9696

9797
# override the javac in the JDK.
98-
"--patch-module=java.compiler=$(location :java_compiler_jar)",
99-
"--patch-module=jdk.compiler=$(location :jdk_compiler_jar)",
98+
#"--patch-module=java.compiler=$(location :java_compiler_jar)",
99+
#"--patch-module=jdk.compiler=$(location :jdk_compiler_jar)",
100100

101101
# quiet warnings from com.google.protobuf.UnsafeUtil,
102102
# see: https://github.com/google/protobuf/issues/3781

0 commit comments

Comments
 (0)