xref: /petsc/config/configure.py (revision 1873fc9fedfc3bd8fc2f82ccd29ab8b44a80f1db)
1#!/usr/bin/env python
2from __future__ import print_function
3import os, sys
4
5extraLogs = []
6petsc_arch = ''
7
8# Use en_US as language so that BuildSystem parses compiler messages in english
9if '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'
10if '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'
11
12if sys.version_info < (2,6):
13  print('************************************************************************')
14  print('*      Python version 2.6+ or 3.4+ is required to run ./configure      *')
15  print('*         Try: "python2.7 ./configure" or "python3 ./configure"        *')
16  print('************************************************************************')
17  sys.exit(4)
18
19def check_for_option_mistakes(opts):
20  for opt in opts[1:]:
21    name = opt.split('=')[0]
22    if name.find('_') >= 0:
23      exception = False
24      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']:
25        if name.find(exc) >= 0:
26          exception = True
27      if not exception:
28        raise ValueError('The option '+name+' should probably be '+name.replace('_', '-'));
29    if opt.find('=') >=0:
30      optval = opt.split('=')[1]
31      if optval == 'ifneeded':
32        raise ValueError('The option '+opt+' should probably be '+opt.replace('ifneeded', '1'));
33    for exc in ['mkl_sparse', 'mkl_sparse_optimize', 'mkl_cpardiso', 'mkl_pardiso', 'superlu_dist']:
34      if name.find(exc.replace('_','-')) > -1:
35        raise ValueError('The option '+opt+' should be '+opt.replace(exc.replace('_','-'),exc));
36  return
37
38def check_for_unsupported_combinations(opts):
39  if '--with-precision=single' in opts and '--with-clanguage=cxx' in opts and '--with-scalar-type=complex' in opts:
40    sys.exit(ValueError('PETSc does not support single precision complex with C++ clanguage, run with --with-clanguage=c'))
41
42def check_for_option_changed(opts):
43# Document changes in command line options here.
44  optMap = [('with-64bit-indices','with-64-bit-indices'),
45            ('with-mpi-exec','with-mpiexec'),
46            ('c-blas-lapack','f2cblaslapack'),
47            ('cholmod','suitesparse'),
48            ('umfpack','suitesparse'),
49            ('f-blas-lapack','fblaslapack'),
50            ('with-cuda-arch',
51             'CUDAFLAGS=-arch'),
52            ('with-packages-dir','with-packages-download-dir'),
53            ('with-external-packages-dir','with-packages-build-dir'),
54            ('package-dirs','with-packages-search-path'),
55            ('download-petsc4py-python','with-python-exec'),
56            ('search-dirs','with-executables-search-path')]
57  for opt in opts[1:]:
58    optname = opt.split('=')[0].strip('-')
59    for oldname,newname in optMap:
60      if optname.find(oldname) >=0:
61        raise ValueError('The option '+opt+' should probably be '+opt.replace(oldname,newname))
62  return
63
64def check_petsc_arch(opts):
65  # If PETSC_ARCH not specified - use script name (if not configure.py)
66  global petsc_arch
67  found = 0
68  for name in opts:
69    if name.find('PETSC_ARCH=') >= 0:
70      petsc_arch=name.split('=')[1]
71      found = 1
72      break
73  # If not yet specified - use the filename of script
74  if not found:
75      filename = os.path.basename(sys.argv[0])
76      if not filename.startswith('configure') and not filename.startswith('reconfigure') and not filename.startswith('setup'):
77        petsc_arch=os.path.splitext(os.path.basename(sys.argv[0]))[0]
78        useName = 'PETSC_ARCH='+petsc_arch
79        opts.append(useName)
80  return 0
81
82def chkenable():
83  #Replace all 'enable-'/'disable-' with 'with-'=0/1/tail
84  #enable-fortran is a special case, the resulting --with-fortran is ambiguous.
85  #Would it mean --with-fc=
86  en_dash = u'\N{EN DASH}'
87  if sys.version_info < (3, 0):
88    en_dash = en_dash.encode('utf-8')
89  for l in range(0,len(sys.argv)):
90    name = sys.argv[l]
91
92    if name.find(en_dash)  >= 0:
93      sys.argv[l] = name.replace(en_dash,'-')
94    if name.find('enable-cxx') >= 0:
95      if name.find('=') == -1:
96        sys.argv[l] = name.replace('enable-cxx','with-clanguage=C++')
97      else:
98        head, tail = name.split('=', 1)
99        if tail=='0':
100          sys.argv[l] = head.replace('enable-cxx','with-clanguage=C')
101        else:
102          sys.argv[l] = head.replace('enable-cxx','with-clanguage=C++')
103      continue
104    if name.find('disable-cxx') >= 0:
105      if name.find('=') == -1:
106        sys.argv[l] = name.replace('disable-cxx','with-clanguage=C')
107      else:
108        head, tail = name.split('=', 1)
109        if tail == '0':
110          sys.argv[l] = head.replace('disable-cxx','with-clanguage=C++')
111        else:
112          sys.argv[l] = head.replace('disable-cxx','with-clanguage=C')
113      continue
114
115
116    if name.find('enable-') >= 0:
117      if name.find('=') == -1:
118        sys.argv[l] = name.replace('enable-','with-')+'=1'
119      else:
120        head, tail = name.split('=', 1)
121        sys.argv[l] = head.replace('enable-','with-')+'='+tail
122    if name.find('disable-') >= 0:
123      if name.find('=') == -1:
124        sys.argv[l] = name.replace('disable-','with-')+'=0'
125      else:
126        head, tail = name.split('=', 1)
127        if tail == '1': tail = '0'
128        sys.argv[l] = head.replace('disable-','with-')+'='+tail
129    if name.find('without-') >= 0:
130      if name.find('=') == -1:
131        sys.argv[l] = name.replace('without-','with-')+'=0'
132      else:
133        head, tail = name.split('=', 1)
134        if tail == '1': tail = '0'
135        sys.argv[l] = head.replace('without-','with-')+'='+tail
136
137def chksynonyms():
138  #replace common configure options with ones that PETSc BuildSystem recognizes
139  simplereplacements = {'F77' : 'FC', 'F90' : 'FC'}
140  for l in range(0,len(sys.argv)):
141    name = sys.argv[l]
142
143    if name.find('with-blas-lapack') >= 0:
144      sys.argv[l] = name.replace('with-blas-lapack','with-blaslapack')
145
146    if name.find('with-debug=') >= 0 or name.endswith('with-debug'):
147      if name.find('=') == -1:
148        sys.argv[l] = name.replace('with-debug','with-debugging')+'=1'
149      else:
150        head, tail = name.split('=', 1)
151        sys.argv[l] = head.replace('with-debug','with-debugging')+'='+tail
152
153    if name.find('with-shared=') >= 0 or name.endswith('with-shared'):
154      if name.find('=') == -1:
155        sys.argv[l] = name.replace('with-shared','with-shared-libraries')+'=1'
156      else:
157        head, tail = name.split('=', 1)
158        sys.argv[l] = head.replace('with-shared','with-shared-libraries')+'='+tail
159
160    if name.find('with-index-size=') >=0:
161      head,tail = name.split('=',1)
162      if int(tail)==32:
163        sys.argv[l] = '--with-64-bit-indices=0'
164      elif int(tail)==64:
165        sys.argv[l] = '--with-64-bit-indices=1'
166      else:
167        raise RuntimeError('--with-index-size= must be 32 or 64')
168
169    if name.find('with-precision=') >=0:
170      head,tail = name.split('=',1)
171      if tail.find('quad')>=0:
172        sys.argv[l]='--with-precision=__float128'
173
174    for i,j in simplereplacements.items():
175      if name.find(i+'=') >= 0:
176        sys.argv[l] = name.replace(i+'=',j+'=')
177      elif name.find('with-'+i.lower()+'=') >= 0:
178        sys.argv[l] = name.replace(i.lower()+'=',j.lower()+'=')
179
180def chkwinf90():
181  for arg in sys.argv:
182    if (arg.find('win32fe') >= 0 and (arg.find('f90') >=0 or arg.find('ifort') >=0)):
183      return 1
184  return 0
185
186def chkdosfiles():
187  # cygwin - but not a hg clone - so check one of files in bin dir
188  if b"\r\n" in open(os.path.join('lib','petsc','bin','petscmpiexec'),"rb").read():
189    print('===============================================================================')
190    print(' *** Scripts are in DOS mode. Was winzip used to extract petsc sources?    ****')
191    print(' *** Please restart with a fresh tarball and use "tar -xzf petsc.tar.gz"   ****')
192    print('===============================================================================')
193    sys.exit(3)
194  return
195
196def chkcygwinlink():
197  if os.path.exists('/usr/bin/cygcheck.exe') and os.path.exists('/usr/bin/link.exe') and chkwinf90():
198      if '--ignore-cygwin-link' in sys.argv: return 0
199      print('===============================================================================')
200      print(' *** Cygwin /usr/bin/link detected! Compiles with CVF/Intel f90 can break!  **')
201      print(' *** To workarround do: "mv /usr/bin/link.exe /usr/bin/link-cygwin.exe"     **')
202      print(' *** Or to ignore this check, use configure option: --ignore-cygwin-link    **')
203      print('===============================================================================')
204      sys.exit(3)
205  return 0
206
207def chkbrokencygwin():
208  if os.path.exists('/usr/bin/cygcheck.exe'):
209    buf = os.popen('/usr/bin/cygcheck.exe -c cygwin').read()
210    if buf.find('1.5.11-1') > -1:
211      print('===============================================================================')
212      print(' *** cygwin-1.5.11-1 detected. ./configure fails with this version ***')
213      print(' *** Please upgrade to cygwin-1.5.12-1 or newer version. This can  ***')
214      print(' *** be done by running cygwin-setup, selecting "next" all the way.***')
215      print('===============================================================================')
216      sys.exit(3)
217  return 0
218
219def chkusingwindowspython():
220  if sys.platform == 'win32':
221    print('===============================================================================')
222    print(' *** Windows python detected. Please rerun ./configure with cygwin-python. ***')
223    print('===============================================================================')
224    sys.exit(3)
225  return 0
226
227def chkcygwinpython():
228  if sys.platform == 'cygwin' :
229    import platform
230    import re
231    r=re.compile("([0-9]+).([0-9]+).([0-9]+)")
232    m=r.match(platform.release())
233    major=int(m.group(1))
234    minor=int(m.group(2))
235    subminor=int(m.group(3))
236    if ((major < 1) or (major == 1 and minor < 7) or (major == 1 and minor == 7 and subminor < 34)):
237      sys.argv.append('--useThreads=0')
238      extraLogs.append('''\
239===============================================================================
240** Cygwin version is older than 1.7.34. Python threads do not work correctly. ***
241** Disabling thread usage for this run of ./configure *******
242===============================================================================''')
243  return 0
244
245def chkcygwinwindowscompilers():
246  '''Adds win32fe for Microsoft/Intel compilers'''
247  if os.path.exists('/usr/bin/cygcheck.exe'):
248    for l in range(1,len(sys.argv)):
249      option = sys.argv[l]
250      for i in ['cl','icl','ifort']:
251        if option.startswith(i):
252          sys.argv[l] = 'win32fe '+option
253          break
254  return 0
255
256def chkrhl9():
257  if os.path.exists('/etc/redhat-release'):
258    try:
259      file = open('/etc/redhat-release','r')
260      buf = file.read()
261      file.close()
262    except:
263      # can't read file - assume dangerous RHL9
264      buf = 'Shrike'
265    if buf.find('Shrike') > -1:
266      sys.argv.append('--useThreads=0')
267      extraLogs.append('''\
268==============================================================================
269   *** RHL9 detected. Threads do not work correctly with this distribution ***
270   ****** Disabling thread usage for this run of ./configure *********
271===============================================================================''')
272  return 0
273
274def chktmpnoexec():
275  if not hasattr(os,'ST_NOEXEC'): return
276  if 'TMPDIR' in os.environ: tmpDir = os.environ['TMPDIR']
277  else: tmpDir = '/tmp'
278  if os.statvfs(tmpDir).f_flag & os.ST_NOEXEC:
279    if os.statvfs(os.path.abspath('.')).f_flag & os.ST_NOEXEC:
280      print('************************************************************************')
281      print('* TMPDIR '+tmpDir+' has noexec attribute. Same with '+os.path.abspath('.')+' where petsc is built.')
282      print('* Suggest building PETSc in a location without this restriction!')
283      print('* Alternatively, set env variable TMPDIR to a location that is not restricted to run binaries.')
284      print('************************************************************************')
285      sys.exit(4)
286    else:
287      newTmp = os.path.abspath('tmp-petsc')
288      print('************************************************************************')
289      print('* TMPDIR '+tmpDir+' has noexec attribute. Using '+newTmp+' instead.')
290      print('************************************************************************')
291      if not os.path.isdir(newTmp): os.mkdir(os.path.abspath(newTmp))
292      os.environ['TMPDIR'] = newTmp
293  return
294
295def check_cray_modules():
296  import script
297  '''For Cray systems check if the cc, CC, ftn compiler suite modules have been set'''
298  cray = os.getenv('CRAY_SITE_LIST_DIR')
299  if not cray: return
300  cray = os.getenv('CRAYPE_DIR')
301  if not cray:
302   print('************************************************************************')
303   print('* You are on a Cray system but no programming environments have been loaded')
304   print('* Perhaps you need:')
305   print('*       module load intel ; module load PrgEnv-intel')
306   print('*   or  module load PrgEnv-cray')
307   print('*   or  module load PrgEnv-gnu')
308   print('* See https://www.mcs.anl.gov/petsc/documentation/installation.html#doemachines')
309   print('************************************************************************')
310   sys.exit(4)
311
312def check_broken_configure_log_links():
313  '''Sometime symlinks can get broken if the original files are deleted. Delete such broken links'''
314  import os
315  for logfile in ['configure.log','configure.log.bkp']:
316    if os.path.islink(logfile) and not os.path.isfile(logfile): os.remove(logfile)
317  return
318
319def move_configure_log(framework):
320  '''Move configure.log to PETSC_ARCH/lib/petsc/conf - and update configure.log.bkp in both locations appropriately'''
321  global petsc_arch
322
323  if hasattr(framework,'arch'): petsc_arch = framework.arch
324  if hasattr(framework,'logName'): curr_file = framework.logName
325  else: curr_file = 'configure.log'
326
327  if petsc_arch:
328    import shutil
329    import os
330
331    # Just in case - confdir is not created
332    lib_dir = os.path.join(petsc_arch,'lib')
333    conf_dir = os.path.join(petsc_arch,'lib','petsc','conf')
334    if not os.path.isdir(petsc_arch): os.mkdir(petsc_arch)
335    if not os.path.isdir(lib_dir): os.mkdir(lib_dir)
336    if not os.path.isdir(conf_dir): os.mkdir(conf_dir)
337
338    curr_bkp  = curr_file + '.bkp'
339    new_file  = os.path.join(conf_dir,curr_file)
340    new_bkp   = new_file + '.bkp'
341
342    # Keep backup in $PETSC_ARCH/lib/petsc/conf location
343    if os.path.isfile(new_bkp): os.remove(new_bkp)
344    if os.path.isfile(new_file): os.rename(new_file,new_bkp)
345    if os.path.isfile(curr_file):
346      shutil.copyfile(curr_file,new_file)
347      os.remove(curr_file)
348    if os.path.isfile(new_file): os.symlink(new_file,curr_file)
349    # If the old bkp is using the same PETSC_ARCH/lib/petsc/conf - then update bkp link
350    if os.path.realpath(curr_bkp) == os.path.realpath(new_file):
351      if os.path.isfile(curr_bkp): os.remove(curr_bkp)
352      if os.path.isfile(new_bkp): os.symlink(new_bkp,curr_bkp)
353  return
354
355def print_final_timestamp(framework):
356  import time
357  framework.log.write(('='*80)+'\n')
358  framework.log.write('Finishing configure run at '+time.strftime('%a, %d %b %Y %H:%M:%S %z')+'\n')
359  framework.log.write(('='*80)+'\n')
360  return
361
362def petsc_configure(configure_options):
363  if 'PETSC_DIR' in os.environ:
364    petscdir = os.environ['PETSC_DIR']
365    if petscdir.find(' ') > -1:
366      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')
367    try:
368      sys.path.append(os.path.join(petscdir,'lib','petsc','bin'))
369      import petscnagupgrade
370      file     = os.path.join(petscdir,'.nagged')
371      if not petscnagupgrade.naggedtoday(file):
372        petscnagupgrade.currentversion(petscdir)
373    except:
374      pass
375  print('===============================================================================')
376  print('             Configuring PETSc to compile on your system                       ')
377  print('===============================================================================')
378
379  try:
380    # Command line arguments take precedence (but don't destroy argv[0])
381    sys.argv = sys.argv[:1] + configure_options + sys.argv[1:]
382    check_for_option_mistakes(sys.argv)
383    check_for_option_changed(sys.argv)
384  except (TypeError, ValueError) as e:
385    emsg = str(e)
386    if not emsg.endswith('\n'): emsg = emsg+'\n'
387    msg ='*******************************************************************************\n'\
388    +'                ERROR in COMMAND LINE ARGUMENT to ./configure \n' \
389    +'-------------------------------------------------------------------------------\n'  \
390    +emsg+'*******************************************************************************\n'
391    sys.exit(msg)
392  # check PETSC_ARCH
393  check_for_unsupported_combinations(sys.argv)
394  check_petsc_arch(sys.argv)
395  check_broken_configure_log_links()
396
397  #rename '--enable-' to '--with-'
398  chkenable()
399  # support a few standard configure option types
400  chksynonyms()
401  # Check for broken cygwin
402  chkbrokencygwin()
403  # Disable threads on RHL9
404  chkrhl9()
405  # Make sure cygwin-python is used on windows
406  chkusingwindowspython()
407  # Threads don't work for cygwin & python...
408  chkcygwinpython()
409  chkcygwinlink()
410  chkdosfiles()
411  chkcygwinwindowscompilers()
412  chktmpnoexec()
413
414  for l in range(1,len(sys.argv)):
415    if sys.argv[l].startswith('--with-fc=') and sys.argv[l].endswith('nagfor'):
416      # need a way to save this value and later CC so that petscnagfor may use them
417      name = sys.argv[l].split('=')[1]
418      sys.argv[l] = '--with-fc='+os.path.join(os.path.abspath('.'),'lib','petsc','bin','petscnagfor')
419      break
420
421
422  # Should be run from the toplevel
423  configDir = os.path.abspath('config')
424  bsDir     = os.path.join(configDir, 'BuildSystem')
425  if not os.path.isdir(configDir):
426    raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.'))
427  sys.path.insert(0, bsDir)
428  sys.path.insert(0, configDir)
429  import config.base
430  import config.framework
431  import pickle
432  import traceback
433
434  # Check Cray without modules
435  check_cray_modules()
436
437  tbo = None
438  framework = None
439  try:
440    framework = config.framework.Framework(['--configModules=PETSc.Configure','--optionsModule=config.compilerOptions']+sys.argv[1:], loadArgDB = 0)
441    framework.setup()
442    framework.logPrint('\n'.join(extraLogs))
443    framework.configure(out = sys.stdout)
444    framework.storeSubstitutions(framework.argDB)
445    framework.argDB['configureCache'] = pickle.dumps(framework)
446    framework.printSummary()
447    framework.argDB.save(force = True)
448    framework.logClear()
449    print_final_timestamp(framework)
450    framework.closeLog()
451    try:
452      move_configure_log(framework)
453    except:
454      # perhaps print an error about unable to shuffle logs?
455      pass
456    return 0
457  except (RuntimeError, config.base.ConfigureSetupError) as e:
458    emsg = str(e)
459    if not emsg.endswith('\n'): emsg = emsg+'\n'
460    msg ='*******************************************************************************\n'\
461    +'         UNABLE to CONFIGURE with GIVEN OPTIONS    (see configure.log for details):\n' \
462    +'-------------------------------------------------------------------------------\n'  \
463    +emsg+'*******************************************************************************\n'
464    se = ''
465  except (TypeError, ValueError) as e:
466    # this exception is automatically deleted by Python so we need to save it to print below
467    tbo = sys.exc_info()[2]
468    emsg = str(e)
469    if not emsg.endswith('\n'): emsg = emsg+'\n'
470    msg ='*******************************************************************************\n'\
471    +'    TypeError or ValueError possibly related to ERROR in COMMAND LINE ARGUMENT while running ./configure \n' \
472    +'-------------------------------------------------------------------------------\n'  \
473    +emsg+'*******************************************************************************\n'
474    se = ''
475  except ImportError as e :
476    # this exception is automatically deleted by Python so we need to save it to print below
477    tbo = sys.exc_info()[2]
478    emsg = str(e)
479    if not emsg.endswith('\n'): emsg = emsg+'\n'
480    msg ='*******************************************************************************\n'\
481    +'                     ImportError while runing ./configure \n' \
482    +'-------------------------------------------------------------------------------\n'  \
483    +emsg+'*******************************************************************************\n'
484    se = ''
485  except OSError as e :
486    emsg = str(e)
487    if not emsg.endswith('\n'): emsg = emsg+'\n'
488    msg ='*******************************************************************************\n'\
489    +'                    OSError while running ./configure \n' \
490    +'-------------------------------------------------------------------------------\n'  \
491    +emsg+'*******************************************************************************\n'
492    se = ''
493  except SystemExit as e:
494    if e.code is None or e.code == 0:
495      return
496    if e.code is 10:
497      sys.exit(10)
498    msg ='*******************************************************************************\n'\
499    +'         CONFIGURATION FAILURE  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
500    +'*******************************************************************************\n'
501    se  = str(e)
502  except Exception as e:
503    msg ='*******************************************************************************\n'\
504    +'        CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
505    +'*******************************************************************************\n'
506    se  = str(e)
507
508  print(msg)
509  if not framework is None:
510    framework.logClear()
511    if hasattr(framework, 'log'):
512      try:
513        if hasattr(framework,'compilerDefines'):
514          framework.log.write('**** Configure header '+framework.compilerDefines+' ****\n')
515          framework.outputHeader(framework.log)
516        if hasattr(framework,'compilerFixes'):
517          framework.log.write('**** C specific Configure header '+framework.compilerFixes+' ****\n')
518          framework.outputCHeader(framework.log)
519      except Exception as e:
520        framework.log.write('Problem writing headers to log: '+str(e))
521      if sys.exc_info()[2]: tbo = sys.exc_info()[2]
522      try:
523        framework.log.write(msg+se)
524        traceback.print_tb(tbo, file = framework.log)
525        print_final_timestamp(framework)
526        if hasattr(framework,'log'): framework.log.close()
527        move_configure_log(framework)
528      except Exception as e:
529        print('Error printing error message from exception or printing the traceback:'+str(e))
530        traceback.print_tb(tbo)
531      sys.exit(1)
532    else:
533      print(se)
534      traceback.print_tb(sys.exc_info()[2])
535  else:
536    print(se)
537    traceback.print_tb(sys.exc_info()[2])
538  if hasattr(framework,'log'): framework.log.close()
539
540if __name__ == '__main__':
541  petsc_configure([])
542
543