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