xref: /petsc/setup.py (revision bfc4c25e64040512a7cdc7bcb995f785926f51e6)
1#!/usr/bin/env python
2
3"""
4PETSc: Portable, Extensible Toolkit for Scientific Computation
5==============================================================
6
7The Portable, Extensible Toolkit for Scientific Computation (PETSc),
8is a suite of data structures and routines for the scalable (parallel)
9solution of scientific applications modeled by partial differential
10equations. It employs the Message Passing Interface (MPI) standard for
11all message-passing communication.
12
13.. note::
14
15   To install ``PETSc`` and ``petsc4py`` (``mpi4py`` is optional
16   but highly recommended) use::
17
18     $ pip install numpy mpi4py
19     $ pip install petsc petsc4py
20
21.. tip::
22
23  You can also install the in-development versions with::
24
25    $ pip install Cython numpy mpi4py
26    $ pip install --no-deps https://bitbucket.org/petsc/petsc/get/master.tar.gz
27    $ pip install --no-deps https://bitbucket.org/petsc/petsc4py/get/master.tar.gz
28
29"""
30
31import sys, os
32from setuptools import setup
33from setuptools.command.install import install as _install
34from distutils.util import get_platform, split_quoted
35from distutils.spawn import find_executable
36from distutils import log
37
38init_py = """\
39# Author:  PETSc Team
40# Contact: petsc-maint@mcs.anl.gov
41
42def get_petsc_dir():
43    import os
44    return os.path.dirname(__file__)
45
46def get_config():
47    conf = {}
48    conf['PETSC_DIR'] = get_petsc_dir()
49    return conf
50"""
51
52metadata = {
53    'provides' : ['petsc'],
54    'zip_safe' : False,
55}
56
57CONFIGURE_OPTIONS = []
58
59def bootstrap():
60    # Set PETSC_DIR and PETSC_ARCH
61    PETSC_DIR  = os.path.abspath(os.getcwd())
62    PETSC_ARCH = 'arch-python-' + get_platform()
63    os.environ['PETSC_DIR']  = PETSC_DIR
64    os.environ['PETSC_ARCH'] = PETSC_ARCH
65    sys.path.insert(0, os.path.join(PETSC_DIR, 'config'))
66    sys.path.insert(0, os.path.join(PETSC_DIR, 'lib','petsc','conf'))
67    # Generate package __init__.py file
68    from distutils.dir_util import mkpath
69    pkgdir = os.path.join('config', 'pypi')
70    if not os.path.exists(pkgdir): mkpath(pkgdir)
71    pkgfile = os.path.join(pkgdir, '__init__.py')
72    fh = open(pkgfile, 'w')
73    fh.write(init_py)
74    fh.close()
75    # Configure options
76    options = os.environ.get('PETSC_CONFIGURE_OPTIONS', '')
77    CONFIGURE_OPTIONS.extend(split_quoted(options))
78    if '--with-mpi=0' not in CONFIGURE_OPTIONS:
79        # Simple-minded lookup for MPI and mpi4py
80        mpi4py = mpicc = None
81        try:
82            import mpi4py
83            conf = mpi4py.get_config()
84            mpicc = conf.get('mpicc')
85        except ImportError: # mpi4py is not installed
86            mpi4py = None
87            mpicc = (os.environ.get('MPICC') or
88                     find_executable('mpicc'))
89        except AttributeError: # mpi4py is too old
90            pass
91        if not mpi4py and mpicc:
92            metadata['install_requires'] = ['mpi4py>=1.2.2']
93
94def config(prefix, dry_run=False):
95    log.info('PETSc: configure')
96    options = [
97        '--prefix=' + prefix,
98        'PETSC_ARCH='+os.environ['PETSC_ARCH'],
99        '--with-shared-libraries=1',
100        '--with-debugging=0',
101        '--with-c2html=0', # not needed
102        ]
103    if '--with-fc=0' in CONFIGURE_OPTIONS:
104        options.append('--with-sowing=0')
105    if '--with-mpi=0' not in CONFIGURE_OPTIONS:
106        try:
107            import mpi4py
108            conf = mpi4py.get_config()
109            mpicc  = conf.get('mpicc')
110            mpicxx = conf.get('mpicxx')
111            mpif90 = conf.get('mpif90')
112        except (ImportError, AttributeError):
113            mpicc  = os.environ.get('MPICC')  or find_executable('mpicc')
114            mpicxx = os.environ.get('MPICXX') or find_executable('mpicxx')
115            mpif90 = os.environ.get('MPIF90') or find_executable('mpif90')
116        if mpicc:
117            options.append('--with-cc='+mpicc)
118            if '--with-cxx=0' not in CONFIGURE_OPTIONS:
119                if mpicxx:
120                    options.append('--with-cxx='+mpicxx)
121                else:
122                    options.append('--with-cxx=0')
123            if '--with-fc=0' not in CONFIGURE_OPTIONS:
124                if mpif90:
125                    options.append('--with-fc='+mpif90)
126                else:
127                    options.append('--with-fc=0')
128                    options.append('--with-sowing=0')
129        else:
130            options.append('--with-mpi=0')
131    options.extend(CONFIGURE_OPTIONS)
132    #
133    log.info('configure options:')
134    for opt in options:
135        log.info(' '*4 + opt)
136    # Run PETSc configure
137    if dry_run: return
138    use_config_py = False
139    if use_config_py:
140        import configure
141        configure.petsc_configure(options)
142        import logger
143        logger.Logger.defaultLog = None
144    else:
145        python = find_executable('python2') or find_executable('python')
146        command = [python, './configure'] + options
147        status = os.system(" ".join(command))
148        if status != 0: raise RuntimeError(status)
149
150def build(dry_run=False):
151    log.info('PETSc: build')
152    # Run PETSc build
153    if dry_run: return
154    use_builder_py = False
155    if use_builder_py:
156        import builder
157        builder.PETScMaker().run()
158        import logger
159        logger.Logger.defaultLog = None
160    else:
161        make = find_executable('make')
162        command = [make, 'all']
163        status = os.system(" ".join(command))
164        if status != 0: raise RuntimeError(status)
165
166def install(dest_dir, dry_run=False):
167    log.info('PETSc: install')
168    options = [
169        '--destDir=' + dest_dir,
170        ]
171    log.info('install options:')
172    for opt in options:
173        log.info(' '*4 + opt)
174    # Run PETSc installer
175    if dry_run: return
176    use_install_py = False
177    if use_install_py:
178        import install
179        install.Installer(options).run()
180        import logger
181        logger.Logger.defaultLog = None
182    else:
183        make = find_executable('make')
184        command = [make, 'install', 'DESTDIR='+dest_dir]
185        status = os.system(" ".join(command))
186        if status != 0: raise RuntimeError(status)
187
188class context(object):
189    def __init__(self):
190        self.sys_argv = sys.argv[:]
191        self.wdir = os.getcwd()
192    def enter(self):
193        del sys.argv[1:]
194        pdir = os.environ['PETSC_DIR']
195        os.chdir(pdir)
196        return self
197    def exit(self):
198        sys.argv[:] = self.sys_argv
199        os.chdir(self.wdir)
200
201class cmd_install(_install):
202
203    def initialize_options(self):
204        _install.initialize_options(self)
205        self.optimize = 1
206
207    def finalize_options(self):
208        _install.finalize_options(self)
209        self.install_lib = self.install_platlib
210        self.install_libbase = self.install_lib
211
212    def run(self):
213        root_dir = os.path.abspath(self.install_lib)
214        dest_dir = prefix = os.path.join(root_dir, 'petsc')
215        #
216        ctx = context().enter()
217        try:
218            config(prefix, self.dry_run)
219            build(self.dry_run)
220            install(dest_dir, self.dry_run)
221        finally:
222            ctx.exit()
223        #
224        self.outputs = []
225        for dirpath, _, filenames in os.walk(dest_dir):
226            for fn in filenames:
227                self.outputs.append(os.path.join(dirpath, fn))
228        #
229        _install.run(self)
230
231    def get_outputs(self):
232        outputs = getattr(self, 'outputs', [])
233        outputs += _install.get_outputs(self)
234        return outputs
235
236def version():
237    import re
238    version_re = {
239        'major'  : re.compile(r"#define\s+PETSC_VERSION_MAJOR\s+(\d+)"),
240        'minor'  : re.compile(r"#define\s+PETSC_VERSION_MINOR\s+(\d+)"),
241        'micro'  : re.compile(r"#define\s+PETSC_VERSION_SUBMINOR\s+(\d+)"),
242        'release': re.compile(r"#define\s+PETSC_VERSION_RELEASE\s+(\d+)"),
243        }
244    petscversion_h = os.path.join('include','petscversion.h')
245    data = open(petscversion_h, 'r').read()
246    major = int(version_re['major'].search(data).groups()[0])
247    minor = int(version_re['minor'].search(data).groups()[0])
248    micro = int(version_re['micro'].search(data).groups()[0])
249    release = int(version_re['release'].search(data).groups()[0])
250    if release:
251        v = "%d.%d.%d" % (major, minor, micro)
252    else:
253        v = "%d.%d.0.dev%d" % (major, minor+1, 0)
254    return v
255
256def tarball():
257    VERSION = version()
258    if '.dev' in VERSION: return None
259    return ('http://ftp.mcs.anl.gov/pub/petsc/release-snapshots/'
260            'petsc-lite-%s.tar.gz#egg=petsc-%s' % (VERSION, VERSION))
261
262description = __doc__.split('\n')[1:-1]; del description[1:3]
263classifiers = """
264Development Status :: 5 - Production/Stable
265Intended Audience :: Developers
266Intended Audience :: Science/Research
267License :: OSI Approved :: BSD License
268Operating System :: POSIX
269Programming Language :: C
270Programming Language :: C++
271Programming Language :: Fortran
272Programming Language :: Python
273Topic :: Scientific/Engineering
274Topic :: Software Development :: Libraries
275"""
276
277if 'bdist_wheel' in sys.argv:
278    sys.stderr.write("petsc: this package cannot be built as a wheel\n")
279    sys.exit(1)
280
281bootstrap()
282setup(name='petsc',
283      version=version(),
284      description=description.pop(0),
285      long_description='\n'.join(description),
286      classifiers= classifiers.split('\n')[1:-1],
287      keywords = ['PETSc', 'MPI'],
288      platforms=['POSIX'],
289      license='PETSc',
290
291      url='http://www.mcs.anl.gov/petsc/',
292      download_url=tarball(),
293
294      author='PETSc Team',
295      author_email='petsc-maint@mcs.anl.gov',
296      maintainer='Lisandro Dalcin',
297      maintainer_email='dalcinl@gmail.com',
298
299      packages = ['petsc'],
300      package_dir = {'petsc': 'config/pypi'},
301      cmdclass={'install': cmd_install},
302      **metadata)
303