Skip to content

Commit

Permalink
Replace zipper with singlejar (bazelbuild#1463)
Browse files Browse the repository at this point in the history
* Replace zipper with singlejar

Remove zipper from common attributes

singlejar is already there and can generate jars with resources

Looking at bazel source code `singlejar` utility is used for:
* [pack_sources](https://github.com/bazelbuild/bazel/blob/455454a56e961affb041a1d4a9214f7f313a05aa/src/main/java/com/google/devtools/build/lib/rules/java/JavaInfoBuildHelper.java#L198)
* [resources_jar](https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/rules/java/ResourceJarActionBuilder.java#L123)

Motivation:

* use precompiled binaries ie singlejar comes precompiled via remote_java_tools while zipper is built from sources (at least in remote execution)
* use single tool to accomplish same goal

* Fix //test/jmh:Run__test_jmh_test_jmh_jdk11  by specifying correct jdk version instead of system default
  • Loading branch information
simuons authored Jan 20, 2023
1 parent a0235fd commit cb950f5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 47 deletions.
6 changes: 0 additions & 6 deletions scala/private/common_attributes.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ implicit_deps = {
default = Label("@bazel_tools//tools/jdk:singlejar"),
allow_files = True,
),
"_zipper": attr.label(
executable = True,
cfg = "exec",
default = Label("@bazel_tools//tools/zip:zipper"),
allow_files = True,
),
"_java_runtime": attr.label(
default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
),
Expand Down
61 changes: 20 additions & 41 deletions scala/private/phases/phase_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -254,44 +254,28 @@ def _compile_or_empty(
)

def _build_nosrc_jar(ctx):
resources = _add_resources_cmd(ctx)
ijar_cmd = ""

# this ensures the file is not empty
resources += "META-INF/MANIFEST.MF=%s\n" % ctx.outputs.manifest.path

zipper_arg_path = ctx.actions.declare_file("%s_zipper_args" % ctx.label.name)
ctx.actions.write(zipper_arg_path, resources)
cmd = """
set -o errexit
set -o nounset
set -o pipefail
rm -f {jar_output}
{zipper} c {jar_output} @{path}
# ensures that empty src targets still emit a statsfile and a diagnosticsfile
touch {statsfile}
touch {diagnosticsfile}
""" + ijar_cmd

cmd = cmd.format(
path = zipper_arg_path.path,
jar_output = ctx.outputs.jar.path,
zipper = ctx.executable._zipper.path,
statsfile = ctx.outputs.statsfile.path,
diagnosticsfile = ctx.outputs.diagnosticsfile.path,
resources = [s + ":" + t for t, s in _resource_paths(ctx.files.resources, ctx.attr.resource_strip_prefix)]

args = ctx.actions.args()
args.set_param_file_format("multiline")
args.use_param_file("@%s")
args.add("--compression")
args.add("--normalize")
args.add("--exclude_build_data")
args.add("--warn_duplicate_resources")
args.add("--output", ctx.outputs.jar)
args.add_all("--resources", resources)

ctx.actions.run(
inputs = ctx.files.resources,
outputs = [ctx.outputs.jar],
executable = ctx.executable._singlejar,
progress_message = "scalac %s" % ctx.label,
arguments = [args],
)

outs = [ctx.outputs.jar, ctx.outputs.statsfile, ctx.outputs.diagnosticsfile]
inputs = ctx.files.resources + [ctx.outputs.manifest]

ctx.actions.run_shell(
inputs = inputs,
tools = [ctx.executable._zipper, zipper_arg_path],
outputs = outs,
command = cmd,
progress_message = "scala %s" % ctx.label,
arguments = [],
)
ctx.actions.write(ctx.outputs.statsfile, "")
ctx.actions.write(ctx.outputs.diagnosticsfile, "")

def _create_scala_compilation_provider(ctx, ijar, source_jar, deps_providers):
exports = []
Expand Down Expand Up @@ -378,11 +362,6 @@ def _try_to_compile_java_jar(
java_compilation_provider = provider,
)

def _add_resources_cmd(ctx):
paths = _resource_paths(ctx.files.resources, ctx.attr.resource_strip_prefix)
lines = ["{target}={source}\n".format(target = p[0], source = p[1]) for p in paths]
return "".join(lines)

def _collect_java_providers_of(deps):
providers = []
for dep in deps:
Expand Down
1 change: 1 addition & 0 deletions test/jmh/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ scala_benchmark_jmh(
scala_benchmark_jmh(
name = "test_jmh_jdk11",
srcs = ["TestJmhRuntimeJdk11.scala"],
runtime_jdk = "@bazel_tools//tools/jdk:remote_jdk11",
)

[sh_test(
Expand Down

0 comments on commit cb950f5

Please sign in to comment.