2013-11-11 05:59:07 +08:00
|
|
|
# Detect present Python & Boost-python libraries on Linux
|
2013-11-11 22:13:36 +08:00
|
|
|
import os, struct
|
2013-11-11 05:59:07 +08:00
|
|
|
|
|
|
|
class PkgConfig(dict):
|
2013-11-11 22:13:36 +08:00
|
|
|
_paths = [
|
|
|
|
'/usr/lib/pkgconfig',
|
|
|
|
'/usr/lib/%s-linux-gnu/pkgconfig' % (os.uname()[4]),
|
2021-12-16 00:43:26 +08:00
|
|
|
'/usr/lib%i/pkgconfig' % (struct.calcsize('P')*8),
|
|
|
|
'/root/miniconda3/lib/pkgconfig',
|
|
|
|
'/root/anaconda3/lib/pkgconfig'
|
2013-11-11 22:13:36 +08:00
|
|
|
]
|
2013-11-11 05:59:07 +08:00
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
for path in self._paths:
|
|
|
|
fn = os.path.join(path, '%s.pc' % name)
|
|
|
|
if os.path.exists(fn):
|
|
|
|
self._parse(fn)
|
|
|
|
break
|
|
|
|
|
|
|
|
def _parse(self, filename):
|
|
|
|
from string import Template
|
|
|
|
|
|
|
|
lines = open(filename).readlines()
|
|
|
|
localVariables = {}
|
|
|
|
|
|
|
|
for line in lines:
|
|
|
|
line = line.strip()
|
|
|
|
if not line or line.startswith('#'):
|
|
|
|
continue
|
|
|
|
elif ':' in line:
|
|
|
|
name, val = line.split(':')
|
|
|
|
val = val.strip()
|
|
|
|
if '$' in val:
|
|
|
|
val = Template(val).substitute(localVariables)
|
|
|
|
self[name] = val
|
|
|
|
elif '=' in line:
|
|
|
|
name, val = line.split('=')
|
|
|
|
val = val.strip()
|
|
|
|
if '$' in val:
|
|
|
|
val = Template(val).substitute(localVariables)
|
|
|
|
localVariables[name] = val
|
|
|
|
|
|
|
|
def find_boost_python(version):
|
|
|
|
libnames = [
|
|
|
|
'boost_python-mt-py%s' % version,
|
|
|
|
'boost_python-py%s' % version,
|
2021-12-16 00:43:26 +08:00
|
|
|
'boost_python%s' % version,
|
2013-11-11 05:59:07 +08:00
|
|
|
'boost_python' + ('3' if version.startswith('3') else '')
|
|
|
|
]
|
2013-11-11 22:13:36 +08:00
|
|
|
basepaths = [
|
2021-12-16 00:43:26 +08:00
|
|
|
'/root/miniconda3/lib/',
|
|
|
|
'/root/anaconda3/lib/',
|
2013-11-11 22:13:36 +08:00
|
|
|
'/usr/lib',
|
|
|
|
'/usr/lib/%s-linux-gnu' % (os.uname()[4]),
|
|
|
|
'/usr/lib%i' % (struct.calcsize('P')*8)
|
|
|
|
]
|
2013-11-11 05:59:07 +08:00
|
|
|
|
|
|
|
for basepath in basepaths:
|
|
|
|
for libname in libnames:
|
|
|
|
if os.path.isfile(os.path.join(basepath, "lib" + libname + ".so")):
|
|
|
|
return libname
|
|
|
|
return None
|
|
|
|
|
|
|
|
def detect_python():
|
2022-01-07 01:07:17 +08:00
|
|
|
pyver = [
|
|
|
|
'2.6', '2.7',
|
|
|
|
'3.0', '3.1', '3.2', '3.3',
|
|
|
|
'3.4', '3.5', '3.6','3.7',
|
|
|
|
'3.8', '3.9', '3.10']
|
2013-11-11 05:59:07 +08:00
|
|
|
pyenv = {}
|
|
|
|
|
|
|
|
for version in pyver:
|
|
|
|
pkgconfig = PkgConfig('python-%s' % version)
|
|
|
|
version = version.replace('.', '')
|
|
|
|
flags = []
|
|
|
|
if 'Cflags' in pkgconfig:
|
|
|
|
flags += pkgconfig['Cflags'].split()
|
|
|
|
if 'Libs' in pkgconfig:
|
|
|
|
flags += pkgconfig['Libs'].split()
|
|
|
|
if len(flags) == 0:
|
|
|
|
continue
|
|
|
|
boost_libname = find_boost_python(version)
|
|
|
|
if boost_libname == None:
|
|
|
|
continue
|
|
|
|
pyenv['PYTHON' + version + 'INCLUDE'] = []
|
|
|
|
pyenv['PYTHON' + version + 'LIBDIR'] = []
|
|
|
|
pyenv['PYTHON' + version + 'LIB'] = [ boost_libname ]
|
|
|
|
for flag in flags:
|
|
|
|
if flag.startswith('-I'):
|
|
|
|
pyenv['PYTHON' + version + 'INCLUDE'] += [flag[2:]]
|
|
|
|
elif flag.startswith('-L'):
|
|
|
|
pyenv['PYTHON' + version + 'LIBDIR'] += [flag[2:]]
|
|
|
|
elif flag.startswith('-l'):
|
|
|
|
pyenv['PYTHON' + version + 'LIB'] += [flag[2:]]
|
2021-12-16 00:43:26 +08:00
|
|
|
print('found some boost python')
|
|
|
|
print(pyenv)
|
2013-11-11 05:59:07 +08:00
|
|
|
return pyenv
|
2013-11-11 22:13:36 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import pprint
|
|
|
|
pprint.pprint(detect_python())
|