Skip to content

Commit

Permalink
Bazel: Switch to using toolchain resolution for java rules
Browse files Browse the repository at this point in the history
Bump Bazel version to release 5.0.0. In this new Bazel release,
--incompatible_use_toolchain_resolution_for_java_rules is flipped, that
means that the build must be adapted to toolchain resolution.

Specification toolchain resolution for java rules is here: [1]. Main
tracking Bazel issue is here: [2].

Given that new Bazel release also added support for remote JDK 17, add
support for building with remote JDK 17 to produce major byte code
version 61. Note that some tests are still failing on JDK 17. This will
be fixed in follow-up change.

This change has a number of side effects:

o Remove workaround for Bazel worker multiplexer bug, see: [3].
o Demote severity for EP bug patterns from error to warning:
- DoNotMockAutoValue
o Re-enable EP bug pattern:
- FutureReturnValueIgnored

Test Plan:

To build with JDK 11 run:

  $ bazel build release

To build with JDK 17 run:

  $ bazel build --config java17 release

To build with JDK 11 on RBE run:

  $ bazel build --config=remote --remote_instance_name=<name> release

To build with JDK 17 on RBE run:

  $ bazel build --config=remote17 --remote_instance_name=<name> release

[1] https://docs.google.com/document/d/1MVbBxbKVKRJJY7DnkptHpvz7ROhyAYy4a-TZ-n7Q0r4/edit?usp=sharing
[2] bazelbuild/bazel#7849
[3] bazelbuild/bazel#13333

Change-Id: Ib3b923db7bb97a24d52ae8468ad1532a970203b5
  • Loading branch information
davido committed Jan 25, 2022
1 parent 40a0e68 commit 21a891c
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 55 deletions.
26 changes: 24 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,30 @@ build --workspace_status_command="python3 ./tools/workspace_status.py"
build --repository_cache=~/.gerritcodereview/bazel-cache/repository
build --action_env=PATH
build --disk_cache=~/.gerritcodereview/bazel-cache/cas
build --java_toolchain=//tools:error_prone_warnings_toolchain_java11

# Builds using remotejdk_11, executes using remotejdk_11 or local_jdk
build --java_language_version=11
build --java_runtime_version=remotejdk_11
build --tool_java_language_version=11
build --tool_java_runtime_version=remotejdk_11

# Builds using remotejdk_17, executes using remotejdk_17 or local_jdk
build:java17 --java_language_version=17
build:java17 --java_runtime_version=remotejdk_17
build:java17 --tool_java_language_version=17
build:java17 --tool_java_runtime_version=remotejdk_17

# Builds and executes on RBE using remotejdk_11
build:remote --java_language_version=11
build:remote --java_runtime_version=remotejdk_11
build:remote --tool_java_language_version=11
build:remote --tool_java_runtime_version=remotejdk_11

# Builds and executes on RBE using remotejdk_17
build:remote17 --java_language_version=17
build:remote17 --java_runtime_version=remotejdk_17
build:remote17 --tool_java_language_version=17
build:remote17 --tool_java_runtime_version=remotejdk_17

# Enable strict_action_env flag to. For more information on this feature see
# https://groups.google.com/forum/#!topic/bazel-discuss/_VmRfMyyHBk.
Expand All @@ -15,6 +38,5 @@ build --announce_rc

test --build_tests_only
test --test_output=all
test --java_toolchain=//tools:error_prone_warnings_toolchain_java11

import %workspace%/tools/remote-bazelrc
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.2.0
5.0.0
11 changes: 2 additions & 9 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@ load("//tools/bzl:pkg_war.bzl", "pkg_war")
package(default_visibility = ["//visibility:public"])

config_setting(
name = "java11",
name = "java17",
values = {
"java_toolchain": "@bazel_tools//tools/jdk:toolchain_java11",
},
)

config_setting(
name = "java_next",
values = {
"java_toolchain": "//tools:toolchain_vanilla",
"java_language_version": "17",
},
)

Expand Down
46 changes: 7 additions & 39 deletions Documentation/dev-bazel.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ should generate a .war file under `bazel-bin/gerrit.war`.
To build Gerrit from source, you need:

