Skip to content
Browse files

try pkg-config first for cblas and lapack libraries

  • Loading branch information...
1 parent 6654a36 commit cf675f0777670edc867634654b4f0a641d72ad7a @strohel committed Jan 20, 2013
Showing with 30 additions and 14 deletions.
  1. +3 −3 support/dist.py
  2. +27 −11 support/dist_cmd_build_prepare.py
View
6 support/dist.py
@@ -42,12 +42,12 @@ def __init__(self, attrs=None):
# cython-build specific options
('blas-lib=', None,
- 'library name that provides cblas_sswap function, without lib prefix [default: cblas]'),
+ 'library name that provides cblas_sswap function, without lib prefix [default: pkg-config --libs cblas or "cblas"]'),
('lapack-lib=', None,
- 'library name that provides clapack_sgetri function, without lib prefix [default: lapack]'),
+ 'library name that provides clapack_sgetri function, without lib prefix [default: pkg-config --libs lapack or "lapack"]'),
('library-dirs=', 'L',
'list of additional directories where libraries are seached for, separated by '
- + '{0} character'.format(os.pathsep)),
+ + '{0} character [default: try pkg-config or none]'.format(os.pathsep)),
]
def has_ext_modules(self):
View
38 support/dist_cmd_build_prepare.py
@@ -6,6 +6,7 @@
An additional subcommand to distutils' build to handle Python/Cython build of PyBayes
"""
+import commands
from distutils.cmd import Command
from distutils.errors import DistutilsSetupError
import distutils.log as log
@@ -23,6 +24,8 @@ class build_prepare(Command):
def initialize_options(self):
self.ext_options = {} # options common to all extensions
self.ext_options['include_dirs'] = [self.distribution.numpy_include_dir]
+ self.ext_options['library_dirs'] = []
+ self.ext_options['libraries'] = []
self.ext_options['extra_compile_args'] = ['-O2']
#self.ext_options['extra_link_args'] = ['-Wl,-O1']
self.ext_options['pyrex_c_in_temp'] = True # do not pollute source directory with .c files
@@ -38,16 +41,18 @@ def initialize_options(self):
def finalize_options(self):
# these options are passed through global distribution
dist = self.distribution
- self.blas_lib = dist.blas_lib
- self.lapack_lib = dist.lapack_lib
- self.library_dirs = dist.library_dirs
-
- self.blas_lib = self.blas_lib or 'cblas'
- self.lapack_lib = self.lapack_lib or 'lapack'
- if self.library_dirs:
- self.library_dirs = self.library_dirs.split(os.pathsep)
+
+ if dist.blas_lib:
+ self.ext_options['libraries'].append(dist.blas_lib)
+ else:
+ self.try_pkgconfig('cblas')
+ if dist.lapack_lib:
+ self.ext_options['libraries'].append(dist.lapack_lib)
else:
- self.library_dirs = []
+ self.try_pkgconfig('lapack')
+
+ if dist.library_dirs:
+ self.ext_options['library_dirs'].extend(dist.library_dirs.split(os.pathsep))
# these are just aliases to distribution variables
self.packages = self.distribution.packages
@@ -63,6 +68,19 @@ def finalize_options(self):
if not self.packages:
raise DistutilsSetupError("PyBayes-tweaked distutils doesn't support nempty `packages`")
+ def try_pkgconfig(self, library):
+ # pkg-config handling inspired by http://code.activestate.com/recipes/502261/
+ flag_map = {'-I': 'include_dirs', '-L': 'library_dirs', '-l': 'libraries'}
+ extra_ext_options = {'include_dirs':[], 'library_dirs':[], 'libraries':[]}
+
+ for token in commands.getoutput("pkg-config --libs --cflags {0}".format(library)).split():
+ extra_ext_options[flag_map.get(token[:2])].append(token[2:])
+ if extra_ext_options['libraries']:
+ for key in extra_ext_options:
+ self.ext_options[key].extend(extra_ext_options[key])
+ else:
+ self.ext_options['libraries'].extend(library)
+
def run(self):
build_py = self.distribution.get_command_obj('build_py')
self.get_package_dir = build_py.get_package_dir # borrow a method from build_py
@@ -84,8 +102,6 @@ def run(self):
self.distribution.ext_modules.append(self.distribution.Extension(
'tokyo', # module name
['tokyo/tokyo.pyx', 'tokyo/tokyo.pxd'], # source file and deps
- libraries=[self.blas_lib, self.lapack_lib],
- library_dirs=self.library_dirs,
**self.ext_options
))
self.package_data['tokyo'] = '*.pxd'

0 comments on commit cf675f0

Please sign in to comment.
Something went wrong with that request. Please try again.