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