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