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