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