Skip to content

Commit

Permalink
Support not bundling SemanticDB files in the output JAR
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaden Peterson committed Aug 26, 2024
1 parent 5eb0406 commit a698dd2
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 51 deletions.
3 changes: 2 additions & 1 deletion docs/newdocs/phases.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def _scala_binary_implementation(ctx):
return _run_phases(ctx, [
("resources", _phase_resources),
("classpaths", _phase_classpaths),
("semanticdb", _phase_semanticdb),
("javainfo", _phase_javainfo),
("compile", _phase_noop),
("singlejar", _phase_singlejar),
Expand All @@ -30,4 +31,4 @@ _NOTE: This is a work in progress_
Bazel scala annex allows users to customize `scala_binary`, `scala_library`, and `scala_test` by adding/replacing [phases](#phases).
Each of these rules has a corresponding `make_<rule>(*extras)` method which takes as arguments a list of of extra config items.

For an example of this in action, see [/rules/scala_with_scalafmt.bzl](/rules/scala_with_scalafmt.bzl) and [/rules/scalafmt/ext.blz](/rules/scalafmt/ext.bzl)
For an example of this in action, see [/rules/scala_with_scalafmt.bzl](/rules/scala_with_scalafmt.bzl) and [/rules/scalafmt/ext.blz](/rules/scalafmt/ext.bzl)
25 changes: 14 additions & 11 deletions rules/private/phases.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ load(":phases/phase_library_defaultinfo.bzl", _phase_library_defaultinfo = "phas
load(":phases/phase_noop.bzl", _phase_noop = "phase_noop")
load(":phases/phase_resources.bzl", _phase_resources = "phase_resources")
load(":phases/phase_scalafmt_nondefault_outputs.bzl", _phase_scalafmt_nondefault_outputs = "phase_scalafmt_nondefault_outputs")
load(":phases/phase_semanticdb.bzl", _phase_semanticdb = "phase_semanticdb")
load(":phases/phase_singlejar.bzl", _phase_singlejar = "phase_singlejar")
load(":phases/phase_test_launcher.bzl", _phase_test_launcher = "phase_test_launcher")
load(":phases/phase_zinc_compile.bzl", _phase_zinc_compile = "phase_zinc_compile")
Expand All @@ -20,6 +21,10 @@ adjust_phases = _adjust_phases

run_phases = _run_phases

phase_binary_deployjar = _phase_binary_deployjar

phase_binary_launcher = _phase_binary_launcher

phase_bootstrap_compile = _phase_bootstrap_compile

phase_classpaths = _phase_classpaths
Expand All @@ -30,24 +35,22 @@ phase_coverage_jacoco = _phase_coverage_jacoco

phase_ijinfo = _phase_ijinfo

phase_noop = _phase_noop
phase_javainfo = _phase_javainfo

phase_resources = _phase_resources
phase_library_defaultinfo = _phase_library_defaultinfo

phase_zinc_compile = _phase_zinc_compile
phase_noop = _phase_noop

phase_zinc_depscheck = _phase_zinc_depscheck
phase_resources = _phase_resources

phase_javainfo = _phase_javainfo
phase_scalafmt_nondefault_outputs = _phase_scalafmt_nondefault_outputs

phase_library_defaultinfo = _phase_library_defaultinfo
phase_semanticdb = _phase_semanticdb

phase_singlejar = _phase_singlejar

phase_binary_deployjar = _phase_binary_deployjar

phase_binary_launcher = _phase_binary_launcher
phase_test_launcher = _phase_test_launcher

phase_scalafmt_nondefault_outputs = _phase_scalafmt_nondefault_outputs
phase_zinc_compile = _phase_zinc_compile

phase_test_launcher = _phase_test_launcher
phase_zinc_depscheck = _phase_zinc_depscheck
4 changes: 2 additions & 2 deletions rules/private/phases/phase_bootstrap_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def phase_bootstrap_compile(ctx, g):
jar_creator = ctx.executable._jar_creator.path,
main_class = main_class,
output_jar = g.classpaths.jar.path,
scalacopts = " ".join(ctx.attr.scalacopts),
scalacopts = " ".join(ctx.attr.scalacopts + g.semanticdb.scalacopts),
srcs = srcs,
tmp = tmp.path,
),
Expand All @@ -65,7 +65,7 @@ def phase_bootstrap_compile(ctx, g):
inputs = inputs,
tools = [ctx.executable._jar_creator],
mnemonic = "BootstrapScalacompile",
outputs = [g.classpaths.jar, tmp],
outputs = [g.classpaths.jar, tmp] + g.semanticdb.outputs,
command = command,
execution_requirements = _resolve_execution_reqs(ctx, {}),
)
42 changes: 42 additions & 0 deletions rules/private/phases/phase_semanticdb.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@rules_scala_annex//rules:providers.bzl", _ScalaConfiguration = "ScalaConfiguration")

#
# PHASE: semanticdb
#
# Configures the compiler to output SemanticDB metadata. Note that this phase won't work without the
# SemanticDB compiler plugin being enabled.
#
def phase_semanticdb(ctx, g):
scala_configuration = ctx.attr.scala[_ScalaConfiguration]

if scala_configuration.semanticdb_bundle:
return struct(outputs = [], scalacopts = [])

outputs = []
semanticdb_directory = paths.join("_semanticdb/", ctx.label.name)
semanticdb_target_root = paths.join(paths.dirname(ctx.outputs.jar.path), semanticdb_directory)

for source in ctx.files.srcs:
if source.extension == "scala":
output_filename = paths.join(
semanticdb_directory,
"META-INF",
"semanticdb",
"{}.semanticdb".format(source.path),
)

outputs.append(ctx.actions.declare_file(output_filename))

if scala_configuration.version.startswith("2"):
scalacopts = [
"-P:semanticdb:failures:error",
"-P:semanticdb:targetroot:{}".format(semanticdb_target_root),
]
else:
scalacopts = [
"-semanticdb-target:{}".format(semanticdb_target_root),
"-Ysemanticdb",
]

return struct(outputs = outputs, scalacopts = scalacopts)
12 changes: 10 additions & 2 deletions rules/private/phases/phase_zinc_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def phase_zinc_compile(ctx, g):
]

zincs = [dep[_ZincInfo] for dep in ctx.attr.deps if _ZincInfo in dep]
scalacopts = ctx.attr.scalacopts + g.semanticdb.scalacopts

args = ctx.actions.args()

Expand All @@ -45,7 +46,7 @@ def phase_zinc_compile(ctx, g):
args.add_all("--compiler_classpath", g.classpaths.compiler)
args.add_all("--classpath", g.classpaths.compile)
args.add_all(scala_configuration.global_scalacopts, format_each = "--compiler_option=%s")
args.add_all(ctx.attr.scalacopts, format_each = "--compiler_option=%s")
args.add_all(scalacopts, format_each = "--compiler_option=%s")
args.add_all(javacopts, format_each = "--java_compiler_option=%s")
args.add(ctx.label, format = "--label=%s")
args.add("--main_manifest", mains_file)
Expand Down Expand Up @@ -73,7 +74,14 @@ def phase_zinc_compile(ctx, g):
] + [zinc.deps_files for zinc in zincs],
)

