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

Implement a toolchain #51

Merged
merged 14 commits into from
Nov 14, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ load("@rules_scala_annex//rules/scala:workspace.bzl", "scala_register_toolchains
scala_repositories()
load("@annex//:defs.bzl", annex_pinned_maven_install = "pinned_maven_install")
annex_pinned_maven_install()
scala_register_toolchains()
scala_register_toolchains(default_scala_toolchain_name = "zinc_3")

load("@rules_scala_annex//rules/scalafmt:workspace.bzl", "scalafmt_default_config", "scalafmt_repositories")
scalafmt_repositories()
Expand Down
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ load("@annex//:defs.bzl", annex_pinned_maven_install = "pinned_maven_install")

annex_pinned_maven_install()

scala_register_toolchains()
scala_register_toolchains(default_scala_toolchain_name = "zinc_2_13")

# Scala 2.12

Expand Down
1 change: 0 additions & 1 deletion docs/newdocs/phases.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def _scala_binary_implementation(ctx):
("resources", _phase_resources),
("classpaths", _phase_classpaths),
("javainfo", _phase_javainfo),
("compile", _phase_noop),
("singlejar", _phase_singlejar),
("coverage", _phase_coverage_jacoco),
("ijinfo", _phase_ijinfo),
Expand Down
72 changes: 47 additions & 25 deletions docs/newdocs/scala_versions.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
## Specifying the Scala version to use

The scala version used by a buildable target is specified via the `ScalaConfiguration` passed in to the rule's `scala` attribute.
We use a [toolchain](https://bazel.build/extending/toolchains) to store compiler configuration,
which includes:
- Which compiler to use
- What compile-time and runtime dependencies to add
- What compiler plugins and options to use
- Which Zinc compiler bridge to use
- etc.

This attribute defaults to using the `default_scala` specified via `bind` in the `WORKSPACE` file of the repo. For example, suppose the `ScalaConfiguration` you wish to default to is defined by `//scala:2_13`. In your `WORKSPACE`, you would include:
We provide two macros for defining Scala toolchains: `register_bootstrap_toolchain` and
`register_zinc_toolchain`, both of which are in `@rules_scala_annex//rules/register_toolchain.bzl`.
The latter requires the former.

Once you've registered both types of toolchains, you'll need to tell Bazel about them and set the
default one (which we recommend is a Zinc toolchain so you can get things like
unused/undeclared dependency checking and test code coverage checking) via the
`scala_register_toolchains` repository rule. Something like this should work, assuming this
repository is mapped to `rules_scala_annex`:

*/BUILD.bazel*
```python
bind(
name = "default_scala",
actual = "//scala:2_13",
load(
"@rules_scala_annex//rules/register_toolchain.bzl",
"register_bootstrap_toolchain",
"register_zinc_toolchain",
)
```

We provide two means of creating the `ScalaConfiguration`: `configure_bootstrap_scala` and `configure_zinc_scala`. The former is required by the latter.

Example:

```python
compiler_classpath_2_13 = [
"@scala_compiler_2_13//jar",
"@scala_library_2_13//jar",
"@scala_reflect_2_13//jar",
]

runtime_classpath_2_13 = [
"@scala_library_2_13//jar",
]
runtime_classpath_2_13 = ["@scala_library_2_13//jar"]

# This creates a basic ScalaConfiguration that relies on the scalac compiler
configure_bootstrap_scala(
register_bootstrap_toolchain(
name = "bootstrap_2_13",
compiler_classpath = compiler_classpath_2_13,
runtime_classpath = runtime_classpath_2_13,
Expand All @@ -38,10 +45,8 @@ configure_bootstrap_scala(
# compiler bridge needed to configure zinc compiler
scala_library(
name = "compiler_bridge_2_13",
srcs = [
"@compiler_bridge_2_13//:src",
],
scala = ":bootstrap_2_13",
srcs = ["@compiler_bridge_2_13//:src"],
scala_toolchain_name = "bootstrap_2_13",
visibility = ["//visibility:public"],
deps = compiler_classpath_2_13 + [
"@scala_annex_org_scala_sbt_compiler_interface//jar",
Expand All @@ -50,7 +55,7 @@ scala_library(
)

# This augments the configuration to configure the zinc compiler
configure_zinc_scala(
register_zinc_toolchain(
name = "zinc_2_13",
compiler_bridge = ":compiler_bridge_2_13",
compiler_classpath = compiler_classpath_2_13,
Expand All @@ -60,24 +65,41 @@ configure_zinc_scala(
)
```

It is possible to use a different `ScalaConfiguration` on different build targets. All you need to do is specify a different one in the `scala` attribute. If no `scala` attribute is specified, the `default_scala` bound to in your `WORKSPACE` is used.
*/WORKSPACE*
```python
load("@rules_scala_annex//rules/scala:workspace.bzl", "scala_register_toolchains")

...

scala_register_toolchains(
toolchains = ["//:bootstrap_2_13", "//:zinc_2_13"],
default_scala_toolchain_name = "zinc_2_13",
)

...
```

Take note of the `scala_toolchain_name` attribute on `scala_library` and the other Scala rules. Each
toolchain that's registered via `scala_register_toolchains` is identified by its `name`. Individual
Scala targets can be made to use a particular toolchain by setting their `scala_toolchain_name`
attribute.

For example:

```python
scala_library(
name = "example_compiled_with_scalac",
srcs = glob(["**/*.scala"])
scala = "<package>:bootstrap_2_13
scala_toolchain_name = "bootstrap_2_13",
)

scala_library(
name = "example_compiled_with_zinc",
srcs = glob(["**/*.scala"])
scala = "<package>:zinc_2_13
scala_toolchain_name = "zinc_2_13",
)

# This would use whatever //external:default_scala points to (i.e. what you bind default_scala to in your WORKSPACE)
# This would use the default toolchain, which we configured via `scala_register_toolchains` above
scala_library(
name = "example_compiled_with_default_scala",
srcs = glob(["**/*.scala"])
Expand Down
2 changes: 1 addition & 1 deletion docs/scala.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Each define may have a value of:

Failed checks emit suggested [buildozer](https://github.com/bazelbuild/buildtools/tree/master/buildozer) commands.

You may also toggle deps check via [configure_zinc_scala](configure_zinc_scala.md):
You may also toggle deps check via `register_zinc_toolchain`:

* `deps_direct` - Work the same as `scala_deps_direct`.
* `deps_used` - Work the same as `scala_deps_used`.
Expand Down
3 changes: 1 addition & 2 deletions docs/stardoc/rules_scala.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<pre>
load("@//rules:rules_scala.bzl", "emulate_rules_scala")

emulate_rules_scala(<a href="#emulate_rules_scala-scala">scala</a>, <a href="#emulate_rules_scala-scalatest">scalatest</a>, <a href="#emulate_rules_scala-extra_deps">extra_deps</a>)
emulate_rules_scala(<a href="#emulate_rules_scala-scalatest">scalatest</a>, <a href="#emulate_rules_scala-extra_deps">extra_deps</a>)
</pre>


Expand All @@ -19,7 +19,6 @@ emulate_rules_scala(<a href="#emulate_rules_scala-scala">scala</a>, <a href="#em

| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="emulate_rules_scala-scala"></a>scala | <p align="center"> - </p> | none |
| <a id="emulate_rules_scala-scalatest"></a>scalatest | <p align="center"> - </p> | none |
| <a id="emulate_rules_scala-extra_deps"></a>extra_deps | <p align="center"> - </p> | `[]` |

Expand Down
Loading
Loading