Skip to content

Commit

Permalink
Remove runtime dependency of non-executable java_binary
Browse files Browse the repository at this point in the history
The `java_binary` Starlark implementation now only directly depends on a Java runtime toolchain when it is used as an executable rule.

This work will eventually allow `java_binary` with `create_executable = False` to be compiled for platforms that do not provide a regular JDK, such as Android.

Work towards #17085
Work towards bazelbuild/rules_java#64
Split off from #18262

Closes #18620.

PiperOrigin-RevId: 540884239
Change-Id: I3c06c02d1a9dc1be2775f409c393a386ac2260b7
  • Loading branch information
fmeum authored and copybara-github committed Jun 16, 2023
1 parent 0a5f437 commit 8c3a4eb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ def _bazel_base_binary_impl(ctx, is_test_rule_class):

runfiles = default_info.runfiles

if executable:
runtime_toolchain = semantics.find_java_runtime_toolchain(ctx)
runfiles = runfiles.merge(ctx.runfiles(transitive_files = runtime_toolchain.files))

test_support = helper.get_test_support(ctx)
if test_support:
runfiles = runfiles.merge(test_support[DefaultInfo].default_runfiles)
Expand Down Expand Up @@ -271,7 +275,9 @@ def _make_binary_rule(implementation, attrs, executable = False, test = False):
test = test,
fragments = ["cpp", "java"],
provides = [JavaInfo],
toolchains = [semantics.JAVA_TOOLCHAIN, semantics.JAVA_RUNTIME_TOOLCHAIN] + cc_helper.use_cpp_toolchain(),
toolchains = [semantics.JAVA_TOOLCHAIN] + cc_helper.use_cpp_toolchain() + (
[semantics.JAVA_RUNTIME_TOOLCHAIN] if executable or test else []
),
# TODO(hvd): replace with filegroups?
outputs = {
"classjar": "%{name}.jar",
Expand Down Expand Up @@ -319,6 +325,7 @@ def make_java_binary(executable, resolve_launcher_flag, has_launcher = False):
"args": attr.string_list(),
"output_licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
}),
remove_attrs = [] if executable else ["_java_runtime_toolchain_type"],
),
executable = executable,
)
Expand Down
21 changes: 1 addition & 20 deletions src/main/starlark/builtins_bzl/common/java/java_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ load(":common/java/java_util.bzl", "create_single_jar")
load(":common/java/java_helper.bzl", helper = "util")
load(":common/java/java_semantics.bzl", "semantics")
load(":common/rule_util.bzl", "merge_attrs")
load(":common/cc/cc_helper.bzl", "cc_helper")
load(":common/cc/semantics.bzl", cc_semantics = "semantics")
load(":common/proto/proto_info.bzl", "ProtoInfo")
load(":common/cc/cc_info.bzl", "CcInfo")
Expand Down Expand Up @@ -211,33 +210,15 @@ def basic_java_binary(

files = depset(files_to_build + common_info.files_to_build)

java_runtime_toolchain = semantics.find_java_runtime_toolchain(ctx)
transitive_runfiles_artifacts = depset(transitive = [
files,
java_attrs.runtime_classpath,
depset(transitive = launcher_info.runfiles),
java_runtime_toolchain.files,
])

# Add symlinks to the C++ runtime libraries under a path that can be built
# into the Java binary without having to embed the crosstool, gcc, and grte
# version information contained within the libraries' package paths.
runfiles_symlinks = {}

# TODO(hvd): do we need this in bazel? if yes, fix abs path check on windows
if not helper.is_absolute_path(ctx, java_runtime_toolchain.java_home):
runfiles_symlinks = {
("_cpp_runtimes/%s" % lib.basename): lib
for lib in cc_helper.find_cpp_toolchain(ctx).dynamic_runtime_lib(
feature_configuration = feature_config,
).to_list()
}

runfiles = ctx.runfiles(
transitive_files = transitive_runfiles_artifacts,
collect_default = True,
symlinks = runfiles_symlinks,
skip_conflict_checking = True,
)

if launcher_info.launcher:
Expand Down Expand Up @@ -348,10 +329,10 @@ def _generate_coverage_manifest(ctx, output, runtime_classpath):

#TODO(hvd): not needed in bazel
def _create_shared_archive(ctx, java_attrs):
runtime = semantics.find_java_runtime_toolchain(ctx)
classlist = ctx.file.classlist if hasattr(ctx.file, "classlist") else None
if not classlist:
return None
runtime = semantics.find_java_runtime_toolchain(ctx)
jsa = ctx.actions.declare_file("%s.jsa" % ctx.label.name)
merged = ctx.actions.declare_file(jsa.dirname + "/" + helper.strip_extension(jsa) + "-merged.jar")
create_single_jar(
Expand Down

0 comments on commit 8c3a4eb

Please sign in to comment.