| | import collections |
| | import contextlib |
| | import functools |
| | import os |
| | import re |
| | import sys |
| | import warnings |
| | from typing import Dict, Generator, Iterator, NamedTuple, Optional, Sequence, Tuple |
| |
|
| | from ._elffile import EIClass, EIData, ELFFile, EMachine |
| |
|
| | EF_ARM_ABIMASK = 0xFF000000 |
| | EF_ARM_ABI_VER5 = 0x05000000 |
| | EF_ARM_ABI_FLOAT_HARD = 0x00000400 |
| |
|
| |
|
| | |
| | |
| | @contextlib.contextmanager |
| | def _parse_elf(path: str) -> Generator[Optional[ELFFile], None, None]: |
| | try: |
| | with open(path, "rb") as f: |
| | yield ELFFile(f) |
| | except (OSError, TypeError, ValueError): |
| | yield None |
| |
|
| |
|
| | def _is_linux_armhf(executable: str) -> bool: |
| | |
| | |
| | |
| | with _parse_elf(executable) as f: |
| | return ( |
| | f is not None |
| | and f.capacity == EIClass.C32 |
| | and f.encoding == EIData.Lsb |
| | and f.machine == EMachine.Arm |
| | and f.flags & EF_ARM_ABIMASK == EF_ARM_ABI_VER5 |
| | and f.flags & EF_ARM_ABI_FLOAT_HARD == EF_ARM_ABI_FLOAT_HARD |
| | ) |
| |
|
| |
|
| | def _is_linux_i686(executable: str) -> bool: |
| | with _parse_elf(executable) as f: |
| | return ( |
| | f is not None |
| | and f.capacity == EIClass.C32 |
| | and f.encoding == EIData.Lsb |
| | and f.machine == EMachine.I386 |
| | ) |
| |
|
| |
|
| | def _have_compatible_abi(executable: str, archs: Sequence[str]) -> bool: |
| | if "armv7l" in archs: |
| | return _is_linux_armhf(executable) |
| | if "i686" in archs: |
| | return _is_linux_i686(executable) |
| | allowed_archs = { |
| | "x86_64", |
| | "aarch64", |
| | "ppc64", |
| | "ppc64le", |
| | "s390x", |
| | "loongarch64", |
| | "riscv64", |
| | } |
| | return any(arch in allowed_archs for arch in archs) |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | _LAST_GLIBC_MINOR: Dict[int, int] = collections.defaultdict(lambda: 50) |
| |
|
| |
|
| | class _GLibCVersion(NamedTuple): |
| | major: int |
| | minor: int |
| |
|
| |
|
| | def _glibc_version_string_confstr() -> Optional[str]: |
| | """ |
| | Primary implementation of glibc_version_string using os.confstr. |
| | """ |
| | |
| | |
| | |
| | |
| | try: |
| | |
| | version_string: Optional[str] = os.confstr("CS_GNU_LIBC_VERSION") |
| | assert version_string is not None |
| | _, version = version_string.rsplit() |
| | except (AssertionError, AttributeError, OSError, ValueError): |
| | |
| | return None |
| | return version |
| |
|
| |
|
| | def _glibc_version_string_ctypes() -> Optional[str]: |
| | """ |
| | Fallback implementation of glibc_version_string using ctypes. |
| | """ |
| | try: |
| | import ctypes |
| | except ImportError: |
| | return None |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | try: |
| | process_namespace = ctypes.CDLL(None) |
| | except OSError: |
| | return None |
| |
|
| | try: |
| | gnu_get_libc_version = process_namespace.gnu_get_libc_version |
| | except AttributeError: |
| | |
| | |
| | return None |
| |
|
| | |
| | gnu_get_libc_version.restype = ctypes.c_char_p |
| | version_str: str = gnu_get_libc_version() |
| | |
| | if not isinstance(version_str, str): |
| | version_str = version_str.decode("ascii") |
| |
|
| | return version_str |
| |
|
| |
|
| | def _glibc_version_string() -> Optional[str]: |
| | """Returns glibc version string, or None if not using glibc.""" |
| | return _glibc_version_string_confstr() or _glibc_version_string_ctypes() |
| |
|
| |
|
| | def _parse_glibc_version(version_str: str) -> Tuple[int, int]: |
| | """Parse glibc version. |
| | |
| | We use a regexp instead of str.split because we want to discard any |
| | random junk that might come after the minor version -- this might happen |
| | in patched/forked versions of glibc (e.g. Linaro's version of glibc |
| | uses version strings like "2.20-2014.11"). See gh-3588. |
| | """ |
| | m = re.match(r"(?P<major>[0-9]+)\.(?P<minor>[0-9]+)", version_str) |
| | if not m: |
| | warnings.warn( |
| | f"Expected glibc version with 2 components major.minor," |
| | f" got: {version_str}", |
| | RuntimeWarning, |
| | ) |
| | return -1, -1 |
| | return int(m.group("major")), int(m.group("minor")) |
| |
|
| |
|
| | @functools.lru_cache() |
| | def _get_glibc_version() -> Tuple[int, int]: |
| | version_str = _glibc_version_string() |
| | if version_str is None: |
| | return (-1, -1) |
| | return _parse_glibc_version(version_str) |
| |
|
| |
|
| | |
| | def _is_compatible(arch: str, version: _GLibCVersion) -> bool: |
| | sys_glibc = _get_glibc_version() |
| | if sys_glibc < version: |
| | return False |
| | |
| | try: |
| | import _manylinux |
| | except ImportError: |
| | return True |
| | if hasattr(_manylinux, "manylinux_compatible"): |
| | result = _manylinux.manylinux_compatible(version[0], version[1], arch) |
| | if result is not None: |
| | return bool(result) |
| | return True |
| | if version == _GLibCVersion(2, 5): |
| | if hasattr(_manylinux, "manylinux1_compatible"): |
| | return bool(_manylinux.manylinux1_compatible) |
| | if version == _GLibCVersion(2, 12): |
| | if hasattr(_manylinux, "manylinux2010_compatible"): |
| | return bool(_manylinux.manylinux2010_compatible) |
| | if version == _GLibCVersion(2, 17): |
| | if hasattr(_manylinux, "manylinux2014_compatible"): |
| | return bool(_manylinux.manylinux2014_compatible) |
| | return True |
| |
|
| |
|
| | _LEGACY_MANYLINUX_MAP = { |
| | |
| | (2, 17): "manylinux2014", |
| | |
| | (2, 12): "manylinux2010", |
| | |
| | (2, 5): "manylinux1", |
| | } |
| |
|
| |
|
| | def platform_tags(archs: Sequence[str]) -> Iterator[str]: |
| | """Generate manylinux tags compatible to the current platform. |
| | |
| | :param archs: Sequence of compatible architectures. |
| | The first one shall be the closest to the actual architecture and be the part of |
| | platform tag after the ``linux_`` prefix, e.g. ``x86_64``. |
| | The ``linux_`` prefix is assumed as a prerequisite for the current platform to |
| | be manylinux-compatible. |
| | |
| | :returns: An iterator of compatible manylinux tags. |
| | """ |
| | if not _have_compatible_abi(sys.executable, archs): |
| | return |
| | |
| | too_old_glibc2 = _GLibCVersion(2, 16) |
| | if set(archs) & {"x86_64", "i686"}: |
| | |
| | too_old_glibc2 = _GLibCVersion(2, 4) |
| | current_glibc = _GLibCVersion(*_get_glibc_version()) |
| | glibc_max_list = [current_glibc] |
| | |
| | |
| | |
| | |
| | |
| | |
| | for glibc_major in range(current_glibc.major - 1, 1, -1): |
| | glibc_minor = _LAST_GLIBC_MINOR[glibc_major] |
| | glibc_max_list.append(_GLibCVersion(glibc_major, glibc_minor)) |
| | for arch in archs: |
| | for glibc_max in glibc_max_list: |
| | if glibc_max.major == too_old_glibc2.major: |
| | min_minor = too_old_glibc2.minor |
| | else: |
| | |
| | min_minor = -1 |
| | for glibc_minor in range(glibc_max.minor, min_minor, -1): |
| | glibc_version = _GLibCVersion(glibc_max.major, glibc_minor) |
| | tag = "manylinux_{}_{}".format(*glibc_version) |
| | if _is_compatible(arch, glibc_version): |
| | yield f"{tag}_{arch}" |
| | |
| | if glibc_version in _LEGACY_MANYLINUX_MAP: |
| | legacy_tag = _LEGACY_MANYLINUX_MAP[glibc_version] |
| | if _is_compatible(arch, glibc_version): |
| | yield f"{legacy_tag}_{arch}" |
| |
|