Skip to content

Commit 2843fe1

Browse files
committed
Bazel examples: hermetic JDK for build (not test)
Works in target- and host-config. Still need to figure out how to use it for testing.
1 parent d46430e commit 2843fe1

File tree

7 files changed

+154
-0
lines changed

7 files changed

+154
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.0.0

bazel/2022-12-23--hermetic-java/BUILD

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Build java 19:
2+
# bazel build //:app --java_language_version=19
3+
#
4+
# Run with java 19:
5+
# JAVA_HOME=$PWD/zulu19.30.11-ca-jdk19.0.1-linux_x64 bazel run //:app --java_language_version=19
6+
#
7+
# Run with Bazel-supported java 17:
8+
# bazel run //:app --java_language_version=17 --java_runtime_version=remotejdk_17
9+
#
10+
# Supported values of --java_runtime_version are localjdk, localjdk_[version], remotejdk_11, and
11+
# remote_jdk17. (https://bazel.build/docs/user-manual#java-runtime-version)
12+
#
13+
# Build as tool with Bazel-supported java 17:
14+
# bazel build //:x --java_language_version=17 --java_runtime_version=remotejdk_17 --tool_java_language_version=17 --tool_java_runtime_version=remotejdk_17
15+
#
16+
# Build as tool with java 19:
17+
# bazel build //:x --java_language_version=19 --java_runtime_version=19 --tool_java_language_version=19 --tool_java_runtime_version=19
18+
19+
genrule(
20+
name = "x",
21+
outs = ["x.out"],
22+
cmd = "$(location :app) x > $@",
23+
tools = [":app"],
24+
)
25+
26+
java_library(
27+
name = "app_lib",
28+
srcs = ["Main.java"],
29+
)
30+
31+
java_binary(
32+
name = "app",
33+
main_class = "Main",
34+
runtime_deps = [":app_lib"],
35+
)
36+
37+
load(
38+
"@bazel_tools//tools/jdk:default_java_toolchain.bzl",
39+
"BASE_JDK9_JVM_OPTS",
40+
"DEFAULT_JAVACOPTS",
41+
"DEFAULT_TOOLCHAIN_CONFIGURATION",
42+
"default_java_toolchain",
43+
)
44+
45+
default_java_toolchain(
46+
name = "my_jdk",
47+
configuration = DEFAULT_TOOLCHAIN_CONFIGURATION,
48+
java_runtime = ":my_jdk_runtime",
49+
javacopts = DEFAULT_JAVACOPTS,
50+
jvm_opts = BASE_JDK9_JVM_OPTS,
51+
source_version = "19",
52+
target_version = "19",
53+
)
54+
55+
java_runtime(
56+
name = "my_jdk_runtime",
57+
srcs = ["@my_jdk//:files"],
58+
java = "@my_jdk//:bin/java",
59+
)
60+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Main {
2+
// Sealed classes require Java 17
3+
public static abstract sealed class Foo permits Bar {
4+
}
5+
6+
private static final class Bar extends Foo {
7+
}
8+
9+
public static void main(String[] args) {
10+
// Switch expression requires Java 14 (or 12 / 13 with preview)
11+
var x = switch (args.length) {
12+
case 0 -> "no args";
13+
case 1 -> "one arg";
14+
default -> "many args";
15+
};
16+
System.out.println(x);
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Bazel demo: hermetic JDK from repo
2+
3+
## What
4+
5+
Demonstrate using JDK19 in target- and host-configuration. The JDK comes from an archive on disk, in
6+
the source tree (not checked in), and it's newer than any JDK that Bazel supports by default.
7+
8+
## Why
9+
10+
Wanted to build Java 13 code using a custom JDK 19 (instead of one Bazel downloads for me), and it
11+
was difficult to figure out how to set up the toolchains, so here's the result of my efforts.
12+
13+
## How
14+
15+
1. Download a JDK 19 archive (e.g. the [Zulu JDK](https://www.azul.com/downloads/?package=jdk#download-openjdk))
16+
1. Put the file in the workspace root
17+
1. Update the `WORKSPACE` file: `my_jdk.src` is the archive's name, `my_jdk.strip_prefix` is the
18+
directory name inside the archive (to strip)
19+
1. Build (see `BUILD` for more examples):
20+
21+
```sh
22+
bazel build //:x --java_language_version=19 --java_runtime_version=19 --tool_java_language_version=19 --tool_java_runtime_version=19
23+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("//:archive.bzl", "local_repository_archive")
2+
3+
local_repository_archive(
4+
name = "my_jdk",
5+
src = "//:zulu19.30.11-ca-jdk19.0.1-linux_x64.tar.gz",
6+
strip_prefix = "zulu19.30.11-ca-jdk19.0.1-linux_x64",
7+
)
8+
9+
register_toolchains("//:my_jdk_definition", "//my_tool_jdk:toolchain")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def _impl(repository_ctx):
2+
repository_ctx.extract(
3+
repository_ctx.attr.src,
4+
stripPrefix = repository_ctx.attr.strip_prefix,
5+
)
6+
7+
repository_ctx.file(
8+
"BUILD",
9+
content = """# Generated by local_repository_archive rule
10+
package(default_visibility = ["//visibility:public"])
11+
12+
_FILES = glob(["**"])
13+
14+
filegroup(
15+
name = "files",
16+
srcs = _FILES,
17+
)
18+
19+
exports_files(_FILES)
20+
""",
21+
executable = False,
22+
)
23+
24+
local_repository_archive = repository_rule(
25+
implementation = _impl,
26+
attrs = {
27+
"src": attr.label(mandatory = True, allow_single_file = True),
28+
"strip_prefix": attr.string(),
29+
},
30+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
load("@bazel_tools//tools/jdk:local_java_repository.bzl", "local_java_runtime")
2+
3+
local_java_runtime(
4+
name = "my_jdk_runtime",
5+
java_home = None,
6+
runtime_name = "//:my_jdk_runtime",
7+
version = "19",
8+
)
9+
10+
alias(
11+
name = "toolchain",
12+
actual = "runtime_toolchain_definition",
13+
)

0 commit comments

Comments
 (0)