Skip to content

Introduce build setting for current Scala version #1558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions cross-compilation-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,30 @@ The support for cross-compilation is currently under development.
File created there, `config.bzl`, consists of many variables. In particular:
* `SCALA_VERSION` – representing the default Scala version, e.g. `"3.3.1"`;
* `SCALA_VERSIONS` – representing all configured Scala versions (currently one), e.g. `["3.3.1"]`.


## Build settings
Configured `SCALA_VERSIONS` correspond to allowed values of [build setting](https://bazel.build/extending/config#user-defined-build-setting).

### `scala_version`
`@io_bazel_rules_scala_config` in its root package defines the following build setting:
```starlark
string_setting(
name = "scala_version",
build_setting_default = "3.3.1",
values = ["3.3.1"],
visibility = ["//visibility:public"],
)
```
This build setting can be subject of change by [transitions](https://bazel.build/extending/config#user-defined-transitions) (within allowed `values`).

### Config settings
Then for each Scala version we have a [config setting](https://bazel.build/extending/config#build-settings-and-select):
```starlark
config_setting(
name = "scala_version_3_3_1",
flag_values = {":scala_version": "3.3.1"},
)
```
The `name` of `config_setting` corresponds to `"scala_version" + version_suffix(scala_version)`.
One may use this config setting in `select()` e.g. to provide dependencies relevant to a currently used Scala version.
7 changes: 7 additions & 0 deletions scala/scala_cross_version.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ def scala_mvn_artifact(
artifactid = gav[1]
version = gav[2]
return "%s:%s_%s:%s" % (groupid, artifactid, major_scala_version, version)

def sanitize_version(scala_version):
""" Makes Scala version usable in target names. """
return scala_version.replace(".", "_")

def version_suffix(scala_version):
return "_" + sanitize_version(scala_version)
24 changes: 22 additions & 2 deletions scala_config.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//scala:scala_cross_version.bzl", "extract_major_version", "extract_minor_version")
load("//scala:scala_cross_version.bzl", "extract_major_version", "extract_minor_version", "version_suffix")

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

def _config_setting(scala_version):
return """config_setting(
name = "scala_version{version_suffix}",
flag_values = {{":scala_version": "{version}"}},
)
""".format(version_suffix = version_suffix(scala_version), version = scala_version)

def _config_settings(scala_versions):
return "".join([_config_setting(v) for v in scala_versions])

def _store_config(repository_ctx):
# Default version
scala_version = repository_ctx.os.environ.get(
Expand Down Expand Up @@ -39,8 +49,18 @@ def _store_config(repository_ctx):
"ENABLE_COMPILER_DEPENDENCY_TRACKING=" + enable_compiler_dependency_tracking,
])

build_file_content = """load("@bazel_skylib//rules:common_settings.bzl", "string_setting")
string_setting(
name = "scala_version",
build_setting_default = "{scala_version}",
values = {scala_versions},
visibility = ["//visibility:public"],
)
""".format(scala_versions = scala_versions, scala_version = scala_version)
build_file_content += _config_settings(scala_versions)

repository_ctx.file("config.bzl", config_file_content)
repository_ctx.file("BUILD")
repository_ctx.file("BUILD", build_file_content)

_config_repository = repository_rule(
implementation = _store_config,
Expand Down