Subversion Repositories slepc-dev

Rev

Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1335 Rev 1377
#!/usr/bin/env python
#!/usr/bin/env python
#!/bin/env python
#!/bin/env python
 
#
 
#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
#     SLEPc - Scalable Library for Eigenvalue Problem Computations
 
#     Copyright (c) 2002-2007, Universidad Politecnica de Valencia, Spain
 
#
 
#     This file is part of SLEPc. See the README file for conditions of use
 
#     and additional information.
 
#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#
#    Generates fortran stubs for PETSc using Sowings bfort program
#    Generates fortran stubs for PETSc using Sowings bfort program
#
#
import os
import os
#
#
#  Opens all generated files and fixes them; also generates list in makefile.src
#  Opens all generated files and fixes them; also generates list in makefile.src
#
#
def FixFile(filename):
def FixFile(filename):
  import re
  import re
  ff = open(filename)
  ff = open(filename)
  data = ff.read()
  data = ff.read()
  ff.close()
  ff.close()
 
 
  # gotta be a better way to do this
  # gotta be a better way to do this
  data = re.subn('\nvoid ','\nvoid PETSC_STDCALL ',data)[0]
  data = re.subn('\nvoid ','\nvoid PETSC_STDCALL ',data)[0]
  data = re.subn('\nPetscErrorCode ','\nvoid PETSC_STDCALL ',data)[0]
  data = re.subn('\nPetscErrorCode ','\nvoid PETSC_STDCALL ',data)[0]
  data = re.subn('Petsc([ToRm]*)Pointer\(int\)','Petsc\\1Pointer(void*)',data)[0]        
  data = re.subn('Petsc([ToRm]*)Pointer\(int\)','Petsc\\1Pointer(void*)',data)[0]        
  data = re.subn('PetscToPointer\(a\) \(a\)','PetscToPointer(a) (*(long *)(a))',data)[0]
  data = re.subn('PetscToPointer\(a\) \(a\)','PetscToPointer(a) (*(long *)(a))',data)[0]
  data = re.subn('PetscFromPointer\(a\) \(int\)\(a\)','PetscFromPointer(a) (long)(a)',data)[0]
  data = re.subn('PetscFromPointer\(a\) \(int\)\(a\)','PetscFromPointer(a) (long)(a)',data)[0]
  data = re.subn('PetscToPointer\( \*\(int\*\)','PetscToPointer(',data)[0]
  data = re.subn('PetscToPointer\( \*\(int\*\)','PetscToPointer(',data)[0]
  data = re.subn('MPI_Comm comm','MPI_Comm *comm',data)[0]
  data = re.subn('MPI_Comm comm','MPI_Comm *comm',data)[0]
  data = re.subn('\(MPI_Comm\)PetscToPointer\( \(comm\) \)','(MPI_Comm)MPI_Comm_f2c(*(MPI_Fint*)(comm))',data)[0]
  data = re.subn('\(MPI_Comm\)PetscToPointer\( \(comm\) \)','(MPI_Comm)MPI_Comm_f2c(*(MPI_Fint*)(comm))',data)[0]
  data = re.subn('\(PetscInt\* \)PetscToPointer','',data)[0]
  data = re.subn('\(PetscInt\* \)PetscToPointer','',data)[0]
  match = re.compile(r"""\b(PETSC)(_DLL|VEC_DLL|MAT_DLL|DM_DLL|KSP_DLL|SNES_DLL|TS_DLL|FORTRAN_DLL)(EXPORT)""")
  match = re.compile(r"""\b(PETSC)(_DLL|VEC_DLL|MAT_DLL|DM_DLL|KSP_DLL|SNES_DLL|TS_DLL|FORTRAN_DLL)(EXPORT)""")
  data = match.sub(r'',data)
  data = match.sub(r'',data)
 
 
  ff = open(filename, 'w')
  ff = open(filename, 'w')
  ff.write('#include "petsc.h"\n#include "petscfix.h"\n'+data)
  ff.write('#include "petsc.h"\n#include "petscfix.h"\n'+data)
  ff.close()
  ff.close()
  return
  return
 
 