outputs = [g.classpaths.jar, mains_file, analysis_store, analysis_store_text, used, tmp]
outputs = [
g.classpaths.jar,
mains_file,
analysis_store,
analysis_store_text,
used,
tmp,
] + g.semanticdb.outputs

execution_requirements_tags = {
"supports-multiplex-workers": "1",
Expand Down
22 changes: 14 additions & 8 deletions rules/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ load(
ScalaConfiguration = provider(
doc = "Scala compile-time and runtime configuration",
fields = {
"version": "The Scala full version.",
"compiler_classpath": "The compiler classpath.",
"runtime_classpath": "The runtime classpath.",
"global_plugins": "Globally enabled compiler plugins",
"global_scalacopts": "Globally enabled compiler options",
"runtime_classpath": "The runtime classpath.",
"semanticdb_bundle": "Whether to bundle SemanticDB files in the resulting JAR. Note that in Scala 2, this requires the SemanticDB compiler plugin.",
"use_ijar": "Whether to use ijars for this Scala compiler",
"version": "The Scala full version.",
},
)

Expand All @@ -24,18 +25,14 @@ def _declare_scala_configuration_implementation(ctx):
global_plugins = ctx.attr.global_plugins,
global_scalacopts = ctx.attr.global_scalacopts,
runtime_classpath = ctx.attr.runtime_classpath,
version = ctx.attr.version,
semanticdb_bundle = ctx.attr.semanticdb_bundle,
use_ijar = ctx.attr.use_ijar,
version = ctx.attr.version,
),
]

