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