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