forked from bazelbuild/rules_scala
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Declare scala version in a separate repository
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
Showing
13 changed files
with
230 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
toolchain_type( | ||
name = "toolchain_type", | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters