Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor build_deployable #832

Merged
merged 4 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions scala/private/rule_impls.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -548,22 +548,32 @@ def _create_scala_compilation_provider(ctx, ijar, source_jar, deps_providers):
runtime_deps = runtime_deps,
)

def build_deployable(ctx, jars_list):
# This calls bazels singlejar utility.
# For a full list of available command line options see:
# https://github.com/bazelbuild/bazel/blob/master/src/java_tools/singlejar/java/com/google/devtools/build/singlejar/SingleJar.java#L311
# Use --compression to reduce size of deploy jars.
def merge_jars(actions, deploy_jar, singlejar_executable, label, jars_list, main_class = ""):
"""Calls Bazel's singlejar utility.

For a full list of available command line options see:
https://github.com/bazelbuild/bazel/blob/697d219526bffbecd29f29b402c9122ec5d9f2ee/src/java_tools/singlejar/java/com/google/devtools/build/singlejar/SingleJar.java#L337
Use --compression to reduce size of deploy jars.

Args:
actions: The actions module from ctx: https://docs.bazel.build/versions/master/skylark/lib/actions.html
deploy_jar: The deploy jar, usually defined in ctx.outputs.
singlejar_executable: The singlejar executable file.
label: The label of the target, usually from ctx.label.
jars_list: The jars to pass to singlejar.
main_class: The main class to run, if any. Defaults to an empty string.
"""
args = ["--compression", "--normalize", "--sources"]
args.extend([j.path for j in jars_list])
if getattr(ctx.attr, "main_class", ""):
args.extend(["--main_class", ctx.attr.main_class])
args.extend(["--output", ctx.outputs.deploy_jar.path])
ctx.actions.run(
if main_class:
args.extend(["--main_class", main_class])
args.extend(["--output", deploy_jar.path])
actions.run(
inputs = jars_list,
outputs = [ctx.outputs.deploy_jar],
executable = ctx.executable._singlejar,
outputs = [deploy_jar],
executable = singlejar_executable,
mnemonic = "ScalaDeployJar",
progress_message = "scala deployable %s" % ctx.label,
progress_message = "scala deployable %s" % label,
long-stripe marked this conversation as resolved.
Show resolved Hide resolved
arguments = args,
)

Expand Down Expand Up @@ -832,7 +842,14 @@ def scala_binary_common(
) # no need to build an ijar for an executable
rjars = depset(outputs.full_jars, transitive = [rjars])

build_deployable(ctx, rjars.to_list())
merge_jars(
actions = ctx.actions,
deploy_jar = ctx.outputs.deploy_jar,
singlejar_executable = ctx.executable._singlejar,
label = ctx.label,
jars_list = rjars.to_list(),
main_class = getattr(ctx.attr, "main_class", ""),
)

runfiles = ctx.runfiles(
transitive_files = depset(
Expand Down
11 changes: 9 additions & 2 deletions scala/private/rules/scala_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ load(
)
load(
"@io_bazel_rules_scala//scala/private:rule_impls.bzl",
"build_deployable",
"merge_jars",
"collect_jars_from_common_ctx",
"compile_or_empty",
"get_scalac_provider",
Expand Down Expand Up @@ -82,7 +82,14 @@ def _lib(

transitive_rjars = depset(outputs.full_jars, transitive = [transitive_rjars])

build_deployable(ctx, transitive_rjars.to_list())
merge_jars(
actions = ctx.actions,
deploy_jar = ctx.outputs.deploy_jar,
singlejar_executable = ctx.executable._singlejar,
label = ctx.label,
jars_list = transitive_rjars.to_list(),
main_class = getattr(ctx.attr, "main_class", ""),
)

# Using transitive_files since transitive_rjars a depset and avoiding linearization
runfiles = ctx.runfiles(
Expand Down