declare_scala_configuration = rule(
attrs = {
"version": attr.string(mandatory = True),
"runtime_classpath": attr.label_list(
mandatory = True,
providers = [JavaInfo],
),
"compiler_classpath": attr.label_list(
mandatory = True,
providers = [JavaInfo],
Expand All @@ -47,6 +44,15 @@ declare_scala_configuration = rule(
"global_scalacopts": attr.string_list(
doc = "Scalac options that will always be enabled.",
),
"runtime_classpath": attr.label_list(
mandatory = True,
providers = [JavaInfo],
),
"semanticdb_bundle": attr.bool(
default = True,
doc = "Whether to bundle SemanticDB files in the resulting JAR. Note that in Scala 2, this requires the SemanticDB compiler plugin.",
),
"version": attr.string(mandatory = True),
},
doc = "Creates a `ScalaConfiguration`.",
implementation = _declare_scala_configuration_implementation,
Expand Down
58 changes: 35 additions & 23 deletions rules/scala.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ load(
_phase_library_defaultinfo = "phase_library_defaultinfo",
_phase_noop = "phase_noop",
_phase_resources = "phase_resources",
_phase_semanticdb = "phase_semanticdb",
_phase_singlejar = "phase_singlejar",
_phase_test_launcher = "phase_test_launcher",
_run_phases = "run_phases",
Expand Down Expand Up @@ -213,6 +214,7 @@ def _scala_library_implementation(ctx):
("resources", _phase_resources),
("classpaths", _phase_classpaths),
("javainfo", _phase_javainfo),
("semanticdb", _phase_semanticdb),
("compile", _phase_noop),
("singlejar", _phase_singlejar),
("coverage", _phase_coverage_jacoco),
Expand All @@ -226,6 +228,7 @@ def _scala_binary_implementation(ctx):
("resources", _phase_resources),
("classpaths", _phase_classpaths),
("javainfo", _phase_javainfo),
("semanticdb", _phase_semanticdb),
("compile", _phase_noop),
("singlejar", _phase_singlejar),
("coverage", _phase_coverage_jacoco),
Expand All @@ -240,6 +243,7 @@ def _scala_test_implementation(ctx):
("resources", _phase_resources),
("classpaths", _phase_classpaths),
("javainfo", _phase_javainfo),
("semanticdb", _phase_semanticdb),
("compile", _phase_noop),
("singlejar", _phase_singlejar),
("coverage", _phase_coverage_jacoco),
Expand Down Expand Up @@ -484,65 +488,79 @@ Generates Scaladocs.

configure_bootstrap_scala = rule(
attrs = {
"version": attr.string(mandatory = True),
"compiler_classpath": attr.label_list(
mandatory = True,
providers = [JavaInfo],
),
"runtime_classpath": attr.label_list(
mandatory = True,
providers = [JavaInfo],
),
"global_plugins": attr.label_list(
doc = "Scalac plugins that will always be enabled.",
providers = [JavaInfo],
),
"global_scalacopts": attr.string_list(
doc = "Scalac options that will always be enabled.",
),
"runtime_classpath": attr.label_list(
mandatory = True,
providers = [JavaInfo],
),
"semanticdb_bundle": attr.bool(
default = True,
doc = "Whether to bundle SemanticDB files in the resulting JAR. Note that in Scala 2, this requires the SemanticDB compiler plugin.",
),
"use_ijar": attr.bool(
doc = "Whether to use ijars for this compiler.",
default = True,
),
"version": attr.string(mandatory = True),
},
implementation = _configure_bootstrap_scala_implementation,
)

_configure_zinc_scala = rule(
attrs = {
"version": attr.string(mandatory = True),
"runtime_classpath": attr.label_list(
"compiler_bridge": attr.label(
allow_single_file = True,
mandatory = True,
providers = [JavaInfo],
),
"compiler_classpath": attr.label_list(
mandatory = True,
providers = [JavaInfo],
),
"compiler_bridge": attr.label(
allow_single_file = True,
mandatory = True,
),
"deps_direct": attr.string(default = "error"),
"deps_used": attr.string(default = "error"),
"global_plugins": attr.label_list(
doc = "Scalac plugins that will always be enabled.",
providers = [JavaInfo],
),
"global_scalacopts": attr.string_list(
doc = "Scalac options that will always be enabled.",
),
"incremental": attr.bool(
doc = "Whether Zinc's incremental compilation will be available for this Zinc compiler. If True, this requires additional configuration to use incremental compilation.",
default = False,
),
"log_level": attr.string(
doc = "Compiler log level",
default = "warn",
),
"runtime_classpath": attr.label_list(
mandatory = True,
providers = [JavaInfo],
),
"semanticdb_bundle": attr.bool(
default = True,
doc = "Whether to bundle SemanticDB files in the resulting JAR. Note that in Scala 2, this requires the SemanticDB compiler plugin.",
),
"use_ijar": attr.bool(
doc = "Whether to use ijars for this compiler.",
default = True,
),
"deps_direct": attr.string(default = "error"),
"deps_used": attr.string(default = "error"),
"incremental": attr.bool(
doc = "Whether Zinc's incremental compilation will be available for this Zinc compiler. If True, this requires additional configuration to use incremental compilation.",
default = False,
"version": attr.string(mandatory = True),
"_code_coverage_instrumentation_worker": attr.label(
default = "@rules_scala_annex//src/main/scala/higherkindness/rules_scala/workers/jacoco/instrumenter",
allow_files = True,
executable = True,
cfg = "host",
),
"_compile_worker": attr.label(
default = "@rules_scala_annex//src/main/scala/higherkindness/rules_scala/workers/zinc/compile",
Expand All @@ -556,12 +574,6 @@ _configure_zinc_scala = rule(
executable = True,
cfg = "host",
),
"_code_coverage_instrumentation_worker": attr.label(
default = "@rules_scala_annex//src/main/scala/higherkindness/rules_scala/workers/jacoco/instrumenter",
allow_files = True,
executable = True,
cfg = "host",
),
},
implementation = _configure_zinc_scala_implementation,
)
Expand Down
6 changes: 4 additions & 2 deletions rules/scala/private/provider.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ def configure_bootstrap_scala_implementation(ctx):
global_plugins = ctx.attr.global_plugins,
global_scalacopts = ctx.attr.global_scalacopts,
runtime_classpath = ctx.attr.runtime_classpath,
version = ctx.attr.version,
semanticdb_bundle = ctx.attr.semanticdb_bundle,
use_ijar = ctx.attr.use_ijar,
version = ctx.attr.version,
),
_ScalaRulePhase(
phases = [
Expand All @@ -37,8 +38,9 @@ def configure_zinc_scala_implementation(ctx):
global_plugins = ctx.attr.global_plugins,
global_scalacopts = ctx.attr.global_scalacopts,
runtime_classpath = ctx.attr.runtime_classpath,
version = ctx.attr.version,
semanticdb_bundle = ctx.attr.semanticdb_bundle,
use_ijar = ctx.attr.use_ijar,
version = ctx.attr.version,
),
_CodeCoverageConfiguration(
instrumentation_worker = ctx.attr._code_coverage_instrumentation_worker,
Expand Down
Loading

0 comments on commit a698dd2

Please sign in to comment.