Skip to content

Commit d409cb6

Browse files
authored
Toolchainize //scala_proto:{,deps_}toolchain_type (#1687)
Adds scala_proto toolchains to `scala_toolchains()`. Part of #1482. The most significant part of the change is moving all the toolchain rules from `scala_proto/BUILD` to `setup_scala_toolchains()` in `scala_proto/toolchains.bzl`. Adds the `scala_proto_deps_providers()` macro to replace `//scala_proto:scalapb_{compile,grpc,worker}_deps_provider` targets in the `dep_providers` parameter of `scala_proto_deps_toolchain()`. Examples of this are in `test/proto/custom_generator/BUILD`. Excludes `@scala_proto_rules_scalapb_protoc_gen` from `DEFAULT_SCALAPB_WORKER_DEPS` in `scala_proto/default/default_deps.bzl` for Scala 2.11. For other Scala versions, this repo name will have the Scala version appended. This is to avoid build failures under Bzlmod, since: - This repo is required by ScalaPB 0.11.17, but Scala 2.11 is capped at ScalaPB 0.9.8. - Importing the nonexistent `scala_proto_rules_scalapb_protoc_gen` under Scala 2.11 results in an error under Bzlmod, as does importing it multiple times when configuring multiple Scala versions. - `MODULE.bazel` can iterate over a list of Scala versions, filtering out Scala 2.11, and call `use_repo()` on each version specific repo. A lot of the other changes are more opportunistic removals of `@io_bazel_rules_scala` label prefixes and application of `Label()` where appropriate. Doing this will allow Bzlmod users to use `rules_scala` without setting `repo_name = "@io_bazel_rules_scala"` in `bazel_dep()`.
1 parent 3ca60fb commit d409cb6

File tree

13 files changed

+177
-138
lines changed

13 files changed

+177
-138
lines changed

WORKSPACE

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ load("//scala:toolchains.bzl", "scala_toolchains")
5050

5151
scala_toolchains(
5252
fetch_sources = True,
53+
scala_proto = True,
5354
scalafmt = True,
5455
testing = True,
5556
)
@@ -68,10 +69,6 @@ load("//jmh:jmh.bzl", "jmh_repositories")
6869

6970
jmh_repositories()
7071

