Skip to content

Commit ffbfe7f

Browse files
Introduce build setting for current Scala version
Co-authored-by: mkuta <[email protected]>
1 parent 8f255cd commit ffbfe7f

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

cross-compilation-doc.md

Lines changed: 27 additions & 0 deletions
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_flag(
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 = "3_3_1",
33+
flag_values = {":scala_version": "3.3.1"},
34+
)
35+
```
36+
The `name` of `config_setting` corresponds to `sanitize_version(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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ 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(".", "_")

scala_config.bzl

Lines changed: 24 additions & 2 deletions
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", "sanitize_version")
22

33
def _default_scala_version():
44
"""return the scala version for use in maven coordinates"""
@@ -10,6 +10,17 @@ 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 "\n".join([
15+
'config_setting(',
16+
' name = "{name}",',
17+
' flag_values = {{":scala_version": "{version}"}},',
18+
')\n',
19+
]).format(name = sanitize_version(scala_version), version = scala_version)
20+
21+
def _config_settings(scala_versions):
22+
return "".join([_config_setting(v) for v in scala_versions])
23+
1324
def _store_config(repository_ctx):
1425
# Default version
1526
scala_version = repository_ctx.os.environ.get(
@@ -39,8 +50,19 @@ def _store_config(repository_ctx):
3950
"ENABLE_COMPILER_DEPENDENCY_TRACKING=" + enable_compiler_dependency_tracking,
4051
])
4152

53+
build_file_content = "\n".join([
54+
'load("@bazel_skylib//rules:common_settings.bzl", "string_flag")',
55+
'string_flag(',
56+
' name = "scala_version",',
57+
' build_setting_default = "{scala_version}",',
58+
' values = {scala_versions},',
59+
' visibility = ["//visibility:public"],',
60+
')\n',
61+
]).format(scala_versions = scala_versions, scala_version=scala_version) +\
62+
_config_settings(scala_versions)
63+
4264
repository_ctx.file("config.bzl", config_file_content)
43-
repository_ctx.file("BUILD")
65+
repository_ctx.file("BUILD", build_file_content)
4466

4567
_config_repository = repository_rule(
4668
implementation = _store_config,

0 commit comments

Comments
 (0)