blob: 9aabd8a66b19d0f855d126bf4c51ec93b810961b [file] [log] [blame]
Greg Ward7c6395a2000-06-21 03:33:03 +00001"""distutils.cygwinccompiler
2
Greg Wardf34506a2000-06-29 22:57:55 +00003Provides the CygwinCCompiler class, a subclass of UnixCCompiler that
4handles the Cygwin port of the GNU C compiler to Windows. It also contains
5the Mingw32CCompiler class which handles the mingw32 port of GCC (same as
6cygwin in no-cygwin mode).
Greg Ward7c6395a2000-06-21 03:33:03 +00007"""
8
Greg Wardbf5c7092000-08-02 01:31:56 +00009# 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#
Fred Drakeb94b8492001-12-06 20:51:35 +000021# * We put export_symbols in a def-file, and don't use
Greg Wardbf5c7092000-08-02 01:31:56 +000022# --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:
Fred Drakeb94b8492001-12-06 20:51:35 +000027#
28# * cygwin gcc 2.91.57/ld 2.9.4/dllwrap 0.2.4 works
Greg Wardbf5c7092000-08-02 01:31:56 +000029# (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
Fred Drakeb94b8492001-12-06 20:51:35 +000031# * mingw32 gcc 2.95.2/ld 2.9.4/dllwrap 0.2.4 works
32# (ld doesn't support -shared, so we use dllwrap)
Greg Wardbf5c7092000-08-02 01:31:56 +000033# * 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
Greg Ward7483d682000-09-01 01:24:31 +000035# see also http://sources.redhat.com/ml/cygwin/2000-06/msg01274.html
Fred Drakeb94b8492001-12-06 20:51:35 +000036# - using gcc -mdll instead dllwrap doesn't work without -static because
Greg Wardbf5c7092000-08-02 01:31:56 +000037# it tries to link against dlls instead their import libraries. (If
38# it finds the dll first.)
Fred Drakeb94b8492001-12-06 20:51:35 +000039# 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
Greg Wardbf5c7092000-08-02 01:31:56 +000041# in the dlls.
Fred Drakeb94b8492001-12-06 20:51:35 +000042# *** only the version of June 2000 shows these problems
Greg Wardbf5c7092000-08-02 01:31:56 +000043
Greg Ward7c6395a2000-06-21 03:33:03 +000044# created 2000/05/05, Rene Liebscher
45
46__revision__ = "$Id$"
47
Greg Wardb1dceae2000-08-13 00:43:56 +000048import os,sys,copy
Greg Ward42406482000-09-27 02:08:14 +000049from distutils.ccompiler import gen_preprocess_options, gen_lib_options
Greg Ward7c6395a2000-06-21 03:33:03 +000050from distutils.unixccompiler import UnixCCompiler
Greg Wardbf5c7092000-08-02 01:31:56 +000051from distutils.file_util import write_file
Greg Ward42406482000-09-27 02:08:14 +000052from distutils.errors import DistutilsExecError, CompileError, UnknownFileError
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000053from distutils import log
Greg Ward7c6395a2000-06-21 03:33:03 +000054
Greg Ward7c6395a2000-06-21 03:33:03 +000055class CygwinCCompiler (UnixCCompiler):
56
57 compiler_type = 'cygwin'
Greg Wardb1dceae2000-08-13 00:43:56 +000058 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"
Fred Drakeb94b8492001-12-06 20:51:35 +000064
Jeremy Hylton1bba31d2002-06-13 17:28:18 +000065 def __init__ (self, verbose=0, dry_run=0, force=0):
Greg Ward7c6395a2000-06-21 03:33:03 +000066
67 UnixCCompiler.__init__ (self, verbose, dry_run, force)
68
Greg Warde8e9d112000-08-13 01:18:55 +000069 (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:
Greg Wardbf5c7092000-08-02 01:31:56 +000073 self.warn(
Jeremy Hylton1bba31d2002-06-13 17:28:18 +000074 "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)
Fred Drakeb94b8492001-12-06 20:51:35 +000078
Jeremy Hylton1bba31d2002-06-13 17:28:18 +000079 self.gcc_version, self.ld_version, self.dllwrap_version = \
Greg Wardbf5c7092000-08-02 01:31:56 +000080 get_versions()
Greg Wardb1dceae2000-08-13 00:43:56 +000081 self.debug_print(self.compiler_type + ": gcc %s, ld %s, dllwrap %s\n" %
Fred Drakeb94b8492001-12-06 20:51:35 +000082 (self.gcc_version,
83 self.ld_version,
Greg Wardbf5c7092000-08-02 01:31:56 +000084 self.dllwrap_version) )
85
Fred Drakeb94b8492001-12-06 20:51:35 +000086 # ld_version >= "2.10.90" should also be able to use
Greg Wardbf5c7092000-08-02 01:31:56 +000087 # gcc -mdll instead of dllwrap
Fred Drakeb94b8492001-12-06 20:51:35 +000088 # Older dllwraps had own version numbers, newer ones use the
Greg Wardbf5c7092000-08-02 01:31:56 +000089 # same as the rest of binutils ( also ld )
90 # dllwrap 2.10.90 is buggy
Fred Drakeb94b8492001-12-06 20:51:35 +000091 if self.ld_version >= "2.10.90":
Greg Ward42406482000-09-27 02:08:14 +000092 self.linker_dll = "gcc"
Greg Wardbf5c7092000-08-02 01:31:56 +000093 else:
Greg Ward42406482000-09-27 02:08:14 +000094 self.linker_dll = "dllwrap"
Greg Wardbf5c7092000-08-02 01:31:56 +000095
Greg Wardf34506a2000-06-29 22:57:55 +000096 # Hard-code GCC because that's what this is all about.
97 # XXX optimization, warnings etc. should be customizable.
Greg Wardbf5c7092000-08-02 01:31:56 +000098 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' %
Greg Ward42406482000-09-27 02:08:14 +0000102 self.linker_dll))
Greg Ward7c6395a2000-06-21 03:33:03 +0000103
Fred Drakeb94b8492001-12-06 20:51:35 +0000104 # cygwin and mingw32 need different sets of libraries
Greg Wardbf5c7092000-08-02 01:31:56 +0000105 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"]
Fred Drakeb94b8492001-12-06 20:51:35 +0000109 self.warn(
Greg Wardbf5c7092000-08-02 01:31:56 +0000110 "Consider upgrading to a newer version of gcc")
111 else:
112 self.dll_libraries=[]
Fred Drakeb94b8492001-12-06 20:51:35 +0000113
Greg Ward7c6395a2000-06-21 03:33:03 +0000114 # __init__ ()
115
Greg Ward42406482000-09-27 02:08:14 +0000116
Jeremy Hylton1b046e42002-06-18 18:48:55 +0000117 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
Greg Ward42406482000-09-27 02:08:14 +0000130
Greg Ward42406482000-09-27 02:08:14 +0000131 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):
Fred Drakeb94b8492001-12-06 20:51:35 +0000144
Greg Wardb1dceae2000-08-13 00:43:56 +0000145 # use separate copies, so we can modify the lists
146 extra_preargs = copy.copy(extra_preargs or [])
147 libraries = copy.copy(libraries or [])
Greg Ward42406482000-09-27 02:08:14 +0000148 objects = copy.copy(objects or [])
Fred Drakeb94b8492001-12-06 20:51:35 +0000149
Greg Wardbf5c7092000-08-02 01:31:56 +0000150 # Additional libraries
Greg Wardf34506a2000-06-29 22:57:55 +0000151 libraries.extend(self.dll_libraries)
Greg Wardbf5c7092000-08-02 01:31:56 +0000152
Greg Ward42406482000-09-27 02:08:14 +0000153 # handle export symbols by creating a def-file
154 # with executables this only works with gcc/ld as linker
155 if ((export_symbols is not None) and
156 (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
157 # (The linker doesn't do anything if output is up-to-date.
158 # So it would probably better to check if we really need this,
Fred Drakeb94b8492001-12-06 20:51:35 +0000159 # but for this we had to insert some unchanged parts of
160 # UnixCCompiler, and this is not what we want.)
Greg Ward42406482000-09-27 02:08:14 +0000161
Fred Drakeb94b8492001-12-06 20:51:35 +0000162 # we want to put some files in the same directory as the
Greg Ward42406482000-09-27 02:08:14 +0000163 # object files are, build_temp doesn't help much
164 # where are the object files
165 temp_dir = os.path.dirname(objects[0])
166 # name of dll to give the helper files the same base name
167 (dll_name, dll_extension) = os.path.splitext(
168 os.path.basename(output_filename))
169
170 # generate the filenames for these files
Greg Wardbf5c7092000-08-02 01:31:56 +0000171 def_file = os.path.join(temp_dir, dll_name + ".def")
Greg Ward42406482000-09-27 02:08:14 +0000172 lib_file = os.path.join(temp_dir, 'lib' + dll_name + ".a")
Fred Drakeb94b8492001-12-06 20:51:35 +0000173
Greg Ward42406482000-09-27 02:08:14 +0000174 # Generate .def file
Greg Wardbf5c7092000-08-02 01:31:56 +0000175 contents = [
176 "LIBRARY %s" % os.path.basename(output_filename),
177 "EXPORTS"]
Greg Ward7c6395a2000-06-21 03:33:03 +0000178 for sym in export_symbols:
Greg Wardbf5c7092000-08-02 01:31:56 +0000179 contents.append(sym)
180 self.execute(write_file, (def_file, contents),
181 "writing %s" % def_file)
182
Greg Ward42406482000-09-27 02:08:14 +0000183 # next add options for def-file and to creating import libraries
184
185 # dllwrap uses different options than gcc/ld
186 if self.linker_dll == "dllwrap":
Jeremy Hyltona2f99892002-06-04 20:26:44 +0000187 extra_preargs.extend(["--output-lib", lib_file])
Greg Wardbf5c7092000-08-02 01:31:56 +0000188 # for dllwrap we have to use a special option
Greg Ward42406482000-09-27 02:08:14 +0000189 extra_preargs.extend(["--def", def_file])
190 # we use gcc/ld here and can be sure ld is >= 2.9.10
191 else:
192 # doesn't work: bfd_close build\...\libfoo.a: Invalid operation
193 #extra_preargs.extend(["-Wl,--out-implib,%s" % lib_file])
Jeremy Hyltona2f99892002-06-04 20:26:44 +0000194 # for gcc/ld the def-file is specified as any object files
Greg Ward42406482000-09-27 02:08:14 +0000195 objects.append(def_file)
196
197 #end: if ((export_symbols is not None) and
Fred Drake132dce22000-12-12 23:11:42 +0000198 # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
Fred Drakeb94b8492001-12-06 20:51:35 +0000199
Greg Wardf34506a2000-06-29 22:57:55 +0000200 # who wants symbols and a many times larger output file
Fred Drakeb94b8492001-12-06 20:51:35 +0000201 # should explicitly switch the debug mode on
Greg Wardbf5c7092000-08-02 01:31:56 +0000202 # otherwise we let dllwrap/ld strip the output file
Fred Drakeb94b8492001-12-06 20:51:35 +0000203 # (On my machine: 10KB < stripped_file < ??100KB
Greg Ward42406482000-09-27 02:08:14 +0000204 # unstripped_file = stripped_file + XXX KB
Fred Drakeb94b8492001-12-06 20:51:35 +0000205 # ( XXX=254 for a typical python extension))
206 if not debug:
207 extra_preargs.append("-s")
208
Greg Ward42406482000-09-27 02:08:14 +0000209 UnixCCompiler.link(self,
210 target_desc,
211 objects,
212 output_filename,
213 output_dir,
214 libraries,
215 library_dirs,
216 runtime_library_dirs,
217 None, # export_symbols, we do this in our def-file
218 debug,
219 extra_preargs,
220 extra_postargs,
221 build_temp)
Fred Drakeb94b8492001-12-06 20:51:35 +0000222
Greg Ward42406482000-09-27 02:08:14 +0000223 # link ()
224
225 # -- Miscellaneous methods -----------------------------------------
226
227 # overwrite the one from CCompiler to support rc and res-files
228 def object_filenames (self,
229 source_filenames,
230 strip_dir=0,
231 output_dir=''):
232 if output_dir is None: output_dir = ''
233 obj_names = []
234 for src_name in source_filenames:
235 # use normcase to make sure '.rc' is really '.rc' and not '.RC'
236 (base, ext) = os.path.splitext (os.path.normcase(src_name))
237 if ext not in (self.src_extensions + ['.rc','.res']):
238 raise UnknownFileError, \
239 "unknown file type '%s' (from '%s')" % \
240 (ext, src_name)
241 if strip_dir:
242 base = os.path.basename (base)
243 if ext == '.res' or ext == '.rc':
244 # these need to be compiled to object files
Fred Drakeb94b8492001-12-06 20:51:35 +0000245 obj_names.append (os.path.join (output_dir,
Greg Ward42406482000-09-27 02:08:14 +0000246 base + ext + self.obj_extension))
247 else:
248 obj_names.append (os.path.join (output_dir,
249 base + self.obj_extension))
250 return obj_names
251
252 # object_filenames ()
Greg Ward7c6395a2000-06-21 03:33:03 +0000253
254# class CygwinCCompiler
255
Greg Wardf34506a2000-06-29 22:57:55 +0000256
Greg Ward7c6395a2000-06-21 03:33:03 +0000257# the same as cygwin plus some additional parameters
258class Mingw32CCompiler (CygwinCCompiler):
259
260 compiler_type = 'mingw32'
261
262 def __init__ (self,
263 verbose=0,
264 dry_run=0,
265 force=0):
266
267 CygwinCCompiler.__init__ (self, verbose, dry_run, force)
Fred Drakeb94b8492001-12-06 20:51:35 +0000268
Greg Wardbf5c7092000-08-02 01:31:56 +0000269 # A real mingw32 doesn't need to specify a different entry point,
270 # but cygwin 2.91.57 in no-cygwin-mode needs it.
271 if self.gcc_version <= "2.91.57":
272 entry_point = '--entry _DllMain@12'
273 else:
274 entry_point = ''
Greg Ward7c6395a2000-06-21 03:33:03 +0000275
Greg Wardf34506a2000-06-29 22:57:55 +0000276 self.set_executables(compiler='gcc -mno-cygwin -O -Wall',
Greg Wardbf5c7092000-08-02 01:31:56 +0000277 compiler_so='gcc -mno-cygwin -mdll -O -Wall',
Greg Wardf34506a2000-06-29 22:57:55 +0000278 linker_exe='gcc -mno-cygwin',
Fred Drakeb94b8492001-12-06 20:51:35 +0000279 linker_so='%s -mno-cygwin -mdll -static %s'
Greg Ward42406482000-09-27 02:08:14 +0000280 % (self.linker_dll, entry_point))
Greg Wardbf5c7092000-08-02 01:31:56 +0000281 # Maybe we should also append -mthreads, but then the finished
282 # dlls need another dll (mingwm10.dll see Mingw32 docs)
Fred Drakeb94b8492001-12-06 20:51:35 +0000283 # (-mthreads: Support thread-safe exception handling on `Mingw32')
284
285 # no additional libraries needed
Greg Wardbf5c7092000-08-02 01:31:56 +0000286 self.dll_libraries=[]
Fred Drakeb94b8492001-12-06 20:51:35 +0000287
Greg Ward7c6395a2000-06-21 03:33:03 +0000288 # __init__ ()
Greg Wardbf5c7092000-08-02 01:31:56 +0000289
Greg Ward7c6395a2000-06-21 03:33:03 +0000290# class Mingw32CCompiler
Greg Wardbf5c7092000-08-02 01:31:56 +0000291
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000292# Because these compilers aren't configured in Python's pyconfig.h file by
Greg Wardbf5c7092000-08-02 01:31:56 +0000293# default, we should at least warn the user if he is using a unmodified
294# version.
295
Greg Warde8e9d112000-08-13 01:18:55 +0000296CONFIG_H_OK = "ok"
297CONFIG_H_NOTOK = "not ok"
298CONFIG_H_UNCERTAIN = "uncertain"
299
Greg Wardbf5c7092000-08-02 01:31:56 +0000300def check_config_h():
Greg Warde8e9d112000-08-13 01:18:55 +0000301
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000302 """Check if the current Python installation (specifically, pyconfig.h)
Greg Warde8e9d112000-08-13 01:18:55 +0000303 appears amenable to building extensions with GCC. Returns a tuple
304 (status, details), where 'status' is one of the following constants:
305 CONFIG_H_OK
306 all is well, go ahead and compile
307 CONFIG_H_NOTOK
308 doesn't look good
309 CONFIG_H_UNCERTAIN
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000310 not sure -- unable to read pyconfig.h
Greg Warde8e9d112000-08-13 01:18:55 +0000311 'details' is a human-readable string explaining the situation.
312
313 Note there are two ways to conclude "OK": either 'sys.version' contains
314 the string "GCC" (implying that this Python was built with GCC), or the
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000315 installed "pyconfig.h" contains the string "__GNUC__".
Greg Wardbf5c7092000-08-02 01:31:56 +0000316 """
Greg Warde8e9d112000-08-13 01:18:55 +0000317
318 # XXX since this function also checks sys.version, it's not strictly a
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000319 # "pyconfig.h" check -- should probably be renamed...
Greg Wardbf5c7092000-08-02 01:31:56 +0000320
321 from distutils import sysconfig
Andrew M. Kuchling6e9c0ba2001-03-22 03:50:09 +0000322 import string
Greg Wardbf5c7092000-08-02 01:31:56 +0000323 # if sys.version contains GCC then python was compiled with
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000324 # GCC, and the pyconfig.h file should be OK
Andrew M. Kuchlingac20f772001-03-22 03:48:31 +0000325 if string.find(sys.version,"GCC") >= 0:
Greg Warde8e9d112000-08-13 01:18:55 +0000326 return (CONFIG_H_OK, "sys.version mentions 'GCC'")
Fred Drakeb94b8492001-12-06 20:51:35 +0000327
Greg Warde8e9d112000-08-13 01:18:55 +0000328 fn = sysconfig.get_config_h_filename()
Greg Wardbf5c7092000-08-02 01:31:56 +0000329 try:
330 # It would probably better to read single lines to search.
Fred Drakeb94b8492001-12-06 20:51:35 +0000331 # But we do this only once, and it is fast enough
Greg Warde8e9d112000-08-13 01:18:55 +0000332 f = open(fn)
333 s = f.read()
Greg Wardbf5c7092000-08-02 01:31:56 +0000334 f.close()
Fred Drakeb94b8492001-12-06 20:51:35 +0000335
Greg Warde8e9d112000-08-13 01:18:55 +0000336 except IOError, exc:
Greg Wardbf5c7092000-08-02 01:31:56 +0000337 # if we can't read this file, we cannot say it is wrong
338 # the compiler will complain later about this file as missing
Greg Warde8e9d112000-08-13 01:18:55 +0000339 return (CONFIG_H_UNCERTAIN,
340 "couldn't read '%s': %s" % (fn, exc.strerror))
341
342 else:
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000343 # "pyconfig.h" contains an "#ifdef __GNUC__" or something similar
Andrew M. Kuchlingac20f772001-03-22 03:48:31 +0000344 if string.find(s,"__GNUC__") >= 0:
Greg Warde8e9d112000-08-13 01:18:55 +0000345 return (CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn)
346 else:
347 return (CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn)
348
349
Greg Wardbf5c7092000-08-02 01:31:56 +0000350
351def get_versions():
352 """ Try to find out the versions of gcc, ld and dllwrap.
353 If not possible it returns None for it.
354 """
355 from distutils.version import StrictVersion
356 from distutils.spawn import find_executable
357 import re
Fred Drakeb94b8492001-12-06 20:51:35 +0000358
Greg Wardbf5c7092000-08-02 01:31:56 +0000359 gcc_exe = find_executable('gcc')
360 if gcc_exe:
361 out = os.popen(gcc_exe + ' -dumpversion','r')
362 out_string = out.read()
363 out.close()
364 result = re.search('(\d+\.\d+\.\d+)',out_string)
365 if result:
366 gcc_version = StrictVersion(result.group(1))
367 else:
368 gcc_version = None
369 else:
370 gcc_version = None
371 ld_exe = find_executable('ld')
372 if ld_exe:
373 out = os.popen(ld_exe + ' -v','r')
374 out_string = out.read()
375 out.close()
376 result = re.search('(\d+\.\d+\.\d+)',out_string)
377 if result:
378 ld_version = StrictVersion(result.group(1))
379 else:
380 ld_version = None
381 else:
382 ld_version = None
383 dllwrap_exe = find_executable('dllwrap')
384 if dllwrap_exe:
385 out = os.popen(dllwrap_exe + ' --version','r')
386 out_string = out.read()
387 out.close()
388 result = re.search(' (\d+\.\d+\.\d+)',out_string)
389 if result:
390 dllwrap_version = StrictVersion(result.group(1))
391 else:
392 dllwrap_version = None
393 else:
394 dllwrap_version = None
395 return (gcc_version, ld_version, dllwrap_version)