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