def FixDir(dir):
def FixDir(dir):
  names = []
  names = []
  for f in os.listdir(dir):
  for f in os.listdir(dir):
    if os.path.splitext(f)[1] == '.c':
    if os.path.splitext(f)[1] == '.c':
      FixFile(os.path.join(dir, f))
      FixFile(os.path.join(dir, f))
      names.append(f)
      names.append(f)
  if not names == []:
  if not names == []:
    mfile=os.path.abspath(os.path.join(dir,'..','makefile'))
    mfile=os.path.abspath(os.path.join(dir,'..','makefile'))
    try:
    try:
      fd=open(mfile,'r')
      fd=open(mfile,'r')
    except:
    except:
      print 'Error! missing file:', mfile
      print 'Error! missing file:', mfile
      return
      return
    inbuf = fd.read()
    inbuf = fd.read()
    fd.close()
    fd.close()
    libbase = ""
    libbase = ""
    locdir = ""
    locdir = ""
    for line in inbuf.splitlines():
    for line in inbuf.splitlines():
      if line.find('LIBBASE') >=0:
      if line.find('LIBBASE') >=0:
        libbase = line
        libbase = line
      elif line.find('LOCDIR') >=0:
      elif line.find('LOCDIR') >=0:
        locdir = line.rstrip() + 'ftn-auto/'
        locdir = line.rstrip() + 'ftn-auto/'
 
 
    # now assemble the makefile
    # now assemble the makefile
    outbuf  =  '\n'
    outbuf  =  '\n'
    outbuf +=  "#requirespackage   'PETSC_HAVE_FORTRAN'\n"
    outbuf +=  "#requirespackage   'PETSC_HAVE_FORTRAN'\n"
    outbuf +=  'ALL: lib\n'
    outbuf +=  'ALL: lib\n'
    outbuf +=  'CFLAGS   =\n'
    outbuf +=  'CFLAGS   =\n'
    outbuf +=  'FFLAGS   =\n'
    outbuf +=  'FFLAGS   =\n'
    outbuf +=  'SOURCEC  = ' +' '.join(names)+ '\n'
    outbuf +=  'SOURCEC  = ' +' '.join(names)+ '\n'
    outbuf +=  'OBJSC    = ' +' '.join(names).replace('.c','.o')+ '\n'    
    outbuf +=  'OBJSC    = ' +' '.join(names).replace('.c','.o')+ '\n'    
    outbuf +=  'SOURCEF  =\n'
    outbuf +=  'SOURCEF  =\n'
    outbuf +=  'SOURCEH  =\n'
    outbuf +=  'SOURCEH  =\n'
    outbuf +=  'DIRS     =\n'
    outbuf +=  'DIRS     =\n'
    outbuf +=  libbase + '\n'
    outbuf +=  libbase + '\n'
    outbuf +=  locdir + '\n'
    outbuf +=  locdir + '\n'
#    outbuf +=  'include ${PETSC_DIR}/bmake/common/base\n'
#    outbuf +=  'include ${PETSC_DIR}/bmake/common/base\n'
#    outbuf +=  'include ${PETSC_DIR}/bmake/common/test\n'
#    outbuf +=  'include ${PETSC_DIR}/bmake/common/test\n'
    outbuf +=  'include ${SLEPC_DIR}/bmake/slepc_common  \n'  
    outbuf +=  'include ${SLEPC_DIR}/bmake/slepc_common  \n'  
    ff = open(os.path.join(dir, 'makefile'), 'w')
    ff = open(os.path.join(dir, 'makefile'), 'w')
    ff.write(outbuf)
    ff.write(outbuf)
    ff.close()
    ff.close()
 
 
  # if dir is empty - remove it
  # if dir is empty - remove it
  if os.path.exists(dir) and os.path.isdir(dir) and os.listdir(dir) == []:
  if os.path.exists(dir) and os.path.isdir(dir) and os.listdir(dir) == []:
    os.rmdir(dir)
    os.rmdir(dir)
  return
  return
 
 
