Skip to content
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

Don't rely on SCALA_VERSION in phases #1559

Merged
merged 2 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
18 changes: 18 additions & 0 deletions cross-compilation-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,21 @@ config_setting(
```
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.


## 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
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
3 changes: 3 additions & 0 deletions scala/scala_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ load(
"ENABLE_COMPILER_DEPENDENCY_TRACKING",
"SCALA_MAJOR_VERSION",
)
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")

def _compute_strict_deps_mode(input_strict_deps_mode, dependency_mode):
if dependency_mode == "direct":
Expand Down Expand Up @@ -101,6 +102,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[BuildSettingInfo].value,
)
return [toolchain]

Expand Down Expand Up @@ -173,6 +175,7 @@ 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.label(default = "@io_bazel_rules_scala_config//:scala_version"),
},
fragments = ["java"],
)