Skip to content

Commit 41b764a

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 3eb806e commit 41b764a

File tree

9 files changed

+313
-17
lines changed

9 files changed

+313
-17
lines changed

WORKSPACE

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,16 @@ http_archive(
701701
],
702702
)
703703

704+
705+
# TODO(davido): This is not needed, because jdk compiler module patching is disabled for JDK 14
706+
http_archive(
707+
name = "java_tools_langtools_javac14",
708+
sha256 = "cf0814fa002ef3d794582bb086516d8c9ed0958f83f19799cdb08949019fe4c7",
709+
urls = [
710+
"https://mirror.bazel.build/bazel_java_tools/jdk_langtools/langtools_jdk11_v2.zip",
711+
],
712+
)
713+
704714
# This must be kept in sync with src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_remote_tools.WORKSPACE
705715
http_archive(
706716
name = "android_tools_for_testing",
@@ -779,6 +789,39 @@ http_archive(
779789
urls = ["https://mirror.bazel.build/openjdk/azul-zulu11.37.17-ca-jdk11.0.6/zulu11.37.17-ca-jdk11.0.6-win_x64.zip"],
780790
)
781791

792+
# This must be kept in sync with src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
793+
http_archive(
794+
name = "remotejdk14_linux_for_testing",
795+
build_file = "@local_jdk//:BUILD.bazel",
796+
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
797+
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
798+
sha256 = "48bb8947034cd079ad1ef83335e7634db4b12a26743a0dc314b6b861480777aa",
799+
strip_prefix = "zulu14.28.21-ca-jdk14.0.1-linux_x64",
800+
urls = ["https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-linux_x64.tar.gz"],
801+
)
802+
803+
# This must be kept in sync with src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
804+
http_archive(
805+
name = "remotejdk14_macos_for_testing",
806+
build_file = "@local_jdk//:BUILD.bazel",
807+
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
808+
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
809+
sha256 = "088bd4d0890acc9f032b738283bf0f26b2a55c50b02d1c8a12c451d8ddf080dd",
810+
strip_prefix = "zulu14.28.21-ca-jdk14.0.1-macosx_x64",
811+
urls = ["https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-macosx_x64.tar.gz"],
812+
)
813+
814+
# This must be kept in sync with src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
815+
http_archive(
816+
name = "remotejdk14_win_for_testing",
817+
build_file = "@local_jdk//:BUILD.bazel",
818+
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
819+
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
820+
sha256 = "9cb078b5026a900d61239c866161f0d9558ec759aa15c5b4c7e905370e868284",
821+
strip_prefix = "zulu14.28.21-ca-jdk14.0.1-win_x64",
822+
urls = ["https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-win_x64.zip"],
823+
)
824+
782825
# This must be kept in sync with src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
783826
http_archive(
784827
name = "remote_java_tools_linux_for_testing",
@@ -884,6 +927,42 @@ exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
884927
urls = ["https://mirror.bazel.build/openjdk/azul-zulu11.37.17-ca-jdk11.0.6/zulu11.37.17-ca-jdk11.0.6-win_x64.zip"],
885928
)
886929

930+
# This must be kept in sync with src/test/shell/bazel/testdata/jdk_http_archives.
931+
http_archive(
932+
name = "openjdk14_linux_archive",
933+
build_file_content = """
934+
java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public'])
935+
exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
936+
""",
937+
sha256 = "48bb8947034cd079ad1ef83335e7634db4b12a26743a0dc314b6b861480777aa",
938+
strip_prefix = "zulu14.28.21-ca-jdk14.0.1-linux_x64",
939+
urls = ["https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-linux_x64.tar.gz"],
940+
)
941+
942+
# This must be kept in sync with src/test/shell/bazel/testdata/jdk_http_archives.
943+
http_archive(
944+
name = "openjdk14_darwin_archive",
945+
build_file_content = """
946+
java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public'])
947+
exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
948+
""",
949+
sha256 = "088bd4d0890acc9f032b738283bf0f26b2a55c50b02d1c8a12c451d8ddf080dd",
950+
strip_prefix = "zulu14.28.21-ca-jdk14.0.1-macosx_x64",
951+
urls = ["https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-macosx_x64.tar.gz"],
952+
)
953+
954+
# This must be kept in sync with src/test/shell/bazel/testdata/jdk_http_archives.
955+
http_archive(
956+
name = "openjdk14_windows_archive",
957+
build_file_content = """
958+
java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public'])
959+
exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
960+
""",
961+
sha256 = "9cb078b5026a900d61239c866161f0d9558ec759aa15c5b4c7e905370e868284",
962+
strip_prefix = "zulu14.28.21-ca-jdk14.0.1-win_x64",
963+
urls = ["https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-win_x64.zip"],
964+
)
965+
887966
load("@io_bazel_skydoc//:setup.bzl", "stardoc_repositories")
888967

889968
stardoc_repositories()

src/BUILD

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ JAVA_TOOLS_DEPLOY_JARS = [
567567
"//conditions:default": [],
568568
})
569569

