-
-
Notifications
You must be signed in to change notification settings - Fork 707
Expand file tree
/
Copy pathwhl_library_alias.bzl
More file actions
104 lines (98 loc) · 4.42 KB
/
Copy pathwhl_library_alias.bzl
File metadata and controls
104 lines (98 loc) · 4.42 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
# 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.
"""whl_library aliases for multi_pip_parse."""
load("//python/private:full_version.bzl", "full_version")
def _whl_library_alias_impl(rctx):
rules_python = rctx.attr._rules_python_workspace.repo_name
if rctx.attr.default_version:
default_repo_prefix = rctx.attr.version_map.get(rctx.attr.default_version, None)
else:
default_repo_prefix = None
version_map = rctx.attr.version_map.items()
build_content = ["# Generated by python/pip.bzl"]
for alias_name in ["pkg", "whl", "data", "dist_info"]:
build_content.append(_whl_library_render_alias_target(
alias_name = alias_name,
default_repo_prefix = default_repo_prefix,
minor_mapping = rctx.attr.minor_mapping,
rules_python = rules_python,
version_map = version_map,
wheel_name = rctx.attr.wheel_name,
))
rctx.file("BUILD.bazel", "\n".join(build_content))
def _whl_library_render_alias_target(
*,
alias_name,
default_repo_prefix,
minor_mapping,
rules_python,
version_map,
wheel_name):
alias = ["""\
alias(
name = "{alias_name}",
actual = select({{""".format(alias_name = alias_name)]
for [python_version, repo_prefix] in version_map:
alias.append("""\
"@{rules_python}//python/config_settings:is_python_{full_python_version}": "{actual}",""".format(
full_python_version = full_version(version = python_version, minor_mapping = minor_mapping),
actual = "@{repo_prefix}{wheel_name}//:{alias_name}".format(
repo_prefix = repo_prefix,
wheel_name = wheel_name,
alias_name = alias_name,
),
rules_python = rules_python,
))
if default_repo_prefix:
default_actual = "@{repo_prefix}{wheel_name}//:{alias_name}".format(
repo_prefix = default_repo_prefix,
wheel_name = wheel_name,
alias_name = alias_name,
)
alias.append(' "//conditions:default": "{default_actual}",'.format(
default_actual = default_actual,
))
alias.append(" },") # Close select expression condition dict
alias.append(" ),") # Close the select expression
alias.append(' visibility = ["//visibility:public"],')
if not default_repo_prefix:
alias.append(" target_compatible_with = select({")
for [python_version, _] in version_map:
alias.append("""\
"@{rules_python}//python/config_settings:is_python_{full_python_version}": [],""".format(
full_python_version = full_version(version = python_version, minor_mapping = minor_mapping),
rules_python = rules_python,
))
alias.append(' "//conditions:default": ["@platforms//:incompatible"],')
alias.append(" }),") # Close the select expression
alias.append(")") # Close the alias() expression
return "\n".join(alias)
whl_library_alias = repository_rule(
_whl_library_alias_impl,
attrs = {
"default_version": attr.string(
mandatory = False,
doc = "Optional Python version in major.minor format, e.g. '3.10'." +
"The Python version of the wheel to use when the versions " +
"from `version_map` don't match. This allows the default " +
"(version unaware) rules to match and select a wheel. If " +
"not specified, then the default rules won't be able to " +
"resolve a wheel and the target will be incompatible.",
),
"minor_mapping": attr.string_dict(mandatory = True),
"version_map": attr.string_dict(mandatory = True),
"wheel_name": attr.string(mandatory = True),
"_rules_python_workspace": attr.label(default = Label("//:WORKSPACE")),
},
)