1#!/usr/bin/env python 2import os 3import sys 4import commands 5# to load ~/.pythonrc.py before inserting correct BuildSystem to path 6import user 7extraLogs = [] 8petsc_arch = '' 9 10# Use en_US as language so that BuildSystem parses compiler messages in english 11if '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' 12if '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' 13 14if not hasattr(sys, 'version_info') or not sys.version_info[1] >= 2 or not sys.version_info[0] >= 2: 15 print '**** You must have Python version 2.2 or higher to run config/configure.py ******' 16 print '* Python is easy to install for end users or sys-admin. *' 17 print '* http://www.python.org/download/ *' 18 print '* *' 19 print '* You CANNOT configure PETSc without Python *' 20 print '* http://www.mcs.anl.gov/petsc/petsc-as/documentation/installation.html *' 21 print '*********************************************************************************' 22 sys.exit(4) 23 24def check_for_option_mistakes(opts): 25 for opt in opts[1:]: 26 name = opt.split('=')[0] 27 if name.find('_') >= 0: 28 exception = False 29 for exc in ['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']: 30 if name.find(exc) >= 0: 31 exception = True 32 if not exception: 33 raise ValueError('The option '+name+' should probably be '+name.replace('_', '-')); 34 return 35 36def check_petsc_arch(opts): 37 # If PETSC_ARCH not specified - use script name (if not configure.py) 38 global petsc_arch 39 found = 0 40 for name in opts: 41 if name.find('PETSC_ARCH=') >= 0: 42 petsc_arch=name.split('=')[1] 43 found = 1 44 break 45 # If not yet specified - use the filename of script 46 if not found: 47 filename = os.path.basename(sys.argv[0]) 48 if not filename.startswith('configure') and not filename.startswith('reconfigure'): 49 petsc_arch=os.path.splitext(os.path.basename(sys.argv[0]))[0] 50 useName = 'PETSC_ARCH='+petsc_arch 51 opts.append(useName) 52 return 0 53 54def chkbrokencygwin(): 55 if os.path.exists('/usr/bin/cygcheck.exe'): 56 buf = os.popen('/usr/bin/cygcheck.exe -c cygwin').read() 57 if buf.find('1.5.11-1') > -1: 58 print '=================================================================================' 59 print ' *** cygwin-1.5.11-1 detected. config/configure.py fails with this version ***' 60 print ' *** Please upgrade to cygwin-1.5.12-1 or newer version. This can ***' 61 print ' *** be done by running cygwin-setup, selecting "next" all the way.***' 62 print '=================================================================================' 63 sys.exit(3) 64 return 0 65 66def chkusingwindowspython(): 67 if os.path.exists('/usr/bin/cygcheck.exe') and sys.platform != 'cygwin': 68 print '=================================================================================' 69 print ' *** Non-cygwin python detected. Please rerun config/configure.py with cygwin-python ***' 70 print '=================================================================================' 71 sys.exit(3) 72 return 0 73 74def chkcygwinpythonver(): 75 if os.path.exists('/usr/bin/cygcheck.exe'): 76 buf = os.popen('/usr/bin/cygcheck.exe -c python').read() 77 if (buf.find('2.4') > -1) or (buf.find('2.5') > -1) or (buf.find('2.6') > -1): 78 sys.argv.append('--useThreads=0') 79 extraLogs.append('''\ 80================================================================================ 81** Cygwin-python-2.4/2.5 detected. Threads do not work correctly with this version * 82 ********* Disabling thread usage for this run of config/configure.py ********** 83================================================================================''') 84 return 0 85 86def chkrhl9(): 87 if os.path.exists('/etc/redhat-release'): 88 try: 89 file = open('/etc/redhat-release','r') 90 buf = file.read() 91 file.close() 92 except: 93 # can't read file - assume dangerous RHL9 94 buf = 'Shrike' 95 if buf.find('Shrike') > -1: 96 sys.argv.append('--useThreads=0') 97 extraLogs.append('''\ 98================================================================================ 99 *** RHL9 detected. Threads do not work correctly with this distribution *** 100 ****** Disabling thread usage for this run of config/configure.py ******* 101================================================================================''') 102 return 0 103 104def check_broken_configure_log_links(): 105 '''Sometime symlinks can get broken if the original files are deleted. Delete such broken links''' 106 import os 107 for logfile in ['configure.log','configure.log.bkp']: 108 if os.path.islink(logfile) and not os.path.isfile(logfile): os.remove(logfile) 109 return 110 111def move_configure_log(framework): 112 '''Move configure.log to PETSC_ARCH/conf - and update configure.log.bkp in both locations appropriately''' 113 global petsc_arch 114 115 if hasattr(framework,'arch'): petsc_arch = framework.arch 116 if hasattr(framework,'logName'): curr_file = framework.logName 117 else: curr_file = 'configure.log' 118 119 if petsc_arch: 120 import shutil 121 import os 122 123 # Just in case - confdir is not created 124 conf_dir = os.path.join(petsc_arch,'conf') 125 if not os.path.isdir(petsc_arch): os.mkdir(petsc_arch) 126 if not os.path.isdir(conf_dir): os.mkdir(conf_dir) 127 128 curr_bkp = curr_file + '.bkp' 129 new_file = os.path.join(conf_dir,curr_file) 130 new_bkp = new_file + '.bkp' 131 132 # Keep backup in $PETSC_ARCH/conf location 133 if os.path.isfile(new_bkp): os.remove(new_bkp) 134 if os.path.isfile(new_file): os.rename(new_file,new_bkp) 135 if os.path.isfile(curr_file): 136 shutil.copyfile(curr_file,new_file) 137 os.remove(curr_file) 138 if os.path.isfile(new_file): os.symlink(new_file,curr_file) 139 # If the old bkp is using the same PETSC_ARCH/conf - then update bkp link 140 if os.path.realpath(curr_bkp) == os.path.realpath(new_file): 141 if os.path.isfile(curr_bkp): os.remove(curr_bkp) 142 if os.path.isfile(new_bkp): os.symlink(new_bkp,curr_bkp) 143 return 144 145def petsc_configure(configure_options): 146 print '=================================================================================' 147 print ' Configuring PETSc to compile on your system ' 148 print '=================================================================================' 149 150 # Command line arguments take precedence (but don't destroy argv[0]) 151 sys.argv = sys.argv[:1] + configure_options + sys.argv[1:] 152 check_for_option_mistakes(sys.argv) 153 # check PETSC_ARCH 154 check_petsc_arch(sys.argv) 155 check_broken_configure_log_links() 156 157 # support a few standard configure option types 158 for l in range(0,len(sys.argv)): 159 name = sys.argv[l] 160 if name.find('enable-') >= 0: 161 if name.find('=') == -1: 162 sys.argv[l] = name.replace('enable-','with-')+'=1' 163 else: 164 head, tail = name.split('=', 1) 165 sys.argv[l] = head.replace('enable-','with-')+'='+tail 166 if name.find('disable-') >= 0: 167 if name.find('=') == -1: 168 sys.argv[l] = name.replace('disable-','with-')+'=0' 169 else: 170 head, tail = name.split('=', 1) 171 if tail == '1': tail = '0' 172 sys.argv[l] = head.replace('disable-','with-')+'='+tail 173 if name.find('without-') >= 0: 174 if name.find('=') == -1: 175 sys.argv[l] = name.replace('without-','with-')+'=0' 176 else: 177 head, tail = name.split('=', 1) 178 if tail == '1': tail = '0' 179 sys.argv[l] = head.replace('without-','with-')+'='+tail 180 181 # Check for broken cygwin 182 chkbrokencygwin() 183 # Disable threads on RHL9 184 chkrhl9() 185 # Make sure cygwin-python is used on windows 186 chkusingwindowspython() 187 # Threads don't work for cygwin & python-2.4, 2.5 etc.. 188 chkcygwinpythonver() 189 190 # Should be run from the toplevel 191 configDir = os.path.abspath('config') 192 bsDir = os.path.join(configDir, 'BuildSystem') 193 if not os.path.isdir(configDir): 194 raise RuntimeError('Run configure from $PETSC_DIR, not '+os.path.abspath('.')) 195 if not os.path.isdir(bsDir): 196 print '=================================================================================' 197 print '''++ Could not locate BuildSystem in %s.''' % configDir 198 print '''++ Downloading it using "hg clone http://hg.mcs.anl.gov/petsc/BuildSystem %s"''' % bsDir 199 print '=================================================================================' 200 (status,output) = commands.getstatusoutput('hg clone http://petsc.cs.iit.edu/petsc/BuildSystem '+ bsDir) 201 if status: 202 if output.find('ommand not found') >= 0: 203 print '=================================================================================' 204 print '''** Unable to locate hg (Mercurial) to download BuildSystem; make sure hg is in your path''' 205 print '''** or manually copy BuildSystem to $PETSC_DIR/config/BuildSystem from a machine where''' 206 print '''** you do have hg installed and can clone BuildSystem. ''' 207 print '=================================================================================' 208 elif output.find('Cannot resolve host') >= 0: 209 print '=================================================================================' 210 print '''** Unable to download BuildSystem. You must be off the network.''' 211 print '''** Connect to the internet and run config/configure.py again.''' 212 print '=================================================================================' 213 else: 214 print '=================================================================================' 215 print '''** Unable to download BuildSystem. Please send this message to petsc-maint@mcs.anl.gov''' 216 print '=================================================================================' 217 print output 218 sys.exit(3) 219 220 sys.path.insert(0, bsDir) 221 sys.path.insert(0, configDir) 222 import config.base 223 import config.framework 224 import cPickle 225 226 framework = None 227 try: 228 framework = config.framework.Framework(['--configModules=PETSc.Configure','--optionsModule=PETSc.compilerOptions']+sys.argv[1:], loadArgDB = 0) 229 framework.setup() 230 framework.logPrint('\n'.join(extraLogs)) 231 framework.configure(out = sys.stdout) 232 framework.storeSubstitutions(framework.argDB) 233 framework.argDB['configureCache'] = cPickle.dumps(framework) 234 import PETSc.packages 235 for i in framework.packages: 236 if hasattr(i,'postProcess'): 237 i.postProcess() 238 framework.printSummary() 239 framework.logClear() 240 framework.closeLog() 241 try: 242 move_configure_log(framework) 243 except: 244 # perhaps print an error about unable to shuffle logs? 245 pass 246 return 0 247 except (RuntimeError, config.base.ConfigureSetupError), e: 248 emsg = str(e) 249 if not emsg.endswith('\n'): emsg = emsg+'\n' 250 msg ='*********************************************************************************\n'\ 251 +' UNABLE to CONFIGURE with GIVEN OPTIONS (see configure.log for details):\n' \ 252 +'---------------------------------------------------------------------------------------\n' \ 253 +emsg+'*********************************************************************************\n' 254 se = '' 255 except (TypeError, ValueError), e: 256 emsg = str(e) 257 if not emsg.endswith('\n'): emsg = emsg+'\n' 258 msg ='*********************************************************************************\n'\ 259 +' ERROR in COMMAND LINE ARGUMENT to config/configure.py \n' \ 260 +'---------------------------------------------------------------------------------------\n' \ 261 +emsg+'*********************************************************************************\n' 262 se = '' 263 except ImportError, e : 264 emsg = str(e) 265 if not emsg.endswith('\n'): emsg = emsg+'\n' 266 msg ='*********************************************************************************\n'\ 267 +' UNABLE to FIND MODULE for config/configure.py \n' \ 268 +'---------------------------------------------------------------------------------------\n' \ 269 +emsg+'*********************************************************************************\n' 270 se = '' 271 except OSError, e : 272 emsg = str(e) 273 if not emsg.endswith('\n'): emsg = emsg+'\n' 274 msg ='*********************************************************************************\n'\ 275 +' UNABLE to EXECUTE BINARIES for config/configure.py \n' \ 276 +'---------------------------------------------------------------------------------------\n' \ 277 +emsg+'*********************************************************************************\n' 278 se = '' 279 except SystemExit, e: 280 if e.code is None or e.code == 0: 281 return 282 msg ='*********************************************************************************\n'\ 283 +' CONFIGURATION FAILURE (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \ 284 +'*********************************************************************************\n' 285 se = str(e) 286 except Exception, e: 287 msg ='*********************************************************************************\n'\ 288 +' CONFIGURATION CRASH (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \ 289 +'*********************************************************************************\n' 290 se = str(e) 291 292 print msg 293 if not framework is None: 294 framework.logClear() 295 if hasattr(framework, 'log'): 296 import traceback 297 try: 298 framework.log.write(msg+se) 299 traceback.print_tb(sys.exc_info()[2], file = framework.log) 300 close(framework.log) 301 move_configure_log(framework) 302 except: 303 pass 304 sys.exit(1) 305 else: 306 print se 307 import traceback 308 traceback.print_tb(sys.exc_info()[2]) 309 close(framework.log) 310 move_configure_log(framework) 311 312if __name__ == '__main__': 313 petsc_configure([]) 314 315