blob: 1bb7d1de7ef4e96c4783b1d496c71eeec1ed6de3 [file] [log] [blame]
Thomas Wouters477c8d52006-05-27 19:21:47 +00001import sys, os
Eric Smith5ccfa292009-10-23 12:56:11 +00002import contextlib
Thomas Wouters477c8d52006-05-27 19:21:47 +00003
4# find_library(name) returns the pathname of a library, or None.
5if os.name == "nt":
Thomas Heller3eaaeb42008-05-23 17:26:46 +00006
7 def _get_build_version():
8 """Return the version of MSVC that was used to build Python.
9
10 For Python 2.3 and up, the version number is included in
11 sys.version. For earlier versions, assume the compiler is MSVC 6.
12 """
13 # This function was copied from Lib/distutils/msvccompiler.py
14 prefix = "MSC v."
15 i = sys.version.find(prefix)
16 if i == -1:
17 return 6
18 i = i + len(prefix)
19 s, rest = sys.version[i:].split(" ", 1)
20 majorVersion = int(s[:-2]) - 6
21 minorVersion = int(s[2:3]) / 10.0
22 # I don't think paths are affected by minor version in version 6
23 if majorVersion == 6:
24 minorVersion = 0
25 if majorVersion >= 6:
26 return majorVersion + minorVersion
27 # else we don't know what version of the compiler this is
28 return None
29
30 def find_msvcrt():
31 """Return the name of the VC runtime dll"""
32 version = _get_build_version()
33 if version is None:
34 # better be safe than sorry
35 return None
36 if version <= 6:
37 clibname = 'msvcrt'
38 else:
39 clibname = 'msvcr%d' % (version * 10)
40
41 # If python was built with in debug mode
42 import imp
43 if imp.get_suffixes()[0][0] == '_d.pyd':
44 clibname += 'd'
45 return clibname+'.dll'
46
Thomas Wouters477c8d52006-05-27 19:21:47 +000047 def find_library(name):
Thomas Heller3eaaeb42008-05-23 17:26:46 +000048 if name in ('c', 'm'):
49 return find_msvcrt()
Thomas Wouters477c8d52006-05-27 19:21:47 +000050 # See MSDN for the REAL search order.
51 for directory in os.environ['PATH'].split(os.pathsep):
52 fname = os.path.join(directory, name)
Thomas Heller00cfc372009-05-05 19:04:40 +000053 if os.path.isfile(fname):
Thomas Wouters477c8d52006-05-27 19:21:47 +000054 return fname
55 if fname.lower().endswith(".dll"):
56 continue
57 fname = fname + ".dll"
Thomas Heller00cfc372009-05-05 19:04:40 +000058 if os.path.isfile(fname):
Thomas Wouters477c8d52006-05-27 19:21:47 +000059 return fname
60 return None
61
62if os.name == "ce":
63 # search path according to MSDN:
64 # - absolute path specified by filename
65 # - The .exe launch directory
66 # - the Windows directory
67 # - ROM dll files (where are they?)
68 # - OEM specified search path: HKLM\Loader\SystemPath
69 def find_library(name):
70 return name
71
72if os.name == "posix" and sys.platform == "darwin":
73 from ctypes.macholib.dyld import dyld_find as _dyld_find
74 def find_library(name):
75 possible = ['lib%s.dylib' % name,
76 '%s.dylib' % name,
77 '%s.framework/%s' % (name, name)]
78 for name in possible:
79 try:
80 return _dyld_find(name)
81 except ValueError:
82 continue
83 return None
84
85elif os.name == "posix":
86 # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
Thomas Wouters0e3f5912006-08-11 14:57:12 +000087 import re, tempfile, errno
Thomas Wouters477c8d52006-05-27 19:21:47 +000088
89 def _findLib_gcc(name):
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000090 expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000091 fdout, ccout = tempfile.mkstemp()
92 os.close(fdout)
Matthias Klose2c7e3ee2009-01-10 17:08:25 +000093 cmd = 'if type gcc >/dev/null 2>&1; then CC=gcc; elif type cc >/dev/null 2>&1; then CC=cc;else exit 10; fi;' \
Thomas Wouters0e3f5912006-08-11 14:57:12 +000094 '$CC -Wl,-t -o ' + ccout + ' 2>&1 -l' + name
Thomas Wouters477c8d52006-05-27 19:21:47 +000095 try:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000096 f = os.popen(cmd)
Guido van Rossumf4d4f8b2007-12-12 20:26:00 +000097 try:
98 trace = f.read()
99 finally:
Matthias Klose2c7e3ee2009-01-10 17:08:25 +0000100 rv = f.close()
Thomas Wouters477c8d52006-05-27 19:21:47 +0000101 finally:
102 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000103 os.unlink(ccout)
Guido van Rossumb940e112007-01-10 16:19:56 +0000104 except OSError as e:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000105 if e.errno != errno.ENOENT:
106 raise
Matthias Klose2c7e3ee2009-01-10 17:08:25 +0000107 if rv == 10:
Benjamin Peterson17bf0822009-01-10 22:37:11 +0000108 raise OSError('gcc or cc command not found')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000109 res = re.search(expr, trace)
110 if not res:
111 return None
112 return res.group(0)
113
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000114
115 if sys.platform == "sunos5":
116 # use /usr/ccs/bin/dump on solaris
117 def _get_soname(f):
118 if not f:
119 return None
120 cmd = "/usr/ccs/bin/dump -Lpv 2>/dev/null " + f
Eric Smith5ccfa292009-10-23 12:56:11 +0000121 with contextlib.closing(os.popen(cmd)) as f:
Guido van Rossumf4d4f8b2007-12-12 20:26:00 +0000122 data = f.read()
Guido van Rossumf4d4f8b2007-12-12 20:26:00 +0000123 res = re.search(r'\[.*\]\sSONAME\s+([^\s]+)', data)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000124 if not res:
125 return None
126 return res.group(1)
127 else:
128 def _get_soname(f):
129 # assuming GNU binutils / ELF
130 if not f:
131 return None
Matthias Klose2c7e3ee2009-01-10 17:08:25 +0000132 cmd = 'if ! type objdump >/dev/null 2>&1; then exit 10; fi;' \
133 "objdump -p -j .dynamic 2>/dev/null " + f
134 f = os.popen(cmd)
135 dump = f.read()
136 rv = f.close()
137 if rv == 10:
Benjamin Peterson17bf0822009-01-10 22:37:11 +0000138 raise OSError('objdump command not found')
Eric Smith5ccfa292009-10-23 12:56:11 +0000139 with contextlib.closing(os.popen(cmd)) as f:
Guido van Rossumf4d4f8b2007-12-12 20:26:00 +0000140 data = f.read()
Guido van Rossumf4d4f8b2007-12-12 20:26:00 +0000141 res = re.search(r'\sSONAME\s+([^\s]+)', data)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000142 if not res:
143 return None
144 return res.group(1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000145
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000146 if (sys.platform.startswith("freebsd")
147 or sys.platform.startswith("openbsd")
148 or sys.platform.startswith("dragonfly")):
149
150 def _num_version(libname):
151 # "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ]
152 parts = libname.split(".")
153 nums = []
154 try:
155 while parts:
156 nums.insert(0, int(parts.pop()))
157 except ValueError:
158 pass
Christian Heimesa37d4c62007-12-04 23:02:19 +0000159 return nums or [ sys.maxsize ]
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000160
161 def find_library(name):
162 ename = re.escape(name)
163 expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename)
Eric Smith5ccfa292009-10-23 12:56:11 +0000164 with contextlib.closing(os.popen('/sbin/ldconfig -r 2>/dev/null')) as f:
Guido van Rossumf4d4f8b2007-12-12 20:26:00 +0000165 data = f.read()
Guido van Rossumf4d4f8b2007-12-12 20:26:00 +0000166 res = re.findall(expr, data)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000167 if not res:
168 return _get_soname(_findLib_gcc(name))
Raymond Hettingerd4cb56d2008-01-30 02:55:10 +0000169 res.sort(key=_num_version)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000170 return res[-1]
171
172 else:
173
Matthias Klose2c7e3ee2009-01-10 17:08:25 +0000174 def _findSoname_ldconfig(name):
175 import struct
176 if struct.calcsize('l') == 4:
177 machine = os.uname()[4] + '-32'
178 else:
179 machine = os.uname()[4] + '-64'
180 mach_map = {
181 'x86_64-64': 'libc6,x86-64',
182 'ppc64-64': 'libc6,64bit',
183 'sparc64-64': 'libc6,64bit',
184 's390x-64': 'libc6,64bit',
185 'ia64-64': 'libc6,IA-64',
186 }
187 abi_type = mach_map.get(machine, 'libc6')
188
189 # XXX assuming GLIBC's ldconfig (with option -p)
Meador Ingeffeee352012-02-13 22:08:39 -0600190 expr = r'\s+(lib%s\.[^\s]+)\s+\(%s' % (re.escape(name), abi_type)
Matthias Klosecf1dd712010-09-15 13:06:09 +0000191 with contextlib.closing(os.popen('LC_ALL=C LANG=C /sbin/ldconfig -p 2>/dev/null')) as f:
Eric Smith81fd8042009-09-18 13:23:13 +0000192 data = f.read()
Eric Smith81fd8042009-09-18 13:23:13 +0000193 res = re.search(expr, data)
Matthias Klose2c7e3ee2009-01-10 17:08:25 +0000194 if not res:
195 return None
196 return res.group(1)
197
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000198 def find_library(name):
Matthias Klose2c7e3ee2009-01-10 17:08:25 +0000199 return _findSoname_ldconfig(name) or _get_soname(_findLib_gcc(name))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000200
201################################################################
202# test code
203
204def test():
205 from ctypes import cdll
206 if os.name == "nt":
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000207 print(cdll.msvcrt)
208 print(cdll.load("msvcrt"))
209 print(find_library("msvcrt"))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000210
211 if os.name == "posix":
212 # find and load_version
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000213 print(find_library("m"))
214 print(find_library("c"))
215 print(find_library("bz2"))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000216
217 # getattr
218## print cdll.m
219## print cdll.bz2
220
221 # load
222 if sys.platform == "darwin":
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000223 print(cdll.LoadLibrary("libm.dylib"))
224 print(cdll.LoadLibrary("libcrypto.dylib"))
225 print(cdll.LoadLibrary("libSystem.dylib"))
226 print(cdll.LoadLibrary("System.framework/System"))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000227 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000228 print(cdll.LoadLibrary("libm.so"))
229 print(cdll.LoadLibrary("libcrypt.so"))
230 print(find_library("crypt"))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000231
232if __name__ == "__main__":
233 test()