xref: /petsc/setup.py (revision 0ee81e68bb68cd9dc695ec5cb9812a4d87b703a0)
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 distutils.core import setup
33from distutils.util import get_platform, split_quoted
34from distutils.spawn import find_executable
35from distutils.command.build import build as _build
36if 'setuptools' in sys.modules:
37    from setuptools.command.install import install as _install
38else:
39    from distutils.command.install import install as _install
40from distutils.command.sdist import sdist as _sdist
41from distutils import log
42
43init_py = """\
44# Author:  PETSc Team
45# Contact: petsc-maint@mcs.anl.gov
46
47def get_petsc_dir():
48    import os
49    return os.path.dirname(__file__)
50
51def get_config():
52    conf = {}
53    conf['PETSC_DIR'] = get_petsc_dir()
54    return conf
55"""
56
57metadata = {
58    'provides' : ['petsc'],
59    'requires' : [],
60}
61
62def bootstrap():
63    # Set PETSC_DIR and PETSC_ARCH
64    PETSC_DIR  = os.path.abspath(os.getcwd())
65    PETSC_ARCH = get_platform() + '-python'
66    os.environ['PETSC_DIR']  = PETSC_DIR
67    os.environ['PETSC_ARCH'] = PETSC_ARCH
68    sys.path.insert(0, os.path.join(PETSC_DIR, 'config'))
69    # Generate package __init__.py file
70    from distutils.dir_util import mkpath
71    pkgdir = os.path.join('config', 'pypi')
72    if not os.path.exists(pkgdir): mkpath(pkgdir)
73    pkgfile = os.path.join(pkgdir, '__init__.py')
74    fh = open(pkgfile, 'wt')
75    fh.write(init_py)
76    fh.close()
77    # Simple-minded lookup for MPI and mpi4py
78    mpi4py = mpicc = None
79    try:
80        import mpi4py
81        conf = mpi4py.get_config()
82        mpicc = conf.get('mpicc')
83    except ImportError: # mpi4py is not installed
84        mpi4py = None
85        mpicc = os.environ.get('MPICC') or find_executable('mpicc')
86    except AttributeError: # mpi4py is too old
87        pass
88    if ('setuptools' in sys.modules):
89        metadata['zip_safe'] = False
90        if not mpi4py and mpicc:
91            metadata['install_requires']= ['mpi4py>=1.2.2']
92
93def config(dry_run=False):
94    log.info('PETSc: configure')
95    options = [
96        'PETSC_ARCH='+os.environ['PETSC_ARCH'],
97        '--with-shared-libraries=1',
98        '--with-debugging=0',
99        '--with-c2html=0', # not needed
100        ]
101    # MPI
102    try:
103        import mpi4py
104        conf = mpi4py.get_config()
105        mpicc  = conf.get('mpicc')
106        mpicxx = conf.get('mpicxx')
107        mpif90 = conf.get('mpif90')
108    except (ImportError, AttributeError):
109        mpicc  = os.environ.get('MPICC')  or find_executable('mpicc')
110        mpicxx = os.environ.get('MPICXX') or find_executable('mpicxx')
111        mpif90 = os.environ.get('MPIF90') or find_executable('mpif90')
112    if mpicc:
113        options.append('--with-cc='+mpicc)
114        if mpicxx:
115            options.append('--with-cxx='+mpicxx)
116        else:
117            options.append('--with-cxx=0')
118        if mpif90:
119            options.append('--with-fc='+mpif90)
120        else:
121            options.append('--with-fc=0')
122            options.append('--with-sowing=0')
123    else:
124        options.append('--with-mpi=0')
125    # Extra configure options
126    config_opts = os.environ.get('PETSC_CONFIGURE_OPTIONS', '')
127    config_opts = split_quoted(config_opts)
128    options.extend(config_opts)
129    log.info('configure options:')
130    for opt in options:
131        log.info(' '*4 + opt)
132    # Run PETSc configure
133    if dry_run: return
134    import configure
135    configure.petsc_configure(options)
136    import logger
137    logger.Logger.defaultLog = None
138
139def build(dry_run=False):
140    log.info('PETSc: build')
141    # Run PETSc build
142    if dry_run: return
143    use_builder_py = False
144    if use_builder_py:
145        import builder
146        builder.PETScMaker().run()
147        import logger
148        logger.Logger.defaultLog = None
149    else:
150        make = find_executable('make')
151        status = os.system(" ".join(
152                [make, 'all']
153                ))
154        if status != 0: raise RuntimeError(status)
155
156def install(dest_dir, prefix=None, dry_run=False):
157    log.info('PETSc: install')
158    if prefix is None:
159        prefix = dest_dir
160    options = [
161        '--destDir=' + dest_dir,
162        '--prefix='  + prefix,
163        ]
164    log.info('install options:')
165    for opt in options:
166        log.info(' '*4 + opt)
167    # Run PETSc installer
168    if dry_run: return
169    use_install_py = True
170    if use_install_py:
171        import install
172        install.Installer(options).run()
173        import logger
174        logger.Logger.defaultLog = None
175    else:
176        make = find_executable('make')
177        status = os.system(" ".join(
178                [make, 'install', 'DESTDIR='+dest_dir]
179                ))
180        if status != 0: raise RuntimeError(status)
181
182class context:
183    def __init__(self):
184        self.sys_argv = sys.argv[:]
185        self.wdir = os.getcwd()
186    def enter(self):
187        del sys.argv[1:]
188        pdir = os.environ['PETSC_DIR']
189        os.chdir(pdir)
190        return self
191    def exit(self):
192        sys.argv[:] = self.sys_argv
193        os.chdir(self.wdir)
194
195class cmd_build(_build):
196
197    def initialize_options(self):
198        _build.initialize_options(self)
199        PETSC_ARCH = os.environ.get('PETSC_ARCH', '')
200        self.build_base = os.path.join(PETSC_ARCH, 'build-python')
201
202    def run(self):
203        _build.run(self)
204        ctx = context().enter()
205        try:
206            config(self.dry_run)
207            build(self.dry_run)
208        finally:
209            ctx.exit()
210
211class cmd_install(_install):
212
213    def initialize_options(self):
214        _install.initialize_options(self)
215        self.optimize = 1
216
217    def run(self):
218        root_dir = self.install_platlib
219        dest_dir = os.path.join(root_dir, 'petsc')
220        bdist_base = self.get_finalized_command('bdist').bdist_base
221        if dest_dir.startswith(bdist_base):
222            prefix = dest_dir[len(bdist_base)+1:]
223            prefix = prefix[prefix.index(os.path.sep):]
224        else:
225            prefix = dest_dir
226        dest_dir = os.path.abspath(dest_dir)
227        prefix   = os.path.abspath(prefix)
228        #
229        _install.run(self)
230        ctx = context().enter()
231        try:
232            install(dest_dir, prefix, self.dry_run)
233        finally:
234            ctx.exit()
235
236class cmd_sdist(_sdist):
237
238    def initialize_options(self):
239        _sdist.initialize_options(self)
240        self.force_manifest = 1
241        self.template = os.path.join('config', 'manifest.in')
242
243def version():
244    import re
245    version_re = {
246        'major'  : re.compile(r"#define\s+PETSC_VERSION_MAJOR\s+(\d+)"),
247        'minor'  : re.compile(r"#define\s+PETSC_VERSION_MINOR\s+(\d+)"),
248        'micro'  : re.compile(r"#define\s+PETSC_VERSION_SUBMINOR\s+(\d+)"),
249        'patch'  : re.compile(r"#define\s+PETSC_VERSION_PATCH\s+(\d+)"),
250        'release': re.compile(r"#define\s+PETSC_VERSION_RELEASE\s+(\d+)"),
251        }
252    petscversion_h = os.path.join('include','petscversion.h')
253    data = open(petscversion_h, 'rt').read()
254    major = int(version_re['major'].search(data).groups()[0])
255    minor = int(version_re['minor'].search(data).groups()[0])
256    micro = int(version_re['micro'].search(data).groups()[0])
257    patch = int(version_re['patch'].search(data).groups()[0])
258    release = int(version_re['release'].search(data).groups()[0])
259    if release:
260        v = "%d.%d" % (major, minor)
261        if micro > 0:
262            v += ".%d" % micro
263        #if patch > 0:
264        #    v += ".post%d" % patch
265    else:
266        v = "%d.%d.dev%d" % (major, minor+1, 0)
267    return v
268
269def tarball():
270    VERSION = version()
271    if '.dev' in VERSION:
272        return None
273    bits = VERSION.split('.')
274    if len(bits) == 2: bits.append('0')
275    PETSC_VERSION = '.'.join(bits[:3])
276    return ('http://ftp.mcs.anl.gov/pub/petsc/release-snapshots/'
277            'petsc-lite-%s.tar.gz#egg=petsc-%s' % (PETSC_VERSION, VERSION))
278
279description = __doc__.split('\n')[1:-1]; del description[1:3]
280classifiers = """
281License :: Public Domain
282Operating System :: POSIX
283Intended Audience :: Developers
284Intended Audience :: Science/Research
285Programming Language :: C
286Programming Language :: C++
287Programming Language :: Fortran
288Programming Language :: Python
289Topic :: Scientific/Engineering
290Topic :: Software Development :: Libraries
291"""
292
293bootstrap()
294setup(name='petsc',
295      version=version(),
296      description=description.pop(0),
297      long_description='\n'.join(description),
298      classifiers= classifiers.split('\n')[1:-1],
299      keywords = ['PETSc', 'MPI'],
300      platforms=['POSIX'],
301      license='PETSc',
302
303      url='http://www.mcs.anl.gov/petsc/',
304      download_url=tarball(),
305
306      author='PETSc Team',
307      author_email='petsc-maint@mcs.anl.gov',
308      maintainer='Lisandro Dalcin',
309      maintainer_email='dalcinl@gmail.com',
310
311      packages = ['petsc'],
312      package_dir = {'petsc': 'config/pypi'},
313      cmdclass={
314        'build': cmd_build,
315        'install': cmd_install,
316        'sdist': cmd_sdist,
317        },
318      **metadata)
319