xref: /petsc/config/configure.py (revision 17403302f25e9421e1d9eccc4e2b2db185cb004e)
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_broken_configure_log_links():
296  '''Sometime symlinks can get broken if the original files are deleted. Delete such broken links'''
297  import os
298  for logfile in ['configure.log','configure.log.bkp']:
299    if os.path.islink(logfile) and not os.path.isfile(logfile): os.remove(logfile)
300  return
301
302def move_configure_log(framework):
303  '''Move configure.log to PETSC_ARCH/lib/petsc/conf - and update configure.log.bkp in both locations appropriately'''
304  global petsc_arch
305
306  if hasattr(framework,'arch'): petsc_arch = framework.arch
307  if hasattr(framework,'logName'): curr_file = framework.logName
308  else: curr_file = 'configure.log'
309
310  if petsc_arch:
311    import shutil
312    import os
313
314    # Just in case - confdir is not created
315    lib_dir = os.path.join(petsc_arch,'lib')
316    conf_dir = os.path.join(petsc_arch,'lib','petsc','conf')
317    if not os.path.isdir(petsc_arch): os.mkdir(petsc_arch)
318    if not os.path.isdir(lib_dir): os.mkdir(lib_dir)
319    if not os.path.isdir(conf_dir): os.mkdir(conf_dir)
320
321    curr_bkp  = curr_file + '.bkp'
322    new_file  = os.path.join(conf_dir,curr_file)
323    new_bkp   = new_file + '.bkp'
324
325    # Keep backup in $PETSC_ARCH/lib/petsc/conf location
326    if os.path.isfile(new_bkp): os.remove(new_bkp)
327    if os.path.isfile(new_file): os.rename(new_file,new_bkp)
328    if os.path.isfile(curr_file):
329      shutil.copyfile(curr_file,new_file)
330      os.remove(curr_file)
331    if os.path.isfile(new_file): os.symlink(new_file,curr_file)
332    # If the old bkp is using the same PETSC_ARCH/lib/petsc/conf - then update bkp link
333    if os.path.realpath(curr_bkp) == os.path.realpath(new_file):
334      if os.path.isfile(curr_bkp): os.remove(curr_bkp)
335      if os.path.isfile(new_bkp): os.symlink(new_bkp,curr_bkp)
336  return
337
338def print_final_timestamp(framework):
339  import time
340  framework.log.write(('='*80)+'\n')
341  framework.log.write('Finishing configure run at '+time.strftime('%a, %d %b %Y %H:%M:%S %z')+'\n')
342  framework.log.write(('='*80)+'\n')
343  return
344
345def petsc_configure(configure_options):
346  if 'PETSC_DIR' in os.environ:
347    petscdir = os.environ['PETSC_DIR']
348    if petscdir.find(' ') > -1:
349      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')
350    try:
351      sys.path.append(os.path.join(petscdir,'lib','petsc','bin'))
352      import petscnagupgrade
353      file     = os.path.join(petscdir,'.nagged')
354      if not petscnagupgrade.naggedtoday(file):
355        petscnagupgrade.currentversion(petscdir)
356    except:
357      pass
358  print('===============================================================================')
359  print('             Configuring PETSc to compile on your system                       ')
360  print('===============================================================================')
361
362  try:
363    # Command line arguments take precedence (but don't destroy argv[0])
364    sys.argv = sys.argv[:1] + configure_options + sys.argv[1:]
365    check_for_option_mistakes(sys.argv)
366    check_for_option_changed(sys.argv)
367  except (TypeError, ValueError) as e:
368    emsg = str(e)
369    if not emsg.endswith('\n'): emsg = emsg+'\n'
370    msg ='*******************************************************************************\n'\
371    +'                ERROR in COMMAND LINE ARGUMENT to ./configure \n' \
372    +'-------------------------------------------------------------------------------\n'  \
373    +emsg+'*******************************************************************************\n'
374    sys.exit(msg)
375  # check PETSC_ARCH
376  check_for_unsupported_combinations(sys.argv)
377  check_petsc_arch(sys.argv)
378  check_broken_configure_log_links()
379
380  #rename '--enable-' to '--with-'
381  chkenable()
382  # support a few standard configure option types
383  chksynonyms()
384  # Check for broken cygwin
385  chkbrokencygwin()
386  # Disable threads on RHL9
387  chkrhl9()
388  # Make sure cygwin-python is used on windows
389  chkusingwindowspython()
390  # Threads don't work for cygwin & python...
391  chkcygwinpython()
392  chkcygwinlink()
393  chkdosfiles()
394  chkcygwinwindowscompilers()
395  chktmpnoexec()
396
397  for l in range(1,len(sys.argv)):
398    if sys.argv[l].startswith('--with-fc=') and sys.argv[l].endswith('nagfor'):
399      # need a way to save this value and later CC so that petscnagfor may use them
400      name = sys.argv[l].split('=')[1]
401      sys.argv[l] = '--with-fc='+os.path.join(os.path.abspath('.'),'lib','petsc','bin','petscnagfor')
402      break
403
404
405  # Should be run from the toplevel
406  configDir = os.path.abspath('config')
407  bsDir     = os.path.join(configDir, 'BuildSystem')
408  if not os.path.isdir(configDir):
409    raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.'))
410  sys.path.insert(0, bsDir)
411  sys.path.insert(0, configDir)
412  import config.base
413  import config.framework
414  import pickle
415  import traceback
416
417  tbo = None
418  framework = None
419  try:
420    framework = config.framework.Framework(['--configModules=PETSc.Configure','--optionsModule=config.compilerOptions']+sys.argv[1:], loadArgDB = 0)
421    framework.setup()
422    framework.logPrint('\n'.join(extraLogs))
423    framework.configure(out = sys.stdout)
424    framework.storeSubstitutions(framework.argDB)
425    framework.argDB['configureCache'] = pickle.dumps(framework)
426    framework.printSummary()
427    framework.argDB.save(force = True)
428    framework.logClear()
429    print_final_timestamp(framework)
430    framework.closeLog()
431    try:
432      move_configure_log(framework)
433    except:
434      # perhaps print an error about unable to shuffle logs?
435      pass
436    return 0
437  except (RuntimeError, config.base.ConfigureSetupError) as e:
438    emsg = str(e)
439    if not emsg.endswith('\n'): emsg = emsg+'\n'
440    msg ='*******************************************************************************\n'\
441    +'         UNABLE to CONFIGURE with GIVEN OPTIONS    (see configure.log for details):\n' \
442    +'-------------------------------------------------------------------------------\n'  \
443    +emsg+'*******************************************************************************\n'
444    se = ''
445  except (TypeError, ValueError) as e:
446    # this exception is automatically deleted by Python so we need to save it to print below
447    tbo = sys.exc_info()[2]
448    emsg = str(e)
449    if not emsg.endswith('\n'): emsg = emsg+'\n'
450    msg ='*******************************************************************************\n'\
451    +'    TypeError or ValueError possibly related to ERROR in COMMAND LINE ARGUMENT to ./configure \n' \
452    +'-------------------------------------------------------------------------------\n'  \
453    +emsg+'*******************************************************************************\n'
454    se = ''
455  except ImportError as e :
456    emsg = str(e)
457    if not emsg.endswith('\n'): emsg = emsg+'\n'
458    msg ='*******************************************************************************\n'\
459    +'                     UNABLE to FIND MODULE for ./configure \n' \
460    +'-------------------------------------------------------------------------------\n'  \
461    +emsg+'*******************************************************************************\n'
462    se = ''
463  except OSError as e :
464    emsg = str(e)
465    if not emsg.endswith('\n'): emsg = emsg+'\n'
466    msg ='*******************************************************************************\n'\
467    +'                    UNABLE to EXECUTE BINARIES for ./configure \n' \
468    +'-------------------------------------------------------------------------------\n'  \
469    +emsg+'*******************************************************************************\n'
470    se = ''
471  except SystemExit as e:
472    if e.code is None or e.code == 0:
473      return
474    if e.code is 10:
475      sys.exit(10)
476    msg ='*******************************************************************************\n'\
477    +'         CONFIGURATION FAILURE  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
478    +'*******************************************************************************\n'
479    se  = str(e)
480  except Exception as e:
481    msg ='*******************************************************************************\n'\
482    +'        CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
483    +'*******************************************************************************\n'
484    se  = str(e)
485
486  print(msg)
487  if not framework is None:
488    framework.logClear()
489    if hasattr(framework, 'log'):
490      try:
491        if hasattr(framework,'compilerDefines'):
492          framework.log.write('**** Configure header '+framework.compilerDefines+' ****\n')
493          framework.outputHeader(framework.log)
494        if hasattr(framework,'compilerFixes'):
495          framework.log.write('**** C specific Configure header '+framework.compilerFixes+' ****\n')
496          framework.outputCHeader(framework.log)
497      except Exception as e:
498        framework.log.write('Problem writing headers to log: '+str(e))
499      if sys.exc_info()[2]: tbo = sys.exc_info()[2]
500      try:
501        framework.log.write(msg+se)
502        traceback.print_tb(tbo, file = framework.log)
503        print_final_timestamp(framework)
504        if hasattr(framework,'log'): framework.log.close()
505        move_configure_log(framework)
506      except Exception as e:
507        print('Error printing error message from exception or printing the traceback:'+str(e))
508        traceback.print_tb(tbo)
509      sys.exit(1)
510    else:
511      print(se)
512      traceback.print_tb(sys.exc_info()[2])
513  else:
514    print(se)
515    traceback.print_tb(sys.exc_info()[2])
516  if hasattr(framework,'log'): framework.log.close()
517
518if __name__ == '__main__':
519  petsc_configure([])
520
521