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