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