From 96f9d94cdbdcc09422a0aa6d9a7aeb256c181b26 Mon Sep 17 00:00:00 2001 From: Andy Scott Date: Tue, 28 Jan 2020 22:11:15 -0800 Subject: [PATCH] unify all of the default info phases implementations (#958) * unify all of the default info phases implementations * collect_data = True --- scala/private/phases/phase_compile.bzl | 1 + .../phases/phase_coverage_runfiles.bzl | 1 + .../phases/phase_declare_executable.bzl | 8 ++- scala/private/phases/phase_default_info.bzl | 51 ++++++++++--------- scala/private/phases/phase_runfiles.bzl | 9 ++-- .../private/phases/phase_write_executable.bzl | 2 +- scala/private/phases/phases.bzl | 11 +--- scala/private/rules/scala_binary.bzl | 4 +- scala/private/rules/scala_junit_test.bzl | 4 +- scala/private/rules/scala_library.bzl | 8 +-- scala/private/rules/scala_repl.bzl | 4 +- scala/private/rules/scala_test.bzl | 4 +- 12 files changed, 54 insertions(+), 53 deletions(-) diff --git a/scala/private/phases/phase_compile.bzl b/scala/private/phases/phase_compile.bzl index d5b72ea3c..c9ea6f4e5 100644 --- a/scala/private/phases/phase_compile.bzl +++ b/scala/private/phases/phase_compile.bzl @@ -165,6 +165,7 @@ def _phase_compile( class_jar = out.class_jar, coverage = out.coverage.external, full_jars = out.full_jars, + files = depset(out.full_jars), ijar = out.ijar, ijars = out.ijars, rjars = depset(out.full_jars, transitive = [rjars]), diff --git a/scala/private/phases/phase_coverage_runfiles.bzl b/scala/private/phases/phase_coverage_runfiles.bzl index 00e50cf44..74c8173d4 100644 --- a/scala/private/phases/phase_coverage_runfiles.bzl +++ b/scala/private/phases/phase_coverage_runfiles.bzl @@ -24,5 +24,6 @@ def phase_coverage_runfiles(ctx, p): coverage_runfiles = ctx.files._jacocorunner + ctx.files._lcov_merger + coverage_replacements.values() return struct( coverage_runfiles = coverage_runfiles, + runfiles = depset(coverage_runfiles), rjars = rjars, ) diff --git a/scala/private/phases/phase_declare_executable.bzl b/scala/private/phases/phase_declare_executable.bzl index c7bce9a20..749a20f2c 100644 --- a/scala/private/phases/phase_declare_executable.bzl +++ b/scala/private/phases/phase_declare_executable.bzl @@ -10,6 +10,10 @@ load( def phase_declare_executable(ctx, p): if (is_windows(ctx)): - return ctx.actions.declare_file("%s.exe" % ctx.label.name) + return struct( + executable = ctx.actions.declare_file("%s.exe" % ctx.label.name), + ) else: - return ctx.actions.declare_file(ctx.label.name) + return struct( + executable = ctx.actions.declare_file(ctx.label.name), + ) diff --git a/scala/private/phases/phase_default_info.bzl b/scala/private/phases/phase_default_info.bzl index 8847e8a35..53f056883 100644 --- a/scala/private/phases/phase_default_info.bzl +++ b/scala/private/phases/phase_default_info.bzl @@ -3,34 +3,39 @@ # # DOCUMENT THIS # -def phase_default_info_binary(ctx, p): - return struct( - external_providers = { - "DefaultInfo": DefaultInfo( - executable = p.declare_executable, - files = depset([p.declare_executable] + p.compile.full_jars), - runfiles = p.runfiles.runfiles, - ), - }, - ) -def phase_default_info_library(ctx, p): - return struct( - external_providers = { - "DefaultInfo": DefaultInfo( - files = depset(p.compile.full_jars), - runfiles = p.runfiles.runfiles, - ), - }, - ) +def phase_default_info(ctx, p): + executable = None + files = [] + runfiles = [] + + phase_names = dir(p) + phase_names.remove("to_json") + phase_names.remove("to_proto") + for phase_name in phase_names: + phase = getattr(p, phase_name) + + if hasattr(phase, "executable"): + if executable == None: + executable = phase.executable + else: + fail("only one executable may be provided") + + if hasattr(phase, "files"): + files.append(phase.files) + + if hasattr(phase, "runfiles"): + runfiles.append(phase.runfiles) -def phase_default_info_scalatest(ctx, p): return struct( external_providers = { "DefaultInfo": DefaultInfo( - executable = p.declare_executable, - files = depset([p.declare_executable] + p.compile.full_jars), - runfiles = ctx.runfiles(p.coverage_runfiles.coverage_runfiles, transitive_files = p.runfiles.runfiles.files), + executable = executable, + files = depset(transitive = files), + # TODO: + # Per Bazel documentation, we should avoid using collect_data. The core phases need to be updated + # before we can make the adjustment. + runfiles = ctx.runfiles(transitive_files = depset(transitive = runfiles), collect_data = True), ), }, ) diff --git a/scala/private/phases/phase_runfiles.bzl b/scala/private/phases/phase_runfiles.bzl index c2b164222..03ab539a8 100644 --- a/scala/private/phases/phase_runfiles.bzl +++ b/scala/private/phases/phase_runfiles.bzl @@ -24,7 +24,7 @@ def phase_runfiles_scalatest(ctx, p): args = struct( transitive_files = depset( - [p.declare_executable, p.java_wrapper] + ctx.files._java_runtime + runfiles_ext, + [p.java_wrapper] + ctx.files._java_runtime + runfiles_ext, transitive = [p.compile.rjars], ), args_file = args_file, @@ -38,7 +38,7 @@ def _phase_runfiles_default(ctx, p, _args = struct()): return _phase_runfiles( ctx, _args.transitive_files if hasattr(_args, "transitive_files") else depset( - [p.declare_executable, p.java_wrapper] + ctx.files._java_runtime, + [p.java_wrapper] + ctx.files._java_runtime, transitive = [p.compile.rjars], ), _args.args_file if hasattr(_args, "args_file") else None, @@ -49,10 +49,7 @@ def _phase_runfiles( transitive_files, args_file): return struct( - runfiles = ctx.runfiles( - transitive_files = transitive_files, - collect_data = True, - ), + runfiles = transitive_files, args_file = args_file, ) diff --git a/scala/private/phases/phase_write_executable.bzl b/scala/private/phases/phase_write_executable.bzl index bae55fa99..3c8c11fbc 100644 --- a/scala/private/phases/phase_write_executable.bzl +++ b/scala/private/phases/phase_write_executable.bzl @@ -67,7 +67,7 @@ def _phase_write_executable( jvm_flags, use_jacoco, main_class): - executable = p.declare_executable + executable = p.declare_executable.executable wrapper = p.java_wrapper if (is_windows(ctx)): diff --git a/scala/private/phases/phases.bzl b/scala/private/phases/phases.bzl index 102c9fe87..cc2396684 100644 --- a/scala/private/phases/phases.bzl +++ b/scala/private/phases/phases.bzl @@ -45,12 +45,7 @@ load( _phase_runfiles_library = "phase_runfiles_library", _phase_runfiles_scalatest = "phase_runfiles_scalatest", ) -load( - "@io_bazel_rules_scala//scala/private:phases/phase_default_info.bzl", - _phase_default_info_binary = "phase_default_info_binary", - _phase_default_info_library = "phase_default_info_library", - _phase_default_info_scalatest = "phase_default_info_scalatest", -) +load("@io_bazel_rules_scala//scala/private:phases/phase_default_info.bzl", _phase_default_info = "phase_default_info") load("@io_bazel_rules_scala//scala/private:phases/phase_scalac_provider.bzl", _phase_scalac_provider = "phase_scalac_provider") load("@io_bazel_rules_scala//scala/private:phases/phase_write_manifest.bzl", _phase_write_manifest = "phase_write_manifest") load("@io_bazel_rules_scala//scala/private:phases/phase_collect_srcjars.bzl", _phase_collect_srcjars = "phase_collect_srcjars") @@ -127,9 +122,7 @@ phase_runfiles_scalatest = _phase_runfiles_scalatest phase_runfiles_common = _phase_runfiles_common # default_info -phase_default_info_binary = _phase_default_info_binary -phase_default_info_library = _phase_default_info_library -phase_default_info_scalatest = _phase_default_info_scalatest +phase_default_info = _phase_default_info # scalafmt phase_scalafmt = _phase_scalafmt diff --git a/scala/private/rules/scala_binary.bzl b/scala/private/rules/scala_binary.bzl index ec380a1ba..07afd2574 100644 --- a/scala/private/rules/scala_binary.bzl +++ b/scala/private/rules/scala_binary.bzl @@ -15,7 +15,7 @@ load( "phase_collect_jars_common", "phase_compile_binary", "phase_declare_executable", - "phase_default_info_binary", + "phase_default_info", "phase_java_wrapper_common", "phase_merge_jars", "phase_runfiles_common", @@ -42,7 +42,7 @@ def _scala_binary_impl(ctx): ("merge_jars", phase_merge_jars), ("runfiles", phase_runfiles_common), ("write_executable", phase_write_executable_common), - ("default_info", phase_default_info_binary), + ("default_info", phase_default_info), ], ) diff --git a/scala/private/rules/scala_junit_test.bzl b/scala/private/rules/scala_junit_test.bzl index b98f8d9c3..8b58180b7 100644 --- a/scala/private/rules/scala_junit_test.bzl +++ b/scala/private/rules/scala_junit_test.bzl @@ -14,7 +14,7 @@ load( "phase_collect_jars_junit_test", "phase_compile_junit_test", "phase_declare_executable", - "phase_default_info_binary", + "phase_default_info", "phase_java_wrapper_common", "phase_jvm_flags", "phase_merge_jars", @@ -47,7 +47,7 @@ def _scala_junit_test_impl(ctx): ("runfiles", phase_runfiles_common), ("jvm_flags", phase_jvm_flags), ("write_executable", phase_write_executable_junit_test), - ("default_info", phase_default_info_binary), + ("default_info", phase_default_info), ], ) diff --git a/scala/private/rules/scala_library.bzl b/scala/private/rules/scala_library.bzl index 2098851f7..17356b843 100644 --- a/scala/private/rules/scala_library.bzl +++ b/scala/private/rules/scala_library.bzl @@ -26,7 +26,7 @@ load( "phase_compile_library", "phase_compile_library_for_plugin_bootstrapping", "phase_compile_macro_library", - "phase_default_info_library", + "phase_default_info", "phase_merge_jars", "phase_runfiles_library", "phase_scalac_provider", @@ -66,7 +66,7 @@ def _scala_library_impl(ctx): ("merge_jars", phase_merge_jars), ("runfiles", phase_runfiles_library), ("collect_exports_jars", phase_collect_exports_jars), - ("default_info", phase_default_info_library), + ("default_info", phase_default_info), ], ) @@ -142,7 +142,7 @@ def _scala_library_for_plugin_bootstrapping_impl(ctx): ("merge_jars", phase_merge_jars), ("runfiles", phase_runfiles_library), ("collect_exports_jars", phase_collect_exports_jars), - ("default_info", phase_default_info_library), + ("default_info", phase_default_info), ], ) @@ -197,7 +197,7 @@ def _scala_macro_library_impl(ctx): ("merge_jars", phase_merge_jars), ("runfiles", phase_runfiles_library), ("collect_exports_jars", phase_collect_exports_jars), - ("default_info", phase_default_info_library), + ("default_info", phase_default_info), ], ) diff --git a/scala/private/rules/scala_repl.bzl b/scala/private/rules/scala_repl.bzl index 1550b0c9d..3d508af45 100644 --- a/scala/private/rules/scala_repl.bzl +++ b/scala/private/rules/scala_repl.bzl @@ -15,7 +15,7 @@ load( "phase_collect_jars_repl", "phase_compile_repl", "phase_declare_executable", - "phase_default_info_binary", + "phase_default_info", "phase_java_wrapper_repl", "phase_merge_jars", "phase_runfiles_common", @@ -43,7 +43,7 @@ def _scala_repl_impl(ctx): ("merge_jars", phase_merge_jars), ("runfiles", phase_runfiles_common), ("write_executable", phase_write_executable_repl), - ("default_info", phase_default_info_binary), + ("default_info", phase_default_info), ], ) diff --git a/scala/private/rules/scala_test.bzl b/scala/private/rules/scala_test.bzl index d9d42ead9..1a6bd3e7d 100644 --- a/scala/private/rules/scala_test.bzl +++ b/scala/private/rules/scala_test.bzl @@ -16,7 +16,7 @@ load( "phase_compile_scalatest", "phase_coverage_runfiles", "phase_declare_executable", - "phase_default_info_scalatest", + "phase_default_info", "phase_java_wrapper_common", "phase_merge_jars", "phase_runfiles_scalatest", @@ -44,7 +44,7 @@ def _scala_test_impl(ctx): ("runfiles", phase_runfiles_scalatest), ("coverage_runfiles", phase_coverage_runfiles), ("write_executable", phase_write_executable_scalatest), - ("default_info", phase_default_info_scalatest), + ("default_info", phase_default_info), ], )