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