xref: /petsc/config/configure.py (revision e2ee2a47cae8248a2ea96b9d6f35ff3f37ecfc00)
1#!/usr/bin/env python
2import os
3import sys
4import commands
5# to load ~/.pythonrc.py before inserting correct BuildSystem to path
6import user
7
8
9if not hasattr(sys, 'version_info') or not sys.version_info[1] >= 2 or not sys.version_info[0] >= 2:
10  print '**** You must have Python version 2.2 or higher to run config/configure.py ******'
11  print '*           Python is easy to install for end users or sys-admin.               *'
12  print '*                   http://www.python.org/download/                             *'
13  print '*                                                                               *'
14  print '*            You CANNOT configure PETSc without Python                          *'
15  print '*    http://www.mcs.anl.gov/petsc/petsc-as/documentation/installation.html      *'
16  print '*********************************************************************************'
17  sys.exit(4)
18
19def check_petsc_arch(opts):
20  # If PETSC_ARCH not specified - use script name (if not configure.py)
21  found = 0
22  for name in opts:
23    if name.find('PETSC_ARCH=') >= 0:
24      found = 1
25      break
26  # If not yet specified - use the filename of script
27  if not found:
28      filename = os.path.basename(sys.argv[0])
29      if not filename.startswith('configure') and not filename.startswith('reconfigure'):
30        useName = 'PETSC_ARCH='+os.path.splitext(os.path.basename(sys.argv[0]))[0]
31        opts.append(useName)
32  return
33
34def chkcygwin():
35  if os.path.exists('/usr/bin/cygcheck.exe'):
36    buf = os.popen('/usr/bin/cygcheck.exe -c cygwin').read()
37    if buf.find('1.5.11-1') > -1:
38      return 1
39    else:
40      return 0
41  return 0
42
43def chkcygwinpython():
44  if os.path.exists('/usr/bin/cygcheck.exe'):
45    buf = os.popen('/usr/bin/cygcheck.exe -c python').read()
46    if buf.find('2.4') > -1:
47      return 1
48    else:
49      return 0
50  return 0
51
52def rhl9():
53  try:
54    file = open('/etc/redhat-release','r')
55  except:
56    return 0
57  try:
58    buf = file.read()
59    file.close()
60  except:
61    # can't read file - assume dangerous RHL9
62    return 1
63  if buf.find('Shrike') > -1:
64    return 1
65  else:
66    return 0
67
68def petsc_configure(configure_options):
69  print '================================================================================='
70  print '             Configuring PETSc to compile on your system                         '
71  print '================================================================================='
72
73  # Command line arguments take precedence (but don't destroy argv[0])
74  sys.argv = sys.argv[:1] + configure_options + sys.argv[1:]
75  # check PETSC_ARCH
76  check_petsc_arch(sys.argv)
77  extraLogs = []
78
79  # support a few standard configure option types
80  for l in range(0,len(sys.argv)):
81    name = sys.argv[l]
82    if name.find('enable-') >= 0:
83      sys.argv[l] = name.replace('enable-','with-')
84      if name.find('=') == -1: sys.argv[l] = sys.argv[l]+'=1'
85    if name.find('disable-') >= 0:
86      sys.argv[l] = name.replace('disable-','with-')
87      if name.find('=') == -1: sys.argv[l] = sys.argv[l]+'=0'
88      elif name.endswith('=1'): sys.argv[l].replace('=1','=0')
89    if name.find('without-') >= 0:
90      sys.argv[l] = name.replace('without-','with-')
91      if name.find('=') == -1: sys.argv[l] = sys.argv[l]+'=0'
92      elif name.endswith('=1'): sys.argv[l].replace('=1','=0')
93
94  # Check for sudo
95  if os.getuid() == 0:
96    print '================================================================================='
97    print '             *** Do not run configure as root, or using sudo. ***'
98    print '             ***** That should be reserved for installation *****'
99    print '================================================================================='
100    sys.exit(3)
101
102  # Check for broken cygwin
103  if chkcygwin():
104    print '================================================================================='
105    print ' *** cygwin-1.5.11-1 detected. config/configure.py fails with this version   ***'
106    print ' *** Please upgrade to cygwin-1.5.12-1 or newer version. This can  ***'
107    print ' *** be done by running cygwin-setup, selecting "next" all the way.***'
108    print '================================================================================='
109    sys.exit(3)
110
111  # Disable threads on RHL9
112  if rhl9():
113    sys.argv.append('--useThreads=0')
114    extraLogs.append('''\
115================================================================================
116   *** RHL9 detected. Threads do not work correctly with this distribution ***
117    ****** Disabling thread usage for this run of config/configure.py *******
118================================================================================''')
119
120  # Threads don't work for cygwin & python-2.4
121  if chkcygwinpython():
122    sys.argv.append('--useThreads=0')
123    extraLogs.append('''\
124================================================================================
125** Cygwin-python-2.4 detected. Threads do not work correctly with this version *
126 ********* Disabling thread usage for this run of config/configure.py **********
127================================================================================''')
128
129  # Should be run from the toplevel
130  pythonDir = os.path.abspath(os.path.join('python'))
131  bsDir     = os.path.join(pythonDir, 'BuildSystem')
132  if not os.path.isdir(pythonDir):
133    raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.'))
134  if not os.path.isdir(bsDir):
135    print '================================================================================='
136    print '''++ Could not locate BuildSystem in %s/python.''' % os.getcwd()
137    print '''++ Downloading it using "bk clone http://sidl.bkbits.net/BuildSystem %s/python/BuildSystem"''' % os.getcwd()
138    print '================================================================================='
139    (status,output) = commands.getstatusoutput('bk clone http://sidl.bkbits.net/BuildSystem python/BuildSystem')
140    if status:
141      if output.find('ommand not found') >= 0:
142        print '================================================================================='
143        print '''** Unable to locate bk (Bitkeeper) to download BuildSystem; make sure bk is in your path'''
144        print '''** or manually copy BuildSystem to $PETSC_DIR/python/BuildSystem from a machine where'''
145        print '''** you do have bk installed and can clone BuildSystem. '''
146        print '================================================================================='
147      elif output.find('Cannot resolve host') >= 0:
148        print '================================================================================='
149        print '''** Unable to download BuildSystem. You must be off the network.'''
150        print '''** Connect to the internet and run config/configure.py again.'''
151        print '================================================================================='
152      else:
153        print '================================================================================='
154        print '''** Unable to download BuildSystem. Please send this message to petsc-maint@mcs.anl.gov'''
155        print '================================================================================='
156      print output
157      sys.exit(3)
158
159  sys.path.insert(0, bsDir)
160  sys.path.insert(0, pythonDir)
161  import config.base
162  import config.framework
163  import cPickle
164
165  # Disable shared libraries by default
166  import nargs
167  if nargs.Arg.findArgument('with-shared', sys.argv[1:]) is None:
168    sys.argv.append('--with-shared=0')
169
170  framework = None
171  try:
172    framework = config.framework.Framework(sys.argv[1:]+['--configModules=PETSc.Configure','--optionsModule=PETSc.compilerOptions'], loadArgDB = 0)
173    framework.setup()
174    framework.logPrint('\n'.join(extraLogs))
175    framework.configure(out = sys.stdout)
176    framework.storeSubstitutions(framework.argDB)
177    framework.argDB['configureCache'] = cPickle.dumps(framework)
178    import PETSc.packages
179    for i in framework.packages:
180      if hasattr(i,'postProcess'):
181        i.postProcess()
182    framework.logClear()
183    return 0
184  except (RuntimeError, config.base.ConfigureSetupError), e:
185    emsg = str(e)
186    if not emsg.endswith('\n'): emsg = emsg+'\n'
187    msg ='*********************************************************************************\n'\
188    +'         UNABLE to CONFIGURE with GIVEN OPTIONS    (see configure.log for details):\n' \
189    +'---------------------------------------------------------------------------------------\n'  \
190    +emsg+'*********************************************************************************\n'
191    se = ''
192  except (TypeError, ValueError), e:
193    emsg = str(e)
194    if not emsg.endswith('\n'): emsg = emsg+'\n'
195    msg ='*********************************************************************************\n'\
196    +'                ERROR in COMMAND LINE ARGUMENT to config/configure.py \n' \
197    +'---------------------------------------------------------------------------------------\n'  \
198    +emsg+'*********************************************************************************\n'
199    se = ''
200  except ImportError, e :
201    emsg = str(e)
202    if not emsg.endswith('\n'): emsg = emsg+'\n'
203    msg ='*********************************************************************************\n'\
204    +'                     UNABLE to FIND MODULE for config/configure.py \n' \
205    +'---------------------------------------------------------------------------------------\n'  \
206    +emsg+'*********************************************************************************\n'
207    se = ''
208  except SystemExit, e:
209    if e.code is None or e.code == 0:
210      return
211    msg ='*********************************************************************************\n'\
212    +'           CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
213    +'*********************************************************************************\n'
214    se  = str(e)
215  except Exception, e:
216    msg ='*********************************************************************************\n'\
217    +'          CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
218    +'*********************************************************************************\n'
219    se  = str(e)
220
221  print msg
222  if not framework is None:
223    framework.logClear()
224    if hasattr(framework, 'log'):
225      import traceback
226      framework.log.write(msg+se)
227      traceback.print_tb(sys.exc_info()[2], file = framework.log)
228      if os.path.isfile(framework.logName+'.bkp'):
229        if framework.debugIndent is None:
230          framework.debugIndent = '  '
231        framework.logPrintDivider()
232        framework.logPrintBox('Previous configure logs below', debugSection = None)
233        f = file(framework.logName+'.bkp')
234        framework.log.write(f.read())
235        f.close()
236      sys.exit(1)
237  else:
238    print se
239    import traceback
240    traceback.print_tb(sys.exc_info()[2])
241
242if __name__ == '__main__':
243  petsc_configure([])
244
245