xref: /petsc/config/configure.py (revision edd0a2d9fa6ef4d7e0b754c00c43eb7e21ac31c6)
15d5a5a7bSMatthew Knepley#!/usr/bin/env python
25b6bfdb9SJed Brownfrom __future__ import print_function
32787667cSMatthew G Knepleyimport os, sys
4db5c7c58SSatish Balay
57c9abfe7SSatish BalayextraLogs = []
6b0b472b0SSatish Balaypetsc_arch = ''
74b8aa89bSBarry Smith
844b0d7f9SSatish Balay# Use en_US as language so that BuildSystem parses compiler messages in english
99b436e4bSSatish Balayif 'LC_LOCAL' in os.environ and os.environ['LC_LOCAL'] != '' and os.environ['LC_LOCAL'] != 'en_US' and os.environ['LC_LOCAL']!= 'en_US.UTF-8': os.environ['LC_LOCAL'] = 'en_US.UTF-8'
109b436e4bSSatish Balayif 'LANG' in os.environ and os.environ['LANG'] != '' and os.environ['LANG'] != 'en_US' and os.environ['LANG'] != 'en_US.UTF-8': os.environ['LANG'] = 'en_US.UTF-8'
1144b0d7f9SSatish Balay
12813ae6e9SJed Brownif sys.version_info < (2,6):
1327821aeaSJed Brown  print('************************************************************************')
1427821aeaSJed Brown  print('*      Python version 2.6+ or 3.4+ is required to run ./configure      *')
1527821aeaSJed Brown  print('*         Try: "python2.7 ./configure" or "python3 ./configure"        *')
1627821aeaSJed Brown  print('************************************************************************')
17b26a8723SBarry Smith  sys.exit(4)
182fb34ac0SMatthew Knepley
19ccb279e1SMatthew Knepleydef check_for_option_mistakes(opts):
2045faeebdSBarry Smith  for opt in opts[1:]:
21cda0060aSMatthew Knepley    name = opt.split('=')[0]
22ccb279e1SMatthew Knepley    if name.find('_') >= 0:
23ccb279e1SMatthew Knepley      exception = False
241de7a49fSSatish Balay      for exc in ['mkl_sparse', 'mkl_sparse_optimize', 'mkl_cpardiso', 'mkl_pardiso', 'superlu_dist', 'PETSC_ARCH', 'PETSC_DIR', 'CXX_CXXFLAGS', 'LD_SHARED', 'CC_LINKER_FLAGS', 'CXX_LINKER_FLAGS', 'FC_LINKER_FLAGS', 'AR_FLAGS', 'C_VERSION', 'CXX_VERSION', 'FC_VERSION', 'size_t', 'MPI_Comm','MPI_Fint','int64_t']:
25ccb279e1SMatthew Knepley        if name.find(exc) >= 0:
26ccb279e1SMatthew Knepley          exception = True
27ccb279e1SMatthew Knepley      if not exception:
28ccb279e1SMatthew Knepley        raise ValueError('The option '+name+' should probably be '+name.replace('_', '-'));
29ab610953SSatish Balay    if opt.find('=') >=0:
30ab610953SSatish Balay      optval = opt.split('=')[1]
31ab610953SSatish Balay      if optval == 'ifneeded':
32ab610953SSatish Balay        raise ValueError('The option '+opt+' should probably be '+opt.replace('ifneeded', '1'));
33d1fb55d9SBarry Smith    for exc in ['mkl_sparse', 'mkl_sparse_optimize', 'mkl_cpardiso', 'mkl_pardiso', 'superlu_dist']:
34d1fb55d9SBarry Smith      if name.find(exc.replace('_','-')) > -1:
35d1fb55d9SBarry Smith        raise ValueError('The option '+opt+' should be '+opt.replace(exc.replace('_','-'),exc));
36ccb279e1SMatthew Knepley  return
37ccb279e1SMatthew Knepley
382f393ef5SBarry Smithdef check_for_unsupported_combinations(opts):
392f393ef5SBarry Smith  if '--with-precision=single' in opts and '--with-clanguage=cxx' in opts and '--with-scalar-type=complex' in opts:
402f393ef5SBarry Smith    sys.exit(ValueError('PETSc does not support single precision complex with C++ clanguage, run with --with-clanguage=c'))
412f393ef5SBarry Smith
422af240f0SSatish Balaydef check_for_option_changed(opts):
432af240f0SSatish Balay# Document changes in command line options here.
444544382eSSatish Balay  optMap = [('with-64bit-indices','with-64-bit-indices'),
45008db1b6SSatish Balay            ('with-mpi-exec','with-mpiexec'),
464544382eSSatish Balay            ('c-blas-lapack','f2cblaslapack'),
474544382eSSatish Balay            ('cholmod','suitesparse'),
484544382eSSatish Balay            ('umfpack','suitesparse'),
494544382eSSatish Balay            ('f-blas-lapack','fblaslapack'),
504544382eSSatish Balay            ('with-cuda-arch',
514544382eSSatish Balay             'CUDAFLAGS=-arch'),
524544382eSSatish Balay            ('with-packages-dir','with-packages-download-dir'),
534544382eSSatish Balay            ('with-external-packages-dir','with-packages-build-dir'),
544544382eSSatish Balay            ('package-dirs','with-packages-search-path'),
554544382eSSatish Balay            ('download-petsc4py-python','with-python-exec'),
564544382eSSatish Balay            ('search-dirs','with-executables-search-path')]
572af240f0SSatish Balay  for opt in opts[1:]:
582af240f0SSatish Balay    optname = opt.split('=')[0].strip('-')
592af240f0SSatish Balay    for oldname,newname in optMap:
602af240f0SSatish Balay      if optname.find(oldname) >=0:
612af240f0SSatish Balay        raise ValueError('The option '+opt+' should probably be '+opt.replace(oldname,newname))
622af240f0SSatish Balay  return
632af240f0SSatish Balay
6459e9bfd6SSatish Balaydef check_petsc_arch(opts):
65c43ea0feSSatish Balay  # If PETSC_ARCH not specified - use script name (if not configure.py)
66b0b472b0SSatish Balay  global petsc_arch
67c43ea0feSSatish Balay  found = 0
6859e9bfd6SSatish Balay  for name in opts:
69c43ea0feSSatish Balay    if name.find('PETSC_ARCH=') >= 0:
70b0b472b0SSatish Balay      petsc_arch=name.split('=')[1]
71c43ea0feSSatish Balay      found = 1
7259e9bfd6SSatish Balay      break
7359e9bfd6SSatish Balay  # If not yet specified - use the filename of script
74c43ea0feSSatish Balay  if not found:
7559e9bfd6SSatish Balay      filename = os.path.basename(sys.argv[0])
76e68ebbecSBarry Smith      if not filename.startswith('configure') and not filename.startswith('reconfigure') and not filename.startswith('setup'):
77b0b472b0SSatish Balay        petsc_arch=os.path.splitext(os.path.basename(sys.argv[0]))[0]
78b0b472b0SSatish Balay        useName = 'PETSC_ARCH='+petsc_arch
7959e9bfd6SSatish Balay        opts.append(useName)
801937db7aSSatish Balay  return 0
814b8aa89bSBarry Smith
82f08ee00aSJason Sarichdef chkenable():
83f08ee00aSJason Sarich  #Replace all 'enable-'/'disable-' with 'with-'=0/1/tail
84f08ee00aSJason Sarich  #enable-fortran is a special case, the resulting --with-fortran is ambiguous.
851b266c99SBarry Smith  #Would it mean --with-fc=
865bb5b98aSSatish Balay  en_dash = u'\N{EN DASH}'
875bb5b98aSSatish Balay  if sys.version_info < (3, 0):
885bb5b98aSSatish Balay    en_dash = en_dash.encode('utf-8')
89f08ee00aSJason Sarich  for l in range(0,len(sys.argv)):
90f08ee00aSJason Sarich    name = sys.argv[l]
91f08ee00aSJason Sarich
925bb5b98aSSatish Balay    if name.find(en_dash)  >= 0:
935bb5b98aSSatish Balay      sys.argv[l] = name.replace(en_dash,'-')
942b700d61SJason Sarich    if name.find('enable-cxx') >= 0:
952b700d61SJason Sarich      if name.find('=') == -1:
962b700d61SJason Sarich        sys.argv[l] = name.replace('enable-cxx','with-clanguage=C++')
972b700d61SJason Sarich      else:
982b700d61SJason Sarich        head, tail = name.split('=', 1)
992b700d61SJason Sarich        if tail=='0':
1002b700d61SJason Sarich          sys.argv[l] = head.replace('enable-cxx','with-clanguage=C')
1012b700d61SJason Sarich        else:
1022b700d61SJason Sarich          sys.argv[l] = head.replace('enable-cxx','with-clanguage=C++')
1032b700d61SJason Sarich      continue
1042b700d61SJason Sarich    if name.find('disable-cxx') >= 0:
1052b700d61SJason Sarich      if name.find('=') == -1:
1062b700d61SJason Sarich        sys.argv[l] = name.replace('disable-cxx','with-clanguage=C')
1072b700d61SJason Sarich      else:
1082b700d61SJason Sarich        head, tail = name.split('=', 1)
1092b700d61SJason Sarich        if tail == '0':
1102b700d61SJason Sarich          sys.argv[l] = head.replace('disable-cxx','with-clanguage=C++')
1112b700d61SJason Sarich        else:
1122b700d61SJason Sarich          sys.argv[l] = head.replace('disable-cxx','with-clanguage=C')
1132b700d61SJason Sarich      continue
1142b700d61SJason Sarich
115f08ee00aSJason Sarich
116f08ee00aSJason Sarich    if name.find('enable-') >= 0:
117f08ee00aSJason Sarich      if name.find('=') == -1:
118f08ee00aSJason Sarich        sys.argv[l] = name.replace('enable-','with-')+'=1'
119f08ee00aSJason Sarich      else:
120f08ee00aSJason Sarich        head, tail = name.split('=', 1)
121f08ee00aSJason Sarich        sys.argv[l] = head.replace('enable-','with-')+'='+tail
122f08ee00aSJason Sarich    if name.find('disable-') >= 0:
123f08ee00aSJason Sarich      if name.find('=') == -1:
124f08ee00aSJason Sarich        sys.argv[l] = name.replace('disable-','with-')+'=0'
125f08ee00aSJason Sarich      else:
126f08ee00aSJason Sarich        head, tail = name.split('=', 1)
127f08ee00aSJason Sarich        if tail == '1': tail = '0'
128f08ee00aSJason Sarich        sys.argv[l] = head.replace('disable-','with-')+'='+tail
129f08ee00aSJason Sarich    if name.find('without-') >= 0:
130f08ee00aSJason Sarich      if name.find('=') == -1:
131f08ee00aSJason Sarich        sys.argv[l] = name.replace('without-','with-')+'=0'
132f08ee00aSJason Sarich      else:
133f08ee00aSJason Sarich        head, tail = name.split('=', 1)
134f08ee00aSJason Sarich        if tail == '1': tail = '0'
135f08ee00aSJason Sarich        sys.argv[l] = head.replace('without-','with-')+'='+tail
136f08ee00aSJason Sarich
137f08ee00aSJason Sarichdef chksynonyms():
138f08ee00aSJason Sarich  #replace common configure options with ones that PETSc BuildSystem recognizes
13998d87fb0SBarry Smith  simplereplacements = {'F77' : 'FC', 'F90' : 'FC'}
140f08ee00aSJason Sarich  for l in range(0,len(sys.argv)):
141f08ee00aSJason Sarich    name = sys.argv[l]
142f08ee00aSJason Sarich
1436cdd910dSSatish Balay    if name.find('with-blas-lapack') >= 0:
1446cdd910dSSatish Balay      sys.argv[l] = name.replace('with-blas-lapack','with-blaslapack')
145c87df54fSBarry Smith
146ce54fb35SBarry Smith    if name.find('with-debug=') >= 0 or name.endswith('with-debug'):
147f08ee00aSJason Sarich      if name.find('=') == -1:
148f08ee00aSJason Sarich        sys.argv[l] = name.replace('with-debug','with-debugging')+'=1'
149f08ee00aSJason Sarich      else:
150f08ee00aSJason Sarich        head, tail = name.split('=', 1)
151f08ee00aSJason Sarich        sys.argv[l] = head.replace('with-debug','with-debugging')+'='+tail
152f08ee00aSJason Sarich
153ce54fb35SBarry Smith    if name.find('with-shared=') >= 0 or name.endswith('with-shared'):
154f08ee00aSJason Sarich      if name.find('=') == -1:
155ce54fb35SBarry Smith        sys.argv[l] = name.replace('with-shared','with-shared-libraries')+'=1'
156f08ee00aSJason Sarich      else:
157f08ee00aSJason Sarich        head, tail = name.split('=', 1)
158ce54fb35SBarry Smith        sys.argv[l] = head.replace('with-shared','with-shared-libraries')+'='+tail
159f08ee00aSJason Sarich
1608fd71741SJason Sarich    if name.find('with-index-size=') >=0:
1618fd71741SJason Sarich      head,tail = name.split('=',1)
1628fd71741SJason Sarich      if int(tail)==32:
1638fd71741SJason Sarich        sys.argv[l] = '--with-64-bit-indices=0'
1648fd71741SJason Sarich      elif int(tail)==64:
1658fd71741SJason Sarich        sys.argv[l] = '--with-64-bit-indices=1'
1668fd71741SJason Sarich      else:
1678fd71741SJason Sarich        raise RuntimeError('--with-index-size= must be 32 or 64')
168f08ee00aSJason Sarich
1698fd71741SJason Sarich    if name.find('with-precision=') >=0:
1708fd71741SJason Sarich      head,tail = name.split('=',1)
1718fd71741SJason Sarich      if tail.find('quad')>=0:
1728fd71741SJason Sarich        sys.argv[l]='--with-precision=__float128'
173f08ee00aSJason Sarich
17498d87fb0SBarry Smith    for i,j in simplereplacements.items():
17598d87fb0SBarry Smith      if name.find(i+'=') >= 0:
17698d87fb0SBarry Smith        sys.argv[l] = name.replace(i+'=',j+'=')
17798d87fb0SBarry Smith      elif name.find('with-'+i.lower()+'=') >= 0:
17898d87fb0SBarry Smith        sys.argv[l] = name.replace(i.lower()+'=',j.lower()+'=')
179f08ee00aSJason Sarich
1801921852fSSatish Balaydef chkwinf90():
1816a8f6897SSatish Balay  for arg in sys.argv:
1821921852fSSatish Balay    if (arg.find('win32fe') >= 0 and (arg.find('f90') >=0 or arg.find('ifort') >=0)):
1836a8f6897SSatish Balay      return 1
1846a8f6897SSatish Balay  return 0
1856a8f6897SSatish Balay
1868a4600f2SSatish Balaydef chkdosfiles():
187360dfd13SSatish Balay  # cygwin - but not a hg clone - so check one of files in bin dir
1888fb0cc24SJed Brown  if b"\r\n" in open(os.path.join('lib','petsc','bin','petscmpiexec'),"rb").read():
1895b6bfdb9SJed Brown    print('===============================================================================')
1905b6bfdb9SJed Brown    print(' *** Scripts are in DOS mode. Was winzip used to extract petsc sources?    ****')
1915b6bfdb9SJed Brown    print(' *** Please restart with a fresh tarball and use "tar -xzf petsc.tar.gz"   ****')
1925b6bfdb9SJed Brown    print('===============================================================================')
193db1f124cSVasiliy Kozyrev    sys.exit(3)
1948a4600f2SSatish Balay  return
1958a4600f2SSatish Balay
1966a8f6897SSatish Balaydef chkcygwinlink():
1971921852fSSatish Balay  if os.path.exists('/usr/bin/cygcheck.exe') and os.path.exists('/usr/bin/link.exe') and chkwinf90():
1986a8f6897SSatish Balay      if '--ignore-cygwin-link' in sys.argv: return 0
1995b6bfdb9SJed Brown      print('===============================================================================')
2005b6bfdb9SJed Brown      print(' *** Cygwin /usr/bin/link detected! Compiles with CVF/Intel f90 can break!  **')
2015b6bfdb9SJed Brown      print(' *** To workarround do: "mv /usr/bin/link.exe /usr/bin/link-cygwin.exe"     **')
2025b6bfdb9SJed Brown      print(' *** Or to ignore this check, use configure option: --ignore-cygwin-link    **')
2035b6bfdb9SJed Brown      print('===============================================================================')
2046a8f6897SSatish Balay      sys.exit(3)
2056a8f6897SSatish Balay  return 0
2066a8f6897SSatish Balay
20785ef4d1eSSatish Balaydef chkbrokencygwin():
2089dabcff0SSatish Balay  if os.path.exists('/usr/bin/cygcheck.exe'):
2099dabcff0SSatish Balay    buf = os.popen('/usr/bin/cygcheck.exe -c cygwin').read()
2109dabcff0SSatish Balay    if buf.find('1.5.11-1') > -1:
2115b6bfdb9SJed Brown      print('===============================================================================')
2125b6bfdb9SJed Brown      print(' *** cygwin-1.5.11-1 detected. ./configure fails with this version ***')
2135b6bfdb9SJed Brown      print(' *** Please upgrade to cygwin-1.5.12-1 or newer version. This can  ***')
2145b6bfdb9SJed Brown      print(' *** be done by running cygwin-setup, selecting "next" all the way.***')
2155b6bfdb9SJed Brown      print('===============================================================================')
2161937db7aSSatish Balay      sys.exit(3)
2179dabcff0SSatish Balay  return 0
2189dabcff0SSatish Balay
219ee76e990SSatish Balaydef chkusingwindowspython():
220ee76e990SSatish Balay  if sys.platform == 'win32':
2215b6bfdb9SJed Brown    print('===============================================================================')
2225b6bfdb9SJed Brown    print(' *** Windows python detected. Please rerun ./configure with cygwin-python. ***')
2235b6bfdb9SJed Brown    print('===============================================================================')
224ee76e990SSatish Balay    sys.exit(3)
225ee76e990SSatish Balay  return 0
226ee76e990SSatish Balay
22714f5c25cSSatish Balaydef chkcygwinpython():
2281150532aSSatish Balay  if sys.platform == 'cygwin' :
2291150532aSSatish Balay    import platform
2301150532aSSatish Balay    import re
2311150532aSSatish Balay    r=re.compile("([0-9]+).([0-9]+).([0-9]+)")
2321150532aSSatish Balay    m=r.match(platform.release())
2331150532aSSatish Balay    major=int(m.group(1))
2341150532aSSatish Balay    minor=int(m.group(2))
2351150532aSSatish Balay    subminor=int(m.group(3))
2361150532aSSatish Balay    if ((major < 1) or (major == 1 and minor < 7) or (major == 1 and minor == 7 and subminor < 34)):
2371937db7aSSatish Balay      sys.argv.append('--useThreads=0')
2381937db7aSSatish Balay      extraLogs.append('''\
239a0022257SSatish Balay===============================================================================
2401150532aSSatish Balay** Cygwin version is older than 1.7.34. Python threads do not work correctly. ***
24114f5c25cSSatish Balay** Disabling thread usage for this run of ./configure *******
242a0022257SSatish Balay===============================================================================''')
24371384062SSatish Balay  return 0
24471384062SSatish Balay
245cfb8f47aSBarry Smithdef chkcygwinwindowscompilers():
246cfb8f47aSBarry Smith  '''Adds win32fe for Microsoft/Intel compilers'''
247cfb8f47aSBarry Smith  if os.path.exists('/usr/bin/cygcheck.exe'):
248cfb8f47aSBarry Smith    for l in range(1,len(sys.argv)):
249cfb8f47aSBarry Smith      option = sys.argv[l]
250cfb8f47aSBarry Smith      for i in ['cl','icl','ifort']:
251cfb8f47aSBarry Smith        if option.startswith(i):
252cfb8f47aSBarry Smith          sys.argv[l] = 'win32fe '+option
253cfb8f47aSBarry Smith          break
254cfb8f47aSBarry Smith  return 0
255cfb8f47aSBarry Smith
2561937db7aSSatish Balaydef chkrhl9():
2571937db7aSSatish Balay  if os.path.exists('/etc/redhat-release'):
258836c2c52SSatish Balay    try:
259594eb360SSatish Balay      file = open('/etc/redhat-release','r')
260836c2c52SSatish Balay      buf = file.read()
261836c2c52SSatish Balay      file.close()
262836c2c52SSatish Balay    except:
263836c2c52SSatish Balay      # can't read file - assume dangerous RHL9
2641937db7aSSatish Balay      buf = 'Shrike'
265836c2c52SSatish Balay    if buf.find('Shrike') > -1:
2661937db7aSSatish Balay      sys.argv.append('--useThreads=0')
2671937db7aSSatish Balay      extraLogs.append('''\
268a0022257SSatish Balay==============================================================================
2691937db7aSSatish Balay   *** RHL9 detected. Threads do not work correctly with this distribution ***
270e2e64c6bSBarry Smith   ****** Disabling thread usage for this run of ./configure *********
271a0022257SSatish Balay===============================================================================''')
272836c2c52SSatish Balay  return 0
273836c2c52SSatish Balay
274fd1ac241SSatish Balaydef chktmpnoexec():
275fd1ac241SSatish Balay  if not hasattr(os,'ST_NOEXEC'): return
276fd1ac241SSatish Balay  if 'TMPDIR' in os.environ: tmpDir = os.environ['TMPDIR']
277fd1ac241SSatish Balay  else: tmpDir = '/tmp'
278fd1ac241SSatish Balay  if os.statvfs(tmpDir).f_flag & os.ST_NOEXEC:
279fd1ac241SSatish Balay    if os.statvfs(os.path.abspath('.')).f_flag & os.ST_NOEXEC:
280fd1ac241SSatish Balay      print('************************************************************************')
281fd1ac241SSatish Balay      print('* TMPDIR '+tmpDir+' has noexec attribute. Same with '+os.path.abspath('.')+' where petsc is built.')
282fd1ac241SSatish Balay      print('* Suggest building PETSc in a location without this restriction!')
283fd1ac241SSatish Balay      print('* Alternatively, set env variable TMPDIR to a location that is not restricted to run binaries.')
284fd1ac241SSatish Balay      print('************************************************************************')
285fd1ac241SSatish Balay      sys.exit(4)
286fd1ac241SSatish Balay    else:
287fd1ac241SSatish Balay      newTmp = os.path.abspath('tmp-petsc')
288fd1ac241SSatish Balay      print('************************************************************************')
289fd1ac241SSatish Balay      print('* TMPDIR '+tmpDir+' has noexec attribute. Using '+newTmp+' instead.')
290fd1ac241SSatish Balay      print('************************************************************************')
291fd1ac241SSatish Balay      if not os.path.isdir(newTmp): os.mkdir(os.path.abspath(newTmp))
292fd1ac241SSatish Balay      os.environ['TMPDIR'] = newTmp
293fd1ac241SSatish Balay  return
294fd1ac241SSatish Balay
29505f86fb1SBarry Smithdef check_cray_modules():
29605f86fb1SBarry Smith  import script
29705f86fb1SBarry Smith  '''For Cray systems check if the cc, CC, ftn compiler suite modules have been set'''
29805f86fb1SBarry Smith  cray = os.getenv('CRAY_SITE_LIST_DIR')
29905f86fb1SBarry Smith  if not cray: return
30005f86fb1SBarry Smith  cray = os.getenv('CRAYPE_DIR')
30105f86fb1SBarry Smith  if not cray:
30205f86fb1SBarry Smith   print('************************************************************************')
30305f86fb1SBarry Smith   print('* You are on a Cray system but no programming environments have been loaded')
30405f86fb1SBarry Smith   print('* Perhaps you need:')
30505f86fb1SBarry Smith   print('*       module load intel ; module load PrgEnv-intel')
30605f86fb1SBarry Smith   print('*   or  module load PrgEnv-cray')
30705f86fb1SBarry Smith   print('*   or  module load PrgEnv-gnu')
30805f86fb1SBarry Smith   print('* See https://www.mcs.anl.gov/petsc/documentation/installation.html#doemachines')
30905f86fb1SBarry Smith   print('************************************************************************')
31005f86fb1SBarry Smith   sys.exit(4)
31105f86fb1SBarry Smith
312da58527dSSatish Balaydef check_broken_configure_log_links():
313da58527dSSatish Balay  '''Sometime symlinks can get broken if the original files are deleted. Delete such broken links'''
314da58527dSSatish Balay  import os
315da58527dSSatish Balay  for logfile in ['configure.log','configure.log.bkp']:
316da58527dSSatish Balay    if os.path.islink(logfile) and not os.path.isfile(logfile): os.remove(logfile)
317da58527dSSatish Balay  return
318da58527dSSatish Balay
319da1d79b4SSatish Balaydef move_configure_log(framework):
320af0996ceSBarry Smith  '''Move configure.log to PETSC_ARCH/lib/petsc/conf - and update configure.log.bkp in both locations appropriately'''
321b0b472b0SSatish Balay  global petsc_arch
322b0b472b0SSatish Balay
323b0b472b0SSatish Balay  if hasattr(framework,'arch'): petsc_arch = framework.arch
324b0b472b0SSatish Balay  if hasattr(framework,'logName'): curr_file = framework.logName
325b0b472b0SSatish Balay  else: curr_file = 'configure.log'
326b0b472b0SSatish Balay
327b0b472b0SSatish Balay  if petsc_arch:
328da1d79b4SSatish Balay    import shutil
329da1d79b4SSatish Balay    import os
330b0b472b0SSatish Balay
331b0b472b0SSatish Balay    # Just in case - confdir is not created
332fe998a80SBarry Smith    lib_dir = os.path.join(petsc_arch,'lib')
333af0996ceSBarry Smith    conf_dir = os.path.join(petsc_arch,'lib','petsc','conf')
334b0b472b0SSatish Balay    if not os.path.isdir(petsc_arch): os.mkdir(petsc_arch)
335fe998a80SBarry Smith    if not os.path.isdir(lib_dir): os.mkdir(lib_dir)
336b0b472b0SSatish Balay    if not os.path.isdir(conf_dir): os.mkdir(conf_dir)
337b0b472b0SSatish Balay
338da1d79b4SSatish Balay    curr_bkp  = curr_file + '.bkp'
339b0b472b0SSatish Balay    new_file  = os.path.join(conf_dir,curr_file)
340da1d79b4SSatish Balay    new_bkp   = new_file + '.bkp'
341da1d79b4SSatish Balay
342af0996ceSBarry Smith    # Keep backup in $PETSC_ARCH/lib/petsc/conf location
343da1d79b4SSatish Balay    if os.path.isfile(new_bkp): os.remove(new_bkp)
344da1d79b4SSatish Balay    if os.path.isfile(new_file): os.rename(new_file,new_bkp)
3459e50940cSSatish Balay    if os.path.isfile(curr_file):
3469e50940cSSatish Balay      shutil.copyfile(curr_file,new_file)
3479e50940cSSatish Balay      os.remove(curr_file)
348da58527dSSatish Balay    if os.path.isfile(new_file): os.symlink(new_file,curr_file)
349af0996ceSBarry Smith    # If the old bkp is using the same PETSC_ARCH/lib/petsc/conf - then update bkp link
350da1d79b4SSatish Balay    if os.path.realpath(curr_bkp) == os.path.realpath(new_file):
351da58527dSSatish Balay      if os.path.isfile(curr_bkp): os.remove(curr_bkp)
352da58527dSSatish Balay      if os.path.isfile(new_bkp): os.symlink(new_bkp,curr_bkp)
353da1d79b4SSatish Balay  return
354da1d79b4SSatish Balay
355d93c4beeSSatish Balaydef print_final_timestamp(framework):
356d93c4beeSSatish Balay  import time
357d93c4beeSSatish Balay  framework.log.write(('='*80)+'\n')
3587a01bd3eSSatish Balay  framework.log.write('Finishing configure run at '+time.strftime('%a, %d %b %Y %H:%M:%S %z')+'\n')
359d93c4beeSSatish Balay  framework.log.write(('='*80)+'\n')
360d93c4beeSSatish Balay  return
361d93c4beeSSatish Balay
3625d5a5a7bSMatthew Knepleydef petsc_configure(configure_options):
3636e464aa7SMatthew G. Knepley  if 'PETSC_DIR' in os.environ:
3644a532159SBarry Smith    petscdir = os.environ['PETSC_DIR']
3656e464aa7SMatthew G. Knepley    if petscdir.find(' ') > -1:
3666e464aa7SMatthew G. Knepley      raise RuntimeError('Your PETSC_DIR '+petscdir+' has spaces in it; this is not allowed.\n Change the directory with PETSc to not have spaces in it')
3676e464aa7SMatthew G. Knepley    try:
368c3a89c15SBarry Smith      sys.path.append(os.path.join(petscdir,'lib','petsc','bin'))
3694a532159SBarry Smith      import petscnagupgrade
3704a532159SBarry Smith      file     = os.path.join(petscdir,'.nagged')
3714a532159SBarry Smith      if not petscnagupgrade.naggedtoday(file):
3724a532159SBarry Smith        petscnagupgrade.currentversion(petscdir)
3734a532159SBarry Smith    except:
3744a532159SBarry Smith      pass
3755b6bfdb9SJed Brown  print('===============================================================================')
3765b6bfdb9SJed Brown  print('             Configuring PETSc to compile on your system                       ')
3775b6bfdb9SJed Brown  print('===============================================================================')
37859e9bfd6SSatish Balay
379a258c2c4SMatthew G Knepley  try:
380c43ea0feSSatish Balay    # Command line arguments take precedence (but don't destroy argv[0])
381c43ea0feSSatish Balay    sys.argv = sys.argv[:1] + configure_options + sys.argv[1:]
382ccb279e1SMatthew Knepley    check_for_option_mistakes(sys.argv)
3832af240f0SSatish Balay    check_for_option_changed(sys.argv)
3845b6bfdb9SJed Brown  except (TypeError, ValueError) as e:
385a258c2c4SMatthew G Knepley    emsg = str(e)
386a258c2c4SMatthew G Knepley    if not emsg.endswith('\n'): emsg = emsg+'\n'
387a258c2c4SMatthew G Knepley    msg ='*******************************************************************************\n'\
388a258c2c4SMatthew G Knepley    +'                ERROR in COMMAND LINE ARGUMENT to ./configure \n' \
389a258c2c4SMatthew G Knepley    +'-------------------------------------------------------------------------------\n'  \
390a258c2c4SMatthew G Knepley    +emsg+'*******************************************************************************\n'
391a258c2c4SMatthew G Knepley    sys.exit(msg)
39259e9bfd6SSatish Balay  # check PETSC_ARCH
3932f393ef5SBarry Smith  check_for_unsupported_combinations(sys.argv)
39459e9bfd6SSatish Balay  check_petsc_arch(sys.argv)
395da58527dSSatish Balay  check_broken_configure_log_links()
3965fb2c094SBarry Smith
397f08ee00aSJason Sarich  #rename '--enable-' to '--with-'
398f08ee00aSJason Sarich  chkenable()
399c22cdea9SBarry Smith  # support a few standard configure option types
400f08ee00aSJason Sarich  chksynonyms()
4019dabcff0SSatish Balay  # Check for broken cygwin
4021937db7aSSatish Balay  chkbrokencygwin()
403d65f3bddSMatthew Knepley  # Disable threads on RHL9
4041937db7aSSatish Balay  chkrhl9()
405ee76e990SSatish Balay  # Make sure cygwin-python is used on windows
406ee76e990SSatish Balay  chkusingwindowspython()
40714f5c25cSSatish Balay  # Threads don't work for cygwin & python...
40814f5c25cSSatish Balay  chkcygwinpython()
4096a8f6897SSatish Balay  chkcygwinlink()
4108a4600f2SSatish Balay  chkdosfiles()
411cfb8f47aSBarry Smith  chkcygwinwindowscompilers()
412fd1ac241SSatish Balay  chktmpnoexec()
4139dabcff0SSatish Balay
414a9acdec7SBarry Smith  for l in range(1,len(sys.argv)):
415a9acdec7SBarry Smith    if sys.argv[l].startswith('--with-fc=') and sys.argv[l].endswith('nagfor'):
416a9acdec7SBarry Smith      # need a way to save this value and later CC so that petscnagfor may use them
417a9acdec7SBarry Smith      name = sys.argv[l].split('=')[1]
418a9acdec7SBarry Smith      sys.argv[l] = '--with-fc='+os.path.join(os.path.abspath('.'),'lib','petsc','bin','petscnagfor')
419a9acdec7SBarry Smith      break
420a9acdec7SBarry Smith
421a9acdec7SBarry Smith
42287282423SMatthew Knepley  # Should be run from the toplevel
423dbca6d9dSSatish Balay  configDir = os.path.abspath('config')
424f8833479SBarry Smith  bsDir     = os.path.join(configDir, 'BuildSystem')
425f8833479SBarry Smith  if not os.path.isdir(configDir):
4265d5a5a7bSMatthew Knepley    raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.'))
42787282423SMatthew Knepley  sys.path.insert(0, bsDir)
428f8833479SBarry Smith  sys.path.insert(0, configDir)
429e69ef9dfSMatthew Knepley  import config.base
4305d5a5a7bSMatthew Knepley  import config.framework
431492432c8SJed Brown  import pickle
4324e5e6a77SJed Brown  import traceback
4334f8a5b45SBarry Smith
43405f86fb1SBarry Smith  # Check Cray without modules
43505f86fb1SBarry Smith  check_cray_modules()
43605f86fb1SBarry Smith
43738c5b55eSBarry Smith  tbo = None
4389dd2fdb1SMatthew Knepley  framework = None
4399dd2fdb1SMatthew Knepley  try:
44023a19ef1SSatish Balay    framework = config.framework.Framework(['--configModules=PETSc.Configure','--optionsModule=config.compilerOptions']+sys.argv[1:], loadArgDB = 0)
441d65f3bddSMatthew Knepley    framework.setup()
442d65f3bddSMatthew Knepley    framework.logPrint('\n'.join(extraLogs))
443f24f64feSBarry Smith    framework.configure(out = sys.stdout)
444358ebc22SMatthew Knepley    framework.storeSubstitutions(framework.argDB)
445492432c8SJed Brown    framework.argDB['configureCache'] = pickle.dumps(framework)
4467c939e48SSatish Balay    framework.printSummary()
44712c1d45bSMatthew G Knepley    framework.argDB.save(force = True)
4487cfd0b05SBarry Smith    framework.logClear()
449d93c4beeSSatish Balay    print_final_timestamp(framework)
450eefa2c0fSBarry Smith    framework.closeLog()
4519e50940cSSatish Balay    try:
452da1d79b4SSatish Balay      move_configure_log(framework)
4539e50940cSSatish Balay    except:
4549e50940cSSatish Balay      # perhaps print an error about unable to shuffle logs?
4559e50940cSSatish Balay      pass
456dd50d019SBarry Smith    return 0
4575b6bfdb9SJed Brown  except (RuntimeError, config.base.ConfigureSetupError) as e:
4587d670a3cSBarry Smith    emsg = str(e)
45942351d26SSatish Balay    if not emsg.endswith('\n'): emsg = emsg+'\n'
460a0022257SSatish Balay    msg ='*******************************************************************************\n'\
461fe09c992SBarry Smith    +'         UNABLE to CONFIGURE with GIVEN OPTIONS    (see configure.log for details):\n' \
462a0022257SSatish Balay    +'-------------------------------------------------------------------------------\n'  \
463a0022257SSatish Balay    +emsg+'*******************************************************************************\n'
464e9f3bb17SBarry Smith    se = ''
4655b6bfdb9SJed Brown  except (TypeError, ValueError) as e:
46638c5b55eSBarry Smith    # this exception is automatically deleted by Python so we need to save it to print below
46738c5b55eSBarry Smith    tbo = sys.exc_info()[2]
4687d670a3cSBarry Smith    emsg = str(e)
46942351d26SSatish Balay    if not emsg.endswith('\n'): emsg = emsg+'\n'
470a0022257SSatish Balay    msg ='*******************************************************************************\n'\
471*edd0a2d9SBarry Smith    +'    TypeError or ValueError possibly related to ERROR in COMMAND LINE ARGUMENT while running ./configure \n' \
472a0022257SSatish Balay    +'-------------------------------------------------------------------------------\n'  \
473a0022257SSatish Balay    +emsg+'*******************************************************************************\n'
4741a02243aSBarry Smith    se = ''
4755b6bfdb9SJed Brown  except ImportError as e :
476*edd0a2d9SBarry Smith    # this exception is automatically deleted by Python so we need to save it to print below
477*edd0a2d9SBarry Smith    tbo = sys.exc_info()[2]
4787d670a3cSBarry Smith    emsg = str(e)
47942351d26SSatish Balay    if not emsg.endswith('\n'): emsg = emsg+'\n'
480a0022257SSatish Balay    msg ='*******************************************************************************\n'\
481*edd0a2d9SBarry Smith    +'                     ImportError while runing ./configure \n' \
482a0022257SSatish Balay    +'-------------------------------------------------------------------------------\n'  \
483a0022257SSatish Balay    +emsg+'*******************************************************************************\n'
48496dc2fe8SMatthew Knepley    se = ''
4855b6bfdb9SJed Brown  except OSError as e :
48601def6f0SMatthew Knepley    emsg = str(e)
48701def6f0SMatthew Knepley    if not emsg.endswith('\n'): emsg = emsg+'\n'
488a0022257SSatish Balay    msg ='*******************************************************************************\n'\
489*edd0a2d9SBarry Smith    +'                    OSError while running ./configure \n' \
490a0022257SSatish Balay    +'-------------------------------------------------------------------------------\n'  \
491a0022257SSatish Balay    +emsg+'*******************************************************************************\n'
49201def6f0SMatthew Knepley    se = ''
4935b6bfdb9SJed Brown  except SystemExit as e:
494d7d3c4beSMatthew Knepley    if e.code is None or e.code == 0:
495d7d3c4beSMatthew Knepley      return
496c524ecbbSBarry Smith    if e.code is 10:
497c524ecbbSBarry Smith      sys.exit(10)
498a0022257SSatish Balay    msg ='*******************************************************************************\n'\
499b1dada7fSMatthew Knepley    +'         CONFIGURATION FAILURE  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
500a0022257SSatish Balay    +'*******************************************************************************\n'
501d7d3c4beSMatthew Knepley    se  = str(e)
5025b6bfdb9SJed Brown  except Exception as e:
503a0022257SSatish Balay    msg ='*******************************************************************************\n'\
504fe09c992SBarry Smith    +'        CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
505a0022257SSatish Balay    +'*******************************************************************************\n'
506e9f3bb17SBarry Smith    se  = str(e)
507e9f3bb17SBarry Smith
5085b6bfdb9SJed Brown  print(msg)
5099dd2fdb1SMatthew Knepley  if not framework is None:
5109dd2fdb1SMatthew Knepley    framework.logClear()
511e9f3bb17SBarry Smith    if hasattr(framework, 'log'):
51222c95ba3SMatthew G Knepley      try:
513d71f8ab3SSatish Balay        if hasattr(framework,'compilerDefines'):
514bd5137a2SBarry Smith          framework.log.write('**** Configure header '+framework.compilerDefines+' ****\n')
515febd46b0SSatish Balay          framework.outputHeader(framework.log)
516d71f8ab3SSatish Balay        if hasattr(framework,'compilerFixes'):
517bd5137a2SBarry Smith          framework.log.write('**** C specific Configure header '+framework.compilerFixes+' ****\n')
518febd46b0SSatish Balay          framework.outputCHeader(framework.log)
5195b6bfdb9SJed Brown      except Exception as e:
52022c95ba3SMatthew G Knepley        framework.log.write('Problem writing headers to log: '+str(e))
52138c5b55eSBarry Smith      if sys.exc_info()[2]: tbo = sys.exc_info()[2]
522b1dada7fSMatthew Knepley      try:
523f24f64feSBarry Smith        framework.log.write(msg+se)
52438c5b55eSBarry Smith        traceback.print_tb(tbo, file = framework.log)
525d93c4beeSSatish Balay        print_final_timestamp(framework)
526f73e6a6cSSatish Balay        if hasattr(framework,'log'): framework.log.close()
527da1d79b4SSatish Balay        move_configure_log(framework)
528957f4b51SBarry Smith      except Exception as e:
529957f4b51SBarry Smith        print('Error printing error message from exception or printing the traceback:'+str(e))
53038c5b55eSBarry Smith        traceback.print_tb(tbo)
531e9f3bb17SBarry Smith      sys.exit(1)
5325a74f024SMatthew Knepley    else:
5335b6bfdb9SJed Brown      print(se)
534957f4b51SBarry Smith      traceback.print_tb(sys.exc_info()[2])
535957f4b51SBarry Smith  else:
536957f4b51SBarry Smith    print(se)
5375a74f024SMatthew Knepley    traceback.print_tb(sys.exc_info()[2])
538f73e6a6cSSatish Balay  if hasattr(framework,'log'): framework.log.close()
5395d5a5a7bSMatthew Knepley
5405d5a5a7bSMatthew Knepleyif __name__ == '__main__':
541a030c540SBarry Smith  petsc_configure([])
542759acf64SBarry Smith
543