Skip to content

Commit 579edc1

Browse files
committed
Merge remote-tracking branch 'upstream/master' into bzlmod_support_part1
# Conflicts: # .gitignore # third_party/repositories/repositories.bzl
2 parents 706b5f1 + 6c6ba4e commit 579edc1

File tree

177 files changed

+2548
-1003
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

177 files changed

+2548
-1003
lines changed

.bazelci/presubmit.yml

+23
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ tasks:
4242
platform: macos
4343
shell_commands:
4444
- "./test_rules_scala.sh"
45+
test_rules_scala_win:
46+
name: "./test_rules_scala"
47+
platform: windows
48+
shell_commands:
49+
- "bash test_rules_scala.sh"
4550
test_coverage_linux_6_3_0:
4651
name: "./test_coverage"
4752
platform: ubuntu2004
@@ -80,8 +85,26 @@ tasks:
8085
bazel: 6.3.0
8186
shell_commands:
8287
- "./test_examples.sh"
88+
cross_build_linux:
89+
name: "./test_cross_build"
90+
platform: ubuntu2004
91+
bazel: 6.3.0
92+
shell_commands:
93+
- "./test_cross_build.sh"
8394
lint_linux:
8495
name: "bazel //tools:lint_check"
8596
platform: ubuntu2004
8697
run_targets:
8798
- "//tools:lint_check"
99+
test_rules_scala_jdk21:
100+
name: "./test_rules_scala with jdk21"
101+
platform: ubuntu2004
102+
shell_commands:
103+
- sudo apt update && sudo apt install -y libxml2-utils
104+
- mv tools/bazel.rc.buildkite tools/bazel.rc
105+
- echo "import %workspace%/tools/bazel.rc" > .bazelrc
106+
- echo "build --java_language_version=21" >> .bazelrc
107+
- echo "build --java_runtime_version=21" >> .bazelrc
108+
- echo "build --tool_java_language_version=21" >> .bazelrc
109+
- echo "build --tool_java_runtime_version=21" >> .bazelrc
110+
- "./test_rules_scala.sh"

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ hash2
1010
.vscode
1111
unformatted-*.backup.scala
1212
.scala-build
13+
test/semanticdb/tempsrc
1314
MODULE.bazel.lock

.scalafmt.conf

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
align.openParenCallSite = false
22
align.openParenDefnSite = false
33
continuationIndent.defnSite = 2
4-
danglingParentheses = true
5-
docstrings = JavaDoc
4+
danglingParentheses.preset = true
5+
docstrings.style = Asterisk
66
importSelectors = singleLine
77
maxColumn = 120
88
verticalMultiline.newlineBeforeImplicitKW = true

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ scala_register_toolchains()
162162
Note: Toolchains are a more flexible way to configure dependencies, so you should prefer that way.
163163
Please also note, that the `overriden_artifacts` parameter is likely to be removed in the future.
164164

165+
### Multiple versions (cross-compilation)
166+
167+
Rules scala supports configuring multiple Scala versions and offers target-level control of which one to use.
168+
169+
Please check [cross-compilation.md](docs/cross-compilation.md) for more details on cross-compilation support.
170+
165171
## Bazel compatible versions
166172

