From beb4659dd3a8db54ee4ed97ed694ad3a772d2fa6 Mon Sep 17 00:00:00 2001 From: Googler Date: Mon, 19 Jun 2023 04:44:36 -0700 Subject: [PATCH] Consolidate utility methods in java_helper.bzl and remove java_util.bzl PiperOrigin-RevId: 541603674 Change-Id: Ief9d0884fd32ff8a72ca5c095520608083439ef7 --- .../bazel/java/bazel_java_binary.bzl | 5 +- .../builtins_bzl/common/java/java_binary.bzl | 5 +- .../builtins_bzl/common/java/java_helper.bzl | 44 ++++++++++++++ .../builtins_bzl/common/java/java_util.bzl | 59 ------------------- 4 files changed, 48 insertions(+), 65 deletions(-) delete mode 100644 src/main/starlark/builtins_bzl/common/java/java_util.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 8c6492761ff407..1a5ef91e1605c3 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 @@ -13,7 +13,6 @@ # limitations under the License. load(":common/rule_util.bzl", "merge_attrs") -load(":common/java/java_util.bzl", "shell_quote") load(":common/java/java_semantics.bzl", "semantics") load(":common/cc/cc_helper.bzl", "cc_helper") load(":common/java/java_helper.bzl", "helper") @@ -69,7 +68,7 @@ def _bazel_base_binary_impl(ctx, is_test_rule_class): fail("cannot determine test class") jvm_flags.extend([ "-ea", - "-Dbazel.test_suite=" + shell_quote(test_class), + "-Dbazel.test_suite=" + helper.shell_quote(test_class), ]) java_attrs = providers["InternalDeployJarInfo"].java_attrs @@ -223,7 +222,7 @@ def _create_stub(ctx, java_attrs, launcher, executable, jvm_flags, main_class, c "%set_jacoco_metadata%": "", "%set_jacoco_main_class%": "export JACOCO_MAIN_CLASS=" + coverage_main_class if coverage_enabled else "", "%set_jacoco_java_runfiles_root%": "export JACOCO_JAVA_RUNFILES_ROOT=${JAVA_RUNFILES}/" + workspace_prefix if coverage_enabled else "", - "%java_start_class%": shell_quote(main_class), + "%java_start_class%": helper.shell_quote(main_class), "%jvm_flags%": " ".join(jvm_flags), }, computed_substitutions = td, 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 635403f120319a..d4a6b7ab0a476c 100644 --- a/src/main/starlark/builtins_bzl/common/java/java_binary.bzl +++ b/src/main/starlark/builtins_bzl/common/java/java_binary.bzl @@ -15,7 +15,6 @@ """ Implementation of java_binary for bazel """ load(":common/java/java_common.bzl", "BASIC_JAVA_LIBRARY_IMPLICIT_ATTRS", "basic_java_library", "collect_deps") -load(":common/java/java_util.bzl", "create_single_jar") load(":common/java/java_helper.bzl", "helper") load(":common/java/java_semantics.bzl", "semantics") load(":common/rule_util.bzl", "merge_attrs") @@ -335,7 +334,7 @@ def _create_shared_archive(ctx, java_attrs): 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( + helper.create_single_jar( ctx, merged, java_attrs.runtime_jars, @@ -412,7 +411,7 @@ def _create_one_version_check(ctx, inputs): return output def _create_deploy_sources_jar(ctx, sources): - create_single_jar( + helper.create_single_jar( ctx, ctx.outputs.deploysrcjar, sources, diff --git a/src/main/starlark/builtins_bzl/common/java/java_helper.bzl b/src/main/starlark/builtins_bzl/common/java/java_helper.bzl index aa1f53f6d219f6..eebc50d57dec67 100644 --- a/src/main/starlark/builtins_bzl/common/java/java_helper.bzl +++ b/src/main/starlark/builtins_bzl/common/java/java_helper.bzl @@ -288,6 +288,48 @@ def _test_providers(ctx): return test_providers +def _create_single_jar(ctx, output, *input_depsets): + """Register action for the output jar. + + Args: + ctx: (RuleContext) Used to register the action. + output: (Artifact) Output file of the action. + *input_depsets: (list[depset[Artifact]]) Input files of the action. + + Returns: + (File) Output file which was used for registering the action. + """ + toolchain = semantics.find_java_toolchain(ctx) + args = ctx.actions.args() + args.set_param_file_format("shell").use_param_file("@%s", use_always = True) + args.add("--output", output) + args.add_all( + [ + "--compression", + "--normalize", + "--exclude_build_data", + "--warn_duplicate_resources", + ], + ) + all_inputs = depset(transitive = input_depsets) + args.add_all("--sources", all_inputs) + + ctx.actions.run( + mnemonic = "JavaSingleJar", + progress_message = "Building singlejar jar %s" % output.short_path, + executable = toolchain.single_jar, + toolchain = semantics.JAVA_TOOLCHAIN_TYPE, + inputs = all_inputs, + tools = [toolchain.single_jar], + outputs = [output], + arguments = [args], + ) + return output + +# TODO(hvd): use skylib shell.quote() +def _shell_quote(s): + return "'" + s.replace("'", "'\\''") + "'" + helper = struct( collect_all_targets_as_deps = _collect_all_targets_as_deps, filter_launcher_for_target = _filter_launcher_for_target, @@ -308,4 +350,6 @@ helper = struct( runfiles_enabled = _runfiles_enabled, get_test_support = _get_test_support, test_providers = _test_providers, + create_single_jar = _create_single_jar, + shell_quote = _shell_quote, ) diff --git a/src/main/starlark/builtins_bzl/common/java/java_util.bzl b/src/main/starlark/builtins_bzl/common/java/java_util.bzl deleted file mode 100644 index be5cae17f7dba9..00000000000000 --- a/src/main/starlark/builtins_bzl/common/java/java_util.bzl +++ /dev/null @@ -1,59 +0,0 @@ -# Copyright 2021 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Common util functions for java_* rules""" - -load(":common/java/java_semantics.bzl", "semantics") - -def create_single_jar(ctx, output, *input_depsets): - """Register action for the output jar. - - Args: - ctx: (RuleContext) Used to register the action. - output: (Artifact) Output file of the action. - *input_depsets: (list[depset[Artifact]]) Input files of the action. - - Returns: - (File) Output file which was used for registering the action. - """ - toolchain = semantics.find_java_toolchain(ctx) - args = ctx.actions.args() - args.set_param_file_format("shell").use_param_file("@%s", use_always = True) - args.add("--output", output) - args.add_all( - [ - "--compression", - "--normalize", - "--exclude_build_data", - "--warn_duplicate_resources", - ], - ) - all_inputs = depset(transitive = input_depsets) - args.add_all("--sources", all_inputs) - - ctx.actions.run( - mnemonic = "JavaSingleJar", - progress_message = "Building singlejar jar %s" % output.short_path, - executable = toolchain.single_jar, - toolchain = semantics.JAVA_TOOLCHAIN_TYPE, - inputs = all_inputs, - tools = [toolchain.single_jar], - outputs = [output], - arguments = [args], - ) - return output - -# TODO(hvd): use skylib shell.quote() -def shell_quote(s): - return "'" + s.replace("'", "'\\''") + "'"