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_cpardiso', '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 chkenable(): 68 #Replace all 'enable-'/'disable-' with 'with-'=0/1/tail 69 #enable-fortran is a special case, the resulting --with-fortran is ambiguous. 70 #Would it mean --with-fc= or --with-fortran-interfaces=? 71 for l in range(0,len(sys.argv)): 72 name = sys.argv[l] 73 if name.find('enable-fortran') >= 0: 74 if name.find('=') == -1: 75 sys.argv[l] = name.replace('enable-fortran','with-fortran-interfaces')+'=1' 76 else: 77 head, tail = name.split('=', 1) 78 sys.argv[l] = head.replace('enable-fortran','with-fortran-interfaces')+'='+tail 79 continue 80 if name.find('disable-fortran') >= 0: 81 if name.find('=') == -1: 82 sys.argv[l] = name.replace('disable-fortran','with-fortran-interfaces')+'=0' 83 else: 84 head, tail = name.split('=', 1) 85 if tail == '1': tail = '0' 86 sys.argv[l] = head.replace('disable-fortran','with-fortran-interfaces')+'='+tail 87 continue 88 89 90 if name.find('enable-') >= 0: 91 if name.find('=') == -1: 92 sys.argv[l] = name.replace('enable-','with-')+'=1' 93 else: 94 head, tail = name.split('=', 1) 95 sys.argv[l] = head.replace('enable-','with-')+'='+tail 96 if name.find('disable-') >= 0: 97 if name.find('=') == -1: 98 sys.argv[l] = name.replace('disable-','with-')+'=0' 99 else: 100 head, tail = name.split('=', 1) 101 if tail == '1': tail = '0' 102 sys.argv[l] = head.replace('disable-','with-')+'='+tail 103 if name.find('without-') >= 0: 104 if name.find('=') == -1: 105 sys.argv[l] = name.replace('without-','with-')+'=0' 106 else: 107 head, tail = name.split('=', 1) 108 if tail == '1': tail = '0' 109 sys.argv[l] = head.replace('without-','with-')+'='+tail 110 111 112def chksynonyms(): 113 #replace common configure options with ones that PETSc BuildSystem recognizes 114 for l in range(0,len(sys.argv)): 115 name = sys.argv[l] 116 117 118 if name.find('with-debug=') >= 0 or name.endswith('with-debug'): 119 if name.find('=') == -1: 120 sys.argv[l] = name.replace('with-debug','with-debugging')+'=1' 121 else: 122 head, tail = name.split('=', 1) 123 sys.argv[l] = head.replace('with-debug','with-debugging')+'='+tail 124 125 if name.find('with-shared=') >= 0 or name.endswith('with-shared'): 126 if name.find('=') == -1: 127 sys.argv[l] = name.replace('with-shared','with-shared-libraries')+'=1' 128 else: 129 head, tail = name.split('=', 1) 130 sys.argv[l] = head.replace('with-shared','with-shared-libraries')+'='+tail 131 132 if name.find('with-index-size=') >=0: 133 head,tail = name.split('=',1) 134 if int(tail)==32: 135 sys.argv[l] = '--with-64-bit-indices=0' 136 elif int(tail)==64: 137 sys.argv[l] = '--with-64-bit-indices=1' 138 else: 139 raise RuntimeError('--with-index-size= must be 32 or 64') 140 141 if name.find('with-precision=') >=0: 142 head,tail = name.split('=',1) 143 if tail.find('quad')>=0: 144 sys.argv[l]='--with-precision=__float128' 145 146 147def chkwinf90(): 148 for arg in sys.argv: 149 if (arg.find('win32fe') >= 0 and (arg.find('f90') >=0 or arg.find('ifort') >=0)): 150 return 1 151 return 0 152 153def chkdosfiles(): 154 # cygwin - but not a hg clone - so check one of files in bin dir 155 if "\r\n" in open(os.path.join('bin','petscmpiexec'),"rb").read(): 156 print '===============================================================================' 157 print ' *** Scripts are in DOS mode. Was winzip used to extract petsc sources? ****' 158 print ' *** Please restart with a fresh tarball and use "tar -xzf petsc.tar.gz" ****' 159 print '===============================================================================' 160 sys.exit(3) 161 return 162 163def chkcygwinlink(): 164 if os.path.exists('/usr/bin/cygcheck.exe') and os.path.exists('/usr/bin/link.exe') and chkwinf90(): 165 if '--ignore-cygwin-link' in sys.argv: return 0 166 print '===============================================================================' 167 print ' *** Cygwin /usr/bin/link detected! Compiles with CVF/Intel f90 can break! **' 168 print ' *** To workarround do: "mv /usr/bin/link.exe /usr/bin/link-cygwin.exe" **' 169 print ' *** Or to ignore this check, use configure option: --ignore-cygwin-link **' 170 print '===============================================================================' 171 sys.exit(3) 172 return 0 173 174def chkbrokencygwin(): 175 if os.path.exists('/usr/bin/cygcheck.exe'): 176 buf = os.popen('/usr/bin/cygcheck.exe -c cygwin').read() 177 if buf.find('1.5.11-1') > -1: 178 print '===============================================================================' 179 print ' *** cygwin-1.5.11-1 detected. ./configure fails with this version ***' 180 print ' *** Please upgrade to cygwin-1.5.12-1 or newer version. This can ***' 181 print ' *** be done by running cygwin-setup, selecting "next" all the way.***' 182 print '===============================================================================' 183 sys.exit(3) 184 return 0 185 186def chkusingwindowspython(): 187 if sys.platform == 'win32': 188 print '===============================================================================' 189 print ' *** Windows python detected. Please rerun ./configure with cygwin-python. ***' 190 print '===============================================================================' 191 sys.exit(3) 192 return 0 193 194def chkcygwinpython(): 195 if os.path.exists('/usr/bin/cygcheck.exe') and sys.platform == 'cygwin' : 196 sys.argv.append('--useThreads=0') 197 extraLogs.append('''\ 198=============================================================================== 199** Cygwin-python detected. Threads do not work correctly. *** 200** Disabling thread usage for this run of ./configure ******* 201===============================================================================''') 202 return 0 203 204def chkrhl9(): 205 if os.path.exists('/etc/redhat-release'): 206 try: 207 file = open('/etc/redhat-release','r') 208 buf = file.read() 209 file.close() 210 except: 211 # can't read file - assume dangerous RHL9 212 buf = 'Shrike' 213 if buf.find('Shrike') > -1: 214 sys.argv.append('--useThreads=0') 215 extraLogs.append('''\ 216============================================================================== 217 *** RHL9 detected. Threads do not work correctly with this distribution *** 218 ****** Disabling thread usage for this run of ./configure ********* 219===============================================================================''') 220 return 0 221 222def check_broken_configure_log_links(): 223 '''Sometime symlinks can get broken if the original files are deleted. Delete such broken links''' 224 import os 225 for logfile in ['configure.log','configure.log.bkp']: 226 if os.path.islink(logfile) and not os.path.isfile(logfile): os.remove(logfile) 227 return 228 229def move_configure_log(framework): 230 '''Move configure.log to PETSC_ARCH/lib/petsc-conf - and update configure.log.bkp in both locations appropriately''' 231 global petsc_arch 232 233 if hasattr(framework,'arch'): petsc_arch = framework.arch 234 if hasattr(framework,'logName'): curr_file = framework.logName 235 else: curr_file = 'configure.log' 236 237 if petsc_arch: 238 import shutil 239 import os 240 241 # Just in case - confdir is not created 242 lib_dir = os.path.join(petsc_arch,'lib') 243 conf_dir = os.path.join(petsc_arch,'lib','petsc-conf') 244 if not os.path.isdir(petsc_arch): os.mkdir(petsc_arch) 245 if not os.path.isdir(lib_dir): os.mkdir(lib_dir) 246 if not os.path.isdir(conf_dir): os.mkdir(conf_dir) 247 248 curr_bkp = curr_file + '.bkp' 249 new_file = os.path.join(conf_dir,curr_file) 250 new_bkp = new_file + '.bkp' 251 252 # Keep backup in $PETSC_ARCH/lib/petsc-conf location 253 if os.path.isfile(new_bkp): os.remove(new_bkp) 254 if os.path.isfile(new_file): os.rename(new_file,new_bkp) 255 if os.path.isfile(curr_file): 256 shutil.copyfile(curr_file,new_file) 257 os.remove(curr_file) 258 if os.path.isfile(new_file): os.symlink(new_file,curr_file) 259 # If the old bkp is using the same PETSC_ARCH/lib/petsc-conf - then update bkp link 260 if os.path.realpath(curr_bkp) == os.path.realpath(new_file): 261 if os.path.isfile(curr_bkp): os.remove(curr_bkp) 262 if os.path.isfile(new_bkp): os.symlink(new_bkp,curr_bkp) 263 return 264 265def print_final_timestamp(framework): 266 import time 267 framework.log.write(('='*80)+'\n') 268 framework.log.write('Finishing Configure Run at '+time.ctime(time.time())+'\n') 269 framework.log.write(('='*80)+'\n') 270 return 271 272def petsc_configure(configure_options): 273 try: 274 petscdir = os.environ['PETSC_DIR'] 275 sys.path.append(os.path.join(petscdir,'bin','petsc-pythonscripts')) 276 import petscnagupgrade 277 file = os.path.join(petscdir,'.nagged') 278 if not petscnagupgrade.naggedtoday(file): 279 petscnagupgrade.currentversion(petscdir) 280 except: 281 pass 282 print '===============================================================================' 283 print ' Configuring PETSc to compile on your system ' 284 print '===============================================================================' 285 286 try: 287 # Command line arguments take precedence (but don't destroy argv[0]) 288 sys.argv = sys.argv[:1] + configure_options + sys.argv[1:] 289 check_for_option_mistakes(sys.argv) 290 check_for_option_changed(sys.argv) 291 except (TypeError, ValueError), e: 292 emsg = str(e) 293 if not emsg.endswith('\n'): emsg = emsg+'\n' 294 msg ='*******************************************************************************\n'\ 295 +' ERROR in COMMAND LINE ARGUMENT to ./configure \n' \ 296 +'-------------------------------------------------------------------------------\n' \ 297 +emsg+'*******************************************************************************\n' 298 sys.exit(msg) 299 # check PETSC_ARCH 300 check_petsc_arch(sys.argv) 301 check_broken_configure_log_links() 302 303 #rename '--enable-' to '--with-' 304 chkenable() 305 # support a few standard configure option types 306 chksynonyms() 307 # Check for broken cygwin 308 chkbrokencygwin() 309 # Disable threads on RHL9 310 chkrhl9() 311 # Make sure cygwin-python is used on windows 312 chkusingwindowspython() 313 # Threads don't work for cygwin & python... 314 chkcygwinpython() 315 chkcygwinlink() 316 chkdosfiles() 317 318 # Should be run from the toplevel 319 configDir = os.path.abspath('config') 320 bsDir = os.path.join(configDir, 'BuildSystem') 321 if not os.path.isdir(configDir): 322 raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.')) 323 sys.path.insert(0, bsDir) 324 sys.path.insert(0, configDir) 325 import config.base 326 import config.framework 327 import cPickle 328 329 framework = None 330 try: 331 framework = config.framework.Framework(['--configModules=PETSc.Configure','--optionsModule=config.compilerOptions']+sys.argv[1:], loadArgDB = 0) 332 framework.setup() 333 framework.logPrint('\n'.join(extraLogs)) 334 framework.configure(out = sys.stdout) 335 framework.storeSubstitutions(framework.argDB) 336 framework.argDB['configureCache'] = cPickle.dumps(framework) 337 framework.printSummary() 338 framework.argDB.save(force = True) 339 framework.logClear() 340 print_final_timestamp(framework) 341 framework.closeLog() 342 try: 343 move_configure_log(framework) 344 except: 345 # perhaps print an error about unable to shuffle logs? 346 pass 347 return 0 348 except (RuntimeError, config.base.ConfigureSetupError), e: 349 emsg = str(e) 350 if not emsg.endswith('\n'): emsg = emsg+'\n' 351 msg ='*******************************************************************************\n'\ 352 +' UNABLE to CONFIGURE with GIVEN OPTIONS (see configure.log for details):\n' \ 353 +'-------------------------------------------------------------------------------\n' \ 354 +emsg+'*******************************************************************************\n' 355 se = '' 356 except (TypeError, ValueError), e: 357 emsg = str(e) 358 if not emsg.endswith('\n'): emsg = emsg+'\n' 359 msg ='*******************************************************************************\n'\ 360 +' ERROR in COMMAND LINE ARGUMENT to ./configure \n' \ 361 +'-------------------------------------------------------------------------------\n' \ 362 +emsg+'*******************************************************************************\n' 363 se = '' 364 except ImportError, e : 365 emsg = str(e) 366 if not emsg.endswith('\n'): emsg = emsg+'\n' 367 msg ='*******************************************************************************\n'\ 368 +' UNABLE to FIND MODULE for ./configure \n' \ 369 +'-------------------------------------------------------------------------------\n' \ 370 +emsg+'*******************************************************************************\n' 371 se = '' 372 except OSError, e : 373 emsg = str(e) 374 if not emsg.endswith('\n'): emsg = emsg+'\n' 375 msg ='*******************************************************************************\n'\ 376 +' UNABLE to EXECUTE BINARIES for ./configure \n' \ 377 +'-------------------------------------------------------------------------------\n' \ 378 +emsg+'*******************************************************************************\n' 379 se = '' 380 except SystemExit, e: 381 if e.code is None or e.code == 0: 382 return 383 msg ='*******************************************************************************\n'\ 384 +' CONFIGURATION FAILURE (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \ 385 +'*******************************************************************************\n' 386 se = str(e) 387 except Exception, e: 388 msg ='*******************************************************************************\n'\ 389 +' CONFIGURATION CRASH (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \ 390 +'*******************************************************************************\n' 391 se = str(e) 392 393 print msg 394 if not framework is None: 395 framework.logClear() 396 if hasattr(framework, 'log'): 397 try: 398 if hasattr(framework,'compilerDefines'): 399 framework.log.write('**** Configure header '+framework.compilerDefines+' ****\n') 400 framework.outputHeader(framework.log) 401 if hasattr(framework,'compilerFixes'): 402 framework.log.write('**** C specific Configure header '+framework.compilerFixes+' ****\n') 403 framework.outputCHeader(framework.log) 404 except Exception, e: 405 framework.log.write('Problem writing headers to log: '+str(e)) 406 import traceback 407 try: 408 framework.log.write(msg+se) 409 traceback.print_tb(sys.exc_info()[2], file = framework.log) 410 print_final_timestamp(framework) 411 if hasattr(framework,'log'): framework.log.close() 412 move_configure_log(framework) 413 except: 414 pass 415 sys.exit(1) 416 else: 417 print se 418 import traceback 419 traceback.print_tb(sys.exc_info()[2]) 420 if hasattr(framework,'log'): framework.log.close() 421 422if __name__ == '__main__': 423 petsc_configure([]) 424 425