From b9153897fc4a326da5222b28516751430327c11c Mon Sep 17 00:00:00 2001 From: mkuta Date: Tue, 26 Mar 2024 16:33:16 +0100 Subject: [PATCH 1/3] Add `sanitize_version` utility --- scala/scala_cross_version.bzl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scala/scala_cross_version.bzl b/scala/scala_cross_version.bzl index 24bd30647..85d1953c7 100644 --- a/scala/scala_cross_version.bzl +++ b/scala/scala_cross_version.bzl @@ -44,3 +44,7 @@ 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(".", "_") From a779ad00989ef653e14514b7b9a3e2f9a5160b98 Mon Sep 17 00:00:00 2001 From: mkuta Date: Tue, 26 Mar 2024 16:41:15 +0100 Subject: [PATCH 2/3] Add `version_suffix` utility --- scala/scala_cross_version.bzl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scala/scala_cross_version.bzl b/scala/scala_cross_version.bzl index 85d1953c7..a86046b53 100644 --- a/scala/scala_cross_version.bzl +++ b/scala/scala_cross_version.bzl @@ -48,3 +48,6 @@ def scala_mvn_artifact( 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) From 588f4b3683f11057d21e8c5d55ea133d87796021 Mon Sep 17 00:00:00 2001 From: Adam Szady Date: Fri, 22 Mar 2024 11:12:31 +0100 Subject: [PATCH 3/3] Introduce build setting for current Scala version Co-authored-by: mkuta --- cross-compilation-doc.md | 27 +++++++++++++++++++++++++++ scala_config.bzl | 24 ++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/cross-compilation-doc.md b/cross-compilation-doc.md index 0b2981bd8..b8b06ee32 100644 --- a/cross-compilation-doc.md +++ b/cross-compilation-doc.md @@ -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. diff --git a/scala_config.bzl b/scala_config.bzl index 4c30524bb..b0d7b288f 100644 --- a/scala_config.bzl +++ b/scala_config.bzl @@ -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""" @@ -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( @@ -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,