2014-10-16 04:42:45 -03:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2006-05-12 15:16:03 -03:00
|
|
|
|
|
|
|
# find_library(name) returns the pathname of a library, or None.
|
|
|
|
if os.name == "nt":
|
2008-05-16 17:06:31 -03:00
|
|
|
|
|
|
|
def _get_build_version():
|
|
|
|
"""Return the version of MSVC that was used to build Python.
|
|
|
|
|
|
|
|
For Python 2.3 and up, the version number is included in
|
|
|
|
sys.version. For earlier versions, assume the compiler is MSVC 6.
|
|
|
|
"""
|
|
|
|
# This function was copied from Lib/distutils/msvccompiler.py
|
|
|
|
prefix = "MSC v."
|
|
|
|
i = sys.version.find(prefix)
|
|
|
|
if i == -1:
|
|
|
|
return 6
|
|
|
|
i = i + len(prefix)
|
|
|
|
s, rest = sys.version[i:].split(" ", 1)
|
|
|
|
majorVersion = int(s[:-2]) - 6
|
|
|
|
minorVersion = int(s[2:3]) / 10.0
|
|
|
|
# I don't think paths are affected by minor version in version 6
|
|
|
|
if majorVersion == 6:
|
|
|
|
minorVersion = 0
|
|
|
|
if majorVersion >= 6:
|
|
|
|
return majorVersion + minorVersion
|
|
|
|
# else we don't know what version of the compiler this is
|
|
|
|
return None
|
|
|
|
|
|
|
|
def find_msvcrt():
|
|
|
|
"""Return the name of the VC runtime dll"""
|
|
|
|
version = _get_build_version()
|
|
|
|
if version is None:
|
|
|
|
# better be safe than sorry
|
|
|
|
return None
|
|
|
|
if version <= 6:
|
|
|
|
clibname = 'msvcrt'
|
|
|
|
else:
|
|
|
|
clibname = 'msvcr%d' % (version * 10)
|
|
|
|
|
|
|
|
# If python was built with in debug mode
|
|
|
|
import imp
|
|
|
|
if imp.get_suffixes()[0][0] == '_d.pyd':
|
|
|
|
clibname += 'd'
|
|
|
|
return clibname+'.dll'
|
|
|
|
|
2006-05-12 15:16:03 -03:00
|
|
|
def find_library(name):
|
2008-05-16 17:06:31 -03:00
|
|
|
if name in ('c', 'm'):
|
|
|
|
return find_msvcrt()
|
2006-05-12 15:16:03 -03:00
|
|
|
# See MSDN for the REAL search order.
|
|
|
|
for directory in os.environ['PATH'].split(os.pathsep):
|
|
|
|
fname = os.path.join(directory, name)
|
2009-05-05 15:55:47 -03:00
|
|
|
if os.path.isfile(fname):
|
2006-05-12 15:16:03 -03:00
|
|
|
return fname
|
|
|
|
if fname.lower().endswith(".dll"):
|
|
|
|
continue
|
|
|
|
fname = fname + ".dll"
|
2009-05-05 15:55:47 -03:00
|
|
|
if os.path.isfile(fname):
|
2006-05-12 15:16:03 -03:00
|
|
|
return fname
|
|
|
|
return None
|
|
|
|
|
|
|
|
if os.name == "ce":
|
|
|
|
# search path according to MSDN:
|
|
|
|
# - absolute path specified by filename
|
|
|
|
# - The .exe launch directory
|
|
|
|
# - the Windows directory
|
|
|
|
# - ROM dll files (where are they?)
|
|
|
|
# - OEM specified search path: HKLM\Loader\SystemPath
|
|
|
|
def find_library(name):
|
|
|
|
return name
|
|
|
|
|
|
|
|
if os.name == "posix" and sys.platform == "darwin":
|
|
|
|
from ctypes.macholib.dyld import dyld_find as _dyld_find
|
|
|
|
def find_library(name):
|
|
|
|
possible = ['lib%s.dylib' % name,
|
|
|
|
'%s.dylib' % name,
|
|
|
|
'%s.framework/%s' % (name, name)]
|
|
|
|
for name in possible:
|
|
|
|
try:
|
|
|
|
return _dyld_find(name)
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
return None
|
|
|
|
|
|
|
|
elif os.name == "posix":
|
|
|
|
# Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
|
2006-06-11 05:35:14 -03:00
|
|
|
import re, tempfile, errno
|
2006-05-12 15:16:03 -03:00
|
|
|
|
|
|
|
def _findLib_gcc(name):
|
2014-10-16 04:42:45 -03:00
|
|
|
# Run GCC's linker with the -t (aka --trace) option and examine the
|
|
|
|
# library name it prints out. The GCC command will fail because we
|
|
|
|
# haven't supplied a proper program with main(), but that does not
|
|
|
|
# matter.
|
2007-01-12 16:17:34 -04:00
|
|
|
expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
|
2014-10-16 04:42:45 -03:00
|
|
|
cmd = 'if type gcc >/dev/null 2>&1; then CC=gcc; elif type cc >/dev/null 2>&1; then CC=cc;else exit; fi;' \
|
|
|
|
'LANG=C LC_ALL=C $CC -Wl,-t -o "$2" 2>&1 -l"$1"'
|
|
|
|
|
|
|
|
temp = tempfile.NamedTemporaryFile()
|
2006-05-12 15:16:03 -03:00
|
|
|
try:
|
2014-10-16 04:42:45 -03:00
|
|
|
proc = subprocess.Popen((cmd, '_findLib_gcc', name, temp.name),
|
|
|
|
shell=True,
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
[trace, _] = proc.communicate()
|
2006-05-12 15:16:03 -03:00
|
|
|
finally:
|
2006-07-13 14:01:14 -03:00
|
|
|
try:
|
2014-10-16 04:42:45 -03:00
|
|
|
temp.close()
|
2006-07-13 14:01:14 -03:00
|
|
|
except OSError, e:
|
2014-10-16 04:42:45 -03:00
|
|
|
# ENOENT is raised if the file was already removed, which is
|
|
|
|
# the normal behaviour of GCC if linking fails
|
2006-07-13 14:01:14 -03:00
|
|
|
if e.errno != errno.ENOENT:
|
|
|
|
raise
|
2006-05-12 15:16:03 -03:00
|
|
|
res = re.search(expr, trace)
|
|
|
|
if not res:
|
|
|
|
return None
|
|
|
|
return res.group(0)
|
|
|
|
|
2007-09-14 16:40:35 -03:00
|
|
|
|
|
|
|
if sys.platform == "sunos5":
|
|
|
|
# use /usr/ccs/bin/dump on solaris
|
|
|
|
def _get_soname(f):
|
|
|
|
if not f:
|
|
|
|
return None
|
2014-10-16 04:42:45 -03:00
|
|
|
|
|
|
|
null = open(os.devnull, "wb")
|
2009-10-22 17:13:14 -03:00
|
|
|
try:
|
2014-10-16 04:42:45 -03:00
|
|
|
with null:
|
|
|
|
proc = subprocess.Popen(("/usr/ccs/bin/dump", "-Lpv", f),
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=null)
|
|
|
|
except OSError: # E.g. command not found
|
|
|
|
return None
|
|
|
|
[data, _] = proc.communicate()
|
|
|
|
res = re.search(br'\[.*\]\sSONAME\s+([^\s]+)', data)
|
2007-09-14 16:40:35 -03:00
|
|
|
if not res:
|
|
|
|
return None
|
|
|
|
return res.group(1)
|
|
|
|
else:
|
|
|
|
def _get_soname(f):
|
|
|
|
# assuming GNU binutils / ELF
|
|
|
|
if not f:
|
|
|
|
return None
|
2014-10-16 04:42:45 -03:00
|
|
|
cmd = 'if ! type objdump >/dev/null 2>&1; then exit; fi;' \
|
|
|
|
'objdump -p -j .dynamic 2>/dev/null "$1"'
|
|
|
|
proc = subprocess.Popen((cmd, '_get_soname', f), shell=True,
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
[dump, _] = proc.communicate()
|
|
|
|
res = re.search(br'\sSONAME\s+([^\s]+)', dump)
|
2007-09-14 16:40:35 -03:00
|
|
|
if not res:
|
|
|
|
return None
|
|
|
|
return res.group(1)
|
2006-05-12 15:16:03 -03:00
|
|
|
|
2007-01-12 16:17:34 -04:00
|
|
|
if (sys.platform.startswith("freebsd")
|
|
|
|
or sys.platform.startswith("openbsd")
|
|
|
|
or sys.platform.startswith("dragonfly")):
|
|
|
|
|
|
|
|
def _num_version(libname):
|
|
|
|
# "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ]
|
2014-10-16 04:42:45 -03:00
|
|
|
parts = libname.split(b".")
|
2007-01-12 16:17:34 -04:00
|
|
|
nums = []
|
|
|
|
try:
|
|
|
|
while parts:
|
|
|
|
nums.insert(0, int(parts.pop()))
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2014-10-16 04:42:45 -03:00
|
|
|
return nums or [sys.maxint]
|
2007-01-12 16:17:34 -04:00
|
|
|
|
|
|
|
def find_library(name):
|
|
|
|
ename = re.escape(name)
|
|
|
|
expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename)
|
2014-10-16 04:42:45 -03:00
|
|
|
|
|
|
|
null = open(os.devnull, 'wb')
|
2009-10-22 17:13:14 -03:00
|
|
|
try:
|
2014-10-16 04:42:45 -03:00
|
|
|
with null:
|
|
|
|
proc = subprocess.Popen(('/sbin/ldconfig', '-r'),
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=null)
|
|
|
|
except OSError: # E.g. command not found
|
|
|
|
data = b''
|
|
|
|
else:
|
|
|
|
[data, _] = proc.communicate()
|
|
|
|
|
2009-10-22 17:13:14 -03:00
|
|
|
res = re.findall(expr, data)
|
2007-01-12 16:17:34 -04:00
|
|
|
if not res:
|
|
|
|
return _get_soname(_findLib_gcc(name))
|
2015-01-31 05:27:06 -04:00
|
|
|
res.sort(key=_num_version)
|
2007-01-12 16:17:34 -04:00
|
|
|
return res[-1]
|
|
|
|
|
2013-02-03 20:25:11 -04:00
|
|
|
elif sys.platform == "sunos5":
|
|
|
|
|
|
|
|
def _findLib_crle(name, is64):
|
|
|
|
if not os.path.exists('/usr/bin/crle'):
|
|
|
|
return None
|
|
|
|
|
2014-10-16 04:42:45 -03:00
|
|
|
env = dict(os.environ)
|
|
|
|
env['LC_ALL'] = 'C'
|
|
|
|
|
2013-02-03 20:25:11 -04:00
|
|
|
if is64:
|
2014-10-16 04:42:45 -03:00
|
|
|
args = ('/usr/bin/crle', '-64')
|
2013-02-03 20:25:11 -04:00
|
|
|
else:
|
2014-10-16 04:42:45 -03:00
|
|
|
args = ('/usr/bin/crle',)
|
2013-02-03 20:25:11 -04:00
|
|
|
|
2016-04-30 23:56:59 -03:00
|
|
|
paths = None
|
2014-10-16 04:42:45 -03:00
|
|
|
null = open(os.devnull, 'wb')
|
|
|
|
try:
|
|
|
|
with null:
|
|
|
|
proc = subprocess.Popen(args,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=null,
|
|
|
|
env=env)
|
|
|
|
except OSError: # E.g. bad executable
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
for line in proc.stdout:
|
|
|
|
line = line.strip()
|
|
|
|
if line.startswith(b'Default Library Path (ELF):'):
|
|
|
|
paths = line.split()[4]
|
|
|
|
finally:
|
|
|
|
proc.stdout.close()
|
|
|
|
proc.wait()
|
2013-02-03 20:25:11 -04:00
|
|
|
|
|
|
|
if not paths:
|
|
|
|
return None
|
|
|
|
|
|
|
|
for dir in paths.split(":"):
|
|
|
|
libfile = os.path.join(dir, "lib%s.so" % name)
|
|
|
|
if os.path.exists(libfile):
|
|
|
|
return libfile
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def find_library(name, is64 = False):
|
|
|
|
return _get_soname(_findLib_crle(name, is64) or _findLib_gcc(name))
|
|
|
|
|
2007-01-12 16:17:34 -04:00
|
|
|
else:
|
|
|
|
|
2009-01-10 13:00:42 -04:00
|
|
|
def _findSoname_ldconfig(name):
|
|
|
|
import struct
|
|
|
|
if struct.calcsize('l') == 4:
|
|
|
|
machine = os.uname()[4] + '-32'
|
|
|
|
else:
|
|
|
|
machine = os.uname()[4] + '-64'
|
|
|
|
mach_map = {
|
|
|
|
'x86_64-64': 'libc6,x86-64',
|
|
|
|
'ppc64-64': 'libc6,64bit',
|
|
|
|
'sparc64-64': 'libc6,64bit',
|
|
|
|
's390x-64': 'libc6,64bit',
|
|
|
|
'ia64-64': 'libc6,IA-64',
|
|
|
|
}
|
|
|
|
abi_type = mach_map.get(machine, 'libc6')
|
|
|
|
|
|
|
|
# XXX assuming GLIBC's ldconfig (with option -p)
|
2012-02-14 00:22:06 -04:00
|
|
|
expr = r'\s+(lib%s\.[^\s]+)\s+\(%s' % (re.escape(name), abi_type)
|
2014-10-16 04:42:45 -03:00
|
|
|
|
|
|
|
env = dict(os.environ)
|
|
|
|
env['LC_ALL'] = 'C'
|
|
|
|
env['LANG'] = 'C'
|
|
|
|
null = open(os.devnull, 'wb')
|
2009-10-22 17:13:14 -03:00
|
|
|
try:
|
2014-10-16 04:42:45 -03:00
|
|
|
with null:
|
|
|
|
p = subprocess.Popen(['/sbin/ldconfig', '-p'],
|
|
|
|
stderr=null,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
env=env)
|
|
|
|
except OSError: # E.g. command not found
|
|
|
|
return None
|
|
|
|
[data, _] = p.communicate()
|
2009-10-22 17:13:14 -03:00
|
|
|
res = re.search(expr, data)
|
2009-01-10 13:00:42 -04:00
|
|
|
if not res:
|
|
|
|
return None
|
|
|
|
return res.group(1)
|
|
|
|
|
2007-01-12 16:17:34 -04:00
|
|
|
def find_library(name):
|
2009-01-10 13:00:42 -04:00
|
|
|
return _findSoname_ldconfig(name) or _get_soname(_findLib_gcc(name))
|
2006-05-12 15:16:03 -03:00
|
|
|
|
|
|
|
################################################################
|
|
|
|
# test code
|
|
|
|
|
|
|
|
def test():
|
|
|
|
from ctypes import cdll
|
|
|
|
if os.name == "nt":
|
|
|
|
print cdll.msvcrt
|
|
|
|
print cdll.load("msvcrt")
|
|
|
|
print find_library("msvcrt")
|
|
|
|
|
|
|
|
if os.name == "posix":
|
|
|
|
# find and load_version
|
|
|
|
print find_library("m")
|
|
|
|
print find_library("c")
|
|
|
|
print find_library("bz2")
|
|
|
|
|
|
|
|
# getattr
|
|
|
|
## print cdll.m
|
|
|
|
## print cdll.bz2
|
|
|
|
|
|
|
|
# load
|
|
|
|
if sys.platform == "darwin":
|
|
|
|
print cdll.LoadLibrary("libm.dylib")
|
|
|
|
print cdll.LoadLibrary("libcrypto.dylib")
|
|
|
|
print cdll.LoadLibrary("libSystem.dylib")
|
|
|
|
print cdll.LoadLibrary("System.framework/System")
|
|
|
|
else:
|
|
|
|
print cdll.LoadLibrary("libm.so")
|
|
|
|
print cdll.LoadLibrary("libcrypt.so")
|
|
|
|
print find_library("crypt")
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test()
|