Skip to content

Commit

Permalink
Make jvm_import use java from resolved toolchain instead of host ma…
Browse files Browse the repository at this point in the history
…chine.

Fixes bazel-contrib#895
  • Loading branch information
jin committed Feb 22, 2024
1 parent 8f4b5f2 commit 81f613b
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions private/rules/jvm_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,23 @@ def _jvm_import_impl(ctx):
workspace_name = visible_name,
)

java_runtime = ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"].java.java_runtime
add_jar_manifest_entry_jar = ctx.file._add_jar_manifest_entry

injar = ctx.files.jars[0]
if ctx.attr._stamp_manifest[StampManifestProvider].stamp_enabled:
outjar = ctx.actions.declare_file("processed_" + injar.basename, sibling = injar)
args = ctx.actions.args()
args.add("-jar")
args.add(add_jar_manifest_entry_jar)
args.add_all(["--source", injar, "--output", outjar])
args.add_all(["--manifest-entry", "Target-Label:{target_label}".format(target_label = label)])
ctx.actions.run(
executable = ctx.executable._add_jar_manifest_entry,
executable = java_runtime.java_executable_exec_path,
arguments = [args],
inputs = [injar],
inputs = [injar, add_jar_manifest_entry_jar],
outputs = [outjar],
tools = java_runtime.files,
mnemonic = "StampJarManifest",
progress_message = "Stamping the manifest of %s" % ctx.label,
)
Expand All @@ -42,6 +48,8 @@ def _jvm_import_impl(ctx):

compilejar = ctx.actions.declare_file("header_" + injar.basename, sibling = injar)
args = ctx.actions.args()
args.add("-jar")
args.add(add_jar_manifest_entry_jar)
args.add_all(["--source", outjar, "--output", compilejar])

# We need to remove the `Class-Path` entry since bazel 4.0.0 forces `javac`
Expand All @@ -56,10 +64,11 @@ def _jvm_import_impl(ctx):
args.add("--make-safe")

ctx.actions.run(
executable = ctx.executable._add_jar_manifest_entry,
executable = java_runtime.java_executable_exec_path,
arguments = [args],
inputs = [outjar],
inputs = [outjar, add_jar_manifest_entry_jar],
outputs = [compilejar],
tools = java_runtime.files,
mnemonic = "CreateCompileJar",
progress_message = "Creating compile jar for %s" % ctx.label,
)
Expand Down Expand Up @@ -101,14 +110,15 @@ jvm_import = rule(
default = False,
),
"_add_jar_manifest_entry": attr.label(
executable = True,
allow_single_file = True,
cfg = "exec",
default = "//private/tools/java/com/github/bazelbuild/rules_jvm_external/jar:AddJarManifestEntry",
default = "//private/tools/java/com/github/bazelbuild/rules_jvm_external/jar:AddJarManifestEntry_deploy.jar",
),
"_stamp_manifest": attr.label(
default = "@rules_jvm_external//settings:stamp_manifest",
),
},
implementation = _jvm_import_impl,
provides = [JavaInfo],
toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
)

0 comments on commit 81f613b

Please sign in to comment.