* A Linux or macOS system (Windows is not supported at this time)
* A JDK for Java 11|...
* A JDK for Java 11 or Java 17
* Python 3
* link:https://github.com/nodesource/distributions/blob/master/README.md[Node.js (including npm),role=external,window=_blank]
* Bower (`npm install -g bower`)
Expand Down Expand Up @@ -58,53 +58,21 @@ level, run:
$ bazel build :release
```

[[java-13]]
==== Java 13 support
[[java-17]]
==== Java 17 support

Java 13 (and newer) is supported through vanilla java toolchain
link:https://docs.bazel.build/versions/master/toolchains.html[Bazel option,role=external,window=_blank].
To build Gerrit with Java 13 and newer, specify vanilla java toolchain and
provide the path to JDK home:
Java 17 is supported. To build Gerrit with Java 17, run:

```
$ bazel build \
--define=ABSOLUTE_JAVABASE=<path-to-java-13> \
--javabase=@bazel_tools//tools/jdk:absolute_javabase \
--host_javabase=@bazel_tools//tools/jdk:absolute_javabase \
--host_java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla \
--java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla \
:release
$ bazel build --config=java17 :release
```

To run the tests, `--javabase` option must be passed as well, because
bazel test runs the test using the target javabase:
To run the tests with Java 17, run:

```
$ bazel test \
--define=ABSOLUTE_JAVABASE=<path-to-java-13> \
--javabase=@bazel_tools//tools/jdk:absolute_javabase \
--host_javabase=@bazel_tools//tools/jdk:absolute_javabase \
--host_java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla \
--java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla \
//...
$ bazel test --config=java17 //...
```

To avoid passing all those options on every Bazel build invocation,
they could be added to ~/.bazelrc resource file:

```
$ cat << EOF > ~/.bazelrc
build --define=ABSOLUTE_JAVABASE=<path-to-java-13>
build --javabase=@bazel_tools//tools/jdk:absolute_javabase
build --host_javabase=@bazel_tools//tools/jdk:absolute_javabase
build --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla
build --java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla
EOF
```

Now, invoking Bazel with just `bazel build :release` would include
all those options.

=== Node.js and npm packages
See link:https://gerrit.googlesource.com/gerrit/+/master/polygerrit-ui/README.md#installing-node_js-and-npm-packages[Installing Node.js and npm packages,role=external,window=_blank].

Expand Down
4 changes: 4 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ go_repository(
importpath = "github.com/howeyc/fsnotify",
)

register_toolchains("//tools:error_prone_warnings_toolchain_java11_definition")

register_toolchains("//tools:error_prone_warnings_toolchain_java17_definition")

# JGit external repository consumed from git submodule
local_repository(
name = "jgit",
Expand Down
16 changes: 14 additions & 2 deletions tools/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ default_java_toolchain(
visibility = ["//visibility:public"],
)

default_java_toolchain(
name = "error_prone_warnings_toolchain_java17",
configuration = dict(),
java_runtime = "@bazel_tools//tools/jdk:remotejdk_17",
package_configuration = [
":error_prone",
],
source_version = "17",
target_version = "17",
visibility = ["//visibility:public"],
)

# Error Prone errors enabled by default; see ../.bazelrc for how this is
# enabled. This warnings list is originally based on:
# https://github.com/bazelbuild/BUILD_file_generator/blob/master/tools/bazel_defs/java.bzl
Expand Down Expand Up @@ -110,7 +122,7 @@ java_package_configuration(
"-Xep:DoNotCallSuggester:ERROR",
"-Xep:DoNotClaimAnnotations:ERROR",
"-Xep:DoNotMock:ERROR",
"-Xep:DoNotMockAutoValue:ERROR",
"-Xep:DoNotMockAutoValue:WARN",
"-Xep:DoubleBraceInitialization:ERROR",
"-Xep:DoubleCheckedLocking:ERROR",
"-Xep:DuplicateMapKeys:ERROR",
Expand Down Expand Up @@ -153,7 +165,7 @@ java_package_configuration(
"-Xep:FromTemporalAccessor:ERROR",
"-Xep:FunctionalInterfaceClash:ERROR",
"-Xep:FunctionalInterfaceMethodChanged:ERROR",
# "-Xep:FutureReturnValueIgnored:ERROR", // this check has a bug.
"-Xep:FutureReturnValueIgnored:ERROR",
"-Xep:FuturesGetCheckedIllegalExceptionType:ERROR",
"-Xep:GetClassOnAnnotation:ERROR",
"-Xep:GetClassOnClass:ERROR",
Expand Down
9 changes: 9 additions & 0 deletions tools/bzl/junit.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ POST_JDK8_OPTS = [
"-Djava.locale.providers=COMPAT,CLDR,SPI",
]

POST_JDK17_OPTS = [
# https://github.com/bazelbuild/bazel/issues/14502
"-Djava.security.manager=allow",
]

def junit_tests(name, srcs, **kwargs):
s_name = name.replace("-", "_") + "TestSuite"
_gen_suite(
Expand All @@ -81,6 +86,10 @@ def junit_tests(name, srcs, **kwargs):
outname = s_name,
)
jvm_flags = kwargs.get("jvm_flags", []) + POST_JDK8_OPTS
jvm_flags = jvm_flags + select({
"//:java17": POST_JDK8_OPTS + POST_JDK17_OPTS,
"//conditions:default": POST_JDK8_OPTS,
})
java_test(
name = name,
test_class = s_name,
Expand Down
2 changes: 0 additions & 2 deletions tools/remote-bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ build:remote --disk_cache=

# Set several flags related to specifying the platform, toolchain and java
# properties.
build:remote --host_javabase=@rbe_jdk11//java:jdk
build:remote --javabase=@rbe_jdk11//java:jdk
build:remote --crosstool_top=@rbe_jdk11//cc:toolchain
build:remote --extra_toolchains=@rbe_jdk11//config:cc-toolchain
build:remote --extra_execution_platforms=@rbe_jdk11//config:platform
Expand Down

0 comments on commit 21a891c

Please sign in to comment.