blob: 794dcdb59c8686bb394649da86701923350ff4bb [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# *** only the version of June 2000 shows these problems
Greg Wardbf5c7092000-08-02 01:31:56 +000037
Andrew M. Kuchlingd448f662002-11-19 13:12:28 +000038# This module should be kept compatible with Python 1.5.2.
39
Greg Ward7c6395a2000-06-21 03:33:03 +000040__revision__ = "$Id$"
41
Greg Wardb1dceae2000-08-13 00:43:56 +000042import os,sys,copy
Greg Ward42406482000-09-27 02:08:14 +000043from distutils.ccompiler import gen_preprocess_options, gen_lib_options
Greg Ward7c6395a2000-06-21 03:33:03 +000044from distutils.unixccompiler import UnixCCompiler
Greg Wardbf5c7092000-08-02 01:31:56 +000045from distutils.file_util import write_file
Greg Ward42406482000-09-27 02:08:14 +000046from distutils.errors import DistutilsExecError, CompileError, UnknownFileError
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000047from distutils import log
Greg Ward7c6395a2000-06-21 03:33:03 +000048
Greg Ward7c6395a2000-06-21 03:33:03 +000049class CygwinCCompiler (UnixCCompiler):
50
51 compiler_type = 'cygwin'
Greg Wardb1dceae2000-08-13 00:43:56 +000052 obj_extension = ".o"
53 static_lib_extension = ".a"
54 shared_lib_extension = ".dll"
55 static_lib_format = "lib%s%s"
56 shared_lib_format = "%s%s"
57 exe_extension = ".exe"
Fred Drakeb94b8492001-12-06 20:51:35 +000058
Jeremy Hylton1bba31d2002-06-13 17:28:18 +000059 def __init__ (self, verbose=0, dry_run=0, force=0):
Greg Ward7c6395a2000-06-21 03:33:03 +000060
61 UnixCCompiler.__init__ (self, verbose, dry_run, force)
62
Greg Warde8e9d112000-08-13 01:18:55 +000063 (status, details) = check_config_h()
64 self.debug_print("Python's GCC status: %s (details: %s)" %
65 (status, details))
66 if status is not CONFIG_H_OK:
Greg Wardbf5c7092000-08-02 01:31:56 +000067 self.warn(
Jeremy Hylton1bba31d2002-06-13 17:28:18 +000068 "Python's pyconfig.h doesn't seem to support your compiler. "
69 "Reason: %s. "
70 "Compiling may fail because of undefined preprocessor macros."
71 % details)
Fred Drakeb94b8492001-12-06 20:51:35 +000072
Jeremy Hylton1bba31d2002-06-13 17:28:18 +000073 self.gcc_version, self.ld_version, self.dllwrap_version = \
Greg Wardbf5c7092000-08-02 01:31:56 +000074 get_versions()
Greg Wardb1dceae2000-08-13 00:43:56 +000075 self.debug_print(self.compiler_type + ": gcc %s, ld %s, dllwrap %s\n" %
Fred Drakeb94b8492001-12-06 20:51:35 +000076 (self.gcc_version,
77 self.ld_version,
Greg Wardbf5c7092000-08-02 01:31:56 +000078 self.dllwrap_version) )
79
Fred Drakeb94b8492001-12-06 20:51:35 +000080 # ld_version >= "2.10.90" should also be able to use
Greg Wardbf5c7092000-08-02 01:31:56 +000081 # gcc -mdll instead of dllwrap
Fred Drakeb94b8492001-12-06 20:51:35 +000082 # Older dllwraps had own version numbers, newer ones use the
Greg Wardbf5c7092000-08-02 01:31:56 +000083 # same as the rest of binutils ( also ld )
84 # dllwrap 2.10.90 is buggy
Fred Drakeb94b8492001-12-06 20:51:35 +000085 if self.ld_version >= "2.10.90":
Greg Ward42406482000-09-27 02:08:14 +000086 self.linker_dll = "gcc"
Greg Wardbf5c7092000-08-02 01:31:56 +000087 else:
Greg Ward42406482000-09-27 02:08:14 +000088 self.linker_dll = "dllwrap"
Greg Wardbf5c7092000-08-02 01:31:56 +000089
Greg Wardf34506a2000-06-29 22:57:55 +000090 # Hard-code GCC because that's what this is all about.
91 # XXX optimization, warnings etc. should be customizable.
Greg Wardbf5c7092000-08-02 01:31:56 +000092 self.set_executables(compiler='gcc -mcygwin -O -Wall',
93 compiler_so='gcc -mcygwin -mdll -O -Wall',
94 linker_exe='gcc -mcygwin',
Jason Tishler70367d32003-04-09 16:03:57 +000095 linker_so=('%s -mcygwin -mdll' %
Greg Ward42406482000-09-27 02:08:14 +000096 self.linker_dll))
Greg Ward7c6395a2000-06-21 03:33:03 +000097
Fred Drakeb94b8492001-12-06 20:51:35 +000098 # cygwin and mingw32 need different sets of libraries
Greg Wardbf5c7092000-08-02 01:31:56 +000099 if self.gcc_version == "2.91.57":
100 # cygwin shouldn't need msvcrt, but without the dlls will crash
101 # (gcc version 2.91.57) -- perhaps something about initialization
102 self.dll_libraries=["msvcrt"]
Fred Drakeb94b8492001-12-06 20:51:35 +0000103 self.warn(
Greg Wardbf5c7092000-08-02 01:31:56 +0000104 "Consider upgrading to a newer version of gcc")
105 else:
106 self.dll_libraries=[]
Fred Drakeb94b8492001-12-06 20:51:35 +0000107
Greg Ward7c6395a2000-06-21 03:33:03 +0000108 # __init__ ()
109
Greg Ward42406482000-09-27 02:08:14 +0000110
Jeremy Hylton1b046e42002-06-18 18:48:55 +0000111 def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
112 if ext == '.rc' or ext == '.res':
113 # gcc needs '.res' and '.rc' compiled to object files !!!
114 try:
115 self.spawn(["windres", "-i", src, "-o", obj])
116 except DistutilsExecError, msg:
117 raise CompileError, msg
118 else: # for other files use the C-compiler
119 try:
120 self.spawn(self.compiler_so + cc_args + [src, '-o', obj] +
121 extra_postargs)
122 except DistutilsExecError, msg:
123 raise CompileError, msg
Greg Ward42406482000-09-27 02:08:14 +0000124
Greg Ward42406482000-09-27 02:08:14 +0000125 def link (self,
126 target_desc,
127 objects,
128 output_filename,
129 output_dir=None,
130 libraries=None,
131 library_dirs=None,
132 runtime_library_dirs=None,
133 export_symbols=None,
134 debug=0,
135 extra_preargs=None,
136 extra_postargs=None,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000137 build_temp=None,
138 target_lang=None):
Fred Drakeb94b8492001-12-06 20:51:35 +0000139
Greg Wardb1dceae2000-08-13 00:43:56 +0000140 # use separate copies, so we can modify the lists
141 extra_preargs = copy.copy(extra_preargs or [])
142 libraries = copy.copy(libraries or [])
Greg Ward42406482000-09-27 02:08:14 +0000143 objects = copy.copy(objects or [])
Fred Drakeb94b8492001-12-06 20:51:35 +0000144
Greg Wardbf5c7092000-08-02 01:31:56 +0000145 # Additional libraries
Greg Wardf34506a2000-06-29 22:57:55 +0000146 libraries.extend(self.dll_libraries)
Greg Wardbf5c7092000-08-02 01:31:56 +0000147
Greg Ward42406482000-09-27 02:08:14 +0000148 # handle export symbols by creating a def-file
149 # with executables this only works with gcc/ld as linker
150 if ((export_symbols is not None) and
151 (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
152 # (The linker doesn't do anything if output is up-to-date.
153 # So it would probably better to check if we really need this,
Fred Drakeb94b8492001-12-06 20:51:35 +0000154 # but for this we had to insert some unchanged parts of
155 # UnixCCompiler, and this is not what we want.)
Greg Ward42406482000-09-27 02:08:14 +0000156
Fred Drakeb94b8492001-12-06 20:51:35 +0000157 # we want to put some files in the same directory as the
Greg Ward42406482000-09-27 02:08:14 +0000158 # object files are, build_temp doesn't help much
159 # where are the object files
160 temp_dir = os.path.dirname(objects[0])
161 # name of dll to give the helper files the same base name
162 (dll_name, dll_extension) = os.path.splitext(
163 os.path.basename(output_filename))
164
165 # generate the filenames for these files
Greg Wardbf5c7092000-08-02 01:31:56 +0000166 def_file = os.path.join(temp_dir, dll_name + ".def")
Greg Ward42406482000-09-27 02:08:14 +0000167 lib_file = os.path.join(temp_dir, 'lib' + dll_name + ".a")
Fred Drakeb94b8492001-12-06 20:51:35 +0000168
Greg Ward42406482000-09-27 02:08:14 +0000169 # Generate .def file
Greg Wardbf5c7092000-08-02 01:31:56 +0000170 contents = [
171 "LIBRARY %s" % os.path.basename(output_filename),
172 "EXPORTS"]
Greg Ward7c6395a2000-06-21 03:33:03 +0000173 for sym in export_symbols:
Greg Wardbf5c7092000-08-02 01:31:56 +0000174 contents.append(sym)
175 self.execute(write_file, (def_file, contents),
176 "writing %s" % def_file)
177
Greg Ward42406482000-09-27 02:08:14 +0000178 # next add options for def-file and to creating import libraries
179
180 # dllwrap uses different options than gcc/ld
181 if self.linker_dll == "dllwrap":
Jeremy Hyltona2f99892002-06-04 20:26:44 +0000182 extra_preargs.extend(["--output-lib", lib_file])
Greg Wardbf5c7092000-08-02 01:31:56 +0000183 # for dllwrap we have to use a special option
Greg Ward42406482000-09-27 02:08:14 +0000184 extra_preargs.extend(["--def", def_file])
185 # we use gcc/ld here and can be sure ld is >= 2.9.10
186 else:
187 # doesn't work: bfd_close build\...\libfoo.a: Invalid operation
188 #extra_preargs.extend(["-Wl,--out-implib,%s" % lib_file])
Jeremy Hyltona2f99892002-06-04 20:26:44 +0000189 # for gcc/ld the def-file is specified as any object files
Greg Ward42406482000-09-27 02:08:14 +0000190 objects.append(def_file)
191
192 #end: if ((export_symbols is not None) and
Fred Drake132dce22000-12-12 23:11:42 +0000193 # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
Fred Drakeb94b8492001-12-06 20:51:35 +0000194
Greg Wardf34506a2000-06-29 22:57:55 +0000195 # who wants symbols and a many times larger output file
Fred Drakeb94b8492001-12-06 20:51:35 +0000196 # should explicitly switch the debug mode on
Greg Wardbf5c7092000-08-02 01:31:56 +0000197 # otherwise we let dllwrap/ld strip the output file
Fred Drakeb94b8492001-12-06 20:51:35 +0000198 # (On my machine: 10KB < stripped_file < ??100KB
Greg Ward42406482000-09-27 02:08:14 +0000199 # unstripped_file = stripped_file + XXX KB
Fred Drakeb94b8492001-12-06 20:51:35 +0000200 # ( XXX=254 for a typical python extension))
201 if not debug:
202 extra_preargs.append("-s")
203
Greg Ward42406482000-09-27 02:08:14 +0000204 UnixCCompiler.link(self,
205 target_desc,
206 objects,
207 output_filename,
208 output_dir,
209 libraries,
210 library_dirs,
211 runtime_library_dirs,
212 None, # export_symbols, we do this in our def-file
213 debug,
214 extra_preargs,
215 extra_postargs,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000216 build_temp,
217 target_lang)
Fred Drakeb94b8492001-12-06 20:51:35 +0000218
Greg Ward42406482000-09-27 02:08:14 +0000219 # link ()
220
221 # -- Miscellaneous methods -----------------------------------------
222
223 # overwrite the one from CCompiler to support rc and res-files
224 def object_filenames (self,
225 source_filenames,
226 strip_dir=0,
227 output_dir=''):
228 if output_dir is None: output_dir = ''
229 obj_names = []
230 for src_name in source_filenames:
231 # use normcase to make sure '.rc' is really '.rc' and not '.RC'
232 (base, ext) = os.path.splitext (os.path.normcase(src_name))
233 if ext not in (self.src_extensions + ['.rc','.res']):
234 raise UnknownFileError, \
235 "unknown file type '%s' (from '%s')" % \
236 (ext, src_name)
237 if strip_dir:
238 base = os.path.basename (base)
239 if ext == '.res' or ext == '.rc':
240 # these need to be compiled to object files
Fred Drakeb94b8492001-12-06 20:51:35 +0000241 obj_names.append (os.path.join (output_dir,
Greg Ward42406482000-09-27 02:08:14 +0000242 base + ext + self.obj_extension))
243 else:
244 obj_names.append (os.path.join (output_dir,
245 base + self.obj_extension))
246 return obj_names
247
248 # object_filenames ()
Greg Ward7c6395a2000-06-21 03:33:03 +0000249
250# class CygwinCCompiler
251
Greg Wardf34506a2000-06-29 22:57:55 +0000252
Greg Ward7c6395a2000-06-21 03:33:03 +0000253# the same as cygwin plus some additional parameters
254class Mingw32CCompiler (CygwinCCompiler):
255
256 compiler_type = 'mingw32'
257
258 def __init__ (self,
259 verbose=0,
260 dry_run=0,
261 force=0):
262
263 CygwinCCompiler.__init__ (self, verbose, dry_run, force)
Fred Drakeb94b8492001-12-06 20:51:35 +0000264
Greg Wardbf5c7092000-08-02 01:31:56 +0000265 # A real mingw32 doesn't need to specify a different entry point,
266 # but cygwin 2.91.57 in no-cygwin-mode needs it.
267 if self.gcc_version <= "2.91.57":
268 entry_point = '--entry _DllMain@12'
269 else:
270 entry_point = ''
Greg Ward7c6395a2000-06-21 03:33:03 +0000271
Greg Wardf34506a2000-06-29 22:57:55 +0000272 self.set_executables(compiler='gcc -mno-cygwin -O -Wall',
Greg Wardbf5c7092000-08-02 01:31:56 +0000273 compiler_so='gcc -mno-cygwin -mdll -O -Wall',
Greg Wardf34506a2000-06-29 22:57:55 +0000274 linker_exe='gcc -mno-cygwin',
Jason Tishler70367d32003-04-09 16:03:57 +0000275 linker_so='%s -mno-cygwin -mdll %s'
Greg Ward42406482000-09-27 02:08:14 +0000276 % (self.linker_dll, entry_point))
Greg Wardbf5c7092000-08-02 01:31:56 +0000277 # Maybe we should also append -mthreads, but then the finished
278 # dlls need another dll (mingwm10.dll see Mingw32 docs)
Fred Drakeb94b8492001-12-06 20:51:35 +0000279 # (-mthreads: Support thread-safe exception handling on `Mingw32')
280
281 # no additional libraries needed
Greg Wardbf5c7092000-08-02 01:31:56 +0000282 self.dll_libraries=[]
Fred Drakeb94b8492001-12-06 20:51:35 +0000283
Greg Ward7c6395a2000-06-21 03:33:03 +0000284 # __init__ ()
Greg Wardbf5c7092000-08-02 01:31:56 +0000285
Greg Ward7c6395a2000-06-21 03:33:03 +0000286# class Mingw32CCompiler
Greg Wardbf5c7092000-08-02 01:31:56 +0000287
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000288# Because these compilers aren't configured in Python's pyconfig.h file by
Greg Wardbf5c7092000-08-02 01:31:56 +0000289# default, we should at least warn the user if he is using a unmodified
290# version.
291
Greg Warde8e9d112000-08-13 01:18:55 +0000292CONFIG_H_OK = "ok"
293CONFIG_H_NOTOK = "not ok"
294CONFIG_H_UNCERTAIN = "uncertain"
295
Greg Wardbf5c7092000-08-02 01:31:56 +0000296def check_config_h():
Greg Warde8e9d112000-08-13 01:18:55 +0000297
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000298 """Check if the current Python installation (specifically, pyconfig.h)
Greg Warde8e9d112000-08-13 01:18:55 +0000299 appears amenable to building extensions with GCC. Returns a tuple
300 (status, details), where 'status' is one of the following constants:
301 CONFIG_H_OK
302 all is well, go ahead and compile
303 CONFIG_H_NOTOK
304 doesn't look good
305 CONFIG_H_UNCERTAIN
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000306 not sure -- unable to read pyconfig.h
Greg Warde8e9d112000-08-13 01:18:55 +0000307 'details' is a human-readable string explaining the situation.
308
309 Note there are two ways to conclude "OK": either 'sys.version' contains
310 the string "GCC" (implying that this Python was built with GCC), or the
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000311 installed "pyconfig.h" contains the string "__GNUC__".
Greg Wardbf5c7092000-08-02 01:31:56 +0000312 """
Greg Warde8e9d112000-08-13 01:18:55 +0000313
314 # XXX since this function also checks sys.version, it's not strictly a
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000315 # "pyconfig.h" check -- should probably be renamed...
Greg Wardbf5c7092000-08-02 01:31:56 +0000316
317 from distutils import sysconfig
Andrew M. Kuchling6e9c0ba2001-03-22 03:50:09 +0000318 import string
Greg Wardbf5c7092000-08-02 01:31:56 +0000319 # if sys.version contains GCC then python was compiled with
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000320 # GCC, and the pyconfig.h file should be OK
Andrew M. Kuchlingac20f772001-03-22 03:48:31 +0000321 if string.find(sys.version,"GCC") >= 0:
Greg Warde8e9d112000-08-13 01:18:55 +0000322 return (CONFIG_H_OK, "sys.version mentions 'GCC'")
Fred Drakeb94b8492001-12-06 20:51:35 +0000323
Greg Warde8e9d112000-08-13 01:18:55 +0000324 fn = sysconfig.get_config_h_filename()
Greg Wardbf5c7092000-08-02 01:31:56 +0000325 try:
326 # It would probably better to read single lines to search.
Fred Drakeb94b8492001-12-06 20:51:35 +0000327 # But we do this only once, and it is fast enough
Greg Warde8e9d112000-08-13 01:18:55 +0000328 f = open(fn)
329 s = f.read()
Greg Wardbf5c7092000-08-02 01:31:56 +0000330 f.close()
Fred Drakeb94b8492001-12-06 20:51:35 +0000331
Greg Warde8e9d112000-08-13 01:18:55 +0000332 except IOError, exc:
Greg Wardbf5c7092000-08-02 01:31:56 +0000333 # if we can't read this file, we cannot say it is wrong
334 # the compiler will complain later about this file as missing
Greg Warde8e9d112000-08-13 01:18:55 +0000335 return (CONFIG_H_UNCERTAIN,
336 "couldn't read '%s': %s" % (fn, exc.strerror))
337
338 else:
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000339 # "pyconfig.h" contains an "#ifdef __GNUC__" or something similar
Andrew M. Kuchlingac20f772001-03-22 03:48:31 +0000340 if string.find(s,"__GNUC__") >= 0:
Greg Warde8e9d112000-08-13 01:18:55 +0000341 return (CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn)
342 else:
343 return (CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn)
344
345
Greg Wardbf5c7092000-08-02 01:31:56 +0000346
347def get_versions():
348 """ Try to find out the versions of gcc, ld and dllwrap.
349 If not possible it returns None for it.
350 """
351 from distutils.version import StrictVersion
352 from distutils.spawn import find_executable
353 import re
Fred Drakeb94b8492001-12-06 20:51:35 +0000354
Greg Wardbf5c7092000-08-02 01:31:56 +0000355 gcc_exe = find_executable('gcc')
356 if gcc_exe:
357 out = os.popen(gcc_exe + ' -dumpversion','r')
358 out_string = out.read()
359 out.close()
Jason Tishlerdcae0dc2003-04-09 20:13:59 +0000360 result = re.search('(\d+\.\d+(\.\d+)*)',out_string)
Greg Wardbf5c7092000-08-02 01:31:56 +0000361 if result:
362 gcc_version = StrictVersion(result.group(1))
363 else:
364 gcc_version = None
365 else:
366 gcc_version = None
367 ld_exe = find_executable('ld')
368 if ld_exe:
369 out = os.popen(ld_exe + ' -v','r')
370 out_string = out.read()
371 out.close()
Jason Tishlerdcae0dc2003-04-09 20:13:59 +0000372 result = re.search('(\d+\.\d+(\.\d+)*)',out_string)
Greg Wardbf5c7092000-08-02 01:31:56 +0000373 if result:
374 ld_version = StrictVersion(result.group(1))
375 else:
376 ld_version = None
377 else:
378 ld_version = None
379 dllwrap_exe = find_executable('dllwrap')
380 if dllwrap_exe:
381 out = os.popen(dllwrap_exe + ' --version','r')
382 out_string = out.read()
383 out.close()
Jason Tishlerdcae0dc2003-04-09 20:13:59 +0000384 result = re.search(' (\d+\.\d+(\.\d+)*)',out_string)
Greg Wardbf5c7092000-08-02 01:31:56 +0000385 if result:
386 dllwrap_version = StrictVersion(result.group(1))
387 else:
388 dllwrap_version = None
389 else:
390 dllwrap_version = None
391 return (gcc_version, ld_version, dllwrap_version)