Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove runtime dependency of non-executable java_binary #18620

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,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 @@ -306,6 +308,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# TODO: Remove on import. This file only exists to indicate the changes that have to be made to the
# internal version of java_semantics.bzl.

def _get_java_runtime_dependent_runfiles_and_symlinks(
ctx,
*,
executable,
feature_config,
is_absolute_path):
java_runtime_toolchain = semantics.find_java_runtime_toolchain(ctx)

# 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 = {}

if not 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()
}

return [java_runtime_toolchain.files], runfiles_symlinks

semantics = struct(
get_java_runtime_dependent_runfiles_and_symlinks = _get_java_runtime_dependent_runfiles_and_symlinks,
)
28 changes: 9 additions & 19 deletions src/main/starlark/builtins_bzl/common/java/java_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -211,32 +211,22 @@ def basic_java_binary(

files = depset(files_to_build + common_info.files_to_build)

java_runtime_toolchain = semantics.find_java_runtime_toolchain(ctx)
runtime_runfiles, runtime_symlinks = semantics.get_java_runtime_dependent_runfiles_and_symlinks(
ctx,
executable = executable,
feature_config = feature_config,
is_absolute_path = helper.is_absolute_path,
)
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()
}
] + runtime_runfiles)

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

Expand Down Expand Up @@ -348,10 +338,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
12 changes: 12 additions & 0 deletions src/main/starlark/builtins_bzl/common/java/java_semantics.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ def _stamping_enabled(ctx, stamp):
def _get_build_info(ctx, stamp):
return java_common.get_build_info(ctx, _stamping_enabled(ctx, stamp))

def _get_java_runtime_dependent_runfiles_and_symlinks(
ctx,
*,
executable,
feature_config,
is_absolute_path):
if not executable:
return [], {}
java_runtime_toolchain = semantics.find_java_runtime_toolchain(ctx)
return [java_runtime_toolchain.files], {}

semantics = struct(
JAVA_TOOLCHAIN_LABEL = "@bazel_tools//tools/jdk:current_java_toolchain",
JAVA_TOOLCHAIN_TYPE = "@bazel_tools//tools/jdk:toolchain_type",
Expand All @@ -62,6 +73,7 @@ semantics = struct(
JAVA_RUNTIME_TOOLCHAIN_TYPE = "@bazel_tools//tools/jdk:runtime_toolchain_type",
JAVA_RUNTIME_TOOLCHAIN = _builtins.toplevel.config_common.toolchain_type("@bazel_tools//tools/jdk:runtime_toolchain_type", mandatory = True),
find_java_runtime_toolchain = _find_java_runtime_toolchain,
get_java_runtime_dependent_runfiles_and_symlinks = _get_java_runtime_dependent_runfiles_and_symlinks,
JAVA_PLUGINS_FLAG_ALIAS_LABEL = "@bazel_tools//tools/jdk:java_plugins_flag_alias",
EXTRA_SRCS_TYPES = [],
ALLOWED_RULES_IN_DEPS = [
Expand Down