570-
JAVA_VERSIONS = ("11",)
570+
JAVA_VERSIONS = ("11", "14")
571571

572572
[
573573
genrule(
@@ -638,8 +638,8 @@ JAVA_VERSIONS = ("11",)
638638
[
639639
# The java_tools releases can have BUILD files that vary depending on the
640640
# javac version they embed. Currently the only difference is in the
641-
# java_toolchain source version which has to be 12 for javac 12 to be able
642-
# to build new Java 12 features.
641+
# java_toolchain source version which has to be 14 for javac 14 to be able
642+
# to build new Java 14 features.
643643
genrule(
644644
name = "create_java_tools_build_java" + java_version,
645645
srcs = ["//tools/jdk:BUILD.java_tools"],
@@ -744,6 +744,9 @@ filegroup(
744744
"@openjdk11_darwin_archive//:WORKSPACE",
745745
"@openjdk11_linux_archive//:WORKSPACE",
746746
"@openjdk11_windows_archive//:WORKSPACE",
747+
"@openjdk14_darwin_archive//:WORKSPACE",
748+
"@openjdk14_linux_archive//:WORKSPACE",
749+
"@openjdk14_windows_archive//:WORKSPACE",
747750
"@openjdk_linux_aarch64_minimal//file",
748751
"@openjdk_linux_minimal//file",
749752
"@openjdk_macos_minimal//file",
@@ -760,6 +763,9 @@ filegroup(
760763
"@remotejdk11_linux_ppc64le_for_testing//:WORKSPACE",
761764
"@remotejdk11_macos_for_testing//:WORKSPACE",
762765
"@remotejdk11_win_for_testing//:WORKSPACE",
766+
"@remotejdk14_linux_for_testing//:WORKSPACE",
767+
"@remotejdk14_macos_for_testing//:WORKSPACE",
768+
"@remotejdk14_win_for_testing//:WORKSPACE",
763769
"@rules_cc//:WORKSPACE",
764770
"@rules_java//:WORKSPACE",
765771
"@rules_pkg//:WORKSPACE",

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,38 @@ maybe(
154154
],
155155
)
156156

157+
# This must be kept in sync with the top-level WORKSPACE file.
158+
maybe(
159+
http_archive,
160+
name = "remotejdk14_linux",
161+
build_file = "@local_jdk//:BUILD.bazel",
162+
sha256 = "48bb8947034cd079ad1ef83335e7634db4b12a26743a0dc314b6b861480777aa",
163+
strip_prefix = "zulu14.28.21-ca-jdk14.0.1-linux_x64",
164+
urls = [
165+
"https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-linux_x64.tar.gz",
166+
],
167+
)
168+
169+
# This must be kept in sync with the top-level WORKSPACE file.
170+
maybe(
171+
http_archive,
172+
name = "remotejdk14_macos",
173+
build_file = "@local_jdk//:BUILD.bazel",
174+
sha256 = "088bd4d0890acc9f032b738283bf0f26b2a55c50b02d1c8a12c451d8ddf080dd",
175+
strip_prefix = "zulu14.28.21-ca-jdk14.0.1-macosx_x64",
176+
urls = ["https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-macosx_x64.tar.gz"],
177+
)
178+
179+
# This must be kept in sync with the top-level WORKSPACE file.
180+
maybe(
181+
http_archive,
182+
name = "remotejdk14_win",
183+
build_file = "@local_jdk//:BUILD.bazel",
184+
sha256 = "9cb078b5026a900d61239c866161f0d9558ec759aa15c5b4c7e905370e868284",
185+
strip_prefix = "zulu14.28.21-ca-jdk14.0.1-win_x64",
186+
urls = ["https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-win_x64.zip"],
187+
)
188+
157189
# This must be kept in sync with the top-level WORKSPACE file.
158190
maybe(
159191
http_archive,

src/test/py/bazel/test_base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class TestBase(unittest.TestCase):
6161
'remotejdk11_linux_ppc64le_for_testing',
6262
'remotejdk11_macos_for_testing',
6363
'remotejdk11_win_for_testing',
64+
'remotejdk14_linux_for_testing',
65+
'remotejdk14_macos_for_testing',
66+
'remotejdk14_win_for_testing',
6467
'remote_java_tools_darwin_for_testing',
6568
'remote_java_tools_linux_for_testing',
6669
'remote_java_tools_windows_for_testing',

src/test/shell/bazel/BUILD

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,30 @@ sh_test(
178178
],
179179
)
180180

181+
sh_test(
182+
name = "bazel_java14_test",
183+
srcs = ["bazel_java14_test.sh"],
184+
tags = ["local"],
185+
args = [
186+
# --java_toolchain and --host_java_toolchain values
187+
"@local_java_tools//:toolchain_jdk_14",
188+
# java_tools zip to test
189+
"src/java_tools_java14.zip",
190+
] + select({
191+
# --javabase and --host_javabase values
192+
"//src/conditions:darwin": ["@openjdk12_darwin_archive//:runtime"],
193+
"//src/conditions:darwin_x86_64": ["@openjdk12_darwin_archive//:runtime"],
194+
"//src/conditions:windows": ["@openjdk12_windows_archive//:runtime"],
195+
"//src/conditions:linux_x86_64": ["@openjdk14_linux_archive//:runtime"],
196+
}),
197+
data = [
198+
":test-deps",
199+
"//src:java_tools_java14_zip",
200+
"//src/test/shell/bazel/testdata:jdk_http_archives_filegroup",
201+
"@bazel_tools//tools/bash/runfiles",
202+
],
203+
)
204+
181205
sh_test(
182206
name = "bazel_java_test",
183207
# TODO(iirina): Investigate if the 'large' and 'eternal' values still apply.
@@ -196,7 +220,10 @@ sh_test(
196220
exec_compatible_with = ["//:highcpu_machine"],
197221
)
198222

199-
JAVA_VERSIONS = ("11",)
223+
JAVA_VERSIONS = ("11", "14")
224+
225+
# TODO(davido): Remove this once remote_java_tools_javac14 is released
226+
JAVA_TOOLS_VERSIONS = ("11",)
200227

201228
[
202229
sh_test(
@@ -224,7 +251,7 @@ JAVA_VERSIONS = ("11",)
224251
],
225252
exec_compatible_with = ["//:highcpu_machine"],
226253
)
227-
for java_version in JAVA_VERSIONS
254+
for java_version in JAVA_TOOLS_VERSIONS
228255
]
229256

230257
[
@@ -253,7 +280,7 @@ JAVA_VERSIONS = ("11",)
253280
],
254281
exec_compatible_with = ["//:highcpu_machine"],
255282
)
256-
for java_version in JAVA_VERSIONS
283+
for java_version in JAVA_TOOLS_VERSIONS
257284
]
258285

259286
[
@@ -467,7 +494,7 @@ sh_test(
467494
"no_windows",
468495
],
469496
)
470-
for java_version in JAVA_VERSIONS
497+
for java_version in JAVA_TOOLS_VERSIONS
471498
]
472499

473500
# Test java coverage with the java_toolchain in the java_tools zip built at head.
@@ -496,7 +523,7 @@ sh_test(
496523
"no_windows",
497524
],
498525
)
499-
for java_version in JAVA_VERSIONS
526+
for java_version in JAVA_TOOLS_VERSIONS
500527
]
501528

502529
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"

0 commit comments

Comments
 (0)