Skip to content
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

Add support for toolchain java 14 #11514

Closed

Conversation

davido
Copy link
Contributor

@davido davido commented May 28, 2020

Closes #11017.

Test Plan:

  1. Create java_tools from this commit:
  $ bazel build src:java_tools_java14.zip
  1. 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>",
    ],
  )
  1. 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"],
  )
  1. 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
  1. 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);
    }
  }
  1. 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",
  )
  1. 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

@davido davido marked this pull request as draft May 29, 2020 05:53
@jin jin added the WIP label Jun 15, 2020
@jin
Copy link
Member

jin commented Jun 15, 2020

cc @lberki

@lberki lberki requested a review from comius June 24, 2020 12:59
@lberki
Copy link
Contributor

lberki commented Jun 24, 2020

Thanks for the month of patience! We should really have reviewed this earlier but... things came up.

@comius
Copy link
Contributor

comius commented Jun 25, 2020

Dear @davido, sorry for the waiting. I did a quick general review of your pull request and at first sight it seems this is a work in progress.

Did you plan to do additional work on it? Do you need some support with it?

Also may I ask what was the reasoning to drop Java 12 support?

Thanks again for your effort.

@davido
Copy link
Contributor Author

davido commented Jun 25, 2020

Dear @davido, sorry for the waiting. I did a quick general review of your pull request and at first sight it seems this is a work in progress.

Right. It depends on my Error Prone 2.4.0 PR. Because currently used EP doesn't support Java 14 yet.

Did you plan to do additional work on it? Do you need some support with it?

Yes, I will resume the work on this PR, once EP upgrade is landed.

Also may I ask what was the reasoning to drop Java 12 support?

In fact we could preserve Java 12, but what is the point? Java 12 and 13 are dead.

Thanks again for your effort.

One thing so solve is to conditionally remove these two lines in toolchain definition for java toolchans >= 14:

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

Do you have a suggestion how to do it: preserve those lines for Java 8, 11 and 12 toolchains, but remove them for 14, 15, 17, ...?

One idea I had, is to duplicate this toolchain, and name it differently, say: toolchain_new_jdk_(14|15|17) or something? Because right now this PR broke toolchains for Java 8, 11 and 12 (unconditionally removing the above lines).

@comius
Copy link
Contributor

comius commented Jun 25, 2020

In fact we could preserve Java 12, but what is the point? Java 12 and 13 are dead.

Well one thing I can think of is, there might be people out there with projects depending on Java 12. Having the option and not being forced into switch might help them.

One thing so solve is to conditionally remove these two lines in toolchain definition for java toolchans >= 14:

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

Do you have a suggestion how to do it: preserve those lines for Java 8, 11 and 12 toolchains, but remove them for 14, 15, 17, ...?

One idea I had, is to duplicate this toolchain, and name it differently, say: toolchain_new_jdk_(14|15|17) or something? Because right now this PR broke toolchains for Java 8, 11 and 12 (unconditionally removing the above lines).

