Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_manylinux.py 8.7KB

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