xref: /petsc/config/configure.py (revision 7c9abfe77f1b7ea0ee34cf34e717614f0bbd185a)
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
7*7c9abfe7SSatish BalayextraLogs = []
84b8aa89bSBarry Smith
9378f148eSBarry Smithif not hasattr(sys, 'version_info') or not sys.version_info[1] >= 2 or not sys.version_info[0] >= 2:
10495ffa62SBarry Smith  print '**** You must have Python version 2.2 or higher to run config/configure.py ******'
11495ffa62SBarry Smith  print '*           Python is easy to install for end users or sys-admin.               *'
1232077d6dSBarry Smith  print '*                   http://www.python.org/download/                             *'
1332077d6dSBarry Smith  print '*                                                                               *'
14495ffa62SBarry Smith  print '*            You CANNOT configure PETSc without Python                          *'
15495ffa62SBarry Smith  print '*    http://www.mcs.anl.gov/petsc/petsc-as/documentation/installation.html      *'
1632077d6dSBarry Smith  print '*********************************************************************************'
17b26a8723SBarry Smith  sys.exit(4)
182fb34ac0SMatthew Knepley
1959e9bfd6SSatish Balaydef check_petsc_arch(opts):
20c43ea0feSSatish Balay  # If PETSC_ARCH not specified - use script name (if not configure.py)
21c43ea0feSSatish Balay  found = 0
2259e9bfd6SSatish Balay  for name in opts:
23c43ea0feSSatish Balay    if name.find('PETSC_ARCH=') >= 0:
24c43ea0feSSatish Balay      found = 1
2559e9bfd6SSatish Balay      break
2659e9bfd6SSatish Balay  # If not yet specified - use the filename of script
27c43ea0feSSatish Balay  if not found:
2859e9bfd6SSatish Balay      filename = os.path.basename(sys.argv[0])
297eed1879SBarry Smith      if not filename.startswith('configure') and not filename.startswith('reconfigure'):
30637cc2ebSSatish Balay        useName = 'PETSC_ARCH='+os.path.splitext(os.path.basename(sys.argv[0]))[0]
3159e9bfd6SSatish Balay        opts.append(useName)
321937db7aSSatish Balay  return 0
334b8aa89bSBarry Smith
3485ef4d1eSSatish Balaydef chkbrokencygwin():
359dabcff0SSatish Balay  if os.path.exists('/usr/bin/cygcheck.exe'):
369dabcff0SSatish Balay    buf = os.popen('/usr/bin/cygcheck.exe -c cygwin').read()
379dabcff0SSatish Balay    if buf.find('1.5.11-1') > -1:
381937db7aSSatish Balay      print '================================================================================='
391937db7aSSatish Balay      print ' *** cygwin-1.5.11-1 detected. config/configure.py fails with this version   ***'
401937db7aSSatish Balay      print ' *** Please upgrade to cygwin-1.5.12-1 or newer version. This can  ***'
411937db7aSSatish Balay      print ' *** be done by running cygwin-setup, selecting "next" all the way.***'
421937db7aSSatish Balay      print '================================================================================='
431937db7aSSatish Balay      sys.exit(3)
449dabcff0SSatish Balay  return 0
459dabcff0SSatish Balay
4685ef4d1eSSatish Balaydef chkusingwindowspython():
471937db7aSSatish Balay  if os.path.exists('/usr/bin/cygcheck.exe') and sys.platform != 'cygwin':
481937db7aSSatish Balay    print '================================================================================='
491937db7aSSatish Balay    print ' *** Non-cygwin python detected. Please rerun config/configure.py with cygwin-python ***'
501937db7aSSatish Balay    print '================================================================================='
511937db7aSSatish Balay    sys.exit(3)
5285ef4d1eSSatish Balay  return 0
5385ef4d1eSSatish Balay
5485ef4d1eSSatish Balaydef chkcygwinpythonver():
5571384062SSatish Balay  if os.path.exists('/usr/bin/cygcheck.exe'):
5671384062SSatish Balay    buf = os.popen('/usr/bin/cygcheck.exe -c python').read()
57c4b7e894SSatish Balay    if (buf.find('2.4') > -1) or (buf.find('2.5') > -1) or (buf.find('2.6') > -1):
581937db7aSSatish Balay      sys.argv.append('--useThreads=0')
591937db7aSSatish Balay      extraLogs.append('''\
601937db7aSSatish Balay================================================================================
611937db7aSSatish Balay** Cygwin-python-2.4/2.5 detected. Threads do not work correctly with this version *
621937db7aSSatish Balay ********* Disabling thread usage for this run of config/configure.py **********
631937db7aSSatish Balay================================================================================''')
6471384062SSatish Balay  return 0
6571384062SSatish Balay
661937db7aSSatish Balaydef chkrhl9():
671937db7aSSatish Balay  if os.path.exists('/etc/redhat-release'):
68836c2c52SSatish Balay    try:
69594eb360SSatish Balay      file = open('/etc/redhat-release','r')
70836c2c52SSatish Balay      buf = file.read()
71836c2c52SSatish Balay      file.close()
72836c2c52SSatish Balay    except:
73836c2c52SSatish Balay      # can't read file - assume dangerous RHL9
741937db7aSSatish Balay      buf = 'Shrike'
75836c2c52SSatish Balay    if buf.find('Shrike') > -1:
761937db7aSSatish Balay      sys.argv.append('--useThreads=0')
771937db7aSSatish Balay      extraLogs.append('''\
781937db7aSSatish Balay================================================================================
791937db7aSSatish Balay   *** RHL9 detected. Threads do not work correctly with this distribution ***
801937db7aSSatish Balay    ****** Disabling thread usage for this run of config/configure.py *******
811937db7aSSatish Balay================================================================================''')
82836c2c52SSatish Balay  return 0
83836c2c52SSatish Balay
845d5a5a7bSMatthew Knepleydef petsc_configure(configure_options):
8559e9bfd6SSatish Balay  print '================================================================================='
8659e9bfd6SSatish Balay  print '             Configuring PETSc to compile on your system                         '
8759e9bfd6SSatish Balay  print '================================================================================='
8859e9bfd6SSatish Balay
89c43ea0feSSatish Balay  # Command line arguments take precedence (but don't destroy argv[0])
90c43ea0feSSatish Balay  sys.argv = sys.argv[:1] + configure_options + sys.argv[1:]
9159e9bfd6SSatish Balay  # check PETSC_ARCH
9259e9bfd6SSatish Balay  check_petsc_arch(sys.argv)
935fb2c094SBarry Smith
94c22cdea9SBarry Smith  # support a few standard configure option types
95ed6a7445SBarry Smith  for l in range(0,len(sys.argv)):
96c22cdea9SBarry Smith    name = sys.argv[l]
97637cc2ebSSatish Balay    if name.find('enable-') >= 0:
98193cd51eSMatthew Knepley      if name.find('=') == -1:
99193cd51eSMatthew Knepley        sys.argv[l] = name.replace('enable-','with-')+'=1'
100193cd51eSMatthew Knepley      else:
101193cd51eSMatthew Knepley        head, tail = name.split('=', 1)
102193cd51eSMatthew Knepley        sys.argv[l] = head.replace('enable-','with-')+'='+tail
103637cc2ebSSatish Balay    if name.find('disable-') >= 0:
104193cd51eSMatthew Knepley      if name.find('=') == -1:
105193cd51eSMatthew Knepley        sys.argv[l] = name.replace('disable-','with-')+'=0'
106193cd51eSMatthew Knepley      else:
107193cd51eSMatthew Knepley        head, tail = name.split('=', 1)
108193cd51eSMatthew Knepley        if tail == '1': tail = '0'
109193cd51eSMatthew Knepley        sys.argv[l] = head.replace('disable-','with-')+'='+tail
110637cc2ebSSatish Balay    if name.find('without-') >= 0:
111193cd51eSMatthew Knepley      if name.find('=') == -1:
112193cd51eSMatthew Knepley        sys.argv[l] = name.replace('without-','with-')+'=0'
113193cd51eSMatthew Knepley      else:
114193cd51eSMatthew Knepley        head, tail = name.split('=', 1)
115193cd51eSMatthew Knepley        if tail == '1': tail = '0'
116193cd51eSMatthew Knepley        sys.argv[l] = head.replace('without-','with-')+'='+tail
117adc3e427SMatthew Knepley
1189dabcff0SSatish Balay  # Check for broken cygwin
1191937db7aSSatish Balay  chkbrokencygwin()
120d65f3bddSMatthew Knepley  # Disable threads on RHL9
1211937db7aSSatish Balay  chkrhl9()
12285ef4d1eSSatish Balay  # Make sure cygwin-python is used on windows
1231937db7aSSatish Balay  chkusingwindowspython()
12485ef4d1eSSatish Balay  # Threads don't work for cygwin & python-2.4, 2.5 etc..
1251937db7aSSatish Balay  chkcygwinpythonver()
1269dabcff0SSatish Balay
12787282423SMatthew Knepley  # Should be run from the toplevel
128dbca6d9dSSatish Balay  configDir = os.path.abspath('config')
129f8833479SBarry Smith  bsDir     = os.path.join(configDir, 'BuildSystem')
130f8833479SBarry Smith  if not os.path.isdir(configDir):
1315d5a5a7bSMatthew Knepley    raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.'))
13287282423SMatthew Knepley  if not os.path.isdir(bsDir):
133ec1ee742SBarry Smith    print '================================================================================='
134dbca6d9dSSatish Balay    print '''++ Could not locate BuildSystem in %s.''' % configDir
135dbca6d9dSSatish Balay    print '''++ Downloading it using "hg clone http://hg.mcs.anl.gov/petsc/BuildSystem %s"''' % bsDir
136ec1ee742SBarry Smith    print '================================================================================='
137dbca6d9dSSatish Balay    (status,output) = commands.getstatusoutput('hg clone http://petsc.cs.iit.edu/petsc/BuildSystem '+ bsDir)
1387d7624c9SBarry Smith    if status:
1397d7624c9SBarry Smith      if output.find('ommand not found') >= 0:
140ec1ee742SBarry Smith        print '================================================================================='
14159dc5438SMatthew Knepley        print '''** Unable to locate hg (Mercurial) to download BuildSystem; make sure hg is in your path'''
142ab21cac3SMatthew Knepley        print '''** or manually copy BuildSystem to $PETSC_DIR/config/BuildSystem from a machine where'''
14359dc5438SMatthew Knepley        print '''** you do have hg installed and can clone BuildSystem. '''
144ec1ee742SBarry Smith        print '================================================================================='
1457d7624c9SBarry Smith      elif output.find('Cannot resolve host') >= 0:
146ec1ee742SBarry Smith        print '================================================================================='
147d688700cSSatish Balay        print '''** Unable to download BuildSystem. You must be off the network.'''
148d688700cSSatish Balay        print '''** Connect to the internet and run config/configure.py again.'''
149ec1ee742SBarry Smith        print '================================================================================='
1507d7624c9SBarry Smith      else:
151ec1ee742SBarry Smith        print '================================================================================='
152d688700cSSatish Balay        print '''** Unable to download BuildSystem. Please send this message to petsc-maint@mcs.anl.gov'''
153ec1ee742SBarry Smith        print '================================================================================='
1547d7624c9SBarry Smith      print output
15587282423SMatthew Knepley      sys.exit(3)
1564f8a5b45SBarry Smith
15787282423SMatthew Knepley  sys.path.insert(0, bsDir)
158f8833479SBarry Smith  sys.path.insert(0, configDir)
159e69ef9dfSMatthew Knepley  import config.base
1605d5a5a7bSMatthew Knepley  import config.framework
161f56be888SMatthew Knepley  import cPickle
1624f8a5b45SBarry Smith
1632e22a60eSMatthew Knepley  # Disable shared libraries by default
1642e22a60eSMatthew Knepley  import nargs
1652e22a60eSMatthew Knepley  if nargs.Arg.findArgument('with-shared', sys.argv[1:]) is None:
1662e22a60eSMatthew Knepley    sys.argv.append('--with-shared=0')
1672e22a60eSMatthew Knepley
1689dd2fdb1SMatthew Knepley  framework = None
1699dd2fdb1SMatthew Knepley  try:
1701a784507SMatthew Knepley    framework = config.framework.Framework(['--configModules=PETSc.Configure','--optionsModule=PETSc.compilerOptions']+sys.argv[1:], loadArgDB = 0)
171d65f3bddSMatthew Knepley    framework.setup()
172d65f3bddSMatthew Knepley    framework.logPrint('\n'.join(extraLogs))
173f24f64feSBarry Smith    framework.configure(out = sys.stdout)
174358ebc22SMatthew Knepley    framework.storeSubstitutions(framework.argDB)
175f56be888SMatthew Knepley    framework.argDB['configureCache'] = cPickle.dumps(framework)
1767cfd0b05SBarry Smith    import PETSc.packages
1777cfd0b05SBarry Smith    for i in framework.packages:
1787cfd0b05SBarry Smith      if hasattr(i,'postProcess'):
1797cfd0b05SBarry Smith        i.postProcess()
1807cfd0b05SBarry Smith    framework.logClear()
181eefa2c0fSBarry Smith    framework.closeLog()
1821a784507SMatthew Knepley    if hasattr(framework, 'arch'):
183eefa2c0fSBarry Smith      import shutil
184eefa2c0fSBarry Smith      shutil.move(framework.logName,os.path.join(framework.arch,'conf',framework.logName))
185dd50d019SBarry Smith    return 0
186e69ef9dfSMatthew Knepley  except (RuntimeError, config.base.ConfigureSetupError), e:
1877d670a3cSBarry Smith    emsg = str(e)
18842351d26SSatish Balay    if not emsg.endswith('\n'): emsg = emsg+'\n'
189fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
190fe09c992SBarry Smith    +'         UNABLE to CONFIGURE with GIVEN OPTIONS    (see configure.log for details):\n' \
191fe09c992SBarry Smith    +'---------------------------------------------------------------------------------------\n'  \
1927d670a3cSBarry Smith    +emsg+'*********************************************************************************\n'
193e9f3bb17SBarry Smith    se = ''
1949dd2fdb1SMatthew Knepley  except (TypeError, ValueError), e:
1957d670a3cSBarry Smith    emsg = str(e)
19642351d26SSatish Balay    if not emsg.endswith('\n'): emsg = emsg+'\n'
197fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
198fe09c992SBarry Smith    +'                ERROR in COMMAND LINE ARGUMENT to config/configure.py \n' \
199fe09c992SBarry Smith    +'---------------------------------------------------------------------------------------\n'  \
2007d670a3cSBarry Smith    +emsg+'*********************************************************************************\n'
2011a02243aSBarry Smith    se = ''
20296dc2fe8SMatthew Knepley  except ImportError, e :
2037d670a3cSBarry Smith    emsg = str(e)
20442351d26SSatish Balay    if not emsg.endswith('\n'): emsg = emsg+'\n'
205fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
206fe09c992SBarry Smith    +'                     UNABLE to FIND MODULE for config/configure.py \n' \
207fe09c992SBarry Smith    +'---------------------------------------------------------------------------------------\n'  \
2087d670a3cSBarry Smith    +emsg+'*********************************************************************************\n'
20996dc2fe8SMatthew Knepley    se = ''
21001def6f0SMatthew Knepley  except OSError, e :
21101def6f0SMatthew Knepley    emsg = str(e)
21201def6f0SMatthew Knepley    if not emsg.endswith('\n'): emsg = emsg+'\n'
21301def6f0SMatthew Knepley    msg ='*********************************************************************************\n'\
21401def6f0SMatthew Knepley    +'                    UNABLE to EXECUTE BINARIES for config/configure.py \n' \
21501def6f0SMatthew Knepley    +'---------------------------------------------------------------------------------------\n'  \
21601def6f0SMatthew Knepley    +emsg+'*********************************************************************************\n'
21701def6f0SMatthew Knepley    se = ''
218d7d3c4beSMatthew Knepley  except SystemExit, e:
219d7d3c4beSMatthew Knepley    if e.code is None or e.code == 0:
220d7d3c4beSMatthew Knepley      return
221fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
222fe09c992SBarry Smith    +'           CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
223fe09c992SBarry Smith    +'*********************************************************************************\n'
224d7d3c4beSMatthew Knepley    se  = str(e)
225e9f3bb17SBarry Smith  except Exception, e:
226fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
227fe09c992SBarry Smith    +'          CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
228fe09c992SBarry Smith    +'*********************************************************************************\n'
229e9f3bb17SBarry Smith    se  = str(e)
230e9f3bb17SBarry Smith
231e9f3bb17SBarry Smith  print msg
2329dd2fdb1SMatthew Knepley  if not framework is None:
2339dd2fdb1SMatthew Knepley    framework.logClear()
234e9f3bb17SBarry Smith    if hasattr(framework, 'log'):
235f6614063SBarry Smith      import traceback
236f24f64feSBarry Smith      framework.log.write(msg+se)
237f24f64feSBarry Smith      traceback.print_tb(sys.exc_info()[2], file = framework.log)
2382418bae5SMatthew Knepley      if os.path.isfile(framework.logName+'.bkp'):
2395a74f024SMatthew Knepley        if framework.debugIndent is None:
2405a74f024SMatthew Knepley          framework.debugIndent = '  '
2412418bae5SMatthew Knepley        framework.logPrintDivider()
2422418bae5SMatthew Knepley        framework.logPrintBox('Previous configure logs below', debugSection = None)
2432418bae5SMatthew Knepley        f = file(framework.logName+'.bkp')
2442418bae5SMatthew Knepley        framework.log.write(f.read())
2452418bae5SMatthew Knepley        f.close()
246e9f3bb17SBarry Smith      sys.exit(1)
2475a74f024SMatthew Knepley  else:
2485a74f024SMatthew Knepley    print se
2495a74f024SMatthew Knepley    import traceback
2505a74f024SMatthew Knepley    traceback.print_tb(sys.exc_info()[2])
2515d5a5a7bSMatthew Knepley
2525d5a5a7bSMatthew Knepleyif __name__ == '__main__':
253a030c540SBarry Smith  petsc_configure([])
254759acf64SBarry Smith
255