Note
Bazel toolchains can be hard to use. See the Troubleshooting section for best-effort help.
Bazel Toolchains is an optional feature which helps Bazel resolve tools on different platforms and in different project contexts. Several Bazel rule sets use toolchains in order to make the underlying tooling customizable for an end-user's goals.
Java toolchains in Bazel are typically used to select a particular JDK to run against or build against. rules_graalvm
integrates with Bazel toolchains by default; if you want to use a downloaded GVM installation as your Java toolchain, follow the directions below.
Important
Make sure you've installed and setup the rules first.
After you've installed rules_graalvm
and declared a graalvm_repository
, you can register GraalVM as a Java toolchain using the :all
target at the repository root:
In your WORKSPACE.bazel
:
# ...
# graalvm_repository(name = "graalvm", ...)
register_toolchains("@graalvm//:toolchain")
Or, in a bazel.rc
:
build --extra_toolchains=@graalvm//:toolchain
Important
You need to register and select a toolchain in order to actually use it. This sample assumes the graalvm_repository
is named graalvm
.
Bazel uses several JDKs throughout the course of a regular build; there is the embedded JDK which runs Bazel itself, then the tooling JDK which is used to build your code, and, finally, there is the runtime JDK which is used to execute JVM bytecode. This is referred to as Bazel's "JVM sandwich."
You can select GraalVM as the tooling toolchain, the runtime toolchain, or both:
Set tool_java_language_version
to the version matching your GraalVM JDK declared in graalvm_repository
.
In a bazel.rc
:
build --tool_java_language_version=20
build --extra_toolchains=@graalvm//:bootstrap_runtime_toolchain
Set java_runtime_version
to the version matching your GraalVM JDK declared in graalvm_repository
.
In a bazel.rc
:
build --java_language_version=20
build --java_runtime_version=graalvm_20