patch #1610795: BSD version of ctypes.util.find_library, by Martin
Kammerhofer. release25-maint backport candidate, but the release manager has to decide.
This commit is contained in:
parent
572104ff5c
commit
d059e7b423
|
@ -46,23 +46,16 @@ elif os.name == "posix":
|
||||||
import re, tempfile, errno
|
import re, tempfile, errno
|
||||||
|
|
||||||
def _findLib_gcc(name):
|
def _findLib_gcc(name):
|
||||||
expr = '[^\(\)\s]*lib%s\.[^\(\)\s]*' % name
|
expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
|
||||||
fdout, ccout = tempfile.mkstemp()
|
fdout, ccout = tempfile.mkstemp()
|
||||||
os.close(fdout)
|
os.close(fdout)
|
||||||
cmd = 'if type gcc &>/dev/null; then CC=gcc; else CC=cc; fi;' \
|
cmd = 'if type gcc >/dev/null 2>&1; then CC=gcc; else CC=cc; fi;' \
|
||||||
'$CC -Wl,-t -o ' + ccout + ' 2>&1 -l' + name
|
'$CC -Wl,-t -o ' + ccout + ' 2>&1 -l' + name
|
||||||
try:
|
try:
|
||||||
fdout, outfile = tempfile.mkstemp()
|
f = os.popen(cmd)
|
||||||
os.close(fdout)
|
trace = f.read()
|
||||||
fd = os.popen(cmd)
|
f.close()
|
||||||
trace = fd.read()
|
|
||||||
err = fd.close()
|
|
||||||
finally:
|
finally:
|
||||||
try:
|
|
||||||
os.unlink(outfile)
|
|
||||||
except OSError, e:
|
|
||||||
if e.errno != errno.ENOENT:
|
|
||||||
raise
|
|
||||||
try:
|
try:
|
||||||
os.unlink(ccout)
|
os.unlink(ccout)
|
||||||
except OSError, e:
|
except OSError, e:
|
||||||
|
@ -73,29 +66,58 @@ elif os.name == "posix":
|
||||||
return None
|
return None
|
||||||
return res.group(0)
|
return res.group(0)
|
||||||
|
|
||||||
def _findLib_ld(name):
|
|
||||||
expr = '/[^\(\)\s]*lib%s\.[^\(\)\s]*' % name
|
|
||||||
res = re.search(expr, os.popen('/sbin/ldconfig -p 2>/dev/null').read())
|
|
||||||
if not res:
|
|
||||||
# Hm, this works only for libs needed by the python executable.
|
|
||||||
cmd = 'ldd %s 2>/dev/null' % sys.executable
|
|
||||||
res = re.search(expr, os.popen(cmd).read())
|
|
||||||
if not res:
|
|
||||||
return None
|
|
||||||
return res.group(0)
|
|
||||||
|
|
||||||
def _get_soname(f):
|
def _get_soname(f):
|
||||||
|
# assuming GNU binutils / ELF
|
||||||
|
if not f:
|
||||||
|
return None
|
||||||
cmd = "objdump -p -j .dynamic 2>/dev/null " + f
|
cmd = "objdump -p -j .dynamic 2>/dev/null " + f
|
||||||
res = re.search(r'\sSONAME\s+([^\s]+)', os.popen(cmd).read())
|
res = re.search(r'\sSONAME\s+([^\s]+)', os.popen(cmd).read())
|
||||||
if not res:
|
if not res:
|
||||||
return None
|
return None
|
||||||
return res.group(1)
|
return res.group(1)
|
||||||
|
|
||||||
def find_library(name):
|
if (sys.platform.startswith("freebsd")
|
||||||
lib = _findLib_ld(name) or _findLib_gcc(name)
|
or sys.platform.startswith("openbsd")
|
||||||
if not lib:
|
or sys.platform.startswith("dragonfly")):
|
||||||
return None
|
|
||||||
return _get_soname(lib)
|
def _num_version(libname):
|
||||||
|
# "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ]
|
||||||
|
parts = libname.split(".")
|
||||||
|
nums = []
|
||||||
|
try:
|
||||||
|
while parts:
|
||||||
|
nums.insert(0, int(parts.pop()))
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
return nums or [ sys.maxint ]
|
||||||
|
|
||||||
|
def find_library(name):
|
||||||
|
ename = re.escape(name)
|
||||||
|
expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename)
|
||||||
|
res = re.findall(expr,
|
||||||
|
os.popen('/sbin/ldconfig -r 2>/dev/null').read())
|
||||||
|
if not res:
|
||||||
|
return _get_soname(_findLib_gcc(name))
|
||||||
|
res.sort(cmp= lambda x,y: cmp(_num_version(x), _num_version(y)))
|
||||||
|
return res[-1]
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
def _findLib_ldconfig(name):
|
||||||
|
# XXX assuming GLIBC's ldconfig (with option -p)
|
||||||
|
expr = r'/[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
|
||||||
|
res = re.search(expr,
|
||||||
|
os.popen('/sbin/ldconfig -p 2>/dev/null').read())
|
||||||
|
if not res:
|
||||||
|
# Hm, this works only for libs needed by the python executable.
|
||||||
|
cmd = 'ldd %s 2>/dev/null' % sys.executable
|
||||||
|
res = re.search(expr, os.popen(cmd).read())
|
||||||
|
if not res:
|
||||||
|
return None
|
||||||
|
return res.group(0)
|
||||||
|
|
||||||
|
def find_library(name):
|
||||||
|
return _get_soname(_findLib_ldconfig(name) or _findLib_gcc(name))
|
||||||
|
|
||||||
################################################################
|
################################################################
|
||||||
# test code
|
# test code
|
||||||
|
|
Loading…
Reference in New Issue