Skip to content

Commit 914e4ee

Browse files
Bind toolchains to Scala version (#1566)
Use `target_settings` to indicate which Scala version is supported by the toolchain. This will become useful when multiple toolchains for each Scala version are defined. The choice of toolchains to cover by this (first) migration is quite arbitrary, but should be enough to enable the basic cross-build use cases. Co-authored-by: mkuta <[email protected]>
1 parent 800cd82 commit 914e4ee

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

cross-compilation-doc.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,32 @@ def _rule_impl(ctx):
5252
```
5353

5454
### From config setting
55-
TODO
55+
TODO
56+
57+
58+
## Toolchains
59+
Standard [toolchain resolution](https://bazel.build/extending/toolchains#toolchain-resolution) procedure determines which toolchain to use for Scala targets.
60+
61+
Toolchain should declare its compatibility with Scala version by using `target_settings` attribute of the `toolchain` rule:
62+
63+
```starlark
64+
toolchain(
65+
...
66+
target_settings = ["@io_bazel_rules_scala_config//:scala_version_3_3_1"],
67+
...
68+
)
69+
```
70+
71+
### Cross-build support tiers
72+
`rules_scala` consists of many toolchains implementing various toolchain types.
73+
Their support level for cross-build setup varies.
74+
75+
We can distinguish following tiers:
76+
77+
* No `target_settings` set – not migrated, will work on the default `SCALA_VERSION`; undefined behavior on other versions.
78+
* (all toolchains not mentioned elsewhere)
79+
* `target_settings` set to the `SCALA_VERSION` – not fully migrated; will work only on the default `SCALA_VERSION` and will fail the toolchain resolution on other versions.
80+
* [the main Scala toolchain](scala/BUILD)
81+
* [Scalafmt](scala/scalafmt/BUILD)
82+
* [Scalatest](testing/testing.bzl)
83+
* Multiple toolchain instances with `target_settings` corresponding to each of `SCALA_VERSIONS` – fully migrated; will work in cross-build setup.

scala/private/macros/setup_scala_toolchain.bzl

+4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
load("//scala:scala_toolchain.bzl", "scala_toolchain")
22
load("//scala:providers.bzl", "declare_deps_provider")
3+
load("//scala:scala_cross_version.bzl", "version_suffix")
4+
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
35

46
def setup_scala_toolchain(
57
name,
68
scala_compile_classpath,
79
scala_library_classpath,
810
scala_macro_classpath,
11+
scala_version = SCALA_VERSION,
912
scala_xml_deps = None,
1013
parser_combinators_deps = None,
1114
semanticdb_deps = None,
@@ -93,5 +96,6 @@ def setup_scala_toolchain(
9396
name = name,
9497
toolchain = ":%s_impl" % name,
9598
toolchain_type = "@io_bazel_rules_scala//scala:toolchain_type",
99+
target_settings = ["@io_bazel_rules_scala_config//:scala_version" + version_suffix(scala_version)],
96100
visibility = visibility,
97101
)

scala/scalafmt/BUILD

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
load("//scala:scala.bzl", "scala_binary")
2+
load("//scala:scala_cross_version.bzl", "version_suffix")
23
load("//scala/scalafmt/toolchain:toolchain.bzl", "export_scalafmt_deps", "scalafmt_toolchain")
34
load("@io_bazel_rules_scala//scala:providers.bzl", "declare_deps_provider")
5+
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
46
load(
57
"//scala/scalafmt:phase_scalafmt_ext.bzl",
68
"scalafmt_singleton",
@@ -58,6 +60,7 @@ scalafmt_toolchain(
5860

5961
toolchain(
6062
name = "scalafmt_toolchain",
63+
target_settings = ["@io_bazel_rules_scala_config//:scala_version" + version_suffix(SCALA_VERSION)],
6164
toolchain = ":scalafmt_toolchain_impl",
6265
toolchain_type = "//scala/scalafmt/toolchain:scalafmt_toolchain_type",
6366
visibility = ["//visibility:public"],

testing/testing.bzl

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
load("@io_bazel_rules_scala//scala:providers.bzl", "declare_deps_provider")
22
load("@io_bazel_rules_scala//testing/toolchain:toolchain.bzl", "scala_testing_toolchain")
3+
load("//scala:scala_cross_version.bzl", "version_suffix")
4+
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
35

46
def _declare_deps_provider(macro_name, deps_id, deps, visibility):
57
label = "%s_%s_provider" % (macro_name, deps_id)
@@ -13,6 +15,7 @@ def _declare_deps_provider(macro_name, deps_id, deps, visibility):
1315

1416
def setup_scala_testing_toolchain(
1517
name,
18+
scala_version = SCALA_VERSION,
1619
junit_classpath = None,
1720
specs2_classpath = None,
1821
specs2_junit_classpath = None,
@@ -70,5 +73,6 @@ def setup_scala_testing_toolchain(
7073
name = name,
7174
toolchain = ":" + name + "_impl",
7275
toolchain_type = "@io_bazel_rules_scala//testing/toolchain:testing_toolchain_type",
76+
target_settings = ["@io_bazel_rules_scala_config//:scala_version" + version_suffix(scala_version)],
7377
visibility = visibility,
7478
)

0 commit comments

Comments
 (0)