[petsc-dev] setup.py for building Python extensions involving PETSc

Chris Kees cekees at gmail.com
Thu Mar 1 08:56:11 CST 2012


Hi,

This may be of interest to people developing in Python and C/C++.  I worked
out a simple hack to use the petsc4py configuration to build Python
extension modules with the correct petsc compilers and options.  We had
been doing this using some makefile tricks, but our approach wasn't as
reliable as Lisandro's configuration of petsc4py.  I had to hack two of the
build_ext functions. You still have to give setup.py the
--petsc-dir/--petsc-arch arguments.    It might be a nice feature to
install something similar in petsc4py so one could just do 'from petsc4py
import PetscExtension'.

Chris

setup.py
-------------------
from distutils.core import setup
sys.path.append('/path_to_petsc4py')
from conf.petscconf import Extension as PetscExtension
from conf.petscconf import build_ext as petsc_build_ext
from conf.petscconf import config, build, build_src
from conf.petscconf import test, sdist

class my_build_ext(petsc_build_ext):
    def build_configuration(self, arch_list):
        from distutils.util import split_quoted, execute
        #
        template, variables = self.get_config_data(arch_list)
        config_data = template % variables
        #
        build_lib   = self.build_lib
        dist_name   = self.distribution.get_name()
        config_file = os.path.join(build_lib, dist_name,'petsc.cfg')#cek
hack
        #
        def write_file(filename, data):
            fh = open(filename, 'w')
            try: fh.write(config_data)
            finally: fh.close()
        execute(write_file, (config_file, config_data),
                msg='writing %s' % config_file,
                verbose=self.verbose, dry_run=self.dry_run)
    def _copy_ext(self, ext):
        from copy import deepcopy
        extclass = ext.__class__
        fullname = self.get_ext_fullname(ext.name)
        modpath = str.split(fullname, '.')
        pkgpath = os.path.join('', *modpath[0:-1])
        name = modpath[-1]
        sources = list(ext.sources)
        newext = extclass(name, sources)
        newext.__dict__.update(deepcopy(ext.__dict__))
        newext.name = name
        pkgpath=''#cek hack
        return pkgpath, newext

setup(name='proteus',
      ext_package='proteus',
      package_data = {'proteus' : ['petsc.cfg'],},
      cmdclass     = {'config'     : config,
                      'build'      : build,
                      'build_src'  : build_src,
                      'build_ext'  : my_build_ext,
                      'test'       : test,
                      'sdist'      : sdist,
                      },
      ext_modules=[PetscExtension('flcbdfWrappers',

 ['proteus/flcbdfWrappersModule.cpp','proteus/mesh.cpp','proteus/meshio.cpp'],

 define_macros=[('PROTEUS_TRIANGLE_H',PROTEUS_TRIANGLE_H),

('PROTEUS_SUPERLU_H',PROTEUS_SUPERLU_H),
                                            ('CMRVEC_BOUNDS_CHECK',1),
                                            ('MV_VECTOR_BOUNDS_CHECK',1),
                                            ('PETSCVEC_BOUNDS_CHECK',1),
                                            ('F77_POST_UNDERSCORE',1),
                                            ('USE_BLAS',1)],

 include_dirs=['include',numpy.get_include(),PROTEUS_SUPERLU_INCLUDE_DIR,PROTEUS_TRIANGLE_INCLUDE_DIR]+PROTEUS_DAETK_INCLUDE_DIR+PROTEUS_PETSC_INCLUDE_DIRS+
                                           [PROTEUS_MPI_INCLUDE_DIR],

 library_dirs=[PROTEUS_DAETK_LIB_DIR]+PROTEUS_PETSC_LIB_DIRS+[PROTEUS_MPI_LIB_DIR],

 libraries=['m',PROTEUS_DAETK_LIB]+PROTEUS_PETSC_LIBS+PROTEUS_MPI_LIBS,
                             extra_link_args=PROTEUS_EXTRA_LINK_ARGS,

 extra_compile_args=PROTEUS_EXTRA_COMPILE_ARGS)])
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20120301/523eee73/attachment.html>


More information about the petsc-dev mailing list