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