setup.py 8.96 KB
Newer Older
Berk Onat's avatar
Berk Onat committed
1
2
#!/usr/bin/env python

3
import os
4
import re
Berk Onat's avatar
Berk Onat committed
5
import sys
6
7
8
import sysconfig
import platform
import subprocess
Berk Onat's avatar
Berk Onat committed
9
try:
10
    from distutils.version import LooseVersion
Berk Onat's avatar
Berk Onat committed
11
12
    from setuptools.command.build_ext import build_ext
    from setuptools import setup, Extension, Command, find_packages
13
14
    #from Cython.Build import cythonize
    #from Cython.Distutils import build_ext
Berk Onat's avatar
Berk Onat committed
15
except:
16
    from distutils.version import LooseVersion
Berk Onat's avatar
Berk Onat committed
17
18
    from distutils.command.build_ext import build_ext
    from distutils import setup, Extension, Command, find_packages
19
20
    #from Cython.Build import cythonize
    #from Cython.Distutils import build_ext
Berk Onat's avatar
Berk Onat committed
21

Berk Onat's avatar
Berk Onat committed
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
# from http://www.benjack.io/2017/06/12/python-cpp-tests.html
class CMakeExtension(Extension):
    def __init__(self, name, sourcedir=''):
        Extension.__init__(self, name, sources=[])
        self.sourcedir = os.path.abspath(sourcedir)

class CMakeBuild(build_ext):
    def run(self):
        try:
            out = subprocess.check_output(['cmake', '--version'])
        except OSError:
            raise RuntimeError(
                "CMake must be installed to build the following extensions: " +
                ", ".join(e.name for e in self.extensions))

        if platform.system() == "Windows":
            cmake_version = LooseVersion(re.search(r'version\s*([\d.]+)',
                                         out.decode()).group(1))
            if cmake_version < '3.1.0':
                raise RuntimeError("CMake >= 3.1.0 is required on Windows")

        for ext in self.extensions:
            self.build_extension(ext)

    def build_extension(self, ext):
        extdir = os.path.abspath(
            os.path.dirname(self.get_ext_fullpath(ext.name)))
        cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
                      '-DPYTHON_EXECUTABLE=' + sys.executable]

        cfg = 'Debug' if self.debug else 'Release'
        build_args = ['--config', cfg]

        if platform.system() == "Windows":
            cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(
                cfg.upper(),
                extdir)]
            if sys.maxsize > 2**32:
                cmake_args += ['-A', 'x64']
            build_args += ['--', '/m']
        else:
            cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
            build_args += ['--', '-j2']

        env = os.environ.copy()
        env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(
            env.get('CXXFLAGS', ''),
            self.distribution.get_version())
        if not os.path.exists(self.build_temp):
            os.makedirs(self.build_temp)
        subprocess.check_call(['cmake', ext.sourcedir] + cmake_args,
                              cwd=self.build_temp, env=env)
        subprocess.check_call(['cmake', '--build', '.'] + build_args,
                              cwd=self.build_temp)
        print()  # Add an empty line for cleaner output

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def get_ext_filename_without_platform_suffix(filename):
    name, ext = os.path.splitext(filename)
    ext_suffix = sysconfig.get_config_var('EXT_SUFFIX')

    if ext_suffix == ext:
        return filename

    ext_suffix = ext_suffix.replace(ext, '')
    idx = name.find(ext_suffix)

    if idx == -1:
        return filename
    else:
        return name[:idx] + ext

class BuildExtWithoutPlatformSuffix(build_ext):
    def get_ext_filename(self, ext_name):
        filename = super().get_ext_filename(ext_name)
        return get_ext_filename_without_platform_suffix(filename)
Berk Onat's avatar
Berk Onat committed
97
98
99
100
101
102

VERSION = "0.0.1"
CLASSIFIERS = [
    "Development Status :: 1 - Alpha",
    "Intended Audience :: Science/Research",
    "Intended Audience :: Developers",
Berk Onat's avatar
Berk Onat committed
103
    "License :: OSI Approved :: University of Illinois Open Source License (UIUC)",
Berk Onat's avatar
Berk Onat committed
104
    "Programming Language :: C",
Berk Onat's avatar
Berk Onat committed
105
    "Programming Language :: C++",
Berk Onat's avatar
Berk Onat committed
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
    "Programming Language :: Python",
    "Programming Language :: Python :: 3",
    "Topic :: Scientific/Engineering :: Physics",
    "Topic :: Scientific/Engineering :: Chemistry",
    "Topic :: Scientific/Engineering :: Bio-Informatics",
    "Topic :: Scientific/Engineering :: Material Science",
    "Operating System :: MacOS",
    "Operating System :: POSIX",
    "Operating System :: Unix",
    "Operating System :: Microsoft :: Windows",
]


# from MDAnalysis setup.py (http://www.mdanalysis.org/)
class NumpyExtension(Extension, object):
    """Derived class to cleanly handle setup-time (numpy) dependencies.
    """
    # The only setup-time numpy dependency comes when setting up its
    #  include dir.
    # The actual numpy import and call can be delayed until after pip
    #  has figured it must install numpy.
    # This is accomplished by passing the get_numpy_include function
    #  as one of the include_dirs. This derived Extension class takes
    #  care of calling it when needed.
    def __init__(self, *args, **kwargs):
        self._np_include_dirs = []
        super(NumpyExtension, self).__init__(*args, **kwargs)

    @property
    def include_dirs(self):
        if not self._np_include_dirs:
            for item in self._np_include_dir_args:
                try:
                    self._np_include_dirs.append(item())  # The numpy callable
                except TypeError:
                    self._np_include_dirs.append(item)
        return self._np_include_dirs

    @include_dirs.setter
    def include_dirs(self, val):
        self._np_include_dir_args = val


