Skip to content

Commit a499cac

Browse files
committed
Toolchainize //scala_proto:{,deps_}toolchain_type
Adds scala_proto toolchains to `scala_toolchains()`. Part of bazel-contrib#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`. 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 74ccba4 commit a499cac

File tree

13 files changed

+174
-139
lines changed

13 files changed

+174
-139
lines changed

WORKSPACE

+1-4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ load("//scala:toolchains.bzl", "scala_toolchains")
4343

4444
scala_toolchains(
4545
fetch_sources = True,
46+
scala_proto = True,
4647
scalafmt = True,
4748
testing = True,
4849
)
@@ -78,10 +79,6 @@ load("//jmh:jmh.bzl", "jmh_repositories")
7879

7980
jmh_repositories()
8081

81-
load("//scala_proto:scala_proto.bzl", "scala_proto_repositories")
82-
83-
scala_proto_repositories()
84-
8582
MAVEN_SERVER_URLS = default_maven_server_urls()
8683

8784
# needed for the cross repo proto test

scala/private/macros/toolchains_repo.bzl

+45
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/private/toolchain_deps/toolchain_dep_rules.bzl

+3-3
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

+15-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ load(
99
"scalafmt_default_config",
1010
)
1111
load("//scala:scala_cross_version.bzl", "default_maven_server_urls")
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_proto/BUILD

-74
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

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,18 @@
99

1010
load("//scala:scala_cross_version_select.bzl", "select_for_scala_version")
1111

12+
_DEFAULT_DEP_PROVIDER_FORMAT = (
13+
"@io_bazel_rules_scala_toolchains//scala_proto:scalapb_%s_deps_provider"
14+
)
15+
16+
def scala_proto_deps_providers(
17+
compile = _DEFAULT_DEP_PROVIDER_FORMAT % "compile",
18+
grpc = _DEFAULT_DEP_PROVIDER_FORMAT % "grpc",
19+
worker = _DEFAULT_DEP_PROVIDER_FORMAT % "worker"):
20+
return [compile, grpc, worker]
21+
1222
DEFAULT_SCALAPB_COMPILE_DEPS = [
13-
"//scala/private/toolchain_deps:scala_library_classpath",
23+
Label("//scala/private/toolchain_deps:scala_library_classpath"),
1424
"@com_google_protobuf//:protobuf_java",
1525
"@com_lihaoyi_fastparse",
1626
"@scala_proto_rules_scalapb_lenses",

scala_proto/private/scala_proto_aspect.bzl

+11-15
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"
@@ -209,10 +205,10 @@ def _scala_proto_aspect_impl(target, ctx):
209205
def make_scala_proto_aspect(*extras):
210206
attrs = {
211207
"_java_toolchain": attr.label(
212-
default = Label("@bazel_tools//tools/jdk:current_java_toolchain"),
208+
default = "@bazel_tools//tools/jdk:current_java_toolchain",
213209
),
214210
"_java_host_runtime": attr.label(
215-
default = Label("@bazel_tools//tools/jdk:current_host_java_runtime"),
211+
default = "@bazel_tools//tools/jdk:current_host_java_runtime",
216212
),
217213
}
218214
return aspect(
@@ -226,9 +222,9 @@ def make_scala_proto_aspect(*extras):
226222
*[extra["attrs"] for extra in extras if "attrs" in extra]
227223
),
228224
toolchains = [
229-
"@io_bazel_rules_scala//scala:toolchain_type",
230-
"@io_bazel_rules_scala//scala_proto:toolchain_type",
231-
"@io_bazel_rules_scala//scala_proto:deps_toolchain_type",
225+
Label("//scala:toolchain_type"),
226+
Label("//scala_proto:toolchain_type"),
227+
Label("//scala_proto:deps_toolchain_type"),
232228
"@bazel_tools//tools/jdk:toolchain_type",
233229
],
234230
)
+5-3
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

+7-5
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(
@@ -131,13 +135,11 @@ scala_proto_deps_toolchain = rule(
131135
implementation = _scala_proto_deps_toolchain,
132136
attrs = {
133137
"dep_providers": attr.label_list(
134-
default = [
135-
Label("//scala_proto:scalapb_compile_deps_provider"),
136-
Label("//scala_proto:scalapb_grpc_deps_provider"),
137-
Label("//scala_proto:scalapb_worker_deps_provider"),
138-
],
138+
default = _scala_proto_deps_providers(),
139139
cfg = "target",
140140
providers = [DepsInfo],
141141
),
142142
},
143143
)
144+
145+
scala_proto_deps_providers = _scala_proto_deps_providers

0 commit comments

Comments
 (0)