|
2 | 2 |
|
3 | 3 | import os, sys
|
4 | 4 | import shutil
|
| 5 | +from distutils import ccompiler |
5 | 6 | from setuptools import setup
|
6 | 7 | from setuptools.extension import Extension
|
7 | 8 | from numpy import get_include
|
8 | 9 |
|
9 | 10 | cwd = os.path.abspath(os.path.dirname(__file__))
|
10 | 11 | fftwdir = os.path.join(cwd, 'mpi4py_fft', 'fftw')
|
11 | 12 |
|
12 |
| -# For now assuming that all precisions are available |
| 13 | +include_dirs = [get_include(), os.path.join(sys.prefix, 'include')] |
| 14 | +library_dirs = [os.path.join(sys.prefix, 'lib')] |
| 15 | +for f in ('FFTW_ROOT', 'FFTW_DIR'): |
| 16 | + if f in os.environ['PATH']: |
| 17 | + library_dirs.append(os.path.join(os.environ[f], 'lib')) |
| 18 | + include_dirs.append(os.path.join(os.environ[f], 'include')) |
| 19 | + |
| 20 | +compiler = ccompiler.new_compiler() |
| 21 | +assert compiler.find_library_file(library_dirs, 'fftw3'), 'Cannot find FFTW library!' |
| 22 | +has_threads = compiler.find_library_file(library_dirs, 'fftw3_threads') |
| 23 | + |
| 24 | +prec_map = {'float': 'fftwf_', 'double': 'fftw_', 'long double': 'fftwl_'} |
13 | 25 |
|
14 |
| -prec = {'fftwf_': 'float', 'fftw': 'double', 'fftwl_': 'long double'} |
15 | 26 | libs = {
|
16 |
| - 'fftwf_': ['m', 'fftw3f', 'fftw3f_threads'], |
17 |
| - 'fftw_': ['m', 'fftw3', 'fftw3_threads'], |
18 |
| - 'fftwl_': ['m', 'fftw3l', 'fftw3l_threads']} |
| 27 | + 'float': ['fftw3f'], |
| 28 | + 'double': ['fftw3'], |
| 29 | + 'long double': ['fftw3l'] |
| 30 | + } |
| 31 | + |
| 32 | +has_prec = ['double'] |
| 33 | +for d in ('float', 'long double'): |
| 34 | + if compiler.find_library_file(library_dirs, libs[d][0]): |
| 35 | + has_prec.append(d) |
| 36 | + |
| 37 | +if has_threads: |
| 38 | + for d in has_prec: |
| 39 | + libs[d].append('_'.join((libs[d][0], 'threads'))) |
| 40 | + if sys.platform in ('unix', 'darwin'): |
| 41 | + libs[d].append('m') |
19 | 42 |
|
20 |
| -for fl in ('fftw_planxfftn.h', 'fftw_planxfftn.c', 'fftw_xfftn.pyx', 'fftw_xfftn.pxd'): |
21 |
| - for p in ('fftwf_', 'fftwl_'): |
| 43 | +# Generate files with float and long double if needed |
| 44 | +for d in has_prec[1:]: |
| 45 | + p = prec_map[d] |
| 46 | + for fl in ('fftw_planxfftn.h', 'fftw_planxfftn.c', 'fftw_xfftn.pyx', 'fftw_xfftn.pxd'): |
22 | 47 | fp = fl.replace('fftw_', p)
|
23 | 48 | shutil.copy(os.path.join(fftwdir, fl), os.path.join(fftwdir, fp))
|
24 | 49 | sedcmd = "sed -i ''" if sys.platform == 'darwin' else "sed -i''"
|
25 | 50 | os.system(sedcmd + " 's/fftw_/{0}/g' {1}".format(p, os.path.join(fftwdir, fp)))
|
26 |
| - os.system(sedcmd + " 's/double/{0}/g' {1}".format(prec[p], os.path.join(fftwdir, fp))) |
| 51 | + os.system(sedcmd + " 's/double/{0}/g' {1}".format(d, os.path.join(fftwdir, fp))) |
27 | 52 |
|
28 | 53 | ext = [Extension("mpi4py_fft.fftw.utilities",
|
29 | 54 | sources=[os.path.join(fftwdir, "utilities.pyx")],
|
30 |
| - libraries=libs[p], |
31 | 55 | include_dirs=[get_include(),
|
32 |
| - os.path.join(sys.prefix, 'include')], |
33 |
| - library_dirs=[os.path.join(sys.prefix, 'lib')])] |
| 56 | + os.path.join(sys.prefix, 'include')])] |
34 | 57 |
|
35 |
| -for p in ('fftw_', 'fftwf_', 'fftwl_'): |
| 58 | +for d in has_prec: |
| 59 | + p = prec_map[d] |
36 | 60 | ext.append(Extension("mpi4py_fft.fftw.{}xfftn".format(p),
|
37 | 61 | sources=[os.path.join(fftwdir, "{}xfftn.pyx".format(p)),
|
38 | 62 | os.path.join(fftwdir, "{}planxfftn.c".format(p))],
|
39 | 63 | #define_macros=[('NPY_NO_DEPRECATED_API', 'NPY_1_7_API_VERSION')],
|
40 |
| - libraries=libs[p], |
41 |
| - include_dirs=[get_include(), |
42 |
| - os.path.join(sys.prefix, 'include')], |
43 |
| - library_dirs=[os.path.join(sys.prefix, 'lib')])) |
| 64 | + libraries=libs[d], |
| 65 | + include_dirs=include_dirs, |
| 66 | + library_dirs=library_dirs)) |
44 | 67 |
|
45 | 68 | with open("README.rst", "r") as fh:
|
46 | 69 | long_description = fh.read()
|
|
0 commit comments