generated from bazel-contrib/rules-template
-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathpy_binary.bzl
208 lines (184 loc) · 8.06 KB
/
py_binary.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
"""Implementation for the py_binary and py_test rules."""
load("@aspect_bazel_lib//lib:expand_make_vars.bzl", "expand_locations", "expand_variables")
load("@aspect_bazel_lib//lib:paths.bzl", "BASH_RLOCATION_FUNCTION", "to_rlocation_path")
load("@rules_python//python:defs.bzl", "PyInfo")
load("//py/private:py_library.bzl", _py_library = "py_library_utils")
load("//py/private:py_semantics.bzl", _py_semantics = "semantics")
load("//py/private/toolchain:types.bzl", "PY_TOOLCHAIN", "VENV_TOOLCHAIN")
def _dict_to_exports(env):
return [
"export %s=\"%s\"" % (k, v)
for (k, v) in env.items()
]
def _py_binary_rule_impl(ctx):
venv_toolchain = ctx.toolchains[VENV_TOOLCHAIN]
py_toolchain = _py_semantics.resolve_toolchain(ctx)
# Check for duplicate virtual dependency names. Those that map to the same resolution target would have been merged by the depset for us.
virtual_resolution = _py_library.resolve_virtuals(ctx)
imports_depset = _py_library.make_imports_depset(ctx, extra_imports_depsets = virtual_resolution.imports)
pth_lines = ctx.actions.args()
pth_lines.use_param_file("%s", use_always = True)
pth_lines.set_param_file_format("multiline")
# The venv is created at the root in the runfiles tree, in 'VENV_NAME', the full path is "${RUNFILES_DIR}/${VENV_NAME}",
# but depending on if we are running as the top level binary or a tool, then $RUNFILES_DIR may be absolute or relative.
# Paths in the .pth are relative to the site-packages folder where they reside.
# All "import" paths from `py_library` start with the workspace name, so we need to go back up the tree for
# each segment from site-packages in the venv to the root of the runfiles tree.
# Five .. will get us back to the root of the venv:
# {name}.runfiles/.{name}.venv/lib/python{version}/site-packages/first_party.pth
escape = "/".join(([".."] * 4))
# A few imports rely on being able to reference the root of the runfiles tree as a Python module,
# the common case here being the @rules_python//python/runfiles target that adds the runfiles helper,
# which ends up in bazel_tools/tools/python/runfiles/runfiles.py, but there are no imports attrs that hint we
# should be adding the root to the PYTHONPATH
# Maybe in the future we can opt out of this?
pth_lines.add(escape)
pth_lines.add_all(imports_depset, format_each = "{}/%s".format(escape))
site_packages_pth_file = ctx.actions.declare_file("{}.venv.pth".format(ctx.attr.name))
ctx.actions.write(
output = site_packages_pth_file,
content = pth_lines,
)
default_env = {
"BAZEL_TARGET": str(ctx.label).lstrip("@"),
"BAZEL_WORKSPACE": ctx.workspace_name,
"BAZEL_TARGET_NAME": ctx.attr.name,
}
passed_env = dict(ctx.attr.env)
for k, v in passed_env.items():
passed_env[k] = expand_variables(
ctx,
expand_locations(ctx, v, ctx.attr.data),
attribute_name = "env",
)
executable_launcher = ctx.actions.declare_file(ctx.attr.name)
ctx.actions.expand_template(
template = ctx.file._run_tmpl,
output = executable_launcher,
substitutions = {
"{{BASH_RLOCATION_FN}}": BASH_RLOCATION_FUNCTION,
"{{INTERPRETER_FLAGS}}": " ".join(py_toolchain.flags),
"{{VENV_TOOL}}": to_rlocation_path(ctx, venv_toolchain.bin),
"{{ARG_COLLISION_STRATEGY}}": ctx.attr.package_collisions,
"{{ARG_PYTHON}}": to_rlocation_path(ctx, py_toolchain.python) if py_toolchain.runfiles_interpreter else py_toolchain.python.path,
"{{ARG_VENV_NAME}}": ".{}.venv".format(ctx.attr.name),
"{{ARG_PTH_FILE}}": to_rlocation_path(ctx, site_packages_pth_file),
"{{ENTRYPOINT}}": to_rlocation_path(ctx, ctx.file.main),
"{{PYTHON_ENV}}": "\n".join(_dict_to_exports(default_env)).strip(),
"{{EXEC_PYTHON_BIN}}": "python{}".format(
py_toolchain.interpreter_version_info.major,
),
"{{RUNFILES_INTERPRETER}}": str(py_toolchain.runfiles_interpreter).lower(),
},
is_executable = True,
)
srcs_depset = _py_library.make_srcs_depset(ctx)
runfiles = _py_library.make_merged_runfiles(
ctx,
extra_depsets = [
py_toolchain.files,
srcs_depset,
] + virtual_resolution.srcs + virtual_resolution.runfiles,
extra_runfiles = [
site_packages_pth_file,
] + ctx.files._runfiles_lib,
extra_runfiles_depsets = [
venv_toolchain.default_info.default_runfiles,
],
)
instrumented_files_info = _py_library.make_instrumented_files_info(
ctx,
extra_source_attributes = ["main"],
)
return [
DefaultInfo(
files = depset([executable_launcher]),
executable = executable_launcher,
runfiles = runfiles,
),
PyInfo(
imports = imports_depset,
transitive_sources = srcs_depset,
has_py2_only_sources = False,
has_py3_only_sources = True,
uses_shared_libraries = False,
),
instrumented_files_info,
RunEnvironmentInfo(
environment = passed_env,
),
]
_attrs = dict({
"env": attr.string_dict(
doc = "Environment variables to set when running the binary.",
default = {},
),
"main": attr.label(
doc = "Script to execute with the Python interpreter.",
allow_single_file = True,
mandatory = True,
),
"python_version": attr.string(
doc = """Whether to build this target and its transitive deps for a specific python version.""",
),
"package_collisions": attr.string(
doc = """The action that should be taken when a symlink collision is encountered when creating the venv.
A collision can occour when multiple packages providing the same file are installed into the venv. The possible values are:
* "error": When conflicting symlinks are found, an error is reported and venv creation halts.
* "warning": When conflicting symlinks are found, an warning is reported, however venv creation continues.
* "ignore": When conflicting symlinks are found, no message is reported and venv creation continues.
""",
default = "error",
values = ["error", "warning", "ignore"],
),
"_run_tmpl": attr.label(
allow_single_file = True,
default = "//py/private:run.tmpl.sh",
),
"_runfiles_lib": attr.label(
default = "@bazel_tools//tools/bash/runfiles",
),
# NB: this is read by _resolve_toolchain in py_semantics.
"_interpreter_version_flag": attr.label(
default = "//py:interpreter_version",
),
# Required for py_version attribute
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
})
_attrs.update(**_py_library.attrs)
def _python_version_transition_impl(_, attr):
if not attr.python_version:
return {}
return {"@rules_python//python/config_settings:python_version": str(attr.python_version)}
_python_version_transition = transition(
implementation = _python_version_transition_impl,
inputs = [],
outputs = ["@rules_python//python/config_settings:python_version"],
)
py_base = struct(
implementation = _py_binary_rule_impl,
attrs = _attrs,
toolchains = [
PY_TOOLCHAIN,
VENV_TOOLCHAIN,
],
cfg = _python_version_transition,
)
py_binary = rule(
doc = "Run a Python program under Bazel. Most users should use the [py_binary macro](#py_binary) instead of loading this directly.",
implementation = py_base.implementation,
attrs = py_base.attrs,
toolchains = py_base.toolchains,
executable = True,
cfg = py_base.cfg,
)
py_test = rule(
doc = "Run a Python program under Bazel. Most users should use the [py_test macro](#py_test) instead of loading this directly.",
implementation = py_base.implementation,
attrs = py_base.attrs,
toolchains = py_base.toolchains,
test = True,
cfg = py_base.cfg,
)