xref: /petsc/config/configure.py (revision f490541ab37a4342bf3d5955e59cd802d1fbbf64)
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] >= 4:
36  print '*** You must have Python2 version 2.4 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/documentation/installation.html     *'
42  print '*******************************************************************************'
43  sys.exit(4)
44
45def check_for_option_mistakes(opts):
46  for opt in opts[1:]:
47    name = opt.split('=')[0]
48    if name.find('_') >= 0:
49      exception = False
50      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']:
51        if name.find(exc) >= 0:
52          exception = True
53      if not exception:
54        raise ValueError('The option '+name+' should probably be '+name.replace('_', '-'));
55    if opt.find('=') >=0:
56      optval = opt.split('=')[1]
57      if optval == 'ifneeded':
58        raise ValueError('The option '+opt+' should probably be '+opt.replace('ifneeded', '1'));
59  return
60
61def check_for_option_changed(opts):
62# Document changes in command line options here.
63  optMap = [('c-blas-lapack','f2cblaslapack')]
64  for opt in opts[1:]:
65    optname = opt.split('=')[0].strip('-')
66    for oldname,newname in optMap:
67      if optname.find(oldname) >=0:
68        raise ValueError('The option '+opt+' should probably be '+opt.replace(oldname,newname))
69  return
70
71def check_petsc_arch(opts):
72  # If PETSC_ARCH not specified - use script name (if not configure.py)
73  global petsc_arch
74  found = 0
75  for name in opts:
76    if name.find('PETSC_ARCH=') >= 0:
77      petsc_arch=name.split('=')[1]
78      found = 1
79      break
80  # If not yet specified - use the filename of script
81  if not found:
82      filename = os.path.basename(sys.argv[0])
83      if not filename.startswith('configure') and not filename.startswith('reconfigure') and not filename.startswith('setup'):
84        petsc_arch=os.path.splitext(os.path.basename(sys.argv[0]))[0]
85        useName = 'PETSC_ARCH='+petsc_arch
86        opts.append(useName)
87  return 0
88
89def chkwinf90():
90  for arg in sys.argv:
91    if (arg.find('win32fe') >= 0 and (arg.find('f90') >=0 or arg.find('ifort') >=0)):
92      return 1
93  return 0
94
95def chkdosfiles():
96  # cygwin - but not a hg clone - so check one of files in bin dir
97  if "\r\n" in open(os.path.join('bin','petscmpiexec'),"rb").read():
98    print '==============================================================================='
99    print ' *** Scripts are in DOS mode. Was winzip used to extract petsc sources?    ****'
100    print ' *** Please restart with a fresh tarball and use "tar -xzf petsc.tar.gz"   ****'
101    print '==============================================================================='
102    sys.exit(3)
103  return
104
105def chkcygwinlink():
106  if os.path.exists('/usr/bin/cygcheck.exe') and os.path.exists('/usr/bin/link.exe') and chkwinf90():
107      if '--ignore-cygwin-link' in sys.argv: return 0
108      print '==============================================================================='
109      print ' *** Cygwin /usr/bin/link detected! Compiles with CVF/Intel f90 can break!  **'
110      print ' *** To workarround do: "mv /usr/bin/link.exe /usr/bin/link-cygwin.exe"     **'
111      print ' *** Or to ignore this check, use configure option: --ignore-cygwin-link    **'
112      print '==============================================================================='
113      sys.exit(3)
114  return 0
115
116def chkbrokencygwin():
117  if os.path.exists('/usr/bin/cygcheck.exe'):
118    buf = os.popen('/usr/bin/cygcheck.exe -c cygwin').read()
119    if buf.find('1.5.11-1') > -1:
120      print '==============================================================================='
121      print ' *** cygwin-1.5.11-1 detected. ./configure fails with this version ***'
122      print ' *** Please upgrade to cygwin-1.5.12-1 or newer version. This can  ***'
123      print ' *** be done by running cygwin-setup, selecting "next" all the way.***'
124      print '==============================================================================='
125      sys.exit(3)
126  return 0
127
128def chkusingwindowspython():
129  if sys.platform == 'win32':
130    print '==============================================================================='
131    print ' *** Windows python detected. Please rerun ./configure with cygwin-python. ***'
132    print '==============================================================================='
133    sys.exit(3)
134  return 0
135
136def chkcygwinpython():
137  if os.path.exists('/usr/bin/cygcheck.exe') and sys.platform == 'cygwin' :
138    sys.argv.append('--useThreads=0')
139    extraLogs.append('''\
140===============================================================================
141** Cygwin-python detected. Threads do not work correctly. ***
142** Disabling thread usage for this run of ./configure *******
143===============================================================================''')
144  return 0
145
146def chkrhl9():
147  if os.path.exists('/etc/redhat-release'):
148    try:
149      file = open('/etc/redhat-release','r')
150      buf = file.read()
151      file.close()
152    except:
153      # can't read file - assume dangerous RHL9
154      buf = 'Shrike'
155    if buf.find('Shrike') > -1:
156      sys.argv.append('--useThreads=0')
157      extraLogs.append('''\
158==============================================================================
159   *** RHL9 detected. Threads do not work correctly with this distribution ***
160   ****** Disabling thread usage for this run of ./configure *********
161===============================================================================''')
162  return 0
163
164def check_broken_configure_log_links():
165  '''Sometime symlinks can get broken if the original files are deleted. Delete such broken links'''
166  import os
167  for logfile in ['configure.log','configure.log.bkp']:
168    if os.path.islink(logfile) and not os.path.isfile(logfile): os.remove(logfile)
169  return
170
171def move_configure_log(framework):
172  '''Move configure.log to PETSC_ARCH/conf - and update configure.log.bkp in both locations appropriately'''
173  global petsc_arch
174
175  if hasattr(framework,'arch'): petsc_arch = framework.arch
176  if hasattr(framework,'logName'): curr_file = framework.logName
177  else: curr_file = 'configure.log'
178
179  if petsc_arch:
180    import shutil
181    import os
182
183    # Just in case - confdir is not created
184    conf_dir = os.path.join(petsc_arch,'conf')
185    if not os.path.isdir(petsc_arch): os.mkdir(petsc_arch)
186    if not os.path.isdir(conf_dir): os.mkdir(conf_dir)
187
188    curr_bkp  = curr_file + '.bkp'
189    new_file  = os.path.join(conf_dir,curr_file)
190    new_bkp   = new_file + '.bkp'
191
192    # Keep backup in $PETSC_ARCH/conf location
193    if os.path.isfile(new_bkp): os.remove(new_bkp)
194    if os.path.isfile(new_file): os.rename(new_file,new_bkp)
195    if os.path.isfile(curr_file):
196      shutil.copyfile(curr_file,new_file)
197      os.remove(curr_file)
198    if os.path.isfile(new_file): os.symlink(new_file,curr_file)
199    # If the old bkp is using the same PETSC_ARCH/conf - then update bkp link
200    if os.path.realpath(curr_bkp) == os.path.realpath(new_file):
201      if os.path.isfile(curr_bkp): os.remove(curr_bkp)
202      if os.path.isfile(new_bkp): os.symlink(new_bkp,curr_bkp)
203  return
204
205def petsc_configure(configure_options):
206  try:
207    petscdir = os.environ['PETSC_DIR']
208    sys.path.append(os.path.join(petscdir,'bin'))
209    import petscnagupgrade
210    file     = os.path.join(petscdir,'.nagged')
211    if not petscnagupgrade.naggedtoday(file):
212      petscnagupgrade.currentversion(petscdir)
213  except:
214    pass
215  print '==============================================================================='
216  print '             Configuring PETSc to compile on your system                       '
217  print '==============================================================================='
218
219  try:
220    # Command line arguments take precedence (but don't destroy argv[0])
221    sys.argv = sys.argv[:1] + configure_options + sys.argv[1:]
222    check_for_option_mistakes(sys.argv)
223    check_for_option_changed(sys.argv)
224  except (TypeError, ValueError), e:
225    emsg = str(e)
226    if not emsg.endswith('\n'): emsg = emsg+'\n'
227    msg ='*******************************************************************************\n'\
228    +'                ERROR in COMMAND LINE ARGUMENT to ./configure \n' \
229    +'-------------------------------------------------------------------------------\n'  \
230    +emsg+'*******************************************************************************\n'
231    sys.exit(msg)
232  # check PETSC_ARCH
233  check_petsc_arch(sys.argv)
234  check_broken_configure_log_links()
235
236  # support a few standard configure option types
237  for l in range(0,len(sys.argv)):
238    name = sys.argv[l]
239    if name.find('enable-') >= 0:
240      if name.find('=') == -1:
241        sys.argv[l] = name.replace('enable-','with-')+'=1'
242      else:
243        head, tail = name.split('=', 1)
244        sys.argv[l] = head.replace('enable-','with-')+'='+tail
245    if name.find('disable-') >= 0:
246      if name.find('=') == -1:
247        sys.argv[l] = name.replace('disable-','with-')+'=0'
248      else:
249        head, tail = name.split('=', 1)
250        if tail == '1': tail = '0'
251        sys.argv[l] = head.replace('disable-','with-')+'='+tail
252    if name.find('without-') >= 0:
253      if name.find('=') == -1:
254        sys.argv[l] = name.replace('without-','with-')+'=0'
255      else:
256        head, tail = name.split('=', 1)
257        if tail == '1': tail = '0'
258        sys.argv[l] = head.replace('without-','with-')+'='+tail
259
260  # Check for broken cygwin
261  chkbrokencygwin()
262  # Disable threads on RHL9
263  chkrhl9()
264  # Make sure cygwin-python is used on windows
265  chkusingwindowspython()
266  # Threads don't work for cygwin & python...
267  chkcygwinpython()
268  chkcygwinlink()
269  chkdosfiles()
270
271  # Should be run from the toplevel
272  configDir = os.path.abspath('config')
273  bsDir     = os.path.join(configDir, 'BuildSystem')
274  if not os.path.isdir(configDir):
275    raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.'))
276  sys.path.insert(0, bsDir)
277  sys.path.insert(0, configDir)
278  import config.base
279  import config.framework
280  import cPickle
281
282  framework = None
283  try:
284    framework = config.framework.Framework(['--configModules=PETSc.Configure','--optionsModule=PETSc.compilerOptions']+sys.argv[1:], loadArgDB = 0)
285    framework.setup()
286    framework.logPrint('\n'.join(extraLogs))
287    framework.configure(out = sys.stdout)
288    framework.storeSubstitutions(framework.argDB)
289    framework.argDB['configureCache'] = cPickle.dumps(framework)
290    framework.printSummary()
291    framework.argDB.save(force = True)
292    framework.logClear()
293    framework.closeLog()
294    try:
295      move_configure_log(framework)
296    except:
297      # perhaps print an error about unable to shuffle logs?
298      pass
299    return 0
300  except (RuntimeError, config.base.ConfigureSetupError), e:
301    emsg = str(e)
302    if not emsg.endswith('\n'): emsg = emsg+'\n'
303    msg ='*******************************************************************************\n'\
304    +'         UNABLE to CONFIGURE with GIVEN OPTIONS    (see configure.log for details):\n' \
305    +'-------------------------------------------------------------------------------\n'  \
306    +emsg+'*******************************************************************************\n'
307    se = ''
308  except (TypeError, ValueError), e:
309    emsg = str(e)
310    if not emsg.endswith('\n'): emsg = emsg+'\n'
311    msg ='*******************************************************************************\n'\
312    +'                ERROR in COMMAND LINE ARGUMENT to ./configure \n' \
313    +'-------------------------------------------------------------------------------\n'  \
314    +emsg+'*******************************************************************************\n'
315    se = ''
316  except ImportError, e :
317    emsg = str(e)
318    if not emsg.endswith('\n'): emsg = emsg+'\n'
319    msg ='*******************************************************************************\n'\
320    +'                     UNABLE to FIND MODULE for ./configure \n' \
321    +'-------------------------------------------------------------------------------\n'  \
322    +emsg+'*******************************************************************************\n'
323    se = ''
324  except OSError, e :
325    emsg = str(e)
326    if not emsg.endswith('\n'): emsg = emsg+'\n'
327    msg ='*******************************************************************************\n'\
328    +'                    UNABLE to EXECUTE BINARIES for ./configure \n' \
329    +'-------------------------------------------------------------------------------\n'  \
330    +emsg+'*******************************************************************************\n'
331    se = ''
332  except SystemExit, e:
333    if e.code is None or e.code == 0:
334      return
335    msg ='*******************************************************************************\n'\
336    +'         CONFIGURATION FAILURE  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
337    +'*******************************************************************************\n'
338    se  = str(e)
339  except Exception, e:
340    msg ='*******************************************************************************\n'\
341    +'        CONFIGURATION CRASH  (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \
342    +'*******************************************************************************\n'
343    se  = str(e)
344
345  print msg
346  if not framework is None:
347    framework.logClear()
348    if hasattr(framework, 'log'):
349      try:
350        framework.log.write('**** Configure header '+framework.compilerDefines+' ****\n')
351        framework.outputHeader(framework.log)
352        framework.log.write('**** C specific Configure header '+framework.compilerFixes+' ****\n')
353        framework.outputCHeader(framework.log)
354      except Exception, e:
355        framework.log.write('Problem writing headers to log: '+str(e))
356      import traceback
357      try:
358        framework.log.write(msg+se)
359        traceback.print_tb(sys.exc_info()[2], file = framework.log)
360        if hasattr(framework,'log'): framework.log.close()
361        move_configure_log(framework)
362      except:
363        pass
364      sys.exit(1)
365  else:
366    print se
367    import traceback
368    traceback.print_tb(sys.exc_info()[2])
369  if hasattr(framework,'log'): framework.log.close()
370
371if __name__ == '__main__':
372  petsc_configure([])
373
374