xref: /petsc/config/configure.py (revision da1d79b40e932a9330349cd5876a72549b270904)
1#!/usr/bin/env python
2import os
3import sys
4import commands
5# to load ~/.pythonrc.py before inserting correct BuildSystem to path
6import user
7extraLogs = []
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 move_configure_log(framework):
85  '''Move configure.log to PETSC_ARCH/conf - and update configure.log.bkp in both locations appropriately'''
86  if hasattr(framework, 'arch'):
87    import shutil
88    import os
89    curr_file = framework.logName
90    curr_bkp  = curr_file + '.bkp'
91    new_file  = os.path.join(framework.arch,'conf',curr_file)
92    new_bkp   = new_file + '.bkp'
93
94    # Keep backup in $PETSC_ARCH/conf location
95    if os.path.isfile(new_bkp): os.remove(new_bkp)
96    if os.path.isfile(new_file): os.rename(new_file,new_bkp)
97    shutil.move(curr_file,new_file)
98    os.symlink(new_file,curr_file)
99    # If the old bkp is using the same PETSC_ARCH/conf - then update bkp link
100    if os.path.realpath(curr_bkp) == os.path.realpath(new_file):
101      os.remove(curr_bkp)
102      os.symlink(new_bkp,curr_bkp)
103  return
104
105def petsc_configure(configure_options):
106  print '================================================================================='
107  print '             Configuring PETSc to compile on your system                         '
108  print '================================================================================='
109
110  # Command line arguments take precedence (but don't destroy argv[0])
111  sys.argv = sys.argv[:1] + configure_options + sys.argv[1:]
112  # check PETSC_ARCH
113  check_petsc_arch(sys.argv)
114
115  # support a few standard configure option types
116  for l in range(0,len(sys.argv)):
117    name = sys.argv[l]
118    if name.find('enable-') >= 0:
119      if name.find('=') == -1:
120        sys.argv[l] = name.replace('enable-','with-')+'=1'
121      else:
122        head, tail = name.split('=', 1)
123        sys.argv[l] = head.replace('enable-','with-')+'='+tail
124    if name.find('disable-') >= 0:
125      if name.find('=') == -1:
126        sys.argv[l] = name.replace('disable-','with-')+'=0'
127      else:
128        head, tail = name.split('=', 1)
129        if tail == '1': tail = '0'
130        sys.argv[l] = head.replace('disable-','with-')+'='+tail
131    if name.find('without-') >= 0:
132      if name.find('=') == -1:
133        sys.argv[l] = name.replace('without-','with-')+'=0'
134      else:
135        head, tail = name.split('=', 1)
136        if tail == '1': tail = '0'
137        sys.argv[l] = head.replace('without-','with-')+'='+tail
138
139  # Check for broken cygwin
140  chkbrokencygwin()
141  # Disable threads on RHL9
142  chkrhl9()
143  # Make sure cygwin-python is used on windows
144  chkusingwindowspython()
145  # Threads don't work for cygwin & python-2.4, 2.5 etc..
146  chkcygwinpythonver()
147
148  # Should be run from the toplevel
149  configDir = os.path.abspath('config')
150  bsDir     = os.path.join(configDir, 'BuildSystem')
151  if not os.path.isdir(configDir):
152    raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.'))
153  if not os.path.isdir(bsDir):
154    print '================================================================================='
155    print '''++ Could not locate BuildSystem in %s.''' % configDir
156    print '''++ Downloading it using "hg clone http://hg.mcs.anl.gov/petsc/BuildSystem %s"''' % bsDir
157    print '================================================================================='
158    (status,output) = commands.getstatusoutput('hg clone http://petsc.cs.iit.edu/petsc/BuildSystem '+ bsDir)
159    if status:
160      if output.find('ommand not found') >= 0:
161        print '================================================================================='
162        print '''** Unable to locate hg (Mercurial) to download BuildSystem; make sure hg is in your path'''
163        print '''** or manually copy BuildSystem to $PETSC_DIR/config/BuildSystem from a machine where'''
164        print '''** you do have hg installed and can clone BuildSystem. '''
165        print '================================================================================='
166      elif output.find('Cannot resolve host') >= 0:
167        print '================================================================================='
168        print '''** Unable to download BuildSystem. You must be off the network.'''
169        print '''** Connect to the internet and run config/configure.py again.'''
170        print '================================================================================='
171      else:
172        print '================================================================================='
173        print '''** Unable to download BuildSystem. Please send this message to petsc-maint@mcs.anl.gov'''
174        print '================================================================================='
175      print output
176      sys.exit(3)
177
178  sys.path.insert(0, bsDir)
179  sys.path.insert(0, configDir)
180  import config.base
181  import config.framework
182  import cPickle
183
184  # Disable shared libraries by default
185  import nargs
186  if nargs.Arg.findArgument('with-shared', sys.argv[1:]) is None:
187    sys.argv.append('--with-shared=0')
188
189  framework = None
190  try:
191    framework = config.framework.Framework(['--configModules=PETSc.Configure','--optionsModule=PETSc.compilerOptions']+sys.argv[1:], loadArgDB = 0)
192    framework.setup()
193    framework.logPrint('\n'.join(extraLogs))
194    framework.configure(out = sys.stdout)
195    framework.storeSubstitutions(framework.argDB)
196    framework.argDB['configureCache'] = cPickle.dumps(framework)
197    import PETSc.packages
198    for i in framework.packages:
199      if hasattr(i,'postProcess'):
200        i.postProcess()
201    framework.logClear()
202    framework.closeLog()
203    move_configure_log(framework)
204    return 0
205  except (RuntimeError, config.base.ConfigureSetupError), e:
206    emsg = str(e)
207    if not emsg.endswith('\n'): emsg = emsg+'\n'
208    msg ='*********************************************************************************\n'\
209    +'         UNABLE to CONFIGURE with GIVEN OPTIONS    (see configure.log for details):\n' \
210    +'---------------------------------------------------------------------------------------\n'  \
211    +emsg+'*********************************************************************************\n'
212    se = ''
213  except (TypeError, ValueError), e:
214    emsg = str(e)
215    if not emsg.endswith('\n'): emsg = emsg+'\n'
216    msg ='*********************************************************************************\n'\
217    +'                ERROR in COMMAND LINE ARGUMENT to config/configure.py \n' \
218    +'---------------------------------------------------------------------------------------\n'  \
219    +emsg+'*********************************************************************************\n'
220    se = ''
221  except ImportError, e :
222    emsg = str(e)
223    if not emsg.endswith('\n'): emsg = emsg+'\n'
224    msg ='*********************************************************************************\n'\
225    +'                     UNABLE to FIND MODULE for config/configure.py \n' \
226    +'---------------------------------------------------------------------------------------\n'  \
227    +emsg+'*********************************************************************************\n'
228    se = ''
229  except OSError, e :
230    emsg = str(e)
231    if not emsg.endswith('\n'): emsg = emsg+'\n'
232    msg ='*********************************************************************************\n'\
233    +'                    UNABLE to EXECUTE BINARIES for config/configure.py \n' \
234    +'---------------------------------------------------------------------------------------\n'  \
235    +emsg+'*********************************************************************************\n'
236    se = ''
237  except SystemExit, e:
238    if e.code is None or e.code == 0:
239      return
240    msg ='*********************************************************************************\n'\
241    +'           CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
242    +'*********************************************************************************\n'
243    se  = str(e)
244  except Exception, e:
245    msg ='*********************************************************************************\n'\
246    +'          CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
247    +'*********************************************************************************\n'
248    se  = str(e)
249
250  print msg
251  if not framework is None:
252    framework.logClear()
253    if hasattr(framework, 'log'):
254      import traceback
255      framework.log.write(msg+se)
256      traceback.print_tb(sys.exc_info()[2], file = framework.log)
257      move_configure_log(framework)
258      sys.exit(1)
259  else:
260    print se
261    import traceback
262    traceback.print_tb(sys.exc_info()[2])
263  move_configure_log(framework)
264
265if __name__ == '__main__':
266  petsc_configure([])
267
268