xref: /petsc/config/PETSc/Configure.py (revision 402775768c0f572d9ad12ed4af3294714359e42b)
1import config.base
2
3import os
4import re
5import cPickle
6
7# The sorted() builtin is not available with python-2.3
8try: sorted
9except NameError:
10  def sorted(lst):
11    lst.sort()
12    return lst
13
14class Configure(config.base.Configure):
15  def __init__(self, framework):
16    config.base.Configure.__init__(self, framework)
17    self.headerPrefix = 'PETSC'
18    self.substPrefix  = 'PETSC'
19    return
20
21  def __str2__(self):
22    desc = []
23    desc.append('xxx=========================================================================xxx')
24    if self.make.getMakeMacro('MAKE_IS_GNUMAKE'):
25      build_type = 'gnumake build'
26    elif self.getMakeMacro('PETSC_BUILD_USING_CMAKE'):
27      build_type = 'cmake build'
28    else:
29      build_type = 'legacy build'
30    desc.append(' Configure stage complete. Now build PETSc libraries with (%s):' % build_type)
31    desc.append('   make PETSC_DIR='+self.petscdir.dir+' PETSC_ARCH='+self.arch.arch+' all')
32    desc.append('xxx=========================================================================xxx')
33    return '\n'.join(desc)+'\n'
34
35  def setupHelp(self, help):
36    import nargs
37    help.addArgument('PETSc',  '-prefix=<dir>',                   nargs.Arg(None, '', 'Specifiy location to install PETSc (eg. /usr/local)'))
38    help.addArgument('PETSc',  '-with-prefetch=<bool>',           nargs.ArgBool(None, 1,'Enable checking for prefetch instructions'))
39    help.addArgument('Windows','-with-windows-graphics=<bool>',   nargs.ArgBool(None, 1,'Enable check for Windows Graphics'))
40    help.addArgument('PETSc', '-with-default-arch=<bool>',        nargs.ArgBool(None, 1, 'Allow using the last configured arch without setting PETSC_ARCH'))
41    help.addArgument('PETSc','-with-single-library=<bool>',       nargs.ArgBool(None, 1,'Put all PETSc code into the single -lpetsc library'))
42    help.addArgument('PETSc', '-with-ios=<bool>',              nargs.ArgBool(None, 0, 'Build an iPhone/iPad version of PETSc library'))
43    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'))
44    help.addArgument('PETSc', '-known-has-attribute-aligned=<bool>',nargs.ArgBool(None, None, 'Indicates __attribute((aligned(16)) directive works (the usual test will be skipped)'))
45
46    return
47
48  def setupDependencies(self, framework):
49    config.base.Configure.setupDependencies(self, framework)
50    self.programs      = framework.require('config.programs',           self)
51    self.setCompilers  = framework.require('config.setCompilers',       self)
52    self.compilers     = framework.require('config.compilers',          self)
53    self.arch          = framework.require('PETSc.options.arch',        self.setCompilers)
54    self.petscdir      = framework.require('PETSc.options.petscdir',    self.arch)
55    self.installdir    = framework.require('PETSc.options.installDir',  self)
56    self.languages     = framework.require('PETSc.options.languages',   self.setCompilers)
57    self.debugging     = framework.require('PETSc.options.debugging',   self.compilers)
58    self.indexTypes    = framework.require('PETSc.options.indexTypes',  self.compilers)
59    self.compilers     = framework.require('config.compilers',          self)
60    self.types         = framework.require('config.types',              self)
61    self.headers       = framework.require('config.headers',            self)
62    self.functions     = framework.require('config.functions',          self)
63    self.libraries     = framework.require('config.libraries',          self)
64    self.atomics       = framework.require('config.atomics',            self)
65    self.make          = framework.require('config.packages.make',      self)
66    self.blasLapack    = framework.require('config.packages.BlasLapack',self)
67    self.cmake         = framework.require('config.packages.cmake',self)
68    self.externalpackagesdir = framework.require('PETSc.options.externalpackagesdir',self)
69    self.mpi           = framework.require('config.packages.MPI',self)
70
71    for utility in os.listdir(os.path.join('config','PETSc','options')):
72      (utilityName, ext) = os.path.splitext(utility)
73      if not utilityName.startswith('.') and not utilityName.startswith('#') and ext == '.py' and not utilityName == '__init__':
74        utilityObj                    = self.framework.require('PETSc.options.'+utilityName, self)
75        utilityObj.headerPrefix       = self.headerPrefix
76        utilityObj.archProvider       = self.arch
77        utilityObj.languageProvider   = self.languages
78        utilityObj.installDirProvider = self.installdir
79        utilityObj.externalPackagesDirProvider = self.externalpackagesdir
80        setattr(self, utilityName.lower(), utilityObj)
81
82    for utility in os.listdir(os.path.join('config','BuildSystem','config','utilities')):
83      (utilityName, ext) = os.path.splitext(utility)
84      if not utilityName.startswith('.') and not utilityName.startswith('#') and ext == '.py' and not utilityName == '__init__':
85        utilityObj                    = self.framework.require('config.utilities.'+utilityName, self)
86        utilityObj.headerPrefix       = self.headerPrefix
87        utilityObj.archProvider       = self.arch
88        utilityObj.languageProvider   = self.languages
89        utilityObj.installDirProvider = self.installdir
90        utilityObj.externalPackagesDirProvider = self.externalpackagesdir
91        setattr(self, utilityName.lower(), utilityObj)
92
93    if os.path.isdir(os.path.join('config', 'BuildSystem', 'config', 'packages')):
94      for package in os.listdir(os.path.join('config', 'BuildSystem', 'config', 'packages')):
95        (packageName, ext) = os.path.splitext(package)
96        if not packageName.startswith('.') and not packageName.startswith('#') and ext == '.py' and not packageName == '__init__' and not packageName == 'PETSc':
97          packageObj                    = framework.require('config.packages.'+packageName, self)
98          packageObj.headerPrefix       = self.headerPrefix
99          packageObj.archProvider       = self.arch
100          packageObj.languageProvider   = self.languages
101          packageObj.precisionProvider  = self.scalartypes
102          packageObj.indexProvider      = self.indexTypes
103          packageObj.installDirProvider = self.installdir
104          packageObj.externalPackagesDirProvider = self.externalpackagesdir
105          setattr(self, packageName.lower(), packageObj)
106    # Force blaslapack and opencl to depend on scalarType so precision is set before BlasLapack is built
107    framework.require('PETSc.options.scalarTypes', self.f2cblaslapack)
108    framework.require('PETSc.options.scalarTypes', self.fblaslapack)
109    framework.require('PETSc.options.scalarTypes', self.blaslapack)
110    framework.require('PETSc.options.scalarTypes', self.opencl)
111    framework.require('PETSc.Regression', self)
112
113    self.programs.headerPrefix   = self.headerPrefix
114    self.compilers.headerPrefix  = self.headerPrefix
115    self.types.headerPrefix      = self.headerPrefix
116    self.headers.headerPrefix    = self.headerPrefix
117    self.functions.headerPrefix  = self.headerPrefix
118    self.libraries.headerPrefix  = self.headerPrefix
119    self.blaslapack.headerPrefix = self.headerPrefix
120    self.mpi.headerPrefix        = self.headerPrefix
121    headersC = map(lambda name: name+'.h', ['setjmp','dos', 'endian', 'fcntl', 'float', 'io', 'limits', 'malloc', 'pwd', 'search', 'strings',
122                                            'unistd', 'sys/sysinfo', 'machine/endian', 'sys/param', 'sys/procfs', 'sys/resource',
123                                            'sys/systeminfo', 'sys/times', 'sys/utsname','string', 'stdlib',
124                                            'sys/socket','sys/wait','netinet/in','netdb','Direct','time','Ws2tcpip','sys/types',
125                                            'WindowsX', 'cxxabi','float','ieeefp','stdint','sched','pthread','mathimf'])
126    functions = ['access', '_access', 'clock', 'drand48', 'getcwd', '_getcwd', 'getdomainname', 'gethostname',
127                 'gettimeofday', 'getwd', 'memalign', 'memmove', 'mkstemp', 'popen', 'PXFGETARG', 'rand', 'getpagesize',
128                 'readlink', 'realpath',  'sigaction', 'signal', 'sigset', 'usleep', 'sleep', '_sleep', 'socket',
129                 'times', 'gethostbyname', 'uname','snprintf','_snprintf','lseek','_lseek','time','fork','stricmp',
130                 'strcasecmp', 'bzero', 'dlopen', 'dlsym', 'dlclose', 'dlerror','get_nprocs','sysctlbyname',
131                 '_set_output_format','_mkdir']
132    libraries1 = [(['socket', 'nsl'], 'socket'), (['fpe'], 'handle_sigfpes')]
133    self.headers.headers.extend(headersC)
134    self.functions.functions.extend(functions)
135    self.libraries.libraries.extend(libraries1)
136
137    return
138
139  def DumpPkgconfig(self):
140    ''' Create a pkg-config file '''
141    if not os.path.exists(os.path.join(self.petscdir.dir,self.arch.arch,'lib','pkgconfig')):
142      os.makedirs(os.path.join(self.petscdir.dir,self.arch.arch,'lib','pkgconfig'))
143    fd = open(os.path.join(self.petscdir.dir,self.arch.arch,'lib','pkgconfig','PETSc.pc'),'w')
144    if self.framework.argDB['prefix']:
145      fd.write('prefix='+self.installdir.dir+'\n')
146      fd.write('exec_prefix=${prefix}\n')
147      fd.write('includedir=${prefix}/include\n')
148    else:
149      fd.write('prefix='+self.petscdir.dir+'\n')
150      fd.write('exec_prefix=${prefix}\n')
151      fd.write('includedir=${prefix}/include\n')
152    fd.write('libdir='+os.path.join(self.installdir.dir,'lib')+'\n')
153
154    self.setCompilers.pushLanguage('C')
155    fd.write('ccompiler='+self.setCompilers.getCompiler()+'\n')
156    self.setCompilers.popLanguage()
157    if hasattr(self.compilers, 'C++'):
158      self.setCompilers.pushLanguage('C++')
159      fd.write('cxxcompiler='+self.setCompilers.getCompiler()+'\n')
160      self.setCompilers.popLanguage()
161    if hasattr(self.compilers, 'FC'):
162      self.setCompilers.pushLanguage('FC')
163      fd.write('fcompiler='+self.setCompilers.getCompiler()+'\n')
164      self.setCompilers.popLanguage()
165    fd.write('blaslapacklibs='+self.libraries.toStringNoDupes(self.blaslapack.lib)+'\n')
166
167    fd.write('\n')
168    fd.write('Name: PETSc\n')
169    fd.write('Description: Library to solve ODEs and algebraic equations\n')
170    fd.write('Version: %s\n' % self.petscdir.version)
171
172    fd.write('Cflags: '+self.allincludes+'\n')
173
174    plibs = self.libraries.toStringNoDupes(['-L'+os.path.join(self.petscdir.dir,self.arch.arch,'lib'),' -lpetsc'])
175    if self.framework.argDB['prefix']:
176      fd.write('Libs: '+plibs.replace(os.path.join(self.petscdir.dir,self.arch.arch),self.framework.argDB['prefix'])+'\n')
177    else:
178      fd.write('Libs: '+plibs+'\n')
179    fd.write('Libs.private: '+self.libraries.toStringNoDupes(self.packagelibs+self.libraries.math+self.compilers.flibs+self.compilers.cxxlibs)+' '+self.compilers.LIBS+'\n')
180
181    fd.close()
182    return
183
184  def DumpModule(self):
185    ''' Create a module file '''
186    if not os.path.exists(os.path.join(self.petscdir.dir,self.arch.arch,'lib','petsc','conf','modules')):
187      os.makedirs(os.path.join(self.petscdir.dir,self.arch.arch,'lib','petsc','conf','modules'))
188    if not os.path.exists(os.path.join(self.petscdir.dir,self.arch.arch,'lib','petsc','conf','modules','petsc')):
189      os.makedirs(os.path.join(self.petscdir.dir,self.arch.arch,'lib','petsc','conf','modules','petsc'))
190    if self.framework.argDB['prefix']:
191      installdir  = self.installdir.dir
192      installarch = ''
193      installpath = os.path.join(installdir,'bin')
194    else:
195      installdir  = self.petscdir.dir
196      installarch = self.arch.arch
197      installpath = os.path.join(installdir,installarch,'bin')+':'+os.path.join(installdir,'bin')
198    fd = open(os.path.join(self.petscdir.dir,self.arch.arch,'lib','petsc','conf','modules','petsc',self.petscdir.version),'w')
199    fd.write('''\
200#%%Module
201
202proc ModulesHelp { } {
203    puts stderr "This module sets the path and environment variables for petsc-%s"
204    puts stderr "     see http://www.mcs.anl.gov/petsc/ for more information      "
205    puts stderr ""
206}
207module-whatis "PETSc - Portable, Extensible Toolkit for Scientific Computation"
208
209set petsc_dir   %s
210set petsc_arch  %s
211
212setenv PETSC_ARCH $petsc_arch
213setenv PETSC_DIR $petsc_dir
214prepend-path PATH %s
215''' % (self.petscdir.version, installdir, installarch, installpath))
216    fd.close()
217    return
218
219  def Dump(self):
220    ''' Actually put the values into the configuration files '''
221    # eventually everything between -- should be gone
222    if self.mpi.usingMPIUni:
223      #
224      # Remove any MPI/MPICH include files that may have been put here by previous runs of ./configure
225      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)
226
227#-----------------------------------------------------------------------------------------------------
228
229    # Sometimes we need C compiler, even if built with C++
230    self.setCompilers.pushLanguage('C')
231    self.addMakeMacro('CC_FLAGS',self.setCompilers.getCompilerFlags())
232    self.setCompilers.popLanguage()
233
234    # And sometimes we need a C++ compiler even when PETSc is built with C
235    if hasattr(self.compilers, 'CXX'):
236      self.setCompilers.pushLanguage('Cxx')
237      self.addMakeMacro('CXX_FLAGS',self.setCompilers.getCompilerFlags())
238      self.setCompilers.popLanguage()
239
240    # C preprocessor values
241    self.addMakeMacro('CPP_FLAGS',self.setCompilers.CPPFLAGS)
242
243    # compiler values
244    self.setCompilers.pushLanguage(self.languages.clanguage)
245    self.addMakeMacro('PCC',self.setCompilers.getCompiler())
246    self.addMakeMacro('PCC_FLAGS',self.setCompilers.getCompilerFlags())
247    self.setCompilers.popLanguage()
248    # .o or .obj
249    self.addMakeMacro('CC_SUFFIX','o')
250
251    # executable linker values
252    self.setCompilers.pushLanguage(self.languages.clanguage)
253    pcc_linker = self.setCompilers.getLinker()
254    self.addMakeMacro('PCC_LINKER',pcc_linker)
255    self.addMakeMacro('PCC_LINKER_FLAGS',self.setCompilers.getLinkerFlags())
256    self.setCompilers.popLanguage()
257    # '' for Unix, .exe for Windows
258    self.addMakeMacro('CC_LINKER_SUFFIX','')
259
260    if hasattr(self.compilers, 'FC'):
261      self.setCompilers.pushLanguage('FC')
262      # need FPPFLAGS in config/setCompilers
263      self.addDefine('HAVE_FORTRAN','1')
264      self.addMakeMacro('FPP_FLAGS',self.setCompilers.CPPFLAGS)
265
266      # compiler values
267      self.addMakeMacro('FC_FLAGS',self.setCompilers.getCompilerFlags())
268      self.setCompilers.popLanguage()
269      # .o or .obj
270      self.addMakeMacro('FC_SUFFIX','o')
271
272      # executable linker values
273      self.setCompilers.pushLanguage('FC')
274      # Cannot have NAG f90 as the linker - so use pcc_linker as fc_linker
275      fc_linker = self.setCompilers.getLinker()
276      if config.setCompilers.Configure.isNAG(fc_linker, self.log):
277        self.addMakeMacro('FC_LINKER',pcc_linker)
278      else:
279        self.addMakeMacro('FC_LINKER',fc_linker)
280      self.addMakeMacro('FC_LINKER_FLAGS',self.setCompilers.getLinkerFlags())
281      # apple requires this shared library linker flag on SOME versions of the os
282      if self.setCompilers.getLinkerFlags().find('-Wl,-commons,use_dylibs') > -1:
283        self.addMakeMacro('DARWIN_COMMONS_USE_DYLIBS',' -Wl,-commons,use_dylibs ')
284      self.setCompilers.popLanguage()
285
286      # F90 Modules
287      if self.setCompilers.fortranModuleIncludeFlag:
288        self.addMakeMacro('FC_MODULE_FLAG', self.setCompilers.fortranModuleIncludeFlag)
289      else: # for non-f90 compilers like g77
290        self.addMakeMacro('FC_MODULE_FLAG', '-I')
291      if self.setCompilers.fortranModuleIncludeFlag:
292        self.addMakeMacro('FC_MODULE_OUTPUT_FLAG', self.setCompilers.fortranModuleOutputFlag)
293    else:
294      self.addMakeMacro('FC','')
295
296    if hasattr(self.compilers, 'CUDAC'):
297      self.setCompilers.pushLanguage('CUDA')
298      self.addMakeMacro('CUDAC_FLAGS',self.setCompilers.getCompilerFlags())
299      self.setCompilers.popLanguage()
300
301    # shared library linker values
302    self.setCompilers.pushLanguage(self.languages.clanguage)
303    # need to fix BuildSystem to collect these separately
304    self.addMakeMacro('SL_LINKER',self.setCompilers.getLinker())
305    self.addMakeMacro('SL_LINKER_FLAGS','${PCC_LINKER_FLAGS}')
306    self.setCompilers.popLanguage()
307    # One of 'a', 'so', 'lib', 'dll', 'dylib' (perhaps others also?) depending on the library generator and architecture
308    # Note: . is not included in this macro, consistent with AR_LIB_SUFFIX
309    if self.setCompilers.sharedLibraryExt == self.setCompilers.AR_LIB_SUFFIX:
310      self.addMakeMacro('SL_LINKER_SUFFIX', '')
311      self.addDefine('SLSUFFIX','""')
312    else:
313      self.addMakeMacro('SL_LINKER_SUFFIX', self.setCompilers.sharedLibraryExt)
314      self.addDefine('SLSUFFIX','"'+self.setCompilers.sharedLibraryExt+'"')
315
316    self.addMakeMacro('SL_LINKER_LIBS','${PETSC_EXTERNAL_LIB_BASIC}')
317
318#-----------------------------------------------------------------------------------------------------
319
320    # CONLY or CPP. We should change the PETSc makefiles to do this better
321    if self.languages.clanguage == 'C': lang = 'CONLY'
322    else: lang = 'CXXONLY'
323    self.addMakeMacro('PETSC_LANGUAGE',lang)
324
325    # real or complex
326    self.addMakeMacro('PETSC_SCALAR',self.scalartypes.scalartype)
327    # double or float
328    self.addMakeMacro('PETSC_PRECISION',self.scalartypes.precision)
329
330    if self.framework.argDB['with-batch']:
331      self.addMakeMacro('PETSC_WITH_BATCH','1')
332
333    # Test for compiler-specific macros that need to be defined.
334    if self.setCompilers.isCrayVector('CC', self.log):
335      self.addDefine('HAVE_CRAY_VECTOR','1')
336
337#-----------------------------------------------------------------------------------------------------
338    if self.functions.haveFunction('gethostbyname') and self.functions.haveFunction('socket') and self.headers.haveHeader('netinet/in.h'):
339      self.addDefine('USE_SOCKET_VIEWER','1')
340      if self.checkCompile('#include <sys/socket.h>','setsockopt(0,SOL_SOCKET,SO_REUSEADDR,0,0)'):
341        self.addDefine('HAVE_SO_REUSEADDR','1')
342
343#-----------------------------------------------------------------------------------------------------
344    # print include and lib for makefiles
345    self.framework.packages.reverse()
346    includes = [os.path.join(self.petscdir.dir,'include'),os.path.join(self.petscdir.dir,self.arch.arch,'include')]
347    libs = []
348    for i in self.framework.packages:
349      if i.useddirectly:
350        self.addDefine('HAVE_'+i.PACKAGE.replace('-','_'), 1)  # ONLY list package if it is used directly by PETSc (and not only by another package)
351      if not isinstance(i.lib, list):
352        i.lib = [i.lib]
353      if i.linkedbypetsc: libs.extend(i.lib)
354      self.addMakeMacro(i.PACKAGE.replace('-','_')+'_LIB', self.libraries.toStringNoDupes(i.lib))
355      if hasattr(i,'include'):
356        if not isinstance(i.include,list):
357          i.include = [i.include]
358        includes.extend(i.include)
359        self.addMakeMacro(i.PACKAGE.replace('-','_')+'_INCLUDE',self.headers.toStringNoDupes(i.include))
360    self.packagelibs = libs
361    if self.framework.argDB['with-single-library']:
362      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
363      self.addMakeMacro('PETSC_WITH_EXTERNAL_LIB',self.alllibs)
364    else:
365      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
366    self.PETSC_EXTERNAL_LIB_BASIC = self.libraries.toStringNoDupes(libs+self.libraries.math+self.compilers.flibs+self.compilers.cxxlibs)+' '+self.compilers.LIBS
367    if self.framework.argDB['prefix'] and self.setCompilers.CSharedLinkerFlag not in ['-L']:
368      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'))
369    else:
370      lib_basic = self.PETSC_EXTERNAL_LIB_BASIC
371    self.addMakeMacro('PETSC_EXTERNAL_LIB_BASIC',lib_basic)
372    self.allincludes = self.headers.toStringNoDupes(includes)
373    self.addMakeMacro('PETSC_CC_INCLUDES',self.allincludes)
374    self.PETSC_CC_INCLUDES = self.allincludes
375    if hasattr(self.compilers, 'FC'):
376      if self.compilers.fortranIsF90:
377        self.addMakeMacro('PETSC_FC_INCLUDES',self.headers.toStringNoDupes(includes,includes))
378      else:
379        self.addMakeMacro('PETSC_FC_INCLUDES',self.headers.toStringNoDupes(includes))
380
381    self.addMakeMacro('DESTDIR',self.installdir.dir)
382    self.addDefine('LIB_DIR','"'+os.path.join(self.installdir.dir,'lib')+'"')
383
384    if self.framework.argDB['with-single-library']:
385      # overrides the values set in conf/variables
386      self.addMakeMacro('LIBNAME','${INSTALL_LIB_DIR}/libpetsc.${AR_LIB_SUFFIX}')
387      self.addMakeMacro('SHLIBS','libpetsc')
388      self.addMakeMacro('PETSC_LIB_BASIC','-lpetsc')
389      self.addMakeMacro('PETSC_KSP_LIB_BASIC','-lpetsc')
390      self.addMakeMacro('PETSC_TS_LIB_BASIC','-lpetsc')
391      self.addMakeMacro('PETSC_TAO_LIB_BASIC','-lpetsc')
392      self.addDefine('USE_SINGLE_LIBRARY', '1')
393      if self.sharedlibraries.useShared:
394        self.addMakeMacro('PETSC_SYS_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
395        self.addMakeMacro('PETSC_VEC_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
396        self.addMakeMacro('PETSC_MAT_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
397        self.addMakeMacro('PETSC_DM_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
398        self.addMakeMacro('PETSC_KSP_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
399        self.addMakeMacro('PETSC_SNES_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
400        self.addMakeMacro('PETSC_TS_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
401        self.addMakeMacro('PETSC_TAO_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
402        self.addMakeMacro('PETSC_CHARACTERISTIC_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
403        self.addMakeMacro('PETSC_LIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
404        self.addMakeMacro('PETSC_CONTRIB','${C_SH_LIB_PATH} ${PETSC_WITH_EXTERNAL_LIB}')
405      else:
406        self.addMakeMacro('PETSC_SYS_LIB','${PETSC_WITH_EXTERNAL_LIB}')
407        self.addMakeMacro('PETSC_VEC_LIB','${PETSC_WITH_EXTERNAL_LIB}')
408        self.addMakeMacro('PETSC_MAT_LIB','${PETSC_WITH_EXTERNAL_LIB}')
409        self.addMakeMacro('PETSC_DM_LIB','${PETSC_WITH_EXTERNAL_LIB}')
410        self.addMakeMacro('PETSC_KSP_LIB','${PETSC_WITH_EXTERNAL_LIB}')
411        self.addMakeMacro('PETSC_SNES_LIB','${PETSC_WITH_EXTERNAL_LIB}')
412        self.addMakeMacro('PETSC_TS_LIB','${PETSC_WITH_EXTERNAL_LIB}')
413        self.addMakeMacro('PETSC_TAO_LIB','${PETSC_WITH_EXTERNAL_LIB}')
414        self.addMakeMacro('PETSC_CHARACTERISTIC_LIB','${PETSC_WITH_EXTERNAL_LIB}')
415        self.addMakeMacro('PETSC_LIB','${PETSC_WITH_EXTERNAL_LIB}')
416        self.addMakeMacro('PETSC_CONTRIB','${PETSC_WITH_EXTERNAL_LIB}')
417
418    if not os.path.exists(os.path.join(self.petscdir.dir,self.arch.arch,'lib')):
419      os.makedirs(os.path.join(self.petscdir.dir,self.arch.arch,'lib'))
420
421    # add a makefile entry for configure options
422    self.addMakeMacro('CONFIGURE_OPTIONS', self.framework.getOptionsString(['configModules', 'optionsModule']).replace('\"','\\"'))
423    return
424
425  def dumpConfigInfo(self):
426    import time
427    fd = file(os.path.join(self.arch.arch,'include','petscconfiginfo.h'),'w')
428    fd.write('static const char *petscconfigureoptions = "'+self.framework.getOptionsString(['configModules', 'optionsModule']).replace('\"','\\"')+'";\n')
429    fd.close()
430    return
431
432  def dumpMachineInfo(self):
433    import platform
434    import time
435    import script
436    def escape(s):
437      return s.replace('"',r'\"').replace(r'\ ',r'\\ ')
438    fd = file(os.path.join(self.arch.arch,'include','petscmachineinfo.h'),'w')
439    fd.write('static const char *petscmachineinfo = \"\\n\"\n')
440    fd.write('\"-----------------------------------------\\n\"\n')
441    fd.write('\"Libraries compiled on %s on %s \\n\"\n' % (time.ctime(time.time()), platform.node()))
442    fd.write('\"Machine characteristics: %s\\n\"\n' % (platform.platform()))
443    fd.write('\"Using PETSc directory: %s\\n\"\n' % (escape(self.petscdir.dir)))
444    fd.write('\"Using PETSc arch: %s\\n\"\n' % (escape(self.arch.arch)))
445    fd.write('\"-----------------------------------------\\n\";\n')
446    fd.write('static const char *petsccompilerinfo = \"\\n\"\n')
447    self.setCompilers.pushLanguage(self.languages.clanguage)
448    fd.write('\"Using C compiler: %s %s ${COPTFLAGS} ${CFLAGS}\\n\"\n' % (escape(self.setCompilers.getCompiler()), escape(self.setCompilers.getCompilerFlags())))
449    self.setCompilers.popLanguage()
450    if hasattr(self.compilers, 'FC'):
451      self.setCompilers.pushLanguage('FC')
452      fd.write('\"Using Fortran compiler: %s %s ${FOPTFLAGS} ${FFLAGS} %s\\n\"\n' % (escape(self.setCompilers.getCompiler()), escape(self.setCompilers.getCompilerFlags()), escape(self.setCompilers.CPPFLAGS)))
453      self.setCompilers.popLanguage()
454    fd.write('\"-----------------------------------------\\n\";\n')
455    fd.write('static const char *petsccompilerflagsinfo = \"\\n\"\n')
456    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)))
457    fd.write('\"-----------------------------------------\\n\";\n')
458    fd.write('static const char *petsclinkerinfo = \"\\n\"\n')
459    self.setCompilers.pushLanguage(self.languages.clanguage)
460    fd.write('\"Using C linker: %s\\n\"\n' % (escape(self.setCompilers.getLinker())))
461    self.setCompilers.popLanguage()
462    if hasattr(self.compilers, 'FC'):
463      self.setCompilers.pushLanguage('FC')
464      fd.write('\"Using Fortran linker: %s\\n\"\n' % (escape(self.setCompilers.getLinker())))
465      self.setCompilers.popLanguage()
466    if self.framework.argDB['with-single-library']:
467      petsclib = '-lpetsc'
468    else:
469      petsclib = '-lpetscts -lpetscsnes -lpetscksp -lpetscdm -lpetscmat -lpetscvec -lpetscsys'
470    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)))
471    fd.write('\"-----------------------------------------\\n\";\n')
472    fd.close()
473    return
474
475  def dumpCMakeConfig(self):
476    '''
477    Writes configuration-specific values to ${PETSC_ARCH}/lib/petsc/conf/PETScBuildInternal.cmake.
478    This file is private to PETSc and should not be included by third parties
479    (a suitable file can be produced later by CMake, but this is not it).
480    '''
481    def cmakeset(fd,key,val=True):
482      if val == True: val = 'YES'
483      if val == False: val = 'NO'
484      fd.write('set (' + key + ' ' + val + ')\n')
485    def ensurelist(a):
486      if isinstance(a,list):
487        return a
488      else:
489        return [a]
490    def libpath(lib):
491      'Returns a search path if that is what this item provides, else "" which will be cleaned out later'
492      if not isinstance(lib,str): return ''
493      if lib.startswith('-L'): return lib[2:]
494      if lib.startswith('-R'): return lib[2:]
495      if lib.startswith('-Wl,-rpath,'):
496        # This case occurs when an external package needs a specific system library that is normally provided by the compiler.
497        # In other words, the -L path is builtin to the wrapper or compiler, here we provide it so that CMake can locate the
498        # corresponding library.
499        return lib[len('-Wl,-rpath,'):]
500      if lib.startswith('-'): return ''
501      return os.path.dirname(lib)
502    def cleanlib(lib):
503      'Returns a library name if that is what this item provides, else "" which will be cleaned out later'
504      if not isinstance(lib,str): return ''
505      if lib.startswith('-l'):  return lib[2:]
506      if lib.startswith('-Wl') or lib.startswith('-L'): return ''
507      lib = os.path.splitext(os.path.basename(lib))[0]
508      if lib.startswith('lib'): return lib[3:]
509      return lib
510    def nub(lst):
511      'Return a list containing the first occurrence of each unique element'
512      unique = []
513      for elem in lst:
514        if elem not in unique and elem != '':
515          unique.append(elem)
516      return unique
517    try: reversed # reversed was added in Python-2.4
518    except NameError:
519      def reversed(lst): return lst[::-1]
520    def nublast(lst):
521      'Return a list containing the last occurrence of each unique entry in a list'
522      return reversed(nub(reversed(lst)))
523    def cmakeexpand(varname):
524      return r'"${' + varname + r'}"'
525    def uniqextend(lst,new):
526      for x in ensurelist(new):
527        if x not in lst:
528          lst.append(x)
529    def notstandardinclude(path):
530      return path not in '/usr/include'.split() # /usr/local/include is not automatically included on FreeBSD
531    def writeMacroDefinitions(fd):
532      if self.mpi.usingMPIUni:
533        cmakeset(fd,'PETSC_HAVE_MPIUNI')
534      for pkg in self.framework.packages:
535        if pkg.useddirectly:
536          cmakeset(fd,'PETSC_HAVE_' + pkg.PACKAGE.replace('-','_'))
537        for pair in pkg.defines.items():
538          if pair[0].startswith('HAVE_') and pair[1]:
539            cmakeset(fd, self.framework.getFullDefineName(pkg, pair[0]), pair[1])
540      for name,val in self.functions.defines.items():
541        cmakeset(fd,'PETSC_'+name,val)
542      for dct in [self.defines, self.libraryoptions.defines]:
543        for k,v in dct.items():
544          if k.startswith('USE_'):
545            cmakeset(fd,'PETSC_' + k, v)
546      cmakeset(fd,'PETSC_USE_COMPLEX', self.scalartypes.scalartype == 'complex')
547      cmakeset(fd,'PETSC_USE_REAL_' + self.scalartypes.precision.upper())
548      cmakeset(fd,'PETSC_CLANGUAGE_'+self.languages.clanguage)
549      if hasattr(self.compilers, 'FC'):
550        cmakeset(fd,'PETSC_HAVE_FORTRAN')
551        if self.compilers.fortranIsF90:
552          cmakeset(fd,'PETSC_USING_F90')
553        if self.compilers.fortranIsF2003:
554          cmakeset(fd,'PETSC_USING_F2003')
555      if hasattr(self.compilers, 'CXX'):
556        cmakeset(fd,'PETSC_HAVE_CXX')
557      if self.sharedlibraries.useShared:
558        cmakeset(fd,'BUILD_SHARED_LIBS')
559    def writeBuildFlags(fd):
560      def extendby(lib):
561        libs = ensurelist(lib)
562        lib_paths.extend(map(libpath,libs))
563        lib_libs.extend(map(cleanlib,libs))
564      lib_paths = []
565      lib_libs  = []
566      includes  = []
567      libvars   = []
568      for pkg in self.framework.packages:
569        if pkg.linkedbypetsc:
570          extendby(pkg.lib)
571          uniqextend(includes,pkg.include)
572      extendby(self.libraries.math)
573      extendby(self.libraries.rt)
574      extendby(self.compilers.flibs)
575      extendby(self.compilers.cxxlibs)
576      extendby(self.compilers.LIBS.split())
577      for libname in nublast(lib_libs):
578        libvar = 'PETSC_' + libname.upper() + '_LIB'
579        addpath = ''
580        for lpath in nublast(lib_paths):
581          addpath += '"' + str(lpath) + '" '
582        fd.write('find_library (' + libvar + ' ' + libname + ' HINTS ' + addpath + ')\n')
583        libvars.append(libvar)
584      fd.write('mark_as_advanced (' + ' '.join(libvars) + ')\n')
585      fd.write('set (PETSC_PACKAGE_LIBS ' + ' '.join(map(cmakeexpand,libvars)) + ')\n')
586      includes = filter(notstandardinclude,includes)
587      fd.write('set (PETSC_PACKAGE_INCLUDES ' + ' '.join(map(lambda i: '"'+i+'"',includes)) + ')\n')
588    fd = open(os.path.join(self.arch.arch,'lib','petsc','conf','PETScBuildInternal.cmake'), 'w')
589    writeMacroDefinitions(fd)
590    writeBuildFlags(fd)
591    fd.close()
592    return
593
594  def dumpCMakeLists(self):
595    import sys
596    if sys.version_info >= (2,4):
597      import cmakegen
598      try:
599        cmakegen.main(self.petscdir.dir, log=self.framework.log)
600      except (OSError), e:
601        self.framework.logPrint('Generating CMakeLists.txt failed:\n' + str(e))
602    else:
603      self.framework.logPrint('Skipping cmakegen due to old python version: ' +str(sys.version_info) )
604
605  def cmakeBoot(self):
606    import sys
607    self.cmakeboot_success = False
608    if sys.version_info >= (2,4) and hasattr(self.cmake,'cmake'):
609      try:
610        import cmakeboot
611        self.cmakeboot_success = cmakeboot.main(petscdir=self.petscdir.dir,petscarch=self.arch.arch,argDB=self.argDB,framework=self.framework,log=self.framework.log)
612      except (OSError), e:
613        self.framework.logPrint('Booting CMake in PETSC_ARCH failed:\n' + str(e))
614      except (ImportError, KeyError), e:
615        self.framework.logPrint('Importing cmakeboot failed:\n' + str(e))
616      if self.cmakeboot_success:
617        if hasattr(self.compilers, 'FC') and self.compilers.fortranIsF90 and not self.setCompilers.fortranModuleOutputFlag:
618          self.framework.logPrint('CMake configured successfully, but could not be used by default because of missing fortranModuleOutputFlag\n')
619        else:
620          self.framework.logPrint('CMake configured successfully, using as default build\n')
621          self.addMakeMacro('PETSC_BUILD_USING_CMAKE',1)
622      else:
623        self.framework.logPrint('CMake configuration was unsuccessful\n')
624    else:
625      self.framework.logPrint('Skipping cmakeboot due to old python version: ' +str(sys.version_info) )
626    return
627
628  def configurePrefetch(self):
629    '''Sees if there are any prefetch functions supported'''
630    if config.setCompilers.Configure.isSolaris(self.log) or self.framework.argDB['with-ios'] or not self.framework.argDB['with-prefetch']:
631      self.addDefine('Prefetch(a,b,c)', ' ')
632      return
633    self.pushLanguage(self.languages.clanguage)
634    if self.checkLink('#include <xmmintrin.h>', 'void *v = 0;_mm_prefetch((const char*)v,_MM_HINT_NTA);\n'):
635      # The Intel Intrinsics manual [1] specifies the prototype
636      #
637      #   void _mm_prefetch(char const *a, int sel);
638      #
639      # but other vendors seem to insist on using subtly different
640      # prototypes, including void* for the pointer, and an enum for
641      # sel.  These are both reasonable changes, but negatively impact
642      # portability.
643      #
644      # [1] http://software.intel.com/file/6373
645      self.addDefine('HAVE_XMMINTRIN_H', 1)
646      self.addDefine('Prefetch(a,b,c)', '_mm_prefetch((const char*)(a),(c))')
647      self.addDefine('PREFETCH_HINT_NTA', '_MM_HINT_NTA')
648      self.addDefine('PREFETCH_HINT_T0',  '_MM_HINT_T0')
649      self.addDefine('PREFETCH_HINT_T1',  '_MM_HINT_T1')
650      self.addDefine('PREFETCH_HINT_T2',  '_MM_HINT_T2')
651    elif self.checkLink('#include <xmmintrin.h>', 'void *v = 0;_mm_prefetch(v,_MM_HINT_NTA);\n'):
652      self.addDefine('HAVE_XMMINTRIN_H', 1)
653      self.addDefine('Prefetch(a,b,c)', '_mm_prefetch((const void*)(a),(c))')
654      self.addDefine('PREFETCH_HINT_NTA', '_MM_HINT_NTA')
655      self.addDefine('PREFETCH_HINT_T0',  '_MM_HINT_T0')
656      self.addDefine('PREFETCH_HINT_T1',  '_MM_HINT_T1')
657      self.addDefine('PREFETCH_HINT_T2',  '_MM_HINT_T2')
658    elif self.checkLink('', 'void *v = 0;__builtin_prefetch(v,0,0);\n'):
659      # From GCC docs: void __builtin_prefetch(const void *addr,int rw,int locality)
660      #
661      #   The value of rw is a compile-time constant one or zero; one
662      #   means that the prefetch is preparing for a write to the memory
663      #   address and zero, the default, means that the prefetch is
664      #   preparing for a read. The value locality must be a compile-time
665      #   constant integer between zero and three. A value of zero means
666      #   that the data has no temporal locality, so it need not be left
667      #   in the cache after the access. A value of three means that the
668      #   data has a high degree of temporal locality and should be left
669      #   in all levels of cache possible. Values of one and two mean,
670      #   respectively, a low or moderate degree of temporal locality.
671      #
672      # Here we adopt Intel's x86/x86-64 naming scheme for the locality
673      # hints.  Using macros for these values in necessary since some
674      # compilers require an enum.
675      self.addDefine('Prefetch(a,b,c)', '__builtin_prefetch((a),(b),(c))')
676      self.addDefine('PREFETCH_HINT_NTA', '0')
677      self.addDefine('PREFETCH_HINT_T0',  '3')
678      self.addDefine('PREFETCH_HINT_T1',  '2')
679      self.addDefine('PREFETCH_HINT_T2',  '1')
680    else:
681      self.addDefine('Prefetch(a,b,c)', ' ')
682    self.popLanguage()
683
684  def configureAtoll(self):
685    '''Checks if atoll exists'''
686    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")'):
687       self.addDefine('HAVE_ATOLL', '1')
688
689  def configureUnused(self):
690    '''Sees if __attribute((unused)) is supported'''
691    if self.framework.argDB['with-ios']:
692      self.addDefine('UNUSED', ' ')
693      return
694    self.pushLanguage(self.languages.clanguage)
695    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'):
696      self.addDefine('UNUSED', '__attribute((unused))')
697    else:
698      self.addDefine('UNUSED', ' ')
699    self.popLanguage()
700
701  def configureIsatty(self):
702    '''Check if the Unix C function isatty() works correctly
703       Actually just assumes it does not work correctly on batch systems'''
704    if not self.framework.argDB['with-batch']:
705      self.addDefine('USE_ISATTY',1)
706
707  def configureDeprecated(self):
708    '''Check if __attribute((deprecated)) is supported'''
709    self.pushLanguage(self.languages.clanguage)
710    ## Recent versions of gcc and clang support __attribute((deprecated("string argument"))), which is very useful, but
711    ## Intel has conspired to make a supremely environment-sensitive compiler.  The Intel compiler looks at the gcc
712    ## executable in the environment to determine the language compatibility that it should attempt to emulate.  Some
713    ## important Cray installations have built PETSc using the Intel compiler, but with a newer gcc module loaded (e.g.,
714    ## 4.7).  Thus at PETSc configure time, the Intel compiler decides to support the string argument, but the gcc
715    ## found in the default user environment is older and does not support the argument.  If GCC and Intel were cool
716    ## like Clang and supported __has_attribute, we could avoid configure tests entirely, but they don't.  And that is
717    ## why we can't have nice things.
718    #
719    # if self.checkCompile("""__attribute((deprecated("Why you shouldn't use myfunc"))) static int myfunc(void) { return 1;}""", ''):
720    #   self.addDefine('DEPRECATED(why)', '__attribute((deprecated(why)))')
721    if self.checkCompile("""__attribute((deprecated)) static int myfunc(void) { return 1;}""", ''):
722      self.addDefine('DEPRECATED(why)', '__attribute((deprecated))')
723    else:
724      self.addDefine('DEPRECATED(why)', ' ')
725    self.popLanguage()
726
727  def configureAlign(self):
728    '''Check if __attribute(align) is supported'''
729    filename = 'conftestalign'
730    includes = '''
731#include <sys/types.h>
732#if STDC_HEADERS
733#include <stdlib.h>
734#include <stdio.h>
735#include <stddef.h>
736#endif\n'''
737    body     = '''
738struct mystruct {int myint;} __attribute((aligned(16)));
739FILE *f = fopen("'''+filename+'''", "w");
740if (!f) exit(1);
741fprintf(f, "%lu\\n", (unsigned long)sizeof(struct mystruct));
742'''
743    if 'known-has-attribute-aligned' in self.argDB:
744      if self.argDB['known-has-attribute-aligned']:
745        size = 16
746      else:
747        size = -3
748    elif not self.argDB['with-batch']:
749      self.pushLanguage(self.languages.clanguage)
750      try:
751        if self.checkRun(includes, body) and os.path.exists(filename):
752          f    = file(filename)
753          size = int(f.read())
754          f.close()
755          os.remove(filename)
756        else:
757          size = -4
758      except:
759        size = -1
760        self.framework.logPrint('Error checking attribute(aligned)')
761      self.popLanguage()
762    else:
763      self.framework.addBatchInclude(['#include <stdlib.h>', '#include <stdio.h>', '#include <sys/types.h>','struct mystruct {int myint;} __attribute((aligned(16)));'])
764      self.framework.addBatchBody('fprintf(output, "  \'--known-has-attribute-aligned=%d\',\\n", sizeof(struct mystruct)==16);')
765      size = -2
766    if size == 16:
767      self.addDefine('ATTRIBUTEALIGNED(size)', '__attribute((aligned (size)))')
768      self.addDefine('HAVE_ATTRIBUTEALIGNED', 1)
769    else:
770      self.framework.logPrint('incorrect alignment. Found alignment:'+ str(size))
771      self.addDefine('ATTRIBUTEALIGNED(size)', ' ')
772    return
773
774  def configureExpect(self):
775    '''Sees if the __builtin_expect directive is supported'''
776    self.pushLanguage(self.languages.clanguage)
777    if self.checkLink('', 'if (__builtin_expect(0,1)) return 1;'):
778      self.addDefine('HAVE_BUILTIN_EXPECT', 1)
779    self.popLanguage()
780
781  def configureFunctionName(self):
782    '''Sees if the compiler supports __func__ or a variant.  Falls back
783    on __FUNCT__ which PETSc source defines, but most users do not, thus
784    stack traces through user code are better when the compiler's
785    variant is used.'''
786    def getFunctionName(lang):
787      name = '__FUNCT__'
788      self.pushLanguage(lang)
789      if self.checkLink('', "if (__func__[0] != 'm') return 1;"):
790        name = '__func__'
791      elif self.checkLink('', "if (__FUNCTION__[0] != 'm') return 1;"):
792        name = '__FUNCTION__'
793      self.popLanguage()
794      return name
795    langs = []
796
797    self.addDefine('FUNCTION_NAME_C', getFunctionName('C'))
798    if hasattr(self.compilers, 'CXX'):
799      self.addDefine('FUNCTION_NAME_CXX', getFunctionName('Cxx'))
800    else:
801      self.addDefine('FUNCTION_NAME_CXX', '__FUNCT__')
802
803  def configureIntptrt(self):
804    '''Determine what to use for uintptr_t'''
805    def staticAssertSizeMatchesVoidStar(inc,typename):
806      # The declaration is an error if either array size is negative.
807      # It should be okay to use an int that is too large, but it would be very unlikely for this to be the case
808      return self.checkCompile(inc, ('#define STATIC_ASSERT(cond) char negative_length_if_false[2*(!!(cond))-1]\n'
809                                     + 'STATIC_ASSERT(sizeof(void*) == sizeof(%s));'%typename))
810    self.pushLanguage(self.languages.clanguage)
811    if self.checkCompile('#include <stdint.h>', 'int x; uintptr_t i = (uintptr_t)&x;'):
812      self.addDefine('UINTPTR_T', 'uintptr_t')
813    elif staticAssertSizeMatchesVoidStar('','unsigned long long'):
814      self.addDefine('UINTPTR_T', 'unsigned long long')
815    elif staticAssertSizeMatchesVoidStar('#include <stdlib.h>','size_t') or staticAssertSizeMatchesVoidStar('#include <string.h>', 'size_t'):
816      self.addDefine('UINTPTR_T', 'size_t')
817    elif staticAssertSizeMatchesVoidStar('','unsigned long'):
818      self.addDefine('UINTPTR_T', 'unsigned long')
819    elif staticAssertSizeMatchesVoidStar('','unsigned'):
820      self.addDefine('UINTPTR_T', 'unsigned')
821    else:
822      raise RuntimeError('Could not find any unsigned integer type matching void*')
823    self.popLanguage()
824
825  def configureRTLDDefault(self):
826    if self.checkCompile('#include <dlfcn.h>\n void *ptr =  RTLD_DEFAULT;'):
827      self.addDefine('RTLD_DEFAULT','1')
828    return
829
830  def configureSolaris(self):
831    '''Solaris specific stuff'''
832    if os.path.isdir(os.path.join('/usr','ucblib')):
833      try:
834        flag = getattr(self.setCompilers, self.language[-1]+'SharedLinkerFlag')
835      except AttributeError:
836        flag = None
837      if flag is None:
838        self.compilers.LIBS += ' -L/usr/ucblib'
839      else:
840        self.compilers.LIBS += ' '+flag+'/usr/ucblib'
841    return
842
843  def configureLinux(self):
844    '''Linux specific stuff'''
845    # TODO: Test for this by mallocing an odd number of floats and checking the address
846    self.addDefine('HAVE_DOUBLE_ALIGN_MALLOC', 1)
847    return
848
849  def configureWin32(self):
850    '''Win32 non-cygwin specific stuff'''
851    kernel32=0
852    if self.libraries.add('Kernel32.lib','GetComputerName',prototype='#include <Windows.h>', call='GetComputerName(NULL,NULL);'):
853      self.addDefine('HAVE_WINDOWS_H',1)
854      self.addDefine('HAVE_GETCOMPUTERNAME',1)
855      kernel32=1
856    elif self.libraries.add('kernel32','GetComputerName',prototype='#include <Windows.h>', call='GetComputerName(NULL,NULL);'):
857      self.addDefine('HAVE_WINDOWS_H',1)
858      self.addDefine('HAVE_GETCOMPUTERNAME',1)
859      kernel32=1
860    if kernel32:
861      if self.framework.argDB['with-windows-graphics']:
862        self.addDefine('USE_WINDOWS_GRAPHICS',1)
863      if self.checkLink('#include <Windows.h>','LoadLibrary(0)'):
864        self.addDefine('HAVE_LOADLIBRARY',1)
865      if self.checkLink('#include <Windows.h>','GetProcAddress(0,0)'):
866        self.addDefine('HAVE_GETPROCADDRESS',1)
867      if self.checkLink('#include <Windows.h>','FreeLibrary(0)'):
868        self.addDefine('HAVE_FREELIBRARY',1)
869      if self.checkLink('#include <Windows.h>','GetLastError()'):
870        self.addDefine('HAVE_GETLASTERROR',1)
871      if self.checkLink('#include <Windows.h>','SetLastError(0)'):
872        self.addDefine('HAVE_SETLASTERROR',1)
873      if self.checkLink('#include <Windows.h>\n','QueryPerformanceCounter(0);\n'):
874        self.addDefine('USE_MICROSOFT_TIME',1)
875    if self.libraries.add('Advapi32.lib','GetUserName',prototype='#include <Windows.h>', call='GetUserName(NULL,NULL);'):
876      self.addDefine('HAVE_GET_USER_NAME',1)
877    elif self.libraries.add('advapi32','GetUserName',prototype='#include <Windows.h>', call='GetUserName(NULL,NULL);'):
878      self.addDefine('HAVE_GET_USER_NAME',1)
879
880    if not self.libraries.add('User32.lib','GetDC',prototype='#include <Windows.h>',call='GetDC(0);'):
881      self.libraries.add('user32','GetDC',prototype='#include <Windows.h>',call='GetDC(0);')
882    if not self.libraries.add('Gdi32.lib','CreateCompatibleDC',prototype='#include <Windows.h>',call='CreateCompatibleDC(0);'):
883      self.libraries.add('gdi32','CreateCompatibleDC',prototype='#include <Windows.h>',call='CreateCompatibleDC(0);')
884
885    self.types.check('int32_t', 'int')
886    if not self.checkCompile('#include <sys/types.h>\n','uid_t u;\n'):
887      self.addTypedef('int', 'uid_t')
888      self.addTypedef('int', 'gid_t')
889    if not self.checkLink('#if defined(PETSC_HAVE_UNISTD_H)\n#include <unistd.h>\n#endif\n','int a=R_OK;\n'):
890      self.framework.addDefine('R_OK', '04')
891      self.framework.addDefine('W_OK', '02')
892      self.framework.addDefine('X_OK', '01')
893    if not self.checkLink('#include <sys/stat.h>\n','int a=0;\nif (S_ISDIR(a)){}\n'):
894      self.framework.addDefine('S_ISREG(a)', '(((a)&_S_IFMT) == _S_IFREG)')
895      self.framework.addDefine('S_ISDIR(a)', '(((a)&_S_IFMT) == _S_IFDIR)')
896    if self.checkCompile('#include <Windows.h>\n','LARGE_INTEGER a;\nDWORD b=a.u.HighPart;\n'):
897      self.addDefine('HAVE_LARGE_INTEGER_U',1)
898
899    # Windows requires a Binary file creation flag when creating/opening binary files.  Is a better test in order?
900    if self.checkCompile('#include <Windows.h>\n#include <fcntl.h>\n', 'int flags = O_BINARY;'):
901      self.addDefine('HAVE_O_BINARY',1)
902
903    if self.compilers.CC.find('win32fe') >= 0:
904      self.addDefine('PATH_SEPARATOR','\';\'')
905      self.addDefine('DIR_SEPARATOR','\'\\\\\'')
906      self.addDefine('REPLACE_DIR_SEPARATOR','\'/\'')
907      self.addDefine('CANNOT_START_DEBUGGER',1)
908      (petscdir,error,status) = self.executeShellCommand('cygpath -w '+self.petscdir.dir, log = self.log)
909      self.addDefine('DIR','"'+petscdir.replace('\\','\\\\')+'"')
910    else:
911      self.addDefine('PATH_SEPARATOR','\':\'')
912      self.addDefine('REPLACE_DIR_SEPARATOR','\'\\\\\'')
913      self.addDefine('DIR_SEPARATOR','\'/\'')
914      self.addDefine('DIR', '"'+self.petscdir.dir+'"')
915
916    return
917
918#-----------------------------------------------------------------------------------------------------
919  def configureCygwinBrokenPipe(self):
920    '''Cygwin version <= 1.7.18 had issues with pipes and long commands invoked from gnu-make
921    http://cygwin.com/ml/cygwin/2013-05/msg00340.html '''
922    if config.setCompilers.Configure.isCygwin(self.log):
923      import platform
924      import re
925      r=re.compile("([0-9]+).([0-9]+).([0-9]+)")
926      m=r.match(platform.release())
927      major=int(m.group(1))
928      minor=int(m.group(2))
929      subminor=int(m.group(3))
930      if ((major < 1) or (major == 1 and minor < 7) or (major == 1 and minor == 7 and subminor <= 18)):
931        self.addMakeMacro('PETSC_CYGWIN_BROKEN_PIPE','1')
932    return
933
934#-----------------------------------------------------------------------------------------------------
935  def configureDefaultArch(self):
936    conffile = os.path.join('lib','petsc','conf', 'petscvariables')
937    if self.framework.argDB['with-default-arch']:
938      fd = file(conffile, 'w')
939      fd.write('PETSC_ARCH='+self.arch.arch+'\n')
940      fd.write('PETSC_DIR='+self.petscdir.dir+'\n')
941      fd.write('include '+os.path.join(self.petscdir.dir,self.arch.arch,'lib','petsc','conf','petscvariables')+'\n')
942      fd.close()
943      self.framework.actions.addArgument('PETSc', 'Build', 'Set default architecture to '+self.arch.arch+' in '+conffile)
944    elif os.path.isfile(conffile):
945      try:
946        os.unlink(conffile)
947      except:
948        raise RuntimeError('Unable to remove file '+conffile+'. Did a different user create it?')
949    return
950
951#-----------------------------------------------------------------------------------------------------
952  def configureScript(self):
953    '''Output a script in the conf directory which will reproduce the configuration'''
954    import nargs
955    import sys
956    scriptName = os.path.join(self.arch.arch,'lib','petsc','conf', 'reconfigure-'+self.arch.arch+'.py')
957    args = dict([(nargs.Arg.parseArgument(arg)[0], arg) for arg in self.framework.clArgs])
958    if 'with-clean' in args:
959      del args['with-clean']
960    if 'configModules' in args:
961      if nargs.Arg.parseArgument(args['configModules'])[1] == 'PETSc.Configure':
962        del args['configModules']
963    if 'optionsModule' in args:
964      if nargs.Arg.parseArgument(args['optionsModule'])[1] == 'config.compilerOptions':
965        del args['optionsModule']
966    if not 'PETSC_ARCH' in args:
967      args['PETSC_ARCH'] = 'PETSC_ARCH='+str(self.arch.arch)
968    f = file(scriptName, 'w')
969    f.write('#!'+sys.executable+'\n')
970    f.write('if __name__ == \'__main__\':\n')
971    f.write('  import sys\n')
972    f.write('  import os\n')
973    f.write('  sys.path.insert(0, os.path.abspath(\'config\'))\n')
974    f.write('  import configure\n')
975    # pretty print repr(args.values())
976    f.write('  configure_options = [\n')
977    for itm in sorted(args.values()):
978      f.write('    \''+str(itm)+'\',\n')
979    f.write('  ]\n')
980    f.write('  configure.petsc_configure(configure_options)\n')
981    f.close()
982    try:
983      os.chmod(scriptName, 0775)
984    except OSError, e:
985      self.framework.logPrint('Unable to make reconfigure script executable:\n'+str(e))
986    self.framework.actions.addArgument('PETSc', 'File creation', 'Created '+scriptName+' for automatic reconfiguration')
987    return
988
989  def configureInstall(self):
990    '''Setup the directories for installation'''
991    if self.framework.argDB['prefix']:
992      self.addMakeRule('shared_install','',['-@echo "Now to install the libraries do:"',\
993                                              '-@echo "'+self.installdir.installSudo+'make PETSC_DIR=${PETSC_DIR} PETSC_ARCH=${PETSC_ARCH} install"',\
994                                              '-@echo "========================================="'])
995    else:
996      self.addMakeRule('shared_install','',['-@echo "Now to check if the libraries are working do:"',\
997                                              '-@echo "make PETSC_DIR=${PETSC_DIR} PETSC_ARCH=${PETSC_ARCH} test"',\
998                                              '-@echo "========================================="'])
999      return
1000
1001  def configureGCOV(self):
1002    if self.framework.argDB['with-gcov']:
1003      self.addDefine('USE_GCOV','1')
1004    return
1005
1006  def configureFortranFlush(self):
1007    if hasattr(self.compilers, 'FC'):
1008      for baseName in ['flush','flush_']:
1009        if self.libraries.check('', baseName, otherLibs = self.compilers.flibs, fortranMangle = 1):
1010          self.addDefine('HAVE_'+baseName.upper(), 1)
1011          return
1012
1013  def postProcessPackages(self):
1014    postPackages=[]
1015    for i in self.framework.packages:
1016      if hasattr(i,'postProcess'): postPackages.append(i)
1017    if postPackages:
1018      # ctetgen needs petsc conf files. so attempt to create them early
1019      self.framework.dumpConfFiles()
1020      # tacky fix for dependency of Aluimia on Pflotran; requested via petsc-dev Matt provide a correct fix
1021      for i in postPackages:
1022        if i.name.upper() in ['PFLOTRAN']:
1023          i.postProcess()
1024          postPackages.remove(i)
1025      for i in postPackages: i.postProcess()
1026    return
1027
1028  def configure(self):
1029    if not os.path.samefile(self.petscdir.dir, os.getcwd()):
1030      raise RuntimeError('Wrong PETSC_DIR option specified: '+str(self.petscdir.dir) + '\n  Configure invoked in: '+os.path.realpath(os.getcwd()))
1031    if self.framework.argDB['prefix'] and os.path.isdir(self.framework.argDB['prefix']) and os.path.samefile(self.framework.argDB['prefix'],self.petscdir.dir):
1032      raise RuntimeError('Incorrect option --prefix='+self.framework.argDB['prefix']+' specified. It cannot be same as PETSC_DIR!')
1033    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)):
1034      raise RuntimeError('Incorrect option --prefix='+self.framework.argDB['prefix']+' specified. It cannot be same as PETSC_DIR/PETSC_ARCH!')
1035    self.framework.header          = os.path.join(self.arch.arch,'include','petscconf.h')
1036    self.framework.cHeader         = os.path.join(self.arch.arch,'include','petscfix.h')
1037    self.framework.makeMacroHeader = os.path.join(self.arch.arch,'lib','petsc','conf','petscvariables')
1038    self.framework.makeRuleHeader  = os.path.join(self.arch.arch,'lib','petsc','conf','petscrules')
1039    if self.libraries.math is None:
1040      raise RuntimeError('PETSc requires a functional math library. Please send configure.log to petsc-maint@mcs.anl.gov.')
1041    if self.languages.clanguage == 'Cxx' and not hasattr(self.compilers, 'CXX'):
1042      raise RuntimeError('Cannot set C language to C++ without a functional C++ compiler.')
1043    self.executeTest(self.configureRTLDDefault)
1044    self.executeTest(self.configurePrefetch)
1045    self.executeTest(self.configureUnused)
1046    self.executeTest(self.configureDeprecated)
1047    self.executeTest(self.configureIsatty)
1048    self.executeTest(self.configureExpect);
1049    self.executeTest(self.configureAlign);
1050    self.executeTest(self.configureFunctionName);
1051    self.executeTest(self.configureIntptrt);
1052    self.executeTest(self.configureSolaris)
1053    self.executeTest(self.configureLinux)
1054    self.executeTest(self.configureWin32)
1055    self.executeTest(self.configureCygwinBrokenPipe)
1056    self.executeTest(self.configureDefaultArch)
1057    self.executeTest(self.configureScript)
1058    self.executeTest(self.configureInstall)
1059    self.executeTest(self.configureGCOV)
1060    self.executeTest(self.configureFortranFlush)
1061    self.executeTest(self.configureAtoll)
1062    # dummy rules, always needed except for remote builds
1063    self.addMakeRule('remote','')
1064    self.addMakeRule('remoteclean','')
1065
1066    self.Dump()
1067    self.dumpConfigInfo()
1068    self.dumpMachineInfo()
1069    self.dumpCMakeConfig()
1070    self.dumpCMakeLists()
1071    # need to save the current state of BuildSystem so that postProcess() packages can read it in and perhaps run make install
1072    self.framework.storeSubstitutions(self.framework.argDB)
1073    self.framework.argDB['configureCache'] = cPickle.dumps(self.framework)
1074    self.framework.argDB.save(force = True)
1075    self.postProcessPackages()
1076    self.cmakeBoot()
1077    self.DumpPkgconfig()
1078    self.DumpModule()
1079    self.framework.log.write('================================================================================\n')
1080    self.logClear()
1081    return
1082