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

Implement an ijar equivalent for Scala 3 #1679

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ rules_proto_toolchains()

load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")

scala_config(enable_compiler_dependency_tracking = True)
scala_config(
enable_compiler_dependency_tracking = True,
scala_versions = [
"2.12.20",
"3.6.2",
],
)

load("//scala:toolchains.bzl", "scala_toolchains")

Expand Down
1 change: 1 addition & 0 deletions scala/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ scala_toolchain(
[
toolchain(
name = tc,
target_settings = ["@io_bazel_rules_scala_config//:scala_version" + version_suffix(SCALA_VERSION)],
toolchain = tc + "_impl",
toolchain_type = "//scala:toolchain_type",
visibility = ["//visibility:public"],
Expand Down
5 changes: 5 additions & 0 deletions scala/private/common_attributes.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ common_attrs.update({
executable = True,
cfg = "exec",
),
"_dottyijar": attr.label(
cfg = "exec",
default = "//src/scala/io/bazel/rules_scala/dottyijar",
executable = True,
),
})

implicit_deps = {
Expand Down
21 changes: 20 additions & 1 deletion scala/private/macros/scala_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ _COMPILER_SOURCES_ENTRY_TEMPLATE = """
"@io_bazel_rules_scala_config//:scala_version{scala_version_suffix}":
"@scala_compiler_source{scala_version_suffix}//:src","""

_IZUMI_REFLECT_DEPS = ["dev_zio_izumi_reflect_thirdparty_boopickle_shaded"]
_JUNIT_DEPS = ["io_bazel_rules_scala_org_hamcrest_hamcrest_core"]
_SPECS2_DEPS = [
"org_portable_scala_portable_scala_reflect",
"org_scala_sbt_test_interface",
]

def _compiler_sources_repo_impl(rctx):
sources = [
_COMPILER_SOURCES_ENTRY_TEMPLATE.format(
Expand Down Expand Up @@ -144,12 +151,19 @@ def rules_scala_setup(scala_compiler_srcjar = None):

def _artifact_ids(scala_version):
result = [
"commons_io_commons_io",
"io_bazel_rules_scala_junit_junit",
"io_bazel_rules_scala_org_specs2_specs2_common",
"io_bazel_rules_scala_org_specs2_specs2_core",
"io_bazel_rules_scala_org_specs2_specs2_fp",
"io_bazel_rules_scala_org_specs2_specs2_junit",
"io_bazel_rules_scala_org_specs2_specs2_matcher",
"io_bazel_rules_scala_scala_compiler",
"io_bazel_rules_scala_scala_library",
"io_bazel_rules_scala_scala_parser_combinators",
"io_bazel_rules_scala_scala_xml",
"org_scala_lang_modules_scala_collection_compat",
]
] + _JUNIT_DEPS + _SPECS2_DEPS

if scala_version.startswith("2."):
result.extend([
Expand All @@ -167,12 +181,15 @@ def _artifact_ids(scala_version):

if scala_version.startswith("3."):
result.extend([
"com_softwaremill_common_tagging",
"dev_zio_izumi_reflect",
"io_bazel_rules_scala_scala_asm",
"io_bazel_rules_scala_scala_compiler_2",
"io_bazel_rules_scala_scala_interfaces",
"io_bazel_rules_scala_scala_library_2",
"io_bazel_rules_scala_scala_reflect_2",
"io_bazel_rules_scala_scala_tasty_core",
"io_bazel_rules_scala_scala_tasty_inspector",
"org_jline_jline_native",
"org_jline_jline_reader",
"org_jline_jline_terminal",
Expand All @@ -181,6 +198,8 @@ def _artifact_ids(scala_version):
"org_scala_sbt_util_interface",
])

result.extend(_IZUMI_REFLECT_DEPS)

return result

def rules_scala_toolchain_deps_repositories(
Expand Down
39 changes: 29 additions & 10 deletions scala/private/phases/phase_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ def phase_compile_library(ctx, p):
return _phase_compile_default(ctx, p, args)

def phase_compile_library_for_plugin_bootstrapping(ctx, p):
is_scala_2 = ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scala_version.startswith("2.")

args = struct(
buildijar = ctx.attr.build_ijar,
buildijar = ctx.attr.build_ijar and is_scala_2,
)
return _phase_compile_default(ctx, p, args)

Expand Down Expand Up @@ -102,13 +104,11 @@ def phase_compile_common(ctx, p):
return _phase_compile_default(ctx, p)

def _phase_compile_default(ctx, p, _args = struct()):
buildijar_default_value = True if ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scala_version.startswith("2.") else False

return _phase_compile(
ctx,
p,
_args.srcjars if hasattr(_args, "srcjars") else depset(),
_args.buildijar if hasattr(_args, "buildijar") else buildijar_default_value,
not hasattr(_args, "buildijar") or _args.buildijar,
_args.implicit_junit_deps_needed_for_java_compilation if hasattr(_args, "implicit_junit_deps_needed_for_java_compilation") else [],
unused_dependency_checker_ignored_targets = _args.unused_dependency_checker_ignored_targets if hasattr(_args, "unused_dependency_checker_ignored_targets") else [],
)
Expand Down Expand Up @@ -228,12 +228,7 @@ def _compile_or_empty(

# build ijar if needed
if buildijar:
ijar = java_common.run_ijar(
ctx.actions,
jar = ctx.outputs.jar,
target_label = ctx.label,
java_toolchain = specified_java_compile_toolchain(ctx),
)
ijar = _build_ijar(ctx)
else:
# macro code needs to be available at compile-time,
# so set ijar == jar
Expand Down Expand Up @@ -266,6 +261,30 @@ def _compile_or_empty(
merged_provider = merged_provider,
)

def _build_ijar(ctx):
if ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scala_version.startswith("2."):
return java_common.run_ijar(
ctx.actions,
jar = ctx.outputs.jar,
target_label = ctx.label,
java_toolchain = specified_java_compile_toolchain(ctx),
)

output = ctx.actions.declare_file("{}-ijar.jar".format(ctx.label.name))
arguments = ctx.actions.args()
arguments.add(ctx.outputs.jar)
arguments.add(output)

ctx.actions.run(
arguments = [arguments],
executable = ctx.executable._dottyijar,
inputs = [ctx.outputs.jar],
mnemonic = "DottyIjar",
outputs = [output],
)

return output

def _build_nosrc_jar(ctx):
resources = [s + ":" + t for t, s in _resource_paths(ctx.files.resources, ctx.attr.resource_strip_prefix)]

Expand Down
54 changes: 31 additions & 23 deletions scripts/create_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ def select_root_artifacts(scala_version, scala_major, is_scala_3) -> List[str]:

scala_2_version = scala_version
scala_2_major = scala_major
scalatest_major = scala_major
scala_3_major = scala_major

if is_scala_3:
scala_2_version = max_scala_2_version
scala_2_major = max_scala_2_major
scalatest_major = '3'
scala_3_major = '3'

scalafmt_version = SCALAFMT_VERSION
scalapb_version = SCALAPB_VERSION
Expand All @@ -92,29 +92,35 @@ def select_root_artifacts(scala_version, scala_major, is_scala_3) -> List[str]:
scalapb_version = '0.9.8'
protoc_bridge_version = '0.7.14'

if is_scala_3:
specs2_version = '4.20.9'
elif scala_major == '2.11':
specs2_version = '4.10.6'
else:
specs2_version = '4.20.9'

root_artifacts = [
'com.google.api.grpc:proto-google-common-protos:' +
GRPC_COMMON_PROTOS_VERSION,
f'com.google.api.grpc:proto-google-common-protos:{GRPC_COMMON_PROTOS_VERSION}',
f'com.google.guava:guava:{GUAVA_VERSION}',
f'com.google.protobuf:protobuf-java:{PROTOBUF_JAVA_VERSION}',
f'com.thesamet.scalapb:compilerplugin_{scala_2_major}:' +
scalapb_version,
f'com.thesamet.scalapb:protoc-bridge_{scala_2_major}:' +
protoc_bridge_version,
f'com.thesamet.scalapb:scalapb-runtime_{scala_2_major}:' +
scalapb_version,
f'com.thesamet.scalapb:scalapb-runtime-grpc_{scala_2_major}:' +
scalapb_version,
f'org.scala-lang.modules:scala-parser-combinators_{scala_2_major}:' +
PARSER_COMBINATORS_VERSION,
f'com.thesamet.scalapb:compilerplugin_{scala_2_major}:{scalapb_version}',
f'com.thesamet.scalapb:protoc-bridge_{scala_2_major}:{protoc_bridge_version}',
f'com.thesamet.scalapb:scalapb-runtime-grpc_{scala_2_major}:{scalapb_version}',
f'com.thesamet.scalapb:scalapb-runtime_{scala_2_major}:{scalapb_version}',
'commons-io:commons-io:2.18.0',
f'org.scala-lang.modules:scala-parser-combinators_{scala_2_major}:{PARSER_COMBINATORS_VERSION}',
f'org.scala-lang:scala-compiler:{scala_2_version}',
f'org.scala-lang:scala-library:{scala_2_version}',
f'org.scala-lang:scala-reflect:{scala_2_version}',
f'org.scala-lang:scalap:{scala_2_version}',
f'org.scalameta:scalafmt-core_{scala_2_major}:{scalafmt_version}',
f'org.scalatest:scalatest_{scalatest_major}:{SCALATEST_VERSION}',
f'org.typelevel:kind-projector_{scala_2_version}:' +
KIND_PROJECTOR_VERSION,
f'org.scalatest:scalatest_{scala_3_major}:{SCALATEST_VERSION}',
f'org.specs2:specs2-common_{scala_3_major}:{specs2_version}',
f'org.specs2:specs2-core_{scala_3_major}:{specs2_version}',
f'org.specs2:specs2-fp_{scala_3_major}:{specs2_version}',
f'org.specs2:specs2-junit_{scala_3_major}:{specs2_version}',
f'org.specs2:specs2-matcher_{scala_3_major}:{specs2_version}',
f'org.typelevel:kind-projector_{scala_2_version}:{KIND_PROJECTOR_VERSION}',
] + [f'io.grpc:grpc-{lib}:{GRPC_VERSION}' for lib in GRPC_LIBS]

if scala_version == max_scala_2_version or is_scala_3:
Expand All @@ -123,16 +129,18 @@ def select_root_artifacts(scala_version, scala_major, is_scala_3) -> List[str]:

if is_scala_3:
root_artifacts.extend([
f'org.scala-lang:scala3-library_3:{scala_version}',
'com.softwaremill.common:tagging_3:2.3.5',
'dev.zio:izumi-reflect_3:2.3.10',
f'org.jline:jline-reader:{JLINE_VERSION}',
f'org.jline:jline-terminal:{JLINE_VERSION}',
f'org.jline:jline-terminal-jna:{JLINE_VERSION}',
f'org.scala-lang:scala3-compiler_3:{scala_version}',
f'org.scala-lang:scala3-library_3:{scala_version}',
f'org.scala-lang:scala3-interfaces:{scala_version}',
f'org.scala-lang:scala3-tasty-inspector_3:{scala_version}',
f'org.scala-lang:tasty-core_3:{scala_version}',
'org.scala-sbt:compiler-interface:' +
SBT_COMPILER_INTERFACE_VERSION,
f'org.scala-sbt:compiler-interface:{SBT_COMPILER_INTERFACE_VERSION}',
f'org.scala-sbt:util-interface:{SBT_UTIL_INTERFACE_VERSION}',
f'org.jline:jline-reader:{JLINE_VERSION}',
f'org.jline:jline-terminal:{JLINE_VERSION}',
f'org.jline:jline-terminal-jna:{JLINE_VERSION}',
])

else:
Expand Down
42 changes: 42 additions & 0 deletions src/scala/io/bazel/rules_scala/dottyijar/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
load("//scala:scala.bzl", "scala_library_for_plugin_bootstrapping", "scala_specs2_junit_test")
load("@rules_java//java:defs.bzl", "java_binary")

scala_library_for_plugin_bootstrapping(
name = "dottyijar-library",
srcs = glob(
["*.scala"],
exclude = ["*.spec.scala"],
),
scala_version = "3.6.2",
visibility = ["//visibility:public"],
deps = [
"//src/scala/io/bazel/rules_scala/dottyijar/tasty",
"//src/scala/io/bazel/rules_scala/dottyijar/tasty/format",
"@com_softwaremill_common_tagging_3_6_2",
"@dev_zio_izumi_reflect_3_6_2",
],
)

# TODO: Eventually, we should make a bootstrapping toolchain so we don't have to create a `*_for_plugin_bootstrapping` equivalent for each Scala rule.
java_binary(
name = "dottyijar",
main_class = "io.bazel.rules_scala.dottyijar.DottyIjar",
visibility = ["//visibility:public"],
runtime_deps = [":dottyijar-library"],
)

scala_specs2_junit_test(
name = "specs",
srcs = glob(["*.spec.scala"]),
scala_version = "3.6.2",
suffixes = ["Spec"],
deps = [
":dottyijar-library",
"//src/scala/io/bazel/rules_scala/dottyijar/tasty:test",
"@io_bazel_rules_scala_org_specs2_specs2_common_3_6_2",
"@io_bazel_rules_scala_org_specs2_specs2_core_3_6_2",
"@io_bazel_rules_scala_org_specs2_specs2_junit",
"@io_bazel_rules_scala_org_specs2_specs2_matcher_3_6_2",
"@io_bazel_rules_scala_scala_tasty_inspector_3_6_2",
],
)
Loading