Skip to content

Commit

Permalink
toolchain scalac
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkuta256 committed Feb 29, 2024
1 parent 2688885 commit 4622b97
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 5 deletions.
2 changes: 2 additions & 0 deletions scala/private/common_attributes.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ load(
"@io_bazel_rules_scala//scala:plusone.bzl",
_collect_plus_one_deps_aspect = "collect_plus_one_deps_aspect",
)
load("//scala/versions:versions.bzl", "sanitize_version")
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS")

common_attrs_for_plugin_bootstrapping = {
"srcs": attr.label_list(allow_files = [
Expand Down
2 changes: 1 addition & 1 deletion scala/private/macros/setup_scala_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ def setup_scala_toolchain(

scala_toolchain(
name = "%s_impl" % name,
scala_version = scala_version,
dep_providers = dep_providers,
enable_semanticdb = enable_semanticdb,
visibility = visibility,
scalac = Label("@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac:scalac_" + sanitize_version(scala_version)) if scala_version else None,
**kwargs
)

Expand Down
10 changes: 9 additions & 1 deletion scala/private/phases/phase_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ load(
)
load(":resources.bzl", _resource_paths = "paths")
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
load("@io_bazel_rules_scala//scala:scala_cross_version.bzl", "extract_major_version", "extract_minor_version")
load("//scala/versions:versions.bzl", "sanitize_version")

buildijar_default_value = True if SCALA_VERSION.startswith("2.") else False

Expand Down Expand Up @@ -221,7 +223,7 @@ def _compile_or_empty(
ctx.attr.expect_java_output,
ctx.attr.scalac_jvm_flags,
scalacopts,
ctx.executable._scalac,
_select_scalac(ctx),
dependency_info,
unused_dependency_checker_ignored_targets,
additional_outputs,
Expand Down Expand Up @@ -390,3 +392,9 @@ def _interim_java_provider_for_java_compilation(scala_output):
compile_jar = scala_output,
neverlink = True,
)

def _select_scalac(ctx):
scalac = ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scalac
if (scalac):
return scalac.files_to_run
return ctx.attr._scalac
6 changes: 4 additions & 2 deletions scala/scala_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def _scala_toolchain_impl(ctx):
enable_semanticdb = ctx.attr.enable_semanticdb,
semanticdb_bundle_in_jar = ctx.attr.semanticdb_bundle_in_jar,
use_argument_file_in_runner = ctx.attr.use_argument_file_in_runner,
scala_version = ctx.attr.scala_version,
scalac = ctx.attr.scalac,
)
return [toolchain]

Expand Down Expand Up @@ -174,7 +174,9 @@ scala_toolchain = rule(
default = False,
doc = "Changes java binaries scripts (including tests) to use argument files and not classpath jars to improve performance, requires java > 8",
),
"scala_version": attr.string(),
"scalac": attr.label(
allow_files = True,
),
},
fragments = ["java"],
)
29 changes: 29 additions & 0 deletions src/java/io/bazel/rulesscala/scalac/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,32 @@ filegroup(
),
visibility = ["//visibility:public"],
)

load("//src/java/io/bazel/rulesscala/scalac:scalac.bzl", "source_jar")

source_jar(
name = "scalac_2",
srcs = [
"ReportableMainClass.java",
"ScalacInvoker.java",
"ScalacInvokerResults.java",
"ScalacWorker.java",
],
outs = "scalac_2.srcjar",
visibility = ["//visibility:public"],
)

source_jar(
name = "scalac_3",
srcs = [
"ScalacInvoker3.java",
"ScalacInvokerResults.java",
"ScalacWorker.java",
],
outs = "scalac_3.srcjar",
visibility = ["//visibility:public"],
)

load("//src/java/io/bazel/rulesscala/scalac:scalac.bzl", "setup_scalac")

setup_scalac()
1 change: 0 additions & 1 deletion src/java/io/bazel/rulesscala/scalac/compileoptions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ java_library(
name = "compileoptions",
srcs = ["CompileOptions.java"],
visibility = ["//visibility:public"],
deps = ["//scala/private/toolchain_deps:scala_compile_classpath"],
)

java_test(
Expand Down
16 changes: 16 additions & 0 deletions src/java/io/bazel/rulesscala/scalac/deps_tracking_reporter/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,19 @@ filegroup(
],
visibility = ["//visibility:public"],
)

load("//src/java/io/bazel/rulesscala/scalac:scalac.bzl", "source_jar")

[
source_jar(
name = d,
srcs = glob(["%s/*.java" % d]),
outs = "%s.srcjar" % d,
visibility = ["//visibility:public"],
)
for d in [
"after_2_12_13_and_before_2_13_12",
"after_2_13_12",
"before_2_12_13",
]
]
23 changes: 23 additions & 0 deletions src/java/io/bazel/rulesscala/scalac/reporter/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,26 @@ java_proto_library(
visibility = ["//visibility:public"],
deps = [":scala_deps_proto"],
)

