-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathjava_common.bzl
More file actions
333 lines (292 loc) · 11.1 KB
/
Copy pathjava_common.bzl
File metadata and controls
333 lines (292 loc) · 11.1 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Utilities for Java compilation support in Starlark. """
load("@bazel_skylib//lib:paths.bzl", "paths")
load("//java/common:java_semantics.bzl", "semantics")
load("//java/common/rules:java_helper.bzl", "helper")
load("//java/common/rules:java_runtime.bzl", "JavaRuntimeInfo")
load("//java/common/rules:java_toolchain.bzl", "JavaToolchainInfo")
load(":boot_class_path_info.bzl", "BootClassPathInfo")
load(
":java_common_internal.bzl",
_compile_header_internal = "compile_header",
_compile_internal = "compile",
_run_ijar_internal = "run_ijar",
)
load(
":java_info.bzl",
"JavaInfo",
"JavaPluginInfo",
_java_info_make_non_strict = "make_non_strict",
_java_info_merge = "merge",
_java_info_set_annotation_processing = "set_annotation_processing",
)
load(":message_bundle_info.bzl", "MessageBundleInfo")
load(":native.bzl", "get_internal_java_common")
# copybara: default visibility
JavaRuntimeClasspathInfo = provider(
"Provider for the runtime classpath contributions of a Java binary.",
fields = ["runtime_classpath"],
)
def _compile(
ctx,
output,
java_toolchain,
source_jars = [],
source_files = [],
output_source_jar = None,
javac_opts = [],
deps = [],
runtime_deps = [],
exports = [],
plugins = [],
exported_plugins = [],
native_libraries = [],
annotation_processor_additional_inputs = [],
annotation_processor_additional_outputs = [],
strict_deps = "ERROR",
bootclasspath = None,
sourcepath = [],
resources = [],
neverlink = False,
enable_annotation_processing = True):
return _compile_internal(
ctx,
output = output,
java_toolchain = java_toolchain,
source_jars = source_jars,
source_files = source_files,
output_source_jar = output_source_jar,
javac_opts = javac_opts,
deps = deps,
runtime_deps = runtime_deps,
exports = exports,
plugins = plugins,
exported_plugins = exported_plugins,
native_libraries = native_libraries,
annotation_processor_additional_inputs = annotation_processor_additional_inputs,
annotation_processor_additional_outputs = annotation_processor_additional_outputs,
strict_deps = strict_deps,
bootclasspath = bootclasspath,
sourcepath = sourcepath,
resources = resources,
neverlink = neverlink,
enable_annotation_processing = enable_annotation_processing,
)
def _compile_header(
ctx,
output,
java_toolchain,
source_jars = [],
source_files = [],
javac_opts = [],
deps = [],
plugins = [],
strict_deps = "ERROR",
bootclasspath = None,
enable_annotation_processing = True):
return _compile_header_internal(
ctx,
output = output,
java_toolchain = java_toolchain,
source_jars = source_jars,
source_files = source_files,
javac_opts = javac_opts,
deps = deps,
plugins = plugins,
strict_deps = strict_deps,
bootclasspath = bootclasspath,
enable_annotation_processing = enable_annotation_processing,
)
def _run_ijar(actions, jar, java_toolchain, target_label = None):
get_internal_java_common().check_java_toolchain_is_declared_on_rule(actions)
return _run_ijar_internal(
actions = actions,
jar = jar,
java_toolchain = java_toolchain,
target_label = target_label,
)
def _stamp_jar(actions, jar, java_toolchain, target_label):
"""Stamps a jar with a target label for <code>add_dep</code> support.
The return value is typically passed to `JavaInfo.compile_jar`. Prefer to use `run_ijar` when
possible.
Args:
actions: (actions) ctx.actions
jar: (File) The jar to run stamp_jar on.
java_toolchain: (JavaToolchainInfo) The toolchain to used to find the stamp_jar tool.
target_label: (Label) A target label to stamp the jar with. Used for `add_dep` support.
Typically, you would pass `ctx.label` to stamp the jar with the current rule's label.
Returns:
(File) The output artifact
"""
get_internal_java_common().check_java_toolchain_is_declared_on_rule(actions)
output = actions.declare_file(paths.replace_extension(jar.basename, "-stamped.jar"), sibling = jar)
args = actions.args()
args.add(jar)
args.add(output)
args.add("--nostrip_jar")
args.add("--target_label", target_label)
actions.run(
mnemonic = "JavaIjar",
inputs = [jar],
outputs = [output],
executable = java_toolchain.ijar, # ijar doubles as a stamping tool
arguments = [args],
progress_message = "Stamping target label into jar %{input}",
toolchain = semantics.JAVA_TOOLCHAIN_TYPE,
use_default_shell_env = True,
)
return output
def _pack_sources(
actions,
java_toolchain,
output_source_jar,
sources = [],
source_jars = []):
"""Packs sources and source jars into a single source jar file.
The return value is typically passed to `JavaInfo.source_jar`.
Args:
actions: (actions) ctx.actions
java_toolchain: (JavaToolchainInfo) The toolchain used to find the ijar tool.
output_source_jar: (File) The output source jar.
sources: ([File]) A list of Java source files to be packed into the source jar.
source_jars: ([File]) A list of source jars to be packed into the source jar.
Returns:
(File) The output artifact
"""
get_internal_java_common().check_java_toolchain_is_declared_on_rule(actions)
return helper.create_single_jar(
actions,
toolchain = java_toolchain,
output = output_source_jar,
sources = depset(source_jars),
resources = depset(sources),
progress_message = "Building source jar %{output}",
mnemonic = "JavaSourceJar",
)
# TODO: b/78512644 - migrate callers to passing explicit javacopts or using custom toolchains, and delete
def _default_javac_opts(java_toolchain):
"""Experimental! Get default javacopts from a java toolchain
Args:
java_toolchain: (JavaToolchainInfo) the toolchain from which to get the javac options.
Returns:
([str]) A list of javac options
"""
return java_toolchain._javacopts_list
# temporary for migration
def _default_javac_opts_depset(java_toolchain):
"""Experimental! Get default javacopts from a java toolchain
Args:
java_toolchain: (JavaToolchainInfo) the toolchain from which to get the javac options.
Returns:
(depset[str]) A depset of javac options that should be tokenized before passing to javac
"""
return java_toolchain._javacopts
def _merge(providers):
"""Merges the given providers into a single JavaInfo.
Args:
providers: ([JavaInfo]) The list of providers to merge.
Returns:
(JavaInfo) The merged JavaInfo
"""
return _java_info_merge(providers)
def _make_non_strict(java_info):
"""Returns a new JavaInfo instance whose direct-jars part is the union of both the direct and indirect jars of the given Java provider.
Args:
java_info: (JavaInfo) The java info to make non-strict.
Returns:
(JavaInfo)
"""
return _java_info_make_non_strict(java_info)
def _get_message_bundle_info():
return None if semantics.IS_BAZEL else MessageBundleInfo
def _get_constraints(java_info):
"""Returns a set of constraints added.
Args:
java_info: (JavaInfo) The JavaInfo to get constraints from.
Returns:
([str]) The constraints set on the supplied JavaInfo
"""
return [] if semantics.IS_BAZEL else java_info._constraints
def _set_annotation_processing(
java_info,
enabled = False,
processor_classnames = [],
processor_classpath = None,
class_jar = None,
source_jar = None):
"""Returns a copy of the given JavaInfo with the given annotation_processing info.
Args:
java_info: (JavaInfo) The JavaInfo to enhance.
enabled: (bool) Whether the rule uses annotation processing.
processor_classnames: ([str]) Class names of annotation processors applied.
processor_classpath: (depset[File]) Class names of annotation processors applied.
class_jar: (File) Optional. Jar that is the result of annotation processing.
source_jar: (File) Optional. Source archive resulting from annotation processing.
Returns:
(JavaInfo)
"""
if semantics.IS_BAZEL:
return None
return _java_info_set_annotation_processing(
java_info,
enabled = enabled,
processor_classnames = processor_classnames,
processor_classpath = processor_classpath,
class_jar = class_jar,
source_jar = source_jar,
)
def _java_toolchain_label(java_toolchain):
"""Returns the toolchain's label.
Args:
java_toolchain: (JavaToolchainInfo) The toolchain.
Returns:
(Label)
"""
if semantics.IS_BAZEL:
# No implementation in Bazel. This method is not callable in Starlark except through
# (discouraged) use of --experimental_google_legacy_api.
return None
get_internal_java_common().check_provider_instances([java_toolchain], "java_toolchain", JavaToolchainInfo)
return java_toolchain.label
def _make_java_common():
methods = {
"provider": JavaInfo,
"compile": _compile,
"compile_header": _compile_header,
"run_ijar": _run_ijar,
"stamp_jar": _stamp_jar,
"pack_sources": _pack_sources,
"default_javac_opts": _default_javac_opts,
"default_javac_opts_depset": _default_javac_opts_depset,
"merge": _merge,
"JavaPluginInfo": JavaPluginInfo,
"JavaToolchainInfo": JavaToolchainInfo,
"JavaRuntimeInfo": JavaRuntimeInfo,
"BootClassPathInfo": BootClassPathInfo,
"JavaRuntimeClasspathInfo": JavaRuntimeClasspathInfo,
}
if get_internal_java_common().google_legacy_api_enabled():
methods.update(
MessageBundleInfo = _get_message_bundle_info(), # struct field that is None in bazel
get_constraints = _get_constraints,
set_annotation_processing = _set_annotation_processing,
java_toolchain_label = _java_toolchain_label,
)
if semantics.IS_BAZEL:
methods.update(
make_non_strict = _make_non_strict,
)
return struct(**methods)
java_common = _make_java_common()