[6991] | 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 | import numpy |
---|
| 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 | import os, string, sys, types |
---|
| 26 | |
---|
| 27 | # Input check |
---|
| 28 | # |
---|
| 29 | assert not FNs is None, "No filename provided" |
---|
| 30 | |
---|
| 31 | if not type(FNs) == types.ListType: |
---|
| 32 | FNs = [FNs] |
---|
| 33 | |
---|
| 34 | # Determine platform and compiler |
---|
| 35 | # |
---|
| 36 | if sys.platform == 'sunos5': #Solaris |
---|
| 37 | if CC: |
---|
| 38 | compiler = CC |
---|
| 39 | else: |
---|
| 40 | compiler = 'gcc' |
---|
| 41 | if LD: |
---|
| 42 | loader = LD |
---|
| 43 | else: |
---|
| 44 | loader = compiler |
---|
| 45 | if SFLAG: |
---|
| 46 | sharedflag = SFLAG |
---|
| 47 | else: |
---|
| 48 | sharedflag = 'G' |
---|
| 49 | |
---|
| 50 | elif sys.platform == 'osf1V5': #Compaq AlphaServer |
---|
| 51 | if CC: |
---|
| 52 | compiler = CC |
---|
| 53 | else: |
---|
| 54 | compiler = 'cc' |
---|
| 55 | if LD: |
---|
| 56 | loader = LD |
---|
| 57 | else: |
---|
| 58 | loader = compiler |
---|
| 59 | if SFLAG: |
---|
| 60 | sharedflag = SFLAG |
---|
| 61 | else: |
---|
| 62 | sharedflag = 'shared' |
---|
| 63 | |
---|
| 64 | elif sys.platform == 'linux2': #Linux |
---|
| 65 | if CC: |
---|
| 66 | compiler = CC |
---|
| 67 | else: |
---|
| 68 | compiler = 'gcc' |
---|
| 69 | if LD: |
---|
| 70 | loader = LD |
---|
| 71 | else: |
---|
| 72 | loader = compiler |
---|
| 73 | if SFLAG: |
---|
| 74 | sharedflag = SFLAG |
---|
| 75 | else: |
---|
| 76 | sharedflag = 'shared' |
---|
| 77 | |
---|
| 78 | elif sys.platform == 'darwin': #Mac OS X: |
---|
| 79 | if CC: |
---|
| 80 | compiler = CC |
---|
| 81 | else: |
---|
| 82 | compiler = 'cc' |
---|
| 83 | if LD: |
---|
| 84 | loader = LD |
---|
| 85 | else: |
---|
| 86 | loader = compiler |
---|
| 87 | if SFLAG: |
---|
| 88 | sharedflag = SFLAG |
---|
| 89 | else: |
---|
| 90 | sharedflag = 'bundle -flat_namespace -undefined suppress' |
---|
| 91 | |
---|
| 92 | else: |
---|
| 93 | if verbose: print "Unrecognised platform %s - revert to default"\ |
---|
| 94 | %sys.platform |
---|
| 95 | |
---|
| 96 | if CC: |
---|
| 97 | compiler = CC |
---|
| 98 | else: |
---|
| 99 | compiler = 'cc' |
---|
| 100 | if LD: |
---|
| 101 | loader = LD |
---|
| 102 | else: |
---|
| 103 | loader = 'ld' |
---|
| 104 | if SFLAG: |
---|
| 105 | sharedflag = SFLAG |
---|
| 106 | else: |
---|
| 107 | sharedflag = 'G' |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | # Find location of include files |
---|
| 111 | # |
---|
| 112 | python_include = os.path.join(os.path.join(sys.exec_prefix, 'include'), |
---|
| 113 | 'python'+sys.version[:3]) |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | # Check existence of Python.h |
---|
| 117 | # |
---|
| 118 | headerfile = python_include + '/Python.h' |
---|
| 119 | try: |
---|
| 120 | open(headerfile, 'r') |
---|
| 121 | except: |
---|
| 122 | raise """Did not find Python header file %s. |
---|
| 123 | Make sure files for Python C-extensions are installed. |
---|
| 124 | In debian linux, for example, you need to install a |
---|
| 125 | package called something like python2.1-dev""" %headerfile |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | # Get numpy include |
---|
| 129 | numpy_include = numpy.get_include() |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | # Check filename(s) |
---|
| 133 | # |
---|
| 134 | object_files = '' |
---|
| 135 | for FN in FNs: |
---|
| 136 | root, ext = os.path.splitext(FN) |
---|
| 137 | if ext == '': |
---|
| 138 | FN = FN + '.c' |
---|
| 139 | elif ext.lower() != '.c': |
---|
| 140 | raise Exception, "Unrecognised extension: " + FN |
---|
| 141 | |
---|
| 142 | try: |
---|
| 143 | open(FN,'r') |
---|
| 144 | except: |
---|
| 145 | raise Exception, "Could not open: " + FN |
---|
| 146 | |
---|
| 147 | if not object_files: root1 = root #Remember first filename |
---|
| 148 | object_files += root + '.o ' |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | # Compile |
---|
| 152 | # |
---|
| 153 | s = "%s -c %s -I%s -I%s -o %s.o -Wall" %(compiler, FN, |
---|
| 154 | python_include, numpy_include, |
---|
| 155 | root) |
---|
| 156 | if os.name == 'posix' and os.uname()[4] == 'x86_64': |
---|
| 157 | #Extra flags for 64 bit architectures |
---|
| 158 | s += ' -fPIC -m64' #gcc |
---|
| 159 | #s += ' -fPIC -tp amd64' #pgcc |
---|
| 160 | |
---|
| 161 | if verbose: |
---|
| 162 | print s |
---|
| 163 | else: |
---|
| 164 | s = s + ' 2> /dev/null' #Suppress errors |
---|
| 165 | |
---|
| 166 | try: |
---|
| 167 | os.system(s) |
---|
| 168 | except: |
---|
| 169 | raise 'Could not compile %s - please try manually' %FN |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | # Make shared library (*.so) |
---|
| 173 | s = "%s -%s %s -o %s.so" %(loader, sharedflag, object_files, root1) |
---|
| 174 | |
---|
| 175 | if os.name == 'posix' and os.uname()[4] == 'x86_64': |
---|
| 176 | pass |
---|
| 177 | #Extra flags for 64 bit architectures using Portland compilers |
---|
| 178 | s += ' -mcmodel=medium' |
---|
| 179 | |
---|
| 180 | if verbose: |
---|
| 181 | print s |
---|
| 182 | else: |
---|
| 183 | s = s + ' 2> /dev/null' #Suppress warnings |
---|
| 184 | |
---|
| 185 | try: |
---|
| 186 | err=os.system(s) |
---|
| 187 | except: |
---|
| 188 | raise 'Could not link %s - please try manually' %root1 |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | if __name__ == '__main__': |
---|
| 194 | |
---|
| 195 | import sys |
---|
| 196 | |
---|
| 197 | # Get file to compile |
---|
| 198 | # |
---|
| 199 | if len(sys.argv) < 2: |
---|
| 200 | print "Usage:" |
---|
| 201 | print " python compile.py <filename>" |
---|
| 202 | sys.exit() |
---|
| 203 | |
---|
| 204 | compile(sys.argv[1], verbose=1) |
---|
| 205 | |
---|