load("//src/java/io/bazel/rulesscala/scalac:scalac.bzl", "source_jar")

[
source_jar(
name = d,
srcs = glob(["%s/*.java" % d]),
outs = "%s.srcjar" % d,
visibility = ["//visibility:public"],
)
for d in [
"after_2_12_13_and_before_2_13_12",
"after_2_13_12",
"before_2_12_13",
]
]

source_jar(
name = "scala_3",
srcs = ["PlaceholderForEmptyScala3Lib.java"],
outs = "scala_3.srcjar",
visibility = ["//visibility:public"],
)
105 changes: 105 additions & 0 deletions src/java/io/bazel/rulesscala/scalac/scalac.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
load("@rules_java//java:defs.bzl", "java_binary", "java_library")
load("//scala/versions:versions.bzl", "sanitize_version")
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION", "SCALA_VERSIONS")
load("@io_bazel_rules_scala//scala:scala_cross_version.bzl", "extract_major_version", "extract_minor_version")

_SCALA_COMPILE_CLASSPATH_DEPS = {
"2": [
"@io_bazel_rules_scala_scala_compiler",
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
],
"3": [
"@io_bazel_rules_scala_scala_compiler",
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_interfaces",
"@io_bazel_rules_scala_scala_tasty_core",
"@io_bazel_rules_scala_scala_asm",
"@io_bazel_rules_scala_scala_library_2",
],
}

def setup_scalac():
for scala_version in SCALA_VERSIONS:
_scalac(scala_version)

def _scalac(scala_version):
compiler_deps = _compiler_deps(scala_version)
version_suffix = "_" + sanitize_version(scala_version)

java_library(
name = "scalac_reporter" + version_suffix,
srcs = _reporter_srcs(extract_major_version(scala_version), extract_minor_version(scala_version)),
deps = compiler_deps + [
"@io_bazel_rules_scala//src/protobuf/io/bazel/rules_scala:diagnostics_java_proto",
"@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac/compileoptions",
"@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac/reporter:scala_deps_java_proto",
],
)
java_binary(
name = "scalac" + version_suffix,
srcs = _scalac_srcs(scala_version),
main_class = "io.bazel.rulesscala.scalac.ScalacWorker",
visibility = ["//visibility:public"],
javacopts = [
"-source 1.8",
"-target 1.8",
],
deps = compiler_deps + [
":scalac_reporter" + version_suffix,
"@io_bazel_rules_scala//src/java/io/bazel/rulesscala/io_utils",
"@bazel_tools//src/main/protobuf:worker_protocol_java_proto",
"@io_bazel_rules_scala//src/java/io/bazel/rulesscala/jar",
"@io_bazel_rules_scala//src/java/io/bazel/rulesscala/worker",
"@io_bazel_rules_scala//src/protobuf/io/bazel/rules_scala:diagnostics_java_proto",
"@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac/compileoptions",
],
)

def _compiler_deps(scala_version):
suffix = "_" + sanitize_version(scala_version)
return [dep + suffix for dep in _SCALA_COMPILE_CLASSPATH_DEPS[scala_version[0:1]]]

def _reporter_srcs(scala_major_version, scala_minor_version):
if (scala_major_version == "2.11") or (scala_major_version == "2.12" and int(scala_minor_version) < 13):
return [
"@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac/deps_tracking_reporter:before_2_12_13",
"@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac/reporter:before_2_12_13",
]
elif (scala_major_version == "2.12" and int(scala_minor_version) >= 13) or (scala_major_version == "2.13" and int(scala_minor_version) < 12):
return [
"@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac/deps_tracking_reporter:after_2_12_13_and_before_2_13_12",
"@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac/reporter:after_2_12_13_and_before_2_13_12",
]
elif (scala_major_version == "2.13" and int(scala_minor_version) >= 12):
return [
"@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac/deps_tracking_reporter:after_2_13_12",
"@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac/reporter:after_2_13_12",
]
else:
return [
"@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac/reporter:scala_3",
]

def _scalac_srcs(scala_version):
if scala_version.startswith("2"):
return ["@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac:scalac_2"]
else:
return ["@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac:scalac_3"]

def _source_jar(ctx):
java_common.pack_sources(
ctx.actions,
sources = ctx.files.srcs,
output_source_jar = ctx.outputs.outs,
java_toolchain = ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"].java,
)

source_jar = rule(
implementation = _source_jar,
attrs = {
"srcs": attr.label_list(mandatory = True, allow_empty = False, allow_files = True),
"outs": attr.output(mandatory = True),
},
toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
)

0 comments on commit 4622b97

Please sign in to comment.