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