From 09ca59ff43ce6a01722523ff331c48470a8c03eb Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Tue, 1 Aug 2023 12:05:51 +0200 Subject: [PATCH] Remove unused Java runtime dependency of Bazel deploy jar rule Bazel's deploy jar rule never uses the hermetic runtime feature and thus doesn't need a Java runtime dependency. --- .../java/bazel_java_binary_deploy_jar.bzl | 6 ++- .../common/java/java_binary_deploy_jar.bzl | 49 ++++++++++++------- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary_deploy_jar.bzl b/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary_deploy_jar.bzl index 204de369a57a93..bceab88df27f3f 100644 --- a/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary_deploy_jar.bzl +++ b/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary_deploy_jar.bzl @@ -55,8 +55,12 @@ def _bazel_deploy_jars_impl(ctx): build_info_files, str(ctx.attr.binary.label), manifest_lines = info.manifest_lines, + hermetic_runtime_support = False, ) return [] -deploy_jars = make_deploy_jars_rule(implementation = _bazel_deploy_jars_impl) +deploy_jars = make_deploy_jars_rule( + implementation = _bazel_deploy_jars_impl, + hermetic_runtime_support = False, +) diff --git a/src/main/starlark/builtins_bzl/common/java/java_binary_deploy_jar.bzl b/src/main/starlark/builtins_bzl/common/java/java_binary_deploy_jar.bzl index b86aa36b41a446..dfbf9c9cb9db42 100644 --- a/src/main/starlark/builtins_bzl/common/java/java_binary_deploy_jar.bzl +++ b/src/main/starlark/builtins_bzl/common/java/java_binary_deploy_jar.bzl @@ -37,6 +37,7 @@ def create_deploy_archives( build_info_files, build_target, hermetic = False, + hermetic_runtime_support = True, add_exports = depset(), add_opens = depset(), shared_archive = None, @@ -57,6 +58,7 @@ def create_deploy_archives( strip_as_default: (bool) Whether to create unstripped deploy jar build_info_files: ([File]) the artifacts containing workspace status for the current build hermetic: (bool) + hermetic_runtime_support: (bool) add_exports: (depset) add_opens: (depset) shared_archive: (File) Optional .jsa artifact @@ -95,6 +97,7 @@ def create_deploy_archives( one_version_allowlist = one_version_allowlist, multi_release = multi_release, hermetic = hermetic, + hermetic_runtime_support = hermetic_runtime_support, add_exports = add_exports, add_opens = add_opens, extra_args = extra_args, @@ -116,6 +119,7 @@ def create_deploy_archives( output = ctx.outputs.unstrippeddeployjar, multi_release = multi_release, hermetic = hermetic, + hermetic_runtime_support = hermetic_runtime_support, extra_args = extra_args, ) else: @@ -139,6 +143,7 @@ def create_deploy_archive( one_version_allowlist = None, multi_release = False, hermetic = False, + hermetic_runtime_support = True, add_exports = [], add_opens = [], extra_args = []): @@ -167,8 +172,6 @@ def create_deploy_archive( add_opens: (depset) extra_args: (list[Args]) Optional arguments for the deploy jar action """ - runtime = semantics.find_java_runtime_toolchain(ctx) - input_files = [] input_files.extend(build_info_files) @@ -209,17 +212,21 @@ def create_deploy_archive( if multi_release: args.add("--multi_release") - hermetic_files = runtime.hermetic_files - if hermetic and runtime.lib_modules != None and hermetic_files != None: - java_home = runtime.java_home - lib_modules = runtime.lib_modules - args.add("--hermetic_java_home", java_home) - args.add("--jdk_lib_modules", lib_modules) - args.add_all("--resources", hermetic_files) - input_files.append(lib_modules) + if hermetic_runtime_support: + runtime = semantics.find_java_runtime_toolchain(ctx) + hermetic_files = runtime.hermetic_files + if hermetic and runtime.lib_modules != None and hermetic_files != None: + java_home = runtime.java_home + lib_modules = runtime.lib_modules + args.add("--hermetic_java_home", java_home) + args.add("--jdk_lib_modules", lib_modules) + args.add_all("--resources", hermetic_files) + input_files.append(lib_modules) - if shared_archive == None: - shared_archive = runtime.default_cds + if shared_archive == None: + shared_archive = runtime.default_cds + else: + hermetic_files = None if shared_archive: input_files.append(shared_archive) @@ -233,8 +240,7 @@ def create_deploy_archive( classpath_resources, runtime_classpath, runfiles, - hermetic_files, - ]) + ] + ([hermetic_files] if hermetic_files else [])) ctx.actions.run( mnemonic = "JavaDeployJar", @@ -254,7 +260,7 @@ def _implicit_outputs(binary): "unstrippeddeployjar": "%s_deploy.jar.unstripped" % binary_name, } -def make_deploy_jars_rule(implementation): +def make_deploy_jars_rule(implementation, hermetic_runtime_support = True): """Creates the deploy jar auxiliary rule for java_binary Args: @@ -274,12 +280,19 @@ def make_deploy_jars_rule(implementation): ), "_cc_toolchain": attr.label(default = "@" + cc_semantics.get_repo() + "//tools/cpp:current_cc_toolchain"), "_java_toolchain_type": attr.label(default = semantics.JAVA_TOOLCHAIN_TYPE), - "_java_runtime_toolchain_type": attr.label(default = semantics.JAVA_RUNTIME_TOOLCHAIN_TYPE), "_build_info_translator": attr.label( default = semantics.BUILD_INFO_TRANSLATOR_LABEL, ), - }, + } | ( + { + "_java_runtime_toolchain_type": attr.label( + default = semantics.JAVA_RUNTIME_TOOLCHAIN_TYPE, + ), + } if hermetic_runtime_support else {} + ), outputs = _implicit_outputs, fragments = ["java"], - 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 hermetic_runtime_support else [] + ), )