You can use inline if there, something like ] + ( [] if JavaVersion >= 12 else ["--patch1", "--patch2"]) + [ or even better declare a variable before the rule:

if JavaVersion >= 12: 
  _PATCH=[] 
else: 
  _PATCH = ["p1","p2"]

java_toolchain(
  ...
  jvm_args = [...] + _PATCH + [...]

How to actually properly write the condition for java version or if it is even possible I don't know. If you cannot write it then yes, your best option is to split into two toolchains.

@davido
Copy link
Contributor Author

davido commented Jun 29, 2020

@comius Thanks @philwo, EP 2.4.0 upgrade was merged today and new java_tools release was conducted.

I will resume the work here. Also note, that Java 14 toolchain is removed now: #11666.

I should probably wait until the above PR is merged, because this PR touched the same code area.

@davido davido force-pushed the add-support-for-toolchain-java14 branch 9 times, most recently from f091346 to f9ae943 Compare July 14, 2020 19:21
@davido
Copy link
Contributor Author

davido commented Jul 14, 2020

Cc @aiuto @philwo @meteorcloudy

Some tests are failing, e.g.:

https://buildkite.com/bazel/bazel-bazel-github-presubmit/builds/6456#98c62ab3-76df-4257-9ec3-0d15cc9803c4

because download doesn't work:

https://storage.googleapis.com/bazel-untrusted-buildkite-artifacts/98c62ab3-76df-4257-9ec3-0d15cc9803c4/src/test/shell/bazel/bazel_java_test_jdk14_prebuilt_toolchain_head/attempt_1.log

Analyzing: target //java/main:main (1 packages loaded, 0 targets configured)
INFO: Repository openjdk14_linux_archive instantiated at:
  no stack (--record_rule_instantiation_callstack not enabled)
Repository rule http_archive defined at:
  /var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/ec321eb2cc2d0f8f91b676b6d4c66c29/sandbox/linux-sandbox/5285/execroot/io_bazel/_tmp/d618688373d8c1dc5ac3a5dc5a7bcac8/root/c8d20c307803200fe58c026c171542fc/external/bazel_tools/tools/build_defs/repo/http.bzl:336:31: in <toplevel>
WARNING: Download from https://cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-linux_x64.tar.gz failed: class com.google.devtools.build.lib.bazel.repository.downloader.UnrecoverableHttpException Unknown host: cdn.azul.com
ERROR: An error occurred during the fetch of repository 'openjdk14_linux_archive':
   java.io.IOException: Error downloading [https://cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-linux_x64.tar.gz] to /var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/ec321eb2cc2d0f8f91b676b6d4c66c29/sandbox/linux-sandbox/5285/execroot/io_bazel/_tmp/d618688373d8c1dc5ac3a5dc5a7bcac8/root/c8d20c307803200fe58c026c171542fc/external/openjdk14_linux_archive/zulu14.28.21-ca-jdk14.0.1-linux_x64.tar.gz: Unknown host: cdn.azul.com
ERROR: /var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/ec321eb2cc2d0f8f91b676b6d4c66c29/sandbox/linux-sandbox/5285/execroot/io_bazel/_tmp/d618688373d8c1dc5ac3a5dc5a7bcac8/root/c8d20c307803200fe58c026c171542fc/external/bazel_tools/tools/jdk/BUILD:66:26: @bazel_tools//tools/jdk:legacy_current_java_runtime depends on @openjdk14_linux_archive//:runtime in repository @openjdk14_linux_archive which failed to fetch. no such package '@openjdk14_linux_archive//': java.io.IOException: Error downloading [https://cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-linux_x64.tar.gz] to /var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/ec321eb2cc2d0f8f91b676b6d4c66c29/sandbox/linux-sandbox/5285/execroot/io_bazel/_tmp/d618688373d8c1dc5ac3a5dc5a7bcac8/root/c8d20c307803200fe58c026c171542fc/external/openjdk14_linux_archive/zulu14.28.21-ca-jdk14.0.1-linux_x64.tar.gz: Unknown host: cdn.azul.com
ERROR: Analysis of target '//java/main:main' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.147s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (17 packages loaded, 57 targets configured)
FAILED: Build did NOT complete successfully (17 packages loaded, 57 targets configured)

Can you help to upload the required artifact to bazel mirror?

This test is passing as expected here:

  $ bazel test src/test/shell/bazel/bazel_java_test_jdk14_prebuilt_toolchain_head
DEBUG: /home/davido/.cache/bazel/_bazel_davido/4596c3304267e4c44bca87d41e038f29/external/bazel_toolchains/rules/rbe_repo/version_check.bzl:59:14: 
Current running Bazel is not a release version and one was not defined explicitly in rbe_autoconfig target. Falling back to '3.1.0'
DEBUG: /home/davido/.cache/bazel/_bazel_davido/4596c3304267e4c44bca87d41e038f29/external/bazel_toolchains/rules/rbe_repo/checked_in.bzl:103:14: rbe_ubuntu1804_java11 not using checked in configs as detect_java_home was set to True 
DEBUG: /home/davido/.cache/bazel/_bazel_davido/4596c3304267e4c44bca87d41e038f29/external/bazel_toolchains/rules/rbe_repo/version_check.bzl:59:14: 
Current running Bazel is not a release version and one was not defined explicitly in rbe_autoconfig target. Falling back to '3.1.0'
DEBUG: /home/davido/.cache/bazel/_bazel_davido/4596c3304267e4c44bca87d41e038f29/external/bazel_toolchains/rules/rbe_repo/checked_in.bzl:103:14: rbe_ubuntu1604_java8 not using checked in configs as detect_java_home was set to True 
DEBUG: /home/davido/.cache/bazel/_bazel_davido/4596c3304267e4c44bca87d41e038f29/external/build_bazel_rules_nodejs/internal/common/check_bazel_version.bzl:47:14: 
Current Bazel is not a release version, cannot check for compatibility.
DEBUG: /home/davido/.cache/bazel/_bazel_davido/4596c3304267e4c44bca87d41e038f29/external/build_bazel_rules_nodejs/internal/common/check_bazel_version.bzl:49:14: Make sure that you are running at least Bazel 0.21.0.
DEBUG: /home/davido/.cache/bazel/_bazel_davido/4596c3304267e4c44bca87d41e038f29/external/build_bazel_rules_nodejs/internal/common/check_bazel_version.bzl:47:14: 
Current Bazel is not a release version, cannot check for compatibility.
DEBUG: /home/davido/.cache/bazel/_bazel_davido/4596c3304267e4c44bca87d41e038f29/external/build_bazel_rules_nodejs/internal/common/check_bazel_version.bzl:49:14: Make sure that you are running at least Bazel 0.21.0.
INFO: Analyzed target //src/test/shell/bazel:bazel_java_test_jdk14_prebuilt_toolchain_head (9 packages loaded, 224 targets configured).
INFO: Found 1 test target...
Target //src/test/shell/bazel:bazel_java_test_jdk14_prebuilt_toolchain_head up-to-date:
  bazel-bin/src/test/shell/bazel/bazel_java_test_jdk14_prebuilt_toolchain_head
INFO: Elapsed time: 141.694s, Critical Path: 141.28s
INFO: 1 process: 1 linux-sandbox.
INFO: Build completed successfully, 5 total actions
//src/test/shell/bazel:bazel_java_test_jdk14_prebuilt_toolchain_head     PASSED in 140.9s

Executed 1 out of 1 test: 1 test passes.
INFO: Build completed successfully, 5 total actions

@davido
Copy link
Contributor Author

davido commented Jul 15, 2020

When trying to build Gerrit with java_tools produced by this PR the log is flooded with:

INFO: From Compiling Java headers javatests/com/google/gerrit/server/query/group/libabstract_query_tests-hjar.jar (1 source file):
OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
INFO: From Compiling Java headers javatests/com/google/gerrit/server/query/project/libabstract_query_tests-hjar.jar (1 source file):
OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
INFO: From Compiling Java headers javatests/com/google/gerrit/server/query/change/libabstract_query_tests-hjar.jar (2 source files):
OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.

This is because of: https://bugs.openjdk.java.net/browse/JDK-8214731.

Should -Xverify:none be removed from Bazel entirely?

@meteorcloudy
Copy link
Member

@davido
Copy link
Contributor Author

davido commented Jul 15, 2020

https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-linux_x64.tar.gz should be available now.

Thanks for the quick upload. Could you also upload the tars for Darwin and Wndows? Thanks!

@davido davido force-pushed the add-support-for-toolchain-java14 branch 3 times, most recently from a39aa88 to 8696269 Compare July 16, 2020 06:29
@davido
Copy link
Contributor Author

davido commented Jul 16, 2020

bazel_java14_test failure on Mac OS X seems to be related:

ERROR: /private/var/tmp/_bazel_buildkite/d4b6b1e46f2b3ed9c4c55e002c0dab54/execroot/io_bazel/_tmp/8cb3af0b3725c67c81f5d69657690e6a/root/41c9bd7314f6c8ec46377daf39a1a7ad/external/bazel_tools/tools/jdk/BUILD:66:26: every rule of type java_runtime_alias implicitly depends upon the target '@openjdk12_darwin_archive//:runtime', but this target could not be found because of: no such package '@openjdk12_darwin_archive//': The repository '@openjdk12_darwin_archive' could not be resolved
ERROR: Analysis of target '//java/main:Javac14Example' failed; build aborted: Analysis failed
INFO: Elapsed time: 5.181s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (14 packages loaded, 45 targets configured)
ERROR: Build failed. Not running target
FAILED: Build did NOT complete successfully (14 packages loaded, 45 targets configured)

And on Windows:

ERROR: C:/b/komsf7d6/execroot/io_bazel/_tmp/a91705879816f2a5a46f5e840d98ba73/root/ogmfis5l/external/bazel_tools/tools/jdk/BUILD:66:26: every rule of type java_runtime_alias implicitly depends upon the target '@openjdk12_windows_archive//:runtime', but this target could not be found because of: no such package '@openjdk12_windows_archive//': The repository '@openjdk12_windows_archive' could not be resolved
ERROR: Analysis of target '//java/main:Javac14Example' failed; build aborted: Analysis failed
INFO: Elapsed time: 4.272s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (15 packages loaded, 58 targets configured)
ERROR: Build failed. Not running target
FAILED: Build did NOT complete successfully (15 packages loaded, 58 targets configured)

It's trying to use wrong @openjdk12_(darwin|windows)_archive release:

external/bazel_tools/tools/jdk/BUILD:66:26: every rule of type java_runtime_alias implicitly depends upon the target 
'@openjdk12_windows_archive//:runtime', but this target could not be found because of: no such package 
'@openjdk12_windows_archive//': The repository '@openjdk12_windows_archive' could not be resolved
ERROR: Analysis of target '//java/main:Javac14Example' failed; build aborted: Analysis failed

@davido
Copy link
Contributor Author

davido commented Jul 16, 2020

On RBE gen_skylarklibrary test is failing with timeout, that doesn't seem to be related to this PR, link?

(10:34:24) ERROR: /workdir/src/main/java/com/google/devtools/build/lib/BUILD:521:8:
Executing genrule //src/main/java/com/google/devtools/build/lib:gen_skylarklibrary failed (Timeout):
bash failed: error executing command

@davido
Copy link
Contributor Author

davido commented Jul 16, 2020

The //src/test/shell/bazel:bazel_docgen_test test breakage on RBE seems to be unrelated?

(12:50:27) INFO: From Executing genrule //:bazel-distfile-tar:
./combine_distfiles_to_tar.sh: 44: [: Linux: unexpected operator
(13:00:04) ERROR: /workdir/src/main/java/com/google/devtools/build/lib/BUILD:521:8: Executing genrule //src/main/java/com/google/devtools/build/lib:gen_skylarklibrary failed (Timeout): bash failed: error executing command
(cd /var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/ec321eb2cc2d0f8f91b676b6d4c66c29/execroot/io_bazel && \
   exec env - \
     PATH=/bin:/usr/bin:/usr/local/bin \
     /bin/bash -c 'source external/bazel_tools/tools/genrule/genrule-setup.sh; mkdir -p bazel-out/k8-fastbuild/bin/src/main/java/com/google/devtools/build/lib/skylark-lib &&bazel-out/host/bin/src/main/java/com/google/devtools/build/docgen/skydoc_bin bazel-out/k8-fastbuild/bin/src/main/java/com/google/devtools/build/lib/skylark-lib && zip -qj bazel-out/k8-fastbuild/bin/src/main/java/com/google/devtools/build/lib/skylark-library.zip bazel-out/k8-fastbuild/bin/src/main/java/com/google/devtools/build/lib/skylark-lib/*')
  Execution platform: //:rbe_ubuntu1604_java8_platform (failed due to timeout.)
  Generating Starlark documentation...

@davido davido marked this pull request as ready for review July 16, 2020 13:07
@philwo
Copy link
Member

philwo commented Jul 16, 2020

@davido We've been seeing some test timeouts on RBE, not sure of the cause yet. If you believe it's unrelated, then there's a good chance that it is.

@davido
Copy link
Contributor Author

davido commented Jul 16, 2020

@davido We've been seeing some test timeouts on RBE, not sure of the cause yet. If you believe it's unrelated, then there's a good chance that it is.

Thanks, @philwo for confirming.

Can someone help to kick off the pipeline for releasing java_tools for toolchain JDK14 from this PR?

I'm not sure, how @iirina did it for JDK12, but there is "the chicken or the egg" problem. To release java_tools for JDK14,
we need this PR. But we also need to add some tests here, that are using remote_java_tools_javac14, that we can't
add yet, because such release is not available yet.

https://github.com/bazelbuild/java_tools/releases only has releases for javac11 with the current version: v9.0.

The workaround I used for now is to duplicate JAVA_VERSIONS with JAVA_TOOLS_VERSIONS and remove JDK release 14 declaration with:

JAVA_VERSIONS = ("11", "14")

# TODO(davido): Remove this once remote_java_tools_javac14 is released
JAVA_TOOLS_VERSIONS = ("11",)

and skip the bazel_coverage_java_jdk14_toolchain_released_test for now by using reduced JAVA_TOOLS_VERSIONS list to JDK release 11 only:

# Test java coverage with the java_toolchain in the released java_tools versions.
[
    sh_test(
        name = "bazel_coverage_java_jdk" + java_version + "toolchain_released_test",
        srcs = ["bazel_coverage_java_test.sh"],
        args = select({
            "//src/conditions:darwin": ["@remote_java_tools_javac" + java_version + "_test_darwin//:toolchain"],
            "//src/conditions:darwin_x86_64": ["@remote_java_tools_javac" + java_version + "_test_darwin//:toolchain"],
            "//src/conditions:windows": ["@remote_java_tools_javac" + java_version + "_test_windows//:toolchain"],
            "//src/conditions:linux_x86_64": ["@remote_java_tools_javac" + java_version + "_test_linux//:toolchain"],
        }) + [
            # java_tools zip to test
            "released",
            # --javabase value
        ] + select({
            "//src/conditions:darwin": ["@openjdk" + java_version + "_darwin_archive//:runtime"],
            "//src/conditions:darwin_x86_64": ["@openjdk" + java_version + "_darwin_archive//:runtime"],
            "//src/conditions:windows": ["@openjdk" + java_version + "_windows_archive//:runtime"],
            "//src/conditions:linux_x86_64": ["@openjdk" + java_version + "_linux_archive//:runtime"],
        }),
        data = [
            ":test-deps",
            "//src/test/shell/bazel/testdata:jdk_http_archives_filegroup",
        ],
        tags = [
            "no_windows",
        ],
    )
#    for java_version in JAVA_VERSIONS
    for java_version in JAVA_TOOLS_VERSIONS
]

@davido
Copy link
Contributor Author

davido commented Jul 20, 2020

@lberki @philwo @comius

Can we move forward with the review here to make sure toolchain for JDK14 is included in upcoming Bazel releases?

Of course, we could wait for general availbility of JDK 15 on 2020/09/15, see: [1], but if we always wait, we would never release anything new here ;-)

[1] https://openjdk.java.net/projects/jdk/15

Copy link
Contributor

@comius comius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @davido,

thanks for your amazing effort, and sorry for the delay. It took me some time to thoroughly review your PR. Those should now be the last changes.

WORKSPACE Outdated
@@ -701,6 +701,16 @@ http_archive(
],
)


# TODO(davido): This is not needed, because jdk compiler module patching is disabled for JDK 14
http_archive(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is potentially using a different version of Java compiler. Remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

src/BUILD Show resolved Hide resolved
tools/jdk/BUILD.java_tools Show resolved Hide resolved
@davido davido force-pushed the add-support-for-toolchain-java14 branch from 1dfb45a to f4cf7c5 Compare July 23, 2020 06:29
@davido
Copy link
Contributor Author

davido commented Jul 23, 2020

@comius

thanks for your amazing effort, and sorry for the delay. It took me some time to thoroughly review your PR. Those should now be the last changes.

I addressed your comments. PTAL.

@davido davido force-pushed the add-support-for-toolchain-java14 branch 4 times, most recently from 45ab7db to 97b2bd9 Compare July 23, 2020 09:27
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
@davido davido force-pushed the add-support-for-toolchain-java14 branch from 97b2bd9 to a36d987 Compare July 23, 2020 09:46
@davido
Copy link
Contributor Author

davido commented Jul 23, 2020

@comius

Split this genrule into Java 11 and Java 14, and remove the (wrong version of) compilers from Java 14.

I tried that, but the problem is that the default toolchain is still using them:

java_toolchain(
    name = "toolchain",
    bootclasspath = ["@bazel_tools//tools/jdk:platformclasspath"],
    forcibly_disable_header_compilation = 0,
    genclass = [":GenClass"],
    header_compiler = [":Turbine"],
    header_compiler_direct = [":TurbineDirect"],
    ijar = [":ijar"],
    jacocorunner = ":jacoco_coverage_runner_filegroup",
    javabuilder = [":JavaBuilder"],
    javac = [":javac_jar"],
[...]

So that it's still needed. I added a comment, that until default toolchain still relies on it, custom javac compiler should still be included into java_tools for toolchain for Java 14 and later.

Of course, when we abandon Java 8 and Java 11 (in 5 years?) we could remove custom javac from java_tools distribution.

@davido
Copy link
Contributor Author

davido commented Jul 23, 2020

@lberki @philwo @comius

Can we move forward here, and kick off java_tools release pipeline from this PR, so that java_tools for Java 14 is included in the next Bazel release (I'm concerned that we could miss it)?

For that I filed this issue in java_tools project: bazelbuild/java_tools#28.

@davido
Copy link
Contributor Author

davido commented Jul 23, 2020

@jin (or somebody else)

Could you please remove "WIP" label, as this PR is ready now.

@bazel-io bazel-io closed this in fe291f7 Jul 23, 2020
@davido davido deleted the add-support-for-toolchain-java14 branch July 23, 2020 11:15
davido added a commit to davido/bazel that referenced this pull request Jul 23, 2020
Closes bazelbuild#11017.

This is the follow-up for PR: [1]. Because of the chicken and the egg
problem, it has to be done on three steps:

1. add support for java toolchange JDK 14, done in: [1]
2. publish new java_tools_javac14, kicked off by the pipeline in
   java_tools repository
3. add unit test against released java_tools_javac4 published in step 2,
   this change

Test Plan:

  $ bazel test //src/test/shell/bazel:bazel_coverage_java_jdk14toolchain_released_test

[1] bazelbuild#11514
davido added a commit to davido/bazel that referenced this pull request Jul 23, 2020
Closes bazelbuild#11017.

This is the follow-up for PR: [1]. Because of the chicken and the egg
problem, it has to be done in three steps:

1. add support for java toolchange JDK 14, done in: [1]
2. publish new java_tools_javac14, kicked off by the pipeline in
   java_tools repository, in context of: [2]
3. add unit test against released java_tools_javac14 published in step 2,
   this change

Test Plan:

  $ bazel test //src/test/shell/bazel:bazel_coverage_java_jdk14toolchain_released_test

[1] bazelbuild#11514
[2] bazelbuild/java_tools#28
bazel-io pushed a commit that referenced this pull request Aug 1, 2020
Closes #11017.

This is the follow-up for PR: [1]. Because of the chicken and the egg
problem, it has to be done in three steps:

1. add support for java toolchange JDK 14, done in: [1]
2. publish new java_tools_javac14, kicked off by the pipeline in
   java_tools repository, in context of: [2]
3. add unit test against released java_tools_javac14 published in step 2,
   this change

Test Plan:

  $ bazel test //src/test/shell/bazel:bazel_coverage_java_jdk14toolchain_released_test

[1] #11514
[2] bazelbuild/java_tools#28

Closes #11828.

PiperOrigin-RevId: 324358602
ehkloaj pushed a commit to ehkloaj/bazel that referenced this pull request Aug 6, 2020
Closes bazelbuild#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 bazelbuild#11514.

PiperOrigin-RevId: 322759522
ehkloaj pushed a commit to ehkloaj/bazel that referenced this pull request Aug 6, 2020
Closes bazelbuild#11017.

This is the follow-up for PR: [1]. Because of the chicken and the egg
problem, it has to be done in three steps:

1. add support for java toolchange JDK 14, done in: [1]
2. publish new java_tools_javac14, kicked off by the pipeline in
   java_tools repository, in context of: [2]
3. add unit test against released java_tools_javac14 published in step 2,
   this change

Test Plan:

  $ bazel test //src/test/shell/bazel:bazel_coverage_java_jdk14toolchain_released_test

[1] bazelbuild#11514
[2] bazelbuild/java_tools#28

Closes bazelbuild#11828.

PiperOrigin-RevId: 324358602
bazel-io referenced this pull request Oct 7, 2020
Closes #11871

I modelled this on @davido's addition of a java 14 toolchain, here:[https://github.com/bazelbuild/bazel/pull/11514](url).

The test plan has the same form:

Create java_tools from this commit:
`  $ bazel build src:java_tools_java11.zip`

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_java11.zip>",
    ],
  )

```
Add Zulu OpenJDK15 to use as javabase in WORKSPACE file:
```
  http_archive(
    name = "openjdk15_linux_archive",
    build_file_content = """
java_runtime(name = 'runtime', srcs =  glob(['**']), visibility = ['//visibility:public'])
exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
""",
    strip_prefix = "zulu15.27.17-ca-jdk15.0.0-linux_x64",
    urls = ["https://cdn.azul.com/zulu/bin/zulu15.27.17-ca-jdk15.0.0-linux_x64.tar.gz"],
  )
```

Activate custom java toolchain and javabase in .bazelrc file:
```
  build --java_toolchain=@remote_java_tools_linux//:toolchain_jdk_15
  build --host_java_toolchain=@remote_java_tools_linux//:toolchain_jdk_15
  build --javabase=@openjdk15_linux_archive//:runtime
  build --host_javabase=@openjdk15_linux_archive//:runtime

```
Create Java 15 example class file:
```
public class Javac15Example {
  static String textBlock = """
              Hello,
              World
              """;
  public static void main(String[] args) {
    System.out.println(textBlock);
  }
}

```
Add Bazel file to build Java 15 syntax class:
```
  java_binary(
    name = "Javac15Example",
    srcs = ["Javac15Example.java"],
    main_class = "Javac15Example",
  )

```
Test that it works as expected:
```
  $ bazel run java:Javac15Example
INFO: Analyzed target //java:Javac15Example (1 packages loaded, 2 targets configured).
INFO: Found 1 target...
INFO: From Building java/Javac15Example.jar (1 source file):
Target //java:Javac15Example up-to-date:
  bazel-bin/java/Javac15Example.jar
  bazel-bin/java/Javac15Example
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
Hello,
World
```

Closes #12168.

PiperOrigin-RevId: 335931179
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Java 14 toolchain support
7 participants