blob: 629e38315fb5f45ac215fb88d1853ce3e30fc38c [file] [log] [blame]
Thomas Wouters477c8d52006-05-27 19:21:47 +00001import sys, os
Thomas Wouters477c8d52006-05-27 19:21:47 +00002
3# find_library(name) returns the pathname of a library, or None.
4if os.name == "nt":
5 def find_library(name):
6 # See MSDN for the REAL search order.
7 for directory in os.environ['PATH'].split(os.pathsep):
8 fname = os.path.join(directory, name)
9 if os.path.exists(fname):
10 return fname
11 if fname.lower().endswith(".dll"):
12 continue
13 fname = fname + ".dll"
14 if os.path.exists(fname):
15 return fname
16 return None
17
18if os.name == "ce":
19 # search path according to MSDN:
20 # - absolute path specified by filename
21 # - The .exe launch directory
22 # - the Windows directory
23 # - ROM dll files (where are they?)
24 # - OEM specified search path: HKLM\Loader\SystemPath
25 def find_library(name):
26 return name
27
28if os.name == "posix" and sys.platform == "darwin":
29 from ctypes.macholib.dyld import dyld_find as _dyld_find
30 def find_library(name):
31 possible = ['lib%s.dylib' % name,
32 '%s.dylib' % name,
33 '%s.framework/%s' % (name, name)]
34 for name in possible:
35 try:
36 return _dyld_find(name)
37 except ValueError:
38 continue
39 return None
40
41elif os.name == "posix":
42 # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
Thomas Wouters0e3f5912006-08-11 14:57:12 +000043 import re, tempfile, errno
Thomas Wouters477c8d52006-05-27 19:21:47 +000044
45 def _findLib_gcc(name):
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000046 expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000047 fdout, ccout = tempfile.mkstemp()
48 os.close(fdout)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000049 cmd = 'if type gcc >/dev/null 2>&1; then CC=gcc; else CC=cc; fi;' \
Thomas Wouters0e3f5912006-08-11 14:57:12 +000050 '$CC -Wl,-t -o ' + ccout + ' 2>&1 -l' + name
Thomas Wouters477c8d52006-05-27 19:21:47 +000051 try:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000052 f = os.popen(cmd)
53 trace = f.read()
54 f.close()
Thomas Wouters477c8d52006-05-27 19:21:47 +000055 finally:
56 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +000057 os.unlink(ccout)
Guido van Rossumb940e112007-01-10 16:19:56 +000058 except OSError as e:
Thomas Wouters0e3f5912006-08-11 14:57:12 +000059 if e.errno != errno.ENOENT:
60 raise
Thomas Wouters477c8d52006-05-27 19:21:47 +000061 res = re.search(expr, trace)
62 if not res:
63 return None
64 return res.group(0)
65
Thomas Wouters1b7f8912007-09-19 03:06:30 +000066
67 if sys.platform == "sunos5":
68 # use /usr/ccs/bin/dump on solaris
69 def _get_soname(f):
70 if not f:
71 return None
72 cmd = "/usr/ccs/bin/dump -Lpv 2>/dev/null " + f
73 res = re.search(r'\[.*\]\sSONAME\s+([^\s]+)', os.popen(cmd).read())
74 if not res:
75 return None
76 return res.group(1)
77 else:
78 def _get_soname(f):
79 # assuming GNU binutils / ELF
80 if not f:
81 return None
82 cmd = "objdump -p -j .dynamic 2>/dev/null " + f
83 res = re.search(r'\sSONAME\s+([^\s]+)', os.popen(cmd).read())
84 if not res:
85 return None
86 return res.group(1)
Thomas Wouters477c8d52006-05-27 19:21:47 +000087
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000088 if (sys.platform.startswith("freebsd")
89 or sys.platform.startswith("openbsd")
90 or sys.platform.startswith("dragonfly")):
91
92 def _num_version(libname):
93 # "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ]
94 parts = libname.split(".")
95 nums = []
96 try:
97 while parts:
98 nums.insert(0, int(parts.pop()))
99 except ValueError:
100 pass
Christian Heimesa37d4c62007-12-04 23:02:19 +0000101 return nums or [ sys.maxsize ]
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000102
103 def find_library(name):
104 ename = re.escape(name)
105 expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename)
106 res = re.findall(expr,
107 os.popen('/sbin/ldconfig -r 2>/dev/null').read())
108 if not res:
109 return _get_soname(_findLib_gcc(name))
110 res.sort(cmp= lambda x,y: cmp(_num_version(x), _num_version(y)))
111 return res[-1]
112
113 else:
114
115 def _findLib_ldconfig(name):
116 # XXX assuming GLIBC's ldconfig (with option -p)
117 expr = r'/[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
118 res = re.search(expr,
119 os.popen('/sbin/ldconfig -p 2>/dev/null').read())
120 if not res:
121 # Hm, this works only for libs needed by the python executable.
122 cmd = 'ldd %s 2>/dev/null' % sys.executable
123 res = re.search(expr, os.popen(cmd).read())
124 if not res:
125 return None
126 return res.group(0)
127
128 def find_library(name):
129 return _get_soname(_findLib_ldconfig(name) or _findLib_gcc(name))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000130
131################################################################
132# test code
133
134def test():
135 from ctypes import cdll
136 if os.name == "nt":
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000137 print(cdll.msvcrt)
138 print(cdll.load("msvcrt"))
139 print(find_library("msvcrt"))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000140
141 if os.name == "posix":
142 # find and load_version
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000143 print(find_library("m"))
144 print(find_library("c"))
145 print(find_library("bz2"))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000146
147 # getattr
148## print cdll.m
149## print cdll.bz2
150
151 # load
152 if sys.platform == "darwin":
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000153 print(cdll.LoadLibrary("libm.dylib"))
154 print(cdll.LoadLibrary("libcrypto.dylib"))
155 print(cdll.LoadLibrary("libSystem.dylib"))
156 print(cdll.LoadLibrary("System.framework/System"))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000157 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000158 print(cdll.LoadLibrary("libm.so"))
159 print(cdll.LoadLibrary("libcrypt.so"))
160 print(find_library("crypt"))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000161
162if __name__ == "__main__":
163 test()