Files for 2.1b1 distribution.
[python/dscho.git] / Lib / distutils / cygwinccompiler.py
blob42318ad3d4ecbfbf95f753d3ef0aa1c323a2b31e
1 """distutils.cygwinccompiler
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 """
9 # problems:
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
19 # see also http://starship.python.net/crew/kernr/mingw32/Notes.html
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.
26 # tested configurations:
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
44 # created 2000/05/05, Rene Liebscher
46 __revision__ = "$Id$"
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
54 class CygwinCCompiler (UnixCCompiler):
56 compiler_type = 'cygwin'
57 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"
64 def __init__ (self,
65 verbose=0,
66 dry_run=0,
67 force=0):
69 UnixCCompiler.__init__ (self, verbose, dry_run, force)
71 (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:
75 self.warn(
76 "Python's config.h doesn't seem to support your compiler. " +
77 ("Reason: %s." % details) +
78 "Compiling may fail because of undefined preprocessor macros.")
80 (self.gcc_version, self.ld_version, self.dllwrap_version) = \
81 get_versions()
82 self.debug_print(self.compiler_type + ": gcc %s, ld %s, dllwrap %s\n" %
83 (self.gcc_version,
84 self.ld_version,
85 self.dllwrap_version) )
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":
93 self.linker_dll = "gcc"
94 else:
95 self.linker_dll = "dllwrap"
97 # Hard-code GCC because that's what this is all about.
98 # XXX optimization, warnings etc. should be customizable.
99 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' %
103 self.linker_dll))
105 # cygwin and mingw32 need different sets of libraries
106 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=[]
115 # __init__ ()
117 # 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):
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)
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 = []
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
168 # Return *all* object filenames, not just the ones we just built.
169 return objects
171 # compile ()
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):
188 # use separate copies, so we can modify the lists
189 extra_preargs = copy.copy(extra_preargs or [])
190 libraries = copy.copy(libraries or [])
191 objects = copy.copy(objects or [])
193 # Additional libraries
194 libraries.extend(self.dll_libraries)
196 # 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,
202 # but for this we had to insert some unchanged parts of
203 # UnixCCompiler, and this is not what we want.)
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))
213 # generate the filenames for these files
214 def_file = os.path.join(temp_dir, dll_name + ".def")
215 exp_file = os.path.join(temp_dir, dll_name + ".exp")
216 lib_file = os.path.join(temp_dir, 'lib' + dll_name + ".a")
218 # Generate .def file
219 contents = [
220 "LIBRARY %s" % os.path.basename(output_filename),
221 "EXPORTS"]
222 for sym in export_symbols:
223 contents.append(sym)
224 self.execute(write_file, (def_file, contents),
225 "writing %s" % def_file)
227 # next add options for def-file and to creating import libraries
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,
234 # for dllwrap we have to use a special option
235 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)
243 #end: if ((export_symbols is not None) and
244 # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
246 # who wants symbols and a many times larger output file
247 # should explicitly switch the debug mode on
248 # otherwise we let dllwrap/ld strip the output file
249 # (On my machine: 10KB < stripped_file < ??100KB
250 # unstripped_file = stripped_file + XXX KB
251 # ( XXX=254 for a typical python extension))
252 if not debug:
253 extra_preargs.append("-s")
255 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)
269 # link ()
271 # -- Miscellaneous methods -----------------------------------------
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
298 # object_filenames ()
300 # class CygwinCCompiler
303 # the same as cygwin plus some additional parameters
304 class Mingw32CCompiler (CygwinCCompiler):
306 compiler_type = 'mingw32'
308 def __init__ (self,
309 verbose=0,
310 dry_run=0,
311 force=0):
313 CygwinCCompiler.__init__ (self, verbose, dry_run, force)
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 = ''
322 self.set_executables(compiler='gcc -mno-cygwin -O -Wall',
323 compiler_so='gcc -mno-cygwin -mdll -O -Wall',
324 linker_exe='gcc -mno-cygwin',
325 linker_so='%s -mno-cygwin -mdll -static %s'
326 % (self.linker_dll, entry_point))
327 # 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')
331 # no additional libraries needed
332 self.dll_libraries=[]
334 # __init__ ()
336 # class Mingw32CCompiler
338 # Because these compilers aren't configured in Python's config.h file by
339 # default, we should at least warn the user if he is using a unmodified
340 # version.
342 CONFIG_H_OK = "ok"
343 CONFIG_H_NOTOK = "not ok"
344 CONFIG_H_UNCERTAIN = "uncertain"
346 def check_config_h():
348 """Check if the current Python installation (specifically, config.h)
349 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
356 not sure -- unable to read config.h
357 'details' is a human-readable string explaining the situation.
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
361 installed "config.h" contains the string "__GNUC__".
364 # XXX since this function also checks sys.version, it's not strictly a
365 # "config.h" check -- should probably be renamed...
367 from distutils import sysconfig
368 import sys
369 # if sys.version contains GCC then python was compiled with
370 # GCC, and the config.h file should be OK
371 if sys.version.find("GCC") >= 0:
372 return (CONFIG_H_OK, "sys.version mentions 'GCC'")
374 fn = sysconfig.get_config_h_filename()
375 try:
376 # It would probably better to read single lines to search.
377 # But we do this only once, and it is fast enough
378 f = open(fn)
379 s = f.read()
380 f.close()
382 except IOError, exc:
383 # if we can't read this file, we cannot say it is wrong
384 # the compiler will complain later about this file as missing
385 return (CONFIG_H_UNCERTAIN,
386 "couldn't read '%s': %s" % (fn, exc.strerror))
388 else:
389 # "config.h" contains an "#ifdef __GNUC__" or something similar
390 if s.find("__GNUC__") >= 0:
391 return (CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn)
392 else:
393 return (CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn)
397 def get_versions():
398 """ Try to find out the versions of gcc, ld and dllwrap.
399 If not possible it returns None for it.
401 from distutils.version import StrictVersion
402 from distutils.spawn import find_executable
403 import re
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)