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

Bazel: Support building with Java 9 #4257

Merged
merged 1 commit into from
Feb 1, 2018

Conversation

davido
Copy link
Contributor

@davido davido commented Jan 31, 2018

Fixes: #4256.

Bazel@HEAD supports Java 9.

The current code has one single issue with Java 9 compliance: the usage
of sun.misc package. We add sun.misc module with --add-modules compiler
option for now. Long term, the usage of non poblic API should be
avoided.

To build (or test) with Java 9, build custom bazel version and issue:

$ bazel --host_javabase=/usr/lib64/jvm/java-9-openjdk build
--javacopt='--release 9'
--java_toolchain=@bazel_tools//tools/jdk:toolchain_jdk9
:protobuf_java

@grpc-kokoro
Copy link

Thanks for your pull request. The automated tests will run as soon as one of the admins verifies this change is ok for us to run on our infrastructure.

1 similar comment
@grpc-kokoro
Copy link

Thanks for your pull request. The automated tests will run as soon as one of the admins verifies this change is ok for us to run on our infrastructure.

@davido
Copy link
Contributor Author

davido commented Jan 31, 2018

@cushon

Unfortunately, it still doesn't work:

$ bazel --host_javabase=/usr/lib64/jvm/java-9-openjdk build --java_toolchain=@bazel_tools//tools/jdk:toolchain_jdk9 :protobuf_java
INFO: Analysed target //:protobuf_java (0 packages loaded).
INFO: Found 1 target...
ERROR: /home/davido/projects/davido_protobuf/BUILD:615:1: Building libprotobuf_java.jar (78 source files, 1 source jar) failed (Exit 1)
java/core/src/main/java/com/google/protobuf/UnsafeUtil.java:44: error: package sun.misc does not exist
  private static final sun.misc.Unsafe UNSAFE = getUnsafe();
                               ^
java/core/src/main/java/com/google/protobuf/UnsafeUtil.java:290: error: package sun.misc does not exist
  private static sun.misc.Unsafe getUnsafe() {
                         ^
java/core/src/main/java/com/google/protobuf/UnsafeUtil.java:427: error: package sun.misc does not exist
    sun.misc.Unsafe unsafe;
            ^
java/core/src/main/java/com/google/protobuf/UnsafeUtil.java:429: error: package sun.misc does not exist
    MemoryAccessor(sun.misc.Unsafe unsafe) {
                           ^
java/core/src/main/java/com/google/protobuf/UnsafeUtil.java:506: error: package sun.misc does not exist
    JvmMemoryAccessor(sun.misc.Unsafe unsafe) {
                              ^
java/core/src/main/java/com/google/protobuf/UnsafeUtil.java:291: error: package sun.misc does not exist
    sun.misc.Unsafe unsafe = null;
            ^
java/core/src/main/java/com/google/protobuf/UnsafeUtil.java:295: error: package sun.misc does not exist
              new PrivilegedExceptionAction<sun.misc.Unsafe>() {
                                                    ^
java/core/src/main/java/com/google/protobuf/UnsafeUtil.java:297: error: package sun.misc does not exist
                public sun.misc.Unsafe run() throws Exception {
                               ^
java/core/src/main/java/com/google/protobuf/UnsafeUtil.java:298: error: package sun.misc does not exist
                  Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
                                ^
java/core/src/main/java/com/google/protobuf/UnsafeUtil.java:298: error: package sun.misc does not exist
                  Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
                                                     ^
Target //:protobuf_java failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 3.871s, Critical Path: 3.68s
FAILED: Build did NOT complete successfully

Any clue, why passing: --add-modules=jdk.unsupported option has not fixed it?

@davido
Copy link
Contributor Author

davido commented Jan 31, 2018

I wonder, if sun.misc.Unsafe can be replaced with https://github.com/jnr/jffi/blob/master/src/main/java/com/kenai/jffi/MemoryIO.java, to not mess around with internal/unsupported JDK API altogether. Another resource: https://blog.dripstat.com/removal-of-sun-misc-unsafe-a-disaster-in-the-making .

@davido
Copy link
Contributor Author

davido commented Jan 31, 2018

Unfortunately, it still doesn't work: [...]

Never mind, I missed that --release 9 javacopt is now obligatory on Bazel@HEAD:

  $ bazel --host_javabase=/usr/lib64/jvm/java-9-openjdk build --javacopt='--release 9' --java_toolchain=@bazel_tools//tools/jdk:toolchain_jdk9 :protobuf_java
.
INFO: Analysed target //:protobuf_java (10 packages loaded).
INFO: Found 1 target...
Target //:protobuf_java up-to-date:
  bazel-bin/libprotobuf_java.jar
INFO: Elapsed time: 17.899s, Critical Path: 12.86s
INFO: Build completed successfully, 3 total actions

I've updated the commit message correspondingly.

BUILD Outdated
@@ -608,7 +619,10 @@ java_library(
]) + [
":gen_well_known_protos_java",
],
javacopts = ["-source 7", "-target 7"],
javacopts = select({
"//:jdk9": ["--add-modules=jdk.unsupported", "-target 9"],
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add the jdk.unsupported flag but keep "-source 7" and "-target 7"? We don't want to introduce Java 8 or Java 9 only features regardless which javac is used.

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.

I removed explicit "-target 9" version, but --add-modules only works on java 9 release. That why to work on java 9, the Bazel mus be invoked like this:

  $ bazel --host_javabase=/usr/lib64/jvm/java-9-openjdk build --javacopt='--release 9' --java_toolchain=@bazel_tools//tools/jdk:toolchain_jdk9 :protobuf_java

I added the command to the commit message.

But the good news is: this patch is backwards compatible. It still works, on Java 7,8,9 and uses "-source 7", "-target 7" option, if the user doesn't switch to java 9 toolcain.

@xfxyjwf
Copy link
Contributor

xfxyjwf commented Jan 31, 2018

Thanks! I'll merge after the tests pass.

@davido
Copy link
Contributor Author

davido commented Jan 31, 2018

Thanks! I'll merge after the tests pass.

Thanks. Would you mind to create a new release in the next days?

Fixes: protocolbuffers#4256.

Bazel@HEAD supports Java 9.

The current code has one single issue with Java 9 compliance: the usage
of sun.misc package. We add jdk.unsupported module with --add-modules
compiler option for now. Long term, the usage of non public API should
be avoided.

To build with Java 9, build custom bazel version and issue:

  $ bazel --host_javabase=/usr/lib64/jvm/java-9-openjdk build \
    --javacopt='--release 9' \
    --java_toolchain=@bazel_tools//tools/jdk:toolchain_jdk9 \
   :protobuf_java
@davido
Copy link
Contributor Author

davido commented Jan 31, 2018

I only improved the commit message a bit in the latest push.

@xfxyjwf xfxyjwf merged commit 5dad7cc into protocolbuffers:master Feb 1, 2018
@davido davido deleted the support_java9 branch February 1, 2018 23:23
@xfxyjwf xfxyjwf self-assigned this Jun 22, 2018
@xfxyjwf xfxyjwf added the bazel label Jun 22, 2018
zhoutwo pushed a commit to zhoutwo/protobuf that referenced this pull request Apr 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants