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.
8 def find_library(name
):
9 # See MSDN for the REAL search order.
10 for directory
in os
.environ
['PATH'].split(os
.pathsep
):
11 fname
= os
.path
.join(directory
, name
)
12 if os
.path
.exists(fname
):
14 if fname
.lower().endswith(".dll"):
16 fname
= fname
+ ".dll"
17 if os
.path
.exists(fname
):
22 # search path according to MSDN:
23 # - absolute path specified by filename
24 # - The .exe launch directory
25 # - the Windows directory
26 # - ROM dll files (where are they?)
27 # - OEM specified search path: HKLM\Loader\SystemPath
28 def find_library(name
):
31 if os
.name
== "posix" and sys
.platform
== "darwin":
32 from ctypes
.macholib
.dyld
import dyld_find
as _dyld_find
33 def find_library(name
):
34 possible
= ['lib%s.dylib' % name
,
36 '%s.framework/%s' % (name
, name
)]
39 return _dyld_find(name
)
44 elif os
.name
== "posix":
45 # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
46 import re
, tempfile
, errno
48 def _findLib_gcc(name
):
49 expr
= r
'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re
.escape(name
)
50 fdout
, ccout
= tempfile
.mkstemp()
52 cmd
= 'if type gcc >/dev/null 2>&1; then CC=gcc; else CC=cc; fi;' \
53 '$CC -Wl,-t -o ' + ccout
+ ' 2>&1 -l' + name
62 if e
.errno
!= errno
.ENOENT
:
64 res
= re
.search(expr
, trace
)
70 # assuming GNU binutils / ELF
73 cmd
= "objdump -p -j .dynamic 2>/dev/null " + f
74 res
= re
.search(r
'\sSONAME\s+([^\s]+)', os
.popen(cmd
).read())
79 if (sys
.platform
.startswith("freebsd")
80 or sys
.platform
.startswith("openbsd")
81 or sys
.platform
.startswith("dragonfly")):
83 def _num_version(libname
):
84 # "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ]
85 parts
= libname
.split(".")
89 nums
.insert(0, int(parts
.pop()))
92 return nums
or [ sys
.maxint
]
94 def find_library(name
):
95 ename
= re
.escape(name
)
96 expr
= r
':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename
, ename
)
97 res
= re
.findall(expr
,
98 os
.popen('/sbin/ldconfig -r 2>/dev/null').read())
100 return _get_soname(_findLib_gcc(name
))
101 res
.sort(cmp= lambda x
,y
: cmp(_num_version(x
), _num_version(y
)))
106 def _findLib_ldconfig(name
):
107 # XXX assuming GLIBC's ldconfig (with option -p)
108 expr
= r
'/[^\(\)\s]*lib%s\.[^\(\)\s]*' % re
.escape(name
)
109 res
= re
.search(expr
,
110 os
.popen('/sbin/ldconfig -p 2>/dev/null').read())
112 # Hm, this works only for libs needed by the python executable.
113 cmd
= 'ldd %s 2>/dev/null' % sys
.executable
114 res
= re
.search(expr
, os
.popen(cmd
).read())
119 def find_library(name
):
120 return _get_soname(_findLib_ldconfig(name
) or _findLib_gcc(name
))
122 ################################################################
126 from ctypes
import cdll
129 print cdll
.load("msvcrt")
130 print find_library("msvcrt")
132 if os
.name
== "posix":
133 # find and load_version
134 print find_library("m")
135 print find_library("c")
136 print find_library("bz2")
143 if sys
.platform
== "darwin":
144 print cdll
.LoadLibrary("libm.dylib")
145 print cdll
.LoadLibrary("libcrypto.dylib")
146 print cdll
.LoadLibrary("libSystem.dylib")
147 print cdll
.LoadLibrary("System.framework/System")
149 print cdll
.LoadLibrary("libm.so")
150 print cdll
.LoadLibrary("libcrypt.so")
151 print find_library("crypt")
153 if __name__
== "__main__":