Skip to content

Commit 2df777e

Browse files
Introduce build setting for current Scala version (#1558)
* Introduce build setting for current Scala version Co-authored-by: mkuta <[email protected]> --------- Co-authored-by: mkuta <[email protected]>
1 parent 8f255cd commit 2df777e

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

cross-compilation-doc.md

+27
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,30 @@ The support for cross-compilation is currently under development.
88
File created there, `config.bzl`, consists of many variables. In particular:
99
* `SCALA_VERSION` – representing the default Scala version, e.g. `"3.3.1"`;
1010
* `SCALA_VERSIONS` – representing all configured Scala versions (currently one), e.g. `["3.3.1"]`.
11+
12+
13+
## Build settings
14+
Configured `SCALA_VERSIONS` correspond to allowed values of [build setting](https://bazel.build/extending/config#user-defined-build-setting).
15+
16+
### `scala_version`
17+
`@io_bazel_rules_scala_config` in its root package defines the following build setting:
18+
```starlark
19+
string_setting(
20+
name = "scala_version",
21+
build_setting_default = "3.3.1",
22+
values = ["3.3.1"],
23+
visibility = ["//visibility:public"],
24+
)
25+
```
26+
This build setting can be subject of change by [transitions](https://bazel.build/extending/config#user-defined-transitions) (within allowed `values`).
27+
28+
### Config settings
29+
Then for each Scala version we have a [config setting](https://bazel.build/extending/config#build-settings-and-select):
30+
```starlark
31+
config_setting(
32+
name = "scala_version_3_3_1",
33+
flag_values = {":scala_version": "3.3.1"},
34+
)
35+
```
36+
The `name` of `config_setting` corresponds to `"scala_version" + version_suffix(scala_version)`.
37+
One may use this config setting in `select()` e.g. to provide dependencies relevant to a currently used Scala version.

scala/scala_cross_version.bzl

+7
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,10 @@ def scala_mvn_artifact(
4444
artifactid = gav[1]
4545
version = gav[2]
4646
return "%s:%s_%s:%s" % (groupid, artifactid, major_scala_version, version)
47+
48+
def sanitize_version(scala_version):
49+
""" Makes Scala version usable in target names. """
50+
return scala_version.replace(".", "_")
51+
52+
def version_suffix(scala_version):
53+
return "_" + sanitize_version(scala_version)

scala_config.bzl

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("//scala:scala_cross_version.bzl", "extract_major_version", "extract_minor_version")
1+
load("//scala:scala_cross_version.bzl", "extract_major_version", "extract_minor_version", "version_suffix")
22

33
def _default_scala_version():
44
"""return the scala version for use in maven coordinates"""
@@ -10,6 +10,16 @@ def _validate_supported_scala_version(scala_major_version, scala_minor_version):
1010
if scala_major_version == "2.12" and int(scala_minor_version) < 1:
1111
fail("Scala version must be newer or equal to 2.12.1 to use compiler dependency tracking.")
1212

13+
def _config_setting(scala_version):
14+
return """config_setting(
15+
name = "scala_version{version_suffix}",
16+
flag_values = {{":scala_version": "{version}"}},
17+
)
18+
""".format(version_suffix = version_suffix(scala_version), version = scala_version)
19+
20+
def _config_settings(scala_versions):
21+
return "".join([_config_setting(v) for v in scala_versions])
22+
1323
def _store_config(repository_ctx):
1424
# Default version
1525
scala_version = repository_ctx.os.environ.get(
@@ -39,8 +49,18 @@ def _store_config(repository_ctx):
3949
"ENABLE_COMPILER_DEPENDENCY_TRACKING=" + enable_compiler_dependency_tracking,
4050
])
4151

52+
build_file_content = """load("@bazel_skylib//rules:common_settings.bzl", "string_setting")
53+
string_setting(
54+
name = "scala_version",
55+
build_setting_default = "{scala_version}",
56+
values = {scala_versions},
57+
visibility = ["//visibility:public"],
58+
)
59+
""".format(scala_versions = scala_versions, scala_version = scala_version)
60+
build_file_content += _config_settings(scala_versions)
61+
4262
repository_ctx.file("config.bzl", config_file_content)
43-
repository_ctx.file("BUILD")
63+
repository_ctx.file("BUILD", build_file_content)
4464

4565
_config_repository = repository_rule(
4666
implementation = _store_config,

0 commit comments

Comments
 (0)