forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythons_hub.bzl
More file actions
188 lines (170 loc) · 6.78 KB
/
pythons_hub.bzl
File metadata and controls
188 lines (170 loc) · 6.78 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
# Copyright 2023 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.
"Repo rule used by bzlmod extension to create a repo that has a map of Python interpreters and their labels"
load("//python:versions.bzl", "PLATFORMS")
load(":text_util.bzl", "render")
load(":toolchains_repo.bzl", "toolchain_suite_content")
def _have_same_length(*lists):
if not lists:
fail("expected at least one list")
return len({len(length): None for length in lists}) == 1
_HUB_BUILD_FILE_TEMPLATE = """\
# Generated by @rules_python//python/private:pythons_hub.bzl
load("@@{rules_python}//python/private:py_toolchain_suite.bzl", "py_toolchain_suite")
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
bzl_library(
name = "interpreters_bzl",
srcs = ["interpreters.bzl"],
visibility = ["@rules_python//:__subpackages__"],
)
bzl_library(
name = "versions_bzl",
srcs = ["versions.bzl"],
visibility = ["@rules_python//:__subpackages__"],
)
{toolchains}
"""
def _hub_build_file_content(rctx):
# Verify a precondition. If these don't match, then something went wrong.
if not _have_same_length(
rctx.attr.toolchain_names,
rctx.attr.toolchain_platform_keys,
rctx.attr.toolchain_repo_names,
rctx.attr.toolchain_target_compatible_with_map,
rctx.attr.toolchain_target_settings_map,
rctx.attr.toolchain_set_python_version_constraints,
rctx.attr.toolchain_python_versions,
):
fail("all lists must have the same length")
#pad_length = len(str(len(rctx.attr.toolchain_names))) + 1
pad_length = 4
toolchains = []
for i, base_name in enumerate(rctx.attr.toolchain_names):
key = str(i)
platform = rctx.attr.toolchain_platform_keys[key]
if platform in PLATFORMS:
flag_values = PLATFORMS[platform].flag_values
else:
flag_values = {}
toolchains.append(toolchain_suite_content(
prefix = "_{}_{}".format(render.left_pad_zero(i, pad_length), base_name),
user_repository_name = rctx.attr.toolchain_repo_names[key],
target_compatible_with = rctx.attr.toolchain_target_compatible_with_map[key],
flag_values = flag_values,
target_settings = rctx.attr.toolchain_target_settings_map[key],
set_python_version_constraint = rctx.attr.toolchain_set_python_version_constraints[key],
python_version = rctx.attr.toolchain_python_versions[key],
))
return _HUB_BUILD_FILE_TEMPLATE.format(
toolchains = "\n".join(toolchains),
rules_python = rctx.attr._rules_python_workspace.repo_name,
)
_interpreters_bzl_template = """
INTERPRETER_LABELS = {labels}
"""
_versions_bzl_template = """
DEFAULT_PYTHON_VERSION = "{default_python_version}"
MINOR_MAPPING = {minor_mapping}
PYTHON_VERSIONS = {python_versions}
"""
def _hub_repo_impl(rctx):
# Create the various toolchain definitions and
# write them to the BUILD file.
rctx.file(
"BUILD.bazel",
_hub_build_file_content(rctx),
executable = False,
)
# Create a dict that is later used to create
# a symlink to a interpreter.
rctx.file(
"interpreters.bzl",
_interpreters_bzl_template.format(
labels = render.dict(
{
name: 'Label("@{}//:python")'.format(name)
for name in rctx.attr.host_compatible_repo_names
},
value_repr = str,
),
),
executable = False,
)
rctx.file(
"versions.bzl",
_versions_bzl_template.format(
default_python_version = rctx.attr.default_python_version,
minor_mapping = render.dict(rctx.attr.minor_mapping),
python_versions = rctx.attr.python_versions or render.list(sorted({
v: None
for v in rctx.attr.toolchain_python_versions
})),
),
executable = False,
)
hub_repo = repository_rule(
doc = """\
This private rule create a repo with a BUILD file that contains a map of interpreter names
and the labels to said interpreters. This map is used to by the interpreter hub extension.
This rule also writes out the various toolchains for the different Python versions.
""",
implementation = _hub_repo_impl,
attrs = {
"default_python_version": attr.string(
doc = "Default Python version for the build in `X.Y` or `X.Y.Z` format.",
mandatory = True,
),
"host_compatible_repo_names": attr.string_list(
doc = "Names of `host_compatible_python_repo` repos.",
mandatory = True,
),
"minor_mapping": attr.string_dict(
doc = "The minor mapping of the `X.Y` to `X.Y.Z` format that is used in config settings.",
mandatory = True,
),
"python_versions": attr.string_list(
doc = "The list of python versions to include in the `interpreters.bzl` if the toolchains are not specified. Used in `WORKSPACE` builds.",
mandatory = False,
),
"toolchain_names": attr.string_list(
doc = "Names of toolchains",
mandatory = True,
),
"toolchain_platform_keys": attr.string_dict(
doc = "The platform key in PLATFORMS for toolchains.",
mandatory = True,
),
"toolchain_python_versions": attr.string_dict(
doc = "List of Python versions for the toolchains. In `X.Y.Z` format.",
mandatory = True,
),
"toolchain_repo_names": attr.string_dict(
doc = "The repo names containing toolchain implementations.",
mandatory = True,
),
"toolchain_set_python_version_constraints": attr.string_dict(
doc = "List of version contraints for the toolchains",
mandatory = True,
),
"toolchain_target_compatible_with_map": attr.string_list_dict(
doc = "The target_compatible_with settings for toolchains.",
mandatory = True,
),
"toolchain_target_settings_map": attr.string_list_dict(
doc = "The target_settings for toolchains",
mandatory = True,
),
"_rules_python_workspace": attr.label(default = Label("//:does_not_matter_what_this_name_is")),
},
)