blob: fd5296c358f4860e85a196f069f445e1975720d0 [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
Jason Tishler21664d82003-04-14 12:51:26 +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.
Fred Drakeb94b8492001-12-06 20:51:35 +000042# *** only the version of June 2000 shows these problems
Jason Tishler21664d82003-04-14 12:51:26 +000043# * 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)
Greg Wardbf5c7092000-08-02 01:31:56 +000047
Greg Ward7c6395a2000-06-21 03:33:03 +000048__revision__ = "$Id$"
49
Tarek Ziadé7ca57aa2009-06-10 18:49:50 +000050import os
51import sys
52import copy
53from subprocess import Popen, PIPE
54import re
55
Greg Ward42406482000-09-27 02:08:14 +000056from distutils.ccompiler import gen_preprocess_options, gen_lib_options
Greg Ward7c6395a2000-06-21 03:33:03 +000057from distutils.unixccompiler import UnixCCompiler
Greg Wardbf5c7092000-08-02 01:31:56 +000058from distutils.file_util import write_file
Greg Ward42406482000-09-27 02:08:14 +000059from distutils.errors import DistutilsExecError, CompileError, UnknownFileError
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000060from distutils import log
Tarek Ziadé7ca57aa2009-06-10 18:49:50 +000061from distutils.version import LooseVersion
62from distutils.spawn import find_executable
Greg Ward7c6395a2000-06-21 03:33:03 +000063
Christian Heimes3305c522007-12-03 13:47:29 +000064def get_msvcr():
65 """Include the appropriate MSVC runtime library if Python was built
66 with MSVC 7.0 or later.
67 """
68 msc_pos = sys.version.find('MSC v.')
69 if msc_pos != -1:
70 msc_ver = sys.version[msc_pos+6:msc_pos+10]
71 if msc_ver == '1300':
72 # MSVC 7.0
73 return ['msvcr70']
74 elif msc_ver == '1310':
75 # MSVC 7.1
76 return ['msvcr71']
77 elif msc_ver == '1400':
78 # VS2005 / MSVC 8.0
79 return ['msvcr80']
80 elif msc_ver == '1500':
81 # VS2008 / MSVC 9.0
82 return ['msvcr90']
83 else:
Tarek Ziadéc7498f52009-06-11 09:13:36 +000084 raise ValueError("Unknown MS Compiler version %s " % msc_ver)
Christian Heimes3305c522007-12-03 13:47:29 +000085
86
Greg Ward7c6395a2000-06-21 03:33:03 +000087class CygwinCCompiler (UnixCCompiler):
88
89 compiler_type = 'cygwin'
Greg Wardb1dceae2000-08-13 00:43:56 +000090 obj_extension = ".o"
91 static_lib_extension = ".a"
92 shared_lib_extension = ".dll"
93 static_lib_format = "lib%s%s"
94 shared_lib_format = "%s%s"
95 exe_extension = ".exe"
Fred Drakeb94b8492001-12-06 20:51:35 +000096
Jeremy Hylton1bba31d2002-06-13 17:28:18 +000097 def __init__ (self, verbose=0, dry_run=0, force=0):
Greg Ward7c6395a2000-06-21 03:33:03 +000098
99 UnixCCompiler.__init__ (self, verbose, dry_run, force)
100
Greg Warde8e9d112000-08-13 01:18:55 +0000101 (status, details) = check_config_h()
102 self.debug_print("Python's GCC status: %s (details: %s)" %
103 (status, details))
104 if status is not CONFIG_H_OK:
Greg Wardbf5c7092000-08-02 01:31:56 +0000105 self.warn(
Tim Peters182b5ac2004-07-18 06:16:08 +0000106 "Python's pyconfig.h doesn't seem to support your compiler. "
Jeremy Hylton1bba31d2002-06-13 17:28:18 +0000107 "Reason: %s. "
108 "Compiling may fail because of undefined preprocessor macros."
109 % details)
Fred Drakeb94b8492001-12-06 20:51:35 +0000110
Jeremy Hylton1bba31d2002-06-13 17:28:18 +0000111 self.gcc_version, self.ld_version, self.dllwrap_version = \
Greg Wardbf5c7092000-08-02 01:31:56 +0000112 get_versions()
Greg Wardb1dceae2000-08-13 00:43:56 +0000113 self.debug_print(self.compiler_type + ": gcc %s, ld %s, dllwrap %s\n" %
Fred Drakeb94b8492001-12-06 20:51:35 +0000114 (self.gcc_version,
115 self.ld_version,
Greg Wardbf5c7092000-08-02 01:31:56 +0000116 self.dllwrap_version) )
117
Jason Tishler21664d82003-04-14 12:51:26 +0000118 # ld_version >= "2.10.90" and < "2.13" should also be able to use
Greg Wardbf5c7092000-08-02 01:31:56 +0000119 # gcc -mdll instead of dllwrap
Fred Drakeb94b8492001-12-06 20:51:35 +0000120 # Older dllwraps had own version numbers, newer ones use the
Greg Wardbf5c7092000-08-02 01:31:56 +0000121 # same as the rest of binutils ( also ld )
122 # dllwrap 2.10.90 is buggy
Fred Drakeb94b8492001-12-06 20:51:35 +0000123 if self.ld_version >= "2.10.90":
Greg Ward42406482000-09-27 02:08:14 +0000124 self.linker_dll = "gcc"
Greg Wardbf5c7092000-08-02 01:31:56 +0000125 else:
Greg Ward42406482000-09-27 02:08:14 +0000126 self.linker_dll = "dllwrap"
Greg Wardbf5c7092000-08-02 01:31:56 +0000127
Jason Tishler21664d82003-04-14 12:51:26 +0000128 # ld_version >= "2.13" support -shared so use it instead of
129 # -mdll -static
130 if self.ld_version >= "2.13":
131 shared_option = "-shared"
132 else:
133 shared_option = "-mdll -static"
134
Greg Wardf34506a2000-06-29 22:57:55 +0000135 # Hard-code GCC because that's what this is all about.
136 # XXX optimization, warnings etc. should be customizable.
Greg Wardbf5c7092000-08-02 01:31:56 +0000137 self.set_executables(compiler='gcc -mcygwin -O -Wall',
138 compiler_so='gcc -mcygwin -mdll -O -Wall',
Hye-Shik Chang2400e932004-06-05 18:37:53 +0000139 compiler_cxx='g++ -mcygwin -O -Wall',
Greg Wardbf5c7092000-08-02 01:31:56 +0000140 linker_exe='gcc -mcygwin',
Jason Tishler21664d82003-04-14 12:51:26 +0000141 linker_so=('%s -mcygwin %s' %
142 (self.linker_dll, shared_option)))
Greg Ward7c6395a2000-06-21 03:33:03 +0000143
Fred Drakeb94b8492001-12-06 20:51:35 +0000144 # cygwin and mingw32 need different sets of libraries
Greg Wardbf5c7092000-08-02 01:31:56 +0000145 if self.gcc_version == "2.91.57":
146 # cygwin shouldn't need msvcrt, but without the dlls will crash
147 # (gcc version 2.91.57) -- perhaps something about initialization
148 self.dll_libraries=["msvcrt"]
Fred Drakeb94b8492001-12-06 20:51:35 +0000149 self.warn(
Greg Wardbf5c7092000-08-02 01:31:56 +0000150 "Consider upgrading to a newer version of gcc")
151 else:
Tim Peters6db15d72004-08-04 02:36:18 +0000152 # Include the appropriate MSVC runtime library if Python was built
Christian Heimes3305c522007-12-03 13:47:29 +0000153 # with MSVC 7.0 or later.
154 self.dll_libraries = get_msvcr()
Fred Drakeb94b8492001-12-06 20:51:35 +0000155
Greg Ward7c6395a2000-06-21 03:33:03 +0000156 # __init__ ()
157
Greg Ward42406482000-09-27 02:08:14 +0000158
Jeremy Hylton1b046e42002-06-18 18:48:55 +0000159 def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
160 if ext == '.rc' or ext == '.res':
161 # gcc needs '.res' and '.rc' compiled to object files !!!
162 try:
163 self.spawn(["windres", "-i", src, "-o", obj])
164 except DistutilsExecError, msg:
165 raise CompileError, msg
166 else: # for other files use the C-compiler
167 try:
168 self.spawn(self.compiler_so + cc_args + [src, '-o', obj] +
169 extra_postargs)
170 except DistutilsExecError, msg:
171 raise CompileError, msg
Greg Ward42406482000-09-27 02:08:14 +0000172
Greg Ward42406482000-09-27 02:08:14 +0000173 def link (self,
174 target_desc,
175 objects,
176 output_filename,
177 output_dir=None,
178 libraries=None,
179 library_dirs=None,
180 runtime_library_dirs=None,
181 export_symbols=None,
182 debug=0,
183 extra_preargs=None,
184 extra_postargs=None,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000185 build_temp=None,
186 target_lang=None):
Fred Drakeb94b8492001-12-06 20:51:35 +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 [])
Fred Drakeb94b8492001-12-06 20:51:35 +0000192
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,
Fred Drakeb94b8492001-12-06 20:51:35 +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
Fred Drakeb94b8492001-12-06 20:51:35 +0000205 # we want to put some files in the same directory as the
Greg Ward42406482000-09-27 02:08:14 +0000206 # 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 lib_file = os.path.join(temp_dir, 'lib' + dll_name + ".a")
Fred Drakeb94b8492001-12-06 20:51:35 +0000216
Greg Ward42406482000-09-27 02:08:14 +0000217 # Generate .def file
Greg Wardbf5c7092000-08-02 01:31:56 +0000218 contents = [
219 "LIBRARY %s" % os.path.basename(output_filename),
220 "EXPORTS"]
Greg Ward7c6395a2000-06-21 03:33:03 +0000221 for sym in export_symbols:
Greg Wardbf5c7092000-08-02 01:31:56 +0000222 contents.append(sym)
223 self.execute(write_file, (def_file, contents),
224 "writing %s" % def_file)
225
Greg Ward42406482000-09-27 02:08:14 +0000226 # next add options for def-file and to creating import libraries
227
228 # dllwrap uses different options than gcc/ld
229 if self.linker_dll == "dllwrap":
Jeremy Hyltona2f99892002-06-04 20:26:44 +0000230 extra_preargs.extend(["--output-lib", lib_file])
Greg Wardbf5c7092000-08-02 01:31:56 +0000231 # for dllwrap we have to use a special option
Greg Ward42406482000-09-27 02:08:14 +0000232 extra_preargs.extend(["--def", def_file])
233 # we use gcc/ld here and can be sure ld is >= 2.9.10
234 else:
235 # doesn't work: bfd_close build\...\libfoo.a: Invalid operation
236 #extra_preargs.extend(["-Wl,--out-implib,%s" % lib_file])
Jeremy Hyltona2f99892002-06-04 20:26:44 +0000237 # for gcc/ld the def-file is specified as any object files
Greg Ward42406482000-09-27 02:08:14 +0000238 objects.append(def_file)
239
240 #end: if ((export_symbols is not None) and
Fred Drake132dce22000-12-12 23:11:42 +0000241 # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
Fred Drakeb94b8492001-12-06 20:51:35 +0000242
Greg Wardf34506a2000-06-29 22:57:55 +0000243 # who wants symbols and a many times larger output file
Fred Drakeb94b8492001-12-06 20:51:35 +0000244 # should explicitly switch the debug mode on
Greg Wardbf5c7092000-08-02 01:31:56 +0000245 # otherwise we let dllwrap/ld strip the output file
Fred Drakeb94b8492001-12-06 20:51:35 +0000246 # (On my machine: 10KB < stripped_file < ??100KB
Greg Ward42406482000-09-27 02:08:14 +0000247 # unstripped_file = stripped_file + XXX KB
Fred Drakeb94b8492001-12-06 20:51:35 +0000248 # ( XXX=254 for a typical python extension))
249 if not debug:
250 extra_preargs.append("-s")
251
Greg Ward42406482000-09-27 02:08:14 +0000252 UnixCCompiler.link(self,
253 target_desc,
254 objects,
255 output_filename,
256 output_dir,
257 libraries,
258 library_dirs,
259 runtime_library_dirs,
260 None, # export_symbols, we do this in our def-file
261 debug,
262 extra_preargs,
263 extra_postargs,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000264 build_temp,
265 target_lang)
Fred Drakeb94b8492001-12-06 20:51:35 +0000266
Greg Ward42406482000-09-27 02:08:14 +0000267 # link ()
268
269 # -- Miscellaneous methods -----------------------------------------
270
271 # overwrite the one from CCompiler to support rc and res-files
272 def object_filenames (self,
273 source_filenames,
274 strip_dir=0,
275 output_dir=''):
276 if output_dir is None: output_dir = ''
277 obj_names = []
278 for src_name in source_filenames:
279 # use normcase to make sure '.rc' is really '.rc' and not '.RC'
280 (base, ext) = os.path.splitext (os.path.normcase(src_name))
281 if ext not in (self.src_extensions + ['.rc','.res']):
282 raise UnknownFileError, \
283 "unknown file type '%s' (from '%s')" % \
284 (ext, src_name)
285 if strip_dir:
286 base = os.path.basename (base)
287 if ext == '.res' or ext == '.rc':
288 # these need to be compiled to object files
Fred Drakeb94b8492001-12-06 20:51:35 +0000289 obj_names.append (os.path.join (output_dir,
Greg Ward42406482000-09-27 02:08:14 +0000290 base + ext + self.obj_extension))
291 else:
292 obj_names.append (os.path.join (output_dir,
293 base + self.obj_extension))
294 return obj_names
295
296 # object_filenames ()
Greg Ward7c6395a2000-06-21 03:33:03 +0000297
298# class CygwinCCompiler
299
Greg Wardf34506a2000-06-29 22:57:55 +0000300
Greg Ward7c6395a2000-06-21 03:33:03 +0000301# the same as cygwin plus some additional parameters
302class Mingw32CCompiler (CygwinCCompiler):
303
304 compiler_type = 'mingw32'
305
306 def __init__ (self,
307 verbose=0,
308 dry_run=0,
309 force=0):
310
311 CygwinCCompiler.__init__ (self, verbose, dry_run, force)
Fred Drakeb94b8492001-12-06 20:51:35 +0000312
Jason Tishler21664d82003-04-14 12:51:26 +0000313 # ld_version >= "2.13" support -shared so use it instead of
314 # -mdll -static
315 if self.ld_version >= "2.13":
316 shared_option = "-shared"
317 else:
318 shared_option = "-mdll -static"
319
Greg Wardbf5c7092000-08-02 01:31:56 +0000320 # A real mingw32 doesn't need to specify a different entry point,
321 # but cygwin 2.91.57 in no-cygwin-mode needs it.
322 if self.gcc_version <= "2.91.57":
323 entry_point = '--entry _DllMain@12'
324 else:
325 entry_point = ''
Greg Ward7c6395a2000-06-21 03:33:03 +0000326
Greg Wardf34506a2000-06-29 22:57:55 +0000327 self.set_executables(compiler='gcc -mno-cygwin -O -Wall',
Greg Wardbf5c7092000-08-02 01:31:56 +0000328 compiler_so='gcc -mno-cygwin -mdll -O -Wall',
Hye-Shik Chang2400e932004-06-05 18:37:53 +0000329 compiler_cxx='g++ -mno-cygwin -O -Wall',
Greg Wardf34506a2000-06-29 22:57:55 +0000330 linker_exe='gcc -mno-cygwin',
Jason Tishler21664d82003-04-14 12:51:26 +0000331 linker_so='%s -mno-cygwin %s %s'
332 % (self.linker_dll, shared_option,
333 entry_point))
Greg Wardbf5c7092000-08-02 01:31:56 +0000334 # Maybe we should also append -mthreads, but then the finished
335 # dlls need another dll (mingwm10.dll see Mingw32 docs)
Fred Drakeb94b8492001-12-06 20:51:35 +0000336 # (-mthreads: Support thread-safe exception handling on `Mingw32')
337
338 # no additional libraries needed
Greg Wardbf5c7092000-08-02 01:31:56 +0000339 self.dll_libraries=[]
Fred Drakeb94b8492001-12-06 20:51:35 +0000340
Tim Peters6db15d72004-08-04 02:36:18 +0000341 # Include the appropriate MSVC runtime library if Python was built
Christian Heimes3305c522007-12-03 13:47:29 +0000342 # with MSVC 7.0 or later.
343 self.dll_libraries = get_msvcr()
Martin v. Löwis7db57b32004-08-03 12:41:42 +0000344
Greg Ward7c6395a2000-06-21 03:33:03 +0000345 # __init__ ()
Greg Wardbf5c7092000-08-02 01:31:56 +0000346
Greg Ward7c6395a2000-06-21 03:33:03 +0000347# class Mingw32CCompiler
Greg Wardbf5c7092000-08-02 01:31:56 +0000348
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000349# Because these compilers aren't configured in Python's pyconfig.h file by
Greg Wardbf5c7092000-08-02 01:31:56 +0000350# default, we should at least warn the user if he is using a unmodified
351# version.
352
Greg Warde8e9d112000-08-13 01:18:55 +0000353CONFIG_H_OK = "ok"
354CONFIG_H_NOTOK = "not ok"
355CONFIG_H_UNCERTAIN = "uncertain"
356
Greg Wardbf5c7092000-08-02 01:31:56 +0000357def check_config_h():
Tarek Ziadé7ca57aa2009-06-10 18:49:50 +0000358 """Check if the current Python installation appears amenable to building
359 extensions with GCC.
Greg Warde8e9d112000-08-13 01:18:55 +0000360
Tarek Ziadé7ca57aa2009-06-10 18:49:50 +0000361 Returns a tuple (status, details), where 'status' is one of the following
362 constants:
363
364 - CONFIG_H_OK: all is well, go ahead and compile
365 - CONFIG_H_NOTOK: doesn't look good
366 - CONFIG_H_UNCERTAIN: not sure -- unable to read pyconfig.h
367
Greg Warde8e9d112000-08-13 01:18:55 +0000368 'details' is a human-readable string explaining the situation.
369
370 Note there are two ways to conclude "OK": either 'sys.version' contains
371 the string "GCC" (implying that this Python was built with GCC), or the
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000372 installed "pyconfig.h" contains the string "__GNUC__".
Greg Wardbf5c7092000-08-02 01:31:56 +0000373 """
Greg Warde8e9d112000-08-13 01:18:55 +0000374
375 # XXX since this function also checks sys.version, it's not strictly a
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000376 # "pyconfig.h" check -- should probably be renamed...
Greg Wardbf5c7092000-08-02 01:31:56 +0000377
378 from distutils import sysconfig
Fred Drakeb94b8492001-12-06 20:51:35 +0000379
Tarek Ziadé7ca57aa2009-06-10 18:49:50 +0000380 # if sys.version contains GCC then python was compiled with GCC, and the
381 # pyconfig.h file should be OK
382 if "GCC" in sys.version:
383 return CONFIG_H_OK, "sys.version mentions 'GCC'"
384
385 # let's see if __GNUC__ is mentioned in python.h
Greg Warde8e9d112000-08-13 01:18:55 +0000386 fn = sysconfig.get_config_h_filename()
Greg Wardbf5c7092000-08-02 01:31:56 +0000387 try:
Tarek Ziadé7ca57aa2009-06-10 18:49:50 +0000388 with open(fn) as config_h:
389 if "__GNUC__" in config_h.read():
390 return CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn
391 else:
392 return CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn
Greg Warde8e9d112000-08-13 01:18:55 +0000393 except IOError, exc:
Greg Warde8e9d112000-08-13 01:18:55 +0000394 return (CONFIG_H_UNCERTAIN,
395 "couldn't read '%s': %s" % (fn, exc.strerror))
396
Tarek Ziadé7ca57aa2009-06-10 18:49:50 +0000397RE_VERSION = re.compile('(\d+\.\d+(\.\d+)*)')
Greg Warde8e9d112000-08-13 01:18:55 +0000398
Tarek Ziadé7ca57aa2009-06-10 18:49:50 +0000399def _find_exe_version(cmd):
400 """Find the version of an executable by running `cmd` in the shell.
Greg Warde8e9d112000-08-13 01:18:55 +0000401
Tarek Ziadé7ca57aa2009-06-10 18:49:50 +0000402 If the command is not found, or the output does not match
403 `RE_VERSION`, returns None.
404 """
405 executable = cmd.split()[0]
406 if find_executable(executable) is None:
407 return None
408 out = Popen(cmd, shell=True, stdout=PIPE).stdout
409 try:
410 out_string = out.read()
411 finally:
412 out.close()
413 result = RE_VERSION.search(out_string)
414 if result is None:
415 return None
416 return LooseVersion(result.group(1))
Greg Wardbf5c7092000-08-02 01:31:56 +0000417
418def get_versions():
419 """ Try to find out the versions of gcc, ld and dllwrap.
Fred Drakeb94b8492001-12-06 20:51:35 +0000420
Tarek Ziadé7ca57aa2009-06-10 18:49:50 +0000421 If not possible it returns None for it.
422 """
423 commands = ['gcc -dumpversion', 'ld -v', 'dllwrap --version']
424 return tuple([_find_exe_version(cmd) for cmd in commands])