xref: /petsc/config/configure.py (revision bebc3af9e5249a5e0d2b55fe7d8481472c0822f3)
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  foundsudo = 0
97  for l in range(0,len(sys.argv)):
98    if sys.argv[l] == '--with-sudo=sudo':
99      foundsudo = 1
100
101  for l in range(0,len(sys.argv)):
102    name = sys.argv[l]
103    if name.find('enable-') >= 0:
104      if name.find('=') == -1:
105        sys.argv[l] = name.replace('enable-','with-')+'=1'
106      else:
107        head, tail = name.split('=', 1)
108        sys.argv[l] = head.replace('enable-','with-')+'='+tail
109    if name.find('disable-') >= 0:
110      if name.find('=') == -1:
111        sys.argv[l] = name.replace('disable-','with-')+'=0'
112      else:
113        head, tail = name.split('=', 1)
114        if tail == '1': tail = '0'
115        sys.argv[l] = head.replace('disable-','with-')+'='+tail
116    if name.find('without-') >= 0:
117      if name.find('=') == -1:
118        sys.argv[l] = name.replace('without-','with-')+'=0'
119      else:
120        head, tail = name.split('=', 1)
121        if tail == '1': tail = '0'
122        sys.argv[l] = head.replace('without-','with-')+'='+tail
123    if name.find('prefix=') >= 0 and not foundsudo:
124      head, installdir = name.split('=', 1)
125      if os.path.exists(installdir):
126        if not os.access(installdir,os.W_OK):
127          print 'You do not have write access to requested install directory given with --prefix='+installdir+' perhaps use --with-sudo=sudo also'
128          sys.exit(3)
129      else:
130         try:
131           os.mkdir(installdir)
132         except:
133           print 'You do not have write access to create install directory given with --prefix='+installdir+' perhaps use --with-sudo=sudo also'
134           sys.exit(3)
135
136
137  # Check for sudo
138  if os.getuid() == 0:
139    print '================================================================================='
140    print '             *** Do not run configure as root, or using sudo. ***'
141    print '             *** Use the --with-sudo=sudo option to have      ***'
142    print '             *** installs of external packages done with sudo ***'
143    print '             *** use only with --prefix= when installing in   ***'
144    print '             *** system directories                           ***'
145    print '================================================================================='
146    sys.exit(3)
147
148  # Check for broken cygwin
149  chkbrokencygwin()
150  # Disable threads on RHL9
151  chkrhl9()
152  # Make sure cygwin-python is used on windows
153  chkusingwindowspython()
154  # Threads don't work for cygwin & python-2.4, 2.5 etc..
155  chkcygwinpythonver()
156
157  # Should be run from the toplevel
158  configDir = os.path.abspath('config')
159  bsDir     = os.path.join(configDir, 'BuildSystem')
160  if not os.path.isdir(configDir):
161    raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.'))
162  if not os.path.isdir(bsDir):
163    print '================================================================================='
164    print '''++ Could not locate BuildSystem in %s.''' % configDir
165    print '''++ Downloading it using "hg clone http://hg.mcs.anl.gov/petsc/BuildSystem %s"''' % bsDir
166    print '================================================================================='
167    (status,output) = commands.getstatusoutput('hg clone http://petsc.cs.iit.edu/petsc/BuildSystem '+ bsDir)
168    if status:
169      if output.find('ommand not found') >= 0:
170        print '================================================================================='
171        print '''** Unable to locate hg (Mercurial) to download BuildSystem; make sure hg is in your path'''
172        print '''** or manually copy BuildSystem to $PETSC_DIR/config/BuildSystem from a machine where'''
173        print '''** you do have hg installed and can clone BuildSystem. '''
174        print '================================================================================='
175      elif output.find('Cannot resolve host') >= 0:
176        print '================================================================================='
177        print '''** Unable to download BuildSystem. You must be off the network.'''
178        print '''** Connect to the internet and run config/configure.py again.'''
179        print '================================================================================='
180      else:
181        print '================================================================================='
182        print '''** Unable to download BuildSystem. Please send this message to petsc-maint@mcs.anl.gov'''
183        print '================================================================================='
184      print output
185      sys.exit(3)
186
187  sys.path.insert(0, bsDir)
188  sys.path.insert(0, configDir)
189  import config.base
190  import config.framework
191  import cPickle
192
193  # Disable shared libraries by default
194  import nargs
195  if nargs.Arg.findArgument('with-shared', sys.argv[1:]) is None:
196    sys.argv.append('--with-shared=0')
197
198  framework = None
199  try:
200    framework = config.framework.Framework(['--configModules=PETSc.Configure','--optionsModule=PETSc.compilerOptions']+sys.argv[1:], loadArgDB = 0)
201    framework.setup()
202    framework.logPrint('\n'.join(extraLogs))
203    framework.configure(out = sys.stdout)
204    framework.storeSubstitutions(framework.argDB)
205    framework.argDB['configureCache'] = cPickle.dumps(framework)
206    import PETSc.packages
207    for i in framework.packages:
208      if hasattr(i,'postProcess'):
209        i.postProcess()
210    framework.logClear()
211    framework.closeLog()
212    if hasattr(framework, 'arch'):
213      import shutil
214      shutil.move(framework.logName,os.path.join(framework.arch,'conf',framework.logName))
215    return 0
216  except (RuntimeError, config.base.ConfigureSetupError), e:
217    emsg = str(e)
218    if not emsg.endswith('\n'): emsg = emsg+'\n'
219    msg ='*********************************************************************************\n'\
220    +'         UNABLE to CONFIGURE with GIVEN OPTIONS    (see configure.log for details):\n' \
221    +'---------------------------------------------------------------------------------------\n'  \
222    +emsg+'*********************************************************************************\n'
223    se = ''
224  except (TypeError, ValueError), e:
225    emsg = str(e)
226    if not emsg.endswith('\n'): emsg = emsg+'\n'
227    msg ='*********************************************************************************\n'\
228    +'                ERROR in COMMAND LINE ARGUMENT to config/configure.py \n' \
229    +'---------------------------------------------------------------------------------------\n'  \
230    +emsg+'*********************************************************************************\n'
231    se = ''
232  except ImportError, e :
233    emsg = str(e)
234    if not emsg.endswith('\n'): emsg = emsg+'\n'
235    msg ='*********************************************************************************\n'\
236    +'                     UNABLE to FIND MODULE for config/configure.py \n' \
237    +'---------------------------------------------------------------------------------------\n'  \
238    +emsg+'*********************************************************************************\n'
239    se = ''
240  except OSError, e :
241    emsg = str(e)
242    if not emsg.endswith('\n'): emsg = emsg+'\n'
243    msg ='*********************************************************************************\n'\
244    +'                    UNABLE to EXECUTE BINARIES for config/configure.py \n' \
245    +'---------------------------------------------------------------------------------------\n'  \
246    +emsg+'*********************************************************************************\n'
247    se = ''
248  except SystemExit, e:
249    if e.code is None or e.code == 0:
250      return
251    msg ='*********************************************************************************\n'\
252    +'           CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
253    +'*********************************************************************************\n'
254    se  = str(e)
255  except Exception, e:
256    msg ='*********************************************************************************\n'\
257    +'          CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
258    +'*********************************************************************************\n'
259    se  = str(e)
260
261  print msg
262  if not framework is None:
263    framework.logClear()
264    if hasattr(framework, 'log'):
265      import traceback
266      framework.log.write(msg+se)
267      traceback.print_tb(sys.exc_info()[2], file = framework.log)
268      if os.path.isfile(framework.logName+'.bkp'):
269        if framework.debugIndent is None:
270          framework.debugIndent = '  '
271        framework.logPrintDivider()
272        framework.logPrintBox('Previous configure logs below', debugSection = None)
273        f = file(framework.logName+'.bkp')
274        framework.log.write(f.read())
275        f.close()
276      sys.exit(1)
277  else:
278    print se
279    import traceback
280    traceback.print_tb(sys.exc_info()[2])
281
282if __name__ == '__main__':
283  petsc_configure([])
284
285