Skip to content

Commit

Permalink
Don't rely on SCALA_VERSION in phases
Browse files Browse the repository at this point in the history
Use a new field of toolchain, `scala_version`, instead.

Co-authored-by: mkuta <mkuta@virtuslab.com>
  • Loading branch information
aszady and mateuszkuta256 committed Mar 25, 2024
1 parent 8f255cd commit 7ff0262
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
18 changes: 18 additions & 0 deletions cross-compilation-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 behaviour
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
5 changes: 2 additions & 3 deletions scala/private/phases/phase_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions scala/private/phases/phase_semanticdb.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions scala/scala_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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"],
)

0 comments on commit 7ff0262

Please sign in to comment.