71-
load("//scala_proto:scala_proto.bzl", "scala_proto_repositories")
72-
73-
scala_proto_repositories()
74-
7572
# needed for the cross repo proto test
7673
local_repository(
7774
name = "proto_cross_repo_boundary",

scala/private/toolchain_deps/toolchain_dep_rules.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ load(
33
"expose_toolchain_deps",
44
)
55

6-
_toolchain_type = "@io_bazel_rules_scala//scala:toolchain_type"
6+
_TOOLCHAIN_TYPE = Label("//scala:toolchain_type")
77

88
def _common_toolchain_deps(ctx):
9-
return expose_toolchain_deps(ctx, _toolchain_type)
9+
return expose_toolchain_deps(ctx, _TOOLCHAIN_TYPE)
1010

1111
common_toolchain_deps = rule(
1212
implementation = _common_toolchain_deps,
1313
attrs = {
1414
"deps_id": attr.string(mandatory = True),
1515
},
16-
toolchains = [_toolchain_type],
16+
toolchains = [_TOOLCHAIN_TYPE],
1717
incompatible_use_toolchain_transition = True,
1818
)

scala/toolchains.bzl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ load(
99
)
1010
load("//scala:scala_cross_version.bzl", "default_maven_server_urls")
1111
load("//scala:toolchains_repo.bzl", "scala_toolchains_repo")
12+
load("//scala_proto/default:repositories.bzl", "scala_proto_artifact_ids")
1213
load("//scalatest:scalatest.bzl", "scalatest_artifact_ids")
1314
load("//specs2:specs2.bzl", "specs2_artifact_ids")
1415
load("//specs2:specs2_junit.bzl", "specs2_junit_artifact_ids")
@@ -28,7 +29,9 @@ def scala_toolchains(
2829
specs2 = False,
2930
testing = False,
3031
scalafmt = False,
31-
scalafmt_default_config_path = ".scalafmt.conf"):
32+
scalafmt_default_config_path = ".scalafmt.conf",
33+
scala_proto = False,
34+
scala_proto_enable_all_options = False):
3235
"""Instantiates @io_bazel_rules_scala_toolchains and all its dependencies.
3336
3437
Provides a unified interface to configuring rules_scala both directly in a
@@ -76,6 +79,10 @@ def scala_toolchains(
7679
scalafmt: whether to instantiate the Scalafmt toolchain
7780
scalafmt_default_config_path: the relative path to the default Scalafmt
7881
config file within the repository
82+
scala_proto: whether to instantiate the scala_proto toolchain
83+
scala_proto_enable_all_options: whether to instantiate the scala_proto
84+
toolchain with all options enabled; `scala_proto` must also be
85+
`True` for this to take effect
7986
"""
8087
scala_repositories(
8188
maven_servers = maven_servers,
@@ -119,6 +126,11 @@ def scala_toolchains(
119126
for scala_version in SCALA_VERSIONS:
120127
version_specific_artifact_ids = {}
121128

129+
if scala_proto:
130+
version_specific_artifact_ids.update({
131+
id: True
132+
for id in scala_proto_artifact_ids(scala_version)
133+
})
122134
if scalafmt:
123135
version_specific_artifact_ids.update({
124136
id: fetch_sources
@@ -145,6 +157,8 @@ def scala_toolchains(
145157
specs2 = specs2,
146158
testing = testing,
147159
scalafmt = scalafmt,
160+
scala_proto = scala_proto,
161+
scala_proto_enable_all_options = scala_proto_enable_all_options,
148162
)
149163

150164
def scala_register_toolchains():

scala/toolchains_repo.bzl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@ def _scala_toolchains_repo_impl(repository_ctx):
3939
repo_attr = repository_ctx.attr
4040
format_args = {
4141
"rules_scala_repo": Label("//:all").repo_name,
42+
"proto_enable_all_options": repo_attr.scala_proto_enable_all_options,
4243
}
4344
toolchains = {}
4445

4546
if repo_attr.scala:
4647
toolchains["scala"] = _SCALA_TOOLCHAIN_BUILD
48+
if repo_attr.scala_proto:
49+
toolchains["scala_proto"] = _SCALA_PROTO_TOOLCHAIN_BUILD
4750

4851
testing_build_args = _generate_testing_toolchain_build_file_args(repo_attr)
4952
if testing_build_args != None:
@@ -73,6 +76,8 @@ _scala_toolchains_repo = repository_rule(
7376
"specs2": attr.bool(),
7477
"testing": attr.bool(),
7578
"scalafmt": attr.bool(),
79+
"scala_proto": attr.bool(),
80+
"scala_proto_enable_all_options": attr.bool(),
7681
},
7782
)
7883

@@ -156,3 +161,43 @@ load(
156161
157162
setup_scalafmt_toolchains()
158163
"""
164+
165+
_SCALA_PROTO_TOOLCHAIN_BUILD = """
166+
load("@@{rules_scala_repo}//scala:providers.bzl", "declare_deps_provider")
167+
load(
168+
"@@{rules_scala_repo}//scala_proto/default:default_deps.bzl",
169+
"DEFAULT_SCALAPB_COMPILE_DEPS",
170+
"DEFAULT_SCALAPB_GRPC_DEPS",
171+
"DEFAULT_SCALAPB_WORKER_DEPS",
172+
)
173+
load(
174+
"@@{rules_scala_repo}//scala_proto:toolchains.bzl",
175+
"setup_scala_proto_toolchains",
176+
)
177+
178+
setup_scala_proto_toolchains(
179+
name = "scala_proto",
180+
enable_all_options = {proto_enable_all_options},
181+
)
182+
183+
declare_deps_provider(
184+
name = "scalapb_compile_deps_provider",
185+
deps_id = "scalapb_compile_deps",
186+
visibility = ["//visibility:public"],
187+
deps = DEFAULT_SCALAPB_COMPILE_DEPS,
188+
)
189+
190+
declare_deps_provider(
191+
name = "scalapb_grpc_deps_provider",
192+
deps_id = "scalapb_grpc_deps",
193+
visibility = ["//visibility:public"],
194+
deps = DEFAULT_SCALAPB_GRPC_DEPS,
195+
)
196+
197+
declare_deps_provider(
198+
name = "scalapb_worker_deps_provider",
199+
deps_id = "scalapb_worker_deps",
200+
visibility = ["//visibility:public"],
201+
deps = DEFAULT_SCALAPB_WORKER_DEPS,
202+
)
203+
"""

scala_proto/BUILD

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
load("//scala:providers.bzl", "declare_deps_provider")
2-
load(
3-
"//scala_proto/default:default_deps.bzl",
4-
"DEFAULT_SCALAPB_COMPILE_DEPS",
5-
"DEFAULT_SCALAPB_GRPC_DEPS",
6-
"DEFAULT_SCALAPB_WORKER_DEPS",
7-
)
81
load("//scala_proto/private:toolchain_deps.bzl", "export_scalapb_toolchain_deps")
9-
load(
10-
"//scala_proto:scala_proto_toolchain.bzl",
11-
"scala_proto_deps_toolchain",
12-
"scala_proto_toolchain",
13-
)
142

153
toolchain_type(
164
name = "toolchain_type",
@@ -22,68 +10,6 @@ toolchain_type(
2210
visibility = ["//visibility:public"],
2311
)
2412

25-
scala_proto_deps_toolchain(
26-
name = "default_deps_toolchain_impl",
27-
visibility = ["//visibility:public"],
28-
)
29-
30-
scala_proto_toolchain(
31-
name = "default_toolchain_impl",
32-
visibility = ["//visibility:public"],
33-
with_flat_package = False,
34-
with_grpc = True,
35-
with_single_line_to_string = False,
36-
)
37-
38-
toolchain(
39-
name = "default_toolchain",
40-
toolchain = ":default_toolchain_impl",
41-
toolchain_type = "//scala_proto:toolchain_type",
42-
visibility = ["//visibility:public"],
43-
)
44-
45-
toolchain(
46-
name = "default_deps_toolchain",
47-
toolchain = ":default_deps_toolchain_impl",
48-
toolchain_type = ":deps_toolchain_type",
49-
)
50-
51-
scala_proto_toolchain(
52-
name = "enable_all_options_toolchain_impl",
53-
visibility = ["//visibility:public"],
54-
with_flat_package = True,
55-
with_grpc = True,
56-
with_single_line_to_string = True,
57-
)
58-
59-
toolchain(
60-
name = "enable_all_options_toolchain",
61-
toolchain = ":enable_all_options_toolchain_impl",
62-
toolchain_type = "//scala_proto:toolchain_type",
63-
visibility = ["//visibility:public"],
64-
)
65-
66-
declare_deps_provider(
67-
name = "scalapb_compile_deps_provider",
68-
deps_id = "scalapb_compile_deps",
69-
visibility = ["//visibility:public"],
70-
deps = DEFAULT_SCALAPB_COMPILE_DEPS,
71-
)
72-
73-
declare_deps_provider(
74-
name = "scalapb_grpc_deps_provider",
75-
deps_id = "scalapb_grpc_deps",
76-
visibility = ["//visibility:public"],
77-
deps = DEFAULT_SCALAPB_GRPC_DEPS,
78-
)
79-
80-
declare_deps_provider(
81-
name = "scalapb_worker_deps_provider",
82-
deps_id = "scalapb_worker_deps",
83-
visibility = ["//visibility:public"],
84-
deps = DEFAULT_SCALAPB_WORKER_DEPS,
85-
)
86-
8713
export_scalapb_toolchain_deps(
8814
name = "scalapb_worker_deps",
8915
deps_id = "scalapb_worker_deps",

scala_proto/default/default_deps.bzl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,22 @@
77
# dependency lists. This needs to be the unrolled transitive path to be used
88
# without such a facility.
99

10+
load("//scala:scala_cross_version.bzl", "repositories")
1011
load("//scala:scala_cross_version_select.bzl", "select_for_scala_version")
12+
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
13+
14+
_DEFAULT_DEP_PROVIDER_FORMAT = (
15+
"@io_bazel_rules_scala_toolchains//scala_proto:scalapb_%s_deps_provider"
16+
)
17+
18+
def scala_proto_deps_providers(
19+
compile = _DEFAULT_DEP_PROVIDER_FORMAT % "compile",
20+
grpc = _DEFAULT_DEP_PROVIDER_FORMAT % "grpc",
21+
worker = _DEFAULT_DEP_PROVIDER_FORMAT % "worker"):
22+
return [compile, grpc, worker]
1123

1224
DEFAULT_SCALAPB_COMPILE_DEPS = [
13-
"//scala/private/toolchain_deps:scala_library_classpath",
25+
Label("//scala/private/toolchain_deps:scala_library_classpath"),
1426
"@com_google_protobuf//:protobuf_java",
1527
"@com_lihaoyi_fastparse",
1628
"@scala_proto_rules_scalapb_lenses",
@@ -51,5 +63,7 @@ DEFAULT_SCALAPB_WORKER_DEPS = [
5163
"@scala_proto_rules_scalapb_protoc_bridge",
5264
] + select_for_scala_version(
5365
any_2_11 = [],
54-
since_2_12 = ["@scala_proto_rules_scalapb_protoc_gen"],
66+
since_2_12 = repositories(SCALA_VERSION, [
67+
"@scala_proto_rules_scalapb_protoc_gen",
68+
]),
5569
)

scala_proto/private/scala_proto_aspect.bzl

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@ load(
99
)
1010
load("//scala/private/toolchain_deps:toolchain_deps.bzl", "find_deps_info_on")
1111
load(
12-
"@io_bazel_rules_scala//scala_proto/private:scala_proto_aspect_provider.bzl",
12+
"//scala_proto/private:scala_proto_aspect_provider.bzl",
1313
"ScalaProtoAspectInfo",
1414
)
15-
load(
16-
"@io_bazel_rules_scala//scala/private:phases/api.bzl",
17-
"extras_phases",
18-
"run_aspect_phases",
19-
)
15+
load("//scala/private:phases/api.bzl", "extras_phases", "run_aspect_phases")
2016
load("@bazel_skylib//lib:dicts.bzl", "dicts")
2117

2218
def _import_paths(proto, ctx):
@@ -47,7 +43,7 @@ def _code_should_be_generated(ctx, toolchain):
4743
return toolchain.blacklisted_protos.get(target_absolute_label) == None
4844

4945
def _compile_deps(ctx, toolchain):
50-
deps_toolchain_type_label = "@io_bazel_rules_scala//scala_proto:deps_toolchain_type"
46+
deps_toolchain_type_label = Label("//scala_proto:deps_toolchain_type")
5147
return [
5248
dep[JavaInfo]
5349
for id in toolchain.compile_dep_ids
@@ -149,14 +145,14 @@ def _phase_deps(ctx, p):
149145
return [d[ScalaProtoAspectInfo].java_info for d in ctx.rule.attr.deps]
150146

151147
def _phase_scalacopts(ctx, p):
152-
return ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scalacopts
148+
return ctx.toolchains[Label("//scala:toolchain_type")].scalacopts
153149

154150
def _phase_generate_and_compile(ctx, p):
155151
proto = p.proto_info
156152
deps = p.deps
157153
scalacopts = p.scalacopts
158154
stamp_label = p.stamp_label
159-
toolchain = ctx.toolchains["@io_bazel_rules_scala//scala_proto:toolchain_type"]
155+
toolchain = ctx.toolchains[Label("//scala_proto:toolchain_type")]
160156

161157
if proto.direct_sources and _code_should_be_generated(ctx, toolchain):
162158
src_jars = _generate_sources(ctx, toolchain, proto)
@@ -181,7 +177,7 @@ def _strip_suffix(str, suffix):
181177

182178
def _phase_stamp_label(ctx, p):
183179
rule_label = str(p.target.label)
184-
toolchain = ctx.toolchains["@io_bazel_rules_scala//scala_proto:toolchain_type"]
180+
toolchain = ctx.toolchains[Label("//scala_proto:toolchain_type")]
185181

186182
if toolchain.stamp_by_convention and rule_label.endswith("_proto"):
187183
return _strip_suffix(rule_label, "_proto") + "_scala_proto"
@@ -228,9 +224,9 @@ def make_scala_proto_aspect(*extras):
228224
*[extra["attrs"] for extra in extras if "attrs" in extra]
229225
),
230226
toolchains = [
231-
"@io_bazel_rules_scala//scala:toolchain_type",
232-
"@io_bazel_rules_scala//scala_proto:toolchain_type",
233-
"@io_bazel_rules_scala//scala_proto:deps_toolchain_type",
227+
Label("//scala:toolchain_type"),
228+
Label("//scala_proto:toolchain_type"),
229+
Label("//scala_proto:deps_toolchain_type"),
234230
"@bazel_tools//tools/jdk:toolchain_type",
235231
],
236232
)
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
load(
2-
"@io_bazel_rules_scala//scala/private/toolchain_deps:toolchain_deps.bzl",
2+
"//scala/private/toolchain_deps:toolchain_deps.bzl",
33
"expose_toolchain_deps",
44
)
55

6+
_DEPS_TOOLCHAIN_TYPE = Label("//scala_proto:deps_toolchain_type")
7+
68
def _export_scalapb_toolchain_deps(ctx):
7-
return expose_toolchain_deps(ctx, "@io_bazel_rules_scala//scala_proto:deps_toolchain_type")
9+
return expose_toolchain_deps(ctx, _DEPS_TOOLCHAIN_TYPE)
810

911
export_scalapb_toolchain_deps = rule(
1012
_export_scalapb_toolchain_deps,
@@ -14,5 +16,5 @@ export_scalapb_toolchain_deps = rule(
1416
),
1517
},
1618
incompatible_use_toolchain_transition = True,
17-
toolchains = ["@io_bazel_rules_scala//scala_proto:deps_toolchain_type"],
19+
toolchains = [_DEPS_TOOLCHAIN_TYPE],
1820
)

scala_proto/scala_proto_toolchain.bzl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
load("//scala:providers.bzl", "DepsInfo")
2+
load(
3+
"//scala_proto/default:default_deps.bzl",
4+
_scala_proto_deps_providers = "scala_proto_deps_providers",
5+
)
26

37
def _generators(ctx):
48
return dict(
@@ -138,13 +142,11 @@ scala_proto_deps_toolchain = rule(
138142
implementation = _scala_proto_deps_toolchain,
139143
attrs = {
140144
"dep_providers": attr.label_list(
141-
default = [
142-
Label("//scala_proto:scalapb_compile_deps_provider"),
143-
Label("//scala_proto:scalapb_grpc_deps_provider"),
144-
Label("//scala_proto:scalapb_worker_deps_provider"),
145-
],
145+
default = _scala_proto_deps_providers(),
146146
cfg = "target",
147147
providers = [DepsInfo],
148148
),
149149
},
150150
)
151+
152+
scala_proto_deps_providers = _scala_proto_deps_providers

0 commit comments

Comments
 (0)