From f9ae2b71abc76fd8e5ed0feb8bf256aaa6e8d17f Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Tue, 2 May 2023 10:45:36 +0200 Subject: [PATCH] Remove runtime dependency of non-executable java_binary 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 https://github.com/bazelbuild/rules_java/issues/64 Split off from #18262 --- .../bazel/java/bazel_java_binary.bzl | 5 +++- .../common/java/blaze_java_semantics.bzl | 29 +++++++++++++++++++ .../builtins_bzl/common/java/java_binary.bzl | 28 ++++++------------ .../common/java/java_semantics.bzl | 12 ++++++++ 4 files changed, 54 insertions(+), 20 deletions(-) create mode 100644 src/main/starlark/builtins_bzl/common/java/blaze_java_semantics.bzl diff --git a/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary.bzl b/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary.bzl index 34843cf1d76649..d184384afa5b78 100644 --- a/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary.bzl +++ b/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary.bzl @@ -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", @@ -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, ) diff --git a/src/main/starlark/builtins_bzl/common/java/blaze_java_semantics.bzl b/src/main/starlark/builtins_bzl/common/java/blaze_java_semantics.bzl new file mode 100644 index 00000000000000..20cd47e8d88a99 --- /dev/null +++ b/src/main/starlark/builtins_bzl/common/java/blaze_java_semantics.bzl @@ -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, +) diff --git a/src/main/starlark/builtins_bzl/common/java/java_binary.bzl b/src/main/starlark/builtins_bzl/common/java/java_binary.bzl index d62f5e3b6af78b..9e8c4917652ec7 100644 --- a/src/main/starlark/builtins_bzl/common/java/java_binary.bzl +++ b/src/main/starlark/builtins_bzl/common/java/java_binary.bzl @@ -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, ) @@ -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( diff --git a/src/main/starlark/builtins_bzl/common/java/java_semantics.bzl b/src/main/starlark/builtins_bzl/common/java/java_semantics.bzl index a3c6948738ae2d..f74f6703c97ae9 100644 --- a/src/main/starlark/builtins_bzl/common/java/java_semantics.bzl +++ b/src/main/starlark/builtins_bzl/common/java/java_semantics.bzl @@ -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", @@ -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 = [