Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env python3
# Copyright 2016 mumpy authors.
#
# This file is part of mumpy. It is subject to the license terms in the file
# LICENSE found in the top-level directory of this distribution. A list of
# mumpy authors can be found in the file AUTHORS.md at the top-level
# directory of this distribution and at https://github.com/basnijholt/mumpy.
import sys
if sys.version_info < (3, 4):
sys.exit('Sorry, Python < 3.4 is not supported')
import re
import subprocess
import configparser
import collections
from setuptools import setup, find_packages, Extension
from distutils.command.build import build
from setuptools.command.sdist import sdist
from setuptools.command.build_ext import build_ext
def configure_extensions(exts, aliases=(), build_summary=None):
"""Modify extension configuration according to the configuration file
`exts` must be a dict of (name, kwargs) tuples that can be used like this:
`Extension(name, **kwargs). This function modifies the kwargs according to
the configuration file.
This function modifies `sys.argv`.
"""
global config_file, config_file_present
# Determine the name of the configuration file.
config_file_option = '--configfile'
# Handle command line option
for i, opt in enumerate(sys.argv):
if not opt.startswith(config_file_option):
continue
l, _, config_file = opt.partition('=')
if l != config_file_option or not config_file:
print('error: Expecting {}=PATH'.format(config_file_option),
file=sys.stderr)
sys.exit(1)
sys.argv.pop(i)
break
else:
config_file = 'build.conf'
# Read build configuration file.
configs = configparser.ConfigParser()
try:
with open(config_file) as f:
configs.read_file(f)
except IOError:
config_file_present = False
else:
config_file_present = True
# Handle section aliases.
for short, long in aliases:
if short in configs:
if long in configs:
print('Error: both {} and {} sections present in {}.'.format(
short, long, config_file))
sys.exit(1)
configs[long] = configs[short]
del configs[short]
# Apply config from file. Use [DEFAULT] section for missing sections.
defaultconfig = configs.defaults()
for name, kwargs in exts.items():
config = configs[name] if name in configs else defaultconfig
for key, value in config.items():
# Most, but not all, keys are lists of strings
if key == 'language':
pass
elif key == 'optional':
value = bool(int(value))
else:
value = value.split()
if key == 'define_macros':
value = [tuple(entry.split('=', maxsplit=1))
for entry in value]
value = [(entry[0], None) if len(entry) == 1 else entry
for entry in value]
if key in kwargs:
msg = 'Caution: user config in file {} shadows {}.{}.'
if build_summary is not None:
build_summary.append(msg.format(config_file, name, key))
kwargs[key] = value
kwargs.setdefault('depends', []).append(config_file)
if config is not defaultconfig:
del configs[name]
unknown_sections = configs.sections()
if unknown_sections:
print('Error: Unknown sections in file {}: {}'.format(
config_file, ', '.join(unknown_sections)))
sys.exit(1)
return exts
def init_cython():
global cython_help
required_cython_version = (0, 22)
import Cython
from Cython.Build import cythonize
# Get Cython version.
match = re.match('([0-9.]*)(.*)', Cython.__version__)
cython_version = [int(n) for n in match.group(1).split('.')]
# Decrease version if the version string contains a suffix.
if match.group(2):
while cython_version[-1] == 0:
cython_version.pop()
cython_version[-1] -= 1
cython_version = tuple(cython_version)
if cython_version < required_cython_version:
cythonize = None
return cythonize
def search_libs(libs):
cmd = ['gcc']
cmd.extend(['-l' + lib for lib in libs])
cmd.extend(['-o/dev/null', '-xc', '-'])
try:
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError:
pass
else:
p.communicate(input=b'int main() {}\n')
if p.wait() == 0:
return libs
def search_mumps():
"""Return the configuration for MUMPS if it is available in a known way.
This is known to work with the MUMPS provided by the Debian package
libmumps-scotch-dev and the MUMPS binaries in the conda-forge channel."""
lib_sets = [
# Debian
['zmumps_scotch', 'mumps_common_scotch', 'mpiseq_scotch'],
# Conda (via conda-forge).
# TODO: remove dependency libs (scotch, metis...) when conda-forge
# packaged mumps/scotch are built as properly linked shared libs
['zmumps', 'mumps_common', 'metis', 'esmumps', 'scotch',
'scotcherr', 'mpiseq'],
]
common_libs = ['pord', 'gfortran']
for libs in lib_sets:
found_libs = search_libs(libs + common_libs)
if found_libs:
return found_libs
return []
def configure_special_extensions(exts, build_summary):
# Special config for MUMPS.
mumps = exts['mumpy._mumps']
if 'libraries' in mumps:
build_summary.append('User-configured MUMPS')
else:
mumps_libs = search_mumps()
if mumps_libs:
mumps['libraries'] = mumps_libs
build_summary.append('Auto-configured MUMPS')
else:
mumps = None
del exts['mumpy._mumps']
build_summary.append('No MUMPS support')
return exts
def main():
exts = collections.OrderedDict([
('mumpy._mumps',
dict(sources=['mumpy/_mumps.pyx'],
depends=['mumpy/cmumps.pxd']))])
# Add NumPy header path to include_dirs of all the extensions.
import numpy
numpy_include = numpy.get_include()
for ext in exts.values():
ext.setdefault('include_dirs', []).append(numpy_include)
aliases = [('mumps', 'mumpy._mumps')]
global build_summary
build_summary = []
exts = configure_extensions(exts, aliases, build_summary)
exts = configure_special_extensions(exts, build_summary)
cythonize = init_cython()
if cythonize:
exts = cythonize([Extension(name, **kwargs)
for name, kwargs in exts.items()],
language_level=3,
compiler_directives={'linetrace': True})
classifiers = """\
Development Status :: 5 - Production/Stable
Intended Audience :: Science/Research
Intended Audience :: Developers
Programming Language :: Python :: 3 :: Only
Topic :: Software Development
Topic :: Scientific/Engineering
Operating System :: POSIX
Operating System :: Unix
Operating System :: MacOS :: MacOS X
Operating System :: Microsoft :: Windows"""
setup(name='mumpy',
version='0.1.0',
author='Bas Nijholt',
author_email='basnijholt@gmail.com',
description=("Python bindings for MUMPS "),
platforms=["Unix", "Linux", "Mac OS-X", "Windows"],
url="https://github.com/basnijholt/mumpy",
license="BSD",
packages=find_packages('.'),
cmdclass={'build': build,
'sdist': sdist,
'build_ext': build_ext},
ext_modules=exts,
install_requires=['numpy', 'scipy'],
classifiers=[c.strip() for c in classifiers.split('\n')])
if __name__ == '__main__':
main()
print(build_summary)