forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_library.bzl
More file actions
381 lines (332 loc) · 13.9 KB
/
Copy pathpy_library.bzl
File metadata and controls
381 lines (332 loc) · 13.9 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
380
381
# 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.
"""Common code for implementing py_library rules."""
load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load(":attr_builders.bzl", "attrb")
load(
":attributes.bzl",
"COMMON_ATTRS",
"IMPORTS_ATTRS",
"PY_SRCS_ATTRS",
"PrecompileAttr",
"REQUIRED_EXEC_GROUP_BUILDERS",
)
load(":builders.bzl", "builders")
load(
":common.bzl",
"PYTHON_FILE_EXTENSIONS",
"collect_cc_info",
"collect_imports",
"collect_runfiles",
"create_instrumented_files_info",
"create_library_semantics_struct",
"create_output_group_info",
"create_py_info",
"filter_to_py_srcs",
"get_imports",
"runfiles_root_path",
)
load(":flags.bzl", "AddSrcsToRunfilesFlag", "PrecompileFlag", "VenvsSitePackages")
load(":normalize_name.bzl", "normalize_name")
load(":precompile.bzl", "maybe_precompile")
load(":py_cc_link_params_info.bzl", "PyCcLinkParamsInfo")
load(":py_info.bzl", "PyInfo", "VenvSymlinkEntry", "VenvSymlinkKind")
load(":py_internal.bzl", "py_internal")
load(":reexports.bzl", "BuiltinPyInfo")
load(":rule_builders.bzl", "ruleb")
load(
":toolchain_types.bzl",
"EXEC_TOOLS_TOOLCHAIN_TYPE",
TOOLCHAIN_TYPE = "TARGET_TOOLCHAIN_TYPE",
)
_py_builtins = py_internal
LIBRARY_ATTRS = dicts.add(
COMMON_ATTRS,
PY_SRCS_ATTRS,
IMPORTS_ATTRS,
{
"experimental_venvs_site_packages": lambda: attrb.Label(
doc = """
**INTERNAL ATTRIBUTE. SHOULD ONLY BE SET BY rules_python-INTERNAL CODE.**
:::{include} /_includes/experimental_api.md
:::
A flag that decides whether the library should treat its sources as a
site-packages layout.
When the flag is `yes`, then the `srcs` files are treated as a site-packages
layout that is relative to the `imports` attribute. The `imports` attribute
can have only a single element. It is a repo-relative runfiles path.
For example, in the `my/pkg/BUILD.bazel` file, given
`srcs=["site-packages/foo/bar.py"]`, specifying
`imports=["my/pkg/site-packages"]` means `foo/bar.py` is the file path
under the binary's venv site-packages directory that should be made available (i.e.
`import foo.bar` will work).
`__init__.py` files are treated specially to provide basic support for [implicit
namespace packages](
https://packaging.python.org/en/latest/guides/packaging-namespace-packages/#native-namespace-packages).
However, the *content* of the files cannot be taken into account, merely their
presence or absense. Stated another way: [pkgutil-style namespace packages](
https://packaging.python.org/en/latest/guides/packaging-namespace-packages/#pkgutil-style-namespace-packages)
won't be understood as namespace packages; they'll be seen as regular packages. This will
likely lead to conflicts with other targets that contribute to the namespace.
:::{tip}
This attributes populates {obj}`PyInfo.venv_symlinks`, which is
a topologically ordered depset. This means dependencies closer and earlier
to a consumer have precedence. See {obj}`PyInfo.venv_symlinks` for
more information.
:::
:::{versionadded} 1.4.0
:::
""",
),
"_add_srcs_to_runfiles_flag": lambda: attrb.Label(
default = "//python/config_settings:add_srcs_to_runfiles",
),
},
)
def _py_library_impl_with_semantics(ctx):
return py_library_impl(
ctx,
semantics = create_library_semantics_struct(
get_imports = get_imports,
maybe_precompile = maybe_precompile,
get_cc_info_for_library = collect_cc_info,
),
)
def py_library_impl(ctx, *, semantics):
"""Abstract implementation of py_library rule.
Args:
ctx: The rule ctx
semantics: A `LibrarySemantics` struct; see `create_library_semantics_struct`
Returns:
A list of modern providers to propagate.
"""
direct_sources = filter_to_py_srcs(ctx.files.srcs)
precompile_result = semantics.maybe_precompile(ctx, direct_sources)
required_py_files = precompile_result.keep_srcs
required_pyc_files = []
implicit_pyc_files = []
implicit_pyc_source_files = direct_sources
precompile_attr = ctx.attr.precompile
precompile_flag = ctx.attr._precompile_flag[BuildSettingInfo].value
if (precompile_attr == PrecompileAttr.ENABLED or
precompile_flag == PrecompileFlag.FORCE_ENABLED):
required_pyc_files.extend(precompile_result.pyc_files)
else:
implicit_pyc_files.extend(precompile_result.pyc_files)
default_outputs = builders.DepsetBuilder()
default_outputs.add(precompile_result.keep_srcs)
default_outputs.add(required_pyc_files)
default_outputs = default_outputs.build()
runfiles = builders.RunfilesBuilder()
if AddSrcsToRunfilesFlag.is_enabled(ctx):
runfiles.add(required_py_files)
runfiles.add(collect_runfiles(ctx))
runfiles = runfiles.build(ctx)
imports = []
venv_symlinks = []
imports, venv_symlinks = _get_imports_and_venv_symlinks(ctx, semantics)
cc_info = semantics.get_cc_info_for_library(ctx)
py_info, deps_transitive_sources, builtins_py_info = create_py_info(
ctx,
original_sources = direct_sources,
required_py_files = required_py_files,
required_pyc_files = required_pyc_files,
implicit_pyc_files = implicit_pyc_files,
implicit_pyc_source_files = implicit_pyc_source_files,
imports = imports,
venv_symlinks = venv_symlinks,
)
# TODO(b/253059598): Remove support for extra actions; https://github.com/bazelbuild/bazel/issues/16455
listeners_enabled = _py_builtins.are_action_listeners_enabled(ctx)
if listeners_enabled:
_py_builtins.add_py_extra_pseudo_action(
ctx = ctx,
dependency_transitive_python_sources = deps_transitive_sources,
)
providers = [
DefaultInfo(files = default_outputs, runfiles = runfiles),
py_info,
create_instrumented_files_info(ctx),
PyCcLinkParamsInfo(cc_info = cc_info),
create_output_group_info(py_info.transitive_sources, extra_groups = {}),
]
if builtins_py_info:
providers.append(builtins_py_info)
return providers
_DEFAULT_PY_LIBRARY_DOC = """
A library of Python code that can be depended upon.
Default outputs:
* The input Python sources
* The precompiled artifacts from the sources.
NOTE: Precompilation affects which of the default outputs are included in the
resulting runfiles. See the precompile-related attributes and flags for
more information.
:::{versionchanged} 0.37.0
Source files are no longer added to the runfiles directly.
:::
"""
def _get_distinfo_metadata(ctx):
data = ctx.files.data or []
for d in data:
# work on case insensitive FSes
if d.basename.lower() != "metadata":
continue
if d.dirname.endswith(".dist-info"):
return d
return None
def _get_imports_and_venv_symlinks(ctx, semantics):
imports = depset()
venv_symlinks = depset()
if VenvsSitePackages.is_enabled(ctx):
dist_info_metadata = _get_distinfo_metadata(ctx)
venv_symlinks = _get_venv_symlinks(
ctx,
dist_info_metadata,
)
else:
imports = collect_imports(ctx, semantics)
return imports, venv_symlinks
def _get_venv_symlinks(ctx, dist_info_metadata):
imports = ctx.attr.imports
if len(imports) == 0:
fail("When venvs_site_packages is enabled, exactly one `imports` " +
"value must be specified, got 0")
elif len(imports) > 1:
fail("When venvs_site_packages is enabled, exactly one `imports` " +
"value must be specified, got {}".format(imports))
else:
site_packages_root = imports[0]
if site_packages_root.endswith("/"):
fail("The site packages root value from `imports` cannot end in " +
"slash, got {}".format(site_packages_root))
if site_packages_root.startswith("/"):
fail("The site packages root value from `imports` cannot start with " +
"slash, got {}".format(site_packages_root))
# Append slash to prevent incorrectly prefix-string matches
site_packages_root += "/"
# We have to build a list of (runfiles path, site-packages path) pairs of
# the files to create in the consuming binary's venv site-packages directory.
# To minimize the number of files to create, we just return the paths
# to the directories containing the code of interest.
#
# However, namespace packages complicate matters: multiple
# distributions install in the same directory in site-packages. This
# works out because they don't overlap in their files. Typically, they
# install to different directories within the namespace package
# directory. Namespace package directories are simply directories
# within site-packages that *don't* have an `__init__.py` file, which
# can be arbitrarily deep. Thus, we simply have to look for the
# directories that _do_ have an `__init__.py` file and treat those as
# the path to symlink to.
repo_runfiles_dirname = None
dirs_with_init = {} # dirname -> runfile path
venv_symlinks = []
package = None
if dist_info_metadata:
# in order to be able to have replacements in the venv, we have to add a
# third value into the venv_symlinks, which would be the normalized
# package name. This allows us to ensure that we can replace the `dist-info`
# directories by checking if the package key is there.
dist_info_dir = paths.basename(dist_info_metadata.dirname)
package, _, _suffix = dist_info_dir.rpartition(".dist-info")
package, _, _version = package.rpartition("-")
package = normalize_name(package)
repo_runfiles_dirname = runfiles_root_path(ctx, dist_info_metadata.short_path).partition("/")[0]
venv_symlinks.append(VenvSymlinkEntry(
kind = VenvSymlinkKind.LIB,
link_to_path = paths.join(repo_runfiles_dirname, site_packages_root, dist_info_dir),
src = package,
venv_path = dist_info_dir,
))
for src in ctx.files.srcs + ctx.files.data:
path = _repo_relative_short_path(src.short_path)
if not path.startswith(site_packages_root):
continue
path = path.removeprefix(site_packages_root)
dir_name, _, filename = path.rpartition("/")
if src.extension not in PYTHON_FILE_EXTENSIONS:
if dir_name.endswith(".dist-info"):
# we have already handled the stuff
pass
elif dir_name:
# TODO @aignas 2025-05-30: is this the right way?
dirs_with_init[dir_name] = None
repo_runfiles_dirname = runfiles_root_path(ctx, src.short_path).partition("/")[0]
elif dir_name and filename.startswith("__init__."):
dirs_with_init[dir_name] = None
repo_runfiles_dirname = runfiles_root_path(ctx, src.short_path).partition("/")[0]
elif not dir_name:
repo_runfiles_dirname = runfiles_root_path(ctx, src.short_path).partition("/")[0]
# This would be files that do not have directories and we just need to add
# direct symlinks to them as is:
venv_symlinks.append(VenvSymlinkEntry(
kind = VenvSymlinkKind.LIB,
link_to_path = paths.join(repo_runfiles_dirname, site_packages_root, filename),
src = package,
venv_path = filename,
))
# Sort so that we encounter `foo` before `foo/bar`. This ensures we
# see the top-most explicit package first.
dirnames = sorted(dirs_with_init.keys())
first_level_explicit_packages = []
for d in dirnames:
is_sub_package = False
for existing in first_level_explicit_packages:
# Suffix with / to prevent foo matching foobar
if d.startswith(existing + "/"):
is_sub_package = True
break
if not is_sub_package:
first_level_explicit_packages.append(d)
for dirname in first_level_explicit_packages:
venv_symlinks.append(VenvSymlinkEntry(
kind = VenvSymlinkKind.LIB,
link_to_path = paths.join(repo_runfiles_dirname, site_packages_root, dirname),
src = package,
venv_path = dirname,
))
return venv_symlinks
def _repo_relative_short_path(short_path):
# Convert `../+pypi+foo/some/file.py` to `some/file.py`
if short_path.startswith("../"):
return short_path[3:].partition("/")[2]
else:
return short_path
_MaybeBuiltinPyInfo = [BuiltinPyInfo] if BuiltinPyInfo != None else []
# NOTE: Exported publicaly
def create_py_library_rule_builder():
"""Create a rule builder for a py_library.
:::{include} /_includes/volatile_api.md
:::
:::{versionadded} 1.3.0
:::
Returns:
{type}`ruleb.Rule` with the necessary settings
for creating a `py_library` rule.
"""
builder = ruleb.Rule(
implementation = _py_library_impl_with_semantics,
doc = _DEFAULT_PY_LIBRARY_DOC,
exec_groups = dict(REQUIRED_EXEC_GROUP_BUILDERS),
attrs = LIBRARY_ATTRS,
fragments = ["py"],
provides = [PyCcLinkParamsInfo, PyInfo] + _MaybeBuiltinPyInfo,
toolchains = [
ruleb.ToolchainType(TOOLCHAIN_TYPE, mandatory = False),
ruleb.ToolchainType(EXEC_TOOLS_TOOLCHAIN_TYPE, mandatory = False),
],
)
return builder