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