diff --git a/cross-compilation-doc.md b/cross-compilation-doc.md index 0b2981bd8e..004c927141 100644 --- a/cross-compilation-doc.md +++ b/cross-compilation-doc.md @@ -8,3 +8,21 @@ 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"]`. + + +## Version-dependent behavior +Don't rely on `SCALA_VERSION` as it represents the default Scala version, not necessarily the one that is currently requested. + +If you need to customize the behavior for specific Scala version, there are two scenarios. + +### From toolchain +If you have an access to the Scala toolchain (`@io_bazel_rules_scala//scala:toolchain_type`), there is `scala_version` field provided in there: +```starlark +def _rule_impl(ctx): + ... + ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scala_version + ... +``` + +### From config setting +TODO \ No newline at end of file diff --git a/scala/private/phases/phase_compile.bzl b/scala/private/phases/phase_compile.bzl index 61e8151cb0..d1534aabbb 100644 --- a/scala/private/phases/phase_compile.bzl +++ b/scala/private/phases/phase_compile.bzl @@ -17,9 +17,6 @@ load( _compile_scala = "compile_scala", ) load(":resources.bzl", _resource_paths = "paths") -load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION") - -buildijar_default_value = True if SCALA_VERSION.startswith("2.") else False def phase_compile_binary(ctx, p): args = struct( @@ -105,6 +102,8 @@ def phase_compile_common(ctx, p): return _phase_compile_default(ctx, p) def _phase_compile_default(ctx, p, _args = struct()): + buildijar_default_value = True if ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scala_version.startswith("2.") else False + return _phase_compile( ctx, p, diff --git a/scala/private/phases/phase_semanticdb.bzl b/scala/private/phases/phase_semanticdb.bzl index 15bb70e6ef..1904576363 100644 --- a/scala/private/phases/phase_semanticdb.bzl +++ b/scala/private/phases/phase_semanticdb.bzl @@ -3,7 +3,6 @@ load( "find_deps_info_on", ) load("@io_bazel_rules_scala//scala:semanticdb_provider.bzl", "SemanticdbInfo") -load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_MAJOR_VERSION") load("@bazel_skylib//lib:paths.bzl", "paths") def phase_semanticdb(ctx, p): @@ -37,7 +36,7 @@ def phase_semanticdb(ctx, p): outputfilename = "%s/%s/%s.semanticdb" % (semanticdb_intpath, semanticdb_outpath, currSrc.path) output_files.append(ctx.actions.declare_file(outputfilename)) - if SCALA_MAJOR_VERSION.startswith("2"): + if toolchain.scala_version.startswith("2"): semanticdb_deps = find_deps_info_on(ctx, toolchain_type_label, "semanticdb").deps if len(semanticdb_deps) == 0: diff --git a/scala/scala_toolchain.bzl b/scala/scala_toolchain.bzl index 2b7accd285..9f59a5455a 100644 --- a/scala/scala_toolchain.bzl +++ b/scala/scala_toolchain.bzl @@ -6,6 +6,8 @@ load( "@io_bazel_rules_scala_config//:config.bzl", "ENABLE_COMPILER_DEPENDENCY_TRACKING", "SCALA_MAJOR_VERSION", + "SCALA_VERSION", + "SCALA_VERSIONS", ) def _compute_strict_deps_mode(input_strict_deps_mode, dependency_mode): @@ -101,6 +103,7 @@ def _scala_toolchain_impl(ctx): enable_semanticdb = ctx.attr.enable_semanticdb, semanticdb_bundle_in_jar = ctx.attr.semanticdb_bundle_in_jar, use_argument_file_in_runner = ctx.attr.use_argument_file_in_runner, + scala_version = ctx.attr.scala_version, ) return [toolchain] @@ -173,6 +176,12 @@ scala_toolchain = rule( default = False, doc = "Changes java binaries scripts (including tests) to use argument files and not classpath jars to improve performance, requires java > 8", ), + "scala_version": attr.string( + default = SCALA_VERSION, + values = SCALA_VERSIONS, + doc = "The Scala version used by the toolchain. Must be consistent with provided `dep_providers`." + + "If default `dep_providers` are used, you need to use default value here as well.", + ), }, fragments = ["java"], )