diff --git a/python/private/python.bzl b/python/private/python.bzl index ea794ecf86..0f3bbee4ac 100644 --- a/python/private/python.bzl +++ b/python/private/python.bzl @@ -298,7 +298,9 @@ def _python_impl(module_ctx): _internal_bzlmod_toolchain_call = True, **kwargs ) - host_compatible = [] + host_platforms = [] + host_os_names = {} + host_archs = {} for repo_name, (platform_name, platform_info) in register_result.impl_repos.items(): toolchain_impls.append(struct( # str: The base name to use for the toolchain() target @@ -317,12 +319,17 @@ def _python_impl(module_ctx): set_python_version_constraint = is_last, )) if _is_compatible_with_host(module_ctx, platform_info): - host_compatible.append(platform_name) + host_key = str(len(host_platforms)) + host_platforms.append(platform_name) + host_os_names[host_key] = platform_info.os_name + host_archs[host_key] = platform_info.arch host_toolchain( name = toolchain_info.name + "_host", # NOTE: Order matters. The first found to be compatible is (usually) used. - platforms = host_compatible, + platforms = host_platforms, + os_names = host_os_names, + arch_names = host_archs, python_version = full_python_version, ) diff --git a/python/private/toolchains_repo.bzl b/python/private/toolchains_repo.bzl index a4188a739a..29ac694fd5 100644 --- a/python/private/toolchains_repo.bzl +++ b/python/private/toolchains_repo.bzl @@ -386,6 +386,16 @@ toolchain_aliases repo because referencing the `python` interpreter target from this repo causes an eager fetch of the toolchain for the host platform. """, attrs = { + "arch_names": attr.string_dict( + doc = """ +If set, overrides the platform metadata. Keyed by index in `platforms` +""", + ), + "os_names": attr.string_dict( + doc = """ +If set, overrides the platform metadata. Keyed by index in `platforms` +""", + ), "platforms": attr.string_list(mandatory = True), "python_version": attr.string(mandatory = True), "_rule_name": attr.string(default = "host_toolchain"), @@ -436,9 +446,20 @@ def _get_host_platform(*, rctx, logger, python_version, os_name, cpu_name, platf Returns: The host platform. """ + if rctx.attr.os_names: + platform_map = {} + for i, platform_name in enumerate(platforms): + key = str(i) + platform_map[platform_name] = struct( + os_name = rctx.attr.os_names[key], + arch = rctx.attr.arch_names[key], + ) + else: + platform_map = PLATFORMS + candidates = [] for platform in platforms: - meta = PLATFORMS[platform] + meta = platform_map[platform] if meta.os_name == os_name and meta.arch == cpu_name: candidates.append(platform)