@@ -28,22 +28,21 @@ load(":util.bzl", "IS_BAZEL_6_4_OR_HIGHER")
28
28
_MAX_NUM_TOOLCHAINS = 9999
29
29
_TOOLCHAIN_INDEX_PAD_LENGTH = len (str (_MAX_NUM_TOOLCHAINS ))
30
30
31
- def _python_register_toolchains (name , toolchain_attr , module , ignore_root_user_error ):
32
- """Calls python_register_toolchains and returns a struct used to collect the toolchains.
31
+ def parse_modules (module_ctx ):
32
+ """Parse the modules and return a struct for registrations.
33
+
34
+ Args:
35
+ module_ctx: {type}`module_ctx` module context.
36
+
37
+ Returns:
38
+ A struct with the following attributes:
39
+ * `toolchains`: The list of toolchains to register. The last
40
+ element is special and is treated as the default toolchain.
41
+ * `defaults`: The default `kwargs` passed to
42
+ {bzl:obj}`python_register_toolchains`.
43
+ * `debug_info`: {type}`None | dict` extra information to be passed
44
+ to the debug repo.
33
45
"""
34
- python_register_toolchains (
35
- name = name ,
36
- python_version = toolchain_attr .python_version ,
37
- register_coverage_tool = toolchain_attr .configure_coverage_tool ,
38
- ignore_root_user_error = ignore_root_user_error ,
39
- )
40
- return struct (
41
- python_version = toolchain_attr .python_version ,
42
- name = name ,
43
- module = struct (name = module .name , is_root = module .is_root ),
44
- )
45
-
46
- def _python_impl (module_ctx ):
47
46
if module_ctx .os .environ .get ("RULES_PYTHON_BZLMOD_DEBUG" , "0" ) == "1" :
48
47
debug_info = {
49
48
"toolchains_registered" : [],
@@ -61,7 +60,7 @@ def _python_impl(module_ctx):
61
60
# This is a toolchain_info struct.
62
61
default_toolchain = None
63
62
64
- # Map of string Major.Minor to the toolchain_info struct
63
+ # Map of version string to the toolchain_info struct
65
64
global_toolchain_versions = {}
66
65
67
66
ignore_root_user_error = None
@@ -139,11 +138,11 @@ def _python_impl(module_ctx):
139
138
)
140
139
toolchain_info = None
141
140
else :
142
- toolchain_info = _python_register_toolchains (
143
- toolchain_name ,
144
- toolchain_attr ,
145
- module = mod ,
146
- ignore_root_user_error = ignore_root_user_error ,
141
+ toolchain_info = struct (
142
+ python_version = toolchain_attr . python_version ,
143
+ name = toolchain_name ,
144
+ register_coverage_tool = toolchain_attr . configure_coverage_tool ,
145
+ module = struct ( name = mod . name , is_root = mod . is_root ) ,
147
146
)
148
147
global_toolchain_versions [toolchain_version ] = toolchain_info
149
148
if debug_info :
@@ -184,39 +183,67 @@ def _python_impl(module_ctx):
184
183
if len (toolchains ) > _MAX_NUM_TOOLCHAINS :
185
184
fail ("more than {} python versions are not supported" .format (_MAX_NUM_TOOLCHAINS ))
186
185
186
+ return struct (
187
+ toolchains = [
188
+ struct (
189
+ python_version = t .python_version ,
190
+ name = t .name ,
191
+ register_coverage_tool = t .register_coverage_tool ,
192
+ )
193
+ for t in toolchains
194
+ ],
195
+ debug_info = debug_info ,
196
+ default_python_version = toolchains [- 1 ].python_version ,
197
+ defaults = {
198
+ "ignore_root_user_error" : ignore_root_user_error ,
199
+ },
200
+ )
201
+
202
+ def _python_impl (module_ctx ):
203
+ py = parse_modules (module_ctx )
204
+
205
+ for toolchain_info in py .toolchains :
206
+ python_register_toolchains (
207
+ name = toolchain_info .name ,
208
+ python_version = toolchain_info .python_version ,
209
+ register_coverage_tool = toolchain_info .register_coverage_tool ,
210
+ ** py .defaults
211
+ )
212
+
187
213
# Create the pythons_hub repo for the interpreter meta data and the
188
214
# the various toolchains.
189
215
hub_repo (
190
216
name = "pythons_hub" ,
191
- default_python_version = default_toolchain .python_version ,
217
+ # Last toolchain is default
218
+ default_python_version = py .default_python_version ,
192
219
toolchain_prefixes = [
193
220
render .toolchain_prefix (index , toolchain .name , _TOOLCHAIN_INDEX_PAD_LENGTH )
194
- for index , toolchain in enumerate (toolchains )
221
+ for index , toolchain in enumerate (py . toolchains )
195
222
],
196
- toolchain_python_versions = [t .python_version for t in toolchains ],
223
+ toolchain_python_versions = [t .python_version for t in py . toolchains ],
197
224
# The last toolchain is the default; it can't have version constraints
198
225
# Despite the implication of the arg name, the values are strs, not bools
199
226
toolchain_set_python_version_constraints = [
200
- "True" if i != len (toolchains ) - 1 else "False"
201
- for i in range (len (toolchains ))
227
+ "True" if i != len (py . toolchains ) - 1 else "False"
228
+ for i in range (len (py . toolchains ))
202
229
],
203
- toolchain_user_repository_names = [t .name for t in toolchains ],
230
+ toolchain_user_repository_names = [t .name for t in py . toolchains ],
204
231
)
205
232
206
233
# This is require in order to support multiple version py_test
207
234
# and py_binary
208
235
multi_toolchain_aliases (
209
236
name = "python_versions" ,
210
237
python_versions = {
211
- version : toolchain .name
212
- for version , toolchain in global_toolchain_versions . items ()
238
+ toolchain . python_version : toolchain .name
239
+ for toolchain in py . toolchains
213
240
},
214
241
)
215
242
216
- if debug_info != None :
243
+ if py . debug_info != None :
217
244
_debug_repo (
218
245
name = "rules_python_bzlmod_debug" ,
219
- debug_info = json .encode_indent (debug_info ),
246
+ debug_info = json .encode_indent (py . debug_info ),
220
247
)
221
248
222
249
if bazel_features .external_deps .extension_metadata_has_reproducible :
0 commit comments