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