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