| 1 |
import os |
| 2 |
import shutil |
| 3 |
import subprocess |
| 4 |
import sys |
| 5 |
|
| 6 |
|
| 7 |
if os.name == "nt": |
| 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 |
|
| 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 |
| 23 |
if majorVersion >= 13: |
| 24 |
majorVersion += 1 |
| 25 |
minorVersion = int(s[2:3]) / 10.0 |
| 26 |
|
| 27 |
if majorVersion == 6: |
| 28 |
minorVersion = 0 |
| 29 |
if majorVersion >= 6: |
| 30 |
return majorVersion + minorVersion |
| 31 |
|
| 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 |
|
| 39 |
return None |
| 40 |
if version <= 6: |
| 41 |
clibname = 'msvcrt' |
| 42 |
elif version <= 13: |
| 43 |
clibname = 'msvcr%d' % (version * 10) |
| 44 |
else: |
| 45 |
|
| 46 |
|
| 47 |
return None |
| 48 |
|
| 49 |
|
| 50 |
import importlib.machinery |
| 51 |
if '_d.pyd' in importlib.machinery.EXTENSION_SUFFIXES: |
| 52 |
clibname += 'd' |
| 53 |
return clibname+'.dll' |
| 54 |
|
| 55 |
def find_library(name): |
| 56 |
if name in ('c', 'm'): |
| 57 |
return find_msvcrt() |
| 58 |
|
| 59 |
for directory in os.environ['PATH'].split(os.pathsep): |
| 60 |
fname = os.path.join(directory, name) |
| 61 |
if os.path.isfile(fname): |
| 62 |
return fname |
| 63 |
if fname.lower().endswith(".dll"): |
| 64 |
continue |
| 65 |
fname = fname + ".dll" |
| 66 |
if os.path.isfile(fname): |
| 67 |
return fname |
| 68 |
return None |
| 69 |
|
| 70 |
elif os.name == "posix" and sys.platform == "darwin": |
| 71 |
from ctypes.macholib.dyld import dyld_find as _dyld_find |
| 72 |
def find_library(name): |
| 73 |
possible = ['lib%s.dylib' % name, |
| 74 |
'%s.dylib' % name, |
| 75 |
'%s.framework/%s' % (name, name)] |
| 76 |
for name in possible: |
| 77 |
try: |
| 78 |
return _dyld_find(name) |
| 79 |
except ValueError: |
| 80 |
continue |
| 81 |
return None |
| 82 |
|
| 83 |
elif sys.platform.startswith("aix"): |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
from ctypes._aix import find_library |
| 91 |
|
| 92 |
elif os.name == "posix": |
| 93 |
|
| 94 |
import re, tempfile |
| 95 |
|
| 96 |
def _is_elf(filename): |
| 97 |
"Return True if the given file is an ELF file" |
| 98 |
elf_header = b'\x7fELF' |
| 99 |
try: |
| 100 |
with open(filename, 'br') as thefile: |
| 101 |
return thefile.read(4) == elf_header |
| 102 |
except FileNotFoundError: |
| 103 |
return False |
| 104 |
|
| 105 |
def _findLib_gcc(name): |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
expr = os.fsencode(r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)) |
| 111 |
|
| 112 |
c_compiler = shutil.which('gcc') |
| 113 |
if not c_compiler: |
| 114 |
c_compiler = shutil.which('cc') |
| 115 |
if not c_compiler: |
| 116 |
|
| 117 |
return None |
| 118 |
|
| 119 |
temp = tempfile.NamedTemporaryFile() |
| 120 |
try: |
| 121 |
args = [c_compiler, '-Wl,-t', '-o', temp.name, '-l' + name] |
| 122 |
|
| 123 |
env = dict(os.environ) |
| 124 |
env['LC_ALL'] = 'C' |
| 125 |
env['LANG'] = 'C' |
| 126 |
try: |
| 127 |
proc = subprocess.Popen(args, |
| 128 |
stdout=subprocess.PIPE, |
| 129 |
stderr=subprocess.STDOUT, |
| 130 |
env=env) |
| 131 |
except OSError: |
| 132 |
return None |
| 133 |
with proc: |
| 134 |
trace = proc.stdout.read() |
| 135 |
finally: |
| 136 |
try: |
| 137 |
temp.close() |
| 138 |
except FileNotFoundError: |
| 139 |
|
| 140 |
|
| 141 |
pass |
| 142 |
res = re.findall(expr, trace) |
| 143 |
if not res: |
| 144 |
return None |
| 145 |
|
| 146 |
for file in res: |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
if not _is_elf(file): |
| 151 |
continue |
| 152 |
return os.fsdecode(file) |
| 153 |
|
| 154 |
|
| 155 |
if sys.platform == "sunos5": |
| 156 |
|
| 157 |
def _get_soname(f): |
| 158 |
if not f: |
| 159 |
return None |
| 160 |
|
| 161 |
try: |
| 162 |
proc = subprocess.Popen(("/usr/ccs/bin/dump", "-Lpv", f), |
| 163 |
stdout=subprocess.PIPE, |
| 164 |
stderr=subprocess.DEVNULL) |
| 165 |
except OSError: |
| 166 |
return None |
| 167 |
with proc: |
| 168 |
data = proc.stdout.read() |
| 169 |
res = re.search(br'\[.*\]\sSONAME\s+([^\s]+)', data) |
| 170 |
if not res: |
| 171 |
return None |
| 172 |
return os.fsdecode(res.group(1)) |
| 173 |
else: |
| 174 |
def _get_soname(f): |
| 175 |
|
| 176 |
if not f: |
| 177 |
return None |
| 178 |
objdump = shutil.which('objdump') |
| 179 |
if not objdump: |
| 180 |
|
| 181 |
return None |
| 182 |
|
| 183 |
try: |
| 184 |
proc = subprocess.Popen((objdump, '-p', '-j', '.dynamic', f), |
| 185 |
stdout=subprocess.PIPE, |
| 186 |
stderr=subprocess.DEVNULL) |
| 187 |
except OSError: |
| 188 |
return None |
| 189 |
with proc: |
| 190 |
dump = proc.stdout.read() |
| 191 |
res = re.search(br'\sSONAME\s+([^\s]+)', dump) |
| 192 |
if not res: |
| 193 |
return None |
| 194 |
return os.fsdecode(res.group(1)) |
| 195 |
|
| 196 |
if sys.platform.startswith(("freebsd", "openbsd", "dragonfly")): |
| 197 |
|
| 198 |
def _num_version(libname): |
| 199 |
|
| 200 |
parts = libname.split(b".") |
| 201 |
nums = [] |
| 202 |
try: |
| 203 |
while parts: |
| 204 |
nums.insert(0, int(parts.pop())) |
| 205 |
except ValueError: |
| 206 |
pass |
| 207 |
return nums or [sys.maxsize] |
| 208 |
|
| 209 |
def find_library(name): |
| 210 |
ename = re.escape(name) |
| 211 |
expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename) |
| 212 |
expr = os.fsencode(expr) |
| 213 |
|
| 214 |
try: |
| 215 |
proc = subprocess.Popen(('/sbin/ldconfig', '-r'), |
| 216 |
stdout=subprocess.PIPE, |
| 217 |
stderr=subprocess.DEVNULL) |
| 218 |
except OSError: |
| 219 |
data = b'' |
| 220 |
else: |
| 221 |
with proc: |
| 222 |
data = proc.stdout.read() |
| 223 |
|
| 224 |
res = re.findall(expr, data) |
| 225 |
if not res: |
| 226 |
return _get_soname(_findLib_gcc(name)) |
| 227 |
res.sort(key=_num_version) |
| 228 |
return os.fsdecode(res[-1]) |
| 229 |
|
| 230 |
elif sys.platform == "sunos5": |
| 231 |
|
| 232 |
def _findLib_crle(name, is64): |
| 233 |
if not os.path.exists('/usr/bin/crle'): |
| 234 |
return None |
| 235 |
|
| 236 |
env = dict(os.environ) |
| 237 |
env['LC_ALL'] = 'C' |
| 238 |
|
| 239 |
if is64: |
| 240 |
args = ('/usr/bin/crle', '-64') |
| 241 |
else: |
| 242 |
args = ('/usr/bin/crle',) |
| 243 |
|
| 244 |
paths = None |
| 245 |
try: |
| 246 |
proc = subprocess.Popen(args, |
| 247 |
stdout=subprocess.PIPE, |
| 248 |
stderr=subprocess.DEVNULL, |
| 249 |
env=env) |
| 250 |
except OSError: |
| 251 |
return None |
| 252 |
with proc: |
| 253 |
for line in proc.stdout: |
| 254 |
line = line.strip() |
| 255 |
if line.startswith(b'Default Library Path (ELF):'): |
| 256 |
paths = os.fsdecode(line).split()[4] |
| 257 |
|
| 258 |
if not paths: |
| 259 |
return None |
| 260 |
|
| 261 |
for dir in paths.split(":"): |
| 262 |
libfile = os.path.join(dir, "lib%s.so" % name) |
| 263 |
if os.path.exists(libfile): |
| 264 |
return libfile |
| 265 |
|
| 266 |
return None |
| 267 |
|
| 268 |
def find_library(name, is64 = False): |
| 269 |
return _get_soname(_findLib_crle(name, is64) or _findLib_gcc(name)) |
| 270 |
|
| 271 |
else: |
| 272 |
|
| 273 |
def _findSoname_ldconfig(name): |
| 274 |
import struct |
| 275 |
if struct.calcsize('l') == 4: |
| 276 |
machine = os.uname().machine + '-32' |
| 277 |
else: |
| 278 |
machine = os.uname().machine + '-64' |
| 279 |
mach_map = { |
| 280 |
'x86_64-64': 'libc6,x86-64', |
| 281 |
'ppc64-64': 'libc6,64bit', |
| 282 |
'sparc64-64': 'libc6,64bit', |
| 283 |
's390x-64': 'libc6,64bit', |
| 284 |
'ia64-64': 'libc6,IA-64', |
| 285 |
} |
| 286 |
abi_type = mach_map.get(machine, 'libc6') |
| 287 |
|
| 288 |
|
| 289 |
regex = r'\s+(lib%s\.[^\s]+)\s+\(%s' |
| 290 |
regex = os.fsencode(regex % (re.escape(name), abi_type)) |
| 291 |
try: |
| 292 |
with subprocess.Popen(['/sbin/ldconfig', '-p'], |
| 293 |
stdin=subprocess.DEVNULL, |
| 294 |
stderr=subprocess.DEVNULL, |
| 295 |
stdout=subprocess.PIPE, |
| 296 |
env={'LC_ALL': 'C', 'LANG': 'C'}) as p: |
| 297 |
res = re.search(regex, p.stdout.read()) |
| 298 |
if res: |
| 299 |
return os.fsdecode(res.group(1)) |
| 300 |
except OSError: |
| 301 |
pass |
| 302 |
|
| 303 |
def _findLib_ld(name): |
| 304 |
|
| 305 |
expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name) |
| 306 |
cmd = ['ld', '-t'] |
| 307 |
libpath = os.environ.get('LD_LIBRARY_PATH') |
| 308 |
if libpath: |
| 309 |
for d in libpath.split(':'): |
| 310 |
cmd.extend(['-L', d]) |
| 311 |
cmd.extend(['-o', os.devnull, '-l%s' % name]) |
| 312 |
result = None |
| 313 |
try: |
| 314 |
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 315 |
stderr=subprocess.PIPE, |
| 316 |
universal_newlines=True) |
| 317 |
out, _ = p.communicate() |
| 318 |
res = re.findall(expr, os.fsdecode(out)) |
| 319 |
for file in res: |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
if not _is_elf(file): |
| 324 |
continue |
| 325 |
return os.fsdecode(file) |
| 326 |
except Exception: |
| 327 |
pass |
| 328 |
return result |
| 329 |
|
| 330 |
def find_library(name): |
| 331 |
|
| 332 |
return _findSoname_ldconfig(name) or \ |
| 333 |
_get_soname(_findLib_gcc(name)) or _get_soname(_findLib_ld(name)) |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
def test(): |
| 339 |
from ctypes import cdll |
| 340 |
if os.name == "nt": |
| 341 |
print(cdll.msvcrt) |
| 342 |
print(cdll.load("msvcrt")) |
| 343 |
print(find_library("msvcrt")) |
| 344 |
|
| 345 |
if os.name == "posix": |
| 346 |
|
| 347 |
print(find_library("m")) |
| 348 |
print(find_library("c")) |
| 349 |
print(find_library("bz2")) |
| 350 |
|
| 351 |
|
| 352 |
if sys.platform == "darwin": |
| 353 |
print(cdll.LoadLibrary("libm.dylib")) |
| 354 |
print(cdll.LoadLibrary("libcrypto.dylib")) |
| 355 |
print(cdll.LoadLibrary("libSystem.dylib")) |
| 356 |
print(cdll.LoadLibrary("System.framework/System")) |
| 357 |
|
| 358 |
elif sys.platform.startswith("aix"): |
| 359 |
from ctypes import CDLL |
| 360 |
if sys.maxsize < 2**32: |
| 361 |
print(f"Using CDLL(name, os.RTLD_MEMBER): {CDLL('libc.a(shr.o)', os.RTLD_MEMBER)}") |
| 362 |
print(f"Using cdll.LoadLibrary(): {cdll.LoadLibrary('libc.a(shr.o)')}") |
| 363 |
|
| 364 |
print(find_library("rpm")) |
| 365 |
print(cdll.LoadLibrary("librpm.so")) |
| 366 |
else: |
| 367 |
print(f"Using CDLL(name, os.RTLD_MEMBER): {CDLL('libc.a(shr_64.o)', os.RTLD_MEMBER)}") |
| 368 |
print(f"Using cdll.LoadLibrary(): {cdll.LoadLibrary('libc.a(shr_64.o)')}") |
| 369 |
print(f"crypt\t:: {find_library('crypt')}") |
| 370 |
print(f"crypt\t:: {cdll.LoadLibrary(find_library('crypt'))}") |
| 371 |
print(f"crypto\t:: {find_library('crypto')}") |
| 372 |
print(f"crypto\t:: {cdll.LoadLibrary(find_library('crypto'))}") |
| 373 |
else: |
| 374 |
print(cdll.LoadLibrary("libm.so")) |
| 375 |
print(cdll.LoadLibrary("libcrypt.so")) |
| 376 |
print(find_library("crypt")) |
| 377 |
|
| 378 |
if __name__ == "__main__": |
| 379 |
test() |