Skip to content

Commit

Permalink
Merge pull request #448 from matthew-brett/npy_log-fix
Browse files Browse the repository at this point in the history
BF: fix link to npy_math function
  • Loading branch information
Garyfallidis committed Nov 2, 2014
2 parents a3d8644 + 4537535 commit 1b2d245
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
13 changes: 9 additions & 4 deletions setup.py
Expand Up @@ -10,7 +10,7 @@
# update it when the contents of directories change.
if os.path.exists('MANIFEST'): os.remove('MANIFEST')

import numpy as np
from numpy.distutils.misc_util import get_info

# Get version and release info, which is all stored in dipy/info.py
ver_file = os.path.join('dipy', 'info.py')
Expand Down Expand Up @@ -73,11 +73,13 @@
from distutils.command import build_py, build_ext

from cythexts import cyproc_exts, get_pyx_sdist, derror_maker
from setup_helpers import install_scripts_bat, add_flag_checking
from setup_helpers import install_scripts_bat, add_flag_checking, check_npymath

# Define extensions
EXTS = []

# Add flags for linking to npymath library
ext_kwargs = get_info('npymath')
ext_kwargs['include_dirs'].append('src')
for modulename, other_sources, language in (
('dipy.reconst.recspeed', [], 'c'),
('dipy.reconst.vec_val_sum', [], 'c'),
Expand All @@ -96,7 +98,7 @@
pyx_src = pjoin(*modulename.split('.')) + '.pyx'
EXTS.append(Extension(modulename, [pyx_src] + other_sources,
language=language,
include_dirs=[np.get_include(), "src"]))
**ext_kwargs))


# Do our own build and install time dependency checking. setup.py gets called in
Expand All @@ -123,6 +125,9 @@ def package_check(*args, **kwargs):
int main(int argc, char** argv) { return(0); }"""
extbuilder = add_flag_checking(
build_ext, [[['-fopenmp'], ['-fopenmp'], omp_test_c, 'HAVE_OPENMP']])
# Fix npymath libraries for Windows
if os.name == 'nt':
extbuilder = check_npymath(extbuilder)

# Installer that checks for install-time dependencies
class installer(install.install):
Expand Down
64 changes: 64 additions & 0 deletions setup_helpers.py
Expand Up @@ -29,6 +29,8 @@

# File to which to write Cython conditional DEF vars
CONFIG_PXI = pjoin('build', 'config.pxi')
# Directory to which to write libraries for building
LIB_DIR_TMP = pjoin('build', 'extra_libs')


class install_scripts_bat(install_scripts):
Expand Down Expand Up @@ -168,3 +170,65 @@ def build_extensions(self):
build_ext_class.build_extensions(self)

return Checker


def check_npymath(build_ext_class):
""" Override input `build_ext_class` to modify ``npymath`` library
If compilation needs 'npymath.lib' (VC format) library, and it doesn't
exist, and the mingw 'libnpymath.a' version does exist, copy the mingw
version to a temporary path and add this path to the library directories.
Parameters
----------
build_ext_class : class
Class implementing ``distutils.command.build_ext.build_ext`` interface,
with a ``build_extensions`` method.
Returns
-------
libfixed_class : class
A class with similar interface to
``distutils.command.build_ext.build_ext``, that has fixed any
references to ``npymath``.
"""

class LibModder(build_ext_class):
lib_name = 'npymath'

def _copy_lib(self, lib_dirs):
mw_name = 'lib{0}.a'.format(self.lib_name)
for lib_dir in lib_dirs:
mingw_path = pjoin(lib_dir, mw_name)
if exists(mingw_path):
break
else:
return
if not exists(LIB_DIR_TMP):
os.makedirs(LIB_DIR_TMP)
vc_path = pjoin(LIB_DIR_TMP, self.lib_name + '.lib')
shutil.copyfile(mingw_path, vc_path)

def fix_ext_libs(self):
cc = self.compiler
lib_name = self.lib_name
if cc.library_option(lib_name) != lib_name + '.lib':
return # not VC format library name
lib_copied = False
for ext in self.extensions:
if not lib_name in ext.libraries:
continue
if cc.find_library_file(ext.library_dirs, lib_name) != None:
continue
# Need to copy library and / or fix library paths
if not lib_copied:
self._copy_lib(ext.library_dirs)
lib_copied = True
ext.library_dirs.append(LIB_DIR_TMP)

def build_extensions(self):
""" Hook into extension building to fix npymath lib """
self.fix_ext_libs()
build_ext_class.build_extensions(self)

return LibModder

0 comments on commit 1b2d245

Please sign in to comment.