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