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 sys.version_info < (2,6): 13 print('************************************************************************') 14 print('* Python version 2.6+ or 3.4+ is required to run ./configure *') 15 print('* Try: "python2.7 ./configure" or "python3 ./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 raise ValueError('The option "'+name+'" has a space character in the name - this is likely incorrect usage.'); 24 if name.find('_') >= 0: 25 exception = False 26 for exc in ['mkl_sparse', 'mkl_sparse_optimize', 'mkl_cpardiso', 'mkl_pardiso', 'superlu_dist', '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']: 27 if name.find(exc) >= 0: 28 exception = True 29 if not exception: 30 raise ValueError('The option '+name+' should probably be '+name.replace('_', '-')); 31 if opt.find('=') >=0: 32 optval = opt.split('=')[1] 33 if optval == 'ifneeded': 34 raise ValueError('The option '+opt+' should probably be '+opt.replace('ifneeded', '1')); 35 for exc in ['mkl_sparse', 'mkl_sparse_optimize', 'mkl_cpardiso', 'mkl_pardiso', 'superlu_dist']: 36 if name.find(exc.replace('_','-')) > -1: 37 raise ValueError('The option '+opt+' should be '+opt.replace(exc.replace('_','-'),exc)); 38 return 39 40def check_for_unsupported_combinations(opts): 41 if '--with-precision=single' in opts and '--with-clanguage=cxx' in opts and '--with-scalar-type=complex' in opts: 42 sys.exit(ValueError('PETSc does not support single precision complex with C++ clanguage, run with --with-clanguage=c')) 43 44def check_for_option_changed(opts): 45# Document changes in command line options here. 46 optMap = [('with-64bit-indices','with-64-bit-indices'), 47 ('with-mpi-exec','with-mpiexec'), 48 ('c-blas-lapack','f2cblaslapack'), 49 ('cholmod','suitesparse'), 50 ('umfpack','suitesparse'), 51 ('f-blas-lapack','fblaslapack'), 52 ('with-cuda-arch', 53 'CUDAFLAGS=-arch'), 54 ('with-packages-dir','with-packages-download-dir'), 55 ('with-external-packages-dir','with-packages-build-dir'), 56 ('package-dirs','with-packages-search-path'), 57 ('download-petsc4py-python','with-python-exec'), 58 ('search-dirs','with-executables-search-path')] 59 for opt in opts[1:]: 60 optname = opt.split('=')[0].strip('-') 61 for oldname,newname in optMap: 62 if optname.find(oldname) >=0: 63 raise ValueError('The option '+opt+' should probably be '+opt.replace(oldname,newname)) 64 return 65 66def check_petsc_arch(opts): 67 # If PETSC_ARCH not specified - use script name (if not configure.py) 68 global petsc_arch 69 found = 0 70 for name in opts: 71 if name.find('PETSC_ARCH=') >= 0: 72 petsc_arch=name.split('=')[1] 73 found = 1 74 break 75 # If not yet specified - use the filename of script 76 if not found: 77 filename = os.path.basename(sys.argv[0]) 78 if not filename.startswith('configure') and not filename.startswith('reconfigure') and not filename.startswith('setup'): 79 petsc_arch=os.path.splitext(os.path.basename(sys.argv[0]))[0] 80 useName = 'PETSC_ARCH='+petsc_arch 81 opts.append(useName) 82 return 0 83 84def chkenable(): 85 #Replace all 'enable-'/'disable-' with 'with-'=0/1/tail 86 #enable-fortran is a special case, the resulting --with-fortran is ambiguous. 87 #Would it mean --with-fc= 88 en_dash = u'\N{EN DASH}' 89 if sys.version_info < (3, 0): 90 en_dash = en_dash.encode('utf-8') 91 for l in range(0,len(sys.argv)): 92 name = sys.argv[l] 93 94 if name.find(en_dash) >= 0: 95 sys.argv[l] = name.replace(en_dash,'-') 96 if name.find('enable-cxx') >= 0: 97 if name.find('=') == -1: 98 sys.argv[l] = name.replace('enable-cxx','with-clanguage=C++') 99 else: 100 head, tail = name.split('=', 1) 101 if tail=='0': 102 sys.argv[l] = head.replace('enable-cxx','with-clanguage=C') 103 else: 104 sys.argv[l] = head.replace('enable-cxx','with-clanguage=C++') 105 continue 106 if name.find('disable-cxx') >= 0: 107 if name.find('=') == -1: 108 sys.argv[l] = name.replace('disable-cxx','with-clanguage=C') 109 else: 110 head, tail = name.split('=', 1) 111 if tail == '0': 112 sys.argv[l] = head.replace('disable-cxx','with-clanguage=C++') 113 else: 114 sys.argv[l] = head.replace('disable-cxx','with-clanguage=C') 115 continue 116 117 118 if name.find('enable-') >= 0: 119 if name.find('=') == -1: 120 sys.argv[l] = name.replace('enable-','with-')+'=1' 121 else: 122 head, tail = name.split('=', 1) 123 sys.argv[l] = head.replace('enable-','with-')+'='+tail 124 if name.find('disable-') >= 0: 125 if name.find('=') == -1: 126 sys.argv[l] = name.replace('disable-','with-')+'=0' 127 else: 128 head, tail = name.split('=', 1) 129 if tail == '1': tail = '0' 130 sys.argv[l] = head.replace('disable-','with-')+'='+tail 131 if name.find('without-') >= 0: 132 if name.find('=') == -1: 133 sys.argv[l] = name.replace('without-','with-')+'=0' 134 else: 135 head, tail = name.split('=', 1) 136 if tail == '1': tail = '0' 137 sys.argv[l] = head.replace('without-','with-')+'='+tail 138 139def chksynonyms(): 140 #replace common configure options with ones that PETSc BuildSystem recognizes 141 simplereplacements = {'F77' : 'FC', 'F90' : 'FC'} 142 for l in range(0,len(sys.argv)): 143 name = sys.argv[l] 144 145 if name.find('with-blas-lapack') >= 0: 146 sys.argv[l] = name.replace('with-blas-lapack','with-blaslapack') 147 148 if name.find('with-debug=') >= 0 or name.endswith('with-debug'): 149 if name.find('=') == -1: 150 sys.argv[l] = name.replace('with-debug','with-debugging')+'=1' 151 else: 152 head, tail = name.split('=', 1) 153 sys.argv[l] = head.replace('with-debug','with-debugging')+'='+tail 154 155 if name.find('with-shared=') >= 0 or name.endswith('with-shared'): 156 if name.find('=') == -1: 157 sys.argv[l] = name.replace('with-shared','with-shared-libraries')+'=1' 158 else: 159 head, tail = name.split('=', 1) 160 sys.argv[l] = head.replace('with-shared','with-shared-libraries')+'='+tail 161 162 if name.find('with-index-size=') >=0: 163 head,tail = name.split('=',1) 164 if int(tail)==32: 165 sys.argv[l] = '--with-64-bit-indices=0' 166 elif int(tail)==64: 167 sys.argv[l] = '--with-64-bit-indices=1' 168 else: 169 raise RuntimeError('--with-index-size= must be 32 or 64') 170 171 if name.find('with-precision=') >=0: 172 head,tail = name.split('=',1) 173 if tail.find('quad')>=0: 174 sys.argv[l]='--with-precision=__float128' 175 176 for i,j in simplereplacements.items(): 177 if name.find(i+'=') >= 0: 178 sys.argv[l] = name.replace(i+'=',j+'=') 179 elif name.find('with-'+i.lower()+'=') >= 0: 180 sys.argv[l] = name.replace(i.lower()+'=',j.lower()+'=') 181 182def chkwincompilerusinglink(): 183 for arg in sys.argv: 184 if (arg.find('win32fe') >= 0 and (arg.find('f90') >=0 or arg.find('ifort') >=0 or arg.find('icl') >=0)): 185 return 1 186 return 0 187 188def chkdosfiles(): 189 # cygwin - but not a hg clone - so check one of files in bin dir 190 if b"\r\n" in open(os.path.join('lib','petsc','bin','petscmpiexec'),"rb").read(): 191 print('===============================================================================') 192 print(' *** Scripts are in DOS mode. Was winzip used to extract petsc sources? ****') 193 print(' *** Please restart with a fresh tarball and use "tar -xzf petsc.tar.gz" ****') 194 print('===============================================================================') 195 sys.exit(3) 196 return 197 198def chkcygwinlink(): 199 if os.path.exists('/usr/bin/cygcheck.exe') and os.path.exists('/usr/bin/link.exe') and chkwincompilerusinglink(): 200 if '--ignore-cygwin-link' in sys.argv: return 0 201 print('===============================================================================') 202 print(' *** Cygwin /usr/bin/link detected! Compiles with Intel icl/ifort can break! **') 203 print(' *** To workarround do: "mv /usr/bin/link.exe /usr/bin/link-cygwin.exe" **') 204 print(' *** Or to ignore this check, use configure option: --ignore-cygwin-link. But compiles can fail. **') 205 print('===============================================================================') 206 sys.exit(3) 207 return 0 208 209def chkbrokencygwin(): 210 if os.path.exists('/usr/bin/cygcheck.exe'): 211 buf = os.popen('/usr/bin/cygcheck.exe -c cygwin').read() 212 if buf.find('1.5.11-1') > -1: 213 print('===============================================================================') 214 print(' *** cygwin-1.5.11-1 detected. ./configure fails with this version ***') 215 print(' *** Please upgrade to cygwin-1.5.12-1 or newer version. This can ***') 216 print(' *** be done by running cygwin-setup, selecting "next" all the way.***') 217 print('===============================================================================') 218 sys.exit(3) 219 return 0 220 221def chkusingwindowspython(): 222 if sys.platform == 'win32': 223 print('===============================================================================') 224 print(' *** Windows python detected. Please rerun ./configure with cygwin-python. ***') 225 print('===============================================================================') 226 sys.exit(3) 227 return 0 228 229def chkcygwinpython(): 230 if sys.platform == 'cygwin' : 231 import platform 232 import re 233 r=re.compile("([0-9]+).([0-9]+).([0-9]+)") 234 m=r.match(platform.release()) 235 major=int(m.group(1)) 236 minor=int(m.group(2)) 237 subminor=int(m.group(3)) 238 if ((major < 1) or (major == 1 and minor < 7) or (major == 1 and minor == 7 and subminor < 34)): 239 sys.argv.append('--useThreads=0') 240 extraLogs.append('''\ 241=============================================================================== 242** Cygwin version is older than 1.7.34. Python threads do not work correctly. *** 243** Disabling thread usage for this run of ./configure ******* 244===============================================================================''') 245 return 0 246 247def chkcygwinwindowscompilers(): 248 '''Adds win32fe for Microsoft/Intel compilers''' 249 if os.path.exists('/usr/bin/cygcheck.exe'): 250 for l in range(1,len(sys.argv)): 251 option = sys.argv[l] 252 for i in ['cl','icl','ifort']: 253 if option.startswith(i): 254 sys.argv[l] = 'win32fe '+option 255 break 256 return 0 257 258def chkrhl9(): 259 if os.path.exists('/etc/redhat-release'): 260 try: 261 file = open('/etc/redhat-release','r') 262 buf = file.read() 263 file.close() 264 except: 265 # can't read file - assume dangerous RHL9 266 buf = 'Shrike' 267 if buf.find('Shrike') > -1: 268 sys.argv.append('--useThreads=0') 269 extraLogs.append('''\ 270============================================================================== 271 *** RHL9 detected. Threads do not work correctly with this distribution *** 272 ****** Disabling thread usage for this run of ./configure ********* 273===============================================================================''') 274 return 0 275 276def chktmpnoexec(): 277 if not hasattr(os,'ST_NOEXEC'): return 278 if 'TMPDIR' in os.environ: tmpDir = os.environ['TMPDIR'] 279 else: tmpDir = '/tmp' 280 if os.statvfs(tmpDir).f_flag & os.ST_NOEXEC: 281 if os.statvfs(os.path.abspath('.')).f_flag & os.ST_NOEXEC: 282 print('************************************************************************') 283 print('* TMPDIR '+tmpDir+' has noexec attribute. Same with '+os.path.abspath('.')+' where petsc is built.') 284 print('* Suggest building PETSc in a location without this restriction!') 285 print('* Alternatively, set env variable TMPDIR to a location that is not restricted to run binaries.') 286 print('************************************************************************') 287 sys.exit(4) 288 else: 289 newTmp = os.path.abspath('tmp-petsc') 290 print('************************************************************************') 291 print('* TMPDIR '+tmpDir+' has noexec attribute. Using '+newTmp+' instead.') 292 print('************************************************************************') 293 if not os.path.isdir(newTmp): os.mkdir(os.path.abspath(newTmp)) 294 os.environ['TMPDIR'] = newTmp 295 return 296 297def check_cray_modules(): 298 import script 299 '''For Cray systems check if the cc, CC, ftn compiler suite modules have been set''' 300 cray = os.getenv('CRAY_SITE_LIST_DIR') 301 if not cray: return 302 cray = os.getenv('CRAYPE_DIR') 303 if not cray: 304 print('************************************************************************') 305 print('* You are on a Cray system but no programming environments have been loaded') 306 print('* Perhaps you need:') 307 print('* module load intel ; module load PrgEnv-intel') 308 print('* or module load PrgEnv-cray') 309 print('* or module load PrgEnv-gnu') 310 print('* See https://www.mcs.anl.gov/petsc/documentation/installation.html#doemachines') 311 print('************************************************************************') 312 sys.exit(4) 313 314def check_broken_configure_log_links(): 315 '''Sometime symlinks can get broken if the original files are deleted. Delete such broken links''' 316 import os 317 for logfile in ['configure.log','configure.log.bkp']: 318 if os.path.islink(logfile) and not os.path.isfile(logfile): os.remove(logfile) 319 return 320 321def move_configure_log(framework): 322 '''Move configure.log to PETSC_ARCH/lib/petsc/conf - and update configure.log.bkp in both locations appropriately''' 323 global petsc_arch 324 325 if hasattr(framework,'arch'): petsc_arch = framework.arch 326 if hasattr(framework,'logName'): curr_file = framework.logName 327 else: curr_file = 'configure.log' 328 329 if petsc_arch: 330 import shutil 331 import os 332 333 # Just in case - confdir is not created 334 lib_dir = os.path.join(petsc_arch,'lib') 335 petsc_dir = os.path.join(petsc_arch,'lib','petsc') 336 conf_dir = os.path.join(petsc_arch,'lib','petsc','conf') 337 if not os.path.isdir(petsc_arch): os.mkdir(petsc_arch) 338 if not os.path.isdir(lib_dir): os.mkdir(lib_dir) 339 if not os.path.isdir(petsc_dir): os.mkdir(petsc_dir) 340 if not os.path.isdir(conf_dir): os.mkdir(conf_dir) 341 342 curr_bkp = curr_file + '.bkp' 343 new_file = os.path.join(conf_dir,curr_file) 344 new_bkp = new_file + '.bkp' 345 346 # Keep backup in $PETSC_ARCH/lib/petsc/conf location 347 if os.path.isfile(new_bkp): os.remove(new_bkp) 348 if os.path.isfile(new_file): os.rename(new_file,new_bkp) 349 if os.path.isfile(curr_file): 350 shutil.copyfile(curr_file,new_file) 351 os.remove(curr_file) 352 if os.path.isfile(new_file): os.symlink(new_file,curr_file) 353 # If the old bkp is using the same PETSC_ARCH/lib/petsc/conf - then update bkp link 354 if os.path.realpath(curr_bkp) == os.path.realpath(new_file): 355 if os.path.isfile(curr_bkp): os.remove(curr_bkp) 356 if os.path.isfile(new_bkp): os.symlink(new_bkp,curr_bkp) 357 return 358 359def print_final_timestamp(framework): 360 import time 361 framework.log.write(('='*80)+'\n') 362 framework.log.write('Finishing configure run at '+time.strftime('%a, %d %b %Y %H:%M:%S %z')+'\n') 363 framework.log.write(('='*80)+'\n') 364 return 365 366def petsc_configure(configure_options): 367 if 'PETSC_DIR' in os.environ: 368 petscdir = os.environ['PETSC_DIR'] 369 if petscdir.find(' ') > -1: 370 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') 371 try: 372 sys.path.append(os.path.join(petscdir,'lib','petsc','bin')) 373 import petscnagupgrade 374 file = os.path.join(petscdir,'.nagged') 375 if not petscnagupgrade.naggedtoday(file): 376 petscnagupgrade.currentversion(petscdir) 377 except: 378 pass 379 print('===============================================================================') 380 print(' Configuring PETSc to compile on your system ') 381 print('===============================================================================') 382 383 try: 384 # Command line arguments take precedence (but don't destroy argv[0]) 385 sys.argv = sys.argv[:1] + configure_options + sys.argv[1:] 386 check_for_option_mistakes(sys.argv) 387 check_for_option_changed(sys.argv) 388 except (TypeError, ValueError) as e: 389 emsg = str(e) 390 if not emsg.endswith('\n'): emsg = emsg+'\n' 391 msg ='*******************************************************************************\n'\ 392 +' ERROR in COMMAND LINE ARGUMENT to ./configure \n' \ 393 +'-------------------------------------------------------------------------------\n' \ 394 +emsg+'*******************************************************************************\n' 395 sys.exit(msg) 396 # check PETSC_ARCH 397 check_for_unsupported_combinations(sys.argv) 398 check_petsc_arch(sys.argv) 399 check_broken_configure_log_links() 400 401 #rename '--enable-' to '--with-' 402 chkenable() 403 # support a few standard configure option types 404 chksynonyms() 405 # Check for broken cygwin 406 chkbrokencygwin() 407 # Disable threads on RHL9 408 chkrhl9() 409 # Make sure cygwin-python is used on windows 410 chkusingwindowspython() 411 # Threads don't work for cygwin & python... 412 chkcygwinpython() 413 chkcygwinlink() 414 chkdosfiles() 415 chkcygwinwindowscompilers() 416 chktmpnoexec() 417 418 for l in range(1,len(sys.argv)): 419 if sys.argv[l].startswith('--with-fc=') and sys.argv[l].endswith('nagfor'): 420 # need a way to save this value and later CC so that petscnagfor may use them 421 name = sys.argv[l].split('=')[1] 422 sys.argv[l] = '--with-fc='+os.path.join(os.path.abspath('.'),'lib','petsc','bin','petscnagfor') 423 break 424 425 426 # Should be run from the toplevel 427 configDir = os.path.abspath('config') 428 bsDir = os.path.join(configDir, 'BuildSystem') 429 if not os.path.isdir(configDir): 430 raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.')) 431 sys.path.insert(0, bsDir) 432 sys.path.insert(0, configDir) 433 import config.base 434 import config.framework 435 import pickle 436 import traceback 437 438 # Check Cray without modules 439 check_cray_modules() 440 441 tbo = None 442 framework = None 443 try: 444 framework = config.framework.Framework(['--configModules=PETSc.Configure','--optionsModule=config.compilerOptions']+sys.argv[1:], loadArgDB = 0) 445 framework.setup() 446 framework.logPrint('\n'.join(extraLogs)) 447 framework.configure(out = sys.stdout) 448 framework.storeSubstitutions(framework.argDB) 449 framework.argDB['configureCache'] = pickle.dumps(framework) 450 framework.printSummary() 451 framework.argDB.save(force = True) 452 framework.logClear() 453 print_final_timestamp(framework) 454 framework.closeLog() 455 try: 456 move_configure_log(framework) 457 except: 458 # perhaps print an error about unable to shuffle logs? 459 pass 460 return 0 461 except (RuntimeError, config.base.ConfigureSetupError) as e: 462 tbo = sys.exc_info()[2] 463 emsg = str(e) 464 if not emsg.endswith('\n'): emsg = emsg+'\n' 465 msg ='*******************************************************************************\n'\ 466 +' UNABLE to CONFIGURE with GIVEN OPTIONS (see configure.log for details):\n' \ 467 +'-------------------------------------------------------------------------------\n' \ 468 +emsg+'*******************************************************************************\n' 469 se = '' 470 except (TypeError, ValueError) as e: 471 # this exception is automatically deleted by Python so we need to save it to print below 472 tbo = sys.exc_info()[2] 473 emsg = str(e) 474 if not emsg.endswith('\n'): emsg = emsg+'\n' 475 msg ='*******************************************************************************\n'\ 476 +' TypeError or ValueError possibly related to ERROR in COMMAND LINE ARGUMENT while running ./configure \n' \ 477 +'-------------------------------------------------------------------------------\n' \ 478 +emsg+'*******************************************************************************\n' 479 se = '' 480 except ImportError as e : 481 # this exception is automatically deleted by Python so we need to save it to print below 482 tbo = sys.exc_info()[2] 483 emsg = str(e) 484 if not emsg.endswith('\n'): emsg = emsg+'\n' 485 msg ='*******************************************************************************\n'\ 486 +' ImportError while runing ./configure \n' \ 487 +'-------------------------------------------------------------------------------\n' \ 488 +emsg+'*******************************************************************************\n' 489 se = '' 490 except OSError as e : 491 tbo = sys.exc_info()[2] 492 emsg = str(e) 493 if not emsg.endswith('\n'): emsg = emsg+'\n' 494 msg ='*******************************************************************************\n'\ 495 +' OSError while running ./configure \n' \ 496 +'-------------------------------------------------------------------------------\n' \ 497 +emsg+'*******************************************************************************\n' 498 se = '' 499 except SystemExit as e: 500 tbo = sys.exc_info()[2] 501 if e.code is None or e.code == 0: 502 return 503 if e.code == 10: 504 sys.exit(10) 505 msg ='*******************************************************************************\n'\ 506 +' CONFIGURATION FAILURE (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \ 507 +'*******************************************************************************\n' 508 se = str(e) 509 except Exception as e: 510 tbo = sys.exc_info()[2] 511 msg ='*******************************************************************************\n'\ 512 +' CONFIGURATION CRASH (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \ 513 +'*******************************************************************************\n' 514 se = str(e) 515 516 print(msg) 517 if not framework is None: 518 framework.logClear() 519 if hasattr(framework, 'log'): 520 try: 521 if hasattr(framework,'compilerDefines'): 522 framework.log.write('**** Configure header '+framework.compilerDefines+' ****\n') 523 framework.outputHeader(framework.log) 524 if hasattr(framework,'compilerFixes'): 525 framework.log.write('**** C specific Configure header '+framework.compilerFixes+' ****\n') 526 framework.outputCHeader(framework.log) 527 except Exception as e: 528 framework.log.write('Problem writing headers to log: '+str(e)) 529 try: 530 framework.log.write(msg+se) 531 traceback.print_tb(tbo, file = framework.log) 532 print_final_timestamp(framework) 533 if hasattr(framework,'log'): framework.log.close() 534 move_configure_log(framework) 535 except Exception as e: 536 print('Error printing error message from exception or printing the traceback:'+str(e)) 537 traceback.print_tb(sys.exc_info()[2]) 538 sys.exit(1) 539 else: 540 print(se) 541 traceback.print_tb(tbo) 542 else: 543 print(se) 544 traceback.print_tb(tbo) 545 if hasattr(framework,'log'): framework.log.close() 546 547if __name__ == '__main__': 548 petsc_configure([]) 549 550