4 # find_library(name) returns the pathname of a library, or None.
6 def find_library(name
):
7 # See MSDN for the REAL search order.
8 for directory
in os
.environ
['PATH'].split(os
.pathsep
):
9 fname
= os
.path
.join(directory
, name
)
10 if os
.path
.exists(fname
):
12 if fname
.lower().endswith(".dll"):
14 fname
= fname
+ ".dll"
15 if os
.path
.exists(fname
):
20 # search path according to MSDN:
21 # - absolute path specified by filename
22 # - The .exe launch directory
23 # - the Windows directory
24 # - ROM dll files (where are they?)
25 # - OEM specified search path: HKLM\Loader\SystemPath
26 def find_library(name
):
29 if os
.name
== "posix" and sys
.platform
== "darwin":
30 from ctypes
.macholib
.dyld
import dyld_find
as _dyld_find
31 def find_library(name
):
32 possible
= ['lib%s.dylib' % name
,
34 '%s.framework/%s' % (name
, name
)]
37 return _dyld_find(name
)
42 elif os
.name
== "posix":
43 # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
46 def _findLib_gcc(name
):
47 expr
= '[^\(\)\s]*lib%s\.[^\(\)\s]*' % name
48 cmd
= 'if type gcc &>/dev/null; then CC=gcc; else CC=cc; fi;' \
49 '$CC -Wl,-t -o /dev/null 2>&1 -l' + name
51 fdout
, outfile
= tempfile
.mkstemp()
59 if e
.errno
!= errno
.ENOENT
:
61 res
= re
.search(expr
, trace
)
66 def _findLib_ld(name
):
67 expr
= '/[^\(\)\s]*lib%s\.[^\(\)\s]*' % name
68 res
= re
.search(expr
, os
.popen('/sbin/ldconfig -p 2>/dev/null').read())
70 # Hm, this works only for libs needed by the python executable.
71 cmd
= 'ldd %s 2>/dev/null' % sys
.executable
72 res
= re
.search(expr
, os
.popen(cmd
).read())
78 cmd
= "objdump -p -j .dynamic 2>/dev/null " + f
79 res
= re
.search(r
'\sSONAME\s+([^\s]+)', os
.popen(cmd
).read())
84 def find_library(name
):
85 lib
= _findLib_ld(name
) or _findLib_gcc(name
)
88 return _get_soname(lib
)
90 ################################################################
94 from ctypes
import cdll
97 print cdll
.load("msvcrt")
98 print find_library("msvcrt")
100 if os
.name
== "posix":
101 # find and load_version
102 print find_library("m")
103 print find_library("c")
104 print find_library("bz2")
111 if sys
.platform
== "darwin":
112 print cdll
.LoadLibrary("libm.dylib")
113 print cdll
.LoadLibrary("libcrypto.dylib")
114 print cdll
.LoadLibrary("libSystem.dylib")
115 print cdll
.LoadLibrary("System.framework/System")
117 print cdll
.LoadLibrary("libm.so")
118 print cdll
.LoadLibrary("libcrypt.so")
119 print find_library("crypt")
121 if __name__
== "__main__":