xref: /petsc/config/configure.py (revision b0b472b02bae8575694e468aba26ed94309ce2c4)
15d5a5a7bSMatthew Knepley#!/usr/bin/env python
25d5a5a7bSMatthew Knepleyimport os
35d5a5a7bSMatthew Knepleyimport sys
44f8a5b45SBarry Smithimport commands
5a1eda5bfSSatish Balay# to load ~/.pythonrc.py before inserting correct BuildSystem to path
6a1eda5bfSSatish Balayimport user
77c9abfe7SSatish BalayextraLogs = []
8*b0b472b0SSatish Balaypetsc_arch = ''
94b8aa89bSBarry Smith
10378f148eSBarry Smithif not hasattr(sys, 'version_info') or not sys.version_info[1] >= 2 or not sys.version_info[0] >= 2:
11495ffa62SBarry Smith  print '**** You must have Python version 2.2 or higher to run config/configure.py ******'
12495ffa62SBarry Smith  print '*           Python is easy to install for end users or sys-admin.               *'
1332077d6dSBarry Smith  print '*                   http://www.python.org/download/                             *'
1432077d6dSBarry Smith  print '*                                                                               *'
15495ffa62SBarry Smith  print '*            You CANNOT configure PETSc without Python                          *'
16495ffa62SBarry Smith  print '*    http://www.mcs.anl.gov/petsc/petsc-as/documentation/installation.html      *'
1732077d6dSBarry Smith  print '*********************************************************************************'
18b26a8723SBarry Smith  sys.exit(4)
192fb34ac0SMatthew Knepley
2059e9bfd6SSatish Balaydef check_petsc_arch(opts):
21c43ea0feSSatish Balay  # If PETSC_ARCH not specified - use script name (if not configure.py)
22*b0b472b0SSatish Balay  global petsc_arch
23c43ea0feSSatish Balay  found = 0
2459e9bfd6SSatish Balay  for name in opts:
25c43ea0feSSatish Balay    if name.find('PETSC_ARCH=') >= 0:
26*b0b472b0SSatish Balay      petsc_arch=name.split('=')[1]
27c43ea0feSSatish Balay      found = 1
2859e9bfd6SSatish Balay      break
2959e9bfd6SSatish Balay  # If not yet specified - use the filename of script
30c43ea0feSSatish Balay  if not found:
3159e9bfd6SSatish Balay      filename = os.path.basename(sys.argv[0])
327eed1879SBarry Smith      if not filename.startswith('configure') and not filename.startswith('reconfigure'):
33*b0b472b0SSatish Balay        petsc_arch=os.path.splitext(os.path.basename(sys.argv[0]))[0]
34*b0b472b0SSatish Balay        useName = 'PETSC_ARCH='+petsc_arch
3559e9bfd6SSatish Balay        opts.append(useName)
361937db7aSSatish Balay  return 0
374b8aa89bSBarry Smith
3885ef4d1eSSatish Balaydef chkbrokencygwin():
399dabcff0SSatish Balay  if os.path.exists('/usr/bin/cygcheck.exe'):
409dabcff0SSatish Balay    buf = os.popen('/usr/bin/cygcheck.exe -c cygwin').read()
419dabcff0SSatish Balay    if buf.find('1.5.11-1') > -1:
421937db7aSSatish Balay      print '================================================================================='
431937db7aSSatish Balay      print ' *** cygwin-1.5.11-1 detected. config/configure.py fails with this version   ***'
441937db7aSSatish Balay      print ' *** Please upgrade to cygwin-1.5.12-1 or newer version. This can  ***'
451937db7aSSatish Balay      print ' *** be done by running cygwin-setup, selecting "next" all the way.***'
461937db7aSSatish Balay      print '================================================================================='
471937db7aSSatish Balay      sys.exit(3)
489dabcff0SSatish Balay  return 0
499dabcff0SSatish Balay
5085ef4d1eSSatish Balaydef chkusingwindowspython():
511937db7aSSatish Balay  if os.path.exists('/usr/bin/cygcheck.exe') and sys.platform != 'cygwin':
521937db7aSSatish Balay    print '================================================================================='
531937db7aSSatish Balay    print ' *** Non-cygwin python detected. Please rerun config/configure.py with cygwin-python ***'
541937db7aSSatish Balay    print '================================================================================='
551937db7aSSatish Balay    sys.exit(3)
5685ef4d1eSSatish Balay  return 0
5785ef4d1eSSatish Balay
5885ef4d1eSSatish Balaydef chkcygwinpythonver():
5971384062SSatish Balay  if os.path.exists('/usr/bin/cygcheck.exe'):
6071384062SSatish Balay    buf = os.popen('/usr/bin/cygcheck.exe -c python').read()
61c4b7e894SSatish Balay    if (buf.find('2.4') > -1) or (buf.find('2.5') > -1) or (buf.find('2.6') > -1):
621937db7aSSatish Balay      sys.argv.append('--useThreads=0')
631937db7aSSatish Balay      extraLogs.append('''\
641937db7aSSatish Balay================================================================================
651937db7aSSatish Balay** Cygwin-python-2.4/2.5 detected. Threads do not work correctly with this version *
661937db7aSSatish Balay ********* Disabling thread usage for this run of config/configure.py **********
671937db7aSSatish Balay================================================================================''')
6871384062SSatish Balay  return 0
6971384062SSatish Balay
701937db7aSSatish Balaydef chkrhl9():
711937db7aSSatish Balay  if os.path.exists('/etc/redhat-release'):
72836c2c52SSatish Balay    try:
73594eb360SSatish Balay      file = open('/etc/redhat-release','r')
74836c2c52SSatish Balay      buf = file.read()
75836c2c52SSatish Balay      file.close()
76836c2c52SSatish Balay    except:
77836c2c52SSatish Balay      # can't read file - assume dangerous RHL9
781937db7aSSatish Balay      buf = 'Shrike'
79836c2c52SSatish Balay    if buf.find('Shrike') > -1:
801937db7aSSatish Balay      sys.argv.append('--useThreads=0')
811937db7aSSatish Balay      extraLogs.append('''\
821937db7aSSatish Balay================================================================================
831937db7aSSatish Balay   *** RHL9 detected. Threads do not work correctly with this distribution ***
841937db7aSSatish Balay    ****** Disabling thread usage for this run of config/configure.py *******
851937db7aSSatish Balay================================================================================''')
86836c2c52SSatish Balay  return 0
87836c2c52SSatish Balay
88da1d79b4SSatish Balaydef move_configure_log(framework):
89da1d79b4SSatish Balay  '''Move configure.log to PETSC_ARCH/conf - and update configure.log.bkp in both locations appropriately'''
90*b0b472b0SSatish Balay  global petsc_arch
91*b0b472b0SSatish Balay
92*b0b472b0SSatish Balay  if hasattr(framework,'arch'): petsc_arch = framework.arch
93*b0b472b0SSatish Balay  if hasattr(framework,'logName'): curr_file = framework.logName
94*b0b472b0SSatish Balay  else: curr_file = 'configure.log'
95*b0b472b0SSatish Balay
96*b0b472b0SSatish Balay  if petsc_arch:
97da1d79b4SSatish Balay    import shutil
98da1d79b4SSatish Balay    import os
99*b0b472b0SSatish Balay
100*b0b472b0SSatish Balay    # Just in case - confdir is not created
101*b0b472b0SSatish Balay    conf_dir = os.path.join(petsc_arch,'conf')
102*b0b472b0SSatish Balay    if not os.path.isdir(petsc_arch): os.mkdir(petsc_arch)
103*b0b472b0SSatish Balay    if not os.path.isdir(conf_dir): os.mkdir(conf_dir)
104*b0b472b0SSatish Balay
105da1d79b4SSatish Balay    curr_bkp  = curr_file + '.bkp'
106*b0b472b0SSatish Balay    new_file  = os.path.join(conf_dir,curr_file)
107da1d79b4SSatish Balay    new_bkp   = new_file + '.bkp'
108da1d79b4SSatish Balay
109da1d79b4SSatish Balay    # Keep backup in $PETSC_ARCH/conf location
110da1d79b4SSatish Balay    if os.path.isfile(new_bkp): os.remove(new_bkp)
111da1d79b4SSatish Balay    if os.path.isfile(new_file): os.rename(new_file,new_bkp)
112da1d79b4SSatish Balay    shutil.move(curr_file,new_file)
113da1d79b4SSatish Balay    os.symlink(new_file,curr_file)
114da1d79b4SSatish Balay    # If the old bkp is using the same PETSC_ARCH/conf - then update bkp link
115da1d79b4SSatish Balay    if os.path.realpath(curr_bkp) == os.path.realpath(new_file):
116da1d79b4SSatish Balay      os.remove(curr_bkp)
117da1d79b4SSatish Balay      os.symlink(new_bkp,curr_bkp)
118da1d79b4SSatish Balay  return
119da1d79b4SSatish Balay
1205d5a5a7bSMatthew Knepleydef petsc_configure(configure_options):
12159e9bfd6SSatish Balay  print '================================================================================='
12259e9bfd6SSatish Balay  print '             Configuring PETSc to compile on your system                         '
12359e9bfd6SSatish Balay  print '================================================================================='
12459e9bfd6SSatish Balay
125c43ea0feSSatish Balay  # Command line arguments take precedence (but don't destroy argv[0])
126c43ea0feSSatish Balay  sys.argv = sys.argv[:1] + configure_options + sys.argv[1:]
12759e9bfd6SSatish Balay  # check PETSC_ARCH
12859e9bfd6SSatish Balay  check_petsc_arch(sys.argv)
1295fb2c094SBarry Smith
130c22cdea9SBarry Smith  # support a few standard configure option types
131ed6a7445SBarry Smith  for l in range(0,len(sys.argv)):
132c22cdea9SBarry Smith    name = sys.argv[l]
133637cc2ebSSatish Balay    if name.find('enable-') >= 0:
134193cd51eSMatthew Knepley      if name.find('=') == -1:
135193cd51eSMatthew Knepley        sys.argv[l] = name.replace('enable-','with-')+'=1'
136193cd51eSMatthew Knepley      else:
137193cd51eSMatthew Knepley        head, tail = name.split('=', 1)
138193cd51eSMatthew Knepley        sys.argv[l] = head.replace('enable-','with-')+'='+tail
139637cc2ebSSatish Balay    if name.find('disable-') >= 0:
140193cd51eSMatthew Knepley      if name.find('=') == -1:
141193cd51eSMatthew Knepley        sys.argv[l] = name.replace('disable-','with-')+'=0'
142193cd51eSMatthew Knepley      else:
143193cd51eSMatthew Knepley        head, tail = name.split('=', 1)
144193cd51eSMatthew Knepley        if tail == '1': tail = '0'
145193cd51eSMatthew Knepley        sys.argv[l] = head.replace('disable-','with-')+'='+tail
146637cc2ebSSatish Balay    if name.find('without-') >= 0:
147193cd51eSMatthew Knepley      if name.find('=') == -1:
148193cd51eSMatthew Knepley        sys.argv[l] = name.replace('without-','with-')+'=0'
149193cd51eSMatthew Knepley      else:
150193cd51eSMatthew Knepley        head, tail = name.split('=', 1)
151193cd51eSMatthew Knepley        if tail == '1': tail = '0'
152193cd51eSMatthew Knepley        sys.argv[l] = head.replace('without-','with-')+'='+tail
153adc3e427SMatthew Knepley
1549dabcff0SSatish Balay  # Check for broken cygwin
1551937db7aSSatish Balay  chkbrokencygwin()
156d65f3bddSMatthew Knepley  # Disable threads on RHL9
1571937db7aSSatish Balay  chkrhl9()
15885ef4d1eSSatish Balay  # Make sure cygwin-python is used on windows
1591937db7aSSatish Balay  chkusingwindowspython()
16085ef4d1eSSatish Balay  # Threads don't work for cygwin & python-2.4, 2.5 etc..
1611937db7aSSatish Balay  chkcygwinpythonver()
1629dabcff0SSatish Balay
16387282423SMatthew Knepley  # Should be run from the toplevel
164dbca6d9dSSatish Balay  configDir = os.path.abspath('config')
165f8833479SBarry Smith  bsDir     = os.path.join(configDir, 'BuildSystem')
166f8833479SBarry Smith  if not os.path.isdir(configDir):
1675d5a5a7bSMatthew Knepley    raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.'))
16887282423SMatthew Knepley  if not os.path.isdir(bsDir):
169ec1ee742SBarry Smith    print '================================================================================='
170dbca6d9dSSatish Balay    print '''++ Could not locate BuildSystem in %s.''' % configDir
171dbca6d9dSSatish Balay    print '''++ Downloading it using "hg clone http://hg.mcs.anl.gov/petsc/BuildSystem %s"''' % bsDir
172ec1ee742SBarry Smith    print '================================================================================='
173dbca6d9dSSatish Balay    (status,output) = commands.getstatusoutput('hg clone http://petsc.cs.iit.edu/petsc/BuildSystem '+ bsDir)
1747d7624c9SBarry Smith    if status:
1757d7624c9SBarry Smith      if output.find('ommand not found') >= 0:
176ec1ee742SBarry Smith        print '================================================================================='
17759dc5438SMatthew Knepley        print '''** Unable to locate hg (Mercurial) to download BuildSystem; make sure hg is in your path'''
178ab21cac3SMatthew Knepley        print '''** or manually copy BuildSystem to $PETSC_DIR/config/BuildSystem from a machine where'''
17959dc5438SMatthew Knepley        print '''** you do have hg installed and can clone BuildSystem. '''
180ec1ee742SBarry Smith        print '================================================================================='
1817d7624c9SBarry Smith      elif output.find('Cannot resolve host') >= 0:
182ec1ee742SBarry Smith        print '================================================================================='
183d688700cSSatish Balay        print '''** Unable to download BuildSystem. You must be off the network.'''
184d688700cSSatish Balay        print '''** Connect to the internet and run config/configure.py again.'''
185ec1ee742SBarry Smith        print '================================================================================='
1867d7624c9SBarry Smith      else:
187ec1ee742SBarry Smith        print '================================================================================='
188d688700cSSatish Balay        print '''** Unable to download BuildSystem. Please send this message to petsc-maint@mcs.anl.gov'''
189ec1ee742SBarry Smith        print '================================================================================='
1907d7624c9SBarry Smith      print output
19187282423SMatthew Knepley      sys.exit(3)
1924f8a5b45SBarry Smith
19387282423SMatthew Knepley  sys.path.insert(0, bsDir)
194f8833479SBarry Smith  sys.path.insert(0, configDir)
195e69ef9dfSMatthew Knepley  import config.base
1965d5a5a7bSMatthew Knepley  import config.framework
197f56be888SMatthew Knepley  import cPickle
1984f8a5b45SBarry Smith
1992e22a60eSMatthew Knepley  # Disable shared libraries by default
2002e22a60eSMatthew Knepley  import nargs
2012e22a60eSMatthew Knepley  if nargs.Arg.findArgument('with-shared', sys.argv[1:]) is None:
2022e22a60eSMatthew Knepley    sys.argv.append('--with-shared=0')
2032e22a60eSMatthew Knepley
2049dd2fdb1SMatthew Knepley  framework = None
2059dd2fdb1SMatthew Knepley  try:
2061a784507SMatthew Knepley    framework = config.framework.Framework(['--configModules=PETSc.Configure','--optionsModule=PETSc.compilerOptions']+sys.argv[1:], loadArgDB = 0)
207d65f3bddSMatthew Knepley    framework.setup()
208d65f3bddSMatthew Knepley    framework.logPrint('\n'.join(extraLogs))
209f24f64feSBarry Smith    framework.configure(out = sys.stdout)
210358ebc22SMatthew Knepley    framework.storeSubstitutions(framework.argDB)
211f56be888SMatthew Knepley    framework.argDB['configureCache'] = cPickle.dumps(framework)
2127cfd0b05SBarry Smith    import PETSc.packages
2137cfd0b05SBarry Smith    for i in framework.packages:
2147cfd0b05SBarry Smith      if hasattr(i,'postProcess'):
2157cfd0b05SBarry Smith        i.postProcess()
2167cfd0b05SBarry Smith    framework.logClear()
217eefa2c0fSBarry Smith    framework.closeLog()
218da1d79b4SSatish Balay    move_configure_log(framework)
219dd50d019SBarry Smith    return 0
220e69ef9dfSMatthew Knepley  except (RuntimeError, config.base.ConfigureSetupError), e:
2217d670a3cSBarry Smith    emsg = str(e)
22242351d26SSatish Balay    if not emsg.endswith('\n'): emsg = emsg+'\n'
223fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
224fe09c992SBarry Smith    +'         UNABLE to CONFIGURE with GIVEN OPTIONS    (see configure.log for details):\n' \
225fe09c992SBarry Smith    +'---------------------------------------------------------------------------------------\n'  \
2267d670a3cSBarry Smith    +emsg+'*********************************************************************************\n'
227e9f3bb17SBarry Smith    se = ''
2289dd2fdb1SMatthew Knepley  except (TypeError, ValueError), e:
2297d670a3cSBarry Smith    emsg = str(e)
23042351d26SSatish Balay    if not emsg.endswith('\n'): emsg = emsg+'\n'
231fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
232fe09c992SBarry Smith    +'                ERROR in COMMAND LINE ARGUMENT to config/configure.py \n' \
233fe09c992SBarry Smith    +'---------------------------------------------------------------------------------------\n'  \
2347d670a3cSBarry Smith    +emsg+'*********************************************************************************\n'
2351a02243aSBarry Smith    se = ''
23696dc2fe8SMatthew Knepley  except ImportError, e :
2377d670a3cSBarry Smith    emsg = str(e)
23842351d26SSatish Balay    if not emsg.endswith('\n'): emsg = emsg+'\n'
239fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
240fe09c992SBarry Smith    +'                     UNABLE to FIND MODULE for config/configure.py \n' \
241fe09c992SBarry Smith    +'---------------------------------------------------------------------------------------\n'  \
2427d670a3cSBarry Smith    +emsg+'*********************************************************************************\n'
24396dc2fe8SMatthew Knepley    se = ''
24401def6f0SMatthew Knepley  except OSError, e :
24501def6f0SMatthew Knepley    emsg = str(e)
24601def6f0SMatthew Knepley    if not emsg.endswith('\n'): emsg = emsg+'\n'
24701def6f0SMatthew Knepley    msg ='*********************************************************************************\n'\
24801def6f0SMatthew Knepley    +'                    UNABLE to EXECUTE BINARIES for config/configure.py \n' \
24901def6f0SMatthew Knepley    +'---------------------------------------------------------------------------------------\n'  \
25001def6f0SMatthew Knepley    +emsg+'*********************************************************************************\n'
25101def6f0SMatthew Knepley    se = ''
252d7d3c4beSMatthew Knepley  except SystemExit, e:
253d7d3c4beSMatthew Knepley    if e.code is None or e.code == 0:
254d7d3c4beSMatthew Knepley      return
255fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
256fe09c992SBarry Smith    +'           CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
257fe09c992SBarry Smith    +'*********************************************************************************\n'
258d7d3c4beSMatthew Knepley    se  = str(e)
259e9f3bb17SBarry Smith  except Exception, e:
260fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
261fe09c992SBarry Smith    +'          CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
262fe09c992SBarry Smith    +'*********************************************************************************\n'
263e9f3bb17SBarry Smith    se  = str(e)
264e9f3bb17SBarry Smith
265e9f3bb17SBarry Smith  print msg
2669dd2fdb1SMatthew Knepley  if not framework is None:
2679dd2fdb1SMatthew Knepley    framework.logClear()
268e9f3bb17SBarry Smith    if hasattr(framework, 'log'):
269f6614063SBarry Smith      import traceback
270f24f64feSBarry Smith      framework.log.write(msg+se)
271f24f64feSBarry Smith      traceback.print_tb(sys.exc_info()[2], file = framework.log)
272da1d79b4SSatish Balay      move_configure_log(framework)
273e9f3bb17SBarry Smith      sys.exit(1)
2745a74f024SMatthew Knepley  else:
2755a74f024SMatthew Knepley    print se
2765a74f024SMatthew Knepley    import traceback
2775a74f024SMatthew Knepley    traceback.print_tb(sys.exc_info()[2])
278da1d79b4SSatish Balay  move_configure_log(framework)
2795d5a5a7bSMatthew Knepley
2805d5a5a7bSMatthew Knepleyif __name__ == '__main__':
281a030c540SBarry Smith  petsc_configure([])
282759acf64SBarry Smith
283