-
-
Notifications
You must be signed in to change notification settings - Fork 583
/
Copy pathwhl_library_targets.bzl
364 lines (333 loc) · 13.7 KB
/
whl_library_targets.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
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
# Copyright 2024 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.
"""Macro to generate all of the targets present in a {obj}`whl_library`."""
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("//python:py_binary.bzl", "py_binary")
load("//python:py_library.bzl", "py_library")
load("//python/private:glob_excludes.bzl", "glob_excludes")
load("//python/private:normalize_name.bzl", "normalize_name")
load(
":labels.bzl",
"DATA_LABEL",
"DIST_INFO_LABEL",
"PY_LIBRARY_IMPL_LABEL",
"PY_LIBRARY_PUBLIC_LABEL",
"WHEEL_ENTRY_POINT_PREFIX",
"WHEEL_FILE_IMPL_LABEL",
"WHEEL_FILE_PUBLIC_LABEL",
)
def whl_library_targets(
*,
name,
dep_template,
data_exclude = [],
srcs_exclude = [],
tags = [],
filegroups = {
DIST_INFO_LABEL: ["site-packages/*.dist-info/**"],
DATA_LABEL: ["data/**"],
},
dependencies = [],
dependencies_by_platform = {},
group_deps = [],
group_name = "",
data = [],
copy_files = {},
copy_executables = {},
entry_points = {},
native = native,
rules = struct(
copy_file = copy_file,
py_binary = py_binary,
py_library = py_library,
)):
"""Create all of the whl_library targets.
Args:
name: {type}`str` The file to match for including it into the `whl`
filegroup. This may be also parsed to generate extra metadata.
dep_template: {type}`str` The dep_template to use for dependency
interpolation.
tags: {type}`list[str]` The tags set on the `py_library`.
dependencies: {type}`list[str]` A list of dependencies.
dependencies_by_platform: {type}`dict[str, list[str]]` A list of
dependencies by platform key.
filegroups: {type}`dict[str, list[str]]` A dictionary of the target
names and the glob matches.
group_name: {type}`str` name of the dependency group (if any) which
contains this library. If set, this library will behave as a shim
to group implementation rules which will provide simultaneously
installed dependencies which would otherwise form a cycle.
group_deps: {type}`list[str]` names of fellow members of the group (if
any). These will be excluded from generated deps lists so as to avoid
direct cycles. These dependencies will be provided at runtime by the
group rules which wrap this library and its fellows together.
copy_executables: {type}`dict[str, str]` The mapping between src and
dest locations for the targets.
copy_files: {type}`dict[str, str]` The mapping between src and
dest locations for the targets.
data_exclude: {type}`list[str]` The globs for data attribute exclusion
in `py_library`.
srcs_exclude: {type}`list[str]` The globs for srcs attribute exclusion
in `py_library`.
data: {type}`list[str]` A list of labels to include as part of the `data` attribute in `py_library`.
entry_points: {type}`dict[str, str]` The mapping between the script
name and the python file to use. DEPRECATED.
native: {type}`native` The native struct for overriding in tests.
rules: {type}`struct` A struct with references to rules for creating targets.
"""
_ = name # buildifier: @unused
dependencies = sorted([normalize_name(d) for d in dependencies])
dependencies_by_platform = {
platform: sorted([normalize_name(d) for d in deps])
for platform, deps in dependencies_by_platform.items()
}
tags = sorted(tags)
data = [] + data
for filegroup_name, glob in filegroups.items():
native.filegroup(
name = filegroup_name,
srcs = native.glob(
glob,
exclude = [
# File names with spaces should be excluded.
"**/* *",
],
allow_empty = True,
),
visibility = ["//visibility:public"],
)
for src, dest in copy_files.items():
rules.copy_file(
name = dest + ".copy",
src = src,
out = dest,
visibility = ["//visibility:public"],
)
data.append(dest)
for src, dest in copy_executables.items():
rules.copy_file(
name = dest + ".copy",
src = src,
out = dest,
is_executable = True,
visibility = ["//visibility:public"],
)
data.append(dest)
_config_settings(
dependencies_by_platform.keys(),
native = native,
visibility = ["//visibility:private"],
)
# TODO @aignas 2024-10-25: remove the entry_point generation once
# `py_console_script_binary` is the only way to use entry points.
for entry_point, entry_point_script_name in entry_points.items():
rules.py_binary(
name = "{}_{}".format(WHEEL_ENTRY_POINT_PREFIX, entry_point),
# Ensure that this works on Windows as well - script may have Windows path separators.
srcs = [entry_point_script_name.replace("\\", "/")],
# This makes this directory a top-level in the python import
# search path for anything that depends on this.
imports = ["."],
deps = [":" + PY_LIBRARY_PUBLIC_LABEL],
visibility = ["//visibility:public"],
)
# Ensure this list is normalized
# Note: mapping used as set
group_deps = {
normalize_name(d): True
for d in group_deps
}
dependencies = [
d
for d in dependencies
if d not in group_deps
]
dependencies_by_platform = {
p: deps
for p, deps in dependencies_by_platform.items()
for deps in [[d for d in deps if d not in group_deps]]
if deps
}
# If this library is a member of a group, its public label aliases need to
# point to the group implementation rule not the implementation rules. We
# also need to mark the implementation rules as visible to the group
# implementation.
if group_name and "//:" in dep_template:
# This is the legacy behaviour where the group library is outside the hub repo
label_tmpl = dep_template.format(
name = "_groups",
target = normalize_name(group_name) + "_{}",
)
impl_vis = [dep_template.format(
name = "_groups",
target = "__pkg__",
)]
native.alias(
name = PY_LIBRARY_PUBLIC_LABEL,
actual = label_tmpl.format(PY_LIBRARY_PUBLIC_LABEL),
visibility = ["//visibility:public"],
)
native.alias(
name = WHEEL_FILE_PUBLIC_LABEL,
actual = label_tmpl.format(WHEEL_FILE_PUBLIC_LABEL),
visibility = ["//visibility:public"],
)
py_library_label = PY_LIBRARY_IMPL_LABEL
whl_file_label = WHEEL_FILE_IMPL_LABEL
elif group_name:
py_library_label = PY_LIBRARY_PUBLIC_LABEL
whl_file_label = WHEEL_FILE_PUBLIC_LABEL
impl_vis = [dep_template.format(name = "", target = "__subpackages__")]
else:
py_library_label = PY_LIBRARY_PUBLIC_LABEL
whl_file_label = WHEEL_FILE_PUBLIC_LABEL
impl_vis = ["//visibility:public"]
if hasattr(native, "filegroup"):
native.filegroup(
name = whl_file_label,
srcs = [name],
data = _deps(
deps = dependencies,
deps_by_platform = dependencies_by_platform,
tmpl = dep_template.format(name = "{}", target = WHEEL_FILE_PUBLIC_LABEL),
# NOTE @aignas 2024-10-28: Actually, `select` is not part of
# `native`, but in order to support bazel 6.4 in unit tests, I
# have to somehow pass the `select` implementation in the unit
# tests and I chose this to be routed through the `native`
# struct. So, tests` will be successful in `getattr` and the
# real code will use the fallback provided here.
select = getattr(native, "select", select),
),
visibility = impl_vis,
)
if hasattr(rules, "py_library"):
# NOTE: pyi files should probably be excluded because they're carried
# by the pyi_srcs attribute. However, historical behavior included
# them in data and some tools currently rely on that.
_data_exclude = [
"**/*.py",
"**/*.pyc",
"**/*.pyc.*", # During pyc creation, temp files named *.pyc.NNNN are created
"**/*.pyo.*", # During pyo creation, temp files named *.pyo.NNNN are created
# RECORD is known to contain sha256 checksums of files which might include the checksums
# of generated files produced when wheels are installed. The file is ignored to avoid
# Bazel caching issues.
"**/*.dist-info/RECORD",
# File names with spaces should be excluded.
"**/* *",
] + glob_excludes.version_dependent_exclusions()
for item in data_exclude:
if item not in _data_exclude:
_data_exclude.append(item)
rules.py_library(
name = py_library_label,
srcs = native.glob(
["site-packages/**/*.py"],
exclude = srcs_exclude + [
# File names with spaces should be excluded.
"**/* *",
],
# Empty sources are allowed to support wheels that don't have any
# pure-Python code, e.g. pymssql, which is written in Cython.
allow_empty = True,
),
pyi_srcs = native.glob(
["site-packages/**/*.pyi"],
allow_empty = True,
),
data = data + native.glob(
["site-packages/**/*"],
exclude = _data_exclude,
),
# This makes this directory a top-level in the python import
# search path for anything that depends on this.
imports = ["site-packages"],
deps = _deps(
deps = dependencies,
deps_by_platform = dependencies_by_platform,
tmpl = dep_template.format(name = "{}", target = PY_LIBRARY_PUBLIC_LABEL),
select = getattr(native, "select", select),
),
tags = tags,
visibility = impl_vis,
experimental_venvs_site_packages = Label("@rules_python//python/config_settings:venvs_site_packages"),
)
def _config_settings(dependencies_by_platform, native = native, **kwargs):
"""Generate config settings for the targets.
Args:
dependencies_by_platform: {type}`list[str]` platform keys, can be
one of the following formats:
* `//conditions:default`
* `@platforms//os:{value}`
* `@platforms//cpu:{value}`
* `@//python/config_settings:is_python_3.{minor_version}`
* `{os}_{cpu}`
* `cp3{minor_version}_{os}_{cpu}`
native: {type}`native` The native struct for overriding in tests.
**kwargs: Extra kwargs to pass to the rule.
"""
for p in dependencies_by_platform:
if p.startswith("@") or p.endswith("default"):
continue
abi, _, tail = p.partition("_")
if not abi.startswith("cp"):
tail = p
abi = ""
os, _, arch = tail.partition("_")
os = "" if os == "anyos" else os
arch = "" if arch == "anyarch" else arch
_kwargs = dict(kwargs)
if arch:
_kwargs.setdefault("constraint_values", []).append("@platforms//cpu:{}".format(arch))
if os:
_kwargs.setdefault("constraint_values", []).append("@platforms//os:{}".format(os))
if abi:
_kwargs["flag_values"] = {
"@rules_python//python/config_settings:python_version_major_minor": "3.{minor_version}".format(
minor_version = abi[len("cp3"):],
),
}
native.config_setting(
name = "is_{name}".format(
name = p.replace("cp3", "python_3."),
),
**_kwargs
)
def _plat_label(plat):
if plat.endswith("default"):
return plat
elif plat.startswith("@//"):
return Label(plat.strip("@"))
elif plat.startswith("@"):
return plat
else:
return ":is_" + plat.replace("cp3", "python_3.")
def _deps(deps, deps_by_platform, tmpl, select = select):
deps = [tmpl.format(d) for d in sorted(deps)]
if not deps_by_platform:
return deps
deps_by_platform = {
_plat_label(p): [
tmpl.format(d)
for d in sorted(deps)
]
for p, deps in sorted(deps_by_platform.items())
}
# Add the default, which means that we will be just using the dependencies in
# `deps` for platforms that are not handled in a special way by the packages
deps_by_platform.setdefault("//conditions:default", [])
if not deps:
return select(deps_by_platform)
else:
return deps + select(deps_by_platform)