feat(pypi): define default pip platforms for all toolchain platforms (#4456)

The default `pip.default` platforms only covered the `x86_64` and
`aarch64`
Linux, macOS and Windows platforms, so the toolchains for the other
platforms
could not use the wheels from a pip hub. Add the default platforms for
all of
the platforms that the hermetic Python toolchains support:

* `linux_armv7`, `linux_i386`, `linux_ppc`, `linux_riscv64`,
`linux_s390x`
  using the `manylinux` wheels.
* `linux_x86_64_musl` using the `musllinux` wheels.
* `osx_arm64e` and `windows_x86_32`.

Each of them has a `_freethreaded` variant. The glibc Linux platforms
now also
match on `--py_linux_libc`, so that they do not overlap with the new
musl
platform.

A new test ensures that each of the toolchain platform variants in
`//python:versions.bzl` has a pip platform with the same constraints and
settings, so that adding a toolchain platform requires adding a pip
platform.

The WORKSPACE `pip_parse` uses the same definitions for selecting the
wheels,
so the `linux_ppc`, `linux_riscv64` and `linux_s390x` platforms there
can now
select their platform specific wheels.

Work towards #3902
diff --git a/examples/pip_parse_vendored/requirements.bzl b/examples/pip_parse_vendored/requirements.bzl
index 82af353..95c4b98 100644
--- a/examples/pip_parse_vendored/requirements.bzl
+++ b/examples/pip_parse_vendored/requirements.bzl
@@ -34,7 +34,7 @@
 
 _packages = [
     ("my_project_pip_deps_vendored_certifi", "certifi==2026.10.23"),
-    ("my_project_pip_deps_vendored_charset_normalizer", "charset-normalizer==3.5.2 --hash=sha256:1c50fe28bbc2ced33386f298650d91218076c05420e6cbd790b913adc41659e7 --hash=sha256:4275811936e2f06feff5e598fb42a1b7ae852da8e39605211892b56b81a34efd --hash=sha256:8d86d6fc60743dc916eb79e2eb1ec4818e21e427731543af40a3021851174a13 --hash=sha256:b6b751274acb69d77b3323d6b7dbaa3c7fdfc1eb829b7eb61d262f32e1af9685 --hash=sha256:e4e81e09c1578b8df602e3db08b0b3ea0a6947ad612f52bf8dc5ea8d47691f0c"),
+    ("my_project_pip_deps_vendored_charset_normalizer", "charset-normalizer==3.5.2 --hash=sha256:0521c5665880b33d603717defa76c094048900010897909952397feb3039da56 --hash=sha256:1c50fe28bbc2ced33386f298650d91218076c05420e6cbd790b913adc41659e7 --hash=sha256:4275811936e2f06feff5e598fb42a1b7ae852da8e39605211892b56b81a34efd --hash=sha256:4d48f2d08b9de5864e2c8744d4461b862fb149a18274abc8b698c45975573438 --hash=sha256:8024d00c3faf3fc0c16e07a69f4405e8eac7cc0ab15f65fe6cf43827c4cf72b4 --hash=sha256:8d86d6fc60743dc916eb79e2eb1ec4818e21e427731543af40a3021851174a13 --hash=sha256:b6b751274acb69d77b3323d6b7dbaa3c7fdfc1eb829b7eb61d262f32e1af9685 --hash=sha256:e4e81e09c1578b8df602e3db08b0b3ea0a6947ad612f52bf8dc5ea8d47691f0c"),
     ("my_project_pip_deps_vendored_idna", "idna==3.20"),
     ("my_project_pip_deps_vendored_requests", "requests==2.34.2"),
     ("my_project_pip_deps_vendored_urllib3", "urllib3==2.8.0"),
diff --git a/news/4456.added.md b/news/4456.added.md
new file mode 100644
index 0000000..4a75c13
--- /dev/null
+++ b/news/4456.added.md
@@ -0,0 +1,5 @@
+(pypi) The default {obj}`pip.default` platforms now cover all of the platforms
+of the hermetic Python toolchains: `linux_armv7`, `linux_i386`, `linux_ppc`,
+`linux_riscv64`, `linux_s390x`, `linux_x86_64_musl`, `osx_arm64e` and
+`windows_x86_32`, together with their `_freethreaded` variants. The glibc Linux
+platforms now also match on the {flag}`--py_linux_libc` flag.
diff --git a/python/private/pypi/default_platforms.bzl b/python/private/pypi/default_platforms.bzl
index 578e7eb..d303272 100644
--- a/python/private/pypi/default_platforms.bzl
+++ b/python/private/pypi/default_platforms.bzl
@@ -1,5 +1,38 @@
 """The default platforms used for selecting the wheels."""
 
+def _platform(
+        *,
+        name,
+        os_name,
+        arch_name,
+        cpu,
+        whl_platform_tags,
+        platform_version,
+        freethreaded,
+        os_constraint = None,
+        config_settings = [],
+        marker = ""):
+    return {
+        "arch_name": arch_name,
+        "config_settings": [
+            "@platforms//cpu:{}".format(cpu),
+            "@platforms//os:{}".format(os_constraint or os_name),
+        ] + config_settings + [
+            "//python/config_settings:_is_py_freethreaded_{}".format(
+                "yes" if freethreaded else "no",
+            ),
+        ],
+        "env": {"platform_version": platform_version},
+        "marker": "python_version >= '3.13'" if freethreaded else marker,
+        "name": name,
+        "os_name": os_name,
+        "whl_abi_tags": ["cp{major}{minor}t"] if freethreaded else [
+            "abi3",
+            "cp{major}{minor}",
+        ],
+        "whl_platform_tags": whl_platform_tags,
+    }
+
 def default_platforms():
     """Return the built-in default platform definitions.
 
@@ -9,6 +42,9 @@
     for build_config; root modules can override individual platforms via
     pip.default tags.
 
+    There is a platform for each of the platforms that the hermetic Python
+    toolchains support, see `//python:versions.bzl`.
+
     Returns:
         A dict of platform name to platform config dicts.
     """
@@ -16,108 +52,87 @@
     # stabilize the API this list may be updated with a mention in the CHANGELOG.
 
     platforms = {}
+    for freethreaded in ["", "_freethreaded"]:
+        # Linux glibc platforms. The key is the platform name suffix and the
+        # values are the `@platforms//cpu` constraint, the `arch_name` and the
+        # wheel platform tag suffix.
+        for name, (cpu, arch_name, tag) in {
+            "aarch64": ("aarch64", "aarch64", "aarch64"),
+            "armv7": ("armv7", "arm", "armv7l"),
+            "i386": ("i386", "x86_32", "i686"),
+            "ppc": ("ppc", "ppc", "ppc64le"),
+            "riscv64": ("riscv64", "riscv64", "riscv64"),
+            "s390x": ("s390x", "s390x", "s390x"),
+            "x86_64": ("x86_64", "x86_64", "x86_64"),
+        }.items():
+            platform_name = "linux_{}{}".format(name, freethreaded)
+            platforms[platform_name] = _platform(
+                name = platform_name,
+                os_name = "linux",
+                arch_name = arch_name,
+                cpu = cpu,
+                config_settings = [
+                    "//python/config_settings:_is_py_linux_libc_glibc",
+                ],
+                platform_version = "0",
+                freethreaded = freethreaded,
+                whl_platform_tags = [
+                    "linux_{}".format(tag),
+                    "manylinux_*_{}".format(tag),
+                ],
+            )
 
-    # Linux platforms
-    for cpu in ["x86_64", "aarch64"]:
-        for freethreaded in ["", "_freethreaded"]:
-            platform_name = "linux_{}{}".format(cpu, freethreaded)
-            platforms[platform_name] = {
-                "arch_name": cpu,
-                "config_settings": [
-                    "@platforms//cpu:{}".format(cpu),
-                    "@platforms//os:linux",
-                    "//python/config_settings:_is_py_freethreaded_{}".format(
-                        "yes" if freethreaded else "no",
-                    ),
-                ],
-                "env": {"platform_version": "0"},
-                "marker": "python_version >= '3.13'" if freethreaded else "",
-                "name": platform_name,
-                "os_name": "linux",
-                "whl_abi_tags": ["cp{major}{minor}t"] if freethreaded else [
-                    "abi3",
-                    "cp{major}{minor}",
-                ],
-                "whl_platform_tags": [
-                    "linux_{}".format(cpu),
-                    "manylinux_*_{}".format(cpu),
-                ],
-            }
+        # Linux musl platforms
+        platform_name = "linux_x86_64_musl{}".format(freethreaded)
+        platforms[platform_name] = _platform(
+            name = platform_name,
+            os_name = "linux",
+            arch_name = "x86_64",
+            cpu = "x86_64",
+            config_settings = [
+                "//python/config_settings:_is_py_linux_libc_musl",
+            ],
+            platform_version = "0",
+            freethreaded = freethreaded,
+            whl_platform_tags = ["musllinux_*_x86_64"],
+        )
 
-    # macOS platforms
-    for cpu, platform_tag_cpus in {
-        "aarch64": ["universal2", "arm64"],
-        "x86_64": ["universal2", "x86_64"],
-    }.items():
-        for freethreaded in ["", "_freethreaded"]:
+        # macOS platforms
+        for cpu, platform_tag_cpus in {
+            "aarch64": ["universal2", "arm64"],
+            "arm64e": ["arm64e"],
+            "x86_64": ["universal2", "x86_64"],
+        }.items():
             platform_name = "osx_{}{}".format(cpu, freethreaded)
-            platforms[platform_name] = {
-                "arch_name": cpu,
-                "config_settings": [
-                    "@platforms//cpu:{}".format(cpu),
-                    "@platforms//os:osx",
-                    "//python/config_settings:_is_py_freethreaded_{}".format(
-                        "yes" if freethreaded else "no",
-                    ),
-                ],
-                "env": {"platform_version": "14.0"},
-                "marker": "python_version >= '3.13'" if freethreaded else "",
-                "name": platform_name,
-                "os_name": "osx",
-                "whl_abi_tags": ["cp{major}{minor}t"] if freethreaded else [
-                    "abi3",
-                    "cp{major}{minor}",
-                ],
-                "whl_platform_tags": [
+            platforms[platform_name] = _platform(
+                name = platform_name,
+                os_name = "osx",
+                arch_name = "aarch64" if cpu == "arm64e" else cpu,
+                cpu = cpu,
+                platform_version = "14.0",
+                freethreaded = freethreaded,
+                whl_platform_tags = [
                     "macosx_*_{}".format(suffix)
                     for suffix in platform_tag_cpus
                 ],
-            }
+            )
 
-    # Windows x86_64 platforms
-    for freethreaded in ["", "_freethreaded"]:
-        platform_name = "windows_x86_64{}".format(freethreaded)
-        platforms[platform_name] = {
-            "arch_name": "x86_64",
-            "config_settings": [
-                "@platforms//cpu:x86_64",
-                "@platforms//os:windows",
-                "//python/config_settings:_is_py_freethreaded_{}".format(
-                    "yes" if freethreaded else "no",
-                ),
-            ],
-            "env": {"platform_version": "0"},
-            "marker": "python_version >= '3.13'" if freethreaded else "",
-            "name": platform_name,
-            "os_name": "windows",
-            "whl_abi_tags": ["cp{major}{minor}t"] if freethreaded else [
-                "abi3",
-                "cp{major}{minor}",
-            ],
-            "whl_platform_tags": ["win_amd64"],
-        }
-
-    # Windows aarch64 platforms
-    for freethreaded in ["", "_freethreaded"]:
-        platform_name = "windows_aarch64{}".format(freethreaded)
-        platforms[platform_name] = {
-            "arch_name": "aarch64",
-            "config_settings": [
-                "@platforms//cpu:aarch64",
-                "@platforms//os:windows",
-                "//python/config_settings:_is_py_freethreaded_{}".format(
-                    "yes" if freethreaded else "no",
-                ),
-            ],
-            "env": {"platform_version": "0"},
-            "marker": "python_version >= '3.13'" if freethreaded else "python_version >= '3.11'",
-            "name": platform_name,
-            "os_name": "windows",
-            "whl_abi_tags": ["cp{major}{minor}t"] if freethreaded else [
-                "abi3",
-                "cp{major}{minor}",
-            ],
-            "whl_platform_tags": ["win_arm64"],
-        }
+        # Windows platforms
+        for cpu, (tag, marker) in {
+            "aarch64": ("win_arm64", "python_version >= '3.11'"),
+            "x86_32": ("win32", ""),
+            "x86_64": ("win_amd64", ""),
+        }.items():
+            platform_name = "windows_{}{}".format(cpu, freethreaded)
+            platforms[platform_name] = _platform(
+                name = platform_name,
+                os_name = "windows",
+                arch_name = cpu,
+                cpu = cpu,
+                platform_version = "0",
+                freethreaded = freethreaded,
+                marker = marker,
+                whl_platform_tags = [tag],
+            )
 
     return platforms
diff --git a/tests/pypi/default_platforms/BUILD.bazel b/tests/pypi/default_platforms/BUILD.bazel
new file mode 100644
index 0000000..118f600
--- /dev/null
+++ b/tests/pypi/default_platforms/BUILD.bazel
@@ -0,0 +1,3 @@
+load(":default_platforms_tests.bzl", "default_platforms_test_suite")
+
+default_platforms_test_suite(name = "default_platforms_tests")
diff --git a/tests/pypi/default_platforms/default_platforms_tests.bzl b/tests/pypi/default_platforms/default_platforms_tests.bzl
new file mode 100644
index 0000000..5dcbfd7
--- /dev/null
+++ b/tests/pypi/default_platforms/default_platforms_tests.bzl
@@ -0,0 +1,71 @@
+"""Tests for the default pip platforms."""
+
+load("@rules_testing//lib:test_suite.bzl", "test_suite")
+load("//python:versions.bzl", "PLATFORMS")
+load("//python/private/pypi:default_platforms.bzl", "default_platforms")  # buildifier: disable=bzl-visibility
+
+_tests = []
+
+# The settings that select the variant of the toolchain distribution, which do
+# not change the compatible wheels.
+_IGNORED_SETTINGS = [
+    "_is_py_pbs_distribution_",
+]
+
+def _normalize(labels):
+    """Return the set of the canonical label strings.
+
+    Args:
+        labels: {type}`list[str | Label]` the labels to normalize.
+
+    Returns:
+        {type}`tuple[str]` the sorted canonical labels.
+    """
+    ret = {}
+    for label in labels:
+        label = Label(label)
+        if any([s in label.name for s in _IGNORED_SETTINGS]):
+            continue
+
+        # `@platforms//os:macos` is an alias of `@platforms//os:osx`.
+        if label.repo_name == "platforms" and label.package == "os" and label.name == "macos":
+            label = Label("@platforms//os:osx")
+        ret[str(label)] = None
+    return tuple(sorted(ret))
+
+def _test_all_toolchain_platforms_have_pip_platforms(env):
+    pip_platforms = {
+        _normalize(values["config_settings"]): name
+        for name, values in default_platforms().items()
+    }
+
+    for name, platform in PLATFORMS.items():
+        want = _normalize(platform.compatible_with + platform.target_settings)
+        env.expect.where(
+            toolchain_platform = name,
+        ).that_str(
+            pip_platforms.get(want, "<missing pip platform for {}>".format(want)),
+        ).not_equals(
+            "<missing pip platform for {}>".format(want),
+        )
+
+_tests.append(_test_all_toolchain_platforms_have_pip_platforms)
+
+def _test_pip_platforms_are_unique(env):
+    seen = {}
+    for name, values in default_platforms().items():
+        key = _normalize(values["config_settings"])
+        env.expect.where(
+            pip_platform = name,
+        ).that_str(seen.get(key, name)).equals(name)
+        seen[key] = name
+
+_tests.append(_test_pip_platforms_are_unique)
+
+def default_platforms_test_suite(name):
+    """Create the test suite.
+
+    Args:
+        name: the name of the test suite
+    """
+    test_suite(name = name, basic_tests = _tests)