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