xref: /petsc/config/configure.py (revision 1937db7aebb6613e5170d8bf36cccef3b41e1ff1)
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
75d5a5a7bSMatthew Knepley
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)
32*1937db7aSSatish 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:
38*1937db7aSSatish Balay      print '================================================================================='
39*1937db7aSSatish Balay      print ' *** cygwin-1.5.11-1 detected. config/configure.py fails with this version   ***'
40*1937db7aSSatish Balay      print ' *** Please upgrade to cygwin-1.5.12-1 or newer version. This can  ***'
41*1937db7aSSatish Balay      print ' *** be done by running cygwin-setup, selecting "next" all the way.***'
42*1937db7aSSatish Balay      print '================================================================================='
43*1937db7aSSatish Balay      sys.exit(3)
449dabcff0SSatish Balay  return 0
459dabcff0SSatish Balay
4685ef4d1eSSatish Balaydef chkusingwindowspython():
47*1937db7aSSatish Balay  if os.path.exists('/usr/bin/cygcheck.exe') and sys.platform != 'cygwin':
48*1937db7aSSatish Balay    print '================================================================================='
49*1937db7aSSatish Balay    print ' *** Non-cygwin python detected. Please rerun config/configure.py with cygwin-python ***'
50*1937db7aSSatish Balay    print '================================================================================='
51*1937db7aSSatish 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):
58*1937db7aSSatish Balay      sys.argv.append('--useThreads=0')
59*1937db7aSSatish Balay      extraLogs.append('''\
60*1937db7aSSatish Balay================================================================================
61*1937db7aSSatish Balay** Cygwin-python-2.4/2.5 detected. Threads do not work correctly with this version *
62*1937db7aSSatish Balay ********* Disabling thread usage for this run of config/configure.py **********
63*1937db7aSSatish Balay================================================================================''')
6471384062SSatish Balay  return 0
6571384062SSatish Balay
66cd8c7a90SSatish Balaydef chkincompletecygwin():
67cd8c7a90SSatish Balay  if os.path.exists('/usr/bin/cygcheck.exe'):
68*1937db7aSSatish Balay    if not os.path.exists('/usr/bin/make'):
69cd8c7a90SSatish Balay      print '================================================================================='
70*1937db7aSSatish Balay      print ' *** Incomplete cygwin install detected . /usr/bin/make is missing. **************'
71*1937db7aSSatish Balay      print ' *** Please rerun cygwin-setup and select module "make" for install.**************'
72*1937db7aSSatish Balay      print '================================================================================='
73*1937db7aSSatish Balay      sys.exit(3)
74*1937db7aSSatish Balay    elif not os.path.exists('/usr/bin/diff'):
75*1937db7aSSatish Balay      print '================================================================================='
76*1937db7aSSatish Balay      print ' *** Incomplete cygwin install detected . /usr/bin/diff is missing. **************'
77*1937db7aSSatish Balay      print ' *** Please rerun cygwin-setup and select module "make" for install.**************'
78cd8c7a90SSatish Balay      print '================================================================================='
79cd8c7a90SSatish Balay      sys.exit(3)
80cd8c7a90SSatish Balay  return 0
81cd8c7a90SSatish Balay
82*1937db7aSSatish Balaydef chkrhl9():
83*1937db7aSSatish Balay  if os.path.exists('/etc/redhat-release'):
84836c2c52SSatish Balay    try:
85594eb360SSatish Balay      file = open('/etc/redhat-release','r')
86836c2c52SSatish Balay      buf = file.read()
87836c2c52SSatish Balay      file.close()
88836c2c52SSatish Balay    except:
89836c2c52SSatish Balay      # can't read file - assume dangerous RHL9
90*1937db7aSSatish Balay      buf = 'Shrike'
91836c2c52SSatish Balay    if buf.find('Shrike') > -1:
92*1937db7aSSatish Balay      sys.argv.append('--useThreads=0')
93*1937db7aSSatish Balay      extraLogs.append('''\
94*1937db7aSSatish Balay================================================================================
95*1937db7aSSatish Balay   *** RHL9 detected. Threads do not work correctly with this distribution ***
96*1937db7aSSatish Balay    ****** Disabling thread usage for this run of config/configure.py *******
97*1937db7aSSatish Balay================================================================================''')
98836c2c52SSatish Balay  return 0
99836c2c52SSatish Balay
1005d5a5a7bSMatthew Knepleydef petsc_configure(configure_options):
10159e9bfd6SSatish Balay  print '================================================================================='
10259e9bfd6SSatish Balay  print '             Configuring PETSc to compile on your system                         '
10359e9bfd6SSatish Balay  print '================================================================================='
10459e9bfd6SSatish Balay
105c43ea0feSSatish Balay  # Command line arguments take precedence (but don't destroy argv[0])
106c43ea0feSSatish Balay  sys.argv = sys.argv[:1] + configure_options + sys.argv[1:]
10759e9bfd6SSatish Balay  # check PETSC_ARCH
10859e9bfd6SSatish Balay  check_petsc_arch(sys.argv)
109d65f3bddSMatthew Knepley  extraLogs = []
1105fb2c094SBarry Smith
111c22cdea9SBarry Smith  # support a few standard configure option types
1125d4be62bSBarry Smith  foundsudo = 0
1135d4be62bSBarry Smith  for l in range(0,len(sys.argv)):
1145d4be62bSBarry Smith    if sys.argv[l] == '--with-sudo=sudo':
1155d4be62bSBarry Smith      foundsudo = 1
1165d4be62bSBarry Smith
117ed6a7445SBarry Smith  for l in range(0,len(sys.argv)):
118c22cdea9SBarry Smith    name = sys.argv[l]
119637cc2ebSSatish Balay    if name.find('enable-') >= 0:
120193cd51eSMatthew Knepley      if name.find('=') == -1:
121193cd51eSMatthew Knepley        sys.argv[l] = name.replace('enable-','with-')+'=1'
122193cd51eSMatthew Knepley      else:
123193cd51eSMatthew Knepley        head, tail = name.split('=', 1)
124193cd51eSMatthew Knepley        sys.argv[l] = head.replace('enable-','with-')+'='+tail
125637cc2ebSSatish Balay    if name.find('disable-') >= 0:
126193cd51eSMatthew Knepley      if name.find('=') == -1:
127193cd51eSMatthew Knepley        sys.argv[l] = name.replace('disable-','with-')+'=0'
128193cd51eSMatthew Knepley      else:
129193cd51eSMatthew Knepley        head, tail = name.split('=', 1)
130193cd51eSMatthew Knepley        if tail == '1': tail = '0'
131193cd51eSMatthew Knepley        sys.argv[l] = head.replace('disable-','with-')+'='+tail
132637cc2ebSSatish Balay    if name.find('without-') >= 0:
133193cd51eSMatthew Knepley      if name.find('=') == -1:
134193cd51eSMatthew Knepley        sys.argv[l] = name.replace('without-','with-')+'=0'
135193cd51eSMatthew Knepley      else:
136193cd51eSMatthew Knepley        head, tail = name.split('=', 1)
137193cd51eSMatthew Knepley        if tail == '1': tail = '0'
138193cd51eSMatthew Knepley        sys.argv[l] = head.replace('without-','with-')+'='+tail
1395d4be62bSBarry Smith    if name.find('prefix=') >= 0 and not foundsudo:
1405d4be62bSBarry Smith      head, installdir = name.split('=', 1)
1415d4be62bSBarry Smith      if os.path.exists(installdir):
1425d4be62bSBarry Smith        if not os.access(installdir,os.W_OK):
1435d4be62bSBarry Smith          print 'You do not have write access to requested install directory given with --prefix='+installdir+' perhaps use --with-sudo=sudo also'
1445d4be62bSBarry Smith          sys.exit(3)
1455d4be62bSBarry Smith      else:
1465d4be62bSBarry Smith         try:
1475d4be62bSBarry Smith           os.mkdir(installdir)
1485d4be62bSBarry Smith         except:
1495d4be62bSBarry Smith           print 'You do not have write access to create install directory given with --prefix='+installdir+' perhaps use --with-sudo=sudo also'
1505d4be62bSBarry Smith           sys.exit(3)
1515d4be62bSBarry Smith
152c22cdea9SBarry Smith
153adc3e427SMatthew Knepley  # Check for sudo
154adc3e427SMatthew Knepley  if os.getuid() == 0:
155adc3e427SMatthew Knepley    print '================================================================================='
156adc3e427SMatthew Knepley    print '             *** Do not run configure as root, or using sudo. ***'
15767e28bfeSBarry Smith    print '             *** Use the --with-sudo=sudo option to have      ***'
15867e28bfeSBarry Smith    print '             *** installs of external packages done with sudo ***'
15967e28bfeSBarry Smith    print '             *** use only with --prefix= when installing in   ***'
16067e28bfeSBarry Smith    print '             *** system directories                           ***'
161adc3e427SMatthew Knepley    print '================================================================================='
162adc3e427SMatthew Knepley    sys.exit(3)
163adc3e427SMatthew Knepley
1649dabcff0SSatish Balay  # Check for broken cygwin
165*1937db7aSSatish Balay  chkbrokencygwin()
166cd8c7a90SSatish Balay  # Check if cygwin install is incomplete
167cd8c7a90SSatish Balay  chkincompletecygwin()
168d65f3bddSMatthew Knepley  # Disable threads on RHL9
169*1937db7aSSatish Balay  chkrhl9()
17085ef4d1eSSatish Balay  # Make sure cygwin-python is used on windows
171*1937db7aSSatish Balay  chkusingwindowspython()
17285ef4d1eSSatish Balay  # Threads don't work for cygwin & python-2.4, 2.5 etc..
173*1937db7aSSatish Balay  chkcygwinpythonver()
1749dabcff0SSatish Balay
17587282423SMatthew Knepley  # Should be run from the toplevel
176dbca6d9dSSatish Balay  configDir = os.path.abspath('config')
177f8833479SBarry Smith  bsDir     = os.path.join(configDir, 'BuildSystem')
178f8833479SBarry Smith  if not os.path.isdir(configDir):
1795d5a5a7bSMatthew Knepley    raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.'))
18087282423SMatthew Knepley  if not os.path.isdir(bsDir):
181ec1ee742SBarry Smith    print '================================================================================='
182dbca6d9dSSatish Balay    print '''++ Could not locate BuildSystem in %s.''' % configDir
183dbca6d9dSSatish Balay    print '''++ Downloading it using "hg clone http://hg.mcs.anl.gov/petsc/BuildSystem %s"''' % bsDir
184ec1ee742SBarry Smith    print '================================================================================='
185dbca6d9dSSatish Balay    (status,output) = commands.getstatusoutput('hg clone http://petsc.cs.iit.edu/petsc/BuildSystem '+ bsDir)
1867d7624c9SBarry Smith    if status:
1877d7624c9SBarry Smith      if output.find('ommand not found') >= 0:
188ec1ee742SBarry Smith        print '================================================================================='
18959dc5438SMatthew Knepley        print '''** Unable to locate hg (Mercurial) to download BuildSystem; make sure hg is in your path'''
190ab21cac3SMatthew Knepley        print '''** or manually copy BuildSystem to $PETSC_DIR/config/BuildSystem from a machine where'''
19159dc5438SMatthew Knepley        print '''** you do have hg installed and can clone BuildSystem. '''
192ec1ee742SBarry Smith        print '================================================================================='
1937d7624c9SBarry Smith      elif output.find('Cannot resolve host') >= 0:
194ec1ee742SBarry Smith        print '================================================================================='
195d688700cSSatish Balay        print '''** Unable to download BuildSystem. You must be off the network.'''
196d688700cSSatish Balay        print '''** Connect to the internet and run config/configure.py again.'''
197ec1ee742SBarry Smith        print '================================================================================='
1987d7624c9SBarry Smith      else:
199ec1ee742SBarry Smith        print '================================================================================='
200d688700cSSatish Balay        print '''** Unable to download BuildSystem. Please send this message to petsc-maint@mcs.anl.gov'''
201ec1ee742SBarry Smith        print '================================================================================='
2027d7624c9SBarry Smith      print output
20387282423SMatthew Knepley      sys.exit(3)
2044f8a5b45SBarry Smith
20587282423SMatthew Knepley  sys.path.insert(0, bsDir)
206f8833479SBarry Smith  sys.path.insert(0, configDir)
207e69ef9dfSMatthew Knepley  import config.base
2085d5a5a7bSMatthew Knepley  import config.framework
209f56be888SMatthew Knepley  import cPickle
2104f8a5b45SBarry Smith
2112e22a60eSMatthew Knepley  # Disable shared libraries by default
2122e22a60eSMatthew Knepley  import nargs
2132e22a60eSMatthew Knepley  if nargs.Arg.findArgument('with-shared', sys.argv[1:]) is None:
2142e22a60eSMatthew Knepley    sys.argv.append('--with-shared=0')
2152e22a60eSMatthew Knepley
2169dd2fdb1SMatthew Knepley  framework = None
2179dd2fdb1SMatthew Knepley  try:
2181a784507SMatthew Knepley    framework = config.framework.Framework(['--configModules=PETSc.Configure','--optionsModule=PETSc.compilerOptions']+sys.argv[1:], loadArgDB = 0)
219d65f3bddSMatthew Knepley    framework.setup()
220d65f3bddSMatthew Knepley    framework.logPrint('\n'.join(extraLogs))
221f24f64feSBarry Smith    framework.configure(out = sys.stdout)
222358ebc22SMatthew Knepley    framework.storeSubstitutions(framework.argDB)
223f56be888SMatthew Knepley    framework.argDB['configureCache'] = cPickle.dumps(framework)
2247cfd0b05SBarry Smith    import PETSc.packages
2257cfd0b05SBarry Smith    for i in framework.packages:
2267cfd0b05SBarry Smith      if hasattr(i,'postProcess'):
2277cfd0b05SBarry Smith        i.postProcess()
2287cfd0b05SBarry Smith    framework.logClear()
229eefa2c0fSBarry Smith    framework.closeLog()
2301a784507SMatthew Knepley    if hasattr(framework, 'arch'):
231eefa2c0fSBarry Smith      import shutil
232eefa2c0fSBarry Smith      shutil.move(framework.logName,os.path.join(framework.arch,'conf',framework.logName))
233dd50d019SBarry Smith    return 0
234e69ef9dfSMatthew Knepley  except (RuntimeError, config.base.ConfigureSetupError), e:
2357d670a3cSBarry Smith    emsg = str(e)
23642351d26SSatish Balay    if not emsg.endswith('\n'): emsg = emsg+'\n'
237fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
238fe09c992SBarry Smith    +'         UNABLE to CONFIGURE with GIVEN OPTIONS    (see configure.log for details):\n' \
239fe09c992SBarry Smith    +'---------------------------------------------------------------------------------------\n'  \
2407d670a3cSBarry Smith    +emsg+'*********************************************************************************\n'
241e9f3bb17SBarry Smith    se = ''
2429dd2fdb1SMatthew Knepley  except (TypeError, ValueError), e:
2437d670a3cSBarry Smith    emsg = str(e)
24442351d26SSatish Balay    if not emsg.endswith('\n'): emsg = emsg+'\n'
245fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
246fe09c992SBarry Smith    +'                ERROR in COMMAND LINE ARGUMENT to config/configure.py \n' \
247fe09c992SBarry Smith    +'---------------------------------------------------------------------------------------\n'  \
2487d670a3cSBarry Smith    +emsg+'*********************************************************************************\n'
2491a02243aSBarry Smith    se = ''
25096dc2fe8SMatthew Knepley  except ImportError, e :
2517d670a3cSBarry Smith    emsg = str(e)
25242351d26SSatish Balay    if not emsg.endswith('\n'): emsg = emsg+'\n'
253fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
254fe09c992SBarry Smith    +'                     UNABLE to FIND MODULE for config/configure.py \n' \
255fe09c992SBarry Smith    +'---------------------------------------------------------------------------------------\n'  \
2567d670a3cSBarry Smith    +emsg+'*********************************************************************************\n'
25796dc2fe8SMatthew Knepley    se = ''
25801def6f0SMatthew Knepley  except OSError, e :
25901def6f0SMatthew Knepley    emsg = str(e)
26001def6f0SMatthew Knepley    if not emsg.endswith('\n'): emsg = emsg+'\n'
26101def6f0SMatthew Knepley    msg ='*********************************************************************************\n'\
26201def6f0SMatthew Knepley    +'                    UNABLE to EXECUTE BINARIES for config/configure.py \n' \
26301def6f0SMatthew Knepley    +'---------------------------------------------------------------------------------------\n'  \
26401def6f0SMatthew Knepley    +emsg+'*********************************************************************************\n'
26501def6f0SMatthew Knepley    se = ''
266d7d3c4beSMatthew Knepley  except SystemExit, e:
267d7d3c4beSMatthew Knepley    if e.code is None or e.code == 0:
268d7d3c4beSMatthew Knepley      return
269fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
270fe09c992SBarry Smith    +'           CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
271fe09c992SBarry Smith    +'*********************************************************************************\n'
272d7d3c4beSMatthew Knepley    se  = str(e)
273e9f3bb17SBarry Smith  except Exception, e:
274fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
275fe09c992SBarry Smith    +'          CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
276fe09c992SBarry Smith    +'*********************************************************************************\n'
277e9f3bb17SBarry Smith    se  = str(e)
278e9f3bb17SBarry Smith
279e9f3bb17SBarry Smith  print msg
2809dd2fdb1SMatthew Knepley  if not framework is None:
2819dd2fdb1SMatthew Knepley    framework.logClear()
282e9f3bb17SBarry Smith    if hasattr(framework, 'log'):
283f6614063SBarry Smith      import traceback
284f24f64feSBarry Smith      framework.log.write(msg+se)
285f24f64feSBarry Smith      traceback.print_tb(sys.exc_info()[2], file = framework.log)
2862418bae5SMatthew Knepley      if os.path.isfile(framework.logName+'.bkp'):
2875a74f024SMatthew Knepley        if framework.debugIndent is None:
2885a74f024SMatthew Knepley          framework.debugIndent = '  '
2892418bae5SMatthew Knepley        framework.logPrintDivider()
2902418bae5SMatthew Knepley        framework.logPrintBox('Previous configure logs below', debugSection = None)
2912418bae5SMatthew Knepley        f = file(framework.logName+'.bkp')
2922418bae5SMatthew Knepley        framework.log.write(f.read())
2932418bae5SMatthew Knepley        f.close()
294e9f3bb17SBarry Smith      sys.exit(1)
2955a74f024SMatthew Knepley  else:
2965a74f024SMatthew Knepley    print se
2975a74f024SMatthew Knepley    import traceback
2985a74f024SMatthew Knepley    traceback.print_tb(sys.exc_info()[2])
2995d5a5a7bSMatthew Knepley
3005d5a5a7bSMatthew Knepleyif __name__ == '__main__':
301a030c540SBarry Smith  petsc_configure([])
302759acf64SBarry Smith
303