This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
forked from bazelbuild/rules_scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scala_library.bzl
253 lines (222 loc) · 7.71 KB
/
scala_library.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
load("@bazel_skylib//lib:dicts.bzl", _dicts = "dicts")
load(
"@io_bazel_rules_scala//scala/private:common.bzl",
"sanitize_string_for_usage",
)
load(
"@io_bazel_rules_scala//scala/private:common_attributes.bzl",
"common_attrs",
"common_attrs_for_plugin_bootstrapping",
"implicit_deps",
"resolve_deps",
)
load("@io_bazel_rules_scala//scala/private:common_outputs.bzl", "common_outputs")
load(
"@io_bazel_rules_scala//scala/private:coverage_replacements_provider.bzl",
_coverage_replacements_provider = "coverage_replacements_provider",
)
load(
"@io_bazel_rules_scala//scala/private:phases/phases.bzl",
"extras_phases",
"phase_collect_exports_jars",
"phase_collect_jars_common",
"phase_collect_jars_macro_library",
"phase_collect_srcjars",
"phase_compile_common",
"phase_compile_library",
"phase_compile_library_for_plugin_bootstrapping",
"phase_compile_macro_library",
"phase_coverage_common",
"phase_coverage_library",
"phase_default_info",
"phase_dependency_common",
"phase_dependency_library_for_plugin_bootstrapping",
"phase_merge_jars",
"phase_runfiles_library",
"phase_scalac_provider",
"phase_write_manifest",
"run_phases",
)
##
# Common stuff to _library rules
##
_library_attrs = {
"main_class": attr.string(),
"exports": attr.label_list(
allow_files = False,
aspects = [_coverage_replacements_provider.aspect],
),
}
##
# scala_library
##
def _scala_library_impl(ctx):
# Build up information from dependency-like attributes
return run_phases(
ctx,
# customizable phases
[
("scalac_provider", phase_scalac_provider),
("collect_srcjars", phase_collect_srcjars),
("write_manifest", phase_write_manifest),
("dependency", phase_dependency_common),
("collect_jars", phase_collect_jars_common),
("compile", phase_compile_library),
("coverage", phase_coverage_library),
("merge_jars", phase_merge_jars),
("runfiles", phase_runfiles_library),
("collect_exports_jars", phase_collect_exports_jars),
("default_info", phase_default_info),
],
)
_scala_library_attrs = {}
_scala_library_attrs.update(implicit_deps)
_scala_library_attrs.update(common_attrs)
_scala_library_attrs.update(_library_attrs)
_scala_library_attrs.update(resolve_deps)
def make_scala_library(*extras):
return rule(
attrs = _dicts.add(
_scala_library_attrs,
extras_phases(extras),
*[extra["attrs"] for extra in extras if "attrs" in extra]
),
fragments = ["java"],
outputs = _dicts.add(
common_outputs,
*[extra["outputs"] for extra in extras if "outputs" in extra]
),
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_library_impl,
)
scala_library = make_scala_library()
# Scala library suite generates a series of scala libraries
# then it depends on them with a meta one which exports all the sub targets
def scala_library_suite(
name,
srcs = [],
exports = [],
visibility = None,
**kwargs):
ts = []
for src_file in srcs:
n = "%s_lib_%s" % (name, sanitize_string_for_usage(src_file))
scala_library(
name = n,
srcs = [src_file],
visibility = visibility,
exports = exports,
unused_dependency_checker_mode = "off",
**kwargs
)
ts.append(n)
scala_library(
name = name,
visibility = visibility,
exports = exports + ts,
deps = ts,
)
##
# scala_library_for_plugin_bootstrapping
##
def _scala_library_for_plugin_bootstrapping_impl(ctx):
return run_phases(
ctx,
# customizable phases
[
("scalac_provider", phase_scalac_provider),
("collect_srcjars", phase_collect_srcjars),
("write_manifest", phase_write_manifest),
("dependency", phase_dependency_library_for_plugin_bootstrapping),
("collect_jars", phase_collect_jars_common),
("compile", phase_compile_library_for_plugin_bootstrapping),
("merge_jars", phase_merge_jars),
("runfiles", phase_runfiles_library),
("collect_exports_jars", phase_collect_exports_jars),
("default_info", phase_default_info),
],
)
# the scala compiler plugin used for dependency analysis is compiled using `scala_library`.
# in order to avoid cyclic dependencies `scala_library_for_plugin_bootstrapping` was created for this purpose,
# which does not contain plugin related attributes, and thus avoids the cyclic dependency issue
_scala_library_for_plugin_bootstrapping_attrs = {
"build_ijar": attr.bool(default = True),
}
_scala_library_for_plugin_bootstrapping_attrs.update(implicit_deps)
_scala_library_for_plugin_bootstrapping_attrs.update(_library_attrs)
_scala_library_for_plugin_bootstrapping_attrs.update(resolve_deps)
_scala_library_for_plugin_bootstrapping_attrs.update(
common_attrs_for_plugin_bootstrapping,
)
def make_scala_library_for_plugin_bootstrapping(*extras):
return rule(
attrs = _dicts.add(
_scala_library_for_plugin_bootstrapping_attrs,
extras_phases(extras),
*[extra["attrs"] for extra in extras if "attrs" in extra]
),
fragments = ["java"],
outputs = _dicts.add(
common_outputs,
*[extra["outputs"] for extra in extras if "outputs" in extra]
),
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_library_for_plugin_bootstrapping_impl,
)
scala_library_for_plugin_bootstrapping = make_scala_library_for_plugin_bootstrapping()
##
# scala_macro_library
##
def _scala_macro_library_impl(ctx):
return run_phases(
ctx,
# customizable phases
[
("scalac_provider", phase_scalac_provider),
("collect_srcjars", phase_collect_srcjars),
("write_manifest", phase_write_manifest),
("dependency", phase_dependency_common),
("collect_jars", phase_collect_jars_macro_library),
("compile", phase_compile_macro_library),
("coverage", phase_coverage_common),
("merge_jars", phase_merge_jars),
("runfiles", phase_runfiles_library),
("collect_exports_jars", phase_collect_exports_jars),
("default_info", phase_default_info),
],
)
_scala_macro_library_attrs = {
"main_class": attr.string(),
"exports": attr.label_list(allow_files = False),
}
_scala_macro_library_attrs.update(implicit_deps)
_scala_macro_library_attrs.update(common_attrs)
_scala_macro_library_attrs.update(_library_attrs)
_scala_macro_library_attrs.update(resolve_deps)
# Set unused_dependency_checker_mode default to off for scala_macro_library
_scala_macro_library_attrs["unused_dependency_checker_mode"] = attr.string(
default = "off",
values = [
"warn",
"error",
"off",
"",
],
mandatory = False,
)
def make_scala_macro_library(*extras):
return rule(
attrs = _dicts.add(
_scala_macro_library_attrs,
extras_phases(extras),
*[extra["attrs"] for extra in extras if "attrs" in extra]
),
fragments = ["java"],
outputs = _dicts.add(
common_outputs,
*[extra["outputs"] for extra in extras if "outputs" in extra]
),
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_macro_library_impl,
)
scala_macro_library = make_scala_macro_library()