def PrepFtnDir(dir):
def PrepFtnDir(dir):
  if os.path.exists(dir) and not os.path.isdir(dir):
  if os.path.exists(dir) and not os.path.isdir(dir):
    raise RuntimeError('Error - specified path is not a dir: ' + dir)
    raise RuntimeError('Error - specified path is not a dir: ' + dir)
  elif not os.path.exists(dir):
  elif not os.path.exists(dir):
    os.mkdir(dir)
    os.mkdir(dir)
  else:
  else:
    files = os.listdir(dir)
    files = os.listdir(dir)
    for file in files:
    for file in files:
      os.remove(os.path.join(dir,file))
      os.remove(os.path.join(dir,file))
  return
  return
 
 
def processDir(arg,dirname,names):
def processDir(arg,dirname,names):
  import commands
  import commands
  petscdir = arg[0]
  petscdir = arg[0]
  bfort    = arg[1]
  bfort    = arg[1]
  newls = []
  newls = []
  for l in names:
  for l in names:
    if os.path.splitext(l)[1] =='.c' or os.path.splitext(l)[1] == '.h':
    if os.path.splitext(l)[1] =='.c' or os.path.splitext(l)[1] == '.h':
      newls.append(l)
      newls.append(l)
  if newls:
  if newls:
    outdir = os.path.join(dirname,'ftn-auto')
    outdir = os.path.join(dirname,'ftn-auto')
    PrepFtnDir(outdir)
    PrepFtnDir(outdir)
    options = ['-dir '+outdir, '-mnative', '-ansi', '-nomsgs', '-noprofile', '-anyname', '-mapptr',
    options = ['-dir '+outdir, '-mnative', '-ansi', '-nomsgs', '-noprofile', '-anyname', '-mapptr',
               '-mpi', '-mpi2', '-ferr', '-ptrprefix Petsc', '-ptr64 PETSC_USE_POINTER_CONVERSION',
               '-mpi', '-mpi2', '-ferr', '-ptrprefix Petsc', '-ptr64 PETSC_USE_POINTER_CONVERSION',
               '-fcaps PETSC_HAVE_FORTRAN_CAPS', '-fuscore PETSC_HAVE_FORTRAN_UNDERSCORE']
               '-fcaps PETSC_HAVE_FORTRAN_CAPS', '-fuscore PETSC_HAVE_FORTRAN_UNDERSCORE']
    (status,output) = commands.getstatusoutput('cd '+dirname+';'+bfort+' '+' '.join(options+newls))
    (status,output) = commands.getstatusoutput('cd '+dirname+';'+bfort+' '+' '.join(options+newls))
    if status:
    if status:
      raise RuntimeError('Error running bfort '+output)
      raise RuntimeError('Error running bfort '+output)
    FixDir(outdir)
    FixDir(outdir)
  for name in ['CVS', 'SCCS', 'output', 'BitKeeper', 'examples', 'externalpackages', 'bilinear', 'ftn-auto','fortran']:
  for name in ['CVS', 'SCCS', 'output', 'BitKeeper', 'examples', 'externalpackages', 'bilinear', 'ftn-auto','fortran']:
    if name in names:
    if name in names:
      names.remove(name)
      names.remove(name)
  return
  return
 
 
def main(bfort):
def main(bfort):
  petscdir = os.getcwd()
  petscdir = os.getcwd()
  tmpdir = os.path
  tmpdir = os.path
  os.path.walk(petscdir, processDir, [petscdir, bfort])
  os.path.walk(petscdir, processDir, [petscdir, bfort])
  return
  return
#
#
# The classes in this file can also be used in other python-programs by using 'import'
# The classes in this file can also be used in other python-programs by using 'import'
#
#
if __name__ ==  '__main__':
if __name__ ==  '__main__':
  import sys
  import sys
  if len(sys.argv) < 2:
  if len(sys.argv) < 2:
    sys.exit('Must give the BFORT program as the first argument')
    sys.exit('Must give the BFORT program as the first argument')
  main(sys.argv[1])
  main(sys.argv[1])