xref: /petsc/config/configure.py (revision fd0ecfce1f1f7352fc1de7f539dc9da29179a274)
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 chkbrokencygwin():
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 chkusingwindowspython():
44  if os.path.exists('/usr/bin/cygcheck.exe'):
45    if sys.platform != 'cygwin':
46      return 1
47  return 0
48
49def chkcygwinpythonver():
50  if os.path.exists('/usr/bin/cygcheck.exe'):
51    buf = os.popen('/usr/bin/cygcheck.exe -c python').read()
52    if (buf.find('2.4') > -1) or (buf.find('2.5') > -1) or (buf.find('2.6') > -1):
53      return 1
54    else:
55      return 0
56  return 0
57
58def rhl9():
59  try:
60    file = open('/etc/redhat-release','r')
61  except:
62    return 0
63  try:
64    buf = file.read()
65    file.close()
66  except:
67    # can't read file - assume dangerous RHL9
68    return 1
69  if buf.find('Shrike') > -1:
70    return 1
71  else:
72    return 0
73
74def chkBrokenF8Diff():
75  if os.path.exists('/bin/rpm'):
76    buf = os.popen('/bin/rpm -q diffutils').read()
77  if buf.find('diffutils-2.8.1-17.fc8') > -1:
78    return 1
79  else:
80    return 0
81
82
83def petsc_configure(configure_options):
84  print '================================================================================='
85  print '             Configuring PETSc to compile on your system                         '
86  print '================================================================================='
87
88  # Command line arguments take precedence (but don't destroy argv[0])
89  sys.argv = sys.argv[:1] + configure_options + sys.argv[1:]
90  # check PETSC_ARCH
91  check_petsc_arch(sys.argv)
92  extraLogs = []
93
94  # support a few standard configure option types
95  for l in range(0,len(sys.argv)):
96    name = sys.argv[l]
97    if name.find('enable-') >= 0:
98      sys.argv[l] = name.replace('enable-','with-')
99      if name.find('=') == -1: sys.argv[l] = sys.argv[l]+'=1'
100    if name.find('disable-') >= 0:
101      sys.argv[l] = name.replace('disable-','with-')
102      if name.find('=') == -1: sys.argv[l] = sys.argv[l]+'=0'
103      elif name.endswith('=1'): sys.argv[l].replace('=1','=0')
104    if name.find('without-') >= 0:
105      sys.argv[l] = name.replace('without-','with-')
106      if name.find('=') == -1: sys.argv[l] = sys.argv[l]+'=0'
107      elif name.endswith('=1'): sys.argv[l].replace('=1','=0')
108
109  # Check for sudo
110  if os.getuid() == 0:
111    print '================================================================================='
112    print '             *** Do not run configure as root, or using sudo. ***'
113    print '             *** Use the --with-sudo=sudo option to have      ***'
114    print '             *** installs of external packages done with sudo ***'
115    print '             *** use only with --prefix= when installing in   ***'
116    print '             *** system directories                           ***'
117    print '================================================================================='
118    sys.exit(3)
119
120  # Check for broken cygwin
121  if chkbrokencygwin():
122    print '================================================================================='
123    print ' *** cygwin-1.5.11-1 detected. config/configure.py fails with this version   ***'
124    print ' *** Please upgrade to cygwin-1.5.12-1 or newer version. This can  ***'
125    print ' *** be done by running cygwin-setup, selecting "next" all the way.***'
126    print '================================================================================='
127    sys.exit(3)
128
129  # Disable threads on RHL9
130  if rhl9():
131    sys.argv.append('--useThreads=0')
132    extraLogs.append('''\
133================================================================================
134   *** RHL9 detected. Threads do not work correctly with this distribution ***
135    ****** Disabling thread usage for this run of config/configure.py *******
136================================================================================''')
137
138  # Check for broken diff on Fedora8
139  if chkBrokenF8Diff():
140    print '================================================================================='
141    print ' *** Fedora 8 Linux with broken diffutils-2.8.1-17.fc8 detected. ****************'
142    print ' *** Please run "sudo yum update diffutils" to get the latest bugfixed version.**'
143    print '================================================================================='
144    sys.exit(3)
145
146  # Make sure cygwin-python is used on windows
147  if chkusingwindowspython():
148    print '================================================================================='
149    print ' *** Non-cygwin python detected. Please rerun config/configure.py with cygwin-python ***'
150    print '================================================================================='
151    sys.exit(3)
152
153  # Threads don't work for cygwin & python-2.4, 2.5 etc..
154  if chkcygwinpythonver():
155    sys.argv.append('--useThreads=0')
156    extraLogs.append('''\
157================================================================================
158** Cygwin-python-2.4/2.5 detected. Threads do not work correctly with this version *
159 ********* Disabling thread usage for this run of config/configure.py **********
160================================================================================''')
161
162  # Should be run from the toplevel
163  pythonDir = os.path.abspath(os.path.join('python'))
164  bsDir     = os.path.join(pythonDir, 'BuildSystem')
165  if not os.path.isdir(pythonDir):
166    raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.'))
167  if not os.path.isdir(bsDir):
168    print '================================================================================='
169    print '''++ Could not locate BuildSystem in %s/python.''' % os.getcwd()
170    print '''++ Downloading it using "hg clone http://hg.mcs.anl.gov/petsc/BuildSystem %s/python/BuildSystem"''' % os.getcwd()
171    print '================================================================================='
172    (status,output) = commands.getstatusoutput('hg clone http://hg.mcs.anl.gov/petsc/BuildSystem python/BuildSystem')
173    if status:
174      if output.find('ommand not found') >= 0:
175        print '================================================================================='
176        print '''** Unable to locate hg (Mercurial) to download BuildSystem; make sure hg is in your path'''
177        print '''** or manually copy BuildSystem to $PETSC_DIR/python/BuildSystem from a machine where'''
178        print '''** you do have hg installed and can clone BuildSystem. '''
179        print '================================================================================='
180      elif output.find('Cannot resolve host') >= 0:
181        print '================================================================================='
182        print '''** Unable to download BuildSystem. You must be off the network.'''
183        print '''** Connect to the internet and run config/configure.py again.'''
184        print '================================================================================='
185      else:
186        print '================================================================================='
187        print '''** Unable to download BuildSystem. Please send this message to petsc-maint@mcs.anl.gov'''
188        print '================================================================================='
189      print output
190      sys.exit(3)
191
192  sys.path.insert(0, bsDir)
193  sys.path.insert(0, pythonDir)
194  import config.base
195  import config.framework
196  import cPickle
197
198  # Disable shared libraries by default
199  import nargs
200  if nargs.Arg.findArgument('with-shared', sys.argv[1:]) is None:
201    sys.argv.append('--with-shared=0')
202
203  framework = None
204  try:
205    framework = config.framework.Framework(sys.argv[1:]+['--configModules=PETSc.Configure','--optionsModule=PETSc.compilerOptions'], loadArgDB = 0)
206    framework.setup()
207    framework.logPrint('\n'.join(extraLogs))
208    framework.configure(out = sys.stdout)
209    framework.storeSubstitutions(framework.argDB)
210    framework.argDB['configureCache'] = cPickle.dumps(framework)
211    import PETSc.packages
212    for i in framework.packages:
213      if hasattr(i,'postProcess'):
214        i.postProcess()
215    framework.logClear()
216    return 0
217  except (RuntimeError, config.base.ConfigureSetupError), e:
218    emsg = str(e)
219    if not emsg.endswith('\n'): emsg = emsg+'\n'
220    msg ='*********************************************************************************\n'\
221    +'         UNABLE to CONFIGURE with GIVEN OPTIONS    (see configure.log for details):\n' \
222    +'---------------------------------------------------------------------------------------\n'  \
223    +emsg+'*********************************************************************************\n'
224    se = ''
225  except (TypeError, ValueError), e:
226    emsg = str(e)
227    if not emsg.endswith('\n'): emsg = emsg+'\n'
228    msg ='*********************************************************************************\n'\
229    +'                ERROR in COMMAND LINE ARGUMENT to config/configure.py \n' \
230    +'---------------------------------------------------------------------------------------\n'  \
231    +emsg+'*********************************************************************************\n'
232    se = ''
233  except ImportError, e :
234    emsg = str(e)
235    if not emsg.endswith('\n'): emsg = emsg+'\n'
236    msg ='*********************************************************************************\n'\
237    +'                     UNABLE to FIND MODULE for config/configure.py \n' \
238    +'---------------------------------------------------------------------------------------\n'  \
239    +emsg+'*********************************************************************************\n'
240    se = ''
241  except OSError, e :
242    emsg = str(e)
243    if not emsg.endswith('\n'): emsg = emsg+'\n'
244    msg ='*********************************************************************************\n'\
245    +'                    UNABLE to EXECUTE BINARIES for config/configure.py \n' \
246    +'---------------------------------------------------------------------------------------\n'  \
247    +emsg+'*********************************************************************************\n'
248    se = ''
249  except SystemExit, e:
250    if e.code is None or e.code == 0:
251      return
252    msg ='*********************************************************************************\n'\
253    +'           CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
254    +'*********************************************************************************\n'
255    se  = str(e)
256  except Exception, e:
257    msg ='*********************************************************************************\n'\
258    +'          CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
259    +'*********************************************************************************\n'
260    se  = str(e)
261
262  print msg
263  if not framework is None:
264    framework.logClear()
265    if hasattr(framework, 'log'):
266      import traceback
267      framework.log.write(msg+se)
268      traceback.print_tb(sys.exc_info()[2], file = framework.log)
269      if os.path.isfile(framework.logName+'.bkp'):
270        if framework.debugIndent is None:
271          framework.debugIndent = '  '
272        framework.logPrintDivider()
273        framework.logPrintBox('Previous configure logs below', debugSection = None)
274        f = file(framework.logName+'.bkp')
275        framework.log.write(f.read())
276        f.close()
277      sys.exit(1)
278  else:
279    print se
280    import traceback
281    traceback.print_tb(sys.exc_info()[2])
282
283if __name__ == '__main__':
284  petsc_configure([])
285
286