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