# from MDAnalysis setup.py (http://www.mdanalysis.org/)
def get_numpy_include():
    try:
        # Obtain the numpy include directory. This logic works across numpy
        # versions.
        # setuptools forgets to unset numpy's setup flag and we get a crippled
        # version of it unless we do it ourselves.
        try:
            import __builtin__  # py2
            __builtin__.__NUMPY_SETUP__ = False
        except:
            import builtins  # py3
            builtins.__NUMPY_SETUP__ = False
        import numpy as np
    except ImportError as e:
        print(e)
        print('*** package "numpy" not found ***')
        print('pymolfile requires a version of NumPy, even for setup.')
        print('Please get it from http://numpy.scipy.org/ or install it through '
              'your package manager.')
        sys.exit(-1)
    try:
        numpy_include = np.get_include()
    except AttributeError:
        numpy_include = np.get_numpy_include()
    return numpy_include

Berk Onat's avatar
Berk Onat committed
176
177
# from SimpleTraj setup.py (https://github.com/arose/simpletraj)
# Needed for large-file seeking under 32bit systems (migth need for indexing and access).
Berk Onat's avatar
Berk Onat committed
178
179
180
181
182
183
184
largefile_macros = [
    ( "_LARGEFILE_SOURCE", None ),
    ( "_LARGEFILE64_SOURCE", None ),
    ( "_FILE_OFFSET_BITS","64" )
]

if __name__ == '__main__':
185

Berk Onat's avatar
Berk Onat committed
186
187
188
189
    libpymolfile_module = Extension(
            'pymolfile/molfile/_libpymolfile', 
            sources=[
                'pymolfile/molfile/libpymolfile.i' , 
190
                'pymolfile/molfile/pymolfile.cxx'
Berk Onat's avatar
Berk Onat committed
191
                ],
192
            swig_opts=['-py3', '-Wall', '-c++'],
Berk Onat's avatar
Berk Onat committed
193
            library_dirs=[
Berk Onat's avatar
Berk Onat committed
194
195
                'pymolfile/molfile/external/tng/lib/',
                'pymolfile/molfile/molfile_plugins/compile/lib/'
Berk Onat's avatar
Berk Onat committed
196
                ],
197
            libraries=['netcdf','tng_io','expat'],
Berk Onat's avatar
Berk Onat committed
198
            include_dirs = [
Berk Onat's avatar
Berk Onat committed
199
                get_numpy_include(),
200
                #get_cmake_includes(),
Berk Onat's avatar
Berk Onat committed
201
                'pymolfile/molfile',
Berk Onat's avatar
Berk Onat committed
202
203
204
205
                'pymolfile/molfile/molfile_plugins/include',
                'pymolfile/molfile/molfile_plugins/molfile_plugin/include',
                'pymolfile/molfile/molfile_plugins/compile/lib/',
                'pymolfile/molfile/external/tng/include',
Berk Onat's avatar
Berk Onat committed
206
207
                ],
            extra_compile_args = [
208
209
210
211
212
                '-fPIC', '-shared', '-O2', '-w'
                #'-DNDEBUG', '-DUNIX', '-D__UNIX',  '-m64', 
                #'-fPIC', '-O2', '-w', '-fmessage-length=0'
                ],
            extra_link_args = [
Berk Onat's avatar
Berk Onat committed
213
                'pymolfile/molfile/molfile_plugins/compile/lib/libmolfile_plugin.a'
214
215
                ],
            define_macros = largefile_macros
Berk Onat's avatar
Berk Onat committed
216
            )
217
218
219
220
    #if sys.version_info > (3,):
    #    command_extension={'build_ext': BuildExtWithoutPlatformSuffix}
    #else:
    #    command_extension={}
Berk Onat's avatar
Berk Onat committed
221
    command_extension=dict(build_ext=CMakeBuild)
Berk Onat's avatar
Berk Onat committed
222

Berk Onat's avatar
Berk Onat committed
223
224
225
226
    setup(
        name = "pymolfile",
        author = "Berk Onat",
        author_email = "b.onat@warwick.ac.uk",
227
        description = "Not just a Python interface for VMD molfile plugins.",
Berk Onat's avatar
Berk Onat committed
228
229
        version = VERSION,
        classifiers = CLASSIFIERS,
Berk Onat's avatar
Berk Onat committed
230
231
        license = "UIUC",
        url = "https://gitlab.mpcdf.mpg.de/berko/pymolfile",
Berk Onat's avatar
Berk Onat committed
232
233
        zip_safe = False,
        packages = find_packages(),
234
        cmdclass= command_extension,
Berk Onat's avatar
Berk Onat committed
235
        ext_modules = [
Berk Onat's avatar
Berk Onat committed
236
            CMakeExtension('molfile', sourcedir='pymolfile/molfile/'),
Berk Onat's avatar
Berk Onat committed
237
            libpymolfile_module,
Berk Onat's avatar
Berk Onat committed
238
        ],
Berk Onat's avatar
Berk Onat committed
239
        py_modules=["pymolfile"],
Berk Onat's avatar
Berk Onat committed
240
241
242
243
        requires = [ "numpy" ],
        setup_requires = [ "numpy" ],
        install_requires = [ "numpy" ],
        extras_require = {
Berk Onat's avatar
Berk Onat committed
244
245
246
          #?do we need this?  "netcdf": [ "netCDF Library" ],
          #?and this?  "tng_io": [ "TNG Library" ],
          #?and this?  "expat": [ "Expat XML Library" ]
Berk Onat's avatar
Berk Onat committed
247
248
        }
    )