xref: /petsc/config/configure.py (revision cd8c7a902c9acc79744e383cf05a0d39ce707d4e)
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)
3259e9bfd6SSatish Balay  return
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:
389dabcff0SSatish Balay      return 1
399dabcff0SSatish Balay    else:
409dabcff0SSatish Balay      return 0
419dabcff0SSatish Balay  return 0
429dabcff0SSatish Balay
4385ef4d1eSSatish Balaydef chkusingwindowspython():
4485ef4d1eSSatish Balay  if os.path.exists('/usr/bin/cygcheck.exe'):
4585ef4d1eSSatish Balay    if sys.platform != 'cygwin':
4685ef4d1eSSatish Balay      return 1
4785ef4d1eSSatish Balay  return 0
4885ef4d1eSSatish Balay
4985ef4d1eSSatish Balaydef chkcygwinpythonver():
5071384062SSatish Balay  if os.path.exists('/usr/bin/cygcheck.exe'):
5171384062SSatish Balay    buf = os.popen('/usr/bin/cygcheck.exe -c python').read()
52c4b7e894SSatish Balay    if (buf.find('2.4') > -1) or (buf.find('2.5') > -1) or (buf.find('2.6') > -1):
5371384062SSatish Balay      return 1
5471384062SSatish Balay    else:
5571384062SSatish Balay      return 0
5671384062SSatish Balay  return 0
5771384062SSatish Balay
58*cd8c7a90SSatish Balaydef chkincompletecygwin():
59*cd8c7a90SSatish Balay  if os.path.exists('/usr/bin/cygcheck.exe'):
60*cd8c7a90SSatish Balay    if not os.path.exists('/usr/bin/make') or not os.path.exists('/usr/bin/diff'):
61*cd8c7a90SSatish Balay      print '================================================================================='
62*cd8c7a90SSatish Balay      print ' *** Incomplete cygwin install detected *****************************************'
63*cd8c7a90SSatish Balay      print ' *** Please rerun cygwin-setup and install module make [and its dependencies]****'
64*cd8c7a90SSatish Balay      print '================================================================================='
65*cd8c7a90SSatish Balay      sys.exit(3)
66*cd8c7a90SSatish Balay  return 0
67*cd8c7a90SSatish Balay
68836c2c52SSatish Balaydef rhl9():
69836c2c52SSatish Balay  try:
70594eb360SSatish Balay    file = open('/etc/redhat-release','r')
71836c2c52SSatish Balay  except:
72836c2c52SSatish Balay    return 0
73836c2c52SSatish Balay  try:
74836c2c52SSatish Balay    buf = file.read()
75836c2c52SSatish Balay    file.close()
76836c2c52SSatish Balay  except:
77836c2c52SSatish Balay    # can't read file - assume dangerous RHL9
78836c2c52SSatish Balay    return 1
79836c2c52SSatish Balay  if buf.find('Shrike') > -1:
80836c2c52SSatish Balay    return 1
81836c2c52SSatish Balay  else:
82836c2c52SSatish Balay    return 0
83836c2c52SSatish Balay
84fd0ecfceSSatish Balaydef chkBrokenF8Diff():
85fd0ecfceSSatish Balay  if os.path.exists('/bin/rpm'):
86fd0ecfceSSatish Balay    buf = os.popen('/bin/rpm -q diffutils').read()
87fd0ecfceSSatish Balay  if buf.find('diffutils-2.8.1-17.fc8') > -1:
88fd0ecfceSSatish Balay    return 1
89fd0ecfceSSatish Balay  else:
90fd0ecfceSSatish Balay    return 0
91fd0ecfceSSatish Balay
92fd0ecfceSSatish Balay
935d5a5a7bSMatthew Knepleydef petsc_configure(configure_options):
9459e9bfd6SSatish Balay  print '================================================================================='
9559e9bfd6SSatish Balay  print '             Configuring PETSc to compile on your system                         '
9659e9bfd6SSatish Balay  print '================================================================================='
9759e9bfd6SSatish Balay
98c43ea0feSSatish Balay  # Command line arguments take precedence (but don't destroy argv[0])
99c43ea0feSSatish Balay  sys.argv = sys.argv[:1] + configure_options + sys.argv[1:]
10059e9bfd6SSatish Balay  # check PETSC_ARCH
10159e9bfd6SSatish Balay  check_petsc_arch(sys.argv)
102d65f3bddSMatthew Knepley  extraLogs = []
1035fb2c094SBarry Smith
104c22cdea9SBarry Smith  # support a few standard configure option types
105ed6a7445SBarry Smith  for l in range(0,len(sys.argv)):
106c22cdea9SBarry Smith    name = sys.argv[l]
107637cc2ebSSatish Balay    if name.find('enable-') >= 0:
108637cc2ebSSatish Balay      sys.argv[l] = name.replace('enable-','with-')
10942351d26SSatish Balay      if name.find('=') == -1: sys.argv[l] = sys.argv[l]+'=1'
110637cc2ebSSatish Balay    if name.find('disable-') >= 0:
111637cc2ebSSatish Balay      sys.argv[l] = name.replace('disable-','with-')
11242351d26SSatish Balay      if name.find('=') == -1: sys.argv[l] = sys.argv[l]+'=0'
113c22cdea9SBarry Smith      elif name.endswith('=1'): sys.argv[l].replace('=1','=0')
114637cc2ebSSatish Balay    if name.find('without-') >= 0:
115637cc2ebSSatish Balay      sys.argv[l] = name.replace('without-','with-')
11642351d26SSatish Balay      if name.find('=') == -1: sys.argv[l] = sys.argv[l]+'=0'
117c22cdea9SBarry Smith      elif name.endswith('=1'): sys.argv[l].replace('=1','=0')
118c22cdea9SBarry Smith
119adc3e427SMatthew Knepley  # Check for sudo
120adc3e427SMatthew Knepley  if os.getuid() == 0:
121adc3e427SMatthew Knepley    print '================================================================================='
122adc3e427SMatthew Knepley    print '             *** Do not run configure as root, or using sudo. ***'
12367e28bfeSBarry Smith    print '             *** Use the --with-sudo=sudo option to have      ***'
12467e28bfeSBarry Smith    print '             *** installs of external packages done with sudo ***'
12567e28bfeSBarry Smith    print '             *** use only with --prefix= when installing in   ***'
12667e28bfeSBarry Smith    print '             *** system directories                           ***'
127adc3e427SMatthew Knepley    print '================================================================================='
128adc3e427SMatthew Knepley    sys.exit(3)
129adc3e427SMatthew Knepley
1309dabcff0SSatish Balay  # Check for broken cygwin
13185ef4d1eSSatish Balay  if chkbrokencygwin():
132ec1ee742SBarry Smith    print '================================================================================='
1337d670a3cSBarry Smith    print ' *** cygwin-1.5.11-1 detected. config/configure.py fails with this version   ***'
1341e42869aSSatish Balay    print ' *** Please upgrade to cygwin-1.5.12-1 or newer version. This can  ***'
1351e42869aSSatish Balay    print ' *** be done by running cygwin-setup, selecting "next" all the way.***'
136ec1ee742SBarry Smith    print '================================================================================='
1379dabcff0SSatish Balay    sys.exit(3)
138d65f3bddSMatthew Knepley
139*cd8c7a90SSatish Balay  # Check if cygwin install is incomplete
140*cd8c7a90SSatish Balay  chkincompletecygwin()
141*cd8c7a90SSatish Balay
142d65f3bddSMatthew Knepley  # Disable threads on RHL9
143d65f3bddSMatthew Knepley  if rhl9():
144d65f3bddSMatthew Knepley    sys.argv.append('--useThreads=0')
145d65f3bddSMatthew Knepley    extraLogs.append('''\
146d65f3bddSMatthew Knepley================================================================================
147d65f3bddSMatthew Knepley   *** RHL9 detected. Threads do not work correctly with this distribution ***
148d65f3bddSMatthew Knepley    ****** Disabling thread usage for this run of config/configure.py *******
149d65f3bddSMatthew Knepley================================================================================''')
150fd0ecfceSSatish Balay
151fd0ecfceSSatish Balay  # Check for broken diff on Fedora8
152fd0ecfceSSatish Balay  if chkBrokenF8Diff():
153fd0ecfceSSatish Balay    print '================================================================================='
154fd0ecfceSSatish Balay    print ' *** Fedora 8 Linux with broken diffutils-2.8.1-17.fc8 detected. ****************'
155fd0ecfceSSatish Balay    print ' *** Please run "sudo yum update diffutils" to get the latest bugfixed version.**'
156fd0ecfceSSatish Balay    print '================================================================================='
157fd0ecfceSSatish Balay    sys.exit(3)
158fd0ecfceSSatish Balay
15985ef4d1eSSatish Balay  # Make sure cygwin-python is used on windows
16085ef4d1eSSatish Balay  if chkusingwindowspython():
16185ef4d1eSSatish Balay    print '================================================================================='
16285ef4d1eSSatish Balay    print ' *** Non-cygwin python detected. Please rerun config/configure.py with cygwin-python ***'
16385ef4d1eSSatish Balay    print '================================================================================='
16485ef4d1eSSatish Balay    sys.exit(3)
165d65f3bddSMatthew Knepley
16685ef4d1eSSatish Balay  # Threads don't work for cygwin & python-2.4, 2.5 etc..
16785ef4d1eSSatish Balay  if chkcygwinpythonver():
16871384062SSatish Balay    sys.argv.append('--useThreads=0')
169d65f3bddSMatthew Knepley    extraLogs.append('''\
170d65f3bddSMatthew Knepley================================================================================
17185ef4d1eSSatish Balay** Cygwin-python-2.4/2.5 detected. Threads do not work correctly with this version *
172d65f3bddSMatthew Knepley ********* Disabling thread usage for this run of config/configure.py **********
173d65f3bddSMatthew Knepley================================================================================''')
1749dabcff0SSatish Balay
17587282423SMatthew Knepley  # Should be run from the toplevel
1765d5a5a7bSMatthew Knepley  pythonDir = os.path.abspath(os.path.join('python'))
17787282423SMatthew Knepley  bsDir     = os.path.join(pythonDir, 'BuildSystem')
17887282423SMatthew Knepley  if not os.path.isdir(pythonDir):
1795d5a5a7bSMatthew Knepley    raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.'))
18087282423SMatthew Knepley  if not os.path.isdir(bsDir):
181ec1ee742SBarry Smith    print '================================================================================='
1829c4310d5SMatthew Knepley    print '''++ Could not locate BuildSystem in %s/python.''' % os.getcwd()
183ec1ef0a0SSatish Balay    print '''++ Downloading it using "hg clone http://hg.mcs.anl.gov/petsc/BuildSystem %s/python/BuildSystem"''' % os.getcwd()
184ec1ee742SBarry Smith    print '================================================================================='
185ec1ef0a0SSatish Balay    (status,output) = commands.getstatusoutput('hg clone http://hg.mcs.anl.gov/petsc/BuildSystem python/BuildSystem')
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'''
190d688700cSSatish Balay        print '''** or manually copy BuildSystem to $PETSC_DIR/python/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)
2065d5a5a7bSMatthew Knepley  sys.path.insert(0, pythonDir)
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:
218637cc2ebSSatish Balay    framework = config.framework.Framework(sys.argv[1:]+['--configModules=PETSc.Configure','--optionsModule=PETSc.compilerOptions'], 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()
229dd50d019SBarry Smith    return 0
230e69ef9dfSMatthew Knepley  except (RuntimeError, config.base.ConfigureSetupError), e:
2317d670a3cSBarry Smith    emsg = str(e)
23242351d26SSatish Balay    if not emsg.endswith('\n'): emsg = emsg+'\n'
233fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
234fe09c992SBarry Smith    +'         UNABLE to CONFIGURE with GIVEN OPTIONS    (see configure.log for details):\n' \
235fe09c992SBarry Smith    +'---------------------------------------------------------------------------------------\n'  \
2367d670a3cSBarry Smith    +emsg+'*********************************************************************************\n'
237e9f3bb17SBarry Smith    se = ''
2389dd2fdb1SMatthew Knepley  except (TypeError, ValueError), e:
2397d670a3cSBarry Smith    emsg = str(e)
24042351d26SSatish Balay    if not emsg.endswith('\n'): emsg = emsg+'\n'
241fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
242fe09c992SBarry Smith    +'                ERROR in COMMAND LINE ARGUMENT to config/configure.py \n' \
243fe09c992SBarry Smith    +'---------------------------------------------------------------------------------------\n'  \
2447d670a3cSBarry Smith    +emsg+'*********************************************************************************\n'
2451a02243aSBarry Smith    se = ''
24696dc2fe8SMatthew Knepley  except ImportError, e :
2477d670a3cSBarry Smith    emsg = str(e)
24842351d26SSatish Balay    if not emsg.endswith('\n'): emsg = emsg+'\n'
249fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
250fe09c992SBarry Smith    +'                     UNABLE to FIND MODULE for config/configure.py \n' \
251fe09c992SBarry Smith    +'---------------------------------------------------------------------------------------\n'  \
2527d670a3cSBarry Smith    +emsg+'*********************************************************************************\n'
25396dc2fe8SMatthew Knepley    se = ''
25401def6f0SMatthew Knepley  except OSError, e :
25501def6f0SMatthew Knepley    emsg = str(e)
25601def6f0SMatthew Knepley    if not emsg.endswith('\n'): emsg = emsg+'\n'
25701def6f0SMatthew Knepley    msg ='*********************************************************************************\n'\
25801def6f0SMatthew Knepley    +'                    UNABLE to EXECUTE BINARIES for config/configure.py \n' \
25901def6f0SMatthew Knepley    +'---------------------------------------------------------------------------------------\n'  \
26001def6f0SMatthew Knepley    +emsg+'*********************************************************************************\n'
26101def6f0SMatthew Knepley    se = ''
262d7d3c4beSMatthew Knepley  except SystemExit, e:
263d7d3c4beSMatthew Knepley    if e.code is None or e.code == 0:
264d7d3c4beSMatthew Knepley      return
265fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
266fe09c992SBarry Smith    +'           CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
267fe09c992SBarry Smith    +'*********************************************************************************\n'
268d7d3c4beSMatthew Knepley    se  = str(e)
269e9f3bb17SBarry Smith  except Exception, e:
270fe09c992SBarry Smith    msg ='*********************************************************************************\n'\
271fe09c992SBarry Smith    +'          CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
272fe09c992SBarry Smith    +'*********************************************************************************\n'
273e9f3bb17SBarry Smith    se  = str(e)
274e9f3bb17SBarry Smith
275e9f3bb17SBarry Smith  print msg
2769dd2fdb1SMatthew Knepley  if not framework is None:
2779dd2fdb1SMatthew Knepley    framework.logClear()
278e9f3bb17SBarry Smith    if hasattr(framework, 'log'):
279f6614063SBarry Smith      import traceback
280f24f64feSBarry Smith      framework.log.write(msg+se)
281f24f64feSBarry Smith      traceback.print_tb(sys.exc_info()[2], file = framework.log)
2822418bae5SMatthew Knepley      if os.path.isfile(framework.logName+'.bkp'):
2835a74f024SMatthew Knepley        if framework.debugIndent is None:
2845a74f024SMatthew Knepley          framework.debugIndent = '  '
2852418bae5SMatthew Knepley        framework.logPrintDivider()
2862418bae5SMatthew Knepley        framework.logPrintBox('Previous configure logs below', debugSection = None)
2872418bae5SMatthew Knepley        f = file(framework.logName+'.bkp')
2882418bae5SMatthew Knepley        framework.log.write(f.read())
2892418bae5SMatthew Knepley        f.close()
290e9f3bb17SBarry Smith      sys.exit(1)
2915a74f024SMatthew Knepley  else:
2925a74f024SMatthew Knepley    print se
2935a74f024SMatthew Knepley    import traceback
2945a74f024SMatthew Knepley    traceback.print_tb(sys.exc_info()[2])
2955d5a5a7bSMatthew Knepley
2965d5a5a7bSMatthew Knepleyif __name__ == '__main__':
297a030c540SBarry Smith  petsc_configure([])
298759acf64SBarry Smith
299