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.

glibc.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from __future__ import absolute_import
  2. import ctypes
  3. import re
  4. import warnings
  5. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  6. if MYPY_CHECK_RUNNING:
  7. from typing import Optional, Tuple # noqa: F401
  8. def glibc_version_string():
  9. # type: () -> Optional[str]
  10. "Returns glibc version string, or None if not using glibc."
  11. # ctypes.CDLL(None) internally calls dlopen(NULL), and as the dlopen
  12. # manpage says, "If filename is NULL, then the returned handle is for the
  13. # main program". This way we can let the linker do the work to figure out
  14. # which libc our process is actually using.
  15. process_namespace = ctypes.CDLL(None)
  16. try:
  17. gnu_get_libc_version = process_namespace.gnu_get_libc_version
  18. except AttributeError:
  19. # Symbol doesn't exist -> therefore, we are not linked to
  20. # glibc.
  21. return None
  22. # Call gnu_get_libc_version, which returns a string like "2.5"
  23. gnu_get_libc_version.restype = ctypes.c_char_p
  24. version_str = gnu_get_libc_version()
  25. # py2 / py3 compatibility:
  26. if not isinstance(version_str, str):
  27. version_str = version_str.decode("ascii")
  28. return version_str
  29. # Separated out from have_compatible_glibc for easier unit testing
  30. def check_glibc_version(version_str, required_major, minimum_minor):
  31. # type: (str, int, int) -> bool
  32. # Parse string and check against requested version.
  33. #
  34. # We use a regexp instead of str.split because we want to discard any
  35. # random junk that might come after the minor version -- this might happen
  36. # in patched/forked versions of glibc (e.g. Linaro's version of glibc
  37. # uses version strings like "2.20-2014.11"). See gh-3588.
  38. m = re.match(r"(?P<major>[0-9]+)\.(?P<minor>[0-9]+)", version_str)
  39. if not m:
  40. warnings.warn("Expected glibc version with 2 components major.minor,"
  41. " got: %s" % version_str, RuntimeWarning)
  42. return False
  43. return (int(m.group("major")) == required_major and
  44. int(m.group("minor")) >= minimum_minor)
  45. def have_compatible_glibc(required_major, minimum_minor):
  46. # type: (int, int) -> bool
  47. version_str = glibc_version_string() # type: Optional[str]
  48. if version_str is None:
  49. return False
  50. return check_glibc_version(version_str, required_major, minimum_minor)
  51. # platform.libc_ver regularly returns completely nonsensical glibc
  52. # versions. E.g. on my computer, platform says:
  53. #
  54. # ~$ python2.7 -c 'import platform; print(platform.libc_ver())'
  55. # ('glibc', '2.7')
  56. # ~$ python3.5 -c 'import platform; print(platform.libc_ver())'
  57. # ('glibc', '2.9')
  58. #
  59. # But the truth is:
  60. #
  61. # ~$ ldd --version
  62. # ldd (Debian GLIBC 2.22-11) 2.22
  63. #
  64. # This is unfortunate, because it means that the linehaul data on libc
  65. # versions that was generated by pip 8.1.2 and earlier is useless and
  66. # misleading. Solution: instead of using platform, use our code that actually
  67. # works.
  68. def libc_ver():
  69. # type: () -> Tuple[str, str]
  70. """Try to determine the glibc version
  71. Returns a tuple of strings (lib, version) which default to empty strings
  72. in case the lookup fails.
  73. """
  74. glibc_version = glibc_version_string()
  75. if glibc_version is None:
  76. return ("", "")
  77. else:
  78. return ("glibc", glibc_version)