Skip to content

Commit fe291f7

Browse files
davidocopybara-github
authored andcommitted
Add support for toolchain java 14
Closes #11017. Test Plan: 1. Create java_tools from this commit: ```bash $ bazel build src:java_tools_java14.zip ``` 2. Switch to using the java_tools from the above step in WORKSPACE file: ```python 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: ```python 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: ```bash 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: ```java 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: ```python 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 ``` Closes #11514. PiperOrigin-RevId: 322759522
1 parent 28ae455 commit fe291f7

File tree

10 files changed

+344
-14
lines changed

10 files changed

+344
-14
lines changed

WORKSPACE

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

704+
704705
# This must be kept in sync with src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_remote_tools.WORKSPACE
705706
http_archive(
706707
name = "android_tools_for_testing",
@@ -779,6 +780,39 @@ http_archive(
779780
urls = ["https://mirror.bazel.build/openjdk/azul-zulu11.37.17-ca-jdk11.0.6/zulu11.37.17-ca-jdk11.0.6-win_x64.zip"],
780781
)
781782

783+
# This must be kept in sync with src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
784+
http_archive(
785+
name = "remotejdk14_linux_for_testing",
786+
build_file = "@local_jdk//:BUILD.bazel",
787+
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
788+
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
789+
sha256 = "48bb8947034cd079ad1ef83335e7634db4b12a26743a0dc314b6b861480777aa",
790+
strip_prefix = "zulu14.28.21-ca-jdk14.0.1-linux_x64",
791+
urls = ["https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-linux_x64.tar.gz"],
792+
)
793+
794+
# This must be kept in sync with src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
795+
http_archive(
796+
name = "remotejdk14_macos_for_testing",
797+
build_file = "@local_jdk//:BUILD.bazel",
798+
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
799+
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
800+
sha256 = "088bd4d0890acc9f032b738283bf0f26b2a55c50b02d1c8a12c451d8ddf080dd",
801+
strip_prefix = "zulu14.28.21-ca-jdk14.0.1-macosx_x64",
802+
urls = ["https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-macosx_x64.tar.gz"],
803+
)
804+
805+
# This must be kept in sync with src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
806+
http_archive(
807+
name = "remotejdk14_win_for_testing",
808+
build_file = "@local_jdk//:BUILD.bazel",
809+
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
810+
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
811+
sha256 = "9cb078b5026a900d61239c866161f0d9558ec759aa15c5b4c7e905370e868284",
812+
strip_prefix = "zulu14.28.21-ca-jdk14.0.1-win_x64",
813+
urls = ["https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-win_x64.zip"],
814+
)
815+
782816
# This must be kept in sync with src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
783817
http_archive(
784818
name = "remote_java_tools_linux_for_testing",
@@ -884,6 +918,42 @@ exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
884918
urls = ["https://mirror.bazel.build/openjdk/azul-zulu11.37.17-ca-jdk11.0.6/zulu11.37.17-ca-jdk11.0.6-win_x64.zip"],
885919
)
886920

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

889959
stardoc_repositories()

src/BUILD

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

570-
JAVA_VERSIONS = ("11",)
570+
JAVA_VERSIONS = ("11", "14")
571+
572+
# TODO(davido): Hard code the javac 11 for now; it is required for default toolchain.
571573

572574
[
573575
genrule(
574576
name = "jars_java_tools_java" + java_version + "_zip",
575577
srcs = JAVA_TOOLS_DEPLOY_JARS + [
576-
"@java_tools_langtools_javac" + java_version + "//:jdk_compiler_jar",
577-
"@java_tools_langtools_javac" + java_version + "//:java_compiler_jar",
578-
"@java_tools_langtools_javac" + java_version + "//:javac_jar",
578+
"@java_tools_langtools_javac11//:jdk_compiler_jar",
579+
"@java_tools_langtools_javac11//:java_compiler_jar",
580+
"@java_tools_langtools_javac11//:javac_jar",
579581
],
580582
outs = ["jars_java_tools_java" + java_version + ".zip"],
581583
cmd = "zip -qjX $@ $$(echo $(SRCS) | sort)",
@@ -598,7 +600,7 @@ JAVA_VERSIONS = ("11",)
598600
"//third_party/ijar:transitive_sources",
599601
"//third_party/java/jacoco:transitive_sources",
600602
"//third_party/java/proguard:srcs",
601-
"@java_tools_langtools_javac" + java_version + "//:srcs",
603+
"@java_tools_langtools_javac11//:srcs",
602604
],
603605
outs = ["java_tools_dist_javac" + java_version + ".zip"],
604606
cmd = "zip -qXr $@ $$(echo $(SRCS) | sort)",
@@ -638,8 +640,10 @@ JAVA_VERSIONS = ("11",)
638640
[
639641
# The java_tools releases can have BUILD files that vary depending on the
640642
# 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.
643+
# java_toolchain source version which has to be 14 for javac 14 to be able
644+
# to build new Java 14 features. This is not used atm, as the toolchain for
645+
# javac 14 was duplicated, but it might be used in future Bazel releases to
646+
# support new javac release, so that we preserve this step for now.
643647
genrule(
644648
name = "create_java_tools_build_java" + java_version,
645649
srcs = ["//tools/jdk:BUILD.java_tools"],
@@ -744,6 +748,9 @@ filegroup(
744748
"@openjdk11_darwin_archive//:WORKSPACE",
745749
"@openjdk11_linux_archive//:WORKSPACE",
746750
"@openjdk11_windows_archive//:WORKSPACE",
751+
"@openjdk14_darwin_archive//:WORKSPACE",
752+
"@openjdk14_linux_archive//:WORKSPACE",
753+
"@openjdk14_windows_archive//:WORKSPACE",
747754
"@openjdk_linux_aarch64_minimal//file",
748755
"@openjdk_linux_minimal//file",
749756
"@openjdk_macos_minimal//file",
@@ -760,6 +767,9 @@ filegroup(
760767
"@remotejdk11_linux_ppc64le_for_testing//:WORKSPACE",
761768
"@remotejdk11_macos_for_testing//:WORKSPACE",
762769
"@remotejdk11_win_for_testing//:WORKSPACE",
770+
"@remotejdk14_linux_for_testing//:WORKSPACE",
771+
"@remotejdk14_macos_for_testing//:WORKSPACE",
772+
"@remotejdk14_win_for_testing//:WORKSPACE",
763773
"@rules_cc//:WORKSPACE",
764774
"@rules_java//:WORKSPACE",
765775
"@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: 29 additions & 2 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+
args = [
185+
# --java_toolchain and --host_java_toolchain values
186+
"@local_java_tools//:toolchain_jdk_14",
187+
# java_tools zip to test
188+
"src/java_tools_java14.zip",
189+
] + select({
190+
# --javabase and --host_javabase values
191+
"//src/conditions:darwin": ["@openjdk14_darwin_archive//:runtime"],
192+
"//src/conditions:darwin_x86_64": ["@openjdk14_darwin_archive//:runtime"],
193+
"//src/conditions:windows": ["@openjdk14_windows_archive//:runtime"],
194+
"//src/conditions:linux_x86_64": ["@openjdk14_linux_archive//:runtime"],
195+
}),
196+
data = [
197+
":test-deps",
198+
"//src:java_tools_java14_zip",
199+
"//src/test/shell/bazel/testdata:jdk_http_archives_filegroup",
200+
"@bazel_tools//tools/bash/runfiles",
201+
],
202+
tags = ["local"],
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(
@@ -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.
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)