Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1 | import sys, os |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2 | |
| 3 | # find_library(name) returns the pathname of a library, or None. |
| 4 | if os.name == "nt": |
Thomas Heller | 3eaaeb4 | 2008-05-23 17:26:46 +0000 | [diff] [blame] | 5 | |
| 6 | def _get_build_version(): |
| 7 | """Return the version of MSVC that was used to build Python. |
| 8 | |
| 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. |
| 11 | """ |
| 12 | # This function was copied from Lib/distutils/msvccompiler.py |
| 13 | prefix = "MSC v." |
| 14 | i = sys.version.find(prefix) |
| 15 | if i == -1: |
| 16 | return 6 |
| 17 | i = i + len(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 |
| 22 | if majorVersion == 6: |
| 23 | minorVersion = 0 |
| 24 | if majorVersion >= 6: |
| 25 | return majorVersion + minorVersion |
| 26 | # else we don't know what version of the compiler this is |
| 27 | return None |
| 28 | |
| 29 | def find_msvcrt(): |
| 30 | """Return the name of the VC runtime dll""" |
| 31 | version = _get_build_version() |
| 32 | if version is None: |
| 33 | # better be safe than sorry |
| 34 | return None |
| 35 | if version <= 6: |
| 36 | clibname = 'msvcrt' |
| 37 | else: |
| 38 | clibname = 'msvcr%d' % (version * 10) |
| 39 | |
| 40 | # If python was built with in debug mode |
| 41 | import imp |
| 42 | if imp.get_suffixes()[0][0] == '_d.pyd': |
| 43 | clibname += 'd' |
| 44 | return clibname+'.dll' |
| 45 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 46 | def find_library(name): |
Thomas Heller | 3eaaeb4 | 2008-05-23 17:26:46 +0000 | [diff] [blame] | 47 | if name in ('c', 'm'): |
| 48 | return find_msvcrt() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 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): |
| 53 | return fname |
| 54 | if fname.lower().endswith(".dll"): |
| 55 | continue |
| 56 | fname = fname + ".dll" |
| 57 | if os.path.exists(fname): |
| 58 | return fname |
| 59 | return None |
| 60 | |
| 61 | if os.name == "ce": |
| 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): |
| 69 | return name |
| 70 | |
| 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, |
| 75 | '%s.dylib' % name, |
| 76 | '%s.framework/%s' % (name, name)] |
| 77 | for name in possible: |
| 78 | try: |
| 79 | return _dyld_find(name) |
| 80 | except ValueError: |
| 81 | continue |
| 82 | return None |
| 83 | |
| 84 | elif os.name == "posix": |
| 85 | # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 86 | import re, tempfile, errno |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 87 | |
| 88 | def _findLib_gcc(name): |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 89 | expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 90 | fdout, ccout = tempfile.mkstemp() |
| 91 | os.close(fdout) |
Matthias Klose | 2c7e3ee | 2009-01-10 17:08:25 +0000 | [diff] [blame] | 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;' \ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 93 | '$CC -Wl,-t -o ' + ccout + ' 2>&1 -l' + name |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 94 | try: |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 95 | f = os.popen(cmd) |
Guido van Rossum | f4d4f8b | 2007-12-12 20:26:00 +0000 | [diff] [blame] | 96 | try: |
| 97 | trace = f.read() |
| 98 | finally: |
Matthias Klose | 2c7e3ee | 2009-01-10 17:08:25 +0000 | [diff] [blame] | 99 | rv = f.close() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 100 | finally: |
| 101 | try: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 102 | os.unlink(ccout) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 103 | except OSError as e: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 104 | if e.errno != errno.ENOENT: |
| 105 | raise |
Matthias Klose | 2c7e3ee | 2009-01-10 17:08:25 +0000 | [diff] [blame] | 106 | if rv == 10: |
Benjamin Peterson | 17bf082 | 2009-01-10 22:37:11 +0000 | [diff] [blame] | 107 | raise OSError('gcc or cc command not found') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 108 | res = re.search(expr, trace) |
| 109 | if not res: |
| 110 | return None |
| 111 | return res.group(0) |
| 112 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 113 | |
| 114 | if sys.platform == "sunos5": |
| 115 | # use /usr/ccs/bin/dump on solaris |
| 116 | def _get_soname(f): |
| 117 | if not f: |
| 118 | return None |
| 119 | cmd = "/usr/ccs/bin/dump -Lpv 2>/dev/null " + f |
Guido van Rossum | f4d4f8b | 2007-12-12 20:26:00 +0000 | [diff] [blame] | 120 | f = os.popen(cmd) |
| 121 | try: |
| 122 | data = f.read() |
| 123 | finally: |
| 124 | f.close() |
| 125 | res = re.search(r'\[.*\]\sSONAME\s+([^\s]+)', data) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 126 | if not res: |
| 127 | return None |
| 128 | return res.group(1) |
| 129 | else: |
| 130 | def _get_soname(f): |
| 131 | # assuming GNU binutils / ELF |
| 132 | if not f: |
| 133 | return None |
Matthias Klose | 2c7e3ee | 2009-01-10 17:08:25 +0000 | [diff] [blame] | 134 | cmd = 'if ! type objdump >/dev/null 2>&1; then exit 10; fi;' \ |
| 135 | "objdump -p -j .dynamic 2>/dev/null " + f |
| 136 | f = os.popen(cmd) |
| 137 | dump = f.read() |
| 138 | rv = f.close() |
| 139 | if rv == 10: |
Benjamin Peterson | 17bf082 | 2009-01-10 22:37:11 +0000 | [diff] [blame] | 140 | raise OSError('objdump command not found') |
Guido van Rossum | f4d4f8b | 2007-12-12 20:26:00 +0000 | [diff] [blame] | 141 | f = os.popen(cmd) |
| 142 | try: |
| 143 | data = f.read() |
| 144 | finally: |
| 145 | f.close() |
| 146 | res = re.search(r'\sSONAME\s+([^\s]+)', data) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 147 | if not res: |
| 148 | return None |
| 149 | return res.group(1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 150 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 151 | if (sys.platform.startswith("freebsd") |
| 152 | or sys.platform.startswith("openbsd") |
| 153 | or sys.platform.startswith("dragonfly")): |
| 154 | |
| 155 | def _num_version(libname): |
| 156 | # "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ] |
| 157 | parts = libname.split(".") |
| 158 | nums = [] |
| 159 | try: |
| 160 | while parts: |
| 161 | nums.insert(0, int(parts.pop())) |
| 162 | except ValueError: |
| 163 | pass |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 164 | return nums or [ sys.maxsize ] |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 165 | |
| 166 | def find_library(name): |
| 167 | ename = re.escape(name) |
| 168 | expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename) |
Guido van Rossum | f4d4f8b | 2007-12-12 20:26:00 +0000 | [diff] [blame] | 169 | f = os.popen('/sbin/ldconfig -r 2>/dev/null') |
| 170 | try: |
| 171 | data = f.read() |
| 172 | finally: |
| 173 | f.close() |
| 174 | res = re.findall(expr, data) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 175 | if not res: |
| 176 | return _get_soname(_findLib_gcc(name)) |
Raymond Hettinger | d4cb56d | 2008-01-30 02:55:10 +0000 | [diff] [blame] | 177 | res.sort(key=_num_version) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 178 | return res[-1] |
| 179 | |
| 180 | else: |
| 181 | |
| 182 | def _findLib_ldconfig(name): |
| 183 | # XXX assuming GLIBC's ldconfig (with option -p) |
| 184 | expr = r'/[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name) |
Guido van Rossum | f4d4f8b | 2007-12-12 20:26:00 +0000 | [diff] [blame] | 185 | f = os.popen('/sbin/ldconfig -p 2>/dev/null') |
| 186 | try: |
| 187 | data = f.read() |
| 188 | finally: |
| 189 | f.close() |
| 190 | res = re.search(expr, data) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 191 | if not res: |
| 192 | # Hm, this works only for libs needed by the python executable. |
| 193 | cmd = 'ldd %s 2>/dev/null' % sys.executable |
Guido van Rossum | f4d4f8b | 2007-12-12 20:26:00 +0000 | [diff] [blame] | 194 | f = os.popen(cmd) |
| 195 | try: |
| 196 | data = f.read() |
| 197 | finally: |
| 198 | f.close() |
| 199 | res = re.search(expr, data) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 200 | if not res: |
| 201 | return None |
| 202 | return res.group(0) |
| 203 | |
Matthias Klose | 2c7e3ee | 2009-01-10 17:08:25 +0000 | [diff] [blame] | 204 | def _findSoname_ldconfig(name): |
| 205 | import struct |
| 206 | if struct.calcsize('l') == 4: |
| 207 | machine = os.uname()[4] + '-32' |
| 208 | else: |
| 209 | machine = os.uname()[4] + '-64' |
| 210 | mach_map = { |
| 211 | 'x86_64-64': 'libc6,x86-64', |
| 212 | 'ppc64-64': 'libc6,64bit', |
| 213 | 'sparc64-64': 'libc6,64bit', |
| 214 | 's390x-64': 'libc6,64bit', |
| 215 | 'ia64-64': 'libc6,IA-64', |
| 216 | } |
| 217 | abi_type = mach_map.get(machine, 'libc6') |
| 218 | |
| 219 | # XXX assuming GLIBC's ldconfig (with option -p) |
| 220 | expr = r'(\S+)\s+\((%s(?:, OS ABI:[^\)]*)?)\)[^/]*(/[^\(\)\s]*lib%s\.[^\(\)\s]*)' \ |
| 221 | % (abi_type, re.escape(name)) |
| 222 | res = re.search(expr, |
| 223 | os.popen('/sbin/ldconfig -p 2>/dev/null').read()) |
| 224 | if not res: |
| 225 | return None |
| 226 | return res.group(1) |
| 227 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 228 | def find_library(name): |
Matthias Klose | 2c7e3ee | 2009-01-10 17:08:25 +0000 | [diff] [blame] | 229 | return _findSoname_ldconfig(name) or _get_soname(_findLib_gcc(name)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 230 | |
| 231 | ################################################################ |
| 232 | # test code |
| 233 | |
| 234 | def test(): |
| 235 | from ctypes import cdll |
| 236 | if os.name == "nt": |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 237 | print(cdll.msvcrt) |
| 238 | print(cdll.load("msvcrt")) |
| 239 | print(find_library("msvcrt")) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 240 | |
| 241 | if os.name == "posix": |
| 242 | # find and load_version |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 243 | print(find_library("m")) |
| 244 | print(find_library("c")) |
| 245 | print(find_library("bz2")) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 246 | |
| 247 | # getattr |
| 248 | ## print cdll.m |
| 249 | ## print cdll.bz2 |
| 250 | |
| 251 | # load |
| 252 | if sys.platform == "darwin": |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 253 | print(cdll.LoadLibrary("libm.dylib")) |
| 254 | print(cdll.LoadLibrary("libcrypto.dylib")) |
| 255 | print(cdll.LoadLibrary("libSystem.dylib")) |
| 256 | print(cdll.LoadLibrary("System.framework/System")) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 257 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 258 | print(cdll.LoadLibrary("libm.so")) |
| 259 | print(cdll.LoadLibrary("libcrypt.so")) |
| 260 | print(find_library("crypt")) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 261 | |
| 262 | if __name__ == "__main__": |
| 263 | test() |