Skip to content

Commit

Permalink
Move code from rule impls/common to phases (bazelbuild#930)
Browse files Browse the repository at this point in the history
* collect_srcjars to phase

* move compile_or_empty and needed code to phase_compile (from rule_impls)

* phase_compile to take scalac_provider from phases instead of rule_impls

* rule_impls loads are loaded as private and unused are removed

* get_scalac_provider in phase_scalac_provider and not rule_impls

* move write_java_wrapper from rule_impls to phase_java_wrapper

* move merge_jars from rule_impls to phase_merge_jars

* move get_unused_dependency_checker_mode from rule_impls to get_unused_dependency_checker_mode

* move write_manifest from common to get_write_manifest

* move collect_jars_from_common_ctx from rule_impls to phase_collect_jars

* move write_executable from rule_impls to phase_write_executable

* linting

* [CR] inline _collect_jars_from_common_ctx

* [CR] inline _collect_srcjars

* [CR] inline write_java_wrapper

* [CR] inline merge_jars

* [CR] inline _write_executable
  • Loading branch information
ittaiz authored and Andre Rocha committed Jul 6, 2020
1 parent 10f9631 commit 60433e0
Show file tree
Hide file tree
Showing 12 changed files with 622 additions and 668 deletions.
11 changes: 0 additions & 11 deletions scala/private/common.bzl
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
load("@io_bazel_rules_scala//scala:jars_to_labels.bzl", "JarsToLabelsInfo")
load("@io_bazel_rules_scala//scala:plusone.bzl", "PlusOneDeps")

def write_manifest(ctx):
main_class = getattr(ctx.attr, "main_class", None)
write_manifest_file(ctx.actions, ctx.outputs.manifest, main_class)

def write_manifest_file(actions, output_file, main_class):
# TODO(bazel-team): I don't think this classpath is what you want
manifest = "Class-Path: \n"
Expand All @@ -13,13 +9,6 @@ def write_manifest_file(actions, output_file, main_class):

actions.write(output = output_file, content = manifest)

def collect_srcjars(targets):
srcjars = []
for target in targets:
if hasattr(target, "srcjars"):
srcjars.append(target.srcjars.srcjar)
return depset(srcjars)

def collect_jars(
dep_targets,
dependency_analyzer_is_off = True,
Expand Down
59 changes: 52 additions & 7 deletions scala/private/phases/phase_collect_jars.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
#
load(
"@io_bazel_rules_scala//scala/private:rule_impls.bzl",
"collect_jars_from_common_ctx",
"is_dependency_analyzer_off",
"is_plus_one_deps_off",
)
load(
"@io_bazel_rules_scala//scala/private:common.bzl",
"collect_jars",
)

def phase_scalatest_collect_jars(ctx, p):
Expand Down Expand Up @@ -59,16 +64,56 @@ def _phase_default_collect_jars(ctx, p, _args = struct()):
_args.unused_dependency_checker_mode if hasattr(_args, "unused_dependency_checker_mode") else p.unused_deps_checker,
)

# Extract very common code out from dependency analysis into single place
# automatically adds dependency on scala-library and scala-reflect
# collects jars from deps, runtime jars from runtime_deps, and
def _phase_collect_jars(
ctx,
base_classpath,
extra_deps,
extra_runtime_deps,
unused_dependency_checker_mode):
return collect_jars_from_common_ctx(
ctx,
base_classpath,
extra_deps,
extra_runtime_deps,
unused_dependency_checker_mode == "off",
unused_dependency_checker_is_off = unused_dependency_checker_mode == "off"
dependency_analyzer_is_off = is_dependency_analyzer_off(ctx)

deps_jars = collect_jars(
ctx.attr.deps + extra_deps + base_classpath,
dependency_analyzer_is_off,
unused_dependency_checker_is_off,
is_plus_one_deps_off(ctx),
)

(
cjars,
transitive_rjars,
jars2labels,
transitive_compile_jars,
deps_providers,
) = (
deps_jars.compile_jars,
deps_jars.transitive_runtime_jars,
deps_jars.jars2labels,
deps_jars.transitive_compile_jars,
deps_jars.deps_providers,
)

transitive_rjars = depset(
transitive = [transitive_rjars] +
_collect_runtime_jars(ctx.attr.runtime_deps + extra_runtime_deps),
)

return struct(
compile_jars = cjars,
jars2labels = jars2labels,
transitive_compile_jars = transitive_compile_jars,
transitive_runtime_jars = transitive_rjars,
deps_providers = deps_providers,
)

def _collect_runtime_jars(dep_targets):
runtime_jars = []

for dep_target in dep_targets:
runtime_jars.append(dep_target[JavaInfo].transitive_runtime_jars)

return runtime_jars
10 changes: 5 additions & 5 deletions scala/private/phases/phase_collect_srcjars.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#
# DOCUMENT THIS
#
load(
"@io_bazel_rules_scala//scala/private:common.bzl",
"collect_srcjars",
)

def phase_collect_srcjars(ctx, p):
# This will be used to pick up srcjars from non-scala library
# targets (like thrift code generation)
return collect_srcjars(ctx.attr.deps)
srcjars = []
for target in ctx.attr.deps:
if hasattr(target, "srcjars"):
srcjars.append(target.srcjars.srcjar)
return depset(srcjars)
Loading

0 comments on commit 60433e0

Please sign in to comment.