|
1 | 1 | #!/usr/bin/env python
|
2 | 2 | #
|
3 |
| -# Copyright Oscar Benjamin 2011 under new BSD license |
| 3 | +# Copyright Oscar Benjamin 2011 under GPLv3+ |
4 | 4 |
|
5 | 5 | import os, os.path, sys
|
6 | 6 | from distutils.core import setup
|
7 | 7 | from distutils.extension import Extension
|
| 8 | +from distutils.command.build_ext import build_ext |
| 9 | +from distutils.cygwinccompiler import Mingw32CCompiler # ming32 fix |
8 | 10 |
|
9 |
| -from Cython.Distutils import build_ext |
10 | 11 | import numpy
|
11 | 12 |
|
12 |
| -import sode |
| 13 | + |
| 14 | +# Use Cython to generate the c-files if available |
| 15 | +# |
| 16 | +# The source distribution includes the generated c-files so someone can build |
| 17 | +# it without needing cython (although they still need a c-compiler). |
| 18 | +# |
| 19 | +# Ensuring that the c-files are distributed in the source distribution means |
| 20 | +# adding the c-files to MANIFEST.in and ensuring that the cython generated |
| 21 | +# c-files are up to date by remembering to do: |
| 22 | +# |
| 23 | +# $ python setup.py build_ext --inplace |
| 24 | +# $ python setup.py sdist |
| 25 | +# |
| 26 | +# See here: |
| 27 | +# http://stackoverflow.com/questions/4505747/how-should-i-structure-a-python-package-that-contains-cython-code |
| 28 | +try: |
| 29 | + from Cython.Distutils import build_ext |
| 30 | +except ImportError: |
| 31 | + use_cython = False |
| 32 | +else: |
| 33 | + use_cython = True |
13 | 34 |
|
14 | 35 |
|
| 36 | +# These data structures are conditional on |
| 37 | +# 1) Whether cython is installed (ext_modules, cmdclass) |
| 38 | +# 2) Whether running on Windows (scripts/*.bat) |
| 39 | +cmdclass = {} |
| 40 | +ext_modules = [] |
| 41 | +scripts = [] |
| 42 | + |
| 43 | + |
| 44 | +# ::::::::::::::::::::::::::::: |
| 45 | +# Extension modules |
| 46 | +# ::::::::::::::::::::::::::::: |
| 47 | + |
| 48 | +# Need to add the .pyx name if compiling fully with cython or the .c name when |
| 49 | +# compiling from sdist without cython. |
| 50 | +def add_cython_ext_module(modname, cname, cnames=[], **kwargs): |
| 51 | + # Use cython to cythonise the .pyx files if possible |
| 52 | + if use_cython: |
| 53 | + cname = os.path.splitext(cname)[0] + '.pyx' |
| 54 | + ext_modules.append(Extension(modname, [cname] + cnames, **kwargs)) |
| 55 | + |
| 56 | + |
| 57 | +# Need to link with libm and librt on posix but not on Windows. |
15 | 58 | if not 'win' in sys.platform:
|
16 |
| - libraries_cysode = ['m', 'rt'] |
17 |
| - libraries_examples = ['m'] |
| 59 | + libs_cysode = ['m', 'rt'] |
| 60 | + libs_cyexamples = ['m'] |
| 61 | + libs_cexamples = ['m', 'rt'] |
18 | 62 | else:
|
19 |
| - libraries_cysode = libraries_examples = [] |
20 |
| - |
21 |
| - |
22 |
| -ext_modules = [ |
23 |
| - Extension( |
24 |
| - 'sode.cysode', |
25 |
| - [os.path.join('sode', 'cysode.pyx'), |
26 |
| - os.path.join('sode', 'cfiles', 'randnorm.c')], |
27 |
| - include_dirs=[numpy.get_include(), '.'], |
28 |
| - libraries=libraries_cysode |
29 |
| - ), |
30 |
| - Extension( |
31 |
| - 'sode.examples.cyfiles.examples', |
32 |
| - [os.path.join('sode', 'examples', 'cyfiles', 'examples.pyx')], |
33 |
| - include_dirs=[numpy.get_include()], |
34 |
| - libraries = libraries_examples |
35 |
| - ) |
36 |
| -] |
| 63 | + libs_cysode = [] |
| 64 | + libs_cyexamples = [] |
| 65 | + libs_cexamples = [] |
| 66 | + |
| 67 | + |
| 68 | +# This extension module is a core part of sode |
| 69 | +add_cython_ext_module('sode.cysode', |
| 70 | + os.path.join('sode', 'cysode.c'), |
| 71 | + [os.path.join('sode', 'cfiles', 'randnorm.c')], |
| 72 | + include_dirs = [numpy.get_include(), '.'], |
| 73 | + libraries = libs_cysode, |
| 74 | +) |
| 75 | + |
| 76 | + |
| 77 | +# This extension module is for the examples |
| 78 | +add_cython_ext_module('sode.examples.cyexamples.examples', |
| 79 | + os.path.join('sode', 'examples', 'cyexamples', 'examples.c'), |
| 80 | + include_dirs = [numpy.get_include(), '.'], |
| 81 | + libraries = libs_cyexamples, |
| 82 | +) |
| 83 | + |
| 84 | + |
| 85 | +# :::::::::::::::::::::::::::::::: |
| 86 | +# Scripts |
| 87 | +# :::::::::::::::::::::::::::::::: |
| 88 | + |
| 89 | +# Executable entry points in scripts |
| 90 | +scripts = ['sode', 'sode-pyexamples', 'sode-cyexamples'] |
| 91 | +scripts_dir = 'scripts' |
| 92 | + |
| 93 | + |
| 94 | +# These .bat files are needed so that Windows users can run the scripts |
| 95 | +# without the .py extension. Giving the scripts .py extensions can cause |
| 96 | +# problems with stdin/stdout redirection on Windows |
| 97 | +# http://support.microsoft.com/default.aspx?kbid=321788 |
| 98 | +if 'win' in sys.platform: |
| 99 | + scripts.extend([sname + '.bat' for sname in scripts]) |
| 100 | + |
| 101 | +scripts.append('sode-time') |
| 102 | + |
| 103 | +scripts = [os.path.join(scripts_dir, sname) for sname in scripts] |
37 | 104 |
|
| 105 | + |
| 106 | +# :::::::::::::::::::::::::::::::: |
| 107 | +# Standalone c program |
| 108 | +# :::::::::::::::::::::::::::::::: |
| 109 | + |
| 110 | +# monkey patch build_ext to also build a standalone c program |
| 111 | +# http://mail.python.org/pipermail/distutils-sig/2009-September/013216.html |
| 112 | +class MonkeyPatch_build_ext(build_ext): |
| 113 | + def run(self): |
| 114 | + global scripts |
| 115 | + build_ext.run(self) |
| 116 | + exe_name = build_examples_cprog(self.compiler) |
| 117 | + scripts.append(exe_name) |
| 118 | + |
| 119 | +cmdclass['build_ext'] = MonkeyPatch_build_ext |
| 120 | + |
| 121 | + |
| 122 | +# We also need to build the examples c-program |
| 123 | +def build_examples_cprog(compiler): |
| 124 | + if isinstance(compiler, Mingw32CCompiler): |
| 125 | + mingw32_compiler_fix(compiler) |
| 126 | + |
| 127 | + cfiles = ['main.c', 'examples.c', 'randnorm.c', 'solvers.c'] |
| 128 | + cfiles = [os.path.join('sode', 'cfiles', p) for p in cfiles] |
| 129 | + exe_name = os.path.join(scripts_dir, 'sode-cexamples') |
| 130 | + |
| 131 | + compiler.link_executable(cfiles, exe_name, libraries=libs_cexamples) |
| 132 | + |
| 133 | + # compiler.exe_extension is None on posix, string on Windows |
| 134 | + if compiler.exe_extension is not None: |
| 135 | + exe_name += compiler.exe_extension |
| 136 | + |
| 137 | + return exe_name |
| 138 | + |
| 139 | + |
| 140 | +# msvcr90 needs manifest etc. See: |
| 141 | +# http://developer.berlios.de/devlog/akruis/2012/06/10/msvcr90dll-and-mingw/ |
| 142 | +# Since this is a standalone program we can just link against the old version |
| 143 | +# of msvcr. |
| 144 | +def mingw32_compiler_fix(compiler): |
| 145 | + for n, dllname in enumerate(compiler.dll_libraries): |
| 146 | + if dllname.startswith('msvcr'): |
| 147 | + compiler.dll_libraries[n] = min(dllname, 'msvcr71') |
| 148 | + return |
| 149 | + |
| 150 | + |
| 151 | +# ::::::::::::::::::::::::::::::: |
| 152 | +# Bring it all together |
| 153 | +# ::::::::::::::::::::::::::::::: |
| 154 | + |
| 155 | +# Use the README.rst file as the front-page on PyPI |
| 156 | +#with open('README.rst') as README: |
| 157 | +# LONG_DESCRIPTION = README.read() |
| 158 | + |
| 159 | + |
| 160 | +# Standard distutils setup |
38 | 161 | setup(
|
| 162 | + # First the metadata |
39 | 163 | name = 'sode',
|
40 |
| - cmdclass = {'build_ext': build_ext}, |
| 164 | + version = '0.0.3', |
| 165 | + author = 'Oscar Benjamin', |
| 166 | + author_email = '[email protected]', |
| 167 | + url = 'https://github.com/oscarbenjamin/sode', |
| 168 | + description = ('Python/Cython lib for solving ' + |
| 169 | + 'Stochastic Ordinary Differential Equations'), |
| 170 | + # long_description = open('README.rst').read() |
| 171 | + platforms = ['linux2', 'win32', 'darwin'], # tested on these |
| 172 | + license = 'GPLv3+', |
| 173 | + |
| 174 | + classifiers = [ |
| 175 | + 'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)', |
| 176 | + |
| 177 | + 'Intended Audience :: Science/Research', |
| 178 | + |
| 179 | + 'Topic :: Scientific/Engineering :: Mathematics', |
| 180 | + 'Topic :: Scientific/Engineering :: Physics', |
| 181 | + 'Topic :: Scientific/Engineering :: Chemistry', |
| 182 | + 'Topic :: Scientific/Engineering :: Bio-Informatics', |
| 183 | + |
| 184 | + 'Operating System :: POSIX :: Linux', |
| 185 | + 'Operating System :: Microsoft :: Windows', |
| 186 | + 'Operating System :: MacOS :: MacOS X', |
| 187 | + |
| 188 | + 'Programming Language :: Cython', |
| 189 | + 'Programming Language :: Python :: Implementation :: CPython', |
| 190 | + 'Programming Language :: Python :: 2.6', |
| 191 | + 'Programming Language :: Python :: 2.7', |
| 192 | + ], |
| 193 | + |
| 194 | + # Dependency information |
| 195 | + provides = ['sode'], |
| 196 | + requires = ['numpy (>=1.4)', 'cython (>=0.15)'], |
| 197 | + |
| 198 | + # Now the content |
| 199 | + cmdclass = cmdclass, |
41 | 200 | ext_modules = ext_modules,
|
| 201 | + packages = ['sode', 'sode.examples', 'sode.examples.pyexamples', |
| 202 | + 'sode.examples.cyexamples'], |
| 203 | + scripts = scripts |
42 | 204 | )
|
0 commit comments