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.

74 lines
2.7 KiB

  1. https://bugs.python.org/issue21622
  2. Based on the patch from Alpine Linux
  3. https://git.alpinelinux.org/aports/tree/main/python2/musl-find_library.patch
  4. --- a/Lib/ctypes/util.py
  5. +++ b/Lib/ctypes/util.py
  6. @@ -92,6 +92,8 @@ elif sys.platform.startswith("aix"):
  7. elif os.name == "posix":
  8. # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
  9. import re, tempfile
  10. + from glob import glob
  11. + musl_ldso = glob('/lib/ld-musl-*.so.1')
  12. def _is_elf(filename):
  13. "Return True if the given file is an ELF file"
  14. @@ -265,6 +267,57 @@ elif os.name == "posix":
  15. def find_library(name, is64 = False):
  16. return _get_soname(_findLib_crle(name, is64) or _findLib_gcc(name))
  17. + elif musl_ldso and os.path.isfile(musl_ldso[0]):
  18. +
  19. + def _is_elf(filepath):
  20. + try:
  21. + with open(filepath, 'rb') as fh:
  22. + return fh.read(4) == b'\x7fELF'
  23. + except:
  24. + return False
  25. +
  26. + def find_library(name):
  27. + # absolute name?
  28. + if os.path.isabs(name):
  29. + if _is_elf(name):
  30. + return name
  31. + else:
  32. + return None
  33. +
  34. + # special case for unified standard libs
  35. + stdlibs = ['libcrypt.so', 'libdl.so', 'libm.so', 'libpthread.so', 'libresolv.so', 'librt.so', 'libutil.so', 'libxnet.so']
  36. + if name in stdlibs:
  37. + name = 'libc.so'
  38. + elif ('lib' + name + '.so') in stdlibs:
  39. + name = 'c'
  40. +
  41. + paths = []
  42. + # read path list from /etc/ld-musl-$(ARCH).path
  43. + path_list = musl_ldso[0].replace('/lib/', '/etc/').replace('.so.1', '.path')
  44. + try:
  45. + with open(path_list, 'r') as fh:
  46. + paths = [path for line in fh for path in line.rstrip('\n').split(':') if path]
  47. + except:
  48. + paths = []
  49. + # default path list if /etc/ld-musl-$(ARCH).path is empty or does not exist
  50. + if not paths:
  51. + paths = ['/lib', '/usr/local/lib', '/usr/lib']
  52. +
  53. + # prepend paths from LD_LIBRARY_PATH
  54. + if 'LD_LIBRARY_PATH' in os.environ:
  55. + paths = os.environ['LD_LIBRARY_PATH'].split(':') + paths
  56. +
  57. + for d in paths:
  58. + f = os.path.join(d, name)
  59. + if _is_elf(f):
  60. + return os.path.basename(f)
  61. +
  62. + prefix = os.path.join(d, 'lib'+name)
  63. + for suffix in ['.so', '.so.*']:
  64. + for f in glob('{0}{1}'.format(prefix, suffix)):
  65. + if _is_elf(f):
  66. + return os.path.basename(f)
  67. +
  68. else:
  69. def _findSoname_ldconfig(name):