xref: /petsc/config/configure.py (revision d3961cc3e093ef1101d6d6cb9074cfaf80e2b657)
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 0
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      print '================================================================================='
39      print ' *** cygwin-1.5.11-1 detected. config/configure.py fails with this version   ***'
40      print ' *** Please upgrade to cygwin-1.5.12-1 or newer version. This can  ***'
41      print ' *** be done by running cygwin-setup, selecting "next" all the way.***'
42      print '================================================================================='
43      sys.exit(3)
44  return 0
45
46def chkusingwindowspython():
47  if os.path.exists('/usr/bin/cygcheck.exe') and sys.platform != 'cygwin':
48    print '================================================================================='
49    print ' *** Non-cygwin python detected. Please rerun config/configure.py with cygwin-python ***'
50    print '================================================================================='
51    sys.exit(3)
52  return 0
53
54def chkcygwinpythonver():
55  if os.path.exists('/usr/bin/cygcheck.exe'):
56    buf = os.popen('/usr/bin/cygcheck.exe -c python').read()
57    if (buf.find('2.4') > -1) or (buf.find('2.5') > -1) or (buf.find('2.6') > -1):
58      sys.argv.append('--useThreads=0')
59      extraLogs.append('''\
60================================================================================
61** Cygwin-python-2.4/2.5 detected. Threads do not work correctly with this version *
62 ********* Disabling thread usage for this run of config/configure.py **********
63================================================================================''')
64  return 0
65
66def chkrhl9():
67  if os.path.exists('/etc/redhat-release'):
68    try:
69      file = open('/etc/redhat-release','r')
70      buf = file.read()
71      file.close()
72    except:
73      # can't read file - assume dangerous RHL9
74      buf = 'Shrike'
75    if buf.find('Shrike') > -1:
76      sys.argv.append('--useThreads=0')
77      extraLogs.append('''\
78================================================================================
79   *** RHL9 detected. Threads do not work correctly with this distribution ***
80    ****** Disabling thread usage for this run of config/configure.py *******
81================================================================================''')
82  return 0
83
84def petsc_configure(configure_options):
85  print '================================================================================='
86  print '             Configuring PETSc to compile on your system                         '
87  print '================================================================================='
88
89  # Command line arguments take precedence (but don't destroy argv[0])
90  sys.argv = sys.argv[:1] + configure_options + sys.argv[1:]
91  # check PETSC_ARCH
92  check_petsc_arch(sys.argv)
93  extraLogs = []
94
95  # support a few standard configure option types
96  for l in range(0,len(sys.argv)):
97    name = sys.argv[l]
98    if name.find('enable-') >= 0:
99      if name.find('=') == -1:
100        sys.argv[l] = name.replace('enable-','with-')+'=1'
101      else:
102        head, tail = name.split('=', 1)
103        sys.argv[l] = head.replace('enable-','with-')+'='+tail
104    if name.find('disable-') >= 0:
105      if name.find('=') == -1:
106        sys.argv[l] = name.replace('disable-','with-')+'=0'
107      else:
108        head, tail = name.split('=', 1)
109        if tail == '1': tail = '0'
110        sys.argv[l] = head.replace('disable-','with-')+'='+tail
111    if name.find('without-') >= 0:
112      if name.find('=') == -1:
113        sys.argv[l] = name.replace('without-','with-')+'=0'
114      else:
115        head, tail = name.split('=', 1)
116        if tail == '1': tail = '0'
117        sys.argv[l] = head.replace('without-','with-')+'='+tail
118
119  # Check for broken cygwin
120  chkbrokencygwin()
121  # Disable threads on RHL9
122  chkrhl9()
123  # Make sure cygwin-python is used on windows
124  chkusingwindowspython()
125  # Threads don't work for cygwin & python-2.4, 2.5 etc..
126  chkcygwinpythonver()
127
128  # Should be run from the toplevel
129  configDir = os.path.abspath('config')
130  bsDir     = os.path.join(configDir, 'BuildSystem')
131  if not os.path.isdir(configDir):
132    raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.'))
133  if not os.path.isdir(bsDir):
134    print '================================================================================='
135    print '''++ Could not locate BuildSystem in %s.''' % configDir
136    print '''++ Downloading it using "hg clone http://hg.mcs.anl.gov/petsc/BuildSystem %s"''' % bsDir
137    print '================================================================================='
138    (status,output) = commands.getstatusoutput('hg clone http://petsc.cs.iit.edu/petsc/BuildSystem '+ bsDir)
139    if status:
140      if output.find('ommand not found') >= 0:
141        print '================================================================================='
142        print '''** Unable to locate hg (Mercurial) to download BuildSystem; make sure hg is in your path'''
143        print '''** or manually copy BuildSystem to $PETSC_DIR/config/BuildSystem from a machine where'''
144        print '''** you do have hg installed and can clone BuildSystem. '''
145        print '================================================================================='
146      elif output.find('Cannot resolve host') >= 0:
147        print '================================================================================='
148        print '''** Unable to download BuildSystem. You must be off the network.'''
149        print '''** Connect to the internet and run config/configure.py again.'''
150        print '================================================================================='
151      else:
152        print '================================================================================='
153        print '''** Unable to download BuildSystem. Please send this message to petsc-maint@mcs.anl.gov'''
154        print '================================================================================='
155      print output
156      sys.exit(3)
157
158  sys.path.insert(0, bsDir)
159  sys.path.insert(0, configDir)
160  import config.base
161  import config.framework
162  import cPickle
163
164  # Disable shared libraries by default
165  import nargs
166  if nargs.Arg.findArgument('with-shared', sys.argv[1:]) is None:
167    sys.argv.append('--with-shared=0')
168
169  framework = None
170  try:
171    framework = config.framework.Framework(['--configModules=PETSc.Configure','--optionsModule=PETSc.compilerOptions']+sys.argv[1:], loadArgDB = 0)
172    framework.setup()
173    framework.logPrint('\n'.join(extraLogs))
174    framework.configure(out = sys.stdout)
175    framework.storeSubstitutions(framework.argDB)
176    framework.argDB['configureCache'] = cPickle.dumps(framework)
177    import PETSc.packages
178    for i in framework.packages:
179      if hasattr(i,'postProcess'):
180        i.postProcess()
181    framework.logClear()
182    framework.closeLog()
183    if hasattr(framework, 'arch'):
184      import shutil
185      shutil.move(framework.logName,os.path.join(framework.arch,'conf',framework.logName))
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 OSError, e :
212    emsg = str(e)
213    if not emsg.endswith('\n'): emsg = emsg+'\n'
214    msg ='*********************************************************************************\n'\
215    +'                    UNABLE to EXECUTE BINARIES for config/configure.py \n' \
216    +'---------------------------------------------------------------------------------------\n'  \
217    +emsg+'*********************************************************************************\n'
218    se = ''
219  except SystemExit, e:
220    if e.code is None or e.code == 0:
221      return
222    msg ='*********************************************************************************\n'\
223    +'           CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
224    +'*********************************************************************************\n'
225    se  = str(e)
226  except Exception, e:
227    msg ='*********************************************************************************\n'\
228    +'          CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
229    +'*********************************************************************************\n'
230    se  = str(e)
231
232  print msg
233  if not framework is None:
234    framework.logClear()
235    if hasattr(framework, 'log'):
236      import traceback
237      framework.log.write(msg+se)
238      traceback.print_tb(sys.exc_info()[2], file = framework.log)
239      if os.path.isfile(framework.logName+'.bkp'):
240        if framework.debugIndent is None:
241          framework.debugIndent = '  '
242        framework.logPrintDivider()
243        framework.logPrintBox('Previous configure logs below', debugSection = None)
244        f = file(framework.logName+'.bkp')
245        framework.log.write(f.read())
246        f.close()
247      sys.exit(1)
248  else:
249    print se
250    import traceback
251    traceback.print_tb(sys.exc_info()[2])
252
253if __name__ == '__main__':
254  petsc_configure([])
255
256