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