xref: /petsc/config/PETSc/Configure.py (revision aa5c8b8edd203001313d8a34df97b430023c2df9)
1f8833479SBarry Smithimport config.base
2f8833479SBarry Smith
3f8833479SBarry Smithimport os
46dd73af6SBarry Smithimport sys
5f8833479SBarry Smithimport re
640277576SBarry Smithimport cPickle
7f8833479SBarry Smith
88bec23c5SJed Brown# The sorted() builtin is not available with python-2.3
98bec23c5SJed Browntry: sorted
108bec23c5SJed Brownexcept NameError:
118bec23c5SJed Brown  def sorted(lst):
128bec23c5SJed Brown    lst.sort()
138bec23c5SJed Brown    return lst
148bec23c5SJed Brown
15f8833479SBarry Smithclass Configure(config.base.Configure):
16f8833479SBarry Smith  def __init__(self, framework):
17f8833479SBarry Smith    config.base.Configure.__init__(self, framework)
18f8833479SBarry Smith    self.headerPrefix = 'PETSC'
19f8833479SBarry Smith    self.substPrefix  = 'PETSC'
20*aa5c8b8eSBarry Smith    self.installed = 0 # 1 indicates that Configure itself has already compiled and installed PETSc
21f8833479SBarry Smith    return
22f8833479SBarry Smith
237c939e48SSatish Balay  def __str2__(self):
247c939e48SSatish Balay    desc = []
25*aa5c8b8eSBarry Smith    if not self.installed:
26a0022257SSatish Balay      desc.append('xxx=========================================================================xxx')
27ac1d0f13SJed Brown      if self.make.getMakeMacro('MAKE_IS_GNUMAKE'):
289481793eSSatish Balay        build_type = 'gnumake build'
299481793eSSatish Balay      elif self.getMakeMacro('PETSC_BUILD_USING_CMAKE'):
30b3618d6dSSatish Balay        build_type = 'cmake build'
31b3618d6dSSatish Balay      else:
32b3618d6dSSatish Balay        build_type = 'legacy build'
33b3618d6dSSatish Balay      desc.append(' Configure stage complete. Now build PETSc libraries with (%s):' % build_type)
34b3618d6dSSatish Balay      desc.append('   make PETSC_DIR='+self.petscdir.dir+' PETSC_ARCH='+self.arch.arch+' all')
35a0022257SSatish Balay      desc.append('xxx=========================================================================xxx')
36*aa5c8b8eSBarry Smith    else:
37*aa5c8b8eSBarry Smith      desc.append('xxx=========================================================================xxx')
38*aa5c8b8eSBarry Smith      desc.append(' Installation complete. You do not need to run make to compile or install the software')
39*aa5c8b8eSBarry Smith      desc.append('xxx=========================================================================xxx')
407c939e48SSatish Balay    return '\n'.join(desc)+'\n'
41f8833479SBarry Smith
42f8833479SBarry Smith  def setupHelp(self, help):
43f8833479SBarry Smith    import nargs
44ce0b2093SBarry Smith    help.addArgument('PETSc',  '-prefix=<dir>',                   nargs.Arg(None, '', 'Specifiy location to install PETSc (eg. /usr/local)'))
457deb5ab3SBarry Smith    help.addArgument('PETSc',  '-with-prefetch=<bool>',           nargs.ArgBool(None, 1,'Enable checking for prefetch instructions'))
46eed94e11SSatish Balay    help.addArgument('Windows','-with-windows-graphics=<bool>',   nargs.ArgBool(None, 1,'Enable check for Windows Graphics'))
47569865ddSSatish Balay    help.addArgument('PETSc', '-with-default-arch=<bool>',        nargs.ArgBool(None, 1, 'Allow using the last configured arch without setting PETSC_ARCH'))
4857cb31baSSatish Balay    help.addArgument('PETSc','-with-single-library=<bool>',       nargs.ArgBool(None, 1,'Put all PETSc code into the single -lpetsc library'))
49525d6f2eSBarry Smith    help.addArgument('PETSc', '-with-ios=<bool>',              nargs.ArgBool(None, 0, 'Build an iPhone/iPad version of PETSc library'))
508fd71741SJason Sarich    help.addArgument('PETSc', '-with-xsdk-defaults', nargs.ArgBool(None, 0, 'Set the following as defaults for the xSDK standard: --enable-debug=1, --enable-shared=1, --with-precision=double, --with-index-size=32, locate blas/lapack automatically'))
51752d89a4SSatish Balay    help.addArgument('PETSc', '-known-has-attribute-aligned=<bool>',nargs.ArgBool(None, None, 'Indicates __attribute((aligned(16)) directive works (the usual test will be skipped)'))
5227b0f280SBarry Smith    help.addArgument('PETSc','-with-viewfromoptions=<bool>',      nargs.ArgBool(None, 1,'Support XXXSetFromOptions() calls, for calls with many small solvers turn this off'))
53752d89a4SSatish Balay
54f8833479SBarry Smith    return
55f8833479SBarry Smith
566dd73af6SBarry Smith  def registerPythonFile(self,filename,directory):
576dd73af6SBarry Smith    ''' Add a python file to the framework and registers its headerprefix, ... externalpackagedir
586dd73af6SBarry Smith        directory is the directory where the file relative to the BuildSystem or config path in python notation with . '''
596dd73af6SBarry Smith    (utilityName, ext) = os.path.splitext(filename)
606dd73af6SBarry Smith    if not utilityName.startswith('.') and not utilityName.startswith('#') and ext == '.py' and not utilityName == '__init__':
616dd73af6SBarry Smith      if directory: directory = directory+'.'
626dd73af6SBarry Smith      utilityObj                             = self.framework.require(directory+utilityName, self)
636dd73af6SBarry Smith      utilityObj.headerPrefix                = self.headerPrefix
646dd73af6SBarry Smith      utilityObj.archProvider                = self.arch
656dd73af6SBarry Smith      utilityObj.languageProvider            = self.languages
666dd73af6SBarry Smith      utilityObj.installDirProvider          = self.installdir
676dd73af6SBarry Smith      utilityObj.externalPackagesDirProvider = self.externalpackagesdir
686dd73af6SBarry Smith      utilityObj.precisionProvider           = self.scalartypes
696dd73af6SBarry Smith      utilityObj.indexProvider               = self.indexTypes
706dd73af6SBarry Smith      setattr(self, utilityName.lower(), utilityObj)
716dd73af6SBarry Smith
72f8833479SBarry Smith  def setupDependencies(self, framework):
73f8833479SBarry Smith    config.base.Configure.setupDependencies(self, framework)
74dca78d2bSSatish Balay    self.programs      = framework.require('config.programs',           self)
75f8833479SBarry Smith    self.setCompilers  = framework.require('config.setCompilers',       self)
7630b8aa07SMatthew G. Knepley    self.compilers     = framework.require('config.compilers',          self)
779d310bb7SBarry Smith    self.arch          = framework.require('PETSc.options.arch',        self.setCompilers)
789d310bb7SBarry Smith    self.petscdir      = framework.require('PETSc.options.petscdir',    self.arch)
799d310bb7SBarry Smith    self.installdir    = framework.require('PETSc.options.installDir',  self)
806dd73af6SBarry Smith    self.scalartypes   = framework.require('PETSc.options.scalarTypes', self)
816dd73af6SBarry Smith    self.indexTypes    = framework.require('PETSc.options.indexTypes',  self)
829d310bb7SBarry Smith    self.languages     = framework.require('PETSc.options.languages',   self.setCompilers)
835aab0b90SMatthew G. Knepley    self.debugging     = framework.require('PETSc.options.debugging',   self.compilers)
8430b8aa07SMatthew G. Knepley    self.indexTypes    = framework.require('PETSc.options.indexTypes',  self.compilers)
85f8833479SBarry Smith    self.compilers     = framework.require('config.compilers',          self)
86f8833479SBarry Smith    self.types         = framework.require('config.types',              self)
87f8833479SBarry Smith    self.headers       = framework.require('config.headers',            self)
88f8833479SBarry Smith    self.functions     = framework.require('config.functions',          self)
89f8833479SBarry Smith    self.libraries     = framework.require('config.libraries',          self)
90cd37d877SShri Abhyankar    self.atomics       = framework.require('config.atomics',            self)
919481793eSSatish Balay    self.make          = framework.require('config.packages.make',      self)
929552296fSBarry Smith    self.blasLapack    = framework.require('config.packages.BlasLapack',self)
9306e08bc7SBarry Smith    self.cmake         = framework.require('config.packages.cmake',self)
949d310bb7SBarry Smith    self.externalpackagesdir = framework.require('PETSc.options.externalpackagesdir',self)
95e6b0c433SBarry Smith    self.mpi           = framework.require('config.packages.MPI',self)
9649d43ecaSSatish Balay
979d310bb7SBarry Smith    for utility in os.listdir(os.path.join('config','PETSc','options')):
986dd73af6SBarry Smith      self.registerPythonFile(utility,'PETSc.options')
999d310bb7SBarry Smith
1009d310bb7SBarry Smith    for utility in os.listdir(os.path.join('config','BuildSystem','config','utilities')):
1016dd73af6SBarry Smith      self.registerPythonFile(utility,'config.utilities')
10206e08bc7SBarry Smith
10347c09f67SMatthew G. Knepley    for package in os.listdir(os.path.join('config', 'BuildSystem', 'config', 'packages')):
1046dd73af6SBarry Smith      self.registerPythonFile(package,'config.packages')
1056dd73af6SBarry Smith
1065faf1eacSMatthew G. Knepley    # Force blaslapack and opencl to depend on scalarType so precision is set before BlasLapack is built
1079d310bb7SBarry Smith    framework.require('PETSc.options.scalarTypes', self.f2cblaslapack)
1089d310bb7SBarry Smith    framework.require('PETSc.options.scalarTypes', self.fblaslapack)
1099d310bb7SBarry Smith    framework.require('PETSc.options.scalarTypes', self.blaslapack)
1105faf1eacSMatthew G. Knepley    framework.require('PETSc.options.scalarTypes', self.opencl)
1119d310bb7SBarry Smith    framework.require('PETSc.Regression', self)
112f8833479SBarry Smith
113dca78d2bSSatish Balay    self.programs.headerPrefix   = self.headerPrefix
114f8833479SBarry Smith    self.compilers.headerPrefix  = self.headerPrefix
115f8833479SBarry Smith    self.types.headerPrefix      = self.headerPrefix
116f8833479SBarry Smith    self.headers.headerPrefix    = self.headerPrefix
117f8833479SBarry Smith    self.functions.headerPrefix  = self.headerPrefix
118f8833479SBarry Smith    self.libraries.headerPrefix  = self.headerPrefix
1196dd73af6SBarry Smith
1206dd73af6SBarry Smith    # Look for any user provided --download-xxx=directory packages
1216dd73af6SBarry Smith    for arg in sys.argv:
1226dd73af6SBarry Smith      if arg.startswith('--download-') and arg.find('=') > -1:
1236dd73af6SBarry Smith        pname = arg[11:arg.find('=')]
1246dd73af6SBarry Smith        if not hasattr(self,pname):
1256dd73af6SBarry Smith          dname = os.path.dirname(arg[arg.find('=')+1:])
126ab079e5dSBarry Smith          if os.path.isdir(dname) and not os.path.isfile(os.path.join(dname,pname+'.py')):
127ab079e5dSBarry Smith            self.framework.logPrint('User is registering a new package: '+arg)
1286dd73af6SBarry Smith            sys.path.append(dname)
1296dd73af6SBarry Smith            self.registerPythonFile(pname+'.py','')
1306dd73af6SBarry Smith
1316dd73af6SBarry Smith    # test for a variety of basic headers and functions
132a8b45ee7SBarry Smith    headersC = map(lambda name: name+'.h', ['setjmp','dos', 'endian', 'fcntl', 'float', 'io', 'limits', 'malloc', 'pwd', 'search', 'strings',
133ba61063dSBarry Smith                                            'unistd', 'sys/sysinfo', 'machine/endian', 'sys/param', 'sys/procfs', 'sys/resource',
134a3aaec0aSJed Brown                                            'sys/systeminfo', 'sys/times', 'sys/utsname','string', 'stdlib',
135f8833479SBarry Smith                                            'sys/socket','sys/wait','netinet/in','netdb','Direct','time','Ws2tcpip','sys/types',
136e4773d96SSatish Balay                                            'WindowsX', 'cxxabi','float','ieeefp','stdint','sched','pthread','mathimf'])
13745082d64SJed Brown    functions = ['access', '_access', 'clock', 'drand48', 'getcwd', '_getcwd', 'getdomainname', 'gethostname',
138f8833479SBarry Smith                 'gettimeofday', 'getwd', 'memalign', 'memmove', 'mkstemp', 'popen', 'PXFGETARG', 'rand', 'getpagesize',
13938ecfe64SSatish Balay                 'readlink', 'realpath',  'sigaction', 'signal', 'sigset', 'usleep', 'sleep', '_sleep', 'socket',
140473bb0d5SSatish Balay                 'times', 'gethostbyname', 'uname','snprintf','_snprintf','lseek','_lseek','time','fork','stricmp',
141ac7218bbSSatish Balay                 'strcasecmp', 'bzero', 'dlopen', 'dlsym', 'dlclose', 'dlerror','get_nprocs','sysctlbyname',
1420787ed6cSSatish Balay                 '_set_output_format','_mkdir']
143f8833479SBarry Smith    libraries1 = [(['socket', 'nsl'], 'socket'), (['fpe'], 'handle_sigfpes')]
144f8833479SBarry Smith    self.headers.headers.extend(headersC)
145f8833479SBarry Smith    self.functions.functions.extend(functions)
146f8833479SBarry Smith    self.libraries.libraries.extend(libraries1)
1477d421530SBarry Smith
148f8833479SBarry Smith    return
149f8833479SBarry Smith
150262119f8SBarry Smith  def DumpPkgconfig(self):
151262119f8SBarry Smith    ''' Create a pkg-config file '''
152262119f8SBarry Smith    if not os.path.exists(os.path.join(self.petscdir.dir,self.arch.arch,'lib','pkgconfig')):
153262119f8SBarry Smith      os.makedirs(os.path.join(self.petscdir.dir,self.arch.arch,'lib','pkgconfig'))
154262119f8SBarry Smith    fd = open(os.path.join(self.petscdir.dir,self.arch.arch,'lib','pkgconfig','PETSc.pc'),'w')
155262119f8SBarry Smith    if self.framework.argDB['prefix']:
1565bb5b263SMatthew G. Knepley      fd.write('prefix='+self.installdir.dir+'\n')
157262119f8SBarry Smith      fd.write('exec_prefix=${prefix}\n')
158262119f8SBarry Smith      fd.write('includedir=${prefix}/include\n')
159262119f8SBarry Smith    else:
160262119f8SBarry Smith      fd.write('prefix='+self.petscdir.dir+'\n')
161262119f8SBarry Smith      fd.write('exec_prefix=${prefix}\n')
162262119f8SBarry Smith      fd.write('includedir=${prefix}/include\n')
1635bb5b263SMatthew G. Knepley    fd.write('libdir='+os.path.join(self.installdir.dir,'lib')+'\n')
164262119f8SBarry Smith
165262119f8SBarry Smith    self.setCompilers.pushLanguage('C')
166262119f8SBarry Smith    fd.write('ccompiler='+self.setCompilers.getCompiler()+'\n')
167262119f8SBarry Smith    self.setCompilers.popLanguage()
168262119f8SBarry Smith    if hasattr(self.compilers, 'C++'):
169262119f8SBarry Smith      self.setCompilers.pushLanguage('C++')
170262119f8SBarry Smith      fd.write('cxxcompiler='+self.setCompilers.getCompiler()+'\n')
171262119f8SBarry Smith      self.setCompilers.popLanguage()
172262119f8SBarry Smith    if hasattr(self.compilers, 'FC'):
173262119f8SBarry Smith      self.setCompilers.pushLanguage('FC')
174262119f8SBarry Smith      fd.write('fcompiler='+self.setCompilers.getCompiler()+'\n')
175262119f8SBarry Smith      self.setCompilers.popLanguage()
1769552296fSBarry Smith    fd.write('blaslapacklibs='+self.libraries.toStringNoDupes(self.blaslapack.lib)+'\n')
177262119f8SBarry Smith
178262119f8SBarry Smith    fd.write('\n')
179262119f8SBarry Smith    fd.write('Name: PETSc\n')
180262119f8SBarry Smith    fd.write('Description: Library to solve ODEs and algebraic equations\n')
181351d3a41SMatthew G Knepley    fd.write('Version: %s\n' % self.petscdir.version)
182262119f8SBarry Smith
183262119f8SBarry Smith    fd.write('Cflags: '+self.allincludes+'\n')
184262119f8SBarry Smith
185473a3ab2SBarry Smith    plibs = self.libraries.toStringNoDupes(['-L'+os.path.join(self.petscdir.dir,self.arch.arch,'lib'),' -lpetsc'])
186262119f8SBarry Smith    if self.framework.argDB['prefix']:
187c032e545SMatthew G. Knepley      fd.write('Libs: '+plibs.replace(os.path.join(self.petscdir.dir,self.arch.arch),self.installdir.dir)+'\n')
188262119f8SBarry Smith    else:
189473a3ab2SBarry Smith      fd.write('Libs: '+plibs+'\n')
1907ef6e71eSSatish Balay    fd.write('Libs.private: '+self.libraries.toStringNoDupes(self.packagelibs+self.libraries.math+self.compilers.flibs+self.compilers.cxxlibs)+' '+self.compilers.LIBS+'\n')
191473a3ab2SBarry Smith
192262119f8SBarry Smith    fd.close()
193262119f8SBarry Smith    return
194262119f8SBarry Smith
195351d3a41SMatthew G Knepley  def DumpModule(self):
196351d3a41SMatthew G Knepley    ''' Create a module file '''
197af0996ceSBarry Smith    if not os.path.exists(os.path.join(self.petscdir.dir,self.arch.arch,'lib','petsc','conf','modules')):
198af0996ceSBarry Smith      os.makedirs(os.path.join(self.petscdir.dir,self.arch.arch,'lib','petsc','conf','modules'))
199af0996ceSBarry Smith    if not os.path.exists(os.path.join(self.petscdir.dir,self.arch.arch,'lib','petsc','conf','modules','petsc')):
200af0996ceSBarry Smith      os.makedirs(os.path.join(self.petscdir.dir,self.arch.arch,'lib','petsc','conf','modules','petsc'))
201351d3a41SMatthew G Knepley    if self.framework.argDB['prefix']:
2025bb5b263SMatthew G. Knepley      installdir  = self.installdir.dir
20355d606a3SSatish Balay      installarch = ''
20455d606a3SSatish Balay      installpath = os.path.join(installdir,'bin')
205351d3a41SMatthew G Knepley    else:
206351d3a41SMatthew G Knepley      installdir  = self.petscdir.dir
20755d606a3SSatish Balay      installarch = self.arch.arch
20855d606a3SSatish Balay      installpath = os.path.join(installdir,installarch,'bin')+':'+os.path.join(installdir,'bin')
209af0996ceSBarry Smith    fd = open(os.path.join(self.petscdir.dir,self.arch.arch,'lib','petsc','conf','modules','petsc',self.petscdir.version),'w')
210351d3a41SMatthew G Knepley    fd.write('''\
211351d3a41SMatthew G Knepley#%%Module
212351d3a41SMatthew G Knepley
213351d3a41SMatthew G Knepleyproc ModulesHelp { } {
214351d3a41SMatthew G Knepley    puts stderr "This module sets the path and environment variables for petsc-%s"
215351d3a41SMatthew G Knepley    puts stderr "     see http://www.mcs.anl.gov/petsc/ for more information      "
216351d3a41SMatthew G Knepley    puts stderr ""
217351d3a41SMatthew G Knepley}
218351d3a41SMatthew G Knepleymodule-whatis "PETSc - Portable, Extensible Toolkit for Scientific Computation"
219351d3a41SMatthew G Knepley
220351d3a41SMatthew G Knepleyset petsc_dir   %s
221351d3a41SMatthew G Knepleyset petsc_arch  %s
222351d3a41SMatthew G Knepley
223351d3a41SMatthew G Knepleysetenv PETSC_ARCH $petsc_arch
224351d3a41SMatthew G Knepleysetenv PETSC_DIR $petsc_dir
22555d606a3SSatish Balayprepend-path PATH %s
22655d606a3SSatish Balay''' % (self.petscdir.version, installdir, installarch, installpath))
227351d3a41SMatthew G Knepley    fd.close()
228351d3a41SMatthew G Knepley    return
229351d3a41SMatthew G Knepley
230f8833479SBarry Smith  def Dump(self):
231f8833479SBarry Smith    ''' Actually put the values into the configuration files '''
232f8833479SBarry Smith    # eventually everything between -- should be gone
23317f368bcSBarry Smith    if self.mpi.usingMPIUni:
23417f368bcSBarry Smith      #
23517f368bcSBarry Smith      # Remove any MPI/MPICH include files that may have been put here by previous runs of ./configure
2367908f030SMatthew G. Knepley      self.executeShellCommand('rm -rf  '+os.path.join(self.petscdir.dir,self.arch.arch,'include','mpi*')+' '+os.path.join(self.petscdir.dir,self.arch.arch,'include','opa*'), log = self.log)
23717f368bcSBarry Smith
238f8833479SBarry Smith#-----------------------------------------------------------------------------------------------------
239f8833479SBarry Smith
240f8833479SBarry Smith    # Sometimes we need C compiler, even if built with C++
241f8833479SBarry Smith    self.setCompilers.pushLanguage('C')
242f8833479SBarry Smith    self.addMakeMacro('CC_FLAGS',self.setCompilers.getCompilerFlags())
243f8833479SBarry Smith    self.setCompilers.popLanguage()
244f8833479SBarry Smith
24534f774f6SJed Brown    # And sometimes we need a C++ compiler even when PETSc is built with C
24634f774f6SJed Brown    if hasattr(self.compilers, 'CXX'):
24734f774f6SJed Brown      self.setCompilers.pushLanguage('Cxx')
24834f774f6SJed Brown      self.addMakeMacro('CXX_FLAGS',self.setCompilers.getCompilerFlags())
24934f774f6SJed Brown      self.setCompilers.popLanguage()
25034f774f6SJed Brown
251f8833479SBarry Smith    # C preprocessor values
2521315f054SBarry Smith    self.addMakeMacro('CPP_FLAGS',self.setCompilers.CPPFLAGS)
253f8833479SBarry Smith
254f8833479SBarry Smith    # compiler values
255f8833479SBarry Smith    self.setCompilers.pushLanguage(self.languages.clanguage)
256f8833479SBarry Smith    self.addMakeMacro('PCC',self.setCompilers.getCompiler())
257f8833479SBarry Smith    self.addMakeMacro('PCC_FLAGS',self.setCompilers.getCompilerFlags())
258f8833479SBarry Smith    self.setCompilers.popLanguage()
259f8833479SBarry Smith    # .o or .obj
260f8833479SBarry Smith    self.addMakeMacro('CC_SUFFIX','o')
261f8833479SBarry Smith
262f8833479SBarry Smith    # executable linker values
263f8833479SBarry Smith    self.setCompilers.pushLanguage(self.languages.clanguage)
264f8833479SBarry Smith    pcc_linker = self.setCompilers.getLinker()
265f8833479SBarry Smith    self.addMakeMacro('PCC_LINKER',pcc_linker)
266c84a332bSSatish Balay    self.addMakeMacro('PCC_LINKER_FLAGS',self.setCompilers.getLinkerFlags())
267f8833479SBarry Smith    self.setCompilers.popLanguage()
268f8833479SBarry Smith    # '' for Unix, .exe for Windows
269f8833479SBarry Smith    self.addMakeMacro('CC_LINKER_SUFFIX','')
270f8833479SBarry Smith
271f8833479SBarry Smith    if hasattr(self.compilers, 'FC'):
272f8833479SBarry Smith      self.setCompilers.pushLanguage('FC')
273f8833479SBarry Smith      # need FPPFLAGS in config/setCompilers
274f8833479SBarry Smith      self.addDefine('HAVE_FORTRAN','1')
275f8833479SBarry Smith      self.addMakeMacro('FPP_FLAGS',self.setCompilers.CPPFLAGS)
276f8833479SBarry Smith
277f8833479SBarry Smith      # compiler values
278f8833479SBarry Smith      self.addMakeMacro('FC_FLAGS',self.setCompilers.getCompilerFlags())
279f8833479SBarry Smith      self.setCompilers.popLanguage()
280f8833479SBarry Smith      # .o or .obj
281f8833479SBarry Smith      self.addMakeMacro('FC_SUFFIX','o')
282f8833479SBarry Smith
283f8833479SBarry Smith      # executable linker values
284f8833479SBarry Smith      self.setCompilers.pushLanguage('FC')
285f8833479SBarry Smith      # Cannot have NAG f90 as the linker - so use pcc_linker as fc_linker
286f8833479SBarry Smith      fc_linker = self.setCompilers.getLinker()
2877fca349cSMatthew G. Knepley      if config.setCompilers.Configure.isNAG(fc_linker, self.log):
288f8833479SBarry Smith        self.addMakeMacro('FC_LINKER',pcc_linker)
289f8833479SBarry Smith      else:
290f8833479SBarry Smith        self.addMakeMacro('FC_LINKER',fc_linker)
2916d53d35eSSatish Balay      self.addMakeMacro('FC_LINKER_FLAGS',self.setCompilers.getLinkerFlags())
2923feacd00SBarry Smith      # apple requires this shared library linker flag on SOME versions of the os
2933feacd00SBarry Smith      if self.setCompilers.getLinkerFlags().find('-Wl,-commons,use_dylibs') > -1:
2943feacd00SBarry Smith        self.addMakeMacro('DARWIN_COMMONS_USE_DYLIBS',' -Wl,-commons,use_dylibs ')
295bb82cf9cSSatish Balay      self.setCompilers.popLanguage()
2965d631499SMatthew Knepley
2975d631499SMatthew Knepley      # F90 Modules
2985d631499SMatthew Knepley      if self.setCompilers.fortranModuleIncludeFlag:
2995d631499SMatthew Knepley        self.addMakeMacro('FC_MODULE_FLAG', self.setCompilers.fortranModuleIncludeFlag)
3006ddd6694SSatish Balay      else: # for non-f90 compilers like g77
3016ddd6694SSatish Balay        self.addMakeMacro('FC_MODULE_FLAG', '-I')
302a324c51cSMatthew G Knepley      if self.setCompilers.fortranModuleIncludeFlag:
303a324c51cSMatthew G Knepley        self.addMakeMacro('FC_MODULE_OUTPUT_FLAG', self.setCompilers.fortranModuleOutputFlag)
304f8833479SBarry Smith    else:
305f8833479SBarry Smith      self.addMakeMacro('FC','')
306f8833479SBarry Smith
30746a3958fSBarry Smith    if hasattr(self.compilers, 'CUDAC'):
3087ff2890cSSatish Balay      self.setCompilers.pushLanguage('CUDA')
309d93a25ecSSatish Balay      self.addMakeMacro('CUDAC_FLAGS',self.setCompilers.getCompilerFlags())
3107ff2890cSSatish Balay      self.setCompilers.popLanguage()
3117ff2890cSSatish Balay
312f8833479SBarry Smith    # shared library linker values
313f8833479SBarry Smith    self.setCompilers.pushLanguage(self.languages.clanguage)
314f8833479SBarry Smith    # need to fix BuildSystem to collect these separately
315f8833479SBarry Smith    self.addMakeMacro('SL_LINKER',self.setCompilers.getLinker())
31670db8aa6SSatish Balay    self.addMakeMacro('SL_LINKER_FLAGS','${PCC_LINKER_FLAGS}')
317f8833479SBarry Smith    self.setCompilers.popLanguage()
318f8833479SBarry Smith    # One of 'a', 'so', 'lib', 'dll', 'dylib' (perhaps others also?) depending on the library generator and architecture
319f8833479SBarry Smith    # Note: . is not included in this macro, consistent with AR_LIB_SUFFIX
320f8833479SBarry Smith    if self.setCompilers.sharedLibraryExt == self.setCompilers.AR_LIB_SUFFIX:
321f8833479SBarry Smith      self.addMakeMacro('SL_LINKER_SUFFIX', '')
32246bc77b6SBarry Smith      self.addDefine('SLSUFFIX','""')
323f8833479SBarry Smith    else:
324f8833479SBarry Smith      self.addMakeMacro('SL_LINKER_SUFFIX', self.setCompilers.sharedLibraryExt)
32546bc77b6SBarry Smith      self.addDefine('SLSUFFIX','"'+self.setCompilers.sharedLibraryExt+'"')
326bb82cf9cSSatish Balay
32723e93537SBarry Smith    self.addMakeMacro('SL_LINKER_LIBS','${PETSC_EXTERNAL_LIB_BASIC}')
328bb82cf9cSSatish Balay
329f8833479SBarry Smith#-----------------------------------------------------------------------------------------------------
330f8833479SBarry Smith
331f8833479SBarry Smith    # CONLY or CPP. We should change the PETSc makefiles to do this better
332f8833479SBarry Smith    if self.languages.clanguage == 'C': lang = 'CONLY'
333f8833479SBarry Smith    else: lang = 'CXXONLY'
334f8833479SBarry Smith    self.addMakeMacro('PETSC_LANGUAGE',lang)
335f8833479SBarry Smith
336f8833479SBarry Smith    # real or complex
337f8833479SBarry Smith    self.addMakeMacro('PETSC_SCALAR',self.scalartypes.scalartype)
338f8833479SBarry Smith    # double or float
339f8833479SBarry Smith    self.addMakeMacro('PETSC_PRECISION',self.scalartypes.precision)
340f8833479SBarry Smith
341f8833479SBarry Smith    if self.framework.argDB['with-batch']:
342f8833479SBarry Smith      self.addMakeMacro('PETSC_WITH_BATCH','1')
343f8833479SBarry Smith
344f8833479SBarry Smith    # Test for compiler-specific macros that need to be defined.
3457fca349cSMatthew G. Knepley    if self.setCompilers.isCrayVector('CC', self.log):
346b409243cSBarry Smith      self.addDefine('HAVE_CRAY_VECTOR','1')
347f8833479SBarry Smith
348f8833479SBarry Smith#-----------------------------------------------------------------------------------------------------
349df1a78b3SMatthew G Knepley    if self.functions.haveFunction('gethostbyname') and self.functions.haveFunction('socket') and self.headers.haveHeader('netinet/in.h'):
350f8833479SBarry Smith      self.addDefine('USE_SOCKET_VIEWER','1')
35180e3883bSBarry Smith      if self.checkCompile('#include <sys/socket.h>','setsockopt(0,SOL_SOCKET,SO_REUSEADDR,0,0)'):
35280e3883bSBarry Smith        self.addDefine('HAVE_SO_REUSEADDR','1')
353f8833479SBarry Smith
354f8833479SBarry Smith#-----------------------------------------------------------------------------------------------------
355a6cc6bb1SBarry Smith    # print include and lib for makefiles
356f8833479SBarry Smith    self.framework.packages.reverse()
357a6cc6bb1SBarry Smith    includes = [os.path.join(self.petscdir.dir,'include'),os.path.join(self.petscdir.dir,self.arch.arch,'include')]
358996b3231SBarry Smith    libs = []
359f8833479SBarry Smith    for i in self.framework.packages:
360898a086dSBarry Smith      if i.useddirectly:
361eeb16384SBarry Smith        self.addDefine('HAVE_'+i.PACKAGE.replace('-','_'), 1)  # ONLY list package if it is used directly by PETSc (and not only by another package)
362f8833479SBarry Smith      if not isinstance(i.lib, list):
363f8833479SBarry Smith        i.lib = [i.lib]
3644d02c0d4SBarry Smith      if i.linkedbypetsc: libs.extend(i.lib)
365eeb16384SBarry Smith      self.addMakeMacro(i.PACKAGE.replace('-','_')+'_LIB', self.libraries.toStringNoDupes(i.lib))
366f8833479SBarry Smith      if hasattr(i,'include'):
367f8833479SBarry Smith        if not isinstance(i.include,list):
368f8833479SBarry Smith          i.include = [i.include]
369ac9e4c42SSatish Balay        includes.extend(i.include)
370eeb16384SBarry Smith        self.addMakeMacro(i.PACKAGE.replace('-','_')+'_INCLUDE',self.headers.toStringNoDupes(i.include))
371473a3ab2SBarry Smith    self.packagelibs = libs
3722df986feSBarry Smith    if self.framework.argDB['with-single-library']:
3731315f054SBarry Smith      self.alllibs = self.libraries.toStringNoDupes(['-L'+os.path.join(self.petscdir.dir,self.arch.arch,'lib'),' -lpetsc']+libs+self.libraries.math+self.compilers.flibs+self.compilers.cxxlibs)+' '+self.compilers.LIBS
374262119f8SBarry Smith      self.addMakeMacro('PETSC_WITH_EXTERNAL_LIB',self.alllibs)
37591bb3077SSatish Balay    else:
3761315f054SBarry Smith      self.alllibs = self.libraries.toStringNoDupes(['-L'+os.path.join(self.petscdir.dir,self.arch.arch,'lib'),'-lpetscts -lpetscsnes -lpetscksp -lpetscdm -lpetscmat -lpetscvec -lpetscsys']+libs+self.libraries.math+self.compilers.flibs+self.compilers.cxxlibs)+' '+self.compilers.LIBS
3771315f054SBarry Smith    self.PETSC_EXTERNAL_LIB_BASIC = self.libraries.toStringNoDupes(libs+self.libraries.math+self.compilers.flibs+self.compilers.cxxlibs)+' '+self.compilers.LIBS
3781026b6b4SSatish Balay    if self.framework.argDB['prefix'] and self.setCompilers.CSharedLinkerFlag not in ['-L']:
3795bb5b263SMatthew G. Knepley      lib_basic = self.PETSC_EXTERNAL_LIB_BASIC.replace(self.setCompilers.CSharedLinkerFlag+os.path.join(self.petscdir.dir,self.arch.arch,'lib'),self.setCompilers.CSharedLinkerFlag+os.path.join(self.installdir.dir,'lib'))
3801026b6b4SSatish Balay    else:
3811026b6b4SSatish Balay      lib_basic = self.PETSC_EXTERNAL_LIB_BASIC
3821026b6b4SSatish Balay    self.addMakeMacro('PETSC_EXTERNAL_LIB_BASIC',lib_basic)
383262119f8SBarry Smith    self.allincludes = self.headers.toStringNoDupes(includes)
384262119f8SBarry Smith    self.addMakeMacro('PETSC_CC_INCLUDES',self.allincludes)
385262119f8SBarry Smith    self.PETSC_CC_INCLUDES = self.allincludes
386cbd5cc15SBarry Smith    if hasattr(self.compilers, 'FC'):
387208c3fd5SBarry Smith      if self.compilers.fortranIsF90:
38843a63bfbSSatish Balay        self.addMakeMacro('PETSC_FC_INCLUDES',self.headers.toStringNoDupes(includes,includes))
38930d43657SSatish Balay      else:
39030d43657SSatish Balay        self.addMakeMacro('PETSC_FC_INCLUDES',self.headers.toStringNoDupes(includes))
391f8833479SBarry Smith
3925bb5b263SMatthew G. Knepley    self.addMakeMacro('DESTDIR',self.installdir.dir)
3935bb5b263SMatthew G. Knepley    self.addDefine('LIB_DIR','"'+os.path.join(self.installdir.dir,'lib')+'"')
394f8833479SBarry Smith
3950f3b21c2SBarry Smith    if self.framework.argDB['with-single-library']:
3960f3b21c2SBarry Smith      # overrides the values set in conf/variables
3970f3b21c2SBarry Smith      self.addMakeMacro('LIBNAME','${INSTALL_LIB_DIR}/libpetsc.${AR_LIB_SUFFIX}')
39857cb31baSSatish Balay      self.addMakeMacro('SHLIBS','libpetsc')
399bccf1c12SBarry Smith      self.addMakeMacro('PETSC_LIB_BASIC','-lpetsc')
400797063a9SSatish Balay      self.addMakeMacro('PETSC_KSP_LIB_BASIC','-lpetsc')
401797063a9SSatish Balay      self.addMakeMacro('PETSC_TS_LIB_BASIC','-lpetsc')
402b0a7d7e7SSatish Balay      self.addMakeMacro('PETSC_TAO_LIB_BASIC','-lpetsc')
403bb84e0fdSBarry Smith      self.addDefine('USE_SINGLE_LIBRARY', '1')
4042df986feSBarry Smith      if self.sharedlibraries.useShared:
405ea820d49SSatish Balay        self.addMakeMacro('PETSC_SYS_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
406ea820d49SSatish Balay        self.addMakeMacro('PETSC_VEC_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
407ea820d49SSatish Balay        self.addMakeMacro('PETSC_MAT_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
408ea820d49SSatish Balay        self.addMakeMacro('PETSC_DM_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
409ea820d49SSatish Balay        self.addMakeMacro('PETSC_KSP_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
410ea820d49SSatish Balay        self.addMakeMacro('PETSC_SNES_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
411ea820d49SSatish Balay        self.addMakeMacro('PETSC_TS_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
412b0a7d7e7SSatish Balay        self.addMakeMacro('PETSC_TAO_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
413fdb87e33SJed Brown        self.addMakeMacro('PETSC_CHARACTERISTIC_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
414ea820d49SSatish Balay        self.addMakeMacro('PETSC_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
415ea820d49SSatish Balay        self.addMakeMacro('PETSC_CONTRIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
4162df986feSBarry Smith      else:
417ea820d49SSatish Balay        self.addMakeMacro('PETSC_SYS_LIB','${PETSC_WITH_EXTERNAL_LIB}')
418ea820d49SSatish Balay        self.addMakeMacro('PETSC_VEC_LIB','${PETSC_WITH_EXTERNAL_LIB}')
419ea820d49SSatish Balay        self.addMakeMacro('PETSC_MAT_LIB','${PETSC_WITH_EXTERNAL_LIB}')
420ea820d49SSatish Balay        self.addMakeMacro('PETSC_DM_LIB','${PETSC_WITH_EXTERNAL_LIB}')
421ea820d49SSatish Balay        self.addMakeMacro('PETSC_KSP_LIB','${PETSC_WITH_EXTERNAL_LIB}')
422ea820d49SSatish Balay        self.addMakeMacro('PETSC_SNES_LIB','${PETSC_WITH_EXTERNAL_LIB}')
423ea820d49SSatish Balay        self.addMakeMacro('PETSC_TS_LIB','${PETSC_WITH_EXTERNAL_LIB}')
424b0a7d7e7SSatish Balay        self.addMakeMacro('PETSC_TAO_LIB','${PETSC_WITH_EXTERNAL_LIB}')
425fdb87e33SJed Brown        self.addMakeMacro('PETSC_CHARACTERISTIC_LIB','${PETSC_WITH_EXTERNAL_LIB}')
426ea820d49SSatish Balay        self.addMakeMacro('PETSC_LIB','${PETSC_WITH_EXTERNAL_LIB}')
427ea820d49SSatish Balay        self.addMakeMacro('PETSC_CONTRIB','${PETSC_WITH_EXTERNAL_LIB}')
4280f3b21c2SBarry Smith
429f8833479SBarry Smith    if not os.path.exists(os.path.join(self.petscdir.dir,self.arch.arch,'lib')):
430f8833479SBarry Smith      os.makedirs(os.path.join(self.petscdir.dir,self.arch.arch,'lib'))
431f8833479SBarry Smith
432f8833479SBarry Smith    # add a makefile entry for configure options
433f8833479SBarry Smith    self.addMakeMacro('CONFIGURE_OPTIONS', self.framework.getOptionsString(['configModules', 'optionsModule']).replace('\"','\\"'))
434f8833479SBarry Smith    return
435f8833479SBarry Smith
436f8833479SBarry Smith  def dumpConfigInfo(self):
437f8833479SBarry Smith    import time
438f8833479SBarry Smith    fd = file(os.path.join(self.arch.arch,'include','petscconfiginfo.h'),'w')
439f8833479SBarry Smith    fd.write('static const char *petscconfigureoptions = "'+self.framework.getOptionsString(['configModules', 'optionsModule']).replace('\"','\\"')+'";\n')
440f8833479SBarry Smith    fd.close()
441f8833479SBarry Smith    return
442f8833479SBarry Smith
4432a4161d9SMatthew G Knepley  def dumpMachineInfo(self):
4442a4161d9SMatthew G Knepley    import platform
4452a4161d9SMatthew G Knepley    import time
44640373944SSatish Balay    import script
447ca77dbeeSGeoffrey Irving    def escape(s):
448ca77dbeeSGeoffrey Irving      return s.replace('"',r'\"').replace(r'\ ',r'\\ ')
4492a4161d9SMatthew G Knepley    fd = file(os.path.join(self.arch.arch,'include','petscmachineinfo.h'),'w')
4502a4161d9SMatthew G Knepley    fd.write('static const char *petscmachineinfo = \"\\n\"\n')
4512a4161d9SMatthew G Knepley    fd.write('\"-----------------------------------------\\n\"\n')
4522a4161d9SMatthew G Knepley    fd.write('\"Libraries compiled on %s on %s \\n\"\n' % (time.ctime(time.time()), platform.node()))
45360acdfe7SSatish Balay    fd.write('\"Machine characteristics: %s\\n\"\n' % (platform.platform()))
454ca77dbeeSGeoffrey Irving    fd.write('\"Using PETSc directory: %s\\n\"\n' % (escape(self.petscdir.dir)))
455ca77dbeeSGeoffrey Irving    fd.write('\"Using PETSc arch: %s\\n\"\n' % (escape(self.arch.arch)))
456cdec380aSBarry Smith    fd.write('\"-----------------------------------------\\n\";\n')
4572a4161d9SMatthew G Knepley    fd.write('static const char *petsccompilerinfo = \"\\n\"\n')
4582a4161d9SMatthew G Knepley    self.setCompilers.pushLanguage(self.languages.clanguage)
459ca77dbeeSGeoffrey Irving    fd.write('\"Using C compiler: %s %s ${COPTFLAGS} ${CFLAGS}\\n\"\n' % (escape(self.setCompilers.getCompiler()), escape(self.setCompilers.getCompilerFlags())))
4602a4161d9SMatthew G Knepley    self.setCompilers.popLanguage()
4618782282cSMatthew G Knepley    if hasattr(self.compilers, 'FC'):
4622a4161d9SMatthew G Knepley      self.setCompilers.pushLanguage('FC')
463ca77dbeeSGeoffrey Irving      fd.write('\"Using Fortran compiler: %s %s ${FOPTFLAGS} ${FFLAGS} %s\\n\"\n' % (escape(self.setCompilers.getCompiler()), escape(self.setCompilers.getCompilerFlags()), escape(self.setCompilers.CPPFLAGS)))
4642a4161d9SMatthew G Knepley      self.setCompilers.popLanguage()
465cdec380aSBarry Smith    fd.write('\"-----------------------------------------\\n\";\n')
4662a4161d9SMatthew G Knepley    fd.write('static const char *petsccompilerflagsinfo = \"\\n\"\n')
467ca77dbeeSGeoffrey Irving    fd.write('\"Using include paths: %s %s %s\\n\"\n' % ('-I'+escape(os.path.join(self.petscdir.dir, self.arch.arch, 'include')), '-I'+escape(os.path.join(self.petscdir.dir, 'include')), escape(self.PETSC_CC_INCLUDES)))
468cdec380aSBarry Smith    fd.write('\"-----------------------------------------\\n\";\n')
4692a4161d9SMatthew G Knepley    fd.write('static const char *petsclinkerinfo = \"\\n\"\n')
4702a4161d9SMatthew G Knepley    self.setCompilers.pushLanguage(self.languages.clanguage)
471ca77dbeeSGeoffrey Irving    fd.write('\"Using C linker: %s\\n\"\n' % (escape(self.setCompilers.getLinker())))
4722a4161d9SMatthew G Knepley    self.setCompilers.popLanguage()
4738782282cSMatthew G Knepley    if hasattr(self.compilers, 'FC'):
4742a4161d9SMatthew G Knepley      self.setCompilers.pushLanguage('FC')
475ca77dbeeSGeoffrey Irving      fd.write('\"Using Fortran linker: %s\\n\"\n' % (escape(self.setCompilers.getLinker())))
4762a4161d9SMatthew G Knepley      self.setCompilers.popLanguage()
477ad782ac6SSatish Balay    if self.framework.argDB['with-single-library']:
478ad782ac6SSatish Balay      petsclib = '-lpetsc'
479ad782ac6SSatish Balay    else:
480ad782ac6SSatish Balay      petsclib = '-lpetscts -lpetscsnes -lpetscksp -lpetscdm -lpetscmat -lpetscvec -lpetscsys'
481ca77dbeeSGeoffrey Irving    fd.write('\"Using libraries: %s%s -L%s %s %s\\n\"\n' % (escape(self.setCompilers.CSharedLinkerFlag), escape(os.path.join(self.petscdir.dir, self.arch.arch, 'lib')), escape(os.path.join(self.petscdir.dir, self.arch.arch, 'lib')), escape(petsclib), escape(self.PETSC_EXTERNAL_LIB_BASIC)))
482cdec380aSBarry Smith    fd.write('\"-----------------------------------------\\n\";\n')
4832a4161d9SMatthew G Knepley    fd.close()
4842a4161d9SMatthew G Knepley    return
485b2843cf1SBarry Smith
486511a6afcSJed Brown  def dumpCMakeConfig(self):
487511a6afcSJed Brown    '''
488724dfae7SSatish Balay    Writes configuration-specific values to ${PETSC_ARCH}/lib/petsc/conf/PETScBuildInternal.cmake.
489511a6afcSJed Brown    This file is private to PETSc and should not be included by third parties
490511a6afcSJed Brown    (a suitable file can be produced later by CMake, but this is not it).
491511a6afcSJed Brown    '''
492511a6afcSJed Brown    def cmakeset(fd,key,val=True):
493511a6afcSJed Brown      if val == True: val = 'YES'
494511a6afcSJed Brown      if val == False: val = 'NO'
495511a6afcSJed Brown      fd.write('set (' + key + ' ' + val + ')\n')
496511a6afcSJed Brown    def ensurelist(a):
497826d9344SJed Brown      if isinstance(a,list):
498826d9344SJed Brown        return a
499826d9344SJed Brown      else:
500826d9344SJed Brown        return [a]
501511a6afcSJed Brown    def libpath(lib):
502511a6afcSJed Brown      'Returns a search path if that is what this item provides, else "" which will be cleaned out later'
5031b1c0b30SJed Brown      if not isinstance(lib,str): return ''
504511a6afcSJed Brown      if lib.startswith('-L'): return lib[2:]
505511a6afcSJed Brown      if lib.startswith('-R'): return lib[2:]
506511a6afcSJed Brown      if lib.startswith('-Wl,-rpath,'):
507511a6afcSJed Brown        # This case occurs when an external package needs a specific system library that is normally provided by the compiler.
508511a6afcSJed Brown        # In other words, the -L path is builtin to the wrapper or compiler, here we provide it so that CMake can locate the
509511a6afcSJed Brown        # corresponding library.
510511a6afcSJed Brown        return lib[len('-Wl,-rpath,'):]
511511a6afcSJed Brown      if lib.startswith('-'): return ''
512511a6afcSJed Brown      return os.path.dirname(lib)
513511a6afcSJed Brown    def cleanlib(lib):
514511a6afcSJed Brown      'Returns a library name if that is what this item provides, else "" which will be cleaned out later'
51542e8629dSMatthew G Knepley      if not isinstance(lib,str): return ''
516511a6afcSJed Brown      if lib.startswith('-l'):  return lib[2:]
517511a6afcSJed Brown      if lib.startswith('-Wl') or lib.startswith('-L'): return ''
518511a6afcSJed Brown      lib = os.path.splitext(os.path.basename(lib))[0]
519511a6afcSJed Brown      if lib.startswith('lib'): return lib[3:]
520511a6afcSJed Brown      return lib
521511a6afcSJed Brown    def nub(lst):
52206e8c1ddSJed Brown      'Return a list containing the first occurrence of each unique element'
523511a6afcSJed Brown      unique = []
524511a6afcSJed Brown      for elem in lst:
525511a6afcSJed Brown        if elem not in unique and elem != '':
526511a6afcSJed Brown          unique.append(elem)
527511a6afcSJed Brown      return unique
52850937898SJed Brown    try: reversed # reversed was added in Python-2.4
52950937898SJed Brown    except NameError:
53050937898SJed Brown      def reversed(lst): return lst[::-1]
53106e8c1ddSJed Brown    def nublast(lst):
53206e8c1ddSJed Brown      'Return a list containing the last occurrence of each unique entry in a list'
53350937898SJed Brown      return reversed(nub(reversed(lst)))
534511a6afcSJed Brown    def cmakeexpand(varname):
535511a6afcSJed Brown      return r'"${' + varname + r'}"'
536582751aaSJed Brown    def uniqextend(lst,new):
537511a6afcSJed Brown      for x in ensurelist(new):
538582751aaSJed Brown        if x not in lst:
539582751aaSJed Brown          lst.append(x)
540511a6afcSJed Brown    def notstandardinclude(path):
541040257f2SJed Brown      return path not in '/usr/include'.split() # /usr/local/include is not automatically included on FreeBSD
542511a6afcSJed Brown    def writeMacroDefinitions(fd):
543511a6afcSJed Brown      if self.mpi.usingMPIUni:
544511a6afcSJed Brown        cmakeset(fd,'PETSC_HAVE_MPIUNI')
545511a6afcSJed Brown      for pkg in self.framework.packages:
546511a6afcSJed Brown        if pkg.useddirectly:
547eeb16384SBarry Smith          cmakeset(fd,'PETSC_HAVE_' + pkg.PACKAGE.replace('-','_'))
548a23e9343SMatthew G Knepley        for pair in pkg.defines.items():
549440af75fSJed Brown          if pair[0].startswith('HAVE_') and pair[1]:
550a23e9343SMatthew G Knepley            cmakeset(fd, self.framework.getFullDefineName(pkg, pair[0]), pair[1])
551511a6afcSJed Brown      for name,val in self.functions.defines.items():
552511a6afcSJed Brown        cmakeset(fd,'PETSC_'+name,val)
553511a6afcSJed Brown      for dct in [self.defines, self.libraryoptions.defines]:
554511a6afcSJed Brown        for k,v in dct.items():
555511a6afcSJed Brown          if k.startswith('USE_'):
556511a6afcSJed Brown            cmakeset(fd,'PETSC_' + k, v)
557511a6afcSJed Brown      cmakeset(fd,'PETSC_USE_COMPLEX', self.scalartypes.scalartype == 'complex')
558ce63c4c1SBarry Smith      cmakeset(fd,'PETSC_USE_REAL_' + self.scalartypes.precision.upper())
559511a6afcSJed Brown      cmakeset(fd,'PETSC_CLANGUAGE_'+self.languages.clanguage)
560511a6afcSJed Brown      if hasattr(self.compilers, 'FC'):
561511a6afcSJed Brown        cmakeset(fd,'PETSC_HAVE_FORTRAN')
562511a6afcSJed Brown        if self.compilers.fortranIsF90:
563511a6afcSJed Brown          cmakeset(fd,'PETSC_USING_F90')
564876d5c60SBarry Smith        if self.compilers.fortranIsF2003:
565876d5c60SBarry Smith          cmakeset(fd,'PETSC_USING_F2003')
56613c0a95cSJed Brown      if hasattr(self.compilers, 'CXX'):
56713c0a95cSJed Brown        cmakeset(fd,'PETSC_HAVE_CXX')
568511a6afcSJed Brown      if self.sharedlibraries.useShared:
569511a6afcSJed Brown        cmakeset(fd,'BUILD_SHARED_LIBS')
570511a6afcSJed Brown    def writeBuildFlags(fd):
57106e8c1ddSJed Brown      def extendby(lib):
57206e8c1ddSJed Brown        libs = ensurelist(lib)
57306e8c1ddSJed Brown        lib_paths.extend(map(libpath,libs))
57406e8c1ddSJed Brown        lib_libs.extend(map(cleanlib,libs))
575511a6afcSJed Brown      lib_paths = []
576511a6afcSJed Brown      lib_libs  = []
577511a6afcSJed Brown      includes  = []
578511a6afcSJed Brown      libvars   = []
579511a6afcSJed Brown      for pkg in self.framework.packages:
5804d02c0d4SBarry Smith        if pkg.linkedbypetsc:
58106e8c1ddSJed Brown          extendby(pkg.lib)
582040257f2SJed Brown          uniqextend(includes,pkg.include)
58306e8c1ddSJed Brown      extendby(self.libraries.math)
58406e8c1ddSJed Brown      extendby(self.libraries.rt)
58506e8c1ddSJed Brown      extendby(self.compilers.flibs)
58606e8c1ddSJed Brown      extendby(self.compilers.cxxlibs)
58706e8c1ddSJed Brown      extendby(self.compilers.LIBS.split())
58806e8c1ddSJed Brown      for libname in nublast(lib_libs):
589511a6afcSJed Brown        libvar = 'PETSC_' + libname.upper() + '_LIB'
5904c0032a9SSatish Balay        addpath = ''
59106e8c1ddSJed Brown        for lpath in nublast(lib_paths):
5924c0032a9SSatish Balay          addpath += '"' + str(lpath) + '" '
5934c0032a9SSatish Balay        fd.write('find_library (' + libvar + ' ' + libname + ' HINTS ' + addpath + ')\n')
594511a6afcSJed Brown        libvars.append(libvar)
595511a6afcSJed Brown      fd.write('mark_as_advanced (' + ' '.join(libvars) + ')\n')
596511a6afcSJed Brown      fd.write('set (PETSC_PACKAGE_LIBS ' + ' '.join(map(cmakeexpand,libvars)) + ')\n')
597040257f2SJed Brown      includes = filter(notstandardinclude,includes)
598040257f2SJed Brown      fd.write('set (PETSC_PACKAGE_INCLUDES ' + ' '.join(map(lambda i: '"'+i+'"',includes)) + ')\n')
599724dfae7SSatish Balay    fd = open(os.path.join(self.arch.arch,'lib','petsc','conf','PETScBuildInternal.cmake'), 'w')
600511a6afcSJed Brown    writeMacroDefinitions(fd)
601511a6afcSJed Brown    writeBuildFlags(fd)
602511a6afcSJed Brown    fd.close()
603511a6afcSJed Brown    return
604511a6afcSJed Brown
6058b0282a9SJed Brown  def dumpCMakeLists(self):
6068b0282a9SJed Brown    import sys
607994b4dadSSatish Balay    if sys.version_info >= (2,4):
6088b0282a9SJed Brown      import cmakegen
6098b0282a9SJed Brown      try:
610a98e69d2SJed Brown        cmakegen.main(self.petscdir.dir, log=self.framework.log)
6118b0282a9SJed Brown      except (OSError), e:
6128b0282a9SJed Brown        self.framework.logPrint('Generating CMakeLists.txt failed:\n' + str(e))
613aac20692SSatish Balay    else:
614aac20692SSatish Balay      self.framework.logPrint('Skipping cmakegen due to old python version: ' +str(sys.version_info) )
6158b0282a9SJed Brown
6168b0282a9SJed Brown  def cmakeBoot(self):
6178b0282a9SJed Brown    import sys
618ae937f1dSJed Brown    self.cmakeboot_success = False
619994b4dadSSatish Balay    if sys.version_info >= (2,4) and hasattr(self.cmake,'cmake'):
6205a4feeedSSatish Balay      oldRead = self.argDB.readonly
6215a4feeedSSatish Balay      self.argDB.readonly = True
622356464bcSMatthew G Knepley      try:
6238b0282a9SJed Brown        import cmakeboot
624ae937f1dSJed Brown        self.cmakeboot_success = cmakeboot.main(petscdir=self.petscdir.dir,petscarch=self.arch.arch,argDB=self.argDB,framework=self.framework,log=self.framework.log)
6258b0282a9SJed Brown      except (OSError), e:
6268b0282a9SJed Brown        self.framework.logPrint('Booting CMake in PETSC_ARCH failed:\n' + str(e))
627356464bcSMatthew G Knepley      except (ImportError, KeyError), e:
628356464bcSMatthew G Knepley        self.framework.logPrint('Importing cmakeboot failed:\n' + str(e))
6295a4feeedSSatish Balay      self.argDB.readonly = oldRead
6309b12c9c7SJed Brown      if self.cmakeboot_success:
6312f730bc2SKarl Rupp        if hasattr(self.compilers, 'FC') and self.compilers.fortranIsF90 and not self.setCompilers.fortranModuleOutputFlag:
63291f9b906SSatish Balay          self.framework.logPrint('CMake configured successfully, but could not be used by default because of missing fortranModuleOutputFlag\n')
6339b12c9c7SJed Brown        else:
6349b12c9c7SJed Brown          self.framework.logPrint('CMake configured successfully, using as default build\n')
635f7b66a64SJed Brown          self.addMakeMacro('PETSC_BUILD_USING_CMAKE',1)
636aac20692SSatish Balay      else:
6379b12c9c7SJed Brown        self.framework.logPrint('CMake configuration was unsuccessful\n')
6389b12c9c7SJed Brown    else:
639aac20692SSatish Balay      self.framework.logPrint('Skipping cmakeboot due to old python version: ' +str(sys.version_info) )
640356464bcSMatthew G Knepley    return
6418b0282a9SJed Brown
642b2843cf1SBarry Smith  def configurePrefetch(self):
643b2843cf1SBarry Smith    '''Sees if there are any prefetch functions supported'''
6447fca349cSMatthew G. Knepley    if config.setCompilers.Configure.isSolaris(self.log) or self.framework.argDB['with-ios'] or not self.framework.argDB['with-prefetch']:
64593f78423SSatish Balay      self.addDefine('Prefetch(a,b,c)', ' ')
64693f78423SSatish Balay      return
647ec284106SBarry Smith    self.pushLanguage(self.languages.clanguage)
64810699583SJed Brown    if self.checkLink('#include <xmmintrin.h>', 'void *v = 0;_mm_prefetch((const char*)v,_MM_HINT_NTA);\n'):
64950d8bf02SJed Brown      # The Intel Intrinsics manual [1] specifies the prototype
65050d8bf02SJed Brown      #
65150d8bf02SJed Brown      #   void _mm_prefetch(char const *a, int sel);
65250d8bf02SJed Brown      #
65350d8bf02SJed Brown      # but other vendors seem to insist on using subtly different
65450d8bf02SJed Brown      # prototypes, including void* for the pointer, and an enum for
65550d8bf02SJed Brown      # sel.  These are both reasonable changes, but negatively impact
65650d8bf02SJed Brown      # portability.
65750d8bf02SJed Brown      #
65850d8bf02SJed Brown      # [1] http://software.intel.com/file/6373
65950d8bf02SJed Brown      self.addDefine('HAVE_XMMINTRIN_H', 1)
66050d8bf02SJed Brown      self.addDefine('Prefetch(a,b,c)', '_mm_prefetch((const char*)(a),(c))')
66150d8bf02SJed Brown      self.addDefine('PREFETCH_HINT_NTA', '_MM_HINT_NTA')
66250d8bf02SJed Brown      self.addDefine('PREFETCH_HINT_T0',  '_MM_HINT_T0')
66350d8bf02SJed Brown      self.addDefine('PREFETCH_HINT_T1',  '_MM_HINT_T1')
66450d8bf02SJed Brown      self.addDefine('PREFETCH_HINT_T2',  '_MM_HINT_T2')
66550d8bf02SJed Brown    elif self.checkLink('#include <xmmintrin.h>', 'void *v = 0;_mm_prefetch(v,_MM_HINT_NTA);\n'):
66650d8bf02SJed Brown      self.addDefine('HAVE_XMMINTRIN_H', 1)
66750d8bf02SJed Brown      self.addDefine('Prefetch(a,b,c)', '_mm_prefetch((const void*)(a),(c))')
66850d8bf02SJed Brown      self.addDefine('PREFETCH_HINT_NTA', '_MM_HINT_NTA')
66950d8bf02SJed Brown      self.addDefine('PREFETCH_HINT_T0',  '_MM_HINT_T0')
67050d8bf02SJed Brown      self.addDefine('PREFETCH_HINT_T1',  '_MM_HINT_T1')
67150d8bf02SJed Brown      self.addDefine('PREFETCH_HINT_T2',  '_MM_HINT_T2')
67210699583SJed Brown    elif self.checkLink('', 'void *v = 0;__builtin_prefetch(v,0,0);\n'):
67310699583SJed Brown      # From GCC docs: void __builtin_prefetch(const void *addr,int rw,int locality)
67410699583SJed Brown      #
67510699583SJed Brown      #   The value of rw is a compile-time constant one or zero; one
67610699583SJed Brown      #   means that the prefetch is preparing for a write to the memory
67710699583SJed Brown      #   address and zero, the default, means that the prefetch is
67810699583SJed Brown      #   preparing for a read. The value locality must be a compile-time
67910699583SJed Brown      #   constant integer between zero and three. A value of zero means
68010699583SJed Brown      #   that the data has no temporal locality, so it need not be left
68110699583SJed Brown      #   in the cache after the access. A value of three means that the
68210699583SJed Brown      #   data has a high degree of temporal locality and should be left
68310699583SJed Brown      #   in all levels of cache possible. Values of one and two mean,
68410699583SJed Brown      #   respectively, a low or moderate degree of temporal locality.
68510699583SJed Brown      #
68610699583SJed Brown      # Here we adopt Intel's x86/x86-64 naming scheme for the locality
68710699583SJed Brown      # hints.  Using macros for these values in necessary since some
68810699583SJed Brown      # compilers require an enum.
68910699583SJed Brown      self.addDefine('Prefetch(a,b,c)', '__builtin_prefetch((a),(b),(c))')
69010699583SJed Brown      self.addDefine('PREFETCH_HINT_NTA', '0')
69110699583SJed Brown      self.addDefine('PREFETCH_HINT_T0',  '3')
69210699583SJed Brown      self.addDefine('PREFETCH_HINT_T1',  '2')
69310699583SJed Brown      self.addDefine('PREFETCH_HINT_T2',  '1')
694b2843cf1SBarry Smith    else:
695b2843cf1SBarry Smith      self.addDefine('Prefetch(a,b,c)', ' ')
6967d490b44SBarry Smith    self.popLanguage()
697b2843cf1SBarry Smith
69809bc878fSSatish Balay  def configureAtoll(self):
69909bc878fSSatish Balay    '''Checks if atoll exists'''
700436b02dcSSatish Balay    if self.checkLink('#define _POSIX_C_SOURCE 200112L\n#include <stdlib.h>','long v = atoll("25")') or self.checkLink ('#include <stdlib.h>','long v = atoll("25")'):
70109bc878fSSatish Balay       self.addDefine('HAVE_ATOLL', '1')
70209bc878fSSatish Balay
7032400fdedSBarry Smith  def configureUnused(self):
7042400fdedSBarry Smith    '''Sees if __attribute((unused)) is supported'''
7051adaff47SSean Farley    if self.framework.argDB['with-ios']:
7062400fdedSBarry Smith      self.addDefine('UNUSED', ' ')
7072400fdedSBarry Smith      return
7082400fdedSBarry Smith    self.pushLanguage(self.languages.clanguage)
709edf21b64SSatish Balay    if self.checkLink('__attribute((unused)) static int myfunc(__attribute((unused)) void *name){ return 1;}', 'int i = 0;\nint j = myfunc(&i);\ntypedef void* atype;\n__attribute((unused))  atype a;\n'):
7102400fdedSBarry Smith      self.addDefine('UNUSED', '__attribute((unused))')
7112400fdedSBarry Smith    else:
7122400fdedSBarry Smith      self.addDefine('UNUSED', ' ')
7132400fdedSBarry Smith    self.popLanguage()
7142400fdedSBarry Smith
71598ed35c3SBarry Smith  def configureIsatty(self):
71698ed35c3SBarry Smith    '''Check if the Unix C function isatty() works correctly
71798ed35c3SBarry Smith       Actually just assumes it does not work correctly on batch systems'''
71898ed35c3SBarry Smith    if not self.framework.argDB['with-batch']:
71998ed35c3SBarry Smith      self.addDefine('USE_ISATTY',1)
72098ed35c3SBarry Smith
7211ef8df7fSJed Brown  def configureDeprecated(self):
7221ef8df7fSJed Brown    '''Check if __attribute((deprecated)) is supported'''
7231ef8df7fSJed Brown    self.pushLanguage(self.languages.clanguage)
72459a26b54SJed Brown    ## Recent versions of gcc and clang support __attribute((deprecated("string argument"))), which is very useful, but
72559a26b54SJed Brown    ## Intel has conspired to make a supremely environment-sensitive compiler.  The Intel compiler looks at the gcc
72659a26b54SJed Brown    ## executable in the environment to determine the language compatibility that it should attempt to emulate.  Some
72759a26b54SJed Brown    ## important Cray installations have built PETSc using the Intel compiler, but with a newer gcc module loaded (e.g.,
728df3898eeSBarry Smith    ## 4.7).  Thus at PETSc configure time, the Intel compiler decides to support the string argument, but the gcc
72959a26b54SJed Brown    ## found in the default user environment is older and does not support the argument.  If GCC and Intel were cool
73059a26b54SJed Brown    ## like Clang and supported __has_attribute, we could avoid configure tests entirely, but they don't.  And that is
73159a26b54SJed Brown    ## why we can't have nice things.
73259a26b54SJed Brown    #
73359a26b54SJed Brown    # if self.checkCompile("""__attribute((deprecated("Why you shouldn't use myfunc"))) static int myfunc(void) { return 1;}""", ''):
73459a26b54SJed Brown    #   self.addDefine('DEPRECATED(why)', '__attribute((deprecated(why)))')
73559a26b54SJed Brown    if self.checkCompile("""__attribute((deprecated)) static int myfunc(void) { return 1;}""", ''):
73659a26b54SJed Brown      self.addDefine('DEPRECATED(why)', '__attribute((deprecated))')
7371ef8df7fSJed Brown    else:
73847644db9SJed Brown      self.addDefine('DEPRECATED(why)', ' ')
7391ef8df7fSJed Brown    self.popLanguage()
7401ef8df7fSJed Brown
74118f41590SBarry Smith  def configureAlign(self):
74218f41590SBarry Smith    '''Check if __attribute(align) is supported'''
743752d89a4SSatish Balay    filename = 'conftestalign'
744752d89a4SSatish Balay    includes = '''
745752d89a4SSatish Balay#include <sys/types.h>
746752d89a4SSatish Balay#if STDC_HEADERS
747752d89a4SSatish Balay#include <stdlib.h>
748752d89a4SSatish Balay#include <stdio.h>
749752d89a4SSatish Balay#include <stddef.h>
750752d89a4SSatish Balay#endif\n'''
751752d89a4SSatish Balay    body     = '''
752752d89a4SSatish Balaystruct mystruct {int myint;} __attribute((aligned(16)));
753752d89a4SSatish BalayFILE *f = fopen("'''+filename+'''", "w");
754752d89a4SSatish Balayif (!f) exit(1);
755752d89a4SSatish Balayfprintf(f, "%lu\\n", (unsigned long)sizeof(struct mystruct));
756752d89a4SSatish Balay'''
757752d89a4SSatish Balay    if 'known-has-attribute-aligned' in self.argDB:
758752d89a4SSatish Balay      if self.argDB['known-has-attribute-aligned']:
759752d89a4SSatish Balay        size = 16
76018f41590SBarry Smith      else:
761752d89a4SSatish Balay        size = -3
762752d89a4SSatish Balay    elif not self.argDB['with-batch']:
763752d89a4SSatish Balay      self.pushLanguage(self.languages.clanguage)
764752d89a4SSatish Balay      try:
765752d89a4SSatish Balay        if self.checkRun(includes, body) and os.path.exists(filename):
766752d89a4SSatish Balay          f    = file(filename)
767752d89a4SSatish Balay          size = int(f.read())
768752d89a4SSatish Balay          f.close()
769752d89a4SSatish Balay          os.remove(filename)
7700045a809SSatish Balay        else:
7710045a809SSatish Balay          size = -4
772752d89a4SSatish Balay      except:
773752d89a4SSatish Balay        size = -1
774752d89a4SSatish Balay        self.framework.logPrint('Error checking attribute(aligned)')
77518f41590SBarry Smith      self.popLanguage()
776752d89a4SSatish Balay    else:
777752d89a4SSatish Balay      self.framework.addBatchInclude(['#include <stdlib.h>', '#include <stdio.h>', '#include <sys/types.h>','struct mystruct {int myint;} __attribute((aligned(16)));'])
778752d89a4SSatish Balay      self.framework.addBatchBody('fprintf(output, "  \'--known-has-attribute-aligned=%d\',\\n", sizeof(struct mystruct)==16);')
779752d89a4SSatish Balay      size = -2
780752d89a4SSatish Balay    if size == 16:
781752d89a4SSatish Balay      self.addDefine('ATTRIBUTEALIGNED(size)', '__attribute((aligned (size)))')
782752d89a4SSatish Balay      self.addDefine('HAVE_ATTRIBUTEALIGNED', 1)
783752d89a4SSatish Balay    else:
784752d89a4SSatish Balay      self.framework.logPrint('incorrect alignment. Found alignment:'+ str(size))
785752d89a4SSatish Balay      self.addDefine('ATTRIBUTEALIGNED(size)', ' ')
786752d89a4SSatish Balay    return
78718f41590SBarry Smith
7889800092aSJed Brown  def configureExpect(self):
7899800092aSJed Brown    '''Sees if the __builtin_expect directive is supported'''
7909800092aSJed Brown    self.pushLanguage(self.languages.clanguage)
7919800092aSJed Brown    if self.checkLink('', 'if (__builtin_expect(0,1)) return 1;'):
7929800092aSJed Brown      self.addDefine('HAVE_BUILTIN_EXPECT', 1)
7939800092aSJed Brown    self.popLanguage()
7949800092aSJed Brown
79553c77d0aSJed Brown  def configureFunctionName(self):
7961ec50b02SJed Brown    '''Sees if the compiler supports __func__ or a variant.  Falls back
7971ec50b02SJed Brown    on __FUNCT__ which PETSc source defines, but most users do not, thus
7981ec50b02SJed Brown    stack traces through user code are better when the compiler's
7991ec50b02SJed Brown    variant is used.'''
8001ec50b02SJed Brown    def getFunctionName(lang):
8011ec50b02SJed Brown      name = '__FUNCT__'
8021ec50b02SJed Brown      self.pushLanguage(lang)
80353c77d0aSJed Brown      if self.checkLink('', "if (__func__[0] != 'm') return 1;"):
8041ec50b02SJed Brown        name = '__func__'
80553c77d0aSJed Brown      elif self.checkLink('', "if (__FUNCTION__[0] != 'm') return 1;"):
8061ec50b02SJed Brown        name = '__FUNCTION__'
8071ec50b02SJed Brown      self.popLanguage()
8081ec50b02SJed Brown      return name
8091ec50b02SJed Brown    langs = []
810628773c9SSatish Balay
811628773c9SSatish Balay    self.addDefine('FUNCTION_NAME_C', getFunctionName('C'))
812628773c9SSatish Balay    if hasattr(self.compilers, 'CXX'):
813628773c9SSatish Balay      self.addDefine('FUNCTION_NAME_CXX', getFunctionName('Cxx'))
81412607bf0SSatish Balay    else:
81512607bf0SSatish Balay      self.addDefine('FUNCTION_NAME_CXX', '__FUNCT__')
81653c77d0aSJed Brown
817753ebd1dSJed Brown  def configureIntptrt(self):
818753ebd1dSJed Brown    '''Determine what to use for uintptr_t'''
819753ebd1dSJed Brown    def staticAssertSizeMatchesVoidStar(inc,typename):
820753ebd1dSJed Brown      # The declaration is an error if either array size is negative.
821753ebd1dSJed Brown      # It should be okay to use an int that is too large, but it would be very unlikely for this to be the case
822d26187a0SJed Brown      return self.checkCompile(inc, ('#define STATIC_ASSERT(cond) char negative_length_if_false[2*(!!(cond))-1]\n'
823979939cdSSatish Balay                                     + 'STATIC_ASSERT(sizeof(void*) == sizeof(%s));'%typename))
824753ebd1dSJed Brown    self.pushLanguage(self.languages.clanguage)
825753ebd1dSJed Brown    if self.checkCompile('#include <stdint.h>', 'int x; uintptr_t i = (uintptr_t)&x;'):
826753ebd1dSJed Brown      self.addDefine('UINTPTR_T', 'uintptr_t')
827753ebd1dSJed Brown    elif staticAssertSizeMatchesVoidStar('','unsigned long long'):
828753ebd1dSJed Brown      self.addDefine('UINTPTR_T', 'unsigned long long')
829753ebd1dSJed Brown    elif staticAssertSizeMatchesVoidStar('#include <stdlib.h>','size_t') or staticAssertSizeMatchesVoidStar('#include <string.h>', 'size_t'):
830753ebd1dSJed Brown      self.addDefine('UINTPTR_T', 'size_t')
831c82284b1SJed Brown    elif staticAssertSizeMatchesVoidStar('','unsigned long'):
832c82284b1SJed Brown      self.addDefine('UINTPTR_T', 'unsigned long')
8332d1b7972SSatish Balay    elif staticAssertSizeMatchesVoidStar('','unsigned'):
834753ebd1dSJed Brown      self.addDefine('UINTPTR_T', 'unsigned')
835d26187a0SJed Brown    else:
836d26187a0SJed Brown      raise RuntimeError('Could not find any unsigned integer type matching void*')
837753ebd1dSJed Brown    self.popLanguage()
838753ebd1dSJed Brown
839ed938b00SJed Brown  def configureRTLDDefault(self):
840bfef2c86SBarry Smith    if self.checkCompile('#include <dlfcn.h>\n void *ptr =  RTLD_DEFAULT;'):
841bfef2c86SBarry Smith      self.addDefine('RTLD_DEFAULT','1')
842f8833479SBarry Smith    return
843f8833479SBarry Smith
844f8833479SBarry Smith  def configureSolaris(self):
845f8833479SBarry Smith    '''Solaris specific stuff'''
846f8833479SBarry Smith    if os.path.isdir(os.path.join('/usr','ucblib')):
847f8833479SBarry Smith      try:
848f8833479SBarry Smith        flag = getattr(self.setCompilers, self.language[-1]+'SharedLinkerFlag')
849f8833479SBarry Smith      except AttributeError:
850f8833479SBarry Smith        flag = None
851f8833479SBarry Smith      if flag is None:
852f8833479SBarry Smith        self.compilers.LIBS += ' -L/usr/ucblib'
853f8833479SBarry Smith      else:
854f8833479SBarry Smith        self.compilers.LIBS += ' '+flag+'/usr/ucblib'
855f8833479SBarry Smith    return
856f8833479SBarry Smith
857f8833479SBarry Smith  def configureLinux(self):
858f8833479SBarry Smith    '''Linux specific stuff'''
8599f15855cSMatthew G Knepley    # TODO: Test for this by mallocing an odd number of floats and checking the address
860f8833479SBarry Smith    self.addDefine('HAVE_DOUBLE_ALIGN_MALLOC', 1)
861f8833479SBarry Smith    return
862f8833479SBarry Smith
863f8833479SBarry Smith  def configureWin32(self):
864f8833479SBarry Smith    '''Win32 non-cygwin specific stuff'''
865f8833479SBarry Smith    kernel32=0
866f8833479SBarry Smith    if self.libraries.add('Kernel32.lib','GetComputerName',prototype='#include <Windows.h>', call='GetComputerName(NULL,NULL);'):
867f8833479SBarry Smith      self.addDefine('HAVE_WINDOWS_H',1)
868f8833479SBarry Smith      self.addDefine('HAVE_GETCOMPUTERNAME',1)
869f8833479SBarry Smith      kernel32=1
870f8833479SBarry Smith    elif self.libraries.add('kernel32','GetComputerName',prototype='#include <Windows.h>', call='GetComputerName(NULL,NULL);'):
871f8833479SBarry Smith      self.addDefine('HAVE_WINDOWS_H',1)
872f8833479SBarry Smith      self.addDefine('HAVE_GETCOMPUTERNAME',1)
873f8833479SBarry Smith      kernel32=1
874f8833479SBarry Smith    if kernel32:
875eed94e11SSatish Balay      if self.framework.argDB['with-windows-graphics']:
876eed94e11SSatish Balay        self.addDefine('USE_WINDOWS_GRAPHICS',1)
877f8833479SBarry Smith      if self.checkLink('#include <Windows.h>','LoadLibrary(0)'):
878f8833479SBarry Smith        self.addDefine('HAVE_LOADLIBRARY',1)
879b50f6d9eSLisandro Dalcin      if self.checkLink('#include <Windows.h>','GetProcAddress(0,0)'):
880b50f6d9eSLisandro Dalcin        self.addDefine('HAVE_GETPROCADDRESS',1)
881b50f6d9eSLisandro Dalcin      if self.checkLink('#include <Windows.h>','FreeLibrary(0)'):
882b50f6d9eSLisandro Dalcin        self.addDefine('HAVE_FREELIBRARY',1)
883a21658a3SLisandro Dalcin      if self.checkLink('#include <Windows.h>','GetLastError()'):
884a21658a3SLisandro Dalcin        self.addDefine('HAVE_GETLASTERROR',1)
885a21658a3SLisandro Dalcin      if self.checkLink('#include <Windows.h>','SetLastError(0)'):
886a21658a3SLisandro Dalcin        self.addDefine('HAVE_SETLASTERROR',1)
887f8833479SBarry Smith      if self.checkLink('#include <Windows.h>\n','QueryPerformanceCounter(0);\n'):
888bea725cfSBarry Smith        self.addDefine('USE_MICROSOFT_TIME',1)
889f8833479SBarry Smith    if self.libraries.add('Advapi32.lib','GetUserName',prototype='#include <Windows.h>', call='GetUserName(NULL,NULL);'):
890f8833479SBarry Smith      self.addDefine('HAVE_GET_USER_NAME',1)
891f8833479SBarry Smith    elif self.libraries.add('advapi32','GetUserName',prototype='#include <Windows.h>', call='GetUserName(NULL,NULL);'):
892f8833479SBarry Smith      self.addDefine('HAVE_GET_USER_NAME',1)
893f8833479SBarry Smith
894f8833479SBarry Smith    if not self.libraries.add('User32.lib','GetDC',prototype='#include <Windows.h>',call='GetDC(0);'):
895f8833479SBarry Smith      self.libraries.add('user32','GetDC',prototype='#include <Windows.h>',call='GetDC(0);')
896f8833479SBarry Smith    if not self.libraries.add('Gdi32.lib','CreateCompatibleDC',prototype='#include <Windows.h>',call='CreateCompatibleDC(0);'):
897f8833479SBarry Smith      self.libraries.add('gdi32','CreateCompatibleDC',prototype='#include <Windows.h>',call='CreateCompatibleDC(0);')
898f8833479SBarry Smith
899f8833479SBarry Smith    self.types.check('int32_t', 'int')
900f8833479SBarry Smith    if not self.checkCompile('#include <sys/types.h>\n','uid_t u;\n'):
901f8833479SBarry Smith      self.addTypedef('int', 'uid_t')
902f8833479SBarry Smith      self.addTypedef('int', 'gid_t')
903f8833479SBarry Smith    if not self.checkLink('#if defined(PETSC_HAVE_UNISTD_H)\n#include <unistd.h>\n#endif\n','int a=R_OK;\n'):
904f8833479SBarry Smith      self.framework.addDefine('R_OK', '04')
905f8833479SBarry Smith      self.framework.addDefine('W_OK', '02')
906f8833479SBarry Smith      self.framework.addDefine('X_OK', '01')
907f8833479SBarry Smith    if not self.checkLink('#include <sys/stat.h>\n','int a=0;\nif (S_ISDIR(a)){}\n'):
908f8833479SBarry Smith      self.framework.addDefine('S_ISREG(a)', '(((a)&_S_IFMT) == _S_IFREG)')
909f8833479SBarry Smith      self.framework.addDefine('S_ISDIR(a)', '(((a)&_S_IFMT) == _S_IFDIR)')
910f8833479SBarry Smith    if self.checkCompile('#include <Windows.h>\n','LARGE_INTEGER a;\nDWORD b=a.u.HighPart;\n'):
911f8833479SBarry Smith      self.addDefine('HAVE_LARGE_INTEGER_U',1)
912f8833479SBarry Smith
913f8833479SBarry Smith    # Windows requires a Binary file creation flag when creating/opening binary files.  Is a better test in order?
914ef2cfba3SSatish Balay    if self.checkCompile('#include <Windows.h>\n#include <fcntl.h>\n', 'int flags = O_BINARY;'):
915f8833479SBarry Smith      self.addDefine('HAVE_O_BINARY',1)
916f8833479SBarry Smith
917f8833479SBarry Smith    if self.compilers.CC.find('win32fe') >= 0:
918f8833479SBarry Smith      self.addDefine('PATH_SEPARATOR','\';\'')
919f8833479SBarry Smith      self.addDefine('DIR_SEPARATOR','\'\\\\\'')
920f8833479SBarry Smith      self.addDefine('REPLACE_DIR_SEPARATOR','\'/\'')
921f8833479SBarry Smith      self.addDefine('CANNOT_START_DEBUGGER',1)
9227908f030SMatthew G. Knepley      (petscdir,error,status) = self.executeShellCommand('cygpath -w '+self.petscdir.dir, log = self.log)
92334531a4dSSatish Balay      self.addDefine('DIR','"'+petscdir.replace('\\','\\\\')+'"')
924f8833479SBarry Smith    else:
925f8833479SBarry Smith      self.addDefine('PATH_SEPARATOR','\':\'')
926f8833479SBarry Smith      self.addDefine('REPLACE_DIR_SEPARATOR','\'\\\\\'')
927f8833479SBarry Smith      self.addDefine('DIR_SEPARATOR','\'/\'')
92834531a4dSSatish Balay      self.addDefine('DIR', '"'+self.petscdir.dir+'"')
929bfef2c86SBarry Smith
930f8833479SBarry Smith    return
931f8833479SBarry Smith
932f8833479SBarry Smith#-----------------------------------------------------------------------------------------------------
933b10d012aSSatish Balay  def configureCygwinBrokenPipe(self):
934b10d012aSSatish Balay    '''Cygwin version <= 1.7.18 had issues with pipes and long commands invoked from gnu-make
935b10d012aSSatish Balay    http://cygwin.com/ml/cygwin/2013-05/msg00340.html '''
9367fca349cSMatthew G. Knepley    if config.setCompilers.Configure.isCygwin(self.log):
937b10d012aSSatish Balay      import platform
938b10d012aSSatish Balay      import re
939b10d012aSSatish Balay      r=re.compile("([0-9]+).([0-9]+).([0-9]+)")
940b10d012aSSatish Balay      m=r.match(platform.release())
941b10d012aSSatish Balay      major=int(m.group(1))
942b10d012aSSatish Balay      minor=int(m.group(2))
943b10d012aSSatish Balay      subminor=int(m.group(3))
944b10d012aSSatish Balay      if ((major < 1) or (major == 1 and minor < 7) or (major == 1 and minor == 7 and subminor <= 18)):
945b10d012aSSatish Balay        self.addMakeMacro('PETSC_CYGWIN_BROKEN_PIPE','1')
946b10d012aSSatish Balay    return
947b10d012aSSatish Balay
948b10d012aSSatish Balay#-----------------------------------------------------------------------------------------------------
949569865ddSSatish Balay  def configureDefaultArch(self):
950af0996ceSBarry Smith    conffile = os.path.join('lib','petsc','conf', 'petscvariables')
951569865ddSSatish Balay    if self.framework.argDB['with-default-arch']:
952569865ddSSatish Balay      fd = file(conffile, 'w')
953569865ddSSatish Balay      fd.write('PETSC_ARCH='+self.arch.arch+'\n')
954da93591fSBarry Smith      fd.write('PETSC_DIR='+self.petscdir.dir+'\n')
955af0996ceSBarry Smith      fd.write('include '+os.path.join(self.petscdir.dir,self.arch.arch,'lib','petsc','conf','petscvariables')+'\n')
956569865ddSSatish Balay      fd.close()
957569865ddSSatish Balay      self.framework.actions.addArgument('PETSc', 'Build', 'Set default architecture to '+self.arch.arch+' in '+conffile)
958569865ddSSatish Balay    elif os.path.isfile(conffile):
959569865ddSSatish Balay      try:
960569865ddSSatish Balay        os.unlink(conffile)
961569865ddSSatish Balay      except:
962569865ddSSatish Balay        raise RuntimeError('Unable to remove file '+conffile+'. Did a different user create it?')
963569865ddSSatish Balay    return
964569865ddSSatish Balay
965569865ddSSatish Balay#-----------------------------------------------------------------------------------------------------
966f8833479SBarry Smith  def configureScript(self):
967f8833479SBarry Smith    '''Output a script in the conf directory which will reproduce the configuration'''
968f8833479SBarry Smith    import nargs
969495bf1a9SSatish Balay    import sys
970af0996ceSBarry Smith    scriptName = os.path.join(self.arch.arch,'lib','petsc','conf', 'reconfigure-'+self.arch.arch+'.py')
971f8833479SBarry Smith    args = dict([(nargs.Arg.parseArgument(arg)[0], arg) for arg in self.framework.clArgs])
972e97fc2efSSatish Balay    if 'with-clean' in args:
973e97fc2efSSatish Balay      del args['with-clean']
974f8833479SBarry Smith    if 'configModules' in args:
9751063a081SSatish Balay      if nargs.Arg.parseArgument(args['configModules'])[1] == 'PETSc.Configure':
976f8833479SBarry Smith        del args['configModules']
977f8833479SBarry Smith    if 'optionsModule' in args:
97823a19ef1SSatish Balay      if nargs.Arg.parseArgument(args['optionsModule'])[1] == 'config.compilerOptions':
979f8833479SBarry Smith        del args['optionsModule']
980f8833479SBarry Smith    if not 'PETSC_ARCH' in args:
9811063a081SSatish Balay      args['PETSC_ARCH'] = 'PETSC_ARCH='+str(self.arch.arch)
982f8833479SBarry Smith    f = file(scriptName, 'w')
983495bf1a9SSatish Balay    f.write('#!'+sys.executable+'\n')
984f8833479SBarry Smith    f.write('if __name__ == \'__main__\':\n')
985f8833479SBarry Smith    f.write('  import sys\n')
9867561c02cSSatish Balay    f.write('  import os\n')
9877561c02cSSatish Balay    f.write('  sys.path.insert(0, os.path.abspath(\'config\'))\n')
988f8833479SBarry Smith    f.write('  import configure\n')
9891063a081SSatish Balay    # pretty print repr(args.values())
9901063a081SSatish Balay    f.write('  configure_options = [\n')
9918bec23c5SJed Brown    for itm in sorted(args.values()):
9921063a081SSatish Balay      f.write('    \''+str(itm)+'\',\n')
9931063a081SSatish Balay    f.write('  ]\n')
994f8833479SBarry Smith    f.write('  configure.petsc_configure(configure_options)\n')
995f8833479SBarry Smith    f.close()
996f8833479SBarry Smith    try:
997f8833479SBarry Smith      os.chmod(scriptName, 0775)
998f8833479SBarry Smith    except OSError, e:
999f8833479SBarry Smith      self.framework.logPrint('Unable to make reconfigure script executable:\n'+str(e))
1000f8833479SBarry Smith    self.framework.actions.addArgument('PETSc', 'File creation', 'Created '+scriptName+' for automatic reconfiguration')
1001f8833479SBarry Smith    return
1002f8833479SBarry Smith
1003f8833479SBarry Smith  def configureInstall(self):
1004f8833479SBarry Smith    '''Setup the directories for installation'''
1005f8833479SBarry Smith    if self.framework.argDB['prefix']:
1006824e893fSSatish Balay      self.addMakeRule('shared_install','',['-@echo "Now to install the libraries do:"',\
1007d093bd8dSBarry Smith                                              '-@echo "'+self.installdir.installSudo+'make PETSC_DIR=${PETSC_DIR} PETSC_ARCH=${PETSC_ARCH} install"',\
1008315b77e6SSatish Balay                                              '-@echo "========================================="'])
1009f8833479SBarry Smith    else:
1010824e893fSSatish Balay      self.addMakeRule('shared_install','',['-@echo "Now to check if the libraries are working do:"',\
1011824e893fSSatish Balay                                              '-@echo "make PETSC_DIR=${PETSC_DIR} PETSC_ARCH=${PETSC_ARCH} test"',\
1012315b77e6SSatish Balay                                              '-@echo "========================================="'])
1013f8833479SBarry Smith      return
1014f8833479SBarry Smith
1015f8833479SBarry Smith  def configureGCOV(self):
1016f8833479SBarry Smith    if self.framework.argDB['with-gcov']:
1017f8833479SBarry Smith      self.addDefine('USE_GCOV','1')
1018f8833479SBarry Smith    return
1019f8833479SBarry Smith
1020f8833479SBarry Smith  def configureFortranFlush(self):
1021f8833479SBarry Smith    if hasattr(self.compilers, 'FC'):
1022f8833479SBarry Smith      for baseName in ['flush','flush_']:
1023f8833479SBarry Smith        if self.libraries.check('', baseName, otherLibs = self.compilers.flibs, fortranMangle = 1):
1024f8833479SBarry Smith          self.addDefine('HAVE_'+baseName.upper(), 1)
1025f8833479SBarry Smith          return
1026f8833479SBarry Smith
102727b0f280SBarry Smith  def configureViewFromOptions(self):
102827b0f280SBarry Smith    if not self.framework.argDB['with-viewfromoptions']:
102927b0f280SBarry Smith      self.addDefine('SKIP_VIEWFROMOPTIONS',1)
103027b0f280SBarry Smith
103128bb2e72SSatish Balay  def postProcessPackages(self):
103228bb2e72SSatish Balay    postPackages=[]
103328bb2e72SSatish Balay    for i in self.framework.packages:
103428bb2e72SSatish Balay      if hasattr(i,'postProcess'): postPackages.append(i)
103528bb2e72SSatish Balay    if postPackages:
1036e64d19dfSSatish Balay      # ctetgen needs petsc conf files. so attempt to create them early
1037a77eb93bSSatish Balay      self.framework.dumpConfFiles()
1038d9293e7bSBarry Smith      # tacky fix for dependency of Aluimia on Pflotran; requested via petsc-dev Matt provide a correct fix
1039d9293e7bSBarry Smith      for i in postPackages:
1040d9293e7bSBarry Smith        if i.name.upper() in ['PFLOTRAN']:
1041d9293e7bSBarry Smith          i.postProcess()
1042d9293e7bSBarry Smith          postPackages.remove(i)
104328bb2e72SSatish Balay      for i in postPackages: i.postProcess()
1044*aa5c8b8eSBarry Smith      for i in postPackages:
1045*aa5c8b8eSBarry Smith        if i.installedpetsc:
1046*aa5c8b8eSBarry Smith          self.installed = 1
1047*aa5c8b8eSBarry Smith          break
104828bb2e72SSatish Balay    return
1049f8833479SBarry Smith
1050f8833479SBarry Smith  def configure(self):
1051f8833479SBarry Smith    if not os.path.samefile(self.petscdir.dir, os.getcwd()):
1052f8833479SBarry Smith      raise RuntimeError('Wrong PETSC_DIR option specified: '+str(self.petscdir.dir) + '\n  Configure invoked in: '+os.path.realpath(os.getcwd()))
1053550489e3SMatthew G Knepley    if self.framework.argDB['prefix'] and os.path.isdir(self.framework.argDB['prefix']) and os.path.samefile(self.framework.argDB['prefix'],self.petscdir.dir):
10543552d8fbSSatish Balay      raise RuntimeError('Incorrect option --prefix='+self.framework.argDB['prefix']+' specified. It cannot be same as PETSC_DIR!')
1055c16c35a9SSatish Balay    if self.framework.argDB['prefix'] and os.path.isdir(self.framework.argDB['prefix']) and os.path.samefile(self.framework.argDB['prefix'],os.path.join(self.petscdir.dir,self.arch.arch)):
1056c16c35a9SSatish Balay      raise RuntimeError('Incorrect option --prefix='+self.framework.argDB['prefix']+' specified. It cannot be same as PETSC_DIR/PETSC_ARCH!')
1057f16c1317SJed Brown    self.framework.header          = os.path.join(self.arch.arch,'include','petscconf.h')
1058f16c1317SJed Brown    self.framework.cHeader         = os.path.join(self.arch.arch,'include','petscfix.h')
1059af0996ceSBarry Smith    self.framework.makeMacroHeader = os.path.join(self.arch.arch,'lib','petsc','conf','petscvariables')
1060af0996ceSBarry Smith    self.framework.makeRuleHeader  = os.path.join(self.arch.arch,'lib','petsc','conf','petscrules')
1061f8833479SBarry Smith    if self.libraries.math is None:
1062f8833479SBarry Smith      raise RuntimeError('PETSc requires a functional math library. Please send configure.log to petsc-maint@mcs.anl.gov.')
1063f8833479SBarry Smith    if self.languages.clanguage == 'Cxx' and not hasattr(self.compilers, 'CXX'):
1064f8833479SBarry Smith      raise RuntimeError('Cannot set C language to C++ without a functional C++ compiler.')
1065ed938b00SJed Brown    self.executeTest(self.configureRTLDDefault)
1066b2843cf1SBarry Smith    self.executeTest(self.configurePrefetch)
10672400fdedSBarry Smith    self.executeTest(self.configureUnused)
10681ef8df7fSJed Brown    self.executeTest(self.configureDeprecated)
106998ed35c3SBarry Smith    self.executeTest(self.configureIsatty)
10709800092aSJed Brown    self.executeTest(self.configureExpect);
107118f41590SBarry Smith    self.executeTest(self.configureAlign);
107253c77d0aSJed Brown    self.executeTest(self.configureFunctionName);
1073753ebd1dSJed Brown    self.executeTest(self.configureIntptrt);
1074f8833479SBarry Smith    self.executeTest(self.configureSolaris)
1075f8833479SBarry Smith    self.executeTest(self.configureLinux)
1076f8833479SBarry Smith    self.executeTest(self.configureWin32)
1077b10d012aSSatish Balay    self.executeTest(self.configureCygwinBrokenPipe)
1078569865ddSSatish Balay    self.executeTest(self.configureDefaultArch)
1079f8833479SBarry Smith    self.executeTest(self.configureScript)
1080f8833479SBarry Smith    self.executeTest(self.configureInstall)
1081f8833479SBarry Smith    self.executeTest(self.configureGCOV)
1082f8833479SBarry Smith    self.executeTest(self.configureFortranFlush)
108309bc878fSSatish Balay    self.executeTest(self.configureAtoll)
108427b0f280SBarry Smith    self.executeTest(self.configureViewFromOptions)
1085f8833479SBarry Smith    # dummy rules, always needed except for remote builds
1086f8833479SBarry Smith    self.addMakeRule('remote','')
1087f8833479SBarry Smith    self.addMakeRule('remoteclean','')
1088f8833479SBarry Smith
1089f8833479SBarry Smith    self.Dump()
1090f8833479SBarry Smith    self.dumpConfigInfo()
10912a4161d9SMatthew G Knepley    self.dumpMachineInfo()
1092511a6afcSJed Brown    self.dumpCMakeConfig()
10938b0282a9SJed Brown    self.dumpCMakeLists()
109440277576SBarry Smith    # need to save the current state of BuildSystem so that postProcess() packages can read it in and perhaps run make install
109540277576SBarry Smith    self.framework.storeSubstitutions(self.framework.argDB)
109640277576SBarry Smith    self.framework.argDB['configureCache'] = cPickle.dumps(self.framework)
109740277576SBarry Smith    self.framework.argDB.save(force = True)
10988b0282a9SJed Brown    self.cmakeBoot()
1099262119f8SBarry Smith    self.DumpPkgconfig()
1100351d3a41SMatthew G Knepley    self.DumpModule()
1101f7ad81e1SBarry Smith    self.postProcessPackages()
1102f8833479SBarry Smith    self.framework.log.write('================================================================================\n')
1103f8833479SBarry Smith    self.logClear()
1104f8833479SBarry Smith    return
1105