Skip to content

Commit

Permalink
Consolidate utility methods in java_helper.bzl and remove java_util.bzl
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 541603674
Change-Id: Ief9d0884fd32ff8a72ca5c095520608083439ef7
  • Loading branch information
hvadehra authored and copybara-github committed Jun 19, 2023
1 parent bfc05e7 commit beb4659
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions src/main/starlark/builtins_bzl/common/java/java_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
44 changes: 44 additions & 0 deletions src/main/starlark/builtins_bzl/common/java/java_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
)
59 changes: 0 additions & 59 deletions src/main/starlark/builtins_bzl/common/java/java_util.bzl

This file was deleted.

0 comments on commit beb4659

Please sign in to comment.