3
# find_library(name) returns the pathname of a library, or None.
6
def _get_build_version():
7
"""Return the version of MSVC that was used to build Python.
9
For Python 2.3 and up, the version number is included in
10
sys.version. For earlier versions, assume the compiler is MSVC 6.
12
# This function was copied from Lib/distutils/msvccompiler.py
14
i = sys.version.find(prefix)
18
s, rest = sys.version[i:].split(" ", 1)
19
majorVersion = int(s[:-2]) - 6
20
minorVersion = int(s[2:3]) / 10.0
21
# I don't think paths are affected by minor version in version 6
25
return majorVersion + minorVersion
26
# else we don't know what version of the compiler this is
30
"""Return the name of the VC runtime dll"""
31
version = _get_build_version()
33
# better be safe than sorry
38
clibname = 'msvcr%d' % (version * 10)
40
# If python was built with in debug mode
42
if imp.get_suffixes()[0][0] == '_d.pyd':
44
return clibname+'.dll'
46
def find_library(name):
47
if name in ('c', 'm'):
49
# See MSDN for the REAL search order.
50
for directory in os.environ['PATH'].split(os.pathsep):
51
fname = os.path.join(directory, name)
52
if os.path.exists(fname):
54
if fname.lower().endswith(".dll"):
56
fname = fname + ".dll"
57
if os.path.exists(fname):
62
# search path according to MSDN:
63
# - absolute path specified by filename
64
# - The .exe launch directory
65
# - the Windows directory
66
# - ROM dll files (where are they?)
67
# - OEM specified search path: HKLM\Loader\SystemPath
68
def find_library(name):
71
if os.name == "posix" and sys.platform == "darwin":
72
from ctypes.macholib.dyld import dyld_find as _dyld_find
73
def find_library(name):
74
possible = ['lib%s.dylib' % name,
76
'%s.framework/%s' % (name, name)]
79
return _dyld_find(name)
84
elif os.name == "posix":
85
# Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
86
import re, tempfile, errno
88
def _findLib_gcc(name):
89
expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
90
fdout, ccout = tempfile.mkstemp()
92
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;' \
93
'$CC -Wl,-t -o ' + ccout + ' 2>&1 -l' + name
104
>>>>>>> .merge-rechts.r68487
109
if e.errno != errno.ENOENT:
112
raise OSError, 'gcc or cc command not found'
113
res = re.search(expr, trace)
119
if sys.platform == "sunos5":
120
# use /usr/ccs/bin/dump on solaris
124
cmd = "/usr/ccs/bin/dump -Lpv 2>/dev/null " + f
130
res = re.search(r'\[.*\]\sSONAME\s+([^\s]+)', data)
136
# assuming GNU binutils / ELF
139
cmd = 'if ! type objdump >/dev/null 2>&1; then exit 10; fi;' \
140
"objdump -p -j .dynamic 2>/dev/null " + f
145
raise OSError, 'objdump command not found'
151
res = re.search(r'\sSONAME\s+([^\s]+)', data)
156
if (sys.platform.startswith("freebsd")
157
or sys.platform.startswith("openbsd")
158
or sys.platform.startswith("dragonfly")):
160
def _num_version(libname):
161
# "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ]
162
parts = libname.split(".")
166
nums.insert(0, int(parts.pop()))
169
return nums or [ sys.maxsize ]
171
def find_library(name):
172
ename = re.escape(name)
173
expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename)
174
f = os.popen('/sbin/ldconfig -r 2>/dev/null')
179
res = re.findall(expr, data)
181
return _get_soname(_findLib_gcc(name))
182
res.sort(key=_num_version)
187
def _findLib_ldconfig(name):
188
# XXX assuming GLIBC's ldconfig (with option -p)
189
expr = r'/[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
190
f = os.popen('/sbin/ldconfig -p 2>/dev/null')
195
res = re.search(expr, data)
197
# Hm, this works only for libs needed by the python executable.
198
cmd = 'ldd %s 2>/dev/null' % sys.executable
204
res = re.search(expr, data)
209
def _findSoname_ldconfig(name):
211
if struct.calcsize('l') == 4:
212
machine = os.uname()[4] + '-32'
214
machine = os.uname()[4] + '-64'
216
'x86_64-64': 'libc6,x86-64',
217
'ppc64-64': 'libc6,64bit',
218
'sparc64-64': 'libc6,64bit',
219
's390x-64': 'libc6,64bit',
220
'ia64-64': 'libc6,IA-64',
222
abi_type = mach_map.get(machine, 'libc6')
224
# XXX assuming GLIBC's ldconfig (with option -p)
225
expr = r'(\S+)\s+\((%s(?:, OS ABI:[^\)]*)?)\)[^/]*(/[^\(\)\s]*lib%s\.[^\(\)\s]*)' \
226
% (abi_type, re.escape(name))
227
res = re.search(expr,
228
os.popen('/sbin/ldconfig -p 2>/dev/null').read())
233
def find_library(name):
234
return _findSoname_ldconfig(name) or _get_soname(_findLib_gcc(name))
236
################################################################
240
from ctypes import cdll
243
print(cdll.load("msvcrt"))
244
print(find_library("msvcrt"))
246
if os.name == "posix":
247
# find and load_version
248
print(find_library("m"))
249
print(find_library("c"))
250
print(find_library("bz2"))
257
if sys.platform == "darwin":
258
print(cdll.LoadLibrary("libm.dylib"))
259
print(cdll.LoadLibrary("libcrypto.dylib"))
260
print(cdll.LoadLibrary("libSystem.dylib"))
261
print(cdll.LoadLibrary("System.framework/System"))
263
print(cdll.LoadLibrary("libm.so"))
264
print(cdll.LoadLibrary("libcrypt.so"))
265
print(find_library("crypt"))
267
if __name__ == "__main__":