[680] | 1 | """compile.py - compile Python C-extension |
---|
| 2 | |
---|
| 3 | Commandline usage: |
---|
| 4 | python compile.py <filename> |
---|
| 5 | |
---|
| 6 | Usage from within Python: |
---|
| 7 | import compile |
---|
| 8 | compile.compile(<filename>,..) |
---|
| 9 | |
---|
[4448] | 10 | Ole Nielsen, Duncan Gray Oct 2001 |
---|
[680] | 11 | """ |
---|
| 12 | |
---|
[6304] | 13 | #NumPy ------------------------------------ |
---|
| 14 | # Something like these lines recommended in "Converting from NUMARRAY to NUMPY" |
---|
| 15 | import numpy |
---|
| 16 | I_dirs = '-I"%s" ' % numpy.get_include() |
---|
| 17 | #NumPy ------------------------------------ |
---|
| 18 | |
---|
[4812] | 19 | # FIXME (Ole): Although this script says it works with a range of compilers, |
---|
| 20 | # it has only really been used with gcc. |
---|
| 21 | |
---|
[4759] | 22 | import os, string, sys, types |
---|
[4813] | 23 | |
---|
| 24 | separation_line = '---------------------------------------' |
---|
[680] | 25 | |
---|
| 26 | def compile(FNs=None, CC=None, LD = None, SFLAG = None, verbose = 1): |
---|
| 27 | """compile(FNs=None, CC=None, LD = None, SFLAG = None): |
---|
| 28 | |
---|
| 29 | Compile FN(s) using compiler CC (e.g. mpicc), |
---|
| 30 | Loader LD and shared flag SFLAG. |
---|
| 31 | If CC is absent use default compiler dependent on platform |
---|
| 32 | if LD is absent CC is used. |
---|
| 33 | if SFLAG is absent platform default is used |
---|
| 34 | FNs can be either one filename or a list of filenames |
---|
| 35 | In the latter case, the first will be used to name so file. |
---|
| 36 | """ |
---|
| 37 | |
---|
| 38 | # Input check |
---|
| 39 | # |
---|
[4813] | 40 | assert not FNs is None, 'No filename provided' |
---|
[680] | 41 | |
---|
| 42 | if not type(FNs) == types.ListType: |
---|
| 43 | FNs = [FNs] |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | libext = 'so' #Default extension (Unix) |
---|
| 47 | libs = '' |
---|
| 48 | version = sys.version[:3] |
---|
| 49 | |
---|
| 50 | # Determine platform and compiler |
---|
| 51 | # |
---|
| 52 | if sys.platform == 'sunos5': #Solaris |
---|
| 53 | if CC: |
---|
| 54 | compiler = CC |
---|
| 55 | else: |
---|
| 56 | compiler = 'gcc' |
---|
| 57 | if LD: |
---|
| 58 | loader = LD |
---|
| 59 | else: |
---|
| 60 | loader = compiler |
---|
| 61 | if SFLAG: |
---|
| 62 | sharedflag = SFLAG |
---|
| 63 | else: |
---|
| 64 | sharedflag = 'G' |
---|
| 65 | |
---|
| 66 | elif sys.platform == 'osf1V5': #Compaq AlphaServer |
---|
| 67 | if CC: |
---|
| 68 | compiler = CC |
---|
| 69 | else: |
---|
| 70 | compiler = 'cc' |
---|
| 71 | if LD: |
---|
| 72 | loader = LD |
---|
| 73 | else: |
---|
| 74 | loader = compiler |
---|
| 75 | if SFLAG: |
---|
| 76 | sharedflag = SFLAG |
---|
| 77 | else: |
---|
| 78 | sharedflag = 'shared' |
---|
| 79 | |
---|
| 80 | elif sys.platform == 'linux2': #Linux |
---|
| 81 | if CC: |
---|
| 82 | compiler = CC |
---|
| 83 | else: |
---|
| 84 | compiler = 'gcc' |
---|
| 85 | if LD: |
---|
| 86 | loader = LD |
---|
| 87 | else: |
---|
| 88 | loader = compiler |
---|
| 89 | if SFLAG: |
---|
| 90 | sharedflag = SFLAG |
---|
| 91 | else: |
---|
| 92 | sharedflag = 'shared' |
---|
| 93 | |
---|
| 94 | elif sys.platform == 'darwin': #Mac OS X: |
---|
| 95 | if CC: |
---|
| 96 | compiler = CC |
---|
| 97 | else: |
---|
| 98 | compiler = 'cc' |
---|
| 99 | if LD: |
---|
| 100 | loader = LD |
---|
| 101 | else: |
---|
| 102 | loader = compiler |
---|
| 103 | if SFLAG: |
---|
| 104 | sharedflag = SFLAG |
---|
| 105 | else: |
---|
| 106 | sharedflag = 'bundle -flat_namespace -undefined suppress' |
---|
| 107 | |
---|
| 108 | elif sys.platform == 'cygwin': #Cygwin (compilation same as linux) |
---|
| 109 | if CC: |
---|
| 110 | compiler = CC |
---|
| 111 | else: |
---|
| 112 | compiler = 'gcc' |
---|
| 113 | if LD: |
---|
| 114 | loader = LD |
---|
| 115 | else: |
---|
| 116 | loader = compiler |
---|
| 117 | if SFLAG: |
---|
| 118 | sharedflag = SFLAG |
---|
| 119 | else: |
---|
| 120 | sharedflag = 'shared' |
---|
| 121 | libext = 'dll' |
---|
| 122 | libs = '/lib/python%s/config/libpython%s.dll.a' %(version,version) |
---|
| 123 | |
---|
| 124 | elif sys.platform == 'win32': #Windows |
---|
| 125 | if CC: |
---|
| 126 | compiler = CC |
---|
| 127 | else: |
---|
[4448] | 128 | compiler = 'gcc.exe' #Some systems require this (a security measure?) |
---|
[680] | 129 | if LD: |
---|
| 130 | loader = LD |
---|
| 131 | else: |
---|
| 132 | loader = compiler |
---|
| 133 | if SFLAG: |
---|
| 134 | sharedflag = SFLAG |
---|
| 135 | else: |
---|
| 136 | sharedflag = 'shared' |
---|
[4493] | 137 | |
---|
[5138] | 138 | # As of python2.5, .pyd is the extension for python extension |
---|
| 139 | # modules. |
---|
| 140 | if sys.version_info[0:2] >= (2, 5): |
---|
| 141 | libext = 'pyd' |
---|
| 142 | else: |
---|
| 143 | libext = 'dll' |
---|
[680] | 144 | |
---|
[4764] | 145 | libs, is_found = set_python_dll_path() |
---|
[680] | 146 | |
---|
| 147 | else: |
---|
| 148 | if verbose: print "Unrecognised platform %s - revert to default"\ |
---|
| 149 | %sys.platform |
---|
| 150 | |
---|
| 151 | if CC: |
---|
| 152 | compiler = CC |
---|
| 153 | else: |
---|
| 154 | compiler = 'cc' |
---|
| 155 | if LD: |
---|
| 156 | loader = LD |
---|
| 157 | else: |
---|
| 158 | loader = 'ld' |
---|
| 159 | if SFLAG: |
---|
| 160 | sharedflag = SFLAG |
---|
| 161 | else: |
---|
| 162 | sharedflag = 'G' |
---|
| 163 | |
---|
[4811] | 164 | |
---|
| 165 | # Verify that compiler can be executed |
---|
[4812] | 166 | print 'Compiler: %s, version ' %compiler, |
---|
[4813] | 167 | sys.stdout.flush() |
---|
[4812] | 168 | s = '%s -dumpversion' %(compiler) |
---|
[4811] | 169 | err = os.system(s) |
---|
[4812] | 170 | print |
---|
[4813] | 171 | |
---|
[4811] | 172 | if err != 0: |
---|
[4812] | 173 | msg = 'Unable to execute compiler: %s. ' %compiler |
---|
| 174 | msg += 'Make sure it is available on the system path.\n' |
---|
| 175 | msg += 'One way to check this is to run %s on ' %compiler |
---|
[4811] | 176 | msg += 'the commandline.' |
---|
[4812] | 177 | raise Exception, msg |
---|
| 178 | |
---|
| 179 | |
---|
[680] | 180 | # Find location of include files |
---|
| 181 | # |
---|
| 182 | if sys.platform == 'win32': #Windows |
---|
[2508] | 183 | python_include = os.path.join(sys.exec_prefix, 'include') |
---|
[680] | 184 | else: |
---|
[2508] | 185 | python_include = os.path.join(os.path.join(sys.exec_prefix, 'include'), |
---|
| 186 | 'python' + version) |
---|
[680] | 187 | |
---|
| 188 | # Check existence of Python.h |
---|
| 189 | # |
---|
[2508] | 190 | headerfile = python_include + os.sep + 'Python.h' |
---|
[680] | 191 | try: |
---|
| 192 | open(headerfile, 'r') |
---|
| 193 | except: |
---|
| 194 | raise """Did not find Python header file %s. |
---|
| 195 | Make sure files for Python C-extensions are installed. |
---|
| 196 | In debian linux, for example, you need to install a |
---|
| 197 | package called something like python2.3-dev""" %headerfile |
---|
[2508] | 198 | |
---|
| 199 | |
---|
| 200 | |
---|
[4812] | 201 | # Add Python path + utilities to includelist (see ticket:31) |
---|
| 202 | # Assume there is only one 'utilities' dir under path dirs |
---|
[3514] | 203 | |
---|
[2508] | 204 | utilities_include_dir = None |
---|
| 205 | for pathdir in sys.path: |
---|
[2778] | 206 | |
---|
[2508] | 207 | utilities_include_dir = pathdir + os.sep + 'utilities' |
---|
[2778] | 208 | #print pathdir |
---|
| 209 | #print utilities_include_dir |
---|
[2508] | 210 | try: |
---|
| 211 | os.stat(utilities_include_dir) |
---|
| 212 | except OSError: |
---|
| 213 | pass |
---|
| 214 | else: |
---|
| 215 | #print 'Found %s to be used as include dir' %utilities_include_dir |
---|
| 216 | break |
---|
| 217 | |
---|
[3526] | 218 | # This is hacky since it |
---|
| 219 | # assumes the location of the compile_all that determines buildroot |
---|
| 220 | try: |
---|
[3560] | 221 | utilities_include_dir = buildroot + os.sep + "source" + os.sep + "anuga" \ |
---|
| 222 | + os.sep + 'utilities' |
---|
| 223 | except: |
---|
| 224 | # This will make compile work locally |
---|
| 225 | utilities_include_dir = '.' |
---|
[4712] | 226 | utilities_include_dir = '../utilities' |
---|
[3560] | 227 | |
---|
| 228 | |
---|
| 229 | |
---|
| 230 | try: |
---|
[3526] | 231 | os.stat(utilities_include_dir) |
---|
| 232 | except OSError: |
---|
| 233 | utilities_include_dir = buildroot + os.sep + 'utilities' |
---|
[680] | 234 | |
---|
| 235 | |
---|
[3526] | 236 | |
---|
[680] | 237 | # Check filename(s) |
---|
| 238 | # |
---|
| 239 | object_files = '' |
---|
| 240 | for FN in FNs: |
---|
| 241 | root, ext = os.path.splitext(FN) |
---|
| 242 | if ext == '': |
---|
| 243 | FN = FN + '.c' |
---|
| 244 | elif ext.lower() != '.c': |
---|
| 245 | raise Exception, "Unrecognised extension: " + FN |
---|
| 246 | |
---|
| 247 | try: |
---|
[2508] | 248 | open(FN, 'r') |
---|
| 249 | except: |
---|
[680] | 250 | raise Exception, "Could not open: " + FN |
---|
| 251 | |
---|
| 252 | if not object_files: root1 = root #Remember first filename |
---|
| 253 | object_files += root + '.o ' |
---|
| 254 | |
---|
| 255 | |
---|
| 256 | # Compile |
---|
| 257 | # |
---|
[2508] | 258 | if utilities_include_dir is None: |
---|
[4448] | 259 | s = '%s -c %s -I"%s" -o "%s.o" -Wall -O3'\ |
---|
[2508] | 260 | %(compiler, FN, python_include, root) |
---|
| 261 | else: |
---|
[4448] | 262 | if FN == "triangle.c" or FN == "mesh_engine_c_layer.c": |
---|
[6304] | 263 | s = '%s -c %s %s -I"%s" -I"%s" -o "%s.o" -O3 -DTRILIBRARY=1 -DNO_TIMER=1'\ |
---|
| 264 | % (compiler, FN, I_dirs, python_include, utilities_include_dir, root) |
---|
[6057] | 265 | elif FN == "polygon_ext.c": |
---|
| 266 | # gcc 4.3.x screws up in this file if '-O3' is used |
---|
[6304] | 267 | s = '%s -c %s %s -I"%s" -I"%s" -o "%s.o" -Wall'\ |
---|
| 268 | %(compiler, FN, I_dirs, python_include, utilities_include_dir, root) |
---|
[4448] | 269 | else: |
---|
[6304] | 270 | s = '%s -c %s %s -I"%s" -I"%s" -o "%s.o" -Wall -O3'\ |
---|
| 271 | %(compiler, FN, I_dirs, python_include, utilities_include_dir, root) |
---|
[755] | 272 | |
---|
[5917] | 273 | if os.name == 'posix' and os.uname()[4] in ['x86_64', 'ia64']: |
---|
| 274 | # Extra flags for 64 bit architectures. |
---|
| 275 | # AMD Opteron will give x86_64 |
---|
| 276 | # Itanium will give ia64 |
---|
| 277 | # |
---|
| 278 | # Second clause will always fail on Win32 because uname is UNIX specific |
---|
| 279 | # but won't get past first clause |
---|
[756] | 280 | |
---|
[5917] | 281 | s += ' -fPIC' # Use position independent code for 64 bit archs |
---|
| 282 | #s += ' -m64' # Used to be necessary for AMD Opteron |
---|
[755] | 283 | |
---|
[756] | 284 | |
---|
[680] | 285 | if verbose: |
---|
| 286 | print s |
---|
[4812] | 287 | |
---|
| 288 | # Doesn't work on Windows anyway |
---|
| 289 | #else: |
---|
| 290 | # s = s + ' 2> /dev/null' #Suppress errors |
---|
| 291 | |
---|
[680] | 292 | try: |
---|
| 293 | err = os.system(s) |
---|
| 294 | if err != 0: |
---|
| 295 | raise 'Attempting to compile %s failed - please try manually' %FN |
---|
| 296 | except: |
---|
| 297 | raise 'Could not compile %s - please try manually' %FN |
---|
| 298 | |
---|
| 299 | |
---|
| 300 | # Make shared library (*.so or *.dll) |
---|
[4448] | 301 | if libs is "": |
---|
| 302 | s = '%s -%s %s -o %s.%s -lm' %(loader, sharedflag, object_files, root1, libext) |
---|
| 303 | else: |
---|
| 304 | s = '%s -%s %s -o %s.%s "%s" -lm' %(loader, sharedflag, object_files, root1, libext, libs) |
---|
[4812] | 305 | |
---|
[680] | 306 | if verbose: |
---|
| 307 | print s |
---|
[4812] | 308 | |
---|
| 309 | # Doesn't work on Windows anyway |
---|
| 310 | #else: |
---|
| 311 | # s = s + ' 2> /dev/null' #Suppress warnings |
---|
[680] | 312 | |
---|
| 313 | try: |
---|
| 314 | err=os.system(s) |
---|
[3791] | 315 | if err != 0: |
---|
[4812] | 316 | raise 'Attempting to link %s failed - please try manually' %root1 |
---|
[680] | 317 | except: |
---|
| 318 | raise 'Could not link %s - please try manually' %root1 |
---|
| 319 | |
---|
| 320 | |
---|
| 321 | def can_use_C_extension(filename): |
---|
| 322 | """Determine whether specified C-extension |
---|
| 323 | can and should be used. |
---|
| 324 | """ |
---|
| 325 | |
---|
[4448] | 326 | from anuga.config import use_extensions |
---|
[680] | 327 | |
---|
| 328 | from os.path import splitext |
---|
| 329 | |
---|
| 330 | root, ext = splitext(filename) |
---|
| 331 | |
---|
| 332 | C=False |
---|
[4448] | 333 | if use_extensions: |
---|
[680] | 334 | try: |
---|
[4448] | 335 | s = 'import %s' %root |
---|
| 336 | #print s |
---|
| 337 | exec(s) |
---|
[680] | 338 | except: |
---|
| 339 | try: |
---|
[4448] | 340 | open(filename) |
---|
[680] | 341 | except: |
---|
[4448] | 342 | msg = 'C extension %s cannot be opened' %filename |
---|
| 343 | print msg |
---|
| 344 | else: |
---|
| 345 | print '------- Trying to compile c-extension %s' %filename |
---|
| 346 | |
---|
[680] | 347 | try: |
---|
[4448] | 348 | compile(filename) |
---|
[680] | 349 | except: |
---|
[4448] | 350 | print 'WARNING: Could not compile C-extension %s'\ |
---|
| 351 | %filename |
---|
[680] | 352 | else: |
---|
[4448] | 353 | try: |
---|
| 354 | exec('import %s' %root) |
---|
| 355 | except: |
---|
| 356 | msg = 'C extension %s seems to compile OK, ' |
---|
| 357 | msg += 'but it can still not be imported.' |
---|
| 358 | raise msg |
---|
| 359 | else: |
---|
| 360 | C=True |
---|
| 361 | else: |
---|
| 362 | C=True |
---|
[680] | 363 | |
---|
| 364 | if not C: |
---|
| 365 | pass |
---|
| 366 | print 'NOTICE: C-extension %s not used' %filename |
---|
| 367 | |
---|
| 368 | return C |
---|
| 369 | |
---|
[4811] | 370 | |
---|
[4764] | 371 | def set_python_dll_path(): |
---|
[5133] | 372 | """ On windows, find which of the two usual hiding places the python |
---|
| 373 | dll is located. |
---|
[4764] | 374 | |
---|
| 375 | If the file can't be found, return None. |
---|
[4759] | 376 | """ |
---|
| 377 | import sys |
---|
| 378 | from os import access, F_OK |
---|
| 379 | |
---|
| 380 | version = sys.version[:3] |
---|
| 381 | v = version.replace('.','') |
---|
| 382 | dllfilename = 'python%s.dll' %(v) |
---|
| 383 | libs = os.path.join(sys.exec_prefix,dllfilename) |
---|
[4811] | 384 | |
---|
[4764] | 385 | is_found = True |
---|
[4759] | 386 | if access(libs,F_OK) == 0 : |
---|
[5133] | 387 | # Sometimes python dll is hidden in %WINDIR%/system32 |
---|
| 388 | libs = os.path.join(os.environ.get('WINDIR', 'C:\WINNT'), 'system32', |
---|
| 389 | dllfilename) |
---|
[4764] | 390 | if access(libs,F_OK) == 0 : |
---|
| 391 | # could not find the dll |
---|
| 392 | libs = os.path.join(sys.exec_prefix,dllfilename) |
---|
| 393 | is_found = False |
---|
| 394 | return libs, is_found |
---|
| 395 | |
---|
| 396 | def check_python_dll(): |
---|
| 397 | libs, is_found = set_python_dll_path() |
---|
| 398 | if not is_found: |
---|
| 399 | print "%s not found.\nPlease install.\nIt is available on the web." \ |
---|
| 400 | %(libs) |
---|
[4759] | 401 | import sys; sys.exit() |
---|
[4764] | 402 | |
---|
[4759] | 403 | |
---|
[680] | 404 | if __name__ == '__main__': |
---|
| 405 | from os.path import splitext |
---|
[4765] | 406 | |
---|
| 407 | if sys.platform == 'win32': |
---|
| 408 | check_python_dll() |
---|
[680] | 409 | if len(sys.argv) > 1: |
---|
| 410 | files = sys.argv[1:] |
---|
| 411 | for filename in files: |
---|
| 412 | root, ext = splitext(filename) |
---|
| 413 | |
---|
| 414 | if ext <> '.c': |
---|
[3791] | 415 | print 'WARNING (compile.py): Skipping %s. I only compile C-files.' %filename |
---|
[680] | 416 | |
---|
| 417 | else: |
---|
| 418 | #path = os.path.split(sys.argv[0])[0] or os.getcwd() |
---|
| 419 | path = '.' |
---|
| 420 | files = os.listdir(path) |
---|
| 421 | |
---|
| 422 | |
---|
| 423 | |
---|
| 424 | for filename in files: |
---|
| 425 | root, ext = splitext(filename) |
---|
| 426 | |
---|
| 427 | if ext == '.c': |
---|
| 428 | for x in ['.dll', '.so']: |
---|
| 429 | try: |
---|
| 430 | os.remove(root + x) |
---|
| 431 | except: |
---|
| 432 | pass |
---|
| 433 | |
---|
[4813] | 434 | print separation_line |
---|
[2508] | 435 | print 'Trying to compile c-extension %s in %s'\ |
---|
| 436 | %(filename, os.getcwd()) |
---|
[680] | 437 | try: |
---|
[4448] | 438 | if filename == 'triang.c': |
---|
| 439 | compile(['triang.c','triangle.c']) |
---|
| 440 | elif filename == 'mesh_engine_c_layer.c': |
---|
| 441 | compile(['mesh_engine_c_layer.c','triangle.c']) |
---|
| 442 | else: |
---|
[680] | 443 | compile(filename) |
---|
[2508] | 444 | except Exception, e: |
---|
[4812] | 445 | msg = 'Could not compile C extension %s\n' %filename |
---|
| 446 | msg += str(e) |
---|
| 447 | raise Exception, msg |
---|
[680] | 448 | else: |
---|
| 449 | print 'C extension %s OK' %filename |
---|
| 450 | print |
---|
| 451 | |
---|
| 452 | |
---|