1#!/usr/bin/env python 2import os, sys 3 4extraLogs = [] 5petsc_arch = '' 6 7# Use en_US as language so that BuildSystem parses compiler messages in english 8if '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' 9if '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' 10 11if not hasattr(sys, 'version_info') or not sys.version_info[0] == 2 or not sys.version_info[1] >= 6: 12 print '*******************************************************************************' 13 print '* Python2 version 2.6 or higher is required to run ./configure *' 14 print '* Try: "python2.7 ./configure" or "python2.6 ./configure" *' 15 print '*******************************************************************************' 16 sys.exit(4) 17 18def check_for_option_mistakes(opts): 19 for opt in opts[1:]: 20 name = opt.split('=')[0] 21 if name.find('_') >= 0: 22 exception = False 23 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']: 24 if name.find(exc) >= 0: 25 exception = True 26 if not exception: 27 raise ValueError('The option '+name+' should probably be '+name.replace('_', '-')); 28 if opt.find('=') >=0: 29 optval = opt.split('=')[1] 30 if optval == 'ifneeded': 31 raise ValueError('The option '+opt+' should probably be '+opt.replace('ifneeded', '1')); 32 return 33 34def check_for_unsupported_combinations(opts): 35 if '--with-precision=single' in opts and '--with-clanguage=cxx' in opts and '--with-scalar-type=complex' in opts: 36 sys.exit(ValueError('PETSc does not support single precision complex with C++ clanguage, run with --with-clanguage=c')) 37 38def check_for_option_changed(opts): 39# Document changes in command line options here. 40 optMap = [('with-64bit-indices','with-64-bit-indices'),('c-blas-lapack','f2cblaslapack'),('cholmod','suitesparse'),('umfpack','suitesparse'),('f-blas-lapack','fblaslapack')] 41 for opt in opts[1:]: 42 optname = opt.split('=')[0].strip('-') 43 for oldname,newname in optMap: 44 if optname.find(oldname) >=0: 45 raise ValueError('The option '+opt+' should probably be '+opt.replace(oldname,newname)) 46 return 47 48def check_petsc_arch(opts): 49 # If PETSC_ARCH not specified - use script name (if not configure.py) 50 global petsc_arch 51 found = 0 52 for name in opts: 53 if name.find('PETSC_ARCH=') >= 0: 54 petsc_arch=name.split('=')[1] 55 found = 1 56 break 57 # If not yet specified - use the filename of script 58 if not found: 59 filename = os.path.basename(sys.argv[0]) 60 if not filename.startswith('configure') and not filename.startswith('reconfigure') and not filename.startswith('setup'): 61 petsc_arch=os.path.splitext(os.path.basename(sys.argv[0]))[0] 62 useName = 'PETSC_ARCH='+petsc_arch 63 opts.append(useName) 64 return 0 65 66def chkenable(): 67 #Replace all 'enable-'/'disable-' with 'with-'=0/1/tail 68 #enable-fortran is a special case, the resulting --with-fortran is ambiguous. 69 #Would it mean --with-fc= 70 for l in range(0,len(sys.argv)): 71 name = sys.argv[l] 72 73 if name.find('enable-cxx') >= 0: 74 if name.find('=') == -1: 75 sys.argv[l] = name.replace('enable-cxx','with-clanguage=C++') 76 else: 77 head, tail = name.split('=', 1) 78 if tail=='0': 79 sys.argv[l] = head.replace('enable-cxx','with-clanguage=C') 80 else: 81 sys.argv[l] = head.replace('enable-cxx','with-clanguage=C++') 82 continue 83 if name.find('disable-cxx') >= 0: 84 if name.find('=') == -1: 85 sys.argv[l] = name.replace('disable-cxx','with-clanguage=C') 86 else: 87 head, tail = name.split('=', 1) 88 if tail == '0': 89 sys.argv[l] = head.replace('disable-cxx','with-clanguage=C++') 90 else: 91 sys.argv[l] = head.replace('disable-cxx','with-clanguage=C') 92 continue 93 94 95 if name.find('enable-') >= 0: 96 if name.find('=') == -1: 97 sys.argv[l] = name.replace('enable-','with-')+'=1' 98 else: 99 head, tail = name.split('=', 1) 100 sys.argv[l] = head.replace('enable-','with-')+'='+tail 101 if name.find('disable-') >= 0: 102 if name.find('=') == -1: 103 sys.argv[l] = name.replace('disable-','with-')+'=0' 104 else: 105 head, tail = name.split('=', 1) 106 if tail == '1': tail = '0' 107 sys.argv[l] = head.replace('disable-','with-')+'='+tail 108 if name.find('without-') >= 0: 109 if name.find('=') == -1: 110 sys.argv[l] = name.replace('without-','with-')+'=0' 111 else: 112 head, tail = name.split('=', 1) 113 if tail == '1': tail = '0' 114 sys.argv[l] = head.replace('without-','with-')+'='+tail 115 116def argsAddDownload(value,deps = [],options = []): 117 # Adds --download-value to args if the command line DOES NOT already has --with-value or --download-value in it 118 # this is to prevent introducing conflicting arguments to ones that already exist 119 for opt in sys.argv[1:]: 120 optname = opt.split('=')[0].strip('-') 121 if optname in ['download-'+value,'with-'+value,'with-'+value+'-dir','with-'+value+'-include','with-'+value+'-lib']: return 122 sys.argv.append('--download-'+value) 123 for i in deps: 124 argsAddDownload(i) 125 for i in options: 126 sys.argv.append(i) 127 128def chksynonyms(): 129 #replace common configure options with ones that PETSc BuildSystem recognizes 130 downloadxsdk = 0 131 downloadideas = 0 132 for l in range(0,len(sys.argv)): 133 name = sys.argv[l] 134 135 if name.find('download-xsdk=') >= 0 or name.endswith('download-xsdk'): 136 downloadxsdk = 1 137 138 if name.find('download-ideas=') >= 0 or name.endswith('download-ideas'): 139 downloadideas = 1 140 141 if name.find('with-debug=') >= 0 or name.endswith('with-debug'): 142 if name.find('=') == -1: 143 sys.argv[l] = name.replace('with-debug','with-debugging')+'=1' 144 else: 145 head, tail = name.split('=', 1) 146 sys.argv[l] = head.replace('with-debug','with-debugging')+'='+tail 147 148 if name.find('with-shared=') >= 0 or name.endswith('with-shared'): 149 if name.find('=') == -1: 150 sys.argv[l] = name.replace('with-shared','with-shared-libraries')+'=1' 151 else: 152 head, tail = name.split('=', 1) 153 sys.argv[l] = head.replace('with-shared','with-shared-libraries')+'='+tail 154 155 if name.find('with-index-size=') >=0: 156 head,tail = name.split('=',1) 157 if int(tail)==32: 158 sys.argv[l] = '--with-64-bit-indices=0' 159 elif int(tail)==64: 160 sys.argv[l] = '--with-64-bit-indices=1' 161 else: 162 raise RuntimeError('--with-index-size= must be 32 or 64') 163 164 if name.find('with-precision=') >=0: 165 head,tail = name.split('=',1) 166 if tail.find('quad')>=0: 167 sys.argv[l]='--with-precision=__float128' 168 169 if downloadideas: 170 downloadxsdk = 1 171 argsAddDownload('pflotran') 172 argsAddDownload('alquimia') 173 # mstk currently cannot build a shared library 174 argsAddDownload('mstk',[],['--download-mstk-shared=0']) 175 argsAddDownload('ascem-io') 176 argsAddDownload('unittestcpp') 177 178 if downloadxsdk: 179 # Common external libraries 180 argsAddDownload('hdf5') 181 argsAddDownload('netcdf') 182 argsAddDownload('metis') 183 184 argsAddDownload('superlu_dist',['parmetis']) 185 186 argsAddDownload('hypre') 187 188 argsAddDownload('trilinos',['boost','xsdktrilinos'],['--with-cxx-dialect=C++11']) 189 190 191def chkwinf90(): 192 for arg in sys.argv: 193 if (arg.find('win32fe') >= 0 and (arg.find('f90') >=0 or arg.find('ifort') >=0)): 194 return 1 195 return 0 196 197def chkdosfiles(): 198 # cygwin - but not a hg clone - so check one of files in bin dir 199 if "\r\n" in open(os.path.join('bin','petscmpiexec'),"rb").read(): 200 print '===============================================================================' 201 print ' *** Scripts are in DOS mode. Was winzip used to extract petsc sources? ****' 202 print ' *** Please restart with a fresh tarball and use "tar -xzf petsc.tar.gz" ****' 203 print '===============================================================================' 204 sys.exit(3) 205 return 206 207def chkcygwinlink(): 208 if os.path.exists('/usr/bin/cygcheck.exe') and os.path.exists('/usr/bin/link.exe') and chkwinf90(): 209 if '--ignore-cygwin-link' in sys.argv: return 0 210 print '===============================================================================' 211 print ' *** Cygwin /usr/bin/link detected! Compiles with CVF/Intel f90 can break! **' 212 print ' *** To workarround do: "mv /usr/bin/link.exe /usr/bin/link-cygwin.exe" **' 213 print ' *** Or to ignore this check, use configure option: --ignore-cygwin-link **' 214 print '===============================================================================' 215 sys.exit(3) 216 return 0 217 218def chkbrokencygwin(): 219 if os.path.exists('/usr/bin/cygcheck.exe'): 220 buf = os.popen('/usr/bin/cygcheck.exe -c cygwin').read() 221 if buf.find('1.5.11-1') > -1: 222 print '===============================================================================' 223 print ' *** cygwin-1.5.11-1 detected. ./configure fails with this version ***' 224 print ' *** Please upgrade to cygwin-1.5.12-1 or newer version. This can ***' 225 print ' *** be done by running cygwin-setup, selecting "next" all the way.***' 226 print '===============================================================================' 227 sys.exit(3) 228 return 0 229 230def chkusingwindowspython(): 231 if sys.platform == 'win32': 232 print '===============================================================================' 233 print ' *** Windows python detected. Please rerun ./configure with cygwin-python. ***' 234 print '===============================================================================' 235 sys.exit(3) 236 return 0 237 238def chkcygwinpython(): 239 if sys.platform == 'cygwin' : 240 import platform 241 import re 242 r=re.compile("([0-9]+).([0-9]+).([0-9]+)") 243 m=r.match(platform.release()) 244 major=int(m.group(1)) 245 minor=int(m.group(2)) 246 subminor=int(m.group(3)) 247 if ((major < 1) or (major == 1 and minor < 7) or (major == 1 and minor == 7 and subminor < 34)): 248 sys.argv.append('--useThreads=0') 249 extraLogs.append('''\ 250=============================================================================== 251** Cygwin version is older than 1.7.34. Python threads do not work correctly. *** 252** Disabling thread usage for this run of ./configure ******* 253===============================================================================''') 254 return 0 255 256def chkrhl9(): 257 if os.path.exists('/etc/redhat-release'): 258 try: 259 file = open('/etc/redhat-release','r') 260 buf = file.read() 261 file.close() 262 except: 263 # can't read file - assume dangerous RHL9 264 buf = 'Shrike' 265 if buf.find('Shrike') > -1: 266 sys.argv.append('--useThreads=0') 267 extraLogs.append('''\ 268============================================================================== 269 *** RHL9 detected. Threads do not work correctly with this distribution *** 270 ****** Disabling thread usage for this run of ./configure ********* 271===============================================================================''') 272 return 0 273 274def check_broken_configure_log_links(): 275 '''Sometime symlinks can get broken if the original files are deleted. Delete such broken links''' 276 import os 277 for logfile in ['configure.log','configure.log.bkp']: 278 if os.path.islink(logfile) and not os.path.isfile(logfile): os.remove(logfile) 279 return 280 281def move_configure_log(framework): 282 '''Move configure.log to PETSC_ARCH/lib/petsc/conf - and update configure.log.bkp in both locations appropriately''' 283 global petsc_arch 284 285 if hasattr(framework,'arch'): petsc_arch = framework.arch 286 if hasattr(framework,'logName'): curr_file = framework.logName 287 else: curr_file = 'configure.log' 288 289 if petsc_arch: 290 import shutil 291 import os 292 293 # Just in case - confdir is not created 294 lib_dir = os.path.join(petsc_arch,'lib') 295 conf_dir = os.path.join(petsc_arch,'lib','petsc','conf') 296 if not os.path.isdir(petsc_arch): os.mkdir(petsc_arch) 297 if not os.path.isdir(lib_dir): os.mkdir(lib_dir) 298 if not os.path.isdir(conf_dir): os.mkdir(conf_dir) 299 300 curr_bkp = curr_file + '.bkp' 301 new_file = os.path.join(conf_dir,curr_file) 302 new_bkp = new_file + '.bkp' 303 304 # Keep backup in $PETSC_ARCH/lib/petsc/conf location 305 if os.path.isfile(new_bkp): os.remove(new_bkp) 306 if os.path.isfile(new_file): os.rename(new_file,new_bkp) 307 if os.path.isfile(curr_file): 308 shutil.copyfile(curr_file,new_file) 309 os.remove(curr_file) 310 if os.path.isfile(new_file): os.symlink(new_file,curr_file) 311 # If the old bkp is using the same PETSC_ARCH/lib/petsc/conf - then update bkp link 312 if os.path.realpath(curr_bkp) == os.path.realpath(new_file): 313 if os.path.isfile(curr_bkp): os.remove(curr_bkp) 314 if os.path.isfile(new_bkp): os.symlink(new_bkp,curr_bkp) 315 return 316 317def print_final_timestamp(framework): 318 import time 319 framework.log.write(('='*80)+'\n') 320 framework.log.write('Finishing Configure Run at '+time.ctime(time.time())+'\n') 321 framework.log.write(('='*80)+'\n') 322 return 323 324def petsc_configure(configure_options): 325 if 'PETSC_DIR' in os.environ: 326 petscdir = os.environ['PETSC_DIR'] 327 if petscdir.find(' ') > -1: 328 raise RuntimeError('Your PETSC_DIR '+petscdir+' has spaces in it; this is not allowed.\n Change the directory with PETSc to not have spaces in it') 329 try: 330 sys.path.append(os.path.join(petscdir,'bin')) 331 import petscnagupgrade 332 file = os.path.join(petscdir,'.nagged') 333 if not petscnagupgrade.naggedtoday(file): 334 petscnagupgrade.currentversion(petscdir) 335 except: 336 pass 337 print '===============================================================================' 338 print ' Configuring PETSc to compile on your system ' 339 print '===============================================================================' 340 341 try: 342 # Command line arguments take precedence (but don't destroy argv[0]) 343 sys.argv = sys.argv[:1] + configure_options + sys.argv[1:] 344 check_for_option_mistakes(sys.argv) 345 check_for_option_changed(sys.argv) 346 except (TypeError, ValueError), e: 347 emsg = str(e) 348 if not emsg.endswith('\n'): emsg = emsg+'\n' 349 msg ='*******************************************************************************\n'\ 350 +' ERROR in COMMAND LINE ARGUMENT to ./configure \n' \ 351 +'-------------------------------------------------------------------------------\n' \ 352 +emsg+'*******************************************************************************\n' 353 sys.exit(msg) 354 # check PETSC_ARCH 355 check_for_unsupported_combinations(sys.argv) 356 check_petsc_arch(sys.argv) 357 check_broken_configure_log_links() 358 359 #rename '--enable-' to '--with-' 360 chkenable() 361 # support a few standard configure option types 362 chksynonyms() 363 # Check for broken cygwin 364 chkbrokencygwin() 365 # Disable threads on RHL9 366 chkrhl9() 367 # Make sure cygwin-python is used on windows 368 chkusingwindowspython() 369 # Threads don't work for cygwin & python... 370 chkcygwinpython() 371 chkcygwinlink() 372 chkdosfiles() 373 374 # Should be run from the toplevel 375 configDir = os.path.abspath('config') 376 bsDir = os.path.join(configDir, 'BuildSystem') 377 if not os.path.isdir(configDir): 378 raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.')) 379 sys.path.insert(0, bsDir) 380 sys.path.insert(0, configDir) 381 import config.base 382 import config.framework 383 import cPickle 384 385 framework = None 386 try: 387 framework = config.framework.Framework(['--configModules=PETSc.Configure','--optionsModule=config.compilerOptions']+sys.argv[1:], loadArgDB = 0) 388 framework.setup() 389 framework.logPrint('\n'.join(extraLogs)) 390 framework.configure(out = sys.stdout) 391 framework.storeSubstitutions(framework.argDB) 392 framework.argDB['configureCache'] = cPickle.dumps(framework) 393 framework.printSummary() 394 framework.argDB.save(force = True) 395 framework.logClear() 396 print_final_timestamp(framework) 397 framework.closeLog() 398 try: 399 move_configure_log(framework) 400 except: 401 # perhaps print an error about unable to shuffle logs? 402 pass 403 return 0 404 except (RuntimeError, config.base.ConfigureSetupError), e: 405 emsg = str(e) 406 if not emsg.endswith('\n'): emsg = emsg+'\n' 407 msg ='*******************************************************************************\n'\ 408 +' UNABLE to CONFIGURE with GIVEN OPTIONS (see configure.log for details):\n' \ 409 +'-------------------------------------------------------------------------------\n' \ 410 +emsg+'*******************************************************************************\n' 411 se = '' 412 except (TypeError, ValueError), e: 413 emsg = str(e) 414 if not emsg.endswith('\n'): emsg = emsg+'\n' 415 msg ='*******************************************************************************\n'\ 416 +' ERROR in COMMAND LINE ARGUMENT to ./configure \n' \ 417 +'-------------------------------------------------------------------------------\n' \ 418 +emsg+'*******************************************************************************\n' 419 se = '' 420 except ImportError, e : 421 emsg = str(e) 422 if not emsg.endswith('\n'): emsg = emsg+'\n' 423 msg ='*******************************************************************************\n'\ 424 +' UNABLE to FIND MODULE for ./configure \n' \ 425 +'-------------------------------------------------------------------------------\n' \ 426 +emsg+'*******************************************************************************\n' 427 se = '' 428 except OSError, e : 429 emsg = str(e) 430 if not emsg.endswith('\n'): emsg = emsg+'\n' 431 msg ='*******************************************************************************\n'\ 432 +' UNABLE to EXECUTE BINARIES for ./configure \n' \ 433 +'-------------------------------------------------------------------------------\n' \ 434 +emsg+'*******************************************************************************\n' 435 se = '' 436 except SystemExit, e: 437 if e.code is None or e.code == 0: 438 return 439 if e.code is 10: 440 sys.exit(10) 441 msg ='*******************************************************************************\n'\ 442 +' CONFIGURATION FAILURE (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \ 443 +'*******************************************************************************\n' 444 se = str(e) 445 except Exception, e: 446 msg ='*******************************************************************************\n'\ 447 +' CONFIGURATION CRASH (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \ 448 +'*******************************************************************************\n' 449 se = str(e) 450 451 print msg 452 if not framework is None: 453 framework.logClear() 454 if hasattr(framework, 'log'): 455 try: 456 if hasattr(framework,'compilerDefines'): 457 framework.log.write('**** Configure header '+framework.compilerDefines+' ****\n') 458 framework.outputHeader(framework.log) 459 if hasattr(framework,'compilerFixes'): 460 framework.log.write('**** C specific Configure header '+framework.compilerFixes+' ****\n') 461 framework.outputCHeader(framework.log) 462 except Exception, e: 463 framework.log.write('Problem writing headers to log: '+str(e)) 464 import traceback 465 try: 466 framework.log.write(msg+se) 467 traceback.print_tb(sys.exc_info()[2], file = framework.log) 468 print_final_timestamp(framework) 469 if hasattr(framework,'log'): framework.log.close() 470 move_configure_log(framework) 471 except: 472 pass 473 sys.exit(1) 474 else: 475 print se 476 import traceback 477 traceback.print_tb(sys.exc_info()[2]) 478 if hasattr(framework,'log'): framework.log.close() 479 480if __name__ == '__main__': 481 petsc_configure([]) 482 483