Skip to content

Commit

Permalink
Make Java runtime dependency of libjvm optional if supported
Browse files Browse the repository at this point in the history
If the current version of Bazel supports optional toolchains, the Java
runtime dependency of `libjvm` is optional. If no Java runtime is
registered for the current target platform, the resulting binaries will
fall back to looking for a Java runtime on the host.

With this commit, the minimum supported version of Bazel is raised to
5.0.0.
  • Loading branch information
fmeum committed Aug 7, 2023
1 parent ed026cb commit 5c1d153
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/run-tests-externally.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ jobs:
strategy:
fail-fast: false
matrix:
bazel: [4.0.0, latest, last_green]
bazel: [5.0.0, latest, last_green]
bazel_mode: [workspace, module]
os: [ubuntu-latest, macos-latest, windows-2019]
jdk: [8, 11, 17]
exclude:
- bazel: 4.0.0
- bazel: 5.0.0
jdk: 11
- bazel: 4.0.0
- bazel: 5.0.0
jdk: 17
- bazel: 4.0.0
- bazel: 5.0.0
bazel_mode: module
- bazel: last_green
jdk: 8
Expand Down
29 changes: 23 additions & 6 deletions jni/tools/libjvm_stub/current_java_runtime.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,18 @@ CURRENT_JAVA_RUNTIME_HEADER_TEMPLATE = """#ifndef RULES_JNI_LIBJVM_STUB_CURRENT_
"""

def _current_java_runtime_impl(ctx):
java_runtime_info = ctx.attr._current_java_runtime[java_common.JavaRuntimeInfo]
java_rlocation_path = rlocation_path(ctx, java_runtime_info.java_executable_runfiles_path)
java_runtime_toolchain = ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"]
if java_runtime_toolchain == None:
# Fall back to using the host Java runtime at runtime.
java_rlocation_path = ""
java_runtime_files = depset([])
else:
java_runtime = java_runtime_toolchain.java_runtime
java_rlocation_path = rlocation_path(
ctx,
java_runtime.java_executable_runfiles_path,
)
java_runtime_files = java_runtime.files

header = ctx.actions.declare_file(ctx.attr.name + ".h")
ctx.actions.write(header, CURRENT_JAVA_RUNTIME_HEADER_TEMPLATE.format(java_rlocation_path))
Expand All @@ -48,27 +58,34 @@ def _current_java_runtime_impl(ctx):

return [
DefaultInfo(
runfiles = ctx.attr._current_java_runtime[DefaultInfo].default_runfiles,
runfiles = ctx.runfiles(transitive_files = java_runtime_files),
),
CcInfo(
compilation_context = compilation_context,
),
]

def _optional_toolchain_if_available(toolchain_type):
if hasattr(config_common, "toolchain_type"):
return config_common.toolchain_type(
toolchain_type,
mandatory = False,
)
else:
return toolchain_type

current_java_runtime = rule(
implementation = _current_java_runtime_impl,
attrs = {
"_cc_toolchain": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
),
"_current_java_runtime": attr.label(
default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
),
},
fragments = ["cpp"],
incompatible_use_toolchain_transition = True,
provides = [CcInfo],
toolchains = [
"@bazel_tools//tools/cpp:toolchain_type",
_optional_toolchain_if_available("@bazel_tools//tools/jdk:runtime_toolchain_type"),
],
)

0 comments on commit 5c1d153

Please sign in to comment.