Skip to content

Commit a746b8f

Browse files
rickeylevaignas
andauthored
refactor: make bzlmod pass platform mapping to host repo creation (#2889)
This makes bzlmod pass the platform metadata to the host_toolchain rule instead of the host toolchain rule using the fixed PLATFORMS global. This allows the bzlmod extension to modify the platforms that are available, where the fixed PLATFORM global can't be changed. Work towards #2081 --------- Co-authored-by: Ignas Anikevicius <[email protected]>
1 parent c7efa25 commit a746b8f

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

python/private/python.bzl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,9 @@ def _python_impl(module_ctx):
298298
_internal_bzlmod_toolchain_call = True,
299299
**kwargs
300300
)
301-
host_compatible = []
301+
host_platforms = []
302+
host_os_names = {}
303+
host_archs = {}
302304
for repo_name, (platform_name, platform_info) in register_result.impl_repos.items():
303305
toolchain_impls.append(struct(
304306
# str: The base name to use for the toolchain() target
@@ -317,12 +319,17 @@ def _python_impl(module_ctx):
317319
set_python_version_constraint = is_last,
318320
))
319321
if _is_compatible_with_host(module_ctx, platform_info):
320-
host_compatible.append(platform_name)
322+
host_key = str(len(host_platforms))
323+
host_platforms.append(platform_name)
324+
host_os_names[host_key] = platform_info.os_name
325+
host_archs[host_key] = platform_info.arch
321326

322327
host_toolchain(
323328
name = toolchain_info.name + "_host",
324329
# NOTE: Order matters. The first found to be compatible is (usually) used.
325-
platforms = host_compatible,
330+
platforms = host_platforms,
331+
os_names = host_os_names,
332+
arch_names = host_archs,
326333
python_version = full_python_version,
327334
)
328335

python/private/toolchains_repo.bzl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,16 @@ toolchain_aliases repo because referencing the `python` interpreter target from
386386
this repo causes an eager fetch of the toolchain for the host platform.
387387
""",
388388
attrs = {
389+
"arch_names": attr.string_dict(
390+
doc = """
391+
If set, overrides the platform metadata. Keyed by index in `platforms`
392+
""",
393+
),
394+
"os_names": attr.string_dict(
395+
doc = """
396+
If set, overrides the platform metadata. Keyed by index in `platforms`
397+
""",
398+
),
389399
"platforms": attr.string_list(mandatory = True),
390400
"python_version": attr.string(mandatory = True),
391401
"_rule_name": attr.string(default = "host_toolchain"),
@@ -436,9 +446,20 @@ def _get_host_platform(*, rctx, logger, python_version, os_name, cpu_name, platf
436446
Returns:
437447
The host platform.
438448
"""
449+
if rctx.attr.os_names:
450+
platform_map = {}
451+
for i, platform_name in enumerate(platforms):
452+
key = str(i)
453+
platform_map[platform_name] = struct(
454+
os_name = rctx.attr.os_names[key],
455+
arch = rctx.attr.arch_names[key],
456+
)
457+
else:
458+
platform_map = PLATFORMS
459+
439460
candidates = []
440461
for platform in platforms:
441-
meta = PLATFORMS[platform]
462+
meta = platform_map[platform]
442463

443464
if meta.os_name == os_name and meta.arch == cpu_name:
444465
candidates.append(platform)

0 commit comments

Comments
 (0)