Skip to content

chore(pypi): remove unused config setting code #3065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions python/private/pypi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,6 @@ bzl_library(
srcs = ["pkg_aliases.bzl"],
deps = [
":labels_bzl",
":parse_whl_name_bzl",
":whl_target_platforms_bzl",
"//python/private:text_util_bzl",
"@bazel_skylib//lib:selects",
],
Expand Down Expand Up @@ -349,9 +347,7 @@ bzl_library(
srcs = ["render_pkg_aliases.bzl"],
deps = [
":generate_group_library_build_bazel_bzl",
":parse_whl_name_bzl",
":whl_config_setting_bzl",
":whl_target_platforms_bzl",
"//python/private:normalize_name_bzl",
"//python/private:text_util_bzl",
],
Expand Down
328 changes: 30 additions & 298 deletions python/private/pypi/config_settings.bzl

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions python/private/pypi/hub_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,6 @@ def whl_config_settings_to_json(repo_mapping):

def _whl_config_setting_dict(a):
ret = {}
if a.config_setting:
ret["config_setting"] = a.config_setting
if a.filename:
ret["filename"] = a.filename
if a.target_platforms:
ret["target_platforms"] = a.target_platforms
if a.version:
Expand Down
243 changes: 11 additions & 232 deletions python/private/pypi/pkg_aliases.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,54 +23,12 @@ Definitions:
:suffix: Can be either empty or `_<os>_<suffix>`, which is usually used to distinguish multiple versions used for different target platforms.
:os: OS identifier that exists in `@platforms//os:<os>`.
:cpu: CPU architecture identifier that exists in `@platforms//cpu:<cpu>`.
:python_tag: The Python tag as defined by the [Python Packaging Authority][packaging_spec]. E.g. `py2.py3`, `py3`, `py311`, `cp311`.
:abi_tag: The ABI tag as defined by the [Python Packaging Authority][packaging_spec]. E.g. `none`, `abi3`, `cp311`, `cp311t`.
:platform_tag: The Platform tag as defined by the [Python Packaging Authority][packaging_spec]. E.g. `manylinux_2_17_x86_64`.
:platform_suffix: is a derivative of the `platform_tag` and is used to implement selection based on `libc` or `osx` version.

All of the config settings used by this macro are generated by
{obj}`config_settings`, for more detailed documentation on what each config
setting maps to and their precedence, refer to documentation on that page.

The first group of config settings that are as follows:

* `//_config:is_cp3<minor_version><suffix>` is used to select legacy `pip`
based `whl` and `sdist` {obj}`whl_library` instances. Whereas other config
settings are created when {obj}`pip.parse.experimental_index_url` is used.
* `//_config:is_cp3<minor_version>_sdist<suffix>` is for wheels built from
`sdist` in {obj}`whl_library`.
* `//_config:is_cp3<minor_version>_py_<abi_tag>_any<suffix>` for wheels with
`py2.py3` `python_tag` value.
* `//_config:is_cp3<minor_version>_py3_<abi_tag>_any<suffix>` for wheels with
`py3` `python_tag` value.
* `//_config:is_cp3<minor_version>_<abi_tag>_any<suffix>` for any other wheels.
* `//_config:is_cp3<minor_version>_py_<abi_tag>_<platform_suffix>` for
platform-specific wheels with `py2.py3` `python_tag` value.
* `//_config:is_cp3<minor_version>_py3_<abi_tag>_<platform_suffix>` for
platform-specific wheels with `py3` `python_tag` value.
* `//_config:is_cp3<minor_version>_<abi_tag>_<platform_suffix>` for any other
platform-specific wheels.

Note that wheels with `abi3` or `none` `abi_tag` values and `python_tag` values
other than `py2.py3` or `py3` are compatible with the python version that is
equal or higher than the one denoted in the `python_tag`. For example: `py37`
and `cp37` wheels are compatible with Python 3.7 and above and in the case of
the target python version being `3.11`, `rules_python` will use
`//_config:is_cp311_<abi_tag>_any<suffix>` config settings.

For platform-specific wheels, i.e. the ones that have their `platform_tag` as
something else than `any`, we treat them as below:
* `linux_<cpu>` tags assume that the target `libc` flavour is `glibc`, so this
is in many ways equivalent to it being `manylinux`, but with an unspecified
`libc` version.
* For `osx` and `linux` OSes wheel filename will be mapped to multiple config settings:
* `osx_<cpu>` and `osx_<major_version>_<minor_version>_<cpu>` where
`major_version` and `minor_version` are the compatible OSX versions.
* `<many|musl>linux_<cpu>` and
`<many|musl>linux_<major_version>_<minor_version>_<cpu>` where the version
identifiers are the compatible libc versions.

[packaging_spec]: https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/
`//_config:is_cp3<minor_version><suffix>` is used to select any target platforms.
"""

load("@bazel_skylib//lib:selects.bzl", "selects")
Expand All @@ -85,14 +43,6 @@ load(
"WHEEL_FILE_IMPL_LABEL",
"WHEEL_FILE_PUBLIC_LABEL",
)
load(":parse_whl_name.bzl", "parse_whl_name")
load(":whl_target_platforms.bzl", "whl_target_platforms")

# This value is used as sentinel value in the alias/config setting machinery
# for libc and osx versions. If we encounter this version in this part of the
# code, then it means that we have a bug in rules_python and that we should fix
# it. It is more of an internal consistency check.
_VERSION_NONE = (0, 0)

_NO_MATCH_ERROR_TEMPLATE = """\
No matching wheel for current configuration's Python version.
Expand Down Expand Up @@ -137,7 +87,7 @@ def pkg_aliases(
to bazel skylib's `selects.with_or`, so they can be tuples as well.
group_name: {type}`str` The group name that the pkg belongs to.
extra_aliases: {type}`list[str]` The extra aliases to be created.
**kwargs: extra kwargs to pass to {bzl:obj}`get_filename_config_settings`.
**kwargs: extra kwargs to pass to {bzl:obj}`get_config_settings`.
"""
alias = kwargs.pop("native", native).alias
select = kwargs.pop("select", selects.with_or)
Expand Down Expand Up @@ -219,21 +169,9 @@ def pkg_aliases(
actual = "//_groups:{}_whl".format(group_name),
)

def _normalize_versions(name, versions):
if not versions:
return []

if _VERSION_NONE in versions:
fail("a sentinel version found in '{}', check render_pkg_aliases for bugs".format(name))

return sorted(versions)

def multiplatform_whl_aliases(
*,
aliases = [],
glibc_versions = [],
muslc_versions = [],
osx_versions = []):
aliases = []):
"""convert a list of aliases from filename to config_setting ones.

Exposed only for unit tests.
Expand All @@ -243,12 +181,6 @@ def multiplatform_whl_aliases(
to process. Any aliases that have the filename set will be
converted to a dict of config settings to repo names. The
struct is created by {func}`whl_config_setting`.
glibc_versions: {type}`list[tuple[int, int]]` list of versions that can be
used in this hub repo.
muslc_versions: {type}`list[tuple[int, int]]` list of versions that can be
used in this hub repo.
osx_versions: {type}`list[tuple[int, int]]` list of versions that can be
used in this hub repo.

Returns:
A dict with of config setting labels to repo names or the repo name itself.
Expand All @@ -258,207 +190,54 @@ def multiplatform_whl_aliases(
# We don't have any aliases, this is a repo name
return aliases

# TODO @aignas 2024-11-17: we might be able to use FeatureFlagInfo and some
# code gen to create a version_lt_x target, which would allow us to check
# if the libc version is in a particular range.
glibc_versions = _normalize_versions("glibc_versions", glibc_versions)
muslc_versions = _normalize_versions("muslc_versions", muslc_versions)
osx_versions = _normalize_versions("osx_versions", osx_versions)

ret = {}
versioned_additions = {}
for alias, repo in aliases.items():
if type(alias) != "struct":
ret[alias] = repo
continue
elif not (alias.filename or alias.target_platforms):
# This is an internal consistency check
fail("Expected to have either 'filename' or 'target_platforms' set, got: {}".format(alias))

config_settings, all_versioned_settings = get_filename_config_settings(
filename = alias.filename or "",
config_settings = get_config_settings(
target_platforms = alias.target_platforms,
python_version = alias.version,
# If we have multiple platforms but no wheel filename, lets use different
# config settings.
non_whl_prefix = "sdist" if alias.filename else "",
glibc_versions = glibc_versions,
muslc_versions = muslc_versions,
osx_versions = osx_versions,
)

for setting in config_settings:
ret["//_config" + setting] = repo

# Now for the versioned platform config settings, we need to select one
# that best fits the bill and if there are multiple wheels, e.g.
# manylinux_2_17_x86_64 and manylinux_2_28_x86_64, then we need to select
# the former when the glibc is in the range of [2.17, 2.28) and then chose
# the later if it is [2.28, ...). If the 2.28 wheel was not present in
# the hub, then we would need to use 2.17 for all the glibc version
# configurations.
#
# Here we add the version settings to a dict where we key the range of
# versions that the whl spans. If the wheel supports musl and glibc at
# the same time, we do this for each supported platform, hence the
# double dict.
for default_setting, versioned in all_versioned_settings.items():
versions = sorted(versioned)
min_version = versions[0]
max_version = versions[-1]

versioned_additions.setdefault(default_setting, {})[(min_version, max_version)] = struct(
repo = repo,
settings = versioned,
)

versioned = {}
for default_setting, candidates in versioned_additions.items():
# Sort the candidates by the range of versions the span, so that we
# start with the lowest version.
for _, candidate in sorted(candidates.items()):
# Set the default with the first candidate, which gives us the highest
# compatibility. If the users want to use a higher-version than the default
# they can configure the glibc_version flag.
versioned.setdefault("//_config" + default_setting, candidate.repo)

# We will be overwriting previously added entries, but that is intended.
for _, setting in candidate.settings.items():
versioned["//_config" + setting] = candidate.repo

ret.update(versioned)
return ret

def get_filename_config_settings(
def get_config_settings(
*,
filename,
target_platforms,
python_version,
glibc_versions = None,
muslc_versions = None,
osx_versions = None,
non_whl_prefix = "sdist"):
python_version):
"""Get the filename config settings.

Exposed only for unit tests.

Args:
filename: the distribution filename (can be a whl or an sdist).
target_platforms: list[str], target platforms in "{abi}_{os}_{cpu}" format.
glibc_versions: list[tuple[int, int]], list of versions.
muslc_versions: list[tuple[int, int]], list of versions.
osx_versions: list[tuple[int, int]], list of versions.
python_version: the python version to generate the config_settings for.
non_whl_prefix: the prefix of the config setting when the whl we don't have
a filename ending with ".whl".

Returns:
A tuple:
* A list of config settings that are generated by ./pip_config_settings.bzl
* The list of default version settings.
"""
prefixes = []
suffixes = []
setting_supported_versions = {}

if filename.endswith(".whl"):
parsed = parse_whl_name(filename)
if parsed.python_tag == "py2.py3":
py = "py_"
elif parsed.python_tag == "py3":
py = "py3_"
elif parsed.python_tag.startswith("cp"):
py = ""
else:
py = "py3_"

abi = parsed.abi_tag

# TODO @aignas 2025-04-20: test
abi, _, _ = abi.partition(".")

if parsed.platform_tag == "any":
prefixes = ["{}{}_any".format(py, abi)]
else:
prefixes = ["{}{}".format(py, abi)]
suffixes = _whl_config_setting_suffixes(
platform_tag = parsed.platform_tag,
glibc_versions = glibc_versions,
muslc_versions = muslc_versions,
osx_versions = osx_versions,
setting_supported_versions = setting_supported_versions,
)
else:
prefixes = [non_whl_prefix or ""]

py = "cp{}".format(python_version).replace(".", "")
prefixes = [
"{}_{}".format(py, prefix) if prefix else py
for prefix in prefixes
"cp{}".format(python_version).replace(".", ""),
]

versioned = {
":is_{}_{}".format(prefix, suffix): {
version: ":is_{}_{}".format(prefix, setting)
for version, setting in versions.items()
}
for prefix in prefixes
for suffix, versions in setting_supported_versions.items()
}

if suffixes or target_platforms or versioned:
if target_platforms:
target_platforms = target_platforms or []
suffixes = suffixes or [_non_versioned_platform(p) for p in target_platforms]
suffixes = [_non_versioned_platform(p) for p in target_platforms]
return [
":is_{}_{}".format(prefix, suffix)
for prefix in prefixes
for suffix in suffixes
], versioned
]
else:
return [":is_{}".format(p) for p in prefixes], setting_supported_versions

def _whl_config_setting_suffixes(
platform_tag,
glibc_versions,
muslc_versions,
osx_versions,
setting_supported_versions):
suffixes = []
for platform_tag in platform_tag.split("."):
for p in whl_target_platforms(platform_tag):
prefix = p.os
suffix = p.cpu
if "manylinux" in platform_tag:
prefix = "manylinux"
versions = glibc_versions
elif "musllinux" in platform_tag:
prefix = "musllinux"
versions = muslc_versions
elif p.os in ["linux", "windows"]:
versions = [(0, 0)]
elif p.os == "osx":
versions = osx_versions
if "universal2" in platform_tag:
suffix = "universal2"
else:
fail("Unsupported whl os: {}".format(p.os))

default_version_setting = "{}_{}".format(prefix, suffix)
supported_versions = {}
for v in versions:
if v == (0, 0):
suffixes.append(default_version_setting)
elif v >= p.version:
supported_versions[v] = "{}_{}_{}_{}".format(
prefix,
v[0],
v[1],
suffix,
)
if supported_versions:
setting_supported_versions[default_version_setting] = supported_versions

return suffixes
return [":is_{}".format(p) for p in prefixes]

def _non_versioned_platform(p, *, strict = False):
"""A small utility function that converts 'cp311_linux_x86_64' to 'linux_x86_64'.
Expand Down
Loading