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 https://github.com/bazel-contrib/rules_python/issues/2081

---------

Co-authored-by: Ignas Anikevicius <240938+aignas@users.noreply.github.com>
diff --git a/python/private/python.bzl b/python/private/python.bzl
index ea794ec..0f3bbee 100644
--- a/python/private/python.bzl
+++ b/python/private/python.bzl
@@ -298,7 +298,9 @@
             _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 @@
                 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 a4188a7..29ac694 100644
--- a/python/private/toolchains_repo.bzl
+++ b/python/private/toolchains_repo.bzl
@@ -386,6 +386,16 @@
 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 @@
     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)