From cb950f52dc1cf14e337fea687fe6b8ea31e46bed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simonas=20Pinevi=C4=8Dius?= Date: Fri, 20 Jan 2023 10:55:25 +0200 Subject: [PATCH] Replace zipper with singlejar (#1463) * 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 --- scala/private/common_attributes.bzl | 6 --- scala/private/phases/phase_compile.bzl | 61 +++++++++----------------- test/jmh/BUILD | 1 + 3 files changed, 21 insertions(+), 47 deletions(-) diff --git a/scala/private/common_attributes.bzl b/scala/private/common_attributes.bzl index 6b7471b61..35e21eb98 100644 --- a/scala/private/common_attributes.bzl +++ b/scala/private/common_attributes.bzl @@ -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"), ), diff --git a/scala/private/phases/phase_compile.bzl b/scala/private/phases/phase_compile.bzl index d3a44d9f7..9ab45804e 100644 --- a/scala/private/phases/phase_compile.bzl +++ b/scala/private/phases/phase_compile.bzl @@ -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 = [] @@ -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: diff --git a/test/jmh/BUILD b/test/jmh/BUILD index c3aa01dfe..1942fbbe8 100644 --- a/test/jmh/BUILD +++ b/test/jmh/BUILD @@ -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(