Changeset 196
- Timestamp:
- Aug 20, 2004, 1:28:27 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/domain.py
r195 r196 473 473 474 474 #C-extensions 475 #import util476 #if util.can_use_C_extension('domain_ext.c'):475 #import compile 476 #if compile.can_use_C_extension('domain_ext.c'): 477 477 # compute_fluxes = compute_fluxes_c 478 478 #distribute_to_vertices_and_edges = distribute_to_vertices_and_edges_c -
inundation/ga/storm_surge/pyvolution/util.py
r195 r196 60 60 61 61 return a0-a1 62 63 64 65 def compile(FNs=None, CC=None, LD = None, SFLAG = None, verbose = 1):66 """compile(FNs=None, CC=None, LD = None, SFLAG = None):67 68 Compile FN(s) using compiler CC (e.g. mpicc),69 Loader LD and shared flag SFLAG.70 If CC is absent use default compiler dependent on platform71 if LD is absent CC is used.72 if SFLAG is absent platform default is used73 FNs can be either one filename or a list of filenames74 In the latter case, the first will be used to name so file.75 """76 import os, string, sys, types77 78 # Input check79 #80 assert not FNs is None, "No filename provided"81 82 if not type(FNs) == types.ListType:83 FNs = [FNs]84 85 86 libext = 'so' #Default extension (Unix)87 libs = ''88 version = sys.version[:3]89 90 # Determine platform and compiler91 #92 if sys.platform == 'sunos5': #Solaris93 if CC:94 compiler = CC95 else:96 compiler = 'gcc'97 if LD:98 loader = LD99 else:100 loader = compiler101 if SFLAG:102 sharedflag = SFLAG103 else:104 sharedflag = 'G'105 106 elif sys.platform == 'osf1V5': #Compaq AlphaServer107 if CC:108 compiler = CC109 else:110 compiler = 'cc'111 if LD:112 loader = LD113 else:114 loader = compiler115 if SFLAG:116 sharedflag = SFLAG117 else:118 sharedflag = 'shared'119 120 elif sys.platform == 'linux2': #Linux121 if CC:122 compiler = CC123 else:124 compiler = 'gcc'125 if LD:126 loader = LD127 else:128 loader = compiler129 if SFLAG:130 sharedflag = SFLAG131 else:132 sharedflag = 'shared'133 134 elif sys.platform == 'darwin': #Mac OS X:135 if CC:136 compiler = CC137 else:138 compiler = 'cc'139 if LD:140 loader = LD141 else:142 loader = compiler143 if SFLAG:144 sharedflag = SFLAG145 else:146 sharedflag = 'bundle -flat_namespace -undefined suppress'147 148 elif sys.platform == 'cygwin': #Cygwin (compilation same as linux)149 if CC:150 compiler = CC151 else:152 compiler = 'gcc'153 if LD:154 loader = LD155 else:156 loader = compiler157 if SFLAG:158 sharedflag = SFLAG159 else:160 sharedflag = 'shared'161 libext = 'dll'162 libs = '/lib/python%s/config/libpython%s.dll.a' %(version,version)163 164 elif sys.platform == 'win32': #Windows165 if CC:166 compiler = CC167 else:168 compiler = 'gcc'169 if LD:170 loader = LD171 else:172 loader = compiler173 if SFLAG:174 sharedflag = SFLAG175 else:176 sharedflag = 'shared'177 libext = 'dll'178 179 v = version.replace('.','')180 dllfilename = 'python%s.dll' %(v)181 libs = os.path.join(sys.exec_prefix,dllfilename)182 183 184 else:185 if verbose: print "Unrecognised platform %s - revert to default"\186 %sys.platform187 188 if CC:189 compiler = CC190 else:191 compiler = 'cc'192 if LD:193 loader = LD194 else:195 loader = 'ld'196 if SFLAG:197 sharedflag = SFLAG198 else:199 sharedflag = 'G'200 201 202 # Find location of include files203 #204 if sys.platform == 'win32': #Windows205 include = os.path.join(sys.exec_prefix, 'include')206 else:207 include = os.path.join(os.path.join(sys.exec_prefix, 'include'),208 'python'+version)209 210 # Check existence of Python.h211 #212 headerfile = include + os.sep +'Python.h'213 try:214 open(headerfile, 'r')215 except:216 raise """Did not find Python header file %s.217 Make sure files for Python C-extensions are installed.218 In debian linux, for example, you need to install a219 package called something like python2.3-dev""" %headerfile220 221 222 # Check filename(s)223 #224 object_files = ''225 for FN in FNs:226 root, ext = os.path.splitext(FN)227 if ext == '':228 FN = FN + '.c'229 elif ext.lower() != '.c':230 raise Exception, "Unrecognised extension: " + FN231 232 try:233 open(FN,'r')234 except:235 raise Exception, "Could not open: " + FN236 237 if not object_files: root1 = root #Remember first filename238 object_files += root + '.o '239 240 241 # Compile242 #243 244 s = "%s -c %s -I%s -o %s.o -Wall -O" %(compiler, FN, include, root)245 if verbose:246 print s247 else:248 s = s + ' 2> /dev/null' #Suppress errors249 250 try:251 err = os.system(s)252 if err != 0:253 raise 'Attempting to compile %s failed - please try manually' %FN254 except:255 raise 'Could not compile %s - please try manually' %FN256 257 258 # Make shared library (*.so or *.dll)259 260 s = "%s -%s %s -o %s.%s %s -lm" %(loader, sharedflag, object_files, root1, libext, libs)261 if verbose:262 print s263 else:264 s = s + ' 2> /dev/null' #Suppress warnings265 266 try:267 err=os.system(s)268 if err != 0:269 raise 'Atempting to link %s failed - please try manually' %root1270 except:271 raise 'Could not link %s - please try manually' %root1272 273 274 275 276 def can_use_C_extension(filename):277 """Determine whether specified C-extension278 can and should be used.279 """280 281 from config import use_extensions282 283 from os.path import splitext284 285 root, ext = splitext(filename)286 287 C=False288 if use_extensions:289 try:290 s = 'import %s' %root291 #print s292 exec(s)293 except:294 try:295 open(filename)296 except:297 msg = 'C extension %s cannot be opened' %filename298 print msg299 else:300 print '------- Trying to compile c-extension %s' %filename301 302 compile(filename)303 try:304 compile(filename)305 except:306 print 'WARNING: Could not compile C-extension %s'\307 %filename308 else:309 try:310 exec('import %s' %root)311 except:312 msg = 'C extension %s seems to compile OK, '313 msg += 'but it can still not be imported.'314 raise msg315 else:316 C=True317 else:318 C=True319 320 if not C:321 pass322 print 'NOTICE: C-extension %s not used' %filename323 324 return C325 326 327 62 328 63 … … 394 129 #Initialise module 395 130 396 import util397 if util.can_use_C_extension('util_ext.c'):131 import compile 132 if compile.can_use_C_extension('util_ext.c'): 398 133 from util_ext import gradient, rotate 399 134 else:
Note: See TracChangeset
for help on using the changeset viewer.