]>
Commit | Line | Data |
---|---|---|
1e4a197e RD |
1 | """distutils.cygwinccompiler |
2 | ||
3 | Provides the CygwinCCompiler class, a subclass of UnixCCompiler that | |
4 | handles the Cygwin port of the GNU C compiler to Windows. It also contains | |
5 | the Mingw32CCompiler class which handles the mingw32 port of GCC (same as | |
6 | cygwin in no-cygwin mode). | |
7 | """ | |
8 | ||
9 | # problems: | |
10 | # | |
11 | # * if you use a msvc compiled python version (1.5.2) | |
12 | # 1. you have to insert a __GNUC__ section in its config.h | |
13 | # 2. you have to generate a import library for its dll | |
14 | # - create a def-file for python??.dll | |
15 | # - create a import library using | |
16 | # dlltool --dllname python15.dll --def python15.def \ | |
17 | # --output-lib libpython15.a | |
18 | # | |
19 | # see also http://starship.python.net/crew/kernr/mingw32/Notes.html | |
20 | # | |
21 | # * We put export_symbols in a def-file, and don't use | |
22 | # --export-all-symbols because it doesn't worked reliable in some | |
23 | # tested configurations. And because other windows compilers also | |
24 | # need their symbols specified this no serious problem. | |
25 | # | |
26 | # tested configurations: | |
27 | # | |
28 | # * cygwin gcc 2.91.57/ld 2.9.4/dllwrap 0.2.4 works | |
29 | # (after patching python's config.h and for C++ some other include files) | |
30 | # see also http://starship.python.net/crew/kernr/mingw32/Notes.html | |
31 | # * mingw32 gcc 2.95.2/ld 2.9.4/dllwrap 0.2.4 works | |
32 | # (ld doesn't support -shared, so we use dllwrap) | |
33 | # * cygwin gcc 2.95.2/ld 2.10.90/dllwrap 2.10.90 works now | |
34 | # - its dllwrap doesn't work, there is a bug in binutils 2.10.90 | |
35 | # see also http://sources.redhat.com/ml/cygwin/2000-06/msg01274.html | |
36 | # - using gcc -mdll instead dllwrap doesn't work without -static because | |
37 | # it tries to link against dlls instead their import libraries. (If | |
38 | # it finds the dll first.) | |
39 | # By specifying -static we force ld to link against the import libraries, | |
40 | # this is windows standard and there are normally not the necessary symbols | |
41 | # in the dlls. | |
42 | # *** only the version of June 2000 shows these problems | |
43 | ||
44 | # This module should be kept compatible with Python 1.5.2. | |
45 | ||
46 | __revision__ = "$Id$" | |
47 | ||
48 | import os,sys,copy | |
49 | from distutils.ccompiler import gen_preprocess_options, gen_lib_options | |
50 | from distutils.unixccompiler import UnixCCompiler | |
51 | from distutils.file_util import write_file | |
52 | from distutils.errors import DistutilsExecError, CompileError, UnknownFileError | |
53 | from distutils import log | |
54 | ||
55 | class CygwinCCompiler (UnixCCompiler): | |
56 | ||
57 | compiler_type = 'cygwin' | |
58 | obj_extension = ".o" | |
59 | static_lib_extension = ".a" | |
60 | shared_lib_extension = ".dll" | |
61 | static_lib_format = "lib%s%s" | |
62 | shared_lib_format = "%s%s" | |
63 | exe_extension = ".exe" | |
64 | ||
65 | def __init__ (self, verbose=0, dry_run=0, force=0): | |
66 | ||
67 | UnixCCompiler.__init__ (self, verbose, dry_run, force) | |
68 | ||
69 | (status, details) = check_config_h() | |
70 | self.debug_print("Python's GCC status: %s (details: %s)" % | |
71 | (status, details)) | |
72 | if status is not CONFIG_H_OK: | |
73 | self.warn( | |
74 | "Python's pyconfig.h doesn't seem to support your compiler. " | |
75 | "Reason: %s. " | |
76 | "Compiling may fail because of undefined preprocessor macros." | |
77 | % details) | |
78 | ||
79 | self.gcc_version, self.ld_version, self.dllwrap_version = \ | |
80 | get_versions() | |
81 | self.debug_print(self.compiler_type + ": gcc %s, ld %s, dllwrap %s\n" % | |
82 | (self.gcc_version, | |
83 | self.ld_version, | |
84 | self.dllwrap_version) ) | |
85 | ||
86 | # ld_version >= "2.10.90" should also be able to use | |
87 | # gcc -mdll instead of dllwrap | |
88 | # Older dllwraps had own version numbers, newer ones use the | |
89 | # same as the rest of binutils ( also ld ) | |
90 | # dllwrap 2.10.90 is buggy | |
91 | if self.ld_version >= "2.10.90": | |
92 | self.linker_dll = "gcc" | |
93 | else: | |
94 | self.linker_dll = "dllwrap" | |
95 | ||
96 | # Hard-code GCC because that's what this is all about. | |
97 | # XXX optimization, warnings etc. should be customizable. | |
98 | self.set_executables(compiler='gcc -mcygwin -O -Wall', | |
99 | compiler_so='gcc -mcygwin -mdll -O -Wall', | |
100 | linker_exe='gcc -mcygwin', | |
101 | linker_so=('%s -mcygwin -mdll -static' % | |
102 | self.linker_dll)) | |
103 | ||
104 | # cygwin and mingw32 need different sets of libraries | |
105 | if self.gcc_version == "2.91.57": | |
106 | # cygwin shouldn't need msvcrt, but without the dlls will crash | |
107 | # (gcc version 2.91.57) -- perhaps something about initialization | |
108 | self.dll_libraries=["msvcrt"] | |
109 | self.warn( | |
110 | "Consider upgrading to a newer version of gcc") | |
111 | else: | |
112 | self.dll_libraries=[] | |
113 | ||
114 | # __init__ () | |
115 | ||
116 | ||
117 | def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts): | |
118 | if ext == '.rc' or ext == '.res': | |
119 | # gcc needs '.res' and '.rc' compiled to object files !!! | |
120 | try: | |
121 | self.spawn(["windres", "-i", src, "-o", obj]) | |
122 | except DistutilsExecError, msg: | |
123 | raise CompileError, msg | |
124 | else: # for other files use the C-compiler | |
125 | try: | |
126 | self.spawn(self.compiler_so + cc_args + [src, '-o', obj] + | |
127 | extra_postargs) | |
128 | except DistutilsExecError, msg: | |
129 | raise CompileError, msg | |
130 | ||
131 | def link (self, | |
132 | target_desc, | |
133 | objects, | |
134 | output_filename, | |
135 | output_dir=None, | |
136 | libraries=None, | |
137 | library_dirs=None, | |
138 | runtime_library_dirs=None, | |
139 | export_symbols=None, | |
140 | debug=0, | |
141 | extra_preargs=None, | |
142 | extra_postargs=None, | |
143 | build_temp=None, | |
144 | target_lang=None): | |
145 | ||
146 | # use separate copies, so we can modify the lists | |
147 | extra_preargs = copy.copy(extra_preargs or []) | |
148 | libraries = copy.copy(libraries or []) | |
149 | objects = copy.copy(objects or []) | |
150 | ||
151 | # Additional libraries | |
152 | libraries.extend(self.dll_libraries) | |
153 | ||
154 | # handle export symbols by creating a def-file | |
155 | # with executables this only works with gcc/ld as linker | |
156 | if ((export_symbols is not None) and | |
157 | (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")): | |
158 | # (The linker doesn't do anything if output is up-to-date. | |
159 | # So it would probably better to check if we really need this, | |
160 | # but for this we had to insert some unchanged parts of | |
161 | # UnixCCompiler, and this is not what we want.) | |
162 | ||
163 | # we want to put some files in the same directory as the | |
164 | # object files are, build_temp doesn't help much | |
165 | # where are the object files | |
166 | temp_dir = os.path.dirname(objects[0]) | |
167 | # name of dll to give the helper files the same base name | |
168 | (dll_name, dll_extension) = os.path.splitext( | |
169 | os.path.basename(output_filename)) | |
170 | ||
171 | # generate the filenames for these files | |
172 | def_file = os.path.join(temp_dir, dll_name + ".def") | |
173 | lib_file = os.path.join(temp_dir, 'lib' + dll_name + ".a") | |
174 | ||
175 | # Generate .def file | |
176 | contents = [ | |
177 | "LIBRARY %s" % os.path.basename(output_filename), | |
178 | "EXPORTS"] | |
179 | for sym in export_symbols: | |
180 | contents.append(sym) | |
181 | self.execute(write_file, (def_file, contents), | |
182 | "writing %s" % def_file) | |
183 | ||
184 | # next add options for def-file and to creating import libraries | |
185 | ||
186 | # dllwrap uses different options than gcc/ld | |
187 | if self.linker_dll == "dllwrap": | |
188 | extra_preargs.extend(["--output-lib", lib_file]) | |
189 | # for dllwrap we have to use a special option | |
190 | extra_preargs.extend(["--def", def_file]) | |
191 | # we use gcc/ld here and can be sure ld is >= 2.9.10 | |
192 | else: | |
193 | # doesn't work: bfd_close build\...\libfoo.a: Invalid operation | |
194 | #extra_preargs.extend(["-Wl,--out-implib,%s" % lib_file]) | |
195 | # for gcc/ld the def-file is specified as any object files | |
196 | objects.append(def_file) | |
197 | ||
198 | #end: if ((export_symbols is not None) and | |
199 | # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")): | |
200 | ||
201 | # who wants symbols and a many times larger output file | |
202 | # should explicitly switch the debug mode on | |
203 | # otherwise we let dllwrap/ld strip the output file | |
204 | # (On my machine: 10KB < stripped_file < ??100KB | |
205 | # unstripped_file = stripped_file + XXX KB | |
206 | # ( XXX=254 for a typical python extension)) | |
207 | if not debug: | |
208 | extra_preargs.append("-s") | |
209 | ||
210 | UnixCCompiler.link(self, | |
211 | target_desc, | |
212 | objects, | |
213 | output_filename, | |
214 | output_dir, | |
215 | libraries, | |
216 | library_dirs, | |
217 | runtime_library_dirs, | |
218 | None, # export_symbols, we do this in our def-file | |
219 | debug, | |
220 | extra_preargs, | |
221 | extra_postargs, | |
222 | build_temp, | |
223 | target_lang) | |
224 | ||
225 | # link () | |
226 | ||
227 | # -- Miscellaneous methods ----------------------------------------- | |
228 | ||
229 | # overwrite the one from CCompiler to support rc and res-files | |
230 | def object_filenames (self, | |
231 | source_filenames, | |
232 | strip_dir=0, | |
233 | output_dir=''): | |
234 | if output_dir is None: output_dir = '' | |
235 | obj_names = [] | |
236 | for src_name in source_filenames: | |
237 | # use normcase to make sure '.rc' is really '.rc' and not '.RC' | |
238 | (base, ext) = os.path.splitext (os.path.normcase(src_name)) | |
239 | if ext not in (self.src_extensions + ['.rc','.res']): | |
240 | raise UnknownFileError, \ | |
241 | "unknown file type '%s' (from '%s')" % \ | |
242 | (ext, src_name) | |
243 | if strip_dir: | |
244 | base = os.path.basename (base) | |
245 | if ext == '.res' or ext == '.rc': | |
246 | # these need to be compiled to object files | |
247 | obj_names.append (os.path.join (output_dir, | |
248 | base + ext + self.obj_extension)) | |
249 | else: | |
250 | obj_names.append (os.path.join (output_dir, | |
251 | base + self.obj_extension)) | |
252 | return obj_names | |
253 | ||
254 | # object_filenames () | |
255 | ||
256 | # class CygwinCCompiler | |
257 | ||
258 | ||
259 | # the same as cygwin plus some additional parameters | |
260 | class Mingw32CCompiler (CygwinCCompiler): | |
261 | ||
262 | compiler_type = 'mingw32' | |
263 | ||
264 | def __init__ (self, | |
265 | verbose=0, | |
266 | dry_run=0, | |
267 | force=0): | |
268 | ||
269 | CygwinCCompiler.__init__ (self, verbose, dry_run, force) | |
270 | ||
271 | # A real mingw32 doesn't need to specify a different entry point, | |
272 | # but cygwin 2.91.57 in no-cygwin-mode needs it. | |
273 | if self.gcc_version <= "2.91.57": | |
274 | entry_point = '--entry _DllMain@12' | |
275 | else: | |
276 | entry_point = '' | |
277 | ||
278 | self.set_executables(compiler='gcc -mno-cygwin -O -Wall', | |
279 | compiler_so='gcc -mno-cygwin -mdll -O -Wall', | |
280 | linker_exe='gcc -mno-cygwin', | |
281 | linker_so='%s -mno-cygwin -mdll -static %s' | |
282 | % (self.linker_dll, entry_point)) | |
283 | # Maybe we should also append -mthreads, but then the finished | |
284 | # dlls need another dll (mingwm10.dll see Mingw32 docs) | |
285 | # (-mthreads: Support thread-safe exception handling on `Mingw32') | |
286 | ||
287 | # no additional libraries needed | |
288 | self.dll_libraries=[] | |
289 | ||
290 | # __init__ () | |
291 | ||
292 | # class Mingw32CCompiler | |
293 | ||
294 | # Because these compilers aren't configured in Python's pyconfig.h file by | |
295 | # default, we should at least warn the user if he is using a unmodified | |
296 | # version. | |
297 | ||
298 | CONFIG_H_OK = "ok" | |
299 | CONFIG_H_NOTOK = "not ok" | |
300 | CONFIG_H_UNCERTAIN = "uncertain" | |
301 | ||
302 | def check_config_h(): | |
303 | ||
304 | """Check if the current Python installation (specifically, pyconfig.h) | |
305 | appears amenable to building extensions with GCC. Returns a tuple | |
306 | (status, details), where 'status' is one of the following constants: | |
307 | CONFIG_H_OK | |
308 | all is well, go ahead and compile | |
309 | CONFIG_H_NOTOK | |
310 | doesn't look good | |
311 | CONFIG_H_UNCERTAIN | |
312 | not sure -- unable to read pyconfig.h | |
313 | 'details' is a human-readable string explaining the situation. | |
314 | ||
315 | Note there are two ways to conclude "OK": either 'sys.version' contains | |
316 | the string "GCC" (implying that this Python was built with GCC), or the | |
317 | installed "pyconfig.h" contains the string "__GNUC__". | |
318 | """ | |
319 | ||
320 | # XXX since this function also checks sys.version, it's not strictly a | |
321 | # "pyconfig.h" check -- should probably be renamed... | |
322 | ||
323 | from distutils import sysconfig | |
324 | import string | |
325 | # if sys.version contains GCC then python was compiled with | |
326 | # GCC, and the pyconfig.h file should be OK | |
327 | if string.find(sys.version,"GCC") >= 0: | |
328 | return (CONFIG_H_OK, "sys.version mentions 'GCC'") | |
329 | ||
330 | fn = sysconfig.get_config_h_filename() | |
331 | try: | |
332 | # It would probably better to read single lines to search. | |
333 | # But we do this only once, and it is fast enough | |
334 | f = open(fn) | |
335 | s = f.read() | |
336 | f.close() | |
337 | ||
338 | except IOError, exc: | |
339 | # if we can't read this file, we cannot say it is wrong | |
340 | # the compiler will complain later about this file as missing | |
341 | return (CONFIG_H_UNCERTAIN, | |
342 | "couldn't read '%s': %s" % (fn, exc.strerror)) | |
343 | ||
344 | else: | |
345 | # "pyconfig.h" contains an "#ifdef __GNUC__" or something similar | |
346 | if string.find(s,"__GNUC__") >= 0: | |
347 | return (CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn) | |
348 | else: | |
349 | return (CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn) | |
350 | ||
351 | ||
352 | ||
353 | def get_versions(): | |
354 | """ Try to find out the versions of gcc, ld and dllwrap. | |
355 | If not possible it returns None for it. | |
356 | """ | |
357 | from distutils.version import StrictVersion | |
358 | from distutils.spawn import find_executable | |
359 | import re | |
360 | ||
361 | gcc_exe = find_executable('gcc') | |
362 | if gcc_exe: | |
363 | out = os.popen(gcc_exe + ' -dumpversion','r') | |
364 | out_string = out.read() | |
365 | out.close() | |
366 | result = re.search('(\d+\.\d+\.\d+)',out_string) | |
367 | if result: | |
368 | gcc_version = StrictVersion(result.group(1)) | |
369 | else: | |
370 | gcc_version = None | |
371 | else: | |
372 | gcc_version = None | |
373 | ld_exe = find_executable('ld') | |
374 | if ld_exe: | |
375 | out = os.popen(ld_exe + ' -v','r') | |
376 | out_string = out.read() | |
377 | out.close() | |
378 | result = re.search('(\d+\.\d+\.\d+)',out_string) | |
379 | if result: | |
380 | ld_version = StrictVersion(result.group(1)) | |
381 | else: | |
382 | ld_version = None | |
383 | else: | |
384 | ld_version = None | |
385 | dllwrap_exe = find_executable('dllwrap') | |
386 | if dllwrap_exe: | |
387 | out = os.popen(dllwrap_exe + ' --version','r') | |
388 | out_string = out.read() | |
389 | out.close() | |
390 | result = re.search(' (\d+\.\d+\.\d+)',out_string) | |
391 | if result: | |
392 | dllwrap_version = StrictVersion(result.group(1)) | |
393 | else: | |
394 | dllwrap_version = None | |
395 | else: | |
396 | dllwrap_version = None | |
397 | return (gcc_version, ld_version, dllwrap_version) |