blob: 0bdd539c37eb1941ec8b6f77f945438ce0a3faba [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
Tarek Ziadé015c8102009-06-10 18:56:35 +000048import os
49import sys
50import copy
51from subprocess import Popen, PIPE
52import re
53
Greg Ward42406482000-09-27 02:08:14 +000054from distutils.ccompiler import gen_preprocess_options, gen_lib_options
Greg Ward7c6395a2000-06-21 03:33:03 +000055from distutils.unixccompiler import UnixCCompiler
Greg Wardbf5c7092000-08-02 01:31:56 +000056from distutils.file_util import write_file
Greg Ward42406482000-09-27 02:08:14 +000057from distutils.errors import DistutilsExecError, CompileError, UnknownFileError
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000058from distutils import log
Tarek Ziadé015c8102009-06-10 18:56:35 +000059from distutils.version import LooseVersion
60from distutils.spawn import find_executable
Greg Ward7c6395a2000-06-21 03:33:03 +000061
Christian Heimescbf3b5c2007-12-03 21:02:03 +000062def get_msvcr():
63 """Include the appropriate MSVC runtime library if Python was built
64 with MSVC 7.0 or later.
65 """
66 msc_pos = sys.version.find('MSC v.')
67 if msc_pos != -1:
68 msc_ver = sys.version[msc_pos+6:msc_pos+10]
69 if msc_ver == '1300':
70 # MSVC 7.0
71 return ['msvcr70']
72 elif msc_ver == '1310':
73 # MSVC 7.1
74 return ['msvcr71']
75 elif msc_ver == '1400':
76 # VS2005 / MSVC 8.0
77 return ['msvcr80']
78 elif msc_ver == '1500':
79 # VS2008 / MSVC 9.0
80 return ['msvcr90']
Martin v. Löwis7d30b802012-07-10 07:07:06 +020081 elif msc_ver == '1600':
82 # VS2010 / MSVC 10.0
83 return ['msvcr100']
Christian Heimescbf3b5c2007-12-03 21:02:03 +000084 else:
Tarek Ziadéff543362009-06-11 09:25:41 +000085 raise ValueError("Unknown MS Compiler version %s " % msc_ver)
Christian Heimescbf3b5c2007-12-03 21:02:03 +000086
87
Tarek Ziadée653a7d2009-06-11 10:00:56 +000088class CygwinCCompiler(UnixCCompiler):
89 """ Handles the Cygwin port of the GNU C compiler to Windows.
90 """
Greg Ward7c6395a2000-06-21 03:33:03 +000091 compiler_type = 'cygwin'
Greg Wardb1dceae2000-08-13 00:43:56 +000092 obj_extension = ".o"
93 static_lib_extension = ".a"
94 shared_lib_extension = ".dll"
95 static_lib_format = "lib%s%s"
96 shared_lib_format = "%s%s"
97 exe_extension = ".exe"
Fred Drakeb94b8492001-12-06 20:51:35 +000098
Tarek Ziadée653a7d2009-06-11 10:00:56 +000099 def __init__(self, verbose=0, dry_run=0, force=0):
Greg Ward7c6395a2000-06-21 03:33:03 +0000100
Tarek Ziadée653a7d2009-06-11 10:00:56 +0000101 UnixCCompiler.__init__(self, verbose, dry_run, force)
Greg Ward7c6395a2000-06-21 03:33:03 +0000102
Tarek Ziadée653a7d2009-06-11 10:00:56 +0000103 status, details = check_config_h()
Greg Warde8e9d112000-08-13 01:18:55 +0000104 self.debug_print("Python's GCC status: %s (details: %s)" %
105 (status, details))
106 if status is not CONFIG_H_OK:
Greg Wardbf5c7092000-08-02 01:31:56 +0000107 self.warn(
Tim Peters182b5ac2004-07-18 06:16:08 +0000108 "Python's pyconfig.h doesn't seem to support your compiler. "
Jeremy Hylton1bba31d2002-06-13 17:28:18 +0000109 "Reason: %s. "
110 "Compiling may fail because of undefined preprocessor macros."
111 % details)
Fred Drakeb94b8492001-12-06 20:51:35 +0000112
Jeremy Hylton1bba31d2002-06-13 17:28:18 +0000113 self.gcc_version, self.ld_version, self.dllwrap_version = \
Greg Wardbf5c7092000-08-02 01:31:56 +0000114 get_versions()
Greg Wardb1dceae2000-08-13 00:43:56 +0000115 self.debug_print(self.compiler_type + ": gcc %s, ld %s, dllwrap %s\n" %
Fred Drakeb94b8492001-12-06 20:51:35 +0000116 (self.gcc_version,
117 self.ld_version,
Greg Wardbf5c7092000-08-02 01:31:56 +0000118 self.dllwrap_version) )
119
Jason Tishler21664d82003-04-14 12:51:26 +0000120 # ld_version >= "2.10.90" and < "2.13" should also be able to use
Greg Wardbf5c7092000-08-02 01:31:56 +0000121 # gcc -mdll instead of dllwrap
Fred Drakeb94b8492001-12-06 20:51:35 +0000122 # Older dllwraps had own version numbers, newer ones use the
Greg Wardbf5c7092000-08-02 01:31:56 +0000123 # same as the rest of binutils ( also ld )
124 # dllwrap 2.10.90 is buggy
Fred Drakeb94b8492001-12-06 20:51:35 +0000125 if self.ld_version >= "2.10.90":
Greg Ward42406482000-09-27 02:08:14 +0000126 self.linker_dll = "gcc"
Greg Wardbf5c7092000-08-02 01:31:56 +0000127 else:
Greg Ward42406482000-09-27 02:08:14 +0000128 self.linker_dll = "dllwrap"
Greg Wardbf5c7092000-08-02 01:31:56 +0000129
Jason Tishler21664d82003-04-14 12:51:26 +0000130 # ld_version >= "2.13" support -shared so use it instead of
131 # -mdll -static
132 if self.ld_version >= "2.13":
133 shared_option = "-shared"
134 else:
135 shared_option = "-mdll -static"
136
Greg Wardf34506a2000-06-29 22:57:55 +0000137 # Hard-code GCC because that's what this is all about.
138 # XXX optimization, warnings etc. should be customizable.
Greg Wardbf5c7092000-08-02 01:31:56 +0000139 self.set_executables(compiler='gcc -mcygwin -O -Wall',
140 compiler_so='gcc -mcygwin -mdll -O -Wall',
Hye-Shik Chang2400e932004-06-05 18:37:53 +0000141 compiler_cxx='g++ -mcygwin -O -Wall',
Greg Wardbf5c7092000-08-02 01:31:56 +0000142 linker_exe='gcc -mcygwin',
Jason Tishler21664d82003-04-14 12:51:26 +0000143 linker_so=('%s -mcygwin %s' %
144 (self.linker_dll, shared_option)))
Greg Ward7c6395a2000-06-21 03:33:03 +0000145
Fred Drakeb94b8492001-12-06 20:51:35 +0000146 # cygwin and mingw32 need different sets of libraries
Greg Wardbf5c7092000-08-02 01:31:56 +0000147 if self.gcc_version == "2.91.57":
148 # cygwin shouldn't need msvcrt, but without the dlls will crash
149 # (gcc version 2.91.57) -- perhaps something about initialization
150 self.dll_libraries=["msvcrt"]
Fred Drakeb94b8492001-12-06 20:51:35 +0000151 self.warn(
Greg Wardbf5c7092000-08-02 01:31:56 +0000152 "Consider upgrading to a newer version of gcc")
153 else:
Tim Peters6db15d72004-08-04 02:36:18 +0000154 # Include the appropriate MSVC runtime library if Python was built
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000155 # with MSVC 7.0 or later.
156 self.dll_libraries = get_msvcr()
Fred Drakeb94b8492001-12-06 20:51:35 +0000157
Jeremy Hylton1b046e42002-06-18 18:48:55 +0000158 def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
Ezio Melotti13925002011-03-16 11:05:33 +0200159 """Compiles the source by spawning GCC and windres if needed."""
Jeremy Hylton1b046e42002-06-18 18:48:55 +0000160 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])
Guido van Rossumb940e112007-01-10 16:19:56 +0000164 except DistutilsExecError as msg:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000165 raise CompileError(msg)
Jeremy Hylton1b046e42002-06-18 18:48:55 +0000166 else: # for other files use the C-compiler
167 try:
168 self.spawn(self.compiler_so + cc_args + [src, '-o', obj] +
169 extra_postargs)
Guido van Rossumb940e112007-01-10 16:19:56 +0000170 except DistutilsExecError as msg:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000171 raise CompileError(msg)
Greg Ward42406482000-09-27 02:08:14 +0000172
Tarek Ziadée653a7d2009-06-11 10:00:56 +0000173 def link(self, target_desc, objects, output_filename, output_dir=None,
174 libraries=None, library_dirs=None, runtime_library_dirs=None,
175 export_symbols=None, debug=0, extra_preargs=None,
176 extra_postargs=None, build_temp=None, target_lang=None):
177 """Link the objects."""
Greg Wardb1dceae2000-08-13 00:43:56 +0000178 # use separate copies, so we can modify the lists
179 extra_preargs = copy.copy(extra_preargs or [])
180 libraries = copy.copy(libraries or [])
Greg Ward42406482000-09-27 02:08:14 +0000181 objects = copy.copy(objects or [])
Fred Drakeb94b8492001-12-06 20:51:35 +0000182
Greg Wardbf5c7092000-08-02 01:31:56 +0000183 # Additional libraries
Greg Wardf34506a2000-06-29 22:57:55 +0000184 libraries.extend(self.dll_libraries)
Greg Wardbf5c7092000-08-02 01:31:56 +0000185
Greg Ward42406482000-09-27 02:08:14 +0000186 # handle export symbols by creating a def-file
187 # with executables this only works with gcc/ld as linker
188 if ((export_symbols is not None) and
189 (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
190 # (The linker doesn't do anything if output is up-to-date.
191 # So it would probably better to check if we really need this,
Fred Drakeb94b8492001-12-06 20:51:35 +0000192 # but for this we had to insert some unchanged parts of
193 # UnixCCompiler, and this is not what we want.)
Greg Ward42406482000-09-27 02:08:14 +0000194
Fred Drakeb94b8492001-12-06 20:51:35 +0000195 # we want to put some files in the same directory as the
Greg Ward42406482000-09-27 02:08:14 +0000196 # object files are, build_temp doesn't help much
197 # where are the object files
198 temp_dir = os.path.dirname(objects[0])
199 # name of dll to give the helper files the same base name
200 (dll_name, dll_extension) = os.path.splitext(
201 os.path.basename(output_filename))
202
203 # generate the filenames for these files
Greg Wardbf5c7092000-08-02 01:31:56 +0000204 def_file = os.path.join(temp_dir, dll_name + ".def")
Greg Ward42406482000-09-27 02:08:14 +0000205 lib_file = os.path.join(temp_dir, 'lib' + dll_name + ".a")
Fred Drakeb94b8492001-12-06 20:51:35 +0000206
Greg Ward42406482000-09-27 02:08:14 +0000207 # Generate .def file
Greg Wardbf5c7092000-08-02 01:31:56 +0000208 contents = [
209 "LIBRARY %s" % os.path.basename(output_filename),
210 "EXPORTS"]
Greg Ward7c6395a2000-06-21 03:33:03 +0000211 for sym in export_symbols:
Greg Wardbf5c7092000-08-02 01:31:56 +0000212 contents.append(sym)
213 self.execute(write_file, (def_file, contents),
214 "writing %s" % def_file)
215
Greg Ward42406482000-09-27 02:08:14 +0000216 # next add options for def-file and to creating import libraries
217
218 # dllwrap uses different options than gcc/ld
219 if self.linker_dll == "dllwrap":
Jeremy Hyltona2f99892002-06-04 20:26:44 +0000220 extra_preargs.extend(["--output-lib", lib_file])
Greg Wardbf5c7092000-08-02 01:31:56 +0000221 # for dllwrap we have to use a special option
Greg Ward42406482000-09-27 02:08:14 +0000222 extra_preargs.extend(["--def", def_file])
223 # we use gcc/ld here and can be sure ld is >= 2.9.10
224 else:
225 # doesn't work: bfd_close build\...\libfoo.a: Invalid operation
226 #extra_preargs.extend(["-Wl,--out-implib,%s" % lib_file])
Jeremy Hyltona2f99892002-06-04 20:26:44 +0000227 # for gcc/ld the def-file is specified as any object files
Greg Ward42406482000-09-27 02:08:14 +0000228 objects.append(def_file)
229
230 #end: if ((export_symbols is not None) and
Fred Drake132dce22000-12-12 23:11:42 +0000231 # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
Fred Drakeb94b8492001-12-06 20:51:35 +0000232
Greg Wardf34506a2000-06-29 22:57:55 +0000233 # who wants symbols and a many times larger output file
Fred Drakeb94b8492001-12-06 20:51:35 +0000234 # should explicitly switch the debug mode on
Greg Wardbf5c7092000-08-02 01:31:56 +0000235 # otherwise we let dllwrap/ld strip the output file
Fred Drakeb94b8492001-12-06 20:51:35 +0000236 # (On my machine: 10KB < stripped_file < ??100KB
Greg Ward42406482000-09-27 02:08:14 +0000237 # unstripped_file = stripped_file + XXX KB
Fred Drakeb94b8492001-12-06 20:51:35 +0000238 # ( XXX=254 for a typical python extension))
239 if not debug:
240 extra_preargs.append("-s")
241
Tarek Ziadée653a7d2009-06-11 10:00:56 +0000242 UnixCCompiler.link(self, target_desc, objects, output_filename,
243 output_dir, libraries, library_dirs,
Greg Ward42406482000-09-27 02:08:14 +0000244 runtime_library_dirs,
245 None, # export_symbols, we do this in our def-file
Tarek Ziadée653a7d2009-06-11 10:00:56 +0000246 debug, extra_preargs, extra_postargs, build_temp,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000247 target_lang)
Fred Drakeb94b8492001-12-06 20:51:35 +0000248
Greg Ward42406482000-09-27 02:08:14 +0000249 # -- Miscellaneous methods -----------------------------------------
250
Tarek Ziadée653a7d2009-06-11 10:00:56 +0000251 def object_filenames(self, source_filenames, strip_dir=0, output_dir=''):
252 """Adds supports for rc and res files."""
253 if output_dir is None:
254 output_dir = ''
Greg Ward42406482000-09-27 02:08:14 +0000255 obj_names = []
256 for src_name in source_filenames:
257 # use normcase to make sure '.rc' is really '.rc' and not '.RC'
Tarek Ziadée653a7d2009-06-11 10:00:56 +0000258 base, ext = os.path.splitext(os.path.normcase(src_name))
Greg Ward42406482000-09-27 02:08:14 +0000259 if ext not in (self.src_extensions + ['.rc','.res']):
Collin Winter5b7e9d72007-08-30 03:52:21 +0000260 raise UnknownFileError("unknown file type '%s' (from '%s')" % \
261 (ext, src_name))
Greg Ward42406482000-09-27 02:08:14 +0000262 if strip_dir:
263 base = os.path.basename (base)
Tarek Ziadée653a7d2009-06-11 10:00:56 +0000264 if ext in ('.res', '.rc'):
Greg Ward42406482000-09-27 02:08:14 +0000265 # these need to be compiled to object files
Tarek Ziadée653a7d2009-06-11 10:00:56 +0000266 obj_names.append (os.path.join(output_dir,
267 base + ext + self.obj_extension))
Greg Ward42406482000-09-27 02:08:14 +0000268 else:
Tarek Ziadée653a7d2009-06-11 10:00:56 +0000269 obj_names.append (os.path.join(output_dir,
270 base + self.obj_extension))
Greg Ward42406482000-09-27 02:08:14 +0000271 return obj_names
272
Greg Ward7c6395a2000-06-21 03:33:03 +0000273# the same as cygwin plus some additional parameters
Tarek Ziadée653a7d2009-06-11 10:00:56 +0000274class Mingw32CCompiler(CygwinCCompiler):
275 """ Handles the Mingw32 port of the GNU C compiler to Windows.
276 """
Greg Ward7c6395a2000-06-21 03:33:03 +0000277 compiler_type = 'mingw32'
278
Tarek Ziadée653a7d2009-06-11 10:00:56 +0000279 def __init__(self, verbose=0, dry_run=0, force=0):
Greg Ward7c6395a2000-06-21 03:33:03 +0000280
281 CygwinCCompiler.__init__ (self, verbose, dry_run, force)
Fred Drakeb94b8492001-12-06 20:51:35 +0000282
Jason Tishler21664d82003-04-14 12:51:26 +0000283 # ld_version >= "2.13" support -shared so use it instead of
284 # -mdll -static
285 if self.ld_version >= "2.13":
286 shared_option = "-shared"
287 else:
288 shared_option = "-mdll -static"
289
Greg Wardbf5c7092000-08-02 01:31:56 +0000290 # A real mingw32 doesn't need to specify a different entry point,
291 # but cygwin 2.91.57 in no-cygwin-mode needs it.
292 if self.gcc_version <= "2.91.57":
293 entry_point = '--entry _DllMain@12'
294 else:
295 entry_point = ''
Greg Ward7c6395a2000-06-21 03:33:03 +0000296
Greg Wardf34506a2000-06-29 22:57:55 +0000297 self.set_executables(compiler='gcc -mno-cygwin -O -Wall',
Greg Wardbf5c7092000-08-02 01:31:56 +0000298 compiler_so='gcc -mno-cygwin -mdll -O -Wall',
Hye-Shik Chang2400e932004-06-05 18:37:53 +0000299 compiler_cxx='g++ -mno-cygwin -O -Wall',
Greg Wardf34506a2000-06-29 22:57:55 +0000300 linker_exe='gcc -mno-cygwin',
Jason Tishler21664d82003-04-14 12:51:26 +0000301 linker_so='%s -mno-cygwin %s %s'
302 % (self.linker_dll, shared_option,
303 entry_point))
Greg Wardbf5c7092000-08-02 01:31:56 +0000304 # Maybe we should also append -mthreads, but then the finished
305 # dlls need another dll (mingwm10.dll see Mingw32 docs)
Fred Drakeb94b8492001-12-06 20:51:35 +0000306 # (-mthreads: Support thread-safe exception handling on `Mingw32')
307
308 # no additional libraries needed
Greg Wardbf5c7092000-08-02 01:31:56 +0000309 self.dll_libraries=[]
Fred Drakeb94b8492001-12-06 20:51:35 +0000310
Tim Peters6db15d72004-08-04 02:36:18 +0000311 # Include the appropriate MSVC runtime library if Python was built
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000312 # with MSVC 7.0 or later.
313 self.dll_libraries = get_msvcr()
Martin v. Löwis7db57b32004-08-03 12:41:42 +0000314
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000315# Because these compilers aren't configured in Python's pyconfig.h file by
Greg Wardbf5c7092000-08-02 01:31:56 +0000316# default, we should at least warn the user if he is using a unmodified
317# version.
318
Greg Warde8e9d112000-08-13 01:18:55 +0000319CONFIG_H_OK = "ok"
320CONFIG_H_NOTOK = "not ok"
321CONFIG_H_UNCERTAIN = "uncertain"
322
Greg Wardbf5c7092000-08-02 01:31:56 +0000323def check_config_h():
Tarek Ziadé015c8102009-06-10 18:56:35 +0000324 """Check if the current Python installation appears amenable to building
325 extensions with GCC.
Greg Warde8e9d112000-08-13 01:18:55 +0000326
Tarek Ziadé015c8102009-06-10 18:56:35 +0000327 Returns a tuple (status, details), where 'status' is one of the following
328 constants:
329
330 - CONFIG_H_OK: all is well, go ahead and compile
331 - CONFIG_H_NOTOK: doesn't look good
332 - CONFIG_H_UNCERTAIN: not sure -- unable to read pyconfig.h
333
Greg Warde8e9d112000-08-13 01:18:55 +0000334 'details' is a human-readable string explaining the situation.
335
336 Note there are two ways to conclude "OK": either 'sys.version' contains
337 the string "GCC" (implying that this Python was built with GCC), or the
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000338 installed "pyconfig.h" contains the string "__GNUC__".
Greg Wardbf5c7092000-08-02 01:31:56 +0000339 """
Greg Warde8e9d112000-08-13 01:18:55 +0000340
341 # XXX since this function also checks sys.version, it's not strictly a
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000342 # "pyconfig.h" check -- should probably be renamed...
Greg Wardbf5c7092000-08-02 01:31:56 +0000343
344 from distutils import sysconfig
Fred Drakeb94b8492001-12-06 20:51:35 +0000345
Tarek Ziadé015c8102009-06-10 18:56:35 +0000346 # if sys.version contains GCC then python was compiled with GCC, and the
347 # pyconfig.h file should be OK
348 if "GCC" in sys.version:
349 return CONFIG_H_OK, "sys.version mentions 'GCC'"
350
351 # let's see if __GNUC__ is mentioned in python.h
Greg Warde8e9d112000-08-13 01:18:55 +0000352 fn = sysconfig.get_config_h_filename()
Greg Wardbf5c7092000-08-02 01:31:56 +0000353 try:
Éric Araujoc6d7ead2010-11-06 02:58:56 +0000354 config_h = open(fn)
355 try:
Tarek Ziadé015c8102009-06-10 18:56:35 +0000356 if "__GNUC__" in config_h.read():
357 return CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn
358 else:
359 return CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn
Éric Araujoc6d7ead2010-11-06 02:58:56 +0000360 finally:
361 config_h.close()
Guido van Rossumb940e112007-01-10 16:19:56 +0000362 except IOError as exc:
Greg Warde8e9d112000-08-13 01:18:55 +0000363 return (CONFIG_H_UNCERTAIN,
364 "couldn't read '%s': %s" % (fn, exc.strerror))
365
Tarek Ziadé41fe2822009-07-12 08:39:08 +0000366RE_VERSION = re.compile(b'(\d+\.\d+(\.\d+)*)')
Greg Warde8e9d112000-08-13 01:18:55 +0000367
Tarek Ziadé015c8102009-06-10 18:56:35 +0000368def _find_exe_version(cmd):
369 """Find the version of an executable by running `cmd` in the shell.
Greg Warde8e9d112000-08-13 01:18:55 +0000370
Tarek Ziadé015c8102009-06-10 18:56:35 +0000371 If the command is not found, or the output does not match
372 `RE_VERSION`, returns None.
373 """
374 executable = cmd.split()[0]
375 if find_executable(executable) is None:
376 return None
377 out = Popen(cmd, shell=True, stdout=PIPE).stdout
378 try:
379 out_string = out.read()
380 finally:
381 out.close()
382 result = RE_VERSION.search(out_string)
383 if result is None:
384 return None
Tarek Ziadé41fe2822009-07-12 08:39:08 +0000385 # LooseVersion works with strings
386 # so we need to decode our bytes
387 return LooseVersion(result.group(1).decode())
Greg Wardbf5c7092000-08-02 01:31:56 +0000388
389def get_versions():
390 """ Try to find out the versions of gcc, ld and dllwrap.
Fred Drakeb94b8492001-12-06 20:51:35 +0000391
Tarek Ziadé015c8102009-06-10 18:56:35 +0000392 If not possible it returns None for it.
393 """
394 commands = ['gcc -dumpversion', 'ld -v', 'dllwrap --version']
395 return tuple([_find_exe_version(cmd) for cmd in commands])