forked from bazelbuild/rules_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_binary_deploy_jar.bzl
More file actions
240 lines (218 loc) · 8.74 KB
/
Copy pathjava_binary_deploy_jar.bzl
File metadata and controls
240 lines (218 loc) · 8.74 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
# Copyright 2022 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.
"""Auxiliary rule to create the deploy archives for java_binary"""
load("//java/common:java_semantics.bzl", "semantics")
load(":java_helper.bzl", "helper")
# copybara: default visibility
def _get_build_info(ctx, stamp):
if helper.is_stamping_enabled(ctx, stamp):
# Makes the target depend on BUILD_INFO_KEY, which helps to discover stamped targets
# See b/326620485 for more details.
ctx.version_file # buildifier: disable=no-effect
return ctx.attr._build_info_translator[OutputGroupInfo].non_redacted_build_info_files.to_list()
else:
return ctx.attr._build_info_translator[OutputGroupInfo].redacted_build_info_files.to_list()
def create_deploy_archives(
ctx,
java_attrs,
launcher_info,
main_class,
coverage_main_class,
strip_as_default,
hermetic = False,
add_exports = depset(),
add_opens = depset(),
one_version_level = "OFF",
one_version_allowlist = None,
extra_args = [],
extra_manifest_lines = []):
""" Registers actions for _deploy.jar and _deploy.jar.unstripped
Args:
ctx: (RuleContext) The rule context
java_attrs: (Struct) Struct of (classpath_resources, runtime_jars, runtime_classpath_for_archive, resources)
launcher_info: (Struct) Struct of (runtime_jars, launcher, unstripped_launcher)
main_class: (String) FQN of the entry point for execution
coverage_main_class: (String) FQN of the entry point for coverage collection
strip_as_default: (bool) Whether to create unstripped deploy jar
hermetic: (bool)
add_exports: (depset)
add_opens: (depset)
one_version_level: (String) Optional one version check level, default OFF
one_version_allowlist: (File) Optional allowlist for one version check
extra_args: (list[Args]) Optional arguments for the deploy jar action
extra_manifest_lines: (list[String]) Optional lines added to the jar manifest
"""
classpath_resources = java_attrs.classpath_resources
runtime_classpath = depset(
direct = launcher_info.runtime_jars,
transitive = [
java_attrs.runtime_jars,
java_attrs.runtime_classpath_for_archive,
],
order = "preorder",
)
multi_release = ctx.fragments.java.multi_release_deploy_jars
build_info_files = _get_build_info(ctx, ctx.attr.stamp)
build_target = str(ctx.label)
manifest_lines = ctx.attr.deploy_manifest_lines + extra_manifest_lines
create_deploy_archive(
ctx,
launcher_info.launcher,
main_class,
coverage_main_class,
java_attrs.resources,
classpath_resources,
runtime_classpath,
manifest_lines,
build_info_files,
build_target,
output = ctx.outputs.deployjar,
one_version_level = one_version_level,
one_version_allowlist = one_version_allowlist,
multi_release = multi_release,
hermetic = hermetic,
add_exports = add_exports,
add_opens = add_opens,
extra_args = extra_args,
)
if strip_as_default:
create_deploy_archive(
ctx,
launcher_info.unstripped_launcher,
main_class,
coverage_main_class,
java_attrs.resources,
classpath_resources,
runtime_classpath,
manifest_lines,
build_info_files,
build_target,
output = ctx.outputs.unstrippeddeployjar,
multi_release = multi_release,
hermetic = hermetic,
add_exports = add_exports,
add_opens = add_opens,
extra_args = extra_args,
)
else:
ctx.actions.write(ctx.outputs.unstrippeddeployjar, "")
def create_deploy_archive(
ctx,
launcher,
main_class,
coverage_main_class,
resources,
classpath_resources,
runtime_classpath,
manifest_lines,
build_info_files,
build_target,
output,
one_version_level = "OFF",
one_version_allowlist = None,
multi_release = False,
hermetic = False,
add_exports = [],
add_opens = [],
extra_args = []):
""" Creates a deploy jar
Requires a Java runtime toolchain if and only if hermetic is True.
Args:
ctx: (RuleContext) The rule context
launcher: (File) the launcher artifact
main_class: (String) FQN of the entry point for execution
coverage_main_class: (String) FQN of the entry point for coverage collection
resources: (Depset) resource inputs
classpath_resources: (Depset) classpath resource inputs
runtime_classpath: (Depset) source files to add to the jar
build_target: (String) Name of the build target for stamping
manifest_lines: (list[String]) Optional lines added to the jar manifest
build_info_files: (list[File]) build info files for stamping
build_target: (String) the owner build target label name string
output: (File) the output jar artifact
one_version_level: (String) Optional one version check level, default OFF
one_version_allowlist: (File) Optional allowlist for one version check
multi_release: (bool)
hermetic: (bool)
add_exports: (depset)
add_opens: (depset)
extra_args: (list[Args]) Optional arguments for the deploy jar action
"""
input_files = []
input_files.extend(build_info_files)
transitive_input_files = [
resources,
classpath_resources,
runtime_classpath,
]
single_jar = semantics.find_java_toolchain(ctx).single_jar
manifest_lines = list(manifest_lines)
if ctx.configuration.coverage_enabled:
manifest_lines.append("Coverage-Main-Class: %s" % coverage_main_class)
args = ctx.actions.args()
args.set_param_file_format("shell").use_param_file("@%s", use_always = True)
args.add("--output", output)
args.add("--build_target", build_target)
args.add("--normalize")
args.add("--compression")
if main_class:
args.add("--main_class", main_class)
args.add_all("--deploy_manifest_lines", manifest_lines)
args.add_all(build_info_files, before_each = "--build_info_file")
if launcher:
input_files.append(launcher)
args.add("--java_launcher", launcher)
args.add_all("--classpath_resources", classpath_resources)
args.add_all(
"--sources",
runtime_classpath,
map_each = helper.jar_and_target_arg_mapper,
)
if one_version_level != "OFF" and one_version_allowlist:
input_files.append(one_version_allowlist)
args.add("--enforce_one_version")
args.add("--one_version_allowlist", one_version_allowlist)
if one_version_level == "WARNING":
args.add("--succeed_on_found_violations")
if multi_release:
args.add("--multi_release")
if hermetic:
runtime = ctx.toolchains["@//tools/jdk/hermetic:hermetic_runtime_toolchain_type"].java_runtime
if runtime.lib_modules != None:
java_home = runtime.java_home
lib_modules = runtime.lib_modules
hermetic_files = runtime.hermetic_files
default_cds = runtime.default_cds
args.add("--hermetic_java_home", java_home)
args.add("--jdk_lib_modules", lib_modules)
args.add_all("--resources", hermetic_files)
input_files.append(lib_modules)
transitive_input_files.append(hermetic_files)
if default_cds:
input_files.append(default_cds)
args.add("--cds_archive", default_cds)
args.add_all("--add_exports", add_exports)
args.add_all("--add_opens", add_opens)
inputs = depset(input_files, transitive = transitive_input_files)
ctx.actions.run(
mnemonic = "JavaDeployJar",
progress_message = "Building deploy jar %s" % output.short_path,
executable = single_jar,
inputs = inputs,
tools = [single_jar],
outputs = [output],
arguments = [args] + extra_args,
use_default_shell_env = True,
toolchain = semantics.JAVA_TOOLCHAIN_TYPE,
)