Changeset 4761
- Timestamp:
- Oct 29, 2007, 11:24:08 AM (17 years ago)
- Location:
- anuga_core/source/anuga
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/mesh_engine/me_compile.py
r4144 r4761 4 4 python compile.py <filename> 5 5 6 Usage from within Python: 7 import compile 8 compile.compile(<filename>,..) 6 """ 9 7 10 SPECIAL VERSION FOR TRIANGLE ! 11 12 Ole Nielsen, Oct 2001 13 """ 14 15 16 def compile(FNs=None, CC=None, LD = None, SFLAG = None, verbose = 1): 17 """compile(FNs=None, CC=None, LD = None, SFLAG = None): 18 19 Compile FN(s) using compiler CC (e.g. mpicc), 20 Loader LD and shared flag SFLAG. 21 If CC is absent use default compiler dependent on platform 22 if LD is absent CC is used. 23 if SFLAG is absent platform default is used 24 FNs can be either one filename or a list of filenames 25 In the latter case, the first will be used to name so file. 26 """ 27 28 import os, string, sys, types 29 30 # Input check 31 # 32 assert not FNs is None, "No filename provided" 33 34 if not type(FNs) == types.ListType: 35 FNs = [FNs] 36 37 38 libext = 'so' #Default extension (Unix) 39 libs = '' 40 version = sys.version[:3] 41 42 # Determine platform and compiler 43 # 44 if sys.platform == 'sunos5': #Solaris 45 if CC: 46 compiler = CC 47 else: 48 compiler = 'gcc' 49 if LD: 50 loader = LD 51 else: 52 loader = compiler 53 if SFLAG: 54 sharedflag = SFLAG 55 else: 56 sharedflag = 'G' 57 58 elif sys.platform == 'osf1V5': #Compaq AlphaServer 59 if CC: 60 compiler = CC 61 else: 62 compiler = 'cc' 63 if LD: 64 loader = LD 65 else: 66 loader = compiler 67 if SFLAG: 68 sharedflag = SFLAG 69 else: 70 sharedflag = 'shared' 71 72 elif sys.platform == 'linux2': #Linux 73 if CC: 74 compiler = CC 75 else: 76 compiler = 'gcc' 77 if LD: 78 loader = LD 79 else: 80 loader = compiler 81 if SFLAG: 82 sharedflag = SFLAG 83 else: 84 sharedflag = 'shared' 85 86 elif sys.platform == 'darwin': #Mac OS X: 87 if CC: 88 compiler = CC 89 else: 90 compiler = 'cc' 91 if LD: 92 loader = LD 93 else: 94 loader = compiler 95 if SFLAG: 96 sharedflag = SFLAG 97 else: 98 sharedflag = 'bundle -flat_namespace -undefined suppress' 99 100 elif sys.platform == 'cygwin': #Cygwin (compilation same as linux) 101 if CC: 102 compiler = CC 103 else: 104 compiler = 'gcc' 105 if LD: 106 loader = LD 107 else: 108 loader = compiler 109 if SFLAG: 110 sharedflag = SFLAG 111 else: 112 sharedflag = 'shared' 113 libext = 'dll' 114 libs = '/lib/python%s/config/libpython%s.dll.a' %(version,version) 115 116 elif sys.platform == 'win32': #Windows 117 if CC: 118 compiler = CC 119 else: 120 compiler = 'gcc' 121 if LD: 122 loader = LD 123 else: 124 loader = compiler 125 if SFLAG: 126 sharedflag = SFLAG 127 else: 128 sharedflag = 'shared' 129 libext = 'dll' 130 131 v = version.replace('.','') 132 dllfilename = 'python%s.dll' %(v) 133 libs = os.path.join(sys.exec_prefix,dllfilename) 134 135 136 else: 137 if verbose: print "Unrecognised platform %s - revert to default"\ 138 %sys.platform 139 140 if CC: 141 compiler = CC 142 else: 143 compiler = 'cc' 144 if LD: 145 loader = LD 146 else: 147 loader = 'ld' 148 if SFLAG: 149 sharedflag = SFLAG 150 else: 151 sharedflag = 'G' 152 153 154 # Find location of include files 155 # 156 if sys.platform == 'win32': #Windows 157 include = os.path.join(sys.exec_prefix, 'include') 158 else: 159 include = os.path.join(os.path.join(sys.exec_prefix, 'include'), 160 'python'+version) 161 162 # Check existence of Python.h 163 # 164 headerfile = include + os.sep +'Python.h' 165 try: 166 open(headerfile, 'r') 167 except: 168 raise """Did not find Python header file %s. 169 Make sure files for Python C-extensions are installed. 170 In debian linux, for example, you need to install a 171 package called something like python2.3-dev""" %headerfile 172 173 174 # Check filename(s) 175 # 176 object_files = '' 177 for FN in FNs: 178 root, ext = os.path.splitext(FN) 179 if ext == '': 180 FN = FN + '.c' 181 elif ext.lower() != '.c': 182 raise Exception, "Unrecognised extension: " + FN 183 184 try: 185 open(FN,'r') 186 except: 187 raise Exception, "Could not open: " + FN 188 189 if not object_files: root1 = root #Remember first filename 190 object_files += root + '.o ' 191 192 193 # Compile 194 # 195 s = "%s -c %s -I%s -o %s.o -O3 -DTRILIBRARY=1 -DNO_TIMER=1" %(compiler, FN, include, root) 196 197 if os.name == 'posix' and os.uname()[4] == 'x86_64': 198 #Extra flags for 64 bit architectures 199 200 #FIXME: Which one? 201 #s += ' -fPIC' 202 s += ' -fPIC -m64' 203 204 205 if verbose: 206 print s 207 else: 208 s = s + ' 2> /dev/null' #Suppress errors 209 210 try: 211 err = os.system(s) 212 if err != 0: 213 raise 'Attempting to compile %s failed - please try manually' %FN 214 except: 215 raise 'Could not compile %s - please try manually' %FN 216 217 218 # Make shared library (*.so or *.dll) 219 220 s = "%s -%s %s -o %s.%s %s -lm" %(loader, sharedflag, object_files, root1, libext, libs) 221 if verbose: 222 print s 223 else: 224 s = s + ' 2> /dev/null' #Suppress warnings 225 226 try: 227 err=os.system(s) 228 if err != 0: 229 raise 'Atempting to link %s failed - please try manually' %root1 230 except: 231 raise 'Could not link %s - please try manually' %root1 232 233 234 def can_use_C_extension(filename): 235 """Determine whether specified C-extension 236 can and should be used. 237 """ 238 239 from anuga.config import use_extensions 240 241 from os.path import splitext 242 243 root, ext = splitext(filename) 244 245 C=False 246 if use_extensions: 247 try: 248 s = 'import %s' %root 249 #print s 250 exec(s) 251 except: 252 try: 253 open(filename) 254 except: 255 msg = 'C extension %s cannot be opened' %filename 256 print msg 257 else: 258 print '------- Trying to compile c-extension %s' %filename 259 260 try: 261 compile(filename) 262 except: 263 print 'WARNING: Could not compile C-extension %s'\ 264 %filename 265 else: 266 try: 267 exec('import %s' %root) 268 except: 269 msg = 'C extension %s seems to compile OK, ' 270 msg += 'but it can still not be imported.' 271 raise msg 272 else: 273 C=True 274 else: 275 C=True 276 277 if not C: 278 pass 279 print 'NOTICE: C-extension %s not used' %filename 280 281 return C 282 283 284 if __name__ == '__main__': 285 286 287 import sys, os 288 from os.path import splitext 289 290 if len(sys.argv) > 1: 291 files = sys.argv[1:] 292 for filename in files: 293 root, ext = splitext(filename) 294 295 if ext <> '.c': 296 print 'WARNING (compile.py): Skipping %s. I only compile C-files.' %filename 297 298 else: 299 #path = os.path.split(sys.argv[0])[0] or os.getcwd() 300 path = '.' 301 files = os.listdir(path) 302 303 304 305 for filename in files: 306 root, ext = splitext(filename) 307 308 if ext == '.c': 309 for x in ['.dll', '.so']: 310 try: 311 os.remove(root + x) 312 except: 313 pass 314 315 print '--------------- Trying to compile c-extension %s' %filename 316 try: 317 if filename == 'mesh_engine_c_layer.c': 318 print "********** Manually doing dependencies **************" 319 compile(['mesh_engine_c_layer.c','triangle.c']) 320 else: 321 compile(filename) 322 except: 323 print 'Could not compile C extension %s' %filename 324 else: 325 print 'C extension %s OK' %filename 326 print 327 328 8 import os 9 execfile('..' + os.sep + 'utilities' + os.sep + 'compile.py') -
anuga_core/source/anuga/pmesh/compile.py
r3494 r4761 3 3 Commandline usage: 4 4 python compile.py <filename> 5 6 """ 7 import os 5 8 6 Usage from within Python: 7 import compile 8 compile.compile(<filename>,..) 9 10 Ole Nielsen, Oct 2001 11 """ 12 13 14 def compile(FNs=None, CC=None, LD = None, SFLAG = None, verbose = 1): 15 """compile(FNs=None, CC=None, LD = None, SFLAG = None): 16 17 Compile FN(s) using compiler CC (e.g. mpicc), 18 Loader LD and shared flag SFLAG. 19 If CC is absent use default compiler dependent on platform 20 if LD is absent CC is used. 21 if SFLAG is absent platform default is used 22 FNs can be either one filename or a list of filenames 23 In the latter case, the first will be used to name so file. 24 """ 25 26 import os, string, sys, types 27 28 # Input check 29 # 30 assert not FNs is None, "No filename provided" 31 32 if not type(FNs) == types.ListType: 33 FNs = [FNs] 9 buildroot = os.getcwd() 10 os.chdir('..') 34 11 35 12 36 libext = 'so' #Default extension (Unix) 37 libs = '' 38 version = sys.version[:3] 39 40 # Determine platform and compiler 41 # 42 if sys.platform == 'sunos5': #Solaris 43 if CC: 44 compiler = CC 45 else: 46 compiler = 'gcc' 47 if LD: 48 loader = LD 49 else: 50 loader = compiler 51 if SFLAG: 52 sharedflag = SFLAG 53 else: 54 sharedflag = 'G' 55 56 elif sys.platform == 'osf1V5': #Compaq AlphaServer 57 if CC: 58 compiler = CC 59 else: 60 compiler = 'cc' 61 if LD: 62 loader = LD 63 else: 64 loader = compiler 65 if SFLAG: 66 sharedflag = SFLAG 67 else: 68 sharedflag = 'shared' 69 70 elif sys.platform == 'linux2': #Linux 71 if CC: 72 compiler = CC 73 else: 74 compiler = 'gcc' 75 if LD: 76 loader = LD 77 else: 78 loader = compiler 79 if SFLAG: 80 sharedflag = SFLAG 81 else: 82 sharedflag = 'shared' 83 84 elif sys.platform == 'darwin': #Mac OS X: 85 if CC: 86 compiler = CC 87 else: 88 compiler = 'cc' 89 if LD: 90 loader = LD 91 else: 92 loader = compiler 93 if SFLAG: 94 sharedflag = SFLAG 95 else: 96 sharedflag = 'bundle -flat_namespace -undefined suppress' 13 print 'Changing to', os.getcwd() 97 14 98 elif sys.platform == 'cygwin': #Cygwin (compilation same as linux) 99 if CC: 100 compiler = CC 101 else: 102 compiler = 'gcc' 103 if LD: 104 loader = LD 105 else: 106 loader = compiler 107 if SFLAG: 108 sharedflag = SFLAG 109 else: 110 sharedflag = 'shared' 111 libext = 'dll' 112 libs = '/lib/python%s/config/libpython%s.dll.a' %(version,version) 113 114 elif sys.platform == 'win32': #Windows 115 if CC: 116 compiler = CC 117 else: 118 compiler = 'gcc' 119 if LD: 120 loader = LD 121 else: 122 loader = compiler 123 if SFLAG: 124 sharedflag = SFLAG 125 else: 126 sharedflag = 'shared' 127 libext = 'dll' 15 #entries = listdir('.') 128 16 129 v = version.replace('.','') 130 dllfilename = 'python%s.dll' %(v) 131 libs = os.path.join(sys.exec_prefix,dllfilename) 132 133 134 else: 135 if verbose: print "Unrecognised platform %s - revert to default"\ 136 %sys.platform 137 138 if CC: 139 compiler = CC 140 else: 141 compiler = 'cc' 142 if LD: 143 loader = LD 144 else: 145 loader = 'ld' 146 if SFLAG: 147 sharedflag = SFLAG 148 else: 149 sharedflag = 'G' 150 151 152 # Find location of include files 153 # 154 if sys.platform == 'win32': #Windows 155 include = os.path.join(sys.exec_prefix, 'include') 156 else: 157 include = os.path.join(os.path.join(sys.exec_prefix, 'include'), 158 'python'+version) 159 160 # Check existence of Python.h 161 # 162 headerfile = include + os.sep +'Python.h' 163 try: 164 open(headerfile, 'r') 165 except: 166 raise """Did not find Python header file %s. 167 Make sure files for Python C-extensions are installed. 168 In debian linux, for example, you need to install a 169 package called something like python2.3-dev""" %headerfile 170 171 172 # Check filename(s) 173 # 174 object_files = '' 175 for FN in FNs: 176 root, ext = os.path.splitext(FN) 177 if ext == '': 178 FN = FN + '.c' 179 elif ext.lower() != '.c': 180 raise Exception, "Unrecognised extension: " + FN 181 182 try: 183 open(FN,'r') 184 except: 185 raise Exception, "Could not open: " + FN 186 187 if not object_files: root1 = root #Remember first filename 188 object_files += root + '.o ' 189 190 191 # Compile 192 # 193 s = "%s -c %s -I%s -o %s.o -Wall -O3 -DTRILIBRARY=1 -DNO_TIMER=1" %(compiler, FN, include, root) 194 195 if os.name == 'posix' and os.uname()[4] == 'x86_64': 196 #Extra flags for 64 bit architectures 197 198 #FIXME: Which one? 199 #s += ' -fPIC' 200 s += ' -fPIC -m64' 201 202 203 if verbose: 204 print s 205 else: 206 s = s + ' 2> /dev/null' #Suppress errors 207 208 try: 209 err = os.system(s) 210 if err != 0: 211 raise 'Attempting to compile %s failed - please try manually' %FN 212 except: 213 raise 'Could not compile %s - please try manually' %FN 214 215 216 # Make shared library (*.so or *.dll) 217 218 s = "%s -%s %s -o %s.%s %s -lm" %(loader, sharedflag, object_files, root1, libext, libs) 219 if verbose: 220 print s 221 else: 222 s = s + ' 2> /dev/null' #Suppress warnings 223 224 try: 225 err=os.system(s) 226 if err != 0: 227 raise 'Atempting to link %s failed - please try manually' %root1 228 except: 229 raise 'Could not link %s - please try manually' %root1 230 17 #Attempt to compile mesh_engine extensions 231 18 232 19 233 if __name__ == '__main__': 20 os.chdir('mesh_engine') 21 execfile('..' + os.sep + 'utilities' + os.sep + 'compile.py') 234 22 235 236 import sys, os 237 from os.path import splitext 238 239 if len(sys.argv) > 1: 240 files = sys.argv[1:] 241 for filename in files: 242 root, ext = splitext(filename) 243 244 if ext <> '.c': 245 print 'WARNING (compile.py): Skipping %s. I only compile C-files.' %filename 246 247 else: 248 #path = os.path.split(sys.argv[0])[0] or os.getcwd() 249 path = '.' 250 files = os.listdir(path) 251 252 for filename in files: 253 root, ext = splitext(filename) 254 255 if ext == '.c': 256 for x in ['.dll', '.so']: 257 try: 258 os.remove(root + x) 259 except: 260 pass 261 262 print '--------------- Trying to compile c-extension %s' %filename 263 try: 264 if filename == 'triang.c': 265 print "********** Manually doing dependancies **************" 266 compile(['triang.c','triangle.c']) 267 else: 268 compile(filename) 269 except: 270 print 'Could not compile C extension %s' %filename 271 else: 272 print 'C extension %s OK' %filename 273 print 274 275 23 os.chdir(buildroot) 24
Note: See TracChangeset
for help on using the changeset viewer.