_manylinux.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. from __future__ import annotations
  2. import collections
  3. import contextlib
  4. import functools
  5. import os
  6. import re
  7. import sys
  8. import warnings
  9. from typing import Generator, Iterator, NamedTuple, Sequence
  10. from ._elffile import EIClass, EIData, ELFFile, EMachine
  11. EF_ARM_ABIMASK = 0xFF000000
  12. EF_ARM_ABI_VER5 = 0x05000000
  13. EF_ARM_ABI_FLOAT_HARD = 0x00000400
  14. # `os.PathLike` not a generic type until Python 3.9, so sticking with `str`
  15. # as the type for `path` until then.
  16. @contextlib.contextmanager
  17. def _parse_elf(path: str) -> Generator[ELFFile | None, None, None]:
  18. try:
  19. with open(path, "rb") as f:
  20. yield ELFFile(f)
  21. except (OSError, TypeError, ValueError):
  22. yield None
  23. def _is_linux_armhf(executable: str) -> bool:
  24. # hard-float ABI can be detected from the ELF header of the running
  25. # process
  26. # https://static.docs.arm.com/ihi0044/g/aaelf32.pdf
  27. with _parse_elf(executable) as f:
  28. return (
  29. f is not None
  30. and f.capacity == EIClass.C32
  31. and f.encoding == EIData.Lsb
  32. and f.machine == EMachine.Arm
  33. and f.flags & EF_ARM_ABIMASK == EF_ARM_ABI_VER5
  34. and f.flags & EF_ARM_ABI_FLOAT_HARD == EF_ARM_ABI_FLOAT_HARD
  35. )
  36. def _is_linux_i686(executable: str) -> bool:
  37. with _parse_elf(executable) as f:
  38. return (
  39. f is not None
  40. and f.capacity == EIClass.C32
  41. and f.encoding == EIData.Lsb
  42. and f.machine == EMachine.I386
  43. )
  44. def _have_compatible_abi(executable: str, archs: Sequence[str]) -> bool:
  45. if "armv7l" in archs:
  46. return _is_linux_armhf(executable)
  47. if "i686" in archs:
  48. return _is_linux_i686(executable)
  49. allowed_archs = {
  50. "x86_64",
  51. "aarch64",
  52. "ppc64",
  53. "ppc64le",
  54. "s390x",
  55. "loongarch64",
  56. "riscv64",
  57. }
  58. return any(arch in allowed_archs for arch in archs)
  59. # If glibc ever changes its major version, we need to know what the last
  60. # minor version was, so we can build the complete list of all versions.
  61. # For now, guess what the highest minor version might be, assume it will
  62. # be 50 for testing. Once this actually happens, update the dictionary
  63. # with the actual value.
  64. _LAST_GLIBC_MINOR: dict[int, int] = collections.defaultdict(lambda: 50)
  65. class _GLibCVersion(NamedTuple):
  66. major: int
  67. minor: int
  68. def _glibc_version_string_confstr() -> str | None:
  69. """
  70. Primary implementation of glibc_version_string using os.confstr.
  71. """
  72. # os.confstr is quite a bit faster than ctypes.DLL. It's also less likely
  73. # to be broken or missing. This strategy is used in the standard library
  74. # platform module.
  75. # https://github.com/python/cpython/blob/fcf1d003bf4f0100c/Lib/platform.py#L175-L183
  76. try:
  77. # Should be a string like "glibc 2.17".
  78. version_string: str | None = os.confstr("CS_GNU_LIBC_VERSION")
  79. assert version_string is not None
  80. _, version = version_string.rsplit()
  81. except (AssertionError, AttributeError, OSError, ValueError):
  82. # os.confstr() or CS_GNU_LIBC_VERSION not available (or a bad value)...
  83. return None
  84. return version
  85. def _glibc_version_string_ctypes() -> str | None:
  86. """
  87. Fallback implementation of glibc_version_string using ctypes.
  88. """
  89. try:
  90. import ctypes
  91. except ImportError:
  92. return None
  93. # ctypes.CDLL(None) internally calls dlopen(NULL), and as the dlopen
  94. # manpage says, "If filename is NULL, then the returned handle is for the
  95. # main program". This way we can let the linker do the work to figure out
  96. # which libc our process is actually using.
  97. #
  98. # We must also handle the special case where the executable is not a
  99. # dynamically linked executable. This can occur when using musl libc,
  100. # for example. In this situation, dlopen() will error, leading to an
  101. # OSError. Interestingly, at least in the case of musl, there is no
  102. # errno set on the OSError. The single string argument used to construct
  103. # OSError comes from libc itself and is therefore not portable to
  104. # hard code here. In any case, failure to call dlopen() means we
  105. # can proceed, so we bail on our attempt.
  106. try:
  107. process_namespace = ctypes.CDLL(None)
  108. except OSError:
  109. return None
  110. try:
  111. gnu_get_libc_version = process_namespace.gnu_get_libc_version
  112. except AttributeError:
  113. # Symbol doesn't exist -> therefore, we are not linked to
  114. # glibc.
  115. return None
  116. # Call gnu_get_libc_version, which returns a string like "2.5"
  117. gnu_get_libc_version.restype = ctypes.c_char_p
  118. version_str: str = gnu_get_libc_version()
  119. # py2 / py3 compatibility:
  120. if not isinstance(version_str, str):
  121. version_str = version_str.decode("ascii")
  122. return version_str
  123. def _glibc_version_string() -> str | None:
  124. """Returns glibc version string, or None if not using glibc."""
  125. return _glibc_version_string_confstr() or _glibc_version_string_ctypes()
  126. def _parse_glibc_version(version_str: str) -> tuple[int, int]:
  127. """Parse glibc version.
  128. We use a regexp instead of str.split because we want to discard any
  129. random junk that might come after the minor version -- this might happen
  130. in patched/forked versions of glibc (e.g. Linaro's version of glibc
  131. uses version strings like "2.20-2014.11"). See gh-3588.
  132. """
  133. m = re.match(r"(?P<major>[0-9]+)\.(?P<minor>[0-9]+)", version_str)
  134. if not m:
  135. warnings.warn(
  136. f"Expected glibc version with 2 components major.minor,"
  137. f" got: {version_str}",
  138. RuntimeWarning,
  139. stacklevel=2,
  140. )
  141. return -1, -1
  142. return int(m.group("major")), int(m.group("minor"))
  143. @functools.lru_cache
  144. def _get_glibc_version() -> tuple[int, int]:
  145. version_str = _glibc_version_string()
  146. if version_str is None:
  147. return (-1, -1)
  148. return _parse_glibc_version(version_str)
  149. # From PEP 513, PEP 600
  150. def _is_compatible(arch: str, version: _GLibCVersion) -> bool:
  151. sys_glibc = _get_glibc_version()
  152. if sys_glibc < version:
  153. return False
  154. # Check for presence of _manylinux module.
  155. try:
  156. import _manylinux
  157. except ImportError:
  158. return True
  159. if hasattr(_manylinux, "manylinux_compatible"):
  160. result = _manylinux.manylinux_compatible(version[0], version[1], arch)
  161. if result is not None:
  162. return bool(result)
  163. return True
  164. if version == _GLibCVersion(2, 5):
  165. if hasattr(_manylinux, "manylinux1_compatible"):
  166. return bool(_manylinux.manylinux1_compatible)
  167. if version == _GLibCVersion(2, 12):
  168. if hasattr(_manylinux, "manylinux2010_compatible"):
  169. return bool(_manylinux.manylinux2010_compatible)
  170. if version == _GLibCVersion(2, 17):
  171. if hasattr(_manylinux, "manylinux2014_compatible"):
  172. return bool(_manylinux.manylinux2014_compatible)
  173. return True
  174. _LEGACY_MANYLINUX_MAP = {
  175. # CentOS 7 w/ glibc 2.17 (PEP 599)
  176. (2, 17): "manylinux2014",
  177. # CentOS 6 w/ glibc 2.12 (PEP 571)
  178. (2, 12): "manylinux2010",
  179. # CentOS 5 w/ glibc 2.5 (PEP 513)
  180. (2, 5): "manylinux1",
  181. }
  182. def platform_tags(archs: Sequence[str]) -> Iterator[str]:
  183. """Generate manylinux tags compatible to the current platform.
  184. :param archs: Sequence of compatible architectures.
  185. The first one shall be the closest to the actual architecture and be the part of
  186. platform tag after the ``linux_`` prefix, e.g. ``x86_64``.
  187. The ``linux_`` prefix is assumed as a prerequisite for the current platform to
  188. be manylinux-compatible.
  189. :returns: An iterator of compatible manylinux tags.
  190. """
  191. if not _have_compatible_abi(sys.executable, archs):
  192. return
  193. # Oldest glibc to be supported regardless of architecture is (2, 17).
  194. too_old_glibc2 = _GLibCVersion(2, 16)
  195. if set(archs) & {"x86_64", "i686"}:
  196. # On x86/i686 also oldest glibc to be supported is (2, 5).
  197. too_old_glibc2 = _GLibCVersion(2, 4)
  198. current_glibc = _GLibCVersion(*_get_glibc_version())
  199. glibc_max_list = [current_glibc]
  200. # We can assume compatibility across glibc major versions.
  201. # https://sourceware.org/bugzilla/show_bug.cgi?id=24636
  202. #
  203. # Build a list of maximum glibc versions so that we can
  204. # output the canonical list of all glibc from current_glibc
  205. # down to too_old_glibc2, including all intermediary versions.
  206. for glibc_major in range(current_glibc.major - 1, 1, -1):
  207. glibc_minor = _LAST_GLIBC_MINOR[glibc_major]
  208. glibc_max_list.append(_GLibCVersion(glibc_major, glibc_minor))
  209. for arch in archs:
  210. for glibc_max in glibc_max_list:
  211. if glibc_max.major == too_old_glibc2.major:
  212. min_minor = too_old_glibc2.minor
  213. else:
  214. # For other glibc major versions oldest supported is (x, 0).
  215. min_minor = -1
  216. for glibc_minor in range(glibc_max.minor, min_minor, -1):
  217. glibc_version = _GLibCVersion(glibc_max.major, glibc_minor)
  218. tag = "manylinux_{}_{}".format(*glibc_version)
  219. if _is_compatible(arch, glibc_version):
  220. yield f"{tag}_{arch}"
  221. # Handle the legacy manylinux1, manylinux2010, manylinux2014 tags.
  222. if glibc_version in _LEGACY_MANYLINUX_MAP:
  223. legacy_tag = _LEGACY_MANYLINUX_MAP[glibc_version]
  224. if _is_compatible(arch, glibc_version):
  225. yield f"{legacy_tag}_{arch}"