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.logClear() 239 framework.closeLog() 240 try: 241 move_configure_log(framework) 242 except: 243 # perhaps print an error about unable to shuffle logs? 244 pass 245 return 0 246 except (RuntimeError, config.base.ConfigureSetupError), e: 247 emsg = str(e) 248 if not emsg.endswith('\n'): emsg = emsg+'\n' 249 msg ='*********************************************************************************\n'\ 250 +' UNABLE to CONFIGURE with GIVEN OPTIONS (see configure.log for details):\n' \ 251 +'---------------------------------------------------------------------------------------\n' \ 252 +emsg+'*********************************************************************************\n' 253 se = '' 254 except (TypeError, ValueError), e: 255 emsg = str(e) 256 if not emsg.endswith('\n'): emsg = emsg+'\n' 257 msg ='*********************************************************************************\n'\ 258 +' ERROR in COMMAND LINE ARGUMENT to config/configure.py \n' \ 259 +'---------------------------------------------------------------------------------------\n' \ 260 +emsg+'*********************************************************************************\n' 261 se = '' 262 except ImportError, e : 263 emsg = str(e) 264 if not emsg.endswith('\n'): emsg = emsg+'\n' 265 msg ='*********************************************************************************\n'\ 266 +' UNABLE to FIND MODULE for config/configure.py \n' \ 267 +'---------------------------------------------------------------------------------------\n' \ 268 +emsg+'*********************************************************************************\n' 269 se = '' 270 except OSError, e : 271 emsg = str(e) 272 if not emsg.endswith('\n'): emsg = emsg+'\n' 273 msg ='*********************************************************************************\n'\ 274 +' UNABLE to EXECUTE BINARIES for config/configure.py \n' \ 275 +'---------------------------------------------------------------------------------------\n' \ 276 +emsg+'*********************************************************************************\n' 277 se = '' 278 except SystemExit, e: 279 if e.code is None or e.code == 0: 280 return 281 msg ='*********************************************************************************\n'\ 282 +' CONFIGURATION FAILURE (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \ 283 +'*********************************************************************************\n' 284 se = str(e) 285 except Exception, e: 286 msg ='*********************************************************************************\n'\ 287 +' CONFIGURATION CRASH (Please send configure.log to petsc-maint@mcs.anl.gov)\n' \ 288 +'*********************************************************************************\n' 289 se = str(e) 290 291 print msg 292 if not framework is None: 293 framework.logClear() 294 if hasattr(framework, 'log'): 295 import traceback 296 try: 297 framework.log.write(msg+se) 298 traceback.print_tb(sys.exc_info()[2], file = framework.log) 299 close(framework.log) 300 move_configure_log(framework) 301 except: 302 pass 303 sys.exit(1) 304 else: 305 print se 306 import traceback 307 traceback.print_tb(sys.exc_info()[2]) 308 close(framework.log) 309 move_configure_log(framework) 310 311if __name__ == '__main__': 312 petsc_configure([]) 313 314