xref: /petsc/config/configure.py (revision 26e7fa26ff6a485bf12e845bcb80b878de68671e)
1#!/usr/bin/env python
2import os, sys
3import commands
4# to load ~/.pythonrc.py before inserting correct BuildSystem to path
5import user
6extraLogs = []
7petsc_arch = ''
8
9import urllib
10import tarfile
11
12def untar(tar, path = '.', leading = ''):
13  if leading:
14    entries = [t.name for t in tar.getmembers()]
15    prefix = os.path.commonprefix(entries)
16    if prefix:
17      for tarinfo in tar.getmembers():
18        tail = tarinfo.name.split(prefix, 1)[1]
19        tarinfo.name = os.path.join(leading, tail)
20  for tarinfo in tar.getmembers():
21    tar.extract(tarinfo, path)
22  return
23
24def downloadPackage(url, filename, targetDirname):
25  '''Download the tarball for a package at url, save it as filename, and untar it into targetDirname'''
26  filename, headers = urllib.urlretrieve(url, filename)
27  tar = tarfile.open(filename, 'r:gz')
28  untar(tar, targetDirname, leading = filename.split('.')[0])
29  return
30
31# Use en_US as language so that BuildSystem parses compiler messages in english
32if 'LC_LOCAL' in os.environ and os.environ['LC_LOCAL'] != '' and os.environ['LC_LOCAL'] != 'en_US' and os.environ['LC_LOCAL']!= 'en_US.UTF-8': os.environ['LC_LOCAL'] = 'en_US.UTF-8'
33if 'LANG' in os.environ and os.environ['LANG'] != '' and os.environ['LANG'] != 'en_US' and os.environ['LANG'] != 'en_US.UTF-8': os.environ['LANG'] = 'en_US.UTF-8'
34
35if not hasattr(sys, 'version_info') or not sys.version_info[0] == 2 or not sys.version_info[1] >= 3:
36  print '*** You must have Python2 version 2.3 or higher to run ./configure        *****'
37  print '*          Python is easy to install for end users or sys-admin.              *'
38  print '*                  http://www.python.org/download/                            *'
39  print '*                                                                             *'
40  print '*           You CANNOT configure PETSc without Python                         *'
41  print '*   http://www.mcs.anl.gov/petsc/petsc-as/documentation/installation.html     *'
42  print '*******************************************************************************'
43  sys.exit(4)
44
45if sys.platform == 'win32':
46  print '**** Windows python detected. ****'
47  print sys.version,'on',sys.platform
48  print ''
49  print '** You must use cygwin python, but not windows python with PETSc configure. ***'
50  sys.exit(4)
51
52def check_for_option_mistakes(opts):
53  for opt in opts[1:]:
54    name = opt.split('=')[0]
55    if name.find('_') >= 0:
56      exception = False
57      for exc in ['superlu_dist', 'PETSC_ARCH', 'PETSC_DIR', 'CXX_CXXFLAGS', 'LD_SHARED', 'CC_LINKER_FLAGS', 'CXX_LINKER_FLAGS', 'FC_LINKER_FLAGS', 'AR_FLAGS', 'C_VERSION', 'CXX_VERSION', 'FC_VERSION', 'size_t', 'MPI_Comm','MPI_Fint']:
58        if name.find(exc) >= 0:
59          exception = True
60      if not exception:
61        raise ValueError('The option '+name+' should probably be '+name.replace('_', '-'));
62    if opt.find('=') >=0:
63      optval = opt.split('=')[1]
64      if optval == 'ifneeded':
65        raise ValueError('The option '+opt+' should probably be '+opt.replace('ifneeded', '1'));
66  return
67
68def check_petsc_arch(opts):
69  # If PETSC_ARCH not specified - use script name (if not configure.py)
70  global petsc_arch
71  found = 0
72  for name in opts:
73    if name.find('PETSC_ARCH=') >= 0:
74      petsc_arch=name.split('=')[1]
75      found = 1
76      break
77  # If not yet specified - use the filename of script
78  if not found:
79      filename = os.path.basename(sys.argv[0])
80      if not filename.startswith('configure') and not filename.startswith('reconfigure') and not filename.startswith('setup'):
81        petsc_arch=os.path.splitext(os.path.basename(sys.argv[0]))[0]
82        useName = 'PETSC_ARCH='+petsc_arch
83        opts.append(useName)
84  return 0
85
86def chkwinf90():
87  for arg in sys.argv:
88    if (arg.find('win32fe') >= 0 and (arg.find('f90') >=0 or arg.find('ifort') >=0)):
89      return 1
90  return 0
91
92def chkcygwinlink():
93  if os.path.exists('/usr/bin/cygcheck.exe') and os.path.exists('/usr/bin/link.exe') and chkwinf90():
94      if '--ignore-cygwin-link' in sys.argv: return 0
95      print '==============================================================================='
96      print ' *** Cygwin /usr/bin/link detected! Compiles with CVF/Intel f90 can break!  **'
97      print ' *** To workarround do: "mv /usr/bin/link.exe /usr/bin/link-cygwin.exe"     **'
98      print ' *** Or to ignore this check, use configure option: --ignore-cygwin-link    **'
99      print '==============================================================================='
100      sys.exit(3)
101  return 0
102
103def chkbrokencygwin():
104  if os.path.exists('/usr/bin/cygcheck.exe'):
105    buf = os.popen('/usr/bin/cygcheck.exe -c cygwin').read()
106    if buf.find('1.5.11-1') > -1:
107      print '==============================================================================='
108      print ' *** cygwin-1.5.11-1 detected. ./configure fails with this version ***'
109      print ' *** Please upgrade to cygwin-1.5.12-1 or newer version. This can  ***'
110      print ' *** be done by running cygwin-setup, selecting "next" all the way.***'
111      print '==============================================================================='
112      sys.exit(3)
113  return 0
114
115def chkusingwindowspython():
116  if os.path.exists('/usr/bin/cygcheck.exe') and sys.platform != 'cygwin':
117    print '==============================================================================='
118    print ' *** Non-cygwin python detected. Please rerun ./configure **'
119    print ' *** with cygwin-python. ***'
120    print '==============================================================================='
121    sys.exit(3)
122  return 0
123
124def chkcygwinpythonver():
125  if os.path.exists('/usr/bin/cygcheck.exe'):
126    buf = os.popen('/usr/bin/cygcheck.exe -c python').read()
127    if (buf.find('2.4') > -1) or (buf.find('2.5') > -1) or (buf.find('2.6') > -1):
128      sys.argv.append('--useThreads=0')
129      extraLogs.append('''\
130===============================================================================
131** Cygwin-python-2.4/2.5/2.6 detected. Threads do not work correctly with this
132** version. Disabling thread usage for this run of ./configure *******
133===============================================================================''')
134  return 0
135
136def chkrhl9():
137  if os.path.exists('/etc/redhat-release'):
138    try:
139      file = open('/etc/redhat-release','r')
140      buf = file.read()
141      file.close()
142    except:
143      # can't read file - assume dangerous RHL9
144      buf = 'Shrike'
145    if buf.find('Shrike') > -1:
146      sys.argv.append('--useThreads=0')
147      extraLogs.append('''\
148==============================================================================
149   *** RHL9 detected. Threads do not work correctly with this distribution ***
150   ****** Disabling thread usage for this run of ./configure *********
151===============================================================================''')
152  return 0
153
154def check_broken_configure_log_links():
155  '''Sometime symlinks can get broken if the original files are deleted. Delete such broken links'''
156  import os
157  for logfile in ['configure.log','configure.log.bkp']:
158    if os.path.islink(logfile) and not os.path.isfile(logfile): os.remove(logfile)
159  return
160
161def move_configure_log(framework):
162  '''Move configure.log to PETSC_ARCH/conf - and update configure.log.bkp in both locations appropriately'''
163  global petsc_arch
164
165  if hasattr(framework,'arch'): petsc_arch = framework.arch
166  if hasattr(framework,'logName'): curr_file = framework.logName
167  else: curr_file = 'configure.log'
168
169  if petsc_arch:
170    import shutil
171    import os
172
173    # Just in case - confdir is not created
174    conf_dir = os.path.join(petsc_arch,'conf')
175    if not os.path.isdir(petsc_arch): os.mkdir(petsc_arch)
176    if not os.path.isdir(conf_dir): os.mkdir(conf_dir)
177
178    curr_bkp  = curr_file + '.bkp'
179    new_file  = os.path.join(conf_dir,curr_file)
180    new_bkp   = new_file + '.bkp'
181
182    # Keep backup in $PETSC_ARCH/conf location
183    if os.path.isfile(new_bkp): os.remove(new_bkp)
184    if os.path.isfile(new_file): os.rename(new_file,new_bkp)
185    if os.path.isfile(curr_file):
186      shutil.copyfile(curr_file,new_file)
187      os.remove(curr_file)
188    if os.path.isfile(new_file): os.symlink(new_file,curr_file)
189    # If the old bkp is using the same PETSC_ARCH/conf - then update bkp link
190    if os.path.realpath(curr_bkp) == os.path.realpath(new_file):
191      if os.path.isfile(curr_bkp): os.remove(curr_bkp)
192      if os.path.isfile(new_bkp): os.symlink(new_bkp,curr_bkp)
193  return
194
195def petsc_configure(configure_options):
196  print '==============================================================================='
197  print '             Configuring PETSc to compile on your system                       '
198  print '==============================================================================='
199
200  try:
201    # Command line arguments take precedence (but don't destroy argv[0])
202    sys.argv = sys.argv[:1] + configure_options + sys.argv[1:]
203    check_for_option_mistakes(sys.argv)
204  except (TypeError, ValueError), e:
205    emsg = str(e)
206    if not emsg.endswith('\n'): emsg = emsg+'\n'
207    msg ='*******************************************************************************\n'\
208    +'                ERROR in COMMAND LINE ARGUMENT to ./configure \n' \
209    +'-------------------------------------------------------------------------------\n'  \
210    +emsg+'*******************************************************************************\n'
211    sys.exit(msg)
212  # check PETSC_ARCH
213  check_petsc_arch(sys.argv)
214  check_broken_configure_log_links()
215
216  # support a few standard configure option types
217  for l in range(0,len(sys.argv)):
218    name = sys.argv[l]
219    if name.find('enable-') >= 0:
220      if name.find('=') == -1:
221        sys.argv[l] = name.replace('enable-','with-')+'=1'
222      else:
223        head, tail = name.split('=', 1)
224        sys.argv[l] = head.replace('enable-','with-')+'='+tail
225    if name.find('disable-') >= 0:
226      if name.find('=') == -1:
227        sys.argv[l] = name.replace('disable-','with-')+'=0'
228      else:
229        head, tail = name.split('=', 1)
230        if tail == '1': tail = '0'
231        sys.argv[l] = head.replace('disable-','with-')+'='+tail
232    if name.find('without-') >= 0:
233      if name.find('=') == -1:
234        sys.argv[l] = name.replace('without-','with-')+'=0'
235      else:
236        head, tail = name.split('=', 1)
237        if tail == '1': tail = '0'
238        sys.argv[l] = head.replace('without-','with-')+'='+tail
239
240  # Check for broken cygwin
241  chkbrokencygwin()
242  # Disable threads on RHL9
243  chkrhl9()
244  # Make sure cygwin-python is used on windows
245  chkusingwindowspython()
246  # Threads don't work for cygwin & python-2.4, 2.5 etc..
247  chkcygwinpythonver()
248  chkcygwinlink()
249
250  # Should be run from the toplevel
251  configDir = os.path.abspath('config')
252  bsDir     = os.path.join(configDir, 'BuildSystem')
253  if not os.path.isdir(configDir):
254    raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.'))
255  if not os.path.isdir(bsDir):
256    print '==============================================================================='
257    print '''++ Could not locate BuildSystem in %s.''' % configDir
258    print '''++ Downloading it from http://petsc.cs.iit.edu/petsc/BuildSystem'''
259    print '==============================================================================='
260    downloadPackage('http://petsc.cs.iit.edu/petsc/BuildSystem/archive/tip.tar.gz', 'BuildSystem.tar.gz', configDir)
261
262  sys.path.insert(0, bsDir)
263  sys.path.insert(0, configDir)
264  import config.base
265  import config.framework
266  import cPickle
267
268  framework = None
269  try:
270    framework = config.framework.Framework(['--configModules=PETSc.Configure','--optionsModule=PETSc.compilerOptions']+sys.argv[1:], loadArgDB = 0)
271    framework.setup()
272    framework.logPrint('\n'.join(extraLogs))
273    framework.configure(out = sys.stdout)
274    framework.storeSubstitutions(framework.argDB)
275    framework.argDB['configureCache'] = cPickle.dumps(framework)
276    import PETSc.packages
277    for i in framework.packages:
278      if hasattr(i,'postProcess'):
279        i.postProcess()
280    framework.printSummary()
281    framework.argDB.save(force = True)
282    framework.logClear()
283    framework.closeLog()
284    try:
285      move_configure_log(framework)
286    except:
287      # perhaps print an error about unable to shuffle logs?
288      pass
289    return 0
290  except (RuntimeError, config.base.ConfigureSetupError), e:
291    emsg = str(e)
292    if not emsg.endswith('\n'): emsg = emsg+'\n'
293    msg ='*******************************************************************************\n'\
294    +'         UNABLE to CONFIGURE with GIVEN OPTIONS    (see configure.log for details):\n' \
295    +'-------------------------------------------------------------------------------\n'  \
296    +emsg+'*******************************************************************************\n'
297    se = ''
298  except (TypeError, ValueError), e:
299    emsg = str(e)
300    if not emsg.endswith('\n'): emsg = emsg+'\n'
301    msg ='*******************************************************************************\n'\
302    +'                ERROR in COMMAND LINE ARGUMENT to ./configure \n' \
303    +'-------------------------------------------------------------------------------\n'  \
304    +emsg+'*******************************************************************************\n'
305    se = ''
306  except ImportError, e :
307    emsg = str(e)
308    if not emsg.endswith('\n'): emsg = emsg+'\n'
309    msg ='*******************************************************************************\n'\
310    +'                     UNABLE to FIND MODULE for ./configure \n' \
311    +'-------------------------------------------------------------------------------\n'  \
312    +emsg+'*******************************************************************************\n'
313    se = ''
314  except OSError, e :
315    emsg = str(e)
316    if not emsg.endswith('\n'): emsg = emsg+'\n'
317    msg ='*******************************************************************************\n'\
318    +'                    UNABLE to EXECUTE BINARIES for ./configure \n' \
319    +'-------------------------------------------------------------------------------\n'  \
320    +emsg+'*******************************************************************************\n'
321    se = ''
322  except SystemExit, e:
323    if e.code is None or e.code == 0:
324      return
325    msg ='*******************************************************************************\n'\
326    +'         CONFIGURATION FAILURE  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
327    +'*******************************************************************************\n'
328    se  = str(e)
329  except Exception, e:
330    msg ='*******************************************************************************\n'\
331    +'        CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
332    +'*******************************************************************************\n'
333    se  = str(e)
334
335  print msg
336  if not framework is None:
337    framework.logClear()
338    if hasattr(framework, 'log'):
339      import traceback
340      try:
341        framework.log.write(msg+se)
342        traceback.print_tb(sys.exc_info()[2], file = framework.log)
343        framework.log.close()
344        move_configure_log(framework)
345      except:
346        pass
347      sys.exit(1)
348  else:
349    print se
350    import traceback
351    traceback.print_tb(sys.exc_info()[2])
352
353if __name__ == '__main__':
354  petsc_configure([])
355
356