1 ######################################################################
2 # This file should be kept compatible with Python 2.3, see PEP 291. #
3 ######################################################################
6 # find_library(name) returns the pathname of a library, or None.
9 def _get_build_version():
10 """Return the version of MSVC that was used to build Python.
12 For Python 2.3 and up, the version number is included in
13 sys.version. For earlier versions, assume the compiler is MSVC 6.
15 # This function was copied from Lib/distutils/msvccompiler.py
17 i
= sys
.version
.find(prefix
)
21 s
, rest
= sys
.version
[i
:].split(" ", 1)
22 majorVersion
= int(s
[:-2]) - 6
23 minorVersion
= int(s
[2:3]) / 10.0
24 # I don't think paths are affected by minor version in version 6
28 return majorVersion
+ minorVersion
29 # else we don't know what version of the compiler this is
33 """Return the name of the VC runtime dll"""
34 version
= _get_build_version()
36 # better be safe than sorry
41 clibname
= 'msvcr%d' % (version
* 10)
43 # If python was built with in debug mode
45 if imp
.get_suffixes()[0][0] == '_d.pyd':
47 return clibname
+'.dll'
49 def find_library(name
):
50 if name
in ('c', 'm'):
52 # See MSDN for the REAL search order.
53 for directory
in os
.environ
['PATH'].split(os
.pathsep
):
54 fname
= os
.path
.join(directory
, name
)
55 if os
.path
.exists(fname
):
57 if fname
.lower().endswith(".dll"):
59 fname
= fname
+ ".dll"
60 if os
.path
.exists(fname
):
65 # search path according to MSDN:
66 # - absolute path specified by filename
67 # - The .exe launch directory
68 # - the Windows directory
69 # - ROM dll files (where are they?)
70 # - OEM specified search path: HKLM\Loader\SystemPath
71 def find_library(name
):
74 if os
.name
== "posix" and sys
.platform
== "darwin":
75 from ctypes
.macholib
.dyld
import dyld_find
as _dyld_find
76 def find_library(name
):
77 possible
= ['lib%s.dylib' % name
,
79 '%s.framework/%s' % (name
, name
)]
82 return _dyld_find(name
)
87 elif os
.name
== "posix":
88 # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
89 import re
, tempfile
, errno
91 def _findLib_gcc(name
):
92 expr
= r
'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re
.escape(name
)
93 fdout
, ccout
= tempfile
.mkstemp()
95 cmd
= 'if type gcc >/dev/null 2>&1; then CC=gcc; else CC=cc; fi;' \
96 '$CC -Wl,-t -o ' + ccout
+ ' 2>&1 -l' + name
105 if e
.errno
!= errno
.ENOENT
:
107 res
= re
.search(expr
, trace
)
113 if sys
.platform
== "sunos5":
114 # use /usr/ccs/bin/dump on solaris
118 cmd
= "/usr/ccs/bin/dump -Lpv 2>/dev/null " + f
119 res
= re
.search(r
'\[.*\]\sSONAME\s+([^\s]+)', os
.popen(cmd
).read())
125 # assuming GNU binutils / ELF
128 cmd
= "objdump -p -j .dynamic 2>/dev/null " + f
129 res
= re
.search(r
'\sSONAME\s+([^\s]+)', os
.popen(cmd
).read())
134 if (sys
.platform
.startswith("freebsd")
135 or sys
.platform
.startswith("openbsd")
136 or sys
.platform
.startswith("dragonfly")):
138 def _num_version(libname
):
139 # "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ]
140 parts
= libname
.split(".")
144 nums
.insert(0, int(parts
.pop()))
147 return nums
or [ sys
.maxint
]
149 def find_library(name
):
150 ename
= re
.escape(name
)
151 expr
= r
':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename
, ename
)
152 res
= re
.findall(expr
,
153 os
.popen('/sbin/ldconfig -r 2>/dev/null').read())
155 return _get_soname(_findLib_gcc(name
))
156 res
.sort(cmp= lambda x
,y
: cmp(_num_version(x
), _num_version(y
)))
161 def _findLib_ldconfig(name
):
162 # XXX assuming GLIBC's ldconfig (with option -p)
163 expr
= r
'/[^\(\)\s]*lib%s\.[^\(\)\s]*' % re
.escape(name
)
164 res
= re
.search(expr
,
165 os
.popen('/sbin/ldconfig -p 2>/dev/null').read())
167 # Hm, this works only for libs needed by the python executable.
168 cmd
= 'ldd %s 2>/dev/null' % sys
.executable
169 res
= re
.search(expr
, os
.popen(cmd
).read())
174 def find_library(name
):
175 return _get_soname(_findLib_ldconfig(name
) or _findLib_gcc(name
))
177 ################################################################
181 from ctypes
import cdll
184 print cdll
.load("msvcrt")
185 print find_library("msvcrt")
187 if os
.name
== "posix":
188 # find and load_version
189 print find_library("m")
190 print find_library("c")
191 print find_library("bz2")
198 if sys
.platform
== "darwin":
199 print cdll
.LoadLibrary("libm.dylib")
200 print cdll
.LoadLibrary("libcrypto.dylib")
201 print cdll
.LoadLibrary("libSystem.dylib")
202 print cdll
.LoadLibrary("System.framework/System")
204 print cdll
.LoadLibrary("libm.so")
205 print cdll
.LoadLibrary("libcrypt.so")
206 print find_library("crypt")
208 if __name__
== "__main__":