Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 1 | import os |
| 2 | import shutil |
Antoine Pitrou | 8c52027 | 2011-04-23 17:51:04 +0200 | [diff] [blame] | 3 | import subprocess |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 4 | import sys |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5 | |
| 6 | # find_library(name) returns the pathname of a library, or None. |
| 7 | if os.name == "nt": |
Thomas Heller | 3eaaeb4 | 2008-05-23 17:26:46 +0000 | [diff] [blame] | 8 | |
| 9 | def _get_build_version(): |
| 10 | """Return the version of MSVC that was used to build Python. |
| 11 | |
| 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. |
| 14 | """ |
| 15 | # This function was copied from Lib/distutils/msvccompiler.py |
| 16 | prefix = "MSC v." |
| 17 | i = sys.version.find(prefix) |
| 18 | if i == -1: |
| 19 | return 6 |
| 20 | i = i + len(prefix) |
| 21 | s, rest = sys.version[i:].split(" ", 1) |
| 22 | majorVersion = int(s[:-2]) - 6 |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 23 | if majorVersion >= 13: |
| 24 | majorVersion += 1 |
Thomas Heller | 3eaaeb4 | 2008-05-23 17:26:46 +0000 | [diff] [blame] | 25 | minorVersion = int(s[2:3]) / 10.0 |
| 26 | # I don't think paths are affected by minor version in version 6 |
| 27 | if majorVersion == 6: |
| 28 | minorVersion = 0 |
| 29 | if majorVersion >= 6: |
| 30 | return majorVersion + minorVersion |
| 31 | # else we don't know what version of the compiler this is |
| 32 | return None |
| 33 | |
| 34 | def find_msvcrt(): |
| 35 | """Return the name of the VC runtime dll""" |
| 36 | version = _get_build_version() |
| 37 | if version is None: |
| 38 | # better be safe than sorry |
| 39 | return None |
| 40 | if version <= 6: |
| 41 | clibname = 'msvcrt' |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 42 | elif version <= 13: |
Thomas Heller | 3eaaeb4 | 2008-05-23 17:26:46 +0000 | [diff] [blame] | 43 | clibname = 'msvcr%d' % (version * 10) |
Steve Dower | 65e4cb1 | 2014-11-22 12:54:57 -0800 | [diff] [blame] | 44 | else: |
Steve Dower | 959ee7c | 2015-03-10 09:56:38 -0700 | [diff] [blame] | 45 | # CRT is no longer directly loadable. See issue23606 for the |
| 46 | # discussion about alternative approaches. |
| 47 | return None |
Thomas Heller | 3eaaeb4 | 2008-05-23 17:26:46 +0000 | [diff] [blame] | 48 | |
| 49 | # If python was built with in debug mode |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 50 | import importlib.machinery |
| 51 | if '_d.pyd' in importlib.machinery.EXTENSION_SUFFIXES: |
Thomas Heller | 3eaaeb4 | 2008-05-23 17:26:46 +0000 | [diff] [blame] | 52 | clibname += 'd' |
| 53 | return clibname+'.dll' |
| 54 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 55 | def find_library(name): |
Thomas Heller | 3eaaeb4 | 2008-05-23 17:26:46 +0000 | [diff] [blame] | 56 | if name in ('c', 'm'): |
| 57 | return find_msvcrt() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 58 | # See MSDN for the REAL search order. |
| 59 | for directory in os.environ['PATH'].split(os.pathsep): |
| 60 | fname = os.path.join(directory, name) |
Thomas Heller | 00cfc37 | 2009-05-05 19:04:40 +0000 | [diff] [blame] | 61 | if os.path.isfile(fname): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 62 | return fname |
| 63 | if fname.lower().endswith(".dll"): |
| 64 | continue |
| 65 | fname = fname + ".dll" |
Thomas Heller | 00cfc37 | 2009-05-05 19:04:40 +0000 | [diff] [blame] | 66 | if os.path.isfile(fname): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 67 | return fname |
| 68 | return None |
| 69 | |
| 70 | if os.name == "ce": |
| 71 | # search path according to MSDN: |
| 72 | # - absolute path specified by filename |
| 73 | # - The .exe launch directory |
| 74 | # - the Windows directory |
| 75 | # - ROM dll files (where are they?) |
| 76 | # - OEM specified search path: HKLM\Loader\SystemPath |
| 77 | def find_library(name): |
| 78 | return name |
| 79 | |
| 80 | if os.name == "posix" and sys.platform == "darwin": |
| 81 | from ctypes.macholib.dyld import dyld_find as _dyld_find |
| 82 | def find_library(name): |
| 83 | possible = ['lib%s.dylib' % name, |
| 84 | '%s.dylib' % name, |
| 85 | '%s.framework/%s' % (name, name)] |
| 86 | for name in possible: |
| 87 | try: |
| 88 | return _dyld_find(name) |
| 89 | except ValueError: |
| 90 | continue |
| 91 | return None |
| 92 | |
| 93 | elif os.name == "posix": |
| 94 | # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump |
Victor Stinner | 7fa767e | 2014-03-20 09:16:38 +0100 | [diff] [blame] | 95 | import re, tempfile |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 96 | |
| 97 | def _findLib_gcc(name): |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 98 | # Run GCC's linker with the -t (aka --trace) option and examine the |
| 99 | # library name it prints out. The GCC command will fail because we |
| 100 | # haven't supplied a proper program with main(), but that does not |
| 101 | # matter. |
| 102 | expr = os.fsencode(r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)) |
| 103 | |
| 104 | c_compiler = shutil.which('gcc') |
| 105 | if not c_compiler: |
| 106 | c_compiler = shutil.which('cc') |
| 107 | if not c_compiler: |
| 108 | # No C compiler available, give up |
| 109 | return None |
| 110 | |
| 111 | temp = tempfile.NamedTemporaryFile() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 112 | try: |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 113 | args = [c_compiler, '-Wl,-t', '-o', temp.name, '-l' + name] |
| 114 | |
| 115 | env = dict(os.environ) |
| 116 | env['LC_ALL'] = 'C' |
| 117 | env['LANG'] = 'C' |
Martin Panter | e1b3431 | 2016-06-14 04:08:30 +0000 | [diff] [blame] | 118 | try: |
| 119 | proc = subprocess.Popen(args, |
| 120 | stdout=subprocess.PIPE, |
| 121 | stderr=subprocess.STDOUT, |
| 122 | env=env) |
| 123 | except OSError: # E.g. bad executable |
| 124 | return None |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 125 | with proc: |
| 126 | trace = proc.stdout.read() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 127 | finally: |
| 128 | try: |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 129 | temp.close() |
Giampaolo Rodola' | 0166a28 | 2013-02-12 15:14:17 +0100 | [diff] [blame] | 130 | except FileNotFoundError: |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 131 | # Raised if the file was already removed, which is the normal |
| 132 | # behaviour of GCC if linking fails |
Giampaolo Rodola' | 0166a28 | 2013-02-12 15:14:17 +0100 | [diff] [blame] | 133 | pass |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 134 | res = re.search(expr, trace) |
| 135 | if not res: |
| 136 | return None |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 137 | return os.fsdecode(res.group(0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 138 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 139 | |
| 140 | if sys.platform == "sunos5": |
| 141 | # use /usr/ccs/bin/dump on solaris |
| 142 | def _get_soname(f): |
| 143 | if not f: |
| 144 | return None |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 145 | |
Martin Panter | e1b3431 | 2016-06-14 04:08:30 +0000 | [diff] [blame] | 146 | try: |
| 147 | proc = subprocess.Popen(("/usr/ccs/bin/dump", "-Lpv", f), |
| 148 | stdout=subprocess.PIPE, |
| 149 | stderr=subprocess.DEVNULL) |
| 150 | except OSError: # E.g. command not found |
| 151 | return None |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 152 | with proc: |
| 153 | data = proc.stdout.read() |
| 154 | res = re.search(br'\[.*\]\sSONAME\s+([^\s]+)', data) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 155 | if not res: |
| 156 | return None |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 157 | return os.fsdecode(res.group(1)) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 158 | else: |
| 159 | def _get_soname(f): |
| 160 | # assuming GNU binutils / ELF |
| 161 | if not f: |
| 162 | return None |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 163 | objdump = shutil.which('objdump') |
| 164 | if not objdump: |
| 165 | # objdump is not available, give up |
| 166 | return None |
| 167 | |
Martin Panter | e1b3431 | 2016-06-14 04:08:30 +0000 | [diff] [blame] | 168 | try: |
| 169 | proc = subprocess.Popen((objdump, '-p', '-j', '.dynamic', f), |
| 170 | stdout=subprocess.PIPE, |
| 171 | stderr=subprocess.DEVNULL) |
| 172 | except OSError: # E.g. bad executable |
| 173 | return None |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 174 | with proc: |
| 175 | dump = proc.stdout.read() |
| 176 | res = re.search(br'\sSONAME\s+([^\s]+)', dump) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 177 | if not res: |
| 178 | return None |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 179 | return os.fsdecode(res.group(1)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 180 | |
Victor Stinner | e674747 | 2011-08-21 00:39:18 +0200 | [diff] [blame] | 181 | if sys.platform.startswith(("freebsd", "openbsd", "dragonfly")): |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 182 | |
| 183 | def _num_version(libname): |
| 184 | # "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ] |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 185 | parts = libname.split(b".") |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 186 | nums = [] |
| 187 | try: |
| 188 | while parts: |
| 189 | nums.insert(0, int(parts.pop())) |
| 190 | except ValueError: |
| 191 | pass |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 192 | return nums or [sys.maxsize] |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 193 | |
| 194 | def find_library(name): |
| 195 | ename = re.escape(name) |
| 196 | expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename) |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 197 | expr = os.fsencode(expr) |
| 198 | |
Martin Panter | e1b3431 | 2016-06-14 04:08:30 +0000 | [diff] [blame] | 199 | try: |
| 200 | proc = subprocess.Popen(('/sbin/ldconfig', '-r'), |
| 201 | stdout=subprocess.PIPE, |
| 202 | stderr=subprocess.DEVNULL) |
| 203 | except OSError: # E.g. command not found |
| 204 | data = b'' |
| 205 | else: |
| 206 | with proc: |
| 207 | data = proc.stdout.read() |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 208 | |
Guido van Rossum | f4d4f8b | 2007-12-12 20:26:00 +0000 | [diff] [blame] | 209 | res = re.findall(expr, data) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 210 | if not res: |
| 211 | return _get_soname(_findLib_gcc(name)) |
Raymond Hettinger | d4cb56d | 2008-01-30 02:55:10 +0000 | [diff] [blame] | 212 | res.sort(key=_num_version) |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 213 | return os.fsdecode(res[-1]) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 214 | |
Benjamin Peterson | cfe3474 | 2013-02-03 19:25:11 -0500 | [diff] [blame] | 215 | elif sys.platform == "sunos5": |
| 216 | |
| 217 | def _findLib_crle(name, is64): |
| 218 | if not os.path.exists('/usr/bin/crle'): |
| 219 | return None |
| 220 | |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 221 | env = dict(os.environ) |
| 222 | env['LC_ALL'] = 'C' |
| 223 | |
Benjamin Peterson | cfe3474 | 2013-02-03 19:25:11 -0500 | [diff] [blame] | 224 | if is64: |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 225 | args = ('/usr/bin/crle', '-64') |
Benjamin Peterson | cfe3474 | 2013-02-03 19:25:11 -0500 | [diff] [blame] | 226 | else: |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 227 | args = ('/usr/bin/crle',) |
Benjamin Peterson | cfe3474 | 2013-02-03 19:25:11 -0500 | [diff] [blame] | 228 | |
Meador Inge | 8988ebf | 2016-04-30 21:56:59 -0500 | [diff] [blame] | 229 | paths = None |
Martin Panter | e1b3431 | 2016-06-14 04:08:30 +0000 | [diff] [blame] | 230 | try: |
| 231 | proc = subprocess.Popen(args, |
| 232 | stdout=subprocess.PIPE, |
| 233 | stderr=subprocess.DEVNULL, |
| 234 | env=env) |
| 235 | except OSError: # E.g. bad executable |
| 236 | return None |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 237 | with proc: |
| 238 | for line in proc.stdout: |
Nick Coghlan | 1889623 | 2013-11-24 12:53:50 +1000 | [diff] [blame] | 239 | line = line.strip() |
Martin Panter | bfb15ab | 2016-06-14 01:27:11 +0000 | [diff] [blame] | 240 | if line.startswith(b'Default Library Path (ELF):'): |
| 241 | paths = os.fsdecode(line).split()[4] |
Benjamin Peterson | cfe3474 | 2013-02-03 19:25:11 -0500 | [diff] [blame] | 242 | |
| 243 | if not paths: |
| 244 | return None |
| 245 | |
| 246 | for dir in paths.split(":"): |
| 247 | libfile = os.path.join(dir, "lib%s.so" % name) |
| 248 | if os.path.exists(libfile): |
| 249 | return libfile |
| 250 | |
| 251 | return None |
| 252 | |
| 253 | def find_library(name, is64 = False): |
| 254 | return _get_soname(_findLib_crle(name, is64) or _findLib_gcc(name)) |
| 255 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 256 | else: |
| 257 | |
Matthias Klose | 2c7e3ee | 2009-01-10 17:08:25 +0000 | [diff] [blame] | 258 | def _findSoname_ldconfig(name): |
| 259 | import struct |
| 260 | if struct.calcsize('l') == 4: |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 261 | machine = os.uname().machine + '-32' |
Matthias Klose | 2c7e3ee | 2009-01-10 17:08:25 +0000 | [diff] [blame] | 262 | else: |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 263 | machine = os.uname().machine + '-64' |
Matthias Klose | 2c7e3ee | 2009-01-10 17:08:25 +0000 | [diff] [blame] | 264 | mach_map = { |
| 265 | 'x86_64-64': 'libc6,x86-64', |
| 266 | 'ppc64-64': 'libc6,64bit', |
| 267 | 'sparc64-64': 'libc6,64bit', |
| 268 | 's390x-64': 'libc6,64bit', |
| 269 | 'ia64-64': 'libc6,IA-64', |
| 270 | } |
| 271 | abi_type = mach_map.get(machine, 'libc6') |
| 272 | |
| 273 | # XXX assuming GLIBC's ldconfig (with option -p) |
Martin Panter | 32f2eb4 | 2016-03-17 07:50:22 +0000 | [diff] [blame] | 274 | regex = r'\s+(lib%s\.[^\s]+)\s+\(%s' |
Martin Panter | b9f3114 | 2016-03-10 01:06:23 +0000 | [diff] [blame] | 275 | regex = os.fsencode(regex % (re.escape(name), abi_type)) |
Antoine Pitrou | 8c52027 | 2011-04-23 17:51:04 +0200 | [diff] [blame] | 276 | try: |
| 277 | with subprocess.Popen(['/sbin/ldconfig', '-p'], |
| 278 | stdin=subprocess.DEVNULL, |
| 279 | stderr=subprocess.DEVNULL, |
| 280 | stdout=subprocess.PIPE, |
| 281 | env={'LC_ALL': 'C', 'LANG': 'C'}) as p: |
| 282 | res = re.search(regex, p.stdout.read()) |
| 283 | if res: |
| 284 | return os.fsdecode(res.group(1)) |
| 285 | except OSError: |
| 286 | pass |
Matthias Klose | 2c7e3ee | 2009-01-10 17:08:25 +0000 | [diff] [blame] | 287 | |
Vinay Sajip | 82df3b3 | 2016-08-17 16:20:07 +0100 | [diff] [blame^] | 288 | def _findLib_ld(name): |
| 289 | # See issue #9998 for why this is needed |
| 290 | expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name) |
| 291 | cmd = ['ld', '-t'] |
| 292 | libpath = os.environ.get('LD_LIBRARY_PATH') |
| 293 | if libpath: |
| 294 | for d in libpath.split(':'): |
| 295 | cmd.extend(['-L', d]) |
| 296 | cmd.extend(['-o', os.devnull, '-l%s' % name]) |
| 297 | result = None |
| 298 | try: |
| 299 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 300 | stderr=subprocess.PIPE, |
| 301 | universal_newlines=True) |
| 302 | out, _ = p.communicate() |
| 303 | res = re.search(expr, os.fsdecode(out)) |
| 304 | if res: |
| 305 | result = res.group(0) |
| 306 | except Exception as e: |
| 307 | pass # result will be None |
| 308 | return result |
| 309 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 310 | def find_library(name): |
Vinay Sajip | 82df3b3 | 2016-08-17 16:20:07 +0100 | [diff] [blame^] | 311 | # See issue #9998 |
| 312 | return _findSoname_ldconfig(name) or \ |
| 313 | _get_soname(_findLib_gcc(name) or _findLib_ld(name)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 314 | |
| 315 | ################################################################ |
| 316 | # test code |
| 317 | |
| 318 | def test(): |
| 319 | from ctypes import cdll |
| 320 | if os.name == "nt": |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 321 | print(cdll.msvcrt) |
| 322 | print(cdll.load("msvcrt")) |
| 323 | print(find_library("msvcrt")) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 324 | |
| 325 | if os.name == "posix": |
| 326 | # find and load_version |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 327 | print(find_library("m")) |
| 328 | print(find_library("c")) |
| 329 | print(find_library("bz2")) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 330 | |
| 331 | # getattr |
| 332 | ## print cdll.m |
| 333 | ## print cdll.bz2 |
| 334 | |
| 335 | # load |
| 336 | if sys.platform == "darwin": |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 337 | print(cdll.LoadLibrary("libm.dylib")) |
| 338 | print(cdll.LoadLibrary("libcrypto.dylib")) |
| 339 | print(cdll.LoadLibrary("libSystem.dylib")) |
| 340 | print(cdll.LoadLibrary("System.framework/System")) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 341 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 342 | print(cdll.LoadLibrary("libm.so")) |
| 343 | print(cdll.LoadLibrary("libcrypt.so")) |
| 344 | print(find_library("crypt")) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 345 | |
| 346 | if __name__ == "__main__": |
| 347 | test() |