aboutsummaryrefslogtreecommitdiffstats
path: root/main/python2/musl-find_library.patch
blob: 2d8d19719cecf26e93f97ac29b4953253b776f57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py
index 1e882e3..b265cc0 100644
--- a/Lib/ctypes/util.py
+++ b/Lib/ctypes/util.py
@@ -238,8 +238,37 @@ elif os.name == "posix":
                 return None
             return res.group(1)
 
+        def _is_elf(filepath):
+            try:
+                with open(filepath, 'rb') as fh:
+                    return fh.read(4) == b'\x7fELF'
+            except:
+                return False
+
+        def _find_libfile(name):
+            from glob import glob
+            # special case for libm, libcrypt and libpthread and musl
+            if name in ['m', 'crypt', 'pthread']:
+                name = 'c'
+            elif name in ['libm.so', 'libcrypt.so', 'libpthread.so']:
+                name = 'libc.so'
+            # search in standard locations
+            paths = ['/lib', '/usr/lib', '/usr/local/lib']
+            if 'LD_LIBRARY_PATH' in os.environ:
+                paths = os.environ['LD_LIBRARY_PATH'].split(':') + paths
+            for d in paths:
+                f = os.path.join(d, name)
+                if _is_elf(f):
+                    return os.path.basename(f)
+
+                prefix = os.path.join(d, 'lib'+name)
+                for suffix in ['.so', '.so.*', '.*.so.*']:
+                    for f in glob('{0}{1}'.format(prefix, suffix)):
+                        if _is_elf(f):
+                            return os.path.basename(f)
+
         def find_library(name):
-            return _findSoname_ldconfig(name) or _get_soname(_findLib_gcc(name))
+            return _find_libfile(name) or _findSoname_ldconfig(name) or _get_soname(_findLib_gcc(name))
 
 ################################################################
 # test code