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).
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
42 # *** only the version of June 2000 shows these problems
43 # * cygwin gcc 3.2/ld 2.13.90 works
44 # (ld supports -shared)
45 # * mingw gcc 3.2/ld 2.13 works
46 # (ld supports -shared)
48 # This module should be kept compatible with Python 2.1.
53 from distutils
.ccompiler
import gen_preprocess_options
, gen_lib_options
54 from distutils
.unixccompiler
import UnixCCompiler
55 from distutils
.file_util
import write_file
56 from distutils
.errors
import DistutilsExecError
, CompileError
, UnknownFileError
57 from distutils
import log
60 """Include the appropriate MSVC runtime library if Python was built
61 with MSVC 7.0 or later.
63 msc_pos
= sys
.version
.find('MSC v.')
65 msc_ver
= sys
.version
[msc_pos
+6:msc_pos
+10]
69 elif msc_ver
== '1310':
72 elif msc_ver
== '1400':
75 elif msc_ver
== '1500':
79 raise ValueError("Unknown MS Compiler version %s " % msc_ver
)
82 class CygwinCCompiler (UnixCCompiler
):
84 compiler_type
= 'cygwin'
86 static_lib_extension
= ".a"
87 shared_lib_extension
= ".dll"
88 static_lib_format
= "lib%s%s"
89 shared_lib_format
= "%s%s"
90 exe_extension
= ".exe"
92 def __init__ (self
, verbose
=0, dry_run
=0, force
=0):
94 UnixCCompiler
.__init
__ (self
, verbose
, dry_run
, force
)
96 (status
, details
) = check_config_h()
97 self
.debug_print("Python's GCC status: %s (details: %s)" %
99 if status
is not CONFIG_H_OK
:
101 "Python's pyconfig.h doesn't seem to support your compiler. "
103 "Compiling may fail because of undefined preprocessor macros."
106 self
.gcc_version
, self
.ld_version
, self
.dllwrap_version
= \
108 self
.debug_print(self
.compiler_type
+ ": gcc %s, ld %s, dllwrap %s\n" %
111 self
.dllwrap_version
) )
113 # ld_version >= "2.10.90" and < "2.13" should also be able to use
114 # gcc -mdll instead of dllwrap
115 # Older dllwraps had own version numbers, newer ones use the
116 # same as the rest of binutils ( also ld )
117 # dllwrap 2.10.90 is buggy
118 if self
.ld_version
>= "2.10.90":
119 self
.linker_dll
= "gcc"
121 self
.linker_dll
= "dllwrap"
123 # ld_version >= "2.13" support -shared so use it instead of
125 if self
.ld_version
>= "2.13":
126 shared_option
= "-shared"
128 shared_option
= "-mdll -static"
130 # Hard-code GCC because that's what this is all about.
131 # XXX optimization, warnings etc. should be customizable.
132 self
.set_executables(compiler
='gcc -mcygwin -O -Wall',
133 compiler_so
='gcc -mcygwin -mdll -O -Wall',
134 compiler_cxx
='g++ -mcygwin -O -Wall',
135 linker_exe
='gcc -mcygwin',
136 linker_so
=('%s -mcygwin %s' %
137 (self
.linker_dll
, shared_option
)))
139 # cygwin and mingw32 need different sets of libraries
140 if self
.gcc_version
== "2.91.57":
141 # cygwin shouldn't need msvcrt, but without the dlls will crash
142 # (gcc version 2.91.57) -- perhaps something about initialization
143 self
.dll_libraries
=["msvcrt"]
145 "Consider upgrading to a newer version of gcc")
147 # Include the appropriate MSVC runtime library if Python was built
148 # with MSVC 7.0 or later.
149 self
.dll_libraries
= get_msvcr()
154 def _compile(self
, obj
, src
, ext
, cc_args
, extra_postargs
, pp_opts
):
155 if ext
== '.rc' or ext
== '.res':
156 # gcc needs '.res' and '.rc' compiled to object files !!!
158 self
.spawn(["windres", "-i", src
, "-o", obj
])
159 except DistutilsExecError
, msg
:
160 raise CompileError
, msg
161 else: # for other files use the C-compiler
163 self
.spawn(self
.compiler_so
+ cc_args
+ [src
, '-o', obj
] +
165 except DistutilsExecError
, msg
:
166 raise CompileError
, msg
175 runtime_library_dirs
=None,
183 # use separate copies, so we can modify the lists
184 extra_preargs
= copy
.copy(extra_preargs
or [])
185 libraries
= copy
.copy(libraries
or [])
186 objects
= copy
.copy(objects
or [])
188 # Additional libraries
189 libraries
.extend(self
.dll_libraries
)
191 # handle export symbols by creating a def-file
192 # with executables this only works with gcc/ld as linker
193 if ((export_symbols
is not None) and
194 (target_desc
!= self
.EXECUTABLE
or self
.linker_dll
== "gcc")):
195 # (The linker doesn't do anything if output is up-to-date.
196 # So it would probably better to check if we really need this,
197 # but for this we had to insert some unchanged parts of
198 # UnixCCompiler, and this is not what we want.)
200 # we want to put some files in the same directory as the
201 # object files are, build_temp doesn't help much
202 # where are the object files
203 temp_dir
= os
.path
.dirname(objects
[0])
204 # name of dll to give the helper files the same base name
205 (dll_name
, dll_extension
) = os
.path
.splitext(
206 os
.path
.basename(output_filename
))
208 # generate the filenames for these files
209 def_file
= os
.path
.join(temp_dir
, dll_name
+ ".def")
210 lib_file
= os
.path
.join(temp_dir
, 'lib' + dll_name
+ ".a")
214 "LIBRARY %s" % os
.path
.basename(output_filename
),
216 for sym
in export_symbols
:
218 self
.execute(write_file
, (def_file
, contents
),
219 "writing %s" % def_file
)
221 # next add options for def-file and to creating import libraries
223 # dllwrap uses different options than gcc/ld
224 if self
.linker_dll
== "dllwrap":
225 extra_preargs
.extend(["--output-lib", lib_file
])
226 # for dllwrap we have to use a special option
227 extra_preargs
.extend(["--def", def_file
])
228 # we use gcc/ld here and can be sure ld is >= 2.9.10
230 # doesn't work: bfd_close build\...\libfoo.a: Invalid operation
231 #extra_preargs.extend(["-Wl,--out-implib,%s" % lib_file])
232 # for gcc/ld the def-file is specified as any object files
233 objects
.append(def_file
)
235 #end: if ((export_symbols is not None) and
236 # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
238 # who wants symbols and a many times larger output file
239 # should explicitly switch the debug mode on
240 # otherwise we let dllwrap/ld strip the output file
241 # (On my machine: 10KB < stripped_file < ??100KB
242 # unstripped_file = stripped_file + XXX KB
243 # ( XXX=254 for a typical python extension))
245 extra_preargs
.append("-s")
247 UnixCCompiler
.link(self
,
254 runtime_library_dirs
,
255 None, # export_symbols, we do this in our def-file
264 # -- Miscellaneous methods -----------------------------------------
266 # overwrite the one from CCompiler to support rc and res-files
267 def object_filenames (self
,
271 if output_dir
is None: output_dir
= ''
273 for src_name
in source_filenames
:
274 # use normcase to make sure '.rc' is really '.rc' and not '.RC'
275 (base
, ext
) = os
.path
.splitext (os
.path
.normcase(src_name
))
276 if ext
not in (self
.src_extensions
+ ['.rc','.res']):
277 raise UnknownFileError
, \
278 "unknown file type '%s' (from '%s')" % \
281 base
= os
.path
.basename (base
)
282 if ext
== '.res' or ext
== '.rc':
283 # these need to be compiled to object files
284 obj_names
.append (os
.path
.join (output_dir
,
285 base
+ ext
+ self
.obj_extension
))
287 obj_names
.append (os
.path
.join (output_dir
,
288 base
+ self
.obj_extension
))
291 # object_filenames ()
293 # class CygwinCCompiler
296 # the same as cygwin plus some additional parameters
297 class Mingw32CCompiler (CygwinCCompiler
):
299 compiler_type
= 'mingw32'
306 CygwinCCompiler
.__init
__ (self
, verbose
, dry_run
, force
)
308 # ld_version >= "2.13" support -shared so use it instead of
310 if self
.ld_version
>= "2.13":
311 shared_option
= "-shared"
313 shared_option
= "-mdll -static"
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'
322 self
.set_executables(compiler
='gcc -mno-cygwin -O -Wall',
323 compiler_so
='gcc -mno-cygwin -mdll -O -Wall',
324 compiler_cxx
='g++ -mno-cygwin -O -Wall',
325 linker_exe
='gcc -mno-cygwin',
326 linker_so
='%s -mno-cygwin %s %s'
327 % (self
.linker_dll
, shared_option
,
329 # Maybe we should also append -mthreads, but then the finished
330 # dlls need another dll (mingwm10.dll see Mingw32 docs)
331 # (-mthreads: Support thread-safe exception handling on `Mingw32')
333 # no additional libraries needed
334 self
.dll_libraries
=[]
336 # Include the appropriate MSVC runtime library if Python was built
337 # with MSVC 7.0 or later.
338 self
.dll_libraries
= get_msvcr()
342 # class Mingw32CCompiler
344 # Because these compilers aren't configured in Python's pyconfig.h file by
345 # default, we should at least warn the user if he is using a unmodified
349 CONFIG_H_NOTOK
= "not ok"
350 CONFIG_H_UNCERTAIN
= "uncertain"
352 def check_config_h():
354 """Check if the current Python installation (specifically, pyconfig.h)
355 appears amenable to building extensions with GCC. Returns a tuple
356 (status, details), where 'status' is one of the following constants:
358 all is well, go ahead and compile
362 not sure -- unable to read pyconfig.h
363 'details' is a human-readable string explaining the situation.
365 Note there are two ways to conclude "OK": either 'sys.version' contains
366 the string "GCC" (implying that this Python was built with GCC), or the
367 installed "pyconfig.h" contains the string "__GNUC__".
370 # XXX since this function also checks sys.version, it's not strictly a
371 # "pyconfig.h" check -- should probably be renamed...
373 from distutils
import sysconfig
375 # if sys.version contains GCC then python was compiled with
376 # GCC, and the pyconfig.h file should be OK
377 if string
.find(sys
.version
,"GCC") >= 0:
378 return (CONFIG_H_OK
, "sys.version mentions 'GCC'")
380 fn
= sysconfig
.get_config_h_filename()
382 # It would probably better to read single lines to search.
383 # But we do this only once, and it is fast enough
389 # if we can't read this file, we cannot say it is wrong
390 # the compiler will complain later about this file as missing
391 return (CONFIG_H_UNCERTAIN
,
392 "couldn't read '%s': %s" % (fn
, exc
.strerror
))
395 # "pyconfig.h" contains an "#ifdef __GNUC__" or something similar
396 if string
.find(s
,"__GNUC__") >= 0:
397 return (CONFIG_H_OK
, "'%s' mentions '__GNUC__'" % fn
)
399 return (CONFIG_H_NOTOK
, "'%s' does not mention '__GNUC__'" % fn
)
404 """ Try to find out the versions of gcc, ld and dllwrap.
405 If not possible it returns None for it.
407 from distutils
.version
import LooseVersion
408 from distutils
.spawn
import find_executable
411 gcc_exe
= find_executable('gcc')
413 out
= os
.popen(gcc_exe
+ ' -dumpversion','r')
414 out_string
= out
.read()
416 result
= re
.search('(\d+\.\d+(\.\d+)*)',out_string
)
418 gcc_version
= LooseVersion(result
.group(1))
423 ld_exe
= find_executable('ld')
425 out
= os
.popen(ld_exe
+ ' -v','r')
426 out_string
= out
.read()
428 result
= re
.search('(\d+\.\d+(\.\d+)*)',out_string
)
430 ld_version
= LooseVersion(result
.group(1))
435 dllwrap_exe
= find_executable('dllwrap')
437 out
= os
.popen(dllwrap_exe
+ ' --version','r')
438 out_string
= out
.read()
440 result
= re
.search(' (\d+\.\d+(\.\d+)*)',out_string
)
442 dllwrap_version
= LooseVersion(result
.group(1))
444 dllwrap_version
= None
446 dllwrap_version
= None
447 return (gcc_version
, ld_version
, dllwrap_version
)