167173
| minimal bazel version | rules_scala gitsha |
@@ -280,6 +286,7 @@ Here's a (non-exhaustive) list of companies that use `rules_scala` in production
280286
* [Stripe](https://stripe.com/)
281287
* [Tally](https://www.meettally.com/)
282288
* [Twitter](https://twitter.com/)
289+
* [VirtusLab](https://virtuslab.com/)
283290
* [VSCO](https://vsco.co)
284291
* [Wix](https://www.wix.com/)
285292

WORKSPACE

+6
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,9 @@ repositories(
203203
],
204204
maven_servers = MAVEN_SERVER_URLS,
205205
)
206+
207+
load("//test/toolchains:jdk.bzl", "remote_jdk21_repositories", "remote_jdk21_toolchains")
208+
209+
remote_jdk21_repositories()
210+
211+
remote_jdk21_toolchains()

docs/cross-compilation.md

+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
# Cross compilation support
2+
3+
Read *Quick start* for an information on how to use cross compilation.
4+
The remaining sections contain more detailed information, useful especially for toolchain & rule developers.
5+
6+
## Quick start
7+
`scala_config` repository rule accepts two parameters related to Scala version:
8+
* `scala_version` – a single, default version;
9+
* `scala_versions` – a list of versions to make available for use.
10+
11+
The first one, `scala_version`, will be used as a default, but it can be overridden for specific targets for any version from the `scala_versions`.
12+
13+
Multiple rules, such as:
14+
- [scala_library](/scala/private/rules/scala_library.bzl)
15+
- [scala_binary](/scala/private/rules/scala_binary.bzl)
16+
- [scala_repl](/scala/private/rules/scala_repl.bzl)
17+
- [scala_test](/scala/private/rules/scala_test.bzl)
18+
19+
support such override via the `scala_version` attribute, e.g.:
20+
```starlark
21+
scala_library(
22+
name = ...
23+
...
24+
scala_version = "2.12.18",
25+
...
26+
)
27+
```
28+
29+
For this library and all its dependencies 2.12.18 compiler will be used, unless explicitly overridden again in another target.
30+
31+
## Version configuration
32+
33+
`scala_config` creates the repository `@io_bazel_rules_scala_config`.
34+
File created there, `config.bzl`, consists of many variables. In particular:
35+
* `SCALA_VERSION` – representing the default Scala version, e.g. `"3.3.1"`;
36+
* `SCALA_VERSIONS` – representing all configured Scala versions, e.g. `["2.12.18", "3.3.1"]`.
37+
38+
39+
## Build settings
40+
Configured `SCALA_VERSIONS` correspond to allowed values of [build setting](https://bazel.build/extending/config#user-defined-build-setting).
41+
42+
### `scala_version`
43+
`@io_bazel_rules_scala_config` in its root package defines the following build setting:
44+
```starlark
45+
string_setting(
46+
name = "scala_version",
47+
build_setting_default = "3.3.1",
48+
values = ["3.3.1"],
49+
visibility = ["//visibility:public"],
50+
)
51+
...
52+
```
53+
This build setting can be subject of change by [transitions](https://bazel.build/extending/config#user-defined-transitions) (within allowed `values`).
54+
55+
### Config settings
56+
Then for each Scala version we have a [config setting](https://bazel.build/extending/config#build-settings-and-select):
57+
```starlark
58+
config_setting(
59+
name = "scala_version_3_3_1",
60+
flag_values = {":scala_version": "3.3.1"},
61+
)
62+
...
63+
```
64+
The `name` of `config_setting` corresponds to `"scala_version" + version_suffix(scala_version)`.
65+
One may use this config setting in `select()` e.g. to provide dependencies relevant to a currently used Scala version.
66+
67+
68+
## Version-dependent behavior
69+
Don't rely on `SCALA_VERSION` as it represents the default Scala version, not necessarily the one that is currently requested.
70+
71+
If you need to customize the behavior for specific Scala version, there are two scenarios.
72+
73+
### From toolchain
74+
If you have an access to the Scala toolchain (`@io_bazel_rules_scala//scala:toolchain_type`), there is `scala_version` field provided in there:
75+
```starlark
76+
def _rule_impl(ctx):
77+
...
78+
ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scala_version
79+
...
80+
```
81+
82+
### From config setting
83+
In BUILD files, you need to use the config settings with `select()`.
84+
Majority of use cases is covered by the `select_for_scala_version` utility macro.
85+
If more flexibility is needed, you can always write the select manually.
86+
87+
#### With select macro
88+
See example usage of the `select_for_scala_version`:
89+
90+
```starlark
91+
load("@io_bazel_rules_scala//:scala_cross_version_select.bzl", "select_for_scala_version")
92+
93+
scala_library(
94+
...
95+
srcs = select_for_scala_version(
96+
before_3_1 = [
97+
# for Scala version < 3.1
98+
],
99+
between_3_1_and_3_2 = [
100+
# for 3.1 ≤ Scala version < 3.2
101+
],
102+
between_3_2_and_3_3_1 = [
103+
# for 3.2 ≤ Scala version < 3.3.1
104+
],
105+
since_3_3_1 = [
106+
# for 3.3.1 ≤ Scala version
107+
],
108+
)
109+
...
110+
)
111+
```
112+
113+
See complete documentation in the [scala_cross_version_select.bzl](/scala/scala_cross_version_select.bzl) file
114+
115+
#### Manually
116+
An example usage of `select()` to provide custom dependency for specific Scala version:
117+
```starlark
118+
deps = select({
119+
"@io_bazel_rules_scala_config//:scala_version_3_3_1": [...],
120+
...
121+
})
122+
```
123+
124+
For more complex logic, you can extract it to a `.bzl` file:
125+
```starlark
126+
def srcs(scala_version):
127+
if scala_version.startswith("2"):
128+
...
129+
...
130+
```
131+
and then in the `BUILD` file:
132+
```starlark
133+
load("@io_bazel_rules_scala//:scala_cross_version.bzl", "version_suffix")
134+
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS")
135+
load("....bzl", "srcs")
136+
137+
scala_library(
138+
...
139+
srcs = select({
140+
"@io_bazel_rules_scala_config//:scala_version" + version_suffix(v): srcs(v)
141+
for v in SCALA_VERSIONS
142+
}),
143+
...
144+
)
145+
```
146+
147+
148+
## Requesting specific version
149+
To use other than default version of Scala, you need to change the current `@io_bazel_rules_scala_config//:scala_version` build setting.
150+
151+
Simple transition, setting the Scala version to one found in `scala_version` attribute:
152+
```starlark
153+
def _scala_version_transition_impl(settings, attr):
154+
if attr.scala_version:
155+
return {"@io_bazel_rules_scala_config//:scala_version": attr.scala_version}
156+
else:
157+
return {}
158+
159+
scala_version_transition = transition(
160+
implementation = _scala_version_transition_impl,
161+
inputs = [],
162+
outputs = ["@io_bazel_rules_scala_config//:scala_version"],
163+
)
164+
```
165+
166+
To use it in a rule, use the `scala_version_transition` as `cfg` and use `toolchain_transition_attr` in `attrs`:
167+
```starlark
168+
load("@io_bazel_rules_scala//scala:scala_cross_version.bzl", "scala_version_transition", "toolchain_transition_attr")
169+
170+
_scala_library_attrs.update(toolchain_transition_attr)
171+
172+
def make_scala_library(*extras):
173+
return rule(
174+
attrs = _dicts.add(
175+
...
176+
toolchain_transition_attr,
177+
...
178+
),
179+
...
180+
cfg = scala_version_transition,
181+
incompatible_use_toolchain_transition = True,
182+
...
183+
)
184+
```
185+
186+
187+
## Toolchains
188+
Standard [toolchain resolution](https://bazel.build/extending/toolchains#toolchain-resolution) procedure determines which toolchain to use for Scala targets.
189+
190+
Toolchain should declare its compatibility with Scala version by using `target_settings` attribute of the `toolchain` rule:
191+
192+
```starlark
193+
toolchain(
194+
...
195+
target_settings = ["@io_bazel_rules_scala_config//:scala_version_3_3_1"],
196+
...
197+
)
198+
```
199+
200+
### Cross-build support tiers
201+
`rules_scala` consists of many toolchains implementing various toolchain types.
202+
Their support level for cross-build setup varies.
203+
204+
We can distinguish following tiers:
205+
206+
* No `target_settings` set – not migrated, will work on the default `SCALA_VERSION`; undefined behavior on other versions.
207+
* (all toolchains not mentioned elsewhere)
208+
* `target_settings` set to the `SCALA_VERSION` – not fully migrated; will work only on the default `SCALA_VERSION` and will fail the toolchain resolution on other versions.
209+
* (no development in progress)
210+
* Multiple toolchain instances with `target_settings` corresponding to each of `SCALA_VERSIONS` – fully migrated; will work in cross-build setup.
211+
* [the main Scala toolchain](/scala/BUILD)
212+
* [Scalafmt](/scala/scalafmt/BUILD)
213+
* [Scalatest](/testing/testing.bzl)

docs/phase_scalafmt.md

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ The extension provides default configuration, but there are 2 ways to use custom
4343
- Put `.scalafmt.conf` at root of your workspace
4444
- Pass `.scalafmt.conf` in via `config` attribute
4545

46+
If using scala 3 you must append `runner.dialect = scala3` to .scalafmt.conf
47+
4648
## IntelliJ plugin support
4749

4850
If you use IntelliJ Bazel plugin, then you should check the [Customizable Phase](/docs/customizable_phase.md#cooperation-with-intellij-plugin) page.

examples/crossbuild/1_single/BUILD

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_binary", "scala_library", "scala_test")
2+
3+
# Here we demonstrate the simplest case,
4+
# single binary, test or library for which we set a specific version or use the default one:
5+
6+
# This one will be compiled by 2.11 compiler:
7+
scala_library(
8+
name = "lib211",
9+
srcs = ["lib.scala"],
10+
scala_version = "2.11.12",
11+
)
12+
13+
# This one will be compiled by 2.13 compiler:
14+
scala_test(
15+
name = "test213",
16+
srcs = ["test.scala"],
17+
scala_version = "2.13.12",
18+
)
19+
20+
# This one will be compiled by 3.3 compiler (the default one):
21+
scala_binary(
22+
name = "bin33",
23+
srcs = ["bin.scala"],
24+
main_class = "X",
25+
)
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object C extends App {
2+
println("Hello")
3+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class A
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import org.scalatest.flatspec.AnyFlatSpec
2+
3+
class Zero extends AnyFlatSpec {
4+
"Equality" should "be tested" in {
5+
assert (0 == -0)
6+
}
7+
}

examples/crossbuild/2_deps/BUILD

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_binary", "scala_library")
2+
3+
# Here we demonstrate how scala_version is propagated through deps.
4+
5+
# This one will always be compiled by 2.11 compiler:
6+
scala_library(
7+
name = "lib211",
8+
srcs = ["lib.scala"],
9+
scala_version = "2.11.12",
10+
)
11+
12+
# This one will be compiled by 3.3 compiler (unless requested otherwise)
13+
scala_library(
14+
name = "lib",
15+
srcs = ["lib_default.scala"],
16+
)
17+
18+
scala_binary(
19+
name = "bin213",
20+
srcs = ["bin.scala"], # compiled with 2.13 (as per `scala_version`)
21+
main_class = "C",
22+
scala_version = "2.13.12",
23+
deps = [
24+
":lib", # compiled 2.13 (as per `scala_version`)
25+
":lib211", # compiled with 2.11 (that target overrides version)
26+
],
27+
)
28+
29+
scala_binary(
30+
name = "bin33",
31+
srcs = ["bin.scala"], # compiled with 3.3 (the default)
32+
main_class = "C",
33+
deps = [
34+
":lib", # compiled with 3.3 (default)
35+
":lib211", # compiled with 2.11 (that target overrides version)
36+
],
37+
)

examples/crossbuild/2_deps/bin.scala

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object C extends App {
2+
println("Hello, world")
3+
}

examples/crossbuild/2_deps/lib.scala

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class A
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class B

0 commit comments

Comments
 (0)