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