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