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