[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 | |
---|
| 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 | |
---|
| 27 | ###########################jhdsgfjgdjfg |
---|
| 28 | |
---|
| 29 | import os, string, sys, types |
---|
| 30 | |
---|
| 31 | # Input check |
---|
| 32 | # |
---|
| 33 | assert not FNs is None, "No filename provided" |
---|
| 34 | |
---|
| 35 | if not type(FNs) == types.ListType: |
---|
| 36 | FNs = [FNs] |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | libext = 'so' #Default extension (Unix) |
---|
| 40 | libs = '' |
---|
| 41 | version = sys.version[:3] |
---|
| 42 | |
---|
| 43 | # Determine platform and compiler |
---|
| 44 | # |
---|
| 45 | if sys.platform == 'sunos5': #Solaris |
---|
| 46 | if CC: |
---|
| 47 | compiler = CC |
---|
| 48 | else: |
---|
| 49 | compiler = 'gcc' |
---|
| 50 | if LD: |
---|
| 51 | loader = LD |
---|
| 52 | else: |
---|
| 53 | loader = compiler |
---|
| 54 | if SFLAG: |
---|
| 55 | sharedflag = SFLAG |
---|
| 56 | else: |
---|
| 57 | sharedflag = 'G' |
---|
| 58 | |
---|
| 59 | elif sys.platform == 'osf1V5': #Compaq AlphaServer |
---|
| 60 | if CC: |
---|
| 61 | compiler = CC |
---|
| 62 | else: |
---|
| 63 | compiler = 'cc' |
---|
| 64 | if LD: |
---|
| 65 | loader = LD |
---|
| 66 | else: |
---|
| 67 | loader = compiler |
---|
| 68 | if SFLAG: |
---|
| 69 | sharedflag = SFLAG |
---|
| 70 | else: |
---|
| 71 | sharedflag = 'shared' |
---|
| 72 | |
---|
| 73 | elif sys.platform == 'linux2': #Linux |
---|
| 74 | if CC: |
---|
| 75 | compiler = CC |
---|
| 76 | else: |
---|
| 77 | compiler = 'gcc' |
---|
| 78 | if LD: |
---|
| 79 | loader = LD |
---|
| 80 | else: |
---|
| 81 | loader = compiler |
---|
| 82 | if SFLAG: |
---|
| 83 | sharedflag = SFLAG |
---|
| 84 | else: |
---|
| 85 | sharedflag = 'shared' |
---|
| 86 | |
---|
| 87 | elif sys.platform == 'darwin': #Mac OS X: |
---|
| 88 | if CC: |
---|
| 89 | compiler = CC |
---|
| 90 | else: |
---|
| 91 | compiler = 'cc' |
---|
| 92 | if LD: |
---|
| 93 | loader = LD |
---|
| 94 | else: |
---|
| 95 | loader = compiler |
---|
| 96 | if SFLAG: |
---|
| 97 | sharedflag = SFLAG |
---|
| 98 | else: |
---|
| 99 | sharedflag = 'bundle -flat_namespace -undefined suppress' |
---|
| 100 | |
---|
| 101 | elif sys.platform == 'cygwin': #Cygwin (compilation same as linux) |
---|
| 102 | if CC: |
---|
| 103 | compiler = CC |
---|
| 104 | else: |
---|
| 105 | compiler = 'gcc' |
---|
| 106 | if LD: |
---|
| 107 | loader = LD |
---|
| 108 | else: |
---|
| 109 | loader = compiler |
---|
| 110 | if SFLAG: |
---|
| 111 | sharedflag = SFLAG |
---|
| 112 | else: |
---|
| 113 | sharedflag = 'shared' |
---|
| 114 | libext = 'dll' |
---|
| 115 | libs = '/lib/python%s/config/libpython%s.dll.a' %(version,version) |
---|
| 116 | |
---|
| 117 | elif sys.platform == 'win32': #Windows |
---|
| 118 | if CC: |
---|
| 119 | compiler = CC |
---|
| 120 | else: |
---|
| 121 | compiler = 'gcc' |
---|
| 122 | if LD: |
---|
| 123 | loader = LD |
---|
| 124 | else: |
---|
| 125 | loader = compiler |
---|
| 126 | if SFLAG: |
---|
| 127 | sharedflag = SFLAG |
---|
| 128 | else: |
---|
| 129 | sharedflag = 'shared' |
---|
| 130 | libext = 'dll' |
---|
| 131 | |
---|
| 132 | v = version.replace('.','') |
---|
| 133 | dllfilename = 'python%s.dll' %(v) |
---|
| 134 | libs = os.path.join(sys.exec_prefix,dllfilename) |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | else: |
---|
| 138 | if verbose: print "Unrecognised platform %s - revert to default"\ |
---|
| 139 | %sys.platform |
---|
| 140 | |
---|
| 141 | if CC: |
---|
| 142 | compiler = CC |
---|
| 143 | else: |
---|
| 144 | compiler = 'cc' |
---|
| 145 | if LD: |
---|
| 146 | loader = LD |
---|
| 147 | else: |
---|
| 148 | loader = 'ld' |
---|
| 149 | if SFLAG: |
---|
| 150 | sharedflag = SFLAG |
---|
| 151 | else: |
---|
| 152 | sharedflag = 'G' |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | # Find location of include files |
---|
| 156 | # |
---|
| 157 | if sys.platform == 'win32': #Windows |
---|
[2508] | 158 | python_include = os.path.join(sys.exec_prefix, 'include') |
---|
[680] | 159 | else: |
---|
[2508] | 160 | python_include = os.path.join(os.path.join(sys.exec_prefix, 'include'), |
---|
| 161 | 'python' + version) |
---|
[680] | 162 | |
---|
| 163 | # Check existence of Python.h |
---|
| 164 | # |
---|
[2508] | 165 | headerfile = python_include + os.sep + 'Python.h' |
---|
[680] | 166 | try: |
---|
| 167 | open(headerfile, 'r') |
---|
| 168 | except: |
---|
| 169 | raise """Did not find Python header file %s. |
---|
| 170 | Make sure files for Python C-extensions are installed. |
---|
| 171 | In debian linux, for example, you need to install a |
---|
| 172 | package called something like python2.3-dev""" %headerfile |
---|
[2508] | 173 | |
---|
| 174 | |
---|
| 175 | |
---|
| 176 | #Add Python path + utilities to includelist (see ticket:31) |
---|
[2756] | 177 | #Assume there is only one 'utilities' dir under path dirs |
---|
[3514] | 178 | |
---|
[2508] | 179 | utilities_include_dir = None |
---|
| 180 | for pathdir in sys.path: |
---|
[2778] | 181 | |
---|
[2508] | 182 | utilities_include_dir = pathdir + os.sep + 'utilities' |
---|
[2778] | 183 | #print pathdir |
---|
| 184 | #print utilities_include_dir |
---|
[2508] | 185 | try: |
---|
| 186 | os.stat(utilities_include_dir) |
---|
| 187 | except OSError: |
---|
| 188 | pass |
---|
| 189 | else: |
---|
| 190 | #print 'Found %s to be used as include dir' %utilities_include_dir |
---|
| 191 | break |
---|
| 192 | |
---|
[3526] | 193 | # This is hacky since it |
---|
| 194 | # assumes the location of the compile_all that determines buildroot |
---|
| 195 | try: |
---|
[3560] | 196 | utilities_include_dir = buildroot + os.sep + "source" + os.sep + "anuga" \ |
---|
| 197 | + os.sep + 'utilities' |
---|
| 198 | except: |
---|
| 199 | # This will make compile work locally |
---|
| 200 | utilities_include_dir = '.' |
---|
| 201 | |
---|
| 202 | |
---|
| 203 | |
---|
| 204 | try: |
---|
[3526] | 205 | os.stat(utilities_include_dir) |
---|
| 206 | except OSError: |
---|
| 207 | utilities_include_dir = buildroot + os.sep + 'utilities' |
---|
[680] | 208 | |
---|
| 209 | |
---|
[3526] | 210 | |
---|
[680] | 211 | # Check filename(s) |
---|
| 212 | # |
---|
| 213 | object_files = '' |
---|
| 214 | for FN in FNs: |
---|
| 215 | root, ext = os.path.splitext(FN) |
---|
| 216 | if ext == '': |
---|
| 217 | FN = FN + '.c' |
---|
| 218 | elif ext.lower() != '.c': |
---|
| 219 | raise Exception, "Unrecognised extension: " + FN |
---|
| 220 | |
---|
| 221 | try: |
---|
[2508] | 222 | open(FN, 'r') |
---|
| 223 | except: |
---|
| 224 | #print 'CWD:', os.getcwd() |
---|
[680] | 225 | raise Exception, "Could not open: " + FN |
---|
| 226 | |
---|
| 227 | if not object_files: root1 = root #Remember first filename |
---|
| 228 | object_files += root + '.o ' |
---|
| 229 | |
---|
| 230 | |
---|
| 231 | # Compile |
---|
| 232 | # |
---|
[2508] | 233 | if utilities_include_dir is None: |
---|
| 234 | s = '%s -c %s -I%s -o %s.o -Wall -O3'\ |
---|
| 235 | %(compiler, FN, python_include, root) |
---|
| 236 | else: |
---|
| 237 | s = '%s -c %s -I%s -I%s -o %s.o -Wall -O3'\ |
---|
| 238 | %(compiler, FN, python_include, utilities_include_dir, root) |
---|
[755] | 239 | |
---|
| 240 | if os.name == 'posix' and os.uname()[4] == 'x86_64': |
---|
[756] | 241 | #Extra flags for 64 bit architectures |
---|
[1087] | 242 | #Second clause will always fail on Win32 because uname is UNIX specific |
---|
| 243 | #but won't get past first clause |
---|
[756] | 244 | |
---|
| 245 | #FIXME: Which one? |
---|
| 246 | #s += ' -fPIC' |
---|
| 247 | s += ' -fPIC -m64' |
---|
[755] | 248 | |
---|
[756] | 249 | |
---|
[680] | 250 | if verbose: |
---|
| 251 | print s |
---|
| 252 | else: |
---|
| 253 | s = s + ' 2> /dev/null' #Suppress errors |
---|
| 254 | |
---|
| 255 | try: |
---|
| 256 | err = os.system(s) |
---|
| 257 | if err != 0: |
---|
| 258 | raise 'Attempting to compile %s failed - please try manually' %FN |
---|
| 259 | except: |
---|
| 260 | raise 'Could not compile %s - please try manually' %FN |
---|
| 261 | |
---|
| 262 | |
---|
| 263 | # Make shared library (*.so or *.dll) |
---|
| 264 | |
---|
| 265 | s = "%s -%s %s -o %s.%s %s -lm" %(loader, sharedflag, object_files, root1, libext, libs) |
---|
| 266 | if verbose: |
---|
| 267 | print s |
---|
| 268 | else: |
---|
| 269 | s = s + ' 2> /dev/null' #Suppress warnings |
---|
| 270 | |
---|
| 271 | try: |
---|
| 272 | err=os.system(s) |
---|
[3791] | 273 | if err != 0: |
---|
| 274 | raise 'Atempting to link %s failed - please try manually' %root1 |
---|
[680] | 275 | except: |
---|
| 276 | raise 'Could not link %s - please try manually' %root1 |
---|
| 277 | |
---|
| 278 | |
---|
| 279 | def can_use_C_extension(filename): |
---|
| 280 | """Determine whether specified C-extension |
---|
| 281 | can and should be used. |
---|
| 282 | """ |
---|
| 283 | |
---|
| 284 | |
---|
| 285 | from os.path import splitext |
---|
| 286 | |
---|
| 287 | root, ext = splitext(filename) |
---|
| 288 | |
---|
| 289 | C=False |
---|
[1910] | 290 | |
---|
| 291 | try: |
---|
| 292 | s = 'import %s' %root |
---|
| 293 | #print s |
---|
| 294 | exec(s) |
---|
| 295 | except: |
---|
[680] | 296 | try: |
---|
[1910] | 297 | open(filename) |
---|
[680] | 298 | except: |
---|
[1910] | 299 | msg = 'C extension %s cannot be opened' %filename |
---|
| 300 | print msg |
---|
| 301 | else: |
---|
| 302 | print '------- Trying to compile c-extension %s' %filename |
---|
[2508] | 303 | |
---|
[3560] | 304 | compile(filename) |
---|
[680] | 305 | try: |
---|
[1910] | 306 | compile(filename) |
---|
[680] | 307 | except: |
---|
[1910] | 308 | print 'WARNING: Could not compile C-extension %s'\ |
---|
| 309 | %filename |
---|
| 310 | else: |
---|
[680] | 311 | try: |
---|
[1910] | 312 | exec('import %s' %root) |
---|
[680] | 313 | except: |
---|
[1910] | 314 | msg = 'C extension %s seems to compile OK, ' |
---|
| 315 | msg += 'but it can still not be imported.' |
---|
| 316 | raise msg |
---|
[680] | 317 | else: |
---|
[1910] | 318 | C=True |
---|
| 319 | else: |
---|
| 320 | C=True |
---|
[680] | 321 | |
---|
| 322 | if not C: |
---|
| 323 | pass |
---|
| 324 | print 'NOTICE: C-extension %s not used' %filename |
---|
| 325 | |
---|
| 326 | return C |
---|
| 327 | |
---|
| 328 | |
---|
| 329 | |
---|
| 330 | |
---|
| 331 | if __name__ == '__main__': |
---|
| 332 | |
---|
| 333 | |
---|
| 334 | import sys, os |
---|
| 335 | from os.path import splitext |
---|
| 336 | |
---|
| 337 | if len(sys.argv) > 1: |
---|
| 338 | files = sys.argv[1:] |
---|
| 339 | for filename in files: |
---|
| 340 | root, ext = splitext(filename) |
---|
| 341 | |
---|
| 342 | if ext <> '.c': |
---|
[3791] | 343 | print 'WARNING (compile.py): Skipping %s. I only compile C-files.' %filename |
---|
[680] | 344 | |
---|
| 345 | else: |
---|
| 346 | #path = os.path.split(sys.argv[0])[0] or os.getcwd() |
---|
| 347 | path = '.' |
---|
| 348 | files = os.listdir(path) |
---|
| 349 | |
---|
| 350 | |
---|
| 351 | |
---|
| 352 | for filename in files: |
---|
| 353 | root, ext = splitext(filename) |
---|
| 354 | |
---|
| 355 | if ext == '.c': |
---|
| 356 | for x in ['.dll', '.so']: |
---|
| 357 | try: |
---|
| 358 | os.remove(root + x) |
---|
| 359 | except: |
---|
| 360 | pass |
---|
| 361 | |
---|
[2508] | 362 | print '--------------------------------------' |
---|
| 363 | print 'Trying to compile c-extension %s in %s'\ |
---|
| 364 | %(filename, os.getcwd()) |
---|
| 365 | |
---|
[3560] | 366 | compile(filename) |
---|
[680] | 367 | try: |
---|
| 368 | compile(filename) |
---|
[2508] | 369 | except Exception, e: |
---|
| 370 | print 'Could not compile C extension %s' %filename |
---|
[680] | 371 | else: |
---|
| 372 | print 'C extension %s OK' %filename |
---|
| 373 | print |
---|
| 374 | |
---|
| 375 | |
---|