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