blob: 07e16655b05f9257ec66b00835bc7e458a481394 [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#
Greg Ward7483d682000-09-01 01:24:31 +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:
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
Greg Ward7483d682000-09-01 01:24:31 +000035# see also http://sources.redhat.com/ml/cygwin/2000-06/msg01274.html
Greg Wardbf5c7092000-08-02 01:31:56 +000036# - 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.
Greg Ward42406482000-09-27 02:08:14 +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
Greg Ward7c6395a2000-06-21 03:33:03 +000053
Greg Ward7c6395a2000-06-21 03:33:03 +000054class CygwinCCompiler (UnixCCompiler):
55
56 compiler_type = 'cygwin'
Greg Wardb1dceae2000-08-13 00:43:56 +000057 obj_extension = ".o"
58 static_lib_extension = ".a"
59 shared_lib_extension = ".dll"
60 static_lib_format = "lib%s%s"
61 shared_lib_format = "%s%s"
62 exe_extension = ".exe"
Greg Ward7c6395a2000-06-21 03:33:03 +000063
64 def __init__ (self,
65 verbose=0,
66 dry_run=0,
67 force=0):
68
69 UnixCCompiler.__init__ (self, verbose, dry_run, force)
70
Greg Warde8e9d112000-08-13 01:18:55 +000071 (status, details) = check_config_h()
72 self.debug_print("Python's GCC status: %s (details: %s)" %
73 (status, details))
74 if status is not CONFIG_H_OK:
Greg Wardbf5c7092000-08-02 01:31:56 +000075 self.warn(
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000076 "Python's pyconfig.h doesn't seem to support your compiler. " +
Greg Warde8e9d112000-08-13 01:18:55 +000077 ("Reason: %s." % details) +
Greg Wardbf5c7092000-08-02 01:31:56 +000078 "Compiling may fail because of undefined preprocessor macros.")
79
80 (self.gcc_version, self.ld_version, self.dllwrap_version) = \
81 get_versions()
Greg Wardb1dceae2000-08-13 00:43:56 +000082 self.debug_print(self.compiler_type + ": gcc %s, ld %s, dllwrap %s\n" %
Greg Wardbf5c7092000-08-02 01:31:56 +000083 (self.gcc_version,
84 self.ld_version,
85 self.dllwrap_version) )
86
87 # ld_version >= "2.10.90" should also be able to use
88 # gcc -mdll instead of dllwrap
89 # Older dllwraps had own version numbers, newer ones use the
90 # same as the rest of binutils ( also ld )
91 # dllwrap 2.10.90 is buggy
92 if self.ld_version >= "2.10.90":
Greg Ward42406482000-09-27 02:08:14 +000093 self.linker_dll = "gcc"
Greg Wardbf5c7092000-08-02 01:31:56 +000094 else:
Greg Ward42406482000-09-27 02:08:14 +000095 self.linker_dll = "dllwrap"
Greg Wardbf5c7092000-08-02 01:31:56 +000096
Greg Wardf34506a2000-06-29 22:57:55 +000097 # Hard-code GCC because that's what this is all about.
98 # XXX optimization, warnings etc. should be customizable.
Greg Wardbf5c7092000-08-02 01:31:56 +000099 self.set_executables(compiler='gcc -mcygwin -O -Wall',
100 compiler_so='gcc -mcygwin -mdll -O -Wall',
101 linker_exe='gcc -mcygwin',
102 linker_so=('%s -mcygwin -mdll -static' %
Greg Ward42406482000-09-27 02:08:14 +0000103 self.linker_dll))
Greg Ward7c6395a2000-06-21 03:33:03 +0000104
Greg Wardf34506a2000-06-29 22:57:55 +0000105 # cygwin and mingw32 need different sets of libraries
Greg Wardbf5c7092000-08-02 01:31:56 +0000106 if self.gcc_version == "2.91.57":
107 # cygwin shouldn't need msvcrt, but without the dlls will crash
108 # (gcc version 2.91.57) -- perhaps something about initialization
109 self.dll_libraries=["msvcrt"]
110 self.warn(
111 "Consider upgrading to a newer version of gcc")
112 else:
113 self.dll_libraries=[]
Greg Ward7c6395a2000-06-21 03:33:03 +0000114
115 # __init__ ()
116
Greg Ward42406482000-09-27 02:08:14 +0000117 # not much different of the compile method in UnixCCompiler,
118 # but we have to insert some lines in the middle of it, so
119 # we put here a adapted version of it.
120 # (If we would call compile() in the base class, it would do some
121 # initializations a second time, this is why all is done here.)
122 def compile (self,
123 sources,
124 output_dir=None,
125 macros=None,
126 include_dirs=None,
127 debug=0,
128 extra_preargs=None,
129 extra_postargs=None):
130
131 (output_dir, macros, include_dirs) = \
132 self._fix_compile_args (output_dir, macros, include_dirs)
133 (objects, skip_sources) = self._prep_compile (sources, output_dir)
134
135 # Figure out the options for the compiler command line.
136 pp_opts = gen_preprocess_options (macros, include_dirs)
137 cc_args = pp_opts + ['-c']
138 if debug:
139 cc_args[:0] = ['-g']
140 if extra_preargs:
141 cc_args[:0] = extra_preargs
142 if extra_postargs is None:
143 extra_postargs = []
144
145 # Compile all source files that weren't eliminated by
146 # '_prep_compile()'.
147 for i in range (len (sources)):
148 src = sources[i] ; obj = objects[i]
149 ext = (os.path.splitext (src))[1]
150 if skip_sources[src]:
151 self.announce ("skipping %s (%s up-to-date)" % (src, obj))
152 else:
153 self.mkpath (os.path.dirname (obj))
154 if ext == '.rc' or ext == '.res':
155 # gcc needs '.res' and '.rc' compiled to object files !!!
156 try:
157 self.spawn (["windres","-i",src,"-o",obj])
158 except DistutilsExecError, msg:
159 raise CompileError, msg
160 else: # for other files use the C-compiler
161 try:
162 self.spawn (self.compiler_so + cc_args +
163 [src, '-o', obj] +
164 extra_postargs)
165 except DistutilsExecError, msg:
166 raise CompileError, msg
167
168 # Return *all* object filenames, not just the ones we just built.
169 return objects
170
171 # compile ()
172
173
174 def link (self,
175 target_desc,
176 objects,
177 output_filename,
178 output_dir=None,
179 libraries=None,
180 library_dirs=None,
181 runtime_library_dirs=None,
182 export_symbols=None,
183 debug=0,
184 extra_preargs=None,
185 extra_postargs=None,
186 build_temp=None):
Greg Ward7c6395a2000-06-21 03:33:03 +0000187
Greg Wardb1dceae2000-08-13 00:43:56 +0000188 # use separate copies, so we can modify the lists
189 extra_preargs = copy.copy(extra_preargs or [])
190 libraries = copy.copy(libraries or [])
Greg Ward42406482000-09-27 02:08:14 +0000191 objects = copy.copy(objects or [])
192
Greg Wardbf5c7092000-08-02 01:31:56 +0000193 # Additional libraries
Greg Wardf34506a2000-06-29 22:57:55 +0000194 libraries.extend(self.dll_libraries)
Greg Wardbf5c7092000-08-02 01:31:56 +0000195
Greg Ward42406482000-09-27 02:08:14 +0000196 # handle export symbols by creating a def-file
197 # with executables this only works with gcc/ld as linker
198 if ((export_symbols is not None) and
199 (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
200 # (The linker doesn't do anything if output is up-to-date.
201 # So it would probably better to check if we really need this,
Greg Wardbf5c7092000-08-02 01:31:56 +0000202 # but for this we had to insert some unchanged parts of
203 # UnixCCompiler, and this is not what we want.)
Greg Ward42406482000-09-27 02:08:14 +0000204
205 # we want to put some files in the same directory as the
206 # object files are, build_temp doesn't help much
207 # where are the object files
208 temp_dir = os.path.dirname(objects[0])
209 # name of dll to give the helper files the same base name
210 (dll_name, dll_extension) = os.path.splitext(
211 os.path.basename(output_filename))
212
213 # generate the filenames for these files
Greg Wardbf5c7092000-08-02 01:31:56 +0000214 def_file = os.path.join(temp_dir, dll_name + ".def")
Greg Ward42406482000-09-27 02:08:14 +0000215 exp_file = os.path.join(temp_dir, dll_name + ".exp")
216 lib_file = os.path.join(temp_dir, 'lib' + dll_name + ".a")
217
218 # Generate .def file
Greg Wardbf5c7092000-08-02 01:31:56 +0000219 contents = [
220 "LIBRARY %s" % os.path.basename(output_filename),
221 "EXPORTS"]
Greg Ward7c6395a2000-06-21 03:33:03 +0000222 for sym in export_symbols:
Greg Wardbf5c7092000-08-02 01:31:56 +0000223 contents.append(sym)
224 self.execute(write_file, (def_file, contents),
225 "writing %s" % def_file)
226
Greg Ward42406482000-09-27 02:08:14 +0000227 # next add options for def-file and to creating import libraries
228
229 # dllwrap uses different options than gcc/ld
230 if self.linker_dll == "dllwrap":
231 extra_preargs.extend([#"--output-exp",exp_file,
232 "--output-lib",lib_file,
233 ])
Greg Wardbf5c7092000-08-02 01:31:56 +0000234 # for dllwrap we have to use a special option
Greg Ward42406482000-09-27 02:08:14 +0000235 extra_preargs.extend(["--def", def_file])
236 # we use gcc/ld here and can be sure ld is >= 2.9.10
237 else:
238 # doesn't work: bfd_close build\...\libfoo.a: Invalid operation
239 #extra_preargs.extend(["-Wl,--out-implib,%s" % lib_file])
240 # for gcc/ld the def-file is specified as any other object files
241 objects.append(def_file)
242
243 #end: if ((export_symbols is not None) and
Fred Drake132dce22000-12-12 23:11:42 +0000244 # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
Greg Wardbf5c7092000-08-02 01:31:56 +0000245
Greg Wardf34506a2000-06-29 22:57:55 +0000246 # who wants symbols and a many times larger output file
Greg Ward612eb9f2000-07-27 02:13:20 +0000247 # should explicitly switch the debug mode on
Greg Wardbf5c7092000-08-02 01:31:56 +0000248 # otherwise we let dllwrap/ld strip the output file
Greg Ward42406482000-09-27 02:08:14 +0000249 # (On my machine: 10KB < stripped_file < ??100KB
250 # unstripped_file = stripped_file + XXX KB
251 # ( XXX=254 for a typical python extension))
Greg Ward7c6395a2000-06-21 03:33:03 +0000252 if not debug:
Greg Wardbf5c7092000-08-02 01:31:56 +0000253 extra_preargs.append("-s")
Greg Wardf34506a2000-06-29 22:57:55 +0000254
Greg Ward42406482000-09-27 02:08:14 +0000255 UnixCCompiler.link(self,
256 target_desc,
257 objects,
258 output_filename,
259 output_dir,
260 libraries,
261 library_dirs,
262 runtime_library_dirs,
263 None, # export_symbols, we do this in our def-file
264 debug,
265 extra_preargs,
266 extra_postargs,
267 build_temp)
Greg Ward7c6395a2000-06-21 03:33:03 +0000268
Greg Ward42406482000-09-27 02:08:14 +0000269 # link ()
270
271 # -- Miscellaneous methods -----------------------------------------
272
273 # overwrite the one from CCompiler to support rc and res-files
274 def object_filenames (self,
275 source_filenames,
276 strip_dir=0,
277 output_dir=''):
278 if output_dir is None: output_dir = ''
279 obj_names = []
280 for src_name in source_filenames:
281 # use normcase to make sure '.rc' is really '.rc' and not '.RC'
282 (base, ext) = os.path.splitext (os.path.normcase(src_name))
283 if ext not in (self.src_extensions + ['.rc','.res']):
284 raise UnknownFileError, \
285 "unknown file type '%s' (from '%s')" % \
286 (ext, src_name)
287 if strip_dir:
288 base = os.path.basename (base)
289 if ext == '.res' or ext == '.rc':
290 # these need to be compiled to object files
291 obj_names.append (os.path.join (output_dir,
292 base + ext + self.obj_extension))
293 else:
294 obj_names.append (os.path.join (output_dir,
295 base + self.obj_extension))
296 return obj_names
297
298 # object_filenames ()
Greg Ward7c6395a2000-06-21 03:33:03 +0000299
300# class CygwinCCompiler
301
Greg Wardf34506a2000-06-29 22:57:55 +0000302
Greg Ward7c6395a2000-06-21 03:33:03 +0000303# the same as cygwin plus some additional parameters
304class Mingw32CCompiler (CygwinCCompiler):
305
306 compiler_type = 'mingw32'
307
308 def __init__ (self,
309 verbose=0,
310 dry_run=0,
311 force=0):
312
313 CygwinCCompiler.__init__ (self, verbose, dry_run, force)
Greg Wardbf5c7092000-08-02 01:31:56 +0000314
315 # A real mingw32 doesn't need to specify a different entry point,
316 # but cygwin 2.91.57 in no-cygwin-mode needs it.
317 if self.gcc_version <= "2.91.57":
318 entry_point = '--entry _DllMain@12'
319 else:
320 entry_point = ''
Greg Ward7c6395a2000-06-21 03:33:03 +0000321
Greg Wardf34506a2000-06-29 22:57:55 +0000322 self.set_executables(compiler='gcc -mno-cygwin -O -Wall',
Greg Wardbf5c7092000-08-02 01:31:56 +0000323 compiler_so='gcc -mno-cygwin -mdll -O -Wall',
Greg Wardf34506a2000-06-29 22:57:55 +0000324 linker_exe='gcc -mno-cygwin',
Greg Wardbf5c7092000-08-02 01:31:56 +0000325 linker_so='%s -mno-cygwin -mdll -static %s'
Greg Ward42406482000-09-27 02:08:14 +0000326 % (self.linker_dll, entry_point))
Greg Wardbf5c7092000-08-02 01:31:56 +0000327 # Maybe we should also append -mthreads, but then the finished
328 # dlls need another dll (mingwm10.dll see Mingw32 docs)
329 # (-mthreads: Support thread-safe exception handling on `Mingw32')
330
331 # no additional libraries needed
332 self.dll_libraries=[]
333
Greg Ward7c6395a2000-06-21 03:33:03 +0000334 # __init__ ()
Greg Wardbf5c7092000-08-02 01:31:56 +0000335
Greg Ward7c6395a2000-06-21 03:33:03 +0000336# class Mingw32CCompiler
Greg Wardbf5c7092000-08-02 01:31:56 +0000337
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000338# Because these compilers aren't configured in Python's pyconfig.h file by
Greg Wardbf5c7092000-08-02 01:31:56 +0000339# default, we should at least warn the user if he is using a unmodified
340# version.
341
Greg Warde8e9d112000-08-13 01:18:55 +0000342CONFIG_H_OK = "ok"
343CONFIG_H_NOTOK = "not ok"
344CONFIG_H_UNCERTAIN = "uncertain"
345
Greg Wardbf5c7092000-08-02 01:31:56 +0000346def check_config_h():
Greg Warde8e9d112000-08-13 01:18:55 +0000347
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000348 """Check if the current Python installation (specifically, pyconfig.h)
Greg Warde8e9d112000-08-13 01:18:55 +0000349 appears amenable to building extensions with GCC. Returns a tuple
350 (status, details), where 'status' is one of the following constants:
351 CONFIG_H_OK
352 all is well, go ahead and compile
353 CONFIG_H_NOTOK
354 doesn't look good
355 CONFIG_H_UNCERTAIN
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000356 not sure -- unable to read pyconfig.h
Greg Warde8e9d112000-08-13 01:18:55 +0000357 'details' is a human-readable string explaining the situation.
358
359 Note there are two ways to conclude "OK": either 'sys.version' contains
360 the string "GCC" (implying that this Python was built with GCC), or the
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000361 installed "pyconfig.h" contains the string "__GNUC__".
Greg Wardbf5c7092000-08-02 01:31:56 +0000362 """
Greg Warde8e9d112000-08-13 01:18:55 +0000363
364 # XXX since this function also checks sys.version, it's not strictly a
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000365 # "pyconfig.h" check -- should probably be renamed...
Greg Wardbf5c7092000-08-02 01:31:56 +0000366
367 from distutils import sysconfig
Andrew M. Kuchling6e9c0ba2001-03-22 03:50:09 +0000368 import string
Greg Wardbf5c7092000-08-02 01:31:56 +0000369 # if sys.version contains GCC then python was compiled with
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000370 # GCC, and the pyconfig.h file should be OK
Andrew M. Kuchlingac20f772001-03-22 03:48:31 +0000371 if string.find(sys.version,"GCC") >= 0:
Greg Warde8e9d112000-08-13 01:18:55 +0000372 return (CONFIG_H_OK, "sys.version mentions 'GCC'")
Greg Wardbf5c7092000-08-02 01:31:56 +0000373
Greg Warde8e9d112000-08-13 01:18:55 +0000374 fn = sysconfig.get_config_h_filename()
Greg Wardbf5c7092000-08-02 01:31:56 +0000375 try:
376 # It would probably better to read single lines to search.
377 # But we do this only once, and it is fast enough
Greg Warde8e9d112000-08-13 01:18:55 +0000378 f = open(fn)
379 s = f.read()
Greg Wardbf5c7092000-08-02 01:31:56 +0000380 f.close()
381
Greg Warde8e9d112000-08-13 01:18:55 +0000382 except IOError, exc:
Greg Wardbf5c7092000-08-02 01:31:56 +0000383 # if we can't read this file, we cannot say it is wrong
384 # the compiler will complain later about this file as missing
Greg Warde8e9d112000-08-13 01:18:55 +0000385 return (CONFIG_H_UNCERTAIN,
386 "couldn't read '%s': %s" % (fn, exc.strerror))
387
388 else:
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000389 # "pyconfig.h" contains an "#ifdef __GNUC__" or something similar
Andrew M. Kuchlingac20f772001-03-22 03:48:31 +0000390 if string.find(s,"__GNUC__") >= 0:
Greg Warde8e9d112000-08-13 01:18:55 +0000391 return (CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn)
392 else:
393 return (CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn)
394
395
Greg Wardbf5c7092000-08-02 01:31:56 +0000396
397def get_versions():
398 """ Try to find out the versions of gcc, ld and dllwrap.
399 If not possible it returns None for it.
400 """
401 from distutils.version import StrictVersion
402 from distutils.spawn import find_executable
403 import re
404
405 gcc_exe = find_executable('gcc')
406 if gcc_exe:
407 out = os.popen(gcc_exe + ' -dumpversion','r')
408 out_string = out.read()
409 out.close()
410 result = re.search('(\d+\.\d+\.\d+)',out_string)
411 if result:
412 gcc_version = StrictVersion(result.group(1))
413 else:
414 gcc_version = None
415 else:
416 gcc_version = None
417 ld_exe = find_executable('ld')
418 if ld_exe:
419 out = os.popen(ld_exe + ' -v','r')
420 out_string = out.read()
421 out.close()
422 result = re.search('(\d+\.\d+\.\d+)',out_string)
423 if result:
424 ld_version = StrictVersion(result.group(1))
425 else:
426 ld_version = None
427 else:
428 ld_version = None
429 dllwrap_exe = find_executable('dllwrap')
430 if dllwrap_exe:
431 out = os.popen(dllwrap_exe + ' --version','r')
432 out_string = out.read()
433 out.close()
434 result = re.search(' (\d+\.\d+\.\d+)',out_string)
435 if result:
436 dllwrap_version = StrictVersion(result.group(1))
437 else:
438 dllwrap_version = None
439 else:
440 dllwrap_version = None
441 return (gcc_version, ld_version, dllwrap_version)
442