forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpy_zipapp_rule.bzl
More file actions
379 lines (330 loc) · 13.4 KB
/
py_zipapp_rule.bzl
File metadata and controls
379 lines (330 loc) · 13.4 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
"""Implementation of the zipapp rules."""
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config")
load("//python/private:attributes.bzl", "apply_config_settings_attr")
load("//python/private:builders.bzl", "builders")
load("//python/private:common.bzl", "BUILTIN_BUILD_PYTHON_ZIP", "actions_run", "create_windows_exe_launcher", "maybe_builtin_build_python_zip", "maybe_create_repo_mapping", "runfiles_root_path", "target_platform_has_any_constraint")
load("//python/private:common_labels.bzl", "labels")
load("//python/private:py_executable_info.bzl", "PyExecutableInfo")
load("//python/private:py_internal.bzl", "py_internal")
load("//python/private:py_runtime_info.bzl", "PyRuntimeInfo")
load("//python/private:toolchain_types.bzl", "EXEC_TOOLS_TOOLCHAIN_TYPE", "LAUNCHER_MAKER_TOOLCHAIN_TYPE")
load("//python/private:transition_labels.bzl", "TRANSITION_LABELS")
def _is_symlink(f):
if hasattr(f, "is_symlink"):
return str(int(f.is_symlink))
else:
return "-1"
def _create_zipapp_main_py(ctx, py_runtime, py_executable, stage2_bootstrap):
venv_python_exe = py_executable.venv_python_exe
if venv_python_exe:
venv_python_exe_path = runfiles_root_path(ctx, venv_python_exe.short_path)
else:
venv_python_exe_path = ""
if py_runtime.interpreter:
python_binary_actual_path = runfiles_root_path(ctx, py_runtime.interpreter.short_path)
else:
python_binary_actual_path = py_runtime.interpreter_path
zip_main_py = ctx.actions.declare_file(ctx.label.name + ".zip_main.py")
ctx.actions.expand_template(
template = py_runtime.zip_main_template,
output = zip_main_py,
substitutions = {
"%python_binary%": venv_python_exe_path,
"%python_binary_actual%": python_binary_actual_path,
"%stage2_bootstrap%": runfiles_root_path(ctx, stage2_bootstrap.short_path),
"%workspace_name%": ctx.workspace_name,
"%EXTRACT_DIR%": paths.join(
(ctx.label.repo_name or "_main"),
ctx.label.package,
ctx.label.name,
),
},
)
return zip_main_py
def _map_zip_empty_filenames(list_paths_cb):
return ["rf-empty|" + path for path in list_paths_cb().to_list()]
def _map_zip_runfiles(file):
return "rf-file|" + _is_symlink(file) + "|" + file.short_path + "|" + file.path
def _map_zip_symlinks(entry):
return "rf-symlink|" + _is_symlink(entry.target_file) + "|" + entry.path + "|" + entry.target_file.path
def _map_zip_root_symlinks(entry):
return "rf-root-symlink|" + _is_symlink(entry.target_file) + "|" + entry.path + "|" + entry.target_file.path
def _build_manifest(ctx, manifest, runfiles, zip_main):
manifest.add("regular|0|__main__.py|{}".format(zip_main.path))
manifest.add_all(
# NOTE: Accessing runfiles.empty_filenames materializes them. A lambda
# is used to defer that.
[lambda: runfiles.empty_filenames],
map_each = _map_zip_empty_filenames,
allow_closure = True,
)
manifest.add_all(runfiles.files, map_each = _map_zip_runfiles)
manifest.add_all(runfiles.symlinks, map_each = _map_zip_symlinks)
manifest.add_all(runfiles.root_symlinks, map_each = _map_zip_root_symlinks)
inputs = [zip_main]
zip_repo_mapping_manifest = maybe_create_repo_mapping(
ctx = ctx,
runfiles = runfiles,
)
if zip_repo_mapping_manifest:
# NOTE: rf-root-symlink is used to make it show up under the runfiles
# subdirectory within the zip.
manifest.add(
zip_repo_mapping_manifest.path,
format = "rf-root-symlink|0|_repo_mapping|%s",
)
inputs.append(zip_repo_mapping_manifest)
return inputs
def _create_zip(ctx, py_runtime, py_executable, stage2_bootstrap):
output = ctx.actions.declare_file(ctx.label.name + ".zip")
manifest = ctx.actions.args()
manifest.use_param_file("%s", use_always = True)
manifest.set_param_file_format("multiline")
runfiles = builders.RunfilesBuilder()
runfiles.add(py_runtime.files)
if py_executable.venv_python_exe:
runfiles.add(py_executable.venv_python_exe)
runfiles.add(py_executable.app_runfiles)
runfiles.add(stage2_bootstrap)
runfiles = runfiles.build(ctx)
zip_main = _create_zipapp_main_py(ctx, py_runtime, py_executable, stage2_bootstrap)
inputs = _build_manifest(ctx, manifest, runfiles, zip_main)
zipper_args = ctx.actions.args()
zipper_args.add(output)
zipper_args.add(ctx.workspace_name, format = "--workspace-name=%s")
zipper_args.add(
str(int(py_internal.get_legacy_external_runfiles(ctx))),
format = "--legacy-external-runfiles=%s",
)
if ctx.attr.compression:
zipper_args.add(ctx.attr.compression, format = "--compression=%s")
zipper_args.add("--runfiles-dir=runfiles")
actions_run(
ctx,
executable = ctx.attr._zipper,
arguments = [manifest, zipper_args],
inputs = depset(inputs, transitive = [runfiles.files]),
outputs = [output],
mnemonic = "PyZipAppCreateZip",
progress_message = "Reticulating zipapp archive: %{label} into %{output}",
)
return output
def _create_shell_bootstrap(ctx, py_runtime, py_executable, stage2_bootstrap):
preamble = ctx.actions.declare_file(ctx.label.name + ".preamble.sh")
bundled_pyexe_path = ""
external_pyexe_path = ""
if py_runtime.interpreter_path:
external_pyexe_path = py_runtime.interpreter_path
else:
bundled_pyexe_path = runfiles_root_path(ctx, py_runtime.interpreter.short_path)
substitutions = {
"%BUNDLED_PYEXE_PATH%": bundled_pyexe_path,
"%EXTERNAL_PYEXE_PATH%": external_pyexe_path,
"%EXTRACT_DIR%": paths.join(
(ctx.label.repo_name or "_main"),
ctx.label.package,
ctx.label.name,
),
"%INTERPRETER_ARGS%": "\n".join([
'"{}"'.format(v)
for v in py_executable.interpreter_args
]),
"%STAGE2_BOOTSTRAP%": runfiles_root_path(ctx, stage2_bootstrap.short_path),
}
ctx.actions.expand_template(
template = ctx.file._zip_shell_template,
output = preamble,
substitutions = substitutions,
is_executable = True,
)
return preamble
def _create_self_executable_zip(ctx, preamble, zip_file):
pyz = ctx.actions.declare_file(ctx.label.name + ".pyz")
args = ctx.actions.args()
args.add(preamble)
args.add(zip_file)
args.add(pyz)
actions_run(
ctx,
executable = ctx.attr._exe_zip_maker,
arguments = [args],
inputs = depset([preamble, zip_file]),
outputs = [pyz],
mnemonic = "PyZipAppCreateExecutableZip",
progress_message = "Reticulating zipapp executable: %{label} into %{output}",
)
return pyz
def _py_zipapp_executable_impl(ctx):
py_executable = ctx.attr.binary[PyExecutableInfo]
py_runtime = ctx.attr.binary[PyRuntimeInfo]
stage2_bootstrap = py_executable.stage2_bootstrap
zip_file = _create_zip(ctx, py_runtime, py_executable, stage2_bootstrap)
if ctx.attr.executable:
if target_platform_has_any_constraint(ctx, ctx.attr._windows_constraints):
executable = ctx.actions.declare_file(ctx.label.name + ".exe")
python_exe = py_executable.venv_python_exe
if python_exe:
python_exe_path = runfiles_root_path(ctx, python_exe.short_path)
elif py_runtime.interpreter:
python_exe_path = runfiles_root_path(ctx, py_runtime.interpreter.short_path)
else:
python_exe_path = py_runtime.interpreter_path
create_windows_exe_launcher(
ctx,
output = executable,
python_binary_path = python_exe_path,
use_zip_file = True,
)
default_outputs = [executable, zip_file]
else:
preamble = _create_shell_bootstrap(ctx, py_runtime, py_executable, stage2_bootstrap)
executable = _create_self_executable_zip(ctx, preamble, zip_file)
default_outputs = [executable]
else:
# Bazel requires executable=True rules to have an executable given, so give
# a fake one to satisfy that.
default_outputs = [zip_file]
executable = ctx.actions.declare_file(ctx.label.name + "-not-executable")
ctx.actions.write(executable, "echo 'ERROR: Non executable zip file'; exit 1")
return [
DefaultInfo(
files = depset(default_outputs),
runfiles = ctx.runfiles(files = default_outputs),
executable = executable,
),
OutputGroupInfo(
python_zip_file = depset([zip_file]),
),
]
def _transition_zipapp_impl(settings, attr):
settings = apply_config_settings_attr(dict(settings), attr)
# Force this to false, otherwise the binary is already a zipapp
settings[labels.BUILD_PYTHON_ZIP] = False
maybe_builtin_build_python_zip("false", settings)
return settings
_zipapp_transition = transition(
implementation = _transition_zipapp_impl,
inputs = TRANSITION_LABELS,
outputs = TRANSITION_LABELS + [
labels.BUILD_PYTHON_ZIP,
] + BUILTIN_BUILD_PYTHON_ZIP,
)
_ATTRS = {
"binary": attr.label(
doc = """
A `py_binary` or `py_test` (or equivalent) target to package.
""",
providers = [PyExecutableInfo, PyRuntimeInfo],
mandatory = True,
),
"compression": attr.string(
doc = """
The compression level to use.
Typically 0 to 9, with higher numbers being to compress more.
""",
default = "",
),
"config_settings": attr.label_keyed_string_dict(
doc = """
Config settings to change for this target.
The keys are labels for settings, and the values are strings for the new value
to use. Pass `Label` objects or canonical label strings for the keys to ensure
they resolve as expected (canonical labels start with `@@` and can be
obtained by calling `str(Label(...))`).
Most `@rules_python//python/config_setting` settings can be used here, which
allows, for example, making only a certain `py_binary` use
{obj}`--bootstrap_impl=script`.
Additional or custom config settings can be registered using the
{obj}`add_transition_setting` API. This allows, for example, forcing a
particular CPU, or defining a custom setting that `select()` uses elsewhere
to pick between `pip.parse` hubs. See the [How to guide on multiple
versions of a library] for a more concrete example.
:::{note}
These values are transitioned on, so will affect the analysis graph and the
associated memory overhead. The more unique configurations in your overall
build, the more memory and (often unnecessary) re-analysis and re-building
can occur. See
https://bazel.build/extending/config#memory-performance-considerations for
more information about risks and considerations.
:::
""",
),
"executable": attr.bool(
doc = """
Whether the output should be an executable zip file.
""",
default = True,
),
# Required to opt-in to the transition feature.
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
"_exe_zip_maker": attr.label(
cfg = "exec",
default = "//tools/private/zipapp:exe_zip_maker",
),
"_launcher": attr.label(
cfg = "target",
# NOTE: This is an executable, but is only used for Windows. It
# can't have executable=True because the backing target is an
# empty target for other platforms.
default = "//tools/launcher:launcher",
),
"_windows_constraints": attr.label_list(
default = [
"@platforms//os:windows",
],
),
"_zip_shell_template": attr.label(
default = ":zip_shell_template",
allow_single_file = True,
),
"_zipper": attr.label(
cfg = "exec",
default = "//tools/private/zipapp:zipper",
),
} | ({
"_windows_launcher_maker": attr.label(
default = "@bazel_tools//tools/launcher:launcher_maker",
cfg = "exec",
executable = True,
),
} if not rp_config.bazel_9_or_later else {})
_TOOLCHAINS = [EXEC_TOOLS_TOOLCHAIN_TYPE] + ([LAUNCHER_MAKER_TOOLCHAIN_TYPE] if rp_config.bazel_9_or_later else [])
_COMMON_RULE_DOC = """
Output groups:
* `python_zip_file`: (*deprecated*) The plain, non-self-executable zipapp zipfile.
*This output group is deprecated and retained for compatibility with
the previous implicit zipapp functionality. Set `executable=False`
and use the default output of the target instead.*
:::{versionadded} 1.9.0
:::
""".lstrip()
py_zipapp_binary = rule(
doc = """
Packages a `py_binary` as a Python zipapp.
{}
""".format(_COMMON_RULE_DOC),
implementation = _py_zipapp_executable_impl,
attrs = _ATTRS,
# NOTE: While this is marked executable, it is conditionally executable
# based on the `executable` attribute.
executable = True,
toolchains = _TOOLCHAINS,
cfg = _zipapp_transition,
)
py_zipapp_test = rule(
doc = """
Packages a `py_test` as a Python zipapp.
This target is also a valid test target to run.
{}
""".format(_COMMON_RULE_DOC),
implementation = _py_zipapp_executable_impl,
attrs = _ATTRS,
# NOTE: While this is marked as a test, it is conditionally executable
# based on the `executable` attribute.
test = True,
toolchains = _TOOLCHAINS,
cfg = _zipapp_transition,
)