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 9# Use en_US as language so that BuildSystem parses compiler messages in english 10if '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' 11if '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' 12 13if not hasattr(sys, 'version_info') or not sys.version_info[0] == 2 or not sys.version_info[1] >= 4: 14 print '*** You must have Python2 version 2.4 or higher to run ./configure *****' 15 print '* Python is easy to install for end users or sys-admin. *' 16 print '* http://www.python.org/download/ *' 17 print '* *' 18 print '* You CANNOT configure PETSc without Python *' 19 print '* http://www.mcs.anl.gov/petsc/documentation/installation.html *' 20 print '*******************************************************************************' 21 sys.exit(4) 22 23def check_for_option_mistakes(opts): 24 for opt in opts[1:]: 25 name = opt.split('=')[0] 26 if name.find('_') >= 0: 27 exception = False 28 for exc in ['mkl_pardiso', 'superlu_dist', 'superlu_mt', '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','int64_t']: 29 if name.find(exc) >= 0: 30 exception = True 31 if not exception: 32 raise ValueError('The option '+name+' should probably be '+name.replace('_', '-')); 33 if opt.find('=') >=0: 34 optval = opt.split('=')[1] 35 if optval == 'ifneeded': 36 raise ValueError('The option '+opt+' should probably be '+opt.replace('ifneeded', '1')); 37 return 38 39def check_for_option_changed(opts): 40# Document changes in command line options here. 41 optMap = [('c-blas-lapack','f2cblaslapack'),('cholmod','suitesparse'),('umfpack','suitesparse'),('f-blas-lapack','fblaslapack')] 42 for opt in opts[1:]: 43 optname = opt.split('=')[0].strip('-') 44 for oldname,newname in optMap: 45 if optname.find(oldname) >=0: 46 raise ValueError('The option '+opt+' should probably be '+opt.replace(oldname,newname)) 47 return 48 49def check_petsc_arch(opts): 50 # If PETSC_ARCH not specified - use script name (if not configure.py) 51 global petsc_arch 52 found = 0 53 for name in opts: 54 if name.find('PETSC_ARCH=') >= 0: 55 petsc_arch=name.split('=')[1] 56 found = 1 57 break 58 # If not yet specified - use the filename of script 59 if not found: 60 filename = os.path.basename(sys.argv[0]) 61 if not filename.startswith('configure') and not filename.startswith('reconfigure') and not filename.startswith('setup'): 62 petsc_arch=os.path.splitext(os.path.basename(sys.argv[0]))[0] 63 useName = 'PETSC_ARCH='+petsc_arch 64 opts.append(useName) 65 return 0 66 67def chkwinf90(): 68 for arg in sys.argv: 69 if (arg.find('win32fe') >= 0 and (arg.find('f90') >=0 or arg.find('ifort') >=0)): 70 return 1 71 return 0 72 73def chkdosfiles(): 74 # cygwin - but not a hg clone - so check one of files in bin dir 75 if "\r\n" in open(os.path.join('bin','petscmpiexec'),"rb").read(): 76 print '===============================================================================' 77 print ' *** Scripts are in DOS mode. Was winzip used to extract petsc sources? ****' 78 print ' *** Please restart with a fresh tarball and use "tar -xzf petsc.tar.gz" ****' 79 print '===============================================================================' 80 sys.exit(3) 81 return 82 83def chkcygwinlink(): 84 if os.path.exists('/usr/bin/cygcheck.exe') and os.path.exists('/usr/bin/link.exe') and chkwinf90(): 85 if '--ignore-cygwin-link' in sys.argv: return 0 86 print '===============================================================================' 87 print ' *** Cygwin /usr/bin/link detected! Compiles with CVF/Intel f90 can break! **' 88 print ' *** To workarround do: "mv /usr/bin/link.exe /usr/bin/link-cygwin.exe" **' 89 print ' *** Or to ignore this check, use configure option: --ignore-cygwin-link **' 90 print '===============================================================================' 91 sys.exit(3) 92 return 0 93 94def chkbrokencygwin(): 95 if os.path.exists('/usr/bin/cygcheck.exe'): 96 buf = os.popen('/usr/bin/cygcheck.exe -c cygwin').read() 97 if buf.find('1.5.11-1') > -1: 98 print '===============================================================================' 99 print ' *** cygwin-1.5.11-1 detected. ./configure fails with this version ***' 100 print ' *** Please upgrade to cygwin-1.5.12-1 or newer version. This can ***' 101 print ' *** be done by running cygwin-setup, selecting "next" all the way.***' 102 print '===============================================================================' 103 sys.exit(3) 104 return 0 105 106def chkusingwindowspython(): 107 if sys.platform == 'win32': 108 print '===============================================================================' 109 print ' *** Windows python detected. Please rerun ./configure with cygwin-python. ***' 110 print '===============================================================================' 111 sys.exit(3) 112 return 0 113 114def chkcygwinpython(): 115 if os.path.exists('/usr/bin/cygcheck.exe') and sys.platform == 'cygwin' : 116 sys.argv.append('--useThreads=0') 117 extraLogs.append('''\ 118=============================================================================== 119** Cygwin-python detected. Threads do not work correctly. *** 120** Disabling thread usage for this run of ./configure ******* 121===============================================================================''') 122 return 0 123 124def chkrhl9(): 125 if os.path.exists('/etc/redhat-release'): 126 try: 127 file = open('/etc/redhat-release','r') 128 buf = file.read() 129 file.close() 130 except: 131 # can't read file - assume dangerous RHL9 132 buf = 'Shrike' 133 if buf.find('Shrike') > -1: 134 sys.argv.append('--useThreads=0') 135 extraLogs.append('''\ 136============================================================================== 137 *** RHL9 detected. Threads do not work correctly with this distribution *** 138 ****** Disabling thread usage for this run of ./configure ********* 139===============================================================================''') 140 return 0 141 142def check_broken_configure_log_links(): 143 '''Sometime symlinks can get broken if the original files are deleted. Delete such broken links''' 144 import os 145 for logfile in ['configure.log','configure.log.bkp']: 146 if os.path.islink(logfile) and not os.path.isfile(logfile): os.remove(logfile) 147 return 148 149def move_configure_log(framework): 150 '''Move configure.log to PETSC_ARCH/conf - and update configure.log.bkp in both locations appropriately''' 151 global petsc_arch 152 153 if hasattr(framework,'arch'): petsc_arch = framework.arch 154 if hasattr(framework,'logName'): curr_file = framework.logName 155 else: curr_file = 'configure.log' 156 157 if petsc_arch: 158 import shutil 159 import os 160 161 # Just in case - confdir is not created 162 conf_dir = os.path.join(petsc_arch,'conf') 163 if not os.path.isdir(petsc_arch): os.mkdir(petsc_arch) 164 if not os.path.isdir(conf_dir): os.mkdir(conf_dir) 165 166 curr_bkp = curr_file + '.bkp' 167 new_file = os.path.join(conf_dir,curr_file) 168 new_bkp = new_file + '.bkp' 169 170 # Keep backup in $PETSC_ARCH/conf location 171 if os.path.isfile(new_bkp): os.remove(new_bkp) 172 if os.path.isfile(new_file): os.rename(new_file,new_bkp) 173 if os.path.isfile(curr_file): 174 shutil.copyfile(curr_file,new_file) 175 os.remove(curr_file) 176 if os.path.isfile(new_file): os.symlink(new_file,curr_file) 177 # If the old bkp is using the same PETSC_ARCH/conf - then update bkp link 178 if os.path.realpath(curr_bkp) == os.path.realpath(new_file): 179 if os.path.isfile(curr_bkp): os.remove(curr_bkp) 180 if os.path.isfile(new_bkp): os.symlink(new_bkp,curr_bkp) 181 return 182 183def print_final_timestamp(framework): 184 import time 185 framework.log.write(('='*80)+'\n') 186 framework.log.write('Finishing Configure Run at '+time.ctime(time.time())+'\n') 187 framework.log.write(('='*80)+'\n') 188 return 189 190def petsc_configure(configure_options): 191 try: 192 petscdir = os.environ['PETSC_DIR'] 193 sys.path.append(os.path.join(petscdir,'bin')) 194 import petscnagupgrade 195 file = os.path.join(petscdir,'.nagged') 196 if not petscnagupgrade.naggedtoday(file): 197 petscnagupgrade.currentversion(petscdir) 198 except: 199 pass 200 print '===============================================================================' 201 print ' Configuring PETSc to compile on your system ' 202 print '===============================================================================' 203 204 try: 205 # Command line arguments take precedence (but don't destroy argv[0]) 206 sys.argv = sys.argv[:1] + configure_options + sys.argv[1:] 207 check_for_option_mistakes(sys.argv) 208 check_for_option_changed(sys.argv) 209 except (TypeError, ValueError), e: 210 emsg = str(e) 211 if not emsg.endswith('\n'): emsg = emsg+'\n' 212 msg ='*******************************************************************************\n'\ 213 +' ERROR in COMMAND LINE ARGUMENT to ./configure \n' \ 214 +'-------------------------------------------------------------------------------\n' \ 215 +emsg+'*******************************************************************************\n' 216 sys.exit(msg) 217 # check PETSC_ARCH 218 check_petsc_arch(sys.argv) 219 check_broken_configure_log_links() 220 221 # support a few standard configure option types 222 for l in range(0,len(sys.argv)): 223 name = sys.argv[l] 224 if name.find('enable-') >= 0: 225 if name.find('=') == -1: 226 sys.argv[l] = name.replace('enable-','with-')+'=1' 227 else: 228 head, tail = name.split('=', 1) 229 sys.argv[l] = head.replace('enable-','with-')+'='+tail 230 if name.find('disable-') >= 0: 231 if name.find('=') == -1: 232 sys.argv[l] = name.replace('disable-','with-')+'=0' 233 else: 234 head, tail = name.split('=', 1) 235 if tail == '1': tail = '0' 236 sys.argv[l] = head.replace('disable-','with-')+'='+tail 237 if name.find('without-') >= 0: 238 if name.find('=') == -1: 239 sys.argv[l] = name.replace('without-','with-')+'=0' 240 else: 241 head, tail = name.split('=', 1) 242 if tail == '1': tail = '0' 243 sys.argv[l] = head.replace('without-','with-')+'='+tail 244 245 # Check for broken cygwin 246 chkbrokencygwin() 247 # Disable threads on RHL9 248 chkrhl9() 249 # Make sure cygwin-python is used on windows 250 chkusingwindowspython() 251 # Threads don't work for cygwin & python... 252 chkcygwinpython() 253 chkcygwinlink() 254 chkdosfiles() 255 256 # Should be run from the toplevel 257 configDir = os.path.abspath('config') 258 bsDir = os.path.join(configDir, 'BuildSystem') 259 if not os.path.isdir(configDir): 260 raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.')) 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=config.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 framework.printSummary() 276 framework.argDB.save(force = True) 277 framework.logClear() 278 print_final_timestamp(framework) 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 try: 336 if hasattr(framework,'compilerDefines'): 337 framework.log.write('**** Configure header '+framework.compilerDefines+' ****\n') 338 framework.outputHeader(framework.log) 339 if hasattr(framework,'compilerFixes'): 340 framework.log.write('**** C specific Configure header '+framework.compilerFixes+' ****\n') 341 framework.outputCHeader(framework.log) 342 except Exception, e: 343 framework.log.write('Problem writing headers to log: '+str(e)) 344 import traceback 345 try: 346 framework.log.write(msg+se) 347 traceback.print_tb(sys.exc_info()[2], file = framework.log) 348 print_final_timestamp(framework) 349 if hasattr(framework,'log'): framework.log.close() 350 move_configure_log(framework) 351 except: 352 pass 353 sys.exit(1) 354 else: 355 print se 356 import traceback 357 traceback.print_tb(sys.exc_info()[2]) 358 if hasattr(framework,'log'): framework.log.close() 359 360if __name__ == '__main__': 361 petsc_configure([]) 362 363