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