-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsetup.py
368 lines (299 loc) · 13 KB
/
setup.py
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# -*- coding: utf-8 -*-
#
"""setuptools-based setup.py template for Cython projects.
Main setup for the library.
Supports Python 2.7 and 3.4.
Usage as usual with setuptools:
python setup.py build_ext
python setup.py build
python setup.py install
python setup.py sdist
For details, see
http://setuptools.readthedocs.io/en/latest/setuptools.html#command-reference
or
python setup.py --help
python setup.py --help-commands
python setup.py --help bdist_wheel # or any command
"""
from __future__ import division, print_function, absolute_import
try:
# Python 3
MyFileNotFoundError = FileNotFoundError
except: # FileNotFoundError does not exist in Python 2.7
# Python 2.7
# - open() raises IOError
# - remove() (not currently used here) raises OSError
MyFileNotFoundError = (IOError, OSError)
#########################################################
# General config
#########################################################
# Name of the top-level package of your library.
#
# This is also the top level of its source tree, relative to the top-level project directory setup.py resides in.
#
libname="mylibrary"
# Choose build type.
#
build_type="optimized"
#build_type="debug"
# Short description for package list on PyPI
#
SHORTDESC="setuptools template for Cython projects"
# Long description for package homepage on PyPI
#
DESC="""setuptools-based setup.py template for Cython projects.
The focus of this template is on numerical scientific projects,
where a custom Cython extension (containing all-new code) can bring a large speedup.
For completeness, a minimal Cython module is included.
Supports Python 2.7 and 3.4.
"""
# Set up data files for packaging.
#
# Directories (relative to the top-level directory where setup.py resides) in which to look for data files.
datadirs = ("test",)
# File extensions to be considered as data files. (Literal, no wildcards.)
dataexts = (".py", ".pyx", ".pxd", ".c", ".cpp", ".h", ".sh", ".lyx", ".tex", ".txt", ".pdf")
# Standard documentation to detect (and package if it exists).
#
standard_docs = ["README", "LICENSE", "TODO", "CHANGELOG", "AUTHORS"] # just the basename without file extension
standard_doc_exts = [".md", ".rst", ".txt", ""] # commonly .md for GitHub projects, but other projects may use .rst or .txt (or even blank).
#########################################################
# Init
#########################################################
# check for Python 2.7 or later
# http://stackoverflow.com/questions/19534896/enforcing-python-version-in-setup-py
import sys
if sys.version_info < (2,7):
sys.exit('Sorry, Python < 2.7 is not supported')
import os
from setuptools import setup
from setuptools.extension import Extension
try:
from Cython.Build import cythonize
except ImportError:
sys.exit("Cython not found. Cython is needed to build the extension modules.")
#########################################################
# Definitions
#########################################################
# Define our base set of compiler and linker flags.
#
# This is geared toward x86_64, see
# https://gcc.gnu.org/onlinedocs/gcc-4.6.4/gcc/i386-and-x86_002d64-Options.html
#
# Customize these as needed.
#
# Note that -O3 may sometimes cause mysterious problems, so we limit ourselves to -O2.
# Modules involving numerical computations
#
extra_compile_args_math_optimized = ['-march=native', '-O2', '-msse', '-msse2', '-mfma', '-mfpmath=sse']
extra_compile_args_math_debug = ['-march=native', '-O0', '-g']
extra_link_args_math_optimized = []
extra_link_args_math_debug = []
# Modules that do not involve numerical computations
#
extra_compile_args_nonmath_optimized = ['-O2']
extra_compile_args_nonmath_debug = ['-O0', '-g']
extra_link_args_nonmath_optimized = []
extra_link_args_nonmath_debug = []
# Additional flags to compile/link with OpenMP
#
openmp_compile_args = ['-fopenmp']
openmp_link_args = ['-fopenmp']
#########################################################
# Helpers
#########################################################
# Make absolute cimports work.
#
# See
# https://github.com/cython/cython/wiki/PackageHierarchy
#
# For example: my_include_dirs = [np.get_include()]
my_include_dirs = ["."]
# Choose the base set of compiler and linker flags.
#
if build_type == 'optimized':
my_extra_compile_args_math = extra_compile_args_math_optimized
my_extra_compile_args_nonmath = extra_compile_args_nonmath_optimized
my_extra_link_args_math = extra_link_args_math_optimized
my_extra_link_args_nonmath = extra_link_args_nonmath_optimized
my_debug = False
print( "build configuration selected: optimized" )
elif build_type == 'debug':
my_extra_compile_args_math = extra_compile_args_math_debug
my_extra_compile_args_nonmath = extra_compile_args_nonmath_debug
my_extra_link_args_math = extra_link_args_math_debug
my_extra_link_args_nonmath = extra_link_args_nonmath_debug
my_debug = True
print( "build configuration selected: debug" )
else:
raise ValueError("Unknown build configuration '%s'; valid: 'optimized', 'debug'" % (build_type))
def declare_cython_extension(extName, use_math=False, use_openmp=False, include_dirs=None):
"""Declare a Cython extension module for setuptools.
Parameters:
extName : str
Absolute module name, e.g. use `mylibrary.mypackage.mymodule`
for the Cython source file `mylibrary/mypackage/mymodule.pyx`.
use_math : bool
If True, set math flags and link with ``libm``.
use_openmp : bool
If True, compile and link with OpenMP.
Return value:
Extension object
that can be passed to ``setuptools.setup``.
"""
extPath = extName.replace(".", os.path.sep)+".pyx"
if use_math:
compile_args = list(my_extra_compile_args_math) # copy
link_args = list(my_extra_link_args_math)
libraries = ["m"] # link libm; this is a list of library names without the "lib" prefix
else:
compile_args = list(my_extra_compile_args_nonmath)
link_args = list(my_extra_link_args_nonmath)
libraries = None # value if no libraries, see setuptools.extension._Extension
# OpenMP
if use_openmp:
compile_args.insert( 0, openmp_compile_args )
link_args.insert( 0, openmp_link_args )
# See
# http://docs.cython.org/src/tutorial/external.html
#
# on linking libraries to your Cython extensions.
#
return Extension( extName,
[extPath],
extra_compile_args=compile_args,
extra_link_args=link_args,
include_dirs=include_dirs,
libraries=libraries
)
# Gather user-defined data files
#
# http://stackoverflow.com/questions/13628979/setuptools-how-to-make-package-contain-extra-data-folder-and-all-folders-inside
#
datafiles = []
getext = lambda filename: os.path.splitext(filename)[1]
for datadir in datadirs:
datafiles.extend( [(root, [os.path.join(root, f) for f in files if getext(f) in dataexts])
for root, dirs, files in os.walk(datadir)] )
# Add standard documentation (README et al.), if any, to data files
#
detected_docs = []
for docname in standard_docs:
for ext in standard_doc_exts:
filename = "".join( (docname, ext) ) # relative to the directory in which setup.py resides
if os.path.isfile(filename):
detected_docs.append(filename)
datafiles.append( ('.', detected_docs) )
# Extract __version__ from the package __init__.py
# (since it's not a good idea to actually run __init__.py during the build process).
#
# http://stackoverflow.com/questions/2058802/how-can-i-get-the-version-defined-in-setup-py-setuptools-in-my-package
#
import ast
init_py_path = os.path.join(libname, '__init__.py')
version = '0.0.unknown'
try:
with open(init_py_path) as f:
for line in f:
if line.startswith('__version__'):
version = ast.parse(line).body[0].value.s
break
else:
print( "WARNING: Version information not found in '%s', using placeholder '%s'" % (init_py_path, version), file=sys.stderr )
except MyFileNotFoundError:
print( "WARNING: Could not find file '%s', using placeholder version information '%s'" % (init_py_path, version), file=sys.stderr )
#########################################################
# Set up modules
#########################################################
# declare Cython extension modules here
#
ext_module_dostuff = declare_cython_extension( "mylibrary.dostuff", use_math=False, use_openmp=False , include_dirs=my_include_dirs )
ext_module_compute = declare_cython_extension( "mylibrary.compute", use_math=True, use_openmp=False , include_dirs=my_include_dirs )
ext_module_helloworld = declare_cython_extension( "mylibrary.subpackage.helloworld", use_math=False, use_openmp=False , include_dirs=my_include_dirs )
# this is mainly to allow a manual logical ordering of the declared modules
#
cython_ext_modules = [ext_module_dostuff,
ext_module_compute,
ext_module_helloworld]
# Call cythonize() explicitly, as recommended in the Cython documentation. See
# http://cython.readthedocs.io/en/latest/src/reference/compilation.html#compiling-with-distutils
#
# This will favor Cython's own handling of '.pyx' sources over that provided by setuptools.
#
# Note that my_ext_modules is just a list of Extension objects. We could add any C sources (not coming from Cython modules) here if needed.
# cythonize() just performs the Cython-level processing, and returns a list of Extension objects.
#
my_ext_modules = cythonize( cython_ext_modules, include_path=my_include_dirs, gdb_debug=my_debug )
#########################################################
# Call setup()
#########################################################
setup(
name = "setup-template-cython",
version = version,
author = "Juha Jeronen",
author_email = "juha.jeronen@jyu.fi",
url = "https://github.com/Technologicat/setup-template-cython",
description = SHORTDESC,
long_description = DESC,
# CHANGE THIS
license = "Unlicense",
# free-form text field; http://stackoverflow.com/questions/34994130/what-platforms-argument-to-setup-in-setup-py-does
platforms = ["Linux"],
# See
# https://pypi.python.org/pypi?%3Aaction=list_classifiers
#
# for the standard classifiers.
#
# Remember to configure these appropriately for your project, especially license!
#
classifiers = [ "Development Status :: 4 - Beta",
"Environment :: Console",
"Intended Audience :: Developers",
"Intended Audience :: Science/Research",
"License :: Unlicense", # not a standard classifier; CHANGE THIS
"Operating System :: POSIX :: Linux",
"Programming Language :: Cython",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.4",
"Topic :: Scientific/Engineering",
"Topic :: Scientific/Engineering :: Mathematics",
"Topic :: Software Development :: Libraries",
"Topic :: Software Development :: Libraries :: Python Modules"
],
# See
# http://setuptools.readthedocs.io/en/latest/setuptools.html
#
setup_requires = ["cython", "numpy"],
install_requires = ["numpy"],
provides = ["setup_template_cython"],
# keywords for PyPI (in case you upload your project)
#
# e.g. the keywords your project uses as topics on GitHub, minus "python" (if there)
#
keywords = ["setuptools template example cython"],
# All extension modules (list of Extension objects)
#
ext_modules = my_ext_modules,
# Declare packages so that python -m setup build will copy .py files (especially __init__.py).
#
# This **does not** automatically recurse into subpackages, so they must also be declared.
#
packages = ["mylibrary", "mylibrary.subpackage"],
# Install also Cython headers so that other Cython modules can cimport ours
#
# Fileglobs relative to each package, **does not** automatically recurse into subpackages.
#
# FIXME: force sdist, but sdist only, to keep the .pyx files (this puts them also in the bdist)
package_data={'mylibrary': ['*.pxd', '*.pyx'],
'mylibrary.subpackage': ['*.pxd', '*.pyx']},
# Disable zip_safe, because:
# - Cython won't find .pxd files inside installed .egg, hard to compile libs depending on this one
# - dynamic loader may need to have the library unzipped to a temporary directory anyway (at import time)
#
zip_safe = False,
# Custom data files not inside a Python package
data_files = datafiles
)