Latest release:
0.9.2
Important Currently in beta. Feedback welcome but will probably break your build.
Use GraalVM from Bazel, with support for:
- Building native image binaries
- Installing components with
gu
- Using GraalVM as a Bazel Java toolchain
- Support for Bazel 6, Bazel 7, and Bzlmod
- Support for Bazel 5 and Bazel 4, drop-in replacement for
rules_graal
- Run tools from GraalVM directly
- Example projects for each Bazel version
- Support for macOS, Linux, Windows (including Native Image!)
- Support for the latest modern GraalVM releases (Community Edition and Oracle GraalVM)
API docs for
graalvm_repository
Via WORKSPACE.bazel
:
http_archive(
name = "rules_graalvm",
sha256 = "baf82fa979fe4c1c90fe676dfe80aea5a70bb6a8287c3d6d1d4418c0f3a3b2b2",
strip_prefix = "rules_graalvm-0.9.2",
urls = [
"https://github.com/sgammon/rules_graalvm/releases/download/v0.9.2/rules_graalvm-0.9.2.zip",
],
)
load("@rules_graalvm//graalvm:repositories.bzl", "graalvm_repository")
graalvm_repository(
name = "graalvm",
components = [
# if you need components like `js` or `wasm`, add them here
],
distribution = "ce", # `oracle`, `ce`, or `community`
java_version = "20", # `17`, `20`, or `21`, as supported by the version provided
version = "20.0.2", # earlier version format like `22.x` also supported
)
load("@rules_graalvm//graalvm:workspace.bzl", "register_graalvm_toolchains", "rules_graalvm_repositories")
rules_graalvm_repositories()
register_graalvm_toolchains()
Or, via MODULE.bazel
:
Important
To use Bzlmod with rules_graalvm
, you will need the archive_override
below (until we go live on BCR).
bazel_dep(name = "rules_graalvm", version = "0.9.2")
# Until we ship to BCR:
archive_override(
module_name = "rules_graalvm",
urls = ["https://github.com/sgammon/rules_graalvm/releases/download/v0.9.2/rules_graalvm-0.9.2.zip"],
strip_prefix = "rules_graalvm-0.9.2",
integrity = "sha256-uvgvqXn+TByQ/mdt/oCupacLtqgofD1tHUQYwPOjsrI=",
)
gvm = use_extension("@rules_graalvm//:extensions.bzl", "graalvm")
gvm.graalvm(
name = "graalvm",
version = "20.0.2", # earlier version format like `22.x` also supported
distribution = "oracle", # `oracle`, `ce`, or `community`
java_version = "20", # `17`, `20`, or `21`, as supported by the version provided
components = [
# if you need components like `js` or `wasm`, add them here
],
)
use_repo(gvm, "graalvm")
register_toolchains("@graalvm//:all")
You can use the graalvm_repository
as a Java toolchain, by registering it like below:
Via WORKSPACE.bazel
:
load("@rules_graalvm//graalvm:workspace.bzl", "register_graalvm_toolchains", "rules_graalvm_repositories")
rules_graalvm_repositories()
register_graalvm_toolchains()
Via Bzlmod:
register_toolchains("@graalvm//:all")
To use the toolchain, add this to your .bazelrc
:
build --extra_toolchains=@graalvm//:toolchain
build --java_runtime_version=graalvm_20
Note If you name your repository
example
and set the Java version to21
, yourjava_runtime_version
would beexample_21
.
API docs for
native_image
In a BUILD.bazel
file:
load("@rules_java//java:defs.bzl", "java_library")
load("@rules_graalvm//graalvm:defs.bzl", "native_image")
java_library(
name = "main",
srcs = glob(["Main.java"]),
)
native_image(
name = "main-native",
deps = [":main"],
main_class = "Main",
native_image_tool = "@graalvm//:native-image",
)
It is supported to specify the native-image
tool as above, using the native_image_tool
attribute
on your target. In fact, you must do this unless you register the GraalVM toolchains as shown in
the installation instructions.
When using toolchains, the native_image_tool
attribute can be omitted, which delegates to Bazel's
toolchain system to resolve the tool:
Resolve via toolchains:
native_image(
name = "main-native",
deps = [":main"],
main_class = "Main",
)
Or point to a specific native-image
tool:
native_image(
name = "main-native",
deps = [":main"],
main_class = "Main",
native_image_tool = "@graalvm//:native-image",
)
API docs for legacy
native_image
rule
In a BUILD.bazel
file:
load("@rules_java//java:defs.bzl", "java_library")
load("@rules_graalvm//graal:graal.bzl", "native_image")
java_library(
name = "main",
srcs = glob(["Main.java"]),
)
native_image(
name = "main-native",
deps = [":main"],
main_class = "Main",
)
Note: In the legacy rules, you don't have to specify native_image_tool
, but on the other hand,
the default target @graalvm//:native-image
is hard-coded in. If you use a different repository name
make sure to add the native_image_tool
attribute to point to @yourrepo//:native-image
.
See the list of examples, which are used as continuous integration tests. Examples are available for Bazel 4-7.
These rules attempt to strike as optimal a balance as possible between older Bazel support (starting at Bazel 4) and the maximum possible strictness/hermeticity for action execution.
Bazel Toolchains are used to resolve the C++ compiler which is provided to native-image
.
Toolchains are additionally used within the rules to provide and resolve tools from GraalVM itself.
For information about strictness tuning on each operating system, see the hermeticity guide.
The GraalVM-specific toolchain type is available at:
@rules_graalvm//graalvm/toolchain:toolchain_type
If you install GraalVM at a repository named @graalvm
, the toolchain targets are:
Java toolchain:
@graalvm//:toolchain
GraalVM toolchain:
@graalvm//:gvm
The default WORKSPACE
and Bzlmod installation instructions register both types of toolchains.
The GraalVM toolchain is required to perform builds with native-image
(or you must provide a native_image_tool
target).
Built on top of @andyscott's fantastic work with rules_graal.