Skip to content

Commit

Permalink
Declare scala version in a separate repository
Browse files Browse the repository at this point in the history
Similar to what rules_java does with repote_java_repository

Generated repository compiles scalac worker and exposes it as a toolchain
together with scala dependencies

Toolchains could be constrained with config_setting

Pass simple case bazel build //test:HelloLib

Could be part of scala_config which could take a list of structs to generate repositories

Probably could be mapped in bzlmod with extensions and tag_class
  • Loading branch information
simuons committed Feb 15, 2024
1 parent 27bcfab commit 919471a
Show file tree
Hide file tree
Showing 13 changed files with 230 additions and 24 deletions.
22 changes: 20 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ rules_scala_setup()

rules_scala_toolchain_deps_repositories(fetch_sources = True)

load("//scala/runtime:repository.bzl", "scala_runtime")

scala_runtime(
name = "scala_toolchain_2.12.18",
compiler = "@io_bazel_rules_scala_scala_compiler",
library = [
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
],
version = "2.12.18",
)

register_toolchains("@scala_toolchain_2.12.18//:all")

load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_toolchains")

scala_register_toolchains()

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")

# Declares @com_google_protobuf//:protoc pointing to released binary
Expand Down Expand Up @@ -97,9 +115,9 @@ local_repository(
path = "third_party/test/example_external_workspace",
)

load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_unused_deps_toolchains")
#load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_unused_deps_toolchains")

scala_register_unused_deps_toolchains()
#scala_register_unused_deps_toolchains()

register_toolchains("@io_bazel_rules_scala//test/proto:scalapb_toolchain")

Expand Down
6 changes: 0 additions & 6 deletions scala/private/common_attributes.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ implicit_deps = {
"_java_runtime": attr.label(
default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
),
"_scalac": attr.label(
executable = True,
cfg = "exec",
default = Label("@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac"),
allow_files = True,
),
"_exe": attr.label(
executable = True,
cfg = "exec",
Expand Down
2 changes: 1 addition & 1 deletion scala/private/phases/phase_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def _compile_or_empty(
ctx.attr.expect_java_output,
ctx.attr.scalac_jvm_flags,
scalacopts,
ctx.executable._scalac,
ctx.toolchains["@io_bazel_rules_scala//scala/runtime:toolchain_type"].compiler,
dependency_info,
unused_dependency_checker_ignored_targets,
additional_outputs,
Expand Down
12 changes: 4 additions & 8 deletions scala/private/phases/phase_scalac_provider.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@ load(
"@io_bazel_rules_scala//scala:providers.bzl",
_ScalacProvider = "ScalacProvider",
)
load(
"@io_bazel_rules_scala//scala/private/toolchain_deps:toolchain_deps.bzl",
"find_deps_info_on",
)

def phase_scalac_provider(ctx, p):
toolchain_type_label = "@io_bazel_rules_scala//scala:toolchain_type"
tc = ctx.toolchains["@io_bazel_rules_scala//scala/runtime:toolchain_type"]

library_classpath = find_deps_info_on(ctx, toolchain_type_label, "scala_library_classpath").deps
compile_classpath = find_deps_info_on(ctx, toolchain_type_label, "scala_compile_classpath").deps
macro_classpath = find_deps_info_on(ctx, toolchain_type_label, "scala_macro_classpath").deps
library_classpath = tc.library
compile_classpath = []
macro_classpath = tc.library

return _ScalacProvider(
default_classpath = library_classpath,
Expand Down
9 changes: 3 additions & 6 deletions scala/private/rules/scala_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def make_scala_library(*extras):
*[extra["outputs"] for extra in extras if "outputs" in extra]
),
toolchains = [
"@io_bazel_rules_scala//scala/runtime:toolchain_type",
"@io_bazel_rules_scala//scala:toolchain_type",
"@bazel_tools//tools/jdk:toolchain_type",
],
Expand Down Expand Up @@ -169,12 +170,6 @@ _scala_library_for_plugin_bootstrapping_attrs.update(implicit_deps)
# which does not contain plugin related attributes, and thus avoids the cyclic dependency issue
_scala_library_for_plugin_bootstrapping_attrs.update({
"build_ijar": attr.bool(default = True),
"_scalac": attr.label(
executable = True,
cfg = "exec",
default = Label("@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac:scalac_bootstrap"),
allow_files = True,
),
})

_scala_library_for_plugin_bootstrapping_attrs.update(_library_attrs)
Expand All @@ -198,6 +193,7 @@ def make_scala_library_for_plugin_bootstrapping(*extras):
*[extra["outputs"] for extra in extras if "outputs" in extra]
),
toolchains = [
"@io_bazel_rules_scala//scala/runtime:toolchain_type",
"@io_bazel_rules_scala//scala:toolchain_type",
"@bazel_tools//tools/jdk:toolchain_type",
],
Expand Down Expand Up @@ -270,6 +266,7 @@ def make_scala_macro_library(*extras):
*[extra["outputs"] for extra in extras if "outputs" in extra]
),
toolchains = [
"@io_bazel_rules_scala//scala/runtime:toolchain_type",
"@io_bazel_rules_scala//scala:toolchain_type",
"@bazel_tools//tools/jdk:toolchain_type",
],
Expand Down
4 changes: 4 additions & 0 deletions scala/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
toolchain_type(
name = "toolchain_type",
visibility = ["//visibility:public"],
)
76 changes: 76 additions & 0 deletions scala/runtime/bootstrap.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
load("@rules_java//java:defs.bzl", "java_binary", "java_library")
load("//scala:scala_cross_version.bzl", "extract_major_version", "extract_minor_version")

def worker(name, scala_version, classpath):
scala_major_version = extract_major_version(scala_version)
scala_minor_version = extract_minor_version(scala_version)

java_library(
name = "%s_reporter" % name,
srcs = _reporter_srcs(scala_major_version, scala_minor_version),
deps = classpath + [
"@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 = name,
srcs = _scalac_srcs(scala_major_version),
deps = classpath + [
":%s_reporter" % name,
"@bazel_tools//src/main/protobuf:worker_protocol_java_proto",
"@io_bazel_rules_scala//src/java/io/bazel/rulesscala/io_utils",
"@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",
],
main_class = "io.bazel.rulesscala.scalac.ScalacWorker",
)

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/deps_tracking_reporter:after_2_13_12", # ???
"@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac/reporter:scala_3",
]

def _scalac_srcs(scala_major_version):
if scala_major_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"],
)
41 changes: 41 additions & 0 deletions scala/runtime/repository.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
def _scala_runtime(repository_ctx):
build = """
load("@io_bazel_rules_scala//scala/runtime:bootstrap.bzl", "worker")
worker("scalac", "{version}", ["{compiler}", {library}])
load("@io_bazel_rules_scala//scala/runtime:toolchain.bzl", "runtime")
runtime(
name = "_runtime",
version = "{version}",
compiler = ":scalac",
library = [{library}],
visibility = ["//visibility:public"],
)
toolchain(
name = "runtime",
toolchain = ":_runtime",
toolchain_type = "@io_bazel_rules_scala//scala/runtime:toolchain_type",
visibility = ["//visibility:public"],
)
""".format(
version = repository_ctx.attr.version,
compiler = str(repository_ctx.attr.compiler),
library = _comma_separated_strings(repository_ctx.attr.library),
)

repository_ctx.file("BUILD", build)

scala_runtime = repository_rule(
_scala_runtime,
attrs = {
"version": attr.string(),
"compiler": attr.label(providers = [JavaInfo]),
"library": attr.label_list(providers = [JavaInfo]),
},
)

def _comma_separated_strings(values):
return ", ".join(["\"%s\"" % v for v in values])
15 changes: 15 additions & 0 deletions scala/runtime/toolchain.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def _runtime(ctx):
return platform_common.ToolchainInfo(
compiler = ctx.attr.compiler.files_to_run,
library = ctx.attr.library,
version = ctx.attr.version,
)

runtime = rule(
_runtime,
attrs = {
"compiler": attr.label(executable = True, cfg = "exec", allow_files = True),
"library": attr.label_list(providers = [JavaInfo]),
"version": attr.string(),
},
)
27 changes: 27 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,30 @@ filegroup(
),
visibility = ["//visibility:public"],
)

# filegroup or export_files doesn't work because java_binary emits warning about misplaced sources
# packaging sources to srcjar does solve this issue
load("//scala/runtime:bootstrap.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 = [
"ReportableMainClass.java",
"ScalacInvoker.java",
"ScalacInvoker3.java",
],
outs = "scalac_3.srcjar",
visibility = ["//visibility:public"],
)
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("//scala/runtime:bootstrap.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("//scala/runtime:bootstrap.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"],
)

0 comments on commit 919471a

Please sign in to comment.