blob: ea4c7971fd072e5a85059f52c91c3e4eef397ad8 [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
Greg Wardb1dceae2000-08-13 00:43:56 +000050import os,sys,copy
Greg Ward42406482000-09-27 02:08:14 +000051from distutils.ccompiler import gen_preprocess_options, gen_lib_options
Greg Ward7c6395a2000-06-21 03:33:03 +000052from distutils.unixccompiler import UnixCCompiler
Greg Wardbf5c7092000-08-02 01:31:56 +000053from distutils.file_util import write_file
Greg Ward42406482000-09-27 02:08:14 +000054from distutils.errors import DistutilsExecError, CompileError, UnknownFileError
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000055from distutils import log
Greg Ward7c6395a2000-06-21 03:33:03 +000056
Christian Heimescbf3b5c2007-12-03 21:02:03 +000057def get_msvcr():
58 """Include the appropriate MSVC runtime library if Python was built
59 with MSVC 7.0 or later.
60 """
61 msc_pos = sys.version.find('MSC v.')
62 if msc_pos != -1:
63 msc_ver = sys.version[msc_pos+6:msc_pos+10]
64 if msc_ver == '1300':
65 # MSVC 7.0
66 return ['msvcr70']
67 elif msc_ver == '1310':
68 # MSVC 7.1
69 return ['msvcr71']
70 elif msc_ver == '1400':
71 # VS2005 / MSVC 8.0
72 return ['msvcr80']
73 elif msc_ver == '1500':
74 # VS2008 / MSVC 9.0
75 return ['msvcr90']
76 else:
77 raise ValueError("Unknown MS Compiler version %i " % msc_Ver)
78
79
Greg Ward7c6395a2000-06-21 03:33:03 +000080class CygwinCCompiler (UnixCCompiler):
81
82 compiler_type = 'cygwin'
Greg Wardb1dceae2000-08-13 00:43:56 +000083 obj_extension = ".o"
84 static_lib_extension = ".a"
85 shared_lib_extension = ".dll"
86 static_lib_format = "lib%s%s"
87 shared_lib_format = "%s%s"
88 exe_extension = ".exe"
Fred Drakeb94b8492001-12-06 20:51:35 +000089
Jeremy Hylton1bba31d2002-06-13 17:28:18 +000090 def __init__ (self, verbose=0, dry_run=0, force=0):
Greg Ward7c6395a2000-06-21 03:33:03 +000091
92 UnixCCompiler.__init__ (self, verbose, dry_run, force)
93
Greg Warde8e9d112000-08-13 01:18:55 +000094 (status, details) = check_config_h()
95 self.debug_print("Python's GCC status: %s (details: %s)" %
96 (status, details))
97 if status is not CONFIG_H_OK:
Greg Wardbf5c7092000-08-02 01:31:56 +000098 self.warn(
Tim Peters182b5ac2004-07-18 06:16:08 +000099 "Python's pyconfig.h doesn't seem to support your compiler. "
Jeremy Hylton1bba31d2002-06-13 17:28:18 +0000100 "Reason: %s. "
101 "Compiling may fail because of undefined preprocessor macros."
102 % details)
Fred Drakeb94b8492001-12-06 20:51:35 +0000103
Jeremy Hylton1bba31d2002-06-13 17:28:18 +0000104 self.gcc_version, self.ld_version, self.dllwrap_version = \
Greg Wardbf5c7092000-08-02 01:31:56 +0000105 get_versions()
Greg Wardb1dceae2000-08-13 00:43:56 +0000106 self.debug_print(self.compiler_type + ": gcc %s, ld %s, dllwrap %s\n" %
Fred Drakeb94b8492001-12-06 20:51:35 +0000107 (self.gcc_version,
108 self.ld_version,
Greg Wardbf5c7092000-08-02 01:31:56 +0000109 self.dllwrap_version) )
110
Jason Tishler21664d82003-04-14 12:51:26 +0000111 # ld_version >= "2.10.90" and < "2.13" should also be able to use
Greg Wardbf5c7092000-08-02 01:31:56 +0000112 # gcc -mdll instead of dllwrap
Fred Drakeb94b8492001-12-06 20:51:35 +0000113 # Older dllwraps had own version numbers, newer ones use the
Greg Wardbf5c7092000-08-02 01:31:56 +0000114 # same as the rest of binutils ( also ld )
115 # dllwrap 2.10.90 is buggy
Fred Drakeb94b8492001-12-06 20:51:35 +0000116 if self.ld_version >= "2.10.90":
Greg Ward42406482000-09-27 02:08:14 +0000117 self.linker_dll = "gcc"
Greg Wardbf5c7092000-08-02 01:31:56 +0000118 else:
Greg Ward42406482000-09-27 02:08:14 +0000119 self.linker_dll = "dllwrap"
Greg Wardbf5c7092000-08-02 01:31:56 +0000120
Jason Tishler21664d82003-04-14 12:51:26 +0000121 # ld_version >= "2.13" support -shared so use it instead of
122 # -mdll -static
123 if self.ld_version >= "2.13":
124 shared_option = "-shared"
125 else:
126 shared_option = "-mdll -static"
127
Greg Wardf34506a2000-06-29 22:57:55 +0000128 # Hard-code GCC because that's what this is all about.
129 # XXX optimization, warnings etc. should be customizable.
Greg Wardbf5c7092000-08-02 01:31:56 +0000130 self.set_executables(compiler='gcc -mcygwin -O -Wall',
131 compiler_so='gcc -mcygwin -mdll -O -Wall',
Hye-Shik Chang2400e932004-06-05 18:37:53 +0000132 compiler_cxx='g++ -mcygwin -O -Wall',
Greg Wardbf5c7092000-08-02 01:31:56 +0000133 linker_exe='gcc -mcygwin',
Jason Tishler21664d82003-04-14 12:51:26 +0000134 linker_so=('%s -mcygwin %s' %
135 (self.linker_dll, shared_option)))
Greg Ward7c6395a2000-06-21 03:33:03 +0000136
Fred Drakeb94b8492001-12-06 20:51:35 +0000137 # cygwin and mingw32 need different sets of libraries
Greg Wardbf5c7092000-08-02 01:31:56 +0000138 if self.gcc_version == "2.91.57":
139 # cygwin shouldn't need msvcrt, but without the dlls will crash
140 # (gcc version 2.91.57) -- perhaps something about initialization
141 self.dll_libraries=["msvcrt"]
Fred Drakeb94b8492001-12-06 20:51:35 +0000142 self.warn(
Greg Wardbf5c7092000-08-02 01:31:56 +0000143 "Consider upgrading to a newer version of gcc")
144 else:
Tim Peters6db15d72004-08-04 02:36:18 +0000145 # Include the appropriate MSVC runtime library if Python was built
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000146 # with MSVC 7.0 or later.
147 self.dll_libraries = get_msvcr()
Fred Drakeb94b8492001-12-06 20:51:35 +0000148
Greg Ward7c6395a2000-06-21 03:33:03 +0000149 # __init__ ()
150
Greg Ward42406482000-09-27 02:08:14 +0000151
Jeremy Hylton1b046e42002-06-18 18:48:55 +0000152 def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
153 if ext == '.rc' or ext == '.res':
154 # gcc needs '.res' and '.rc' compiled to object files !!!
155 try:
156 self.spawn(["windres", "-i", src, "-o", obj])
Guido van Rossumb940e112007-01-10 16:19:56 +0000157 except DistutilsExecError as msg:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000158 raise CompileError(msg)
Jeremy Hylton1b046e42002-06-18 18:48:55 +0000159 else: # for other files use the C-compiler
160 try:
161 self.spawn(self.compiler_so + cc_args + [src, '-o', obj] +
162 extra_postargs)
Guido van Rossumb940e112007-01-10 16:19:56 +0000163 except DistutilsExecError as msg:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000164 raise CompileError(msg)
Greg Ward42406482000-09-27 02:08:14 +0000165
Greg Ward42406482000-09-27 02:08:14 +0000166 def link (self,
167 target_desc,
168 objects,
169 output_filename,
170 output_dir=None,
171 libraries=None,
172 library_dirs=None,
173 runtime_library_dirs=None,
174 export_symbols=None,
175 debug=0,
176 extra_preargs=None,
177 extra_postargs=None,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000178 build_temp=None,
179 target_lang=None):
Fred Drakeb94b8492001-12-06 20:51:35 +0000180
Greg Wardb1dceae2000-08-13 00:43:56 +0000181 # use separate copies, so we can modify the lists
182 extra_preargs = copy.copy(extra_preargs or [])
183 libraries = copy.copy(libraries or [])
Greg Ward42406482000-09-27 02:08:14 +0000184 objects = copy.copy(objects or [])
Fred Drakeb94b8492001-12-06 20:51:35 +0000185
Greg Wardbf5c7092000-08-02 01:31:56 +0000186 # Additional libraries
Greg Wardf34506a2000-06-29 22:57:55 +0000187 libraries.extend(self.dll_libraries)
Greg Wardbf5c7092000-08-02 01:31:56 +0000188
Greg Ward42406482000-09-27 02:08:14 +0000189 # handle export symbols by creating a def-file
190 # with executables this only works with gcc/ld as linker
191 if ((export_symbols is not None) and
192 (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
193 # (The linker doesn't do anything if output is up-to-date.
194 # So it would probably better to check if we really need this,
Fred Drakeb94b8492001-12-06 20:51:35 +0000195 # but for this we had to insert some unchanged parts of
196 # UnixCCompiler, and this is not what we want.)
Greg Ward42406482000-09-27 02:08:14 +0000197
Fred Drakeb94b8492001-12-06 20:51:35 +0000198 # we want to put some files in the same directory as the
Greg Ward42406482000-09-27 02:08:14 +0000199 # object files are, build_temp doesn't help much
200 # where are the object files
201 temp_dir = os.path.dirname(objects[0])
202 # name of dll to give the helper files the same base name
203 (dll_name, dll_extension) = os.path.splitext(
204 os.path.basename(output_filename))
205
206 # generate the filenames for these files
Greg Wardbf5c7092000-08-02 01:31:56 +0000207 def_file = os.path.join(temp_dir, dll_name + ".def")
Greg Ward42406482000-09-27 02:08:14 +0000208 lib_file = os.path.join(temp_dir, 'lib' + dll_name + ".a")
Fred Drakeb94b8492001-12-06 20:51:35 +0000209
Greg Ward42406482000-09-27 02:08:14 +0000210 # Generate .def file
Greg Wardbf5c7092000-08-02 01:31:56 +0000211 contents = [
212 "LIBRARY %s" % os.path.basename(output_filename),
213 "EXPORTS"]
Greg Ward7c6395a2000-06-21 03:33:03 +0000214 for sym in export_symbols:
Greg Wardbf5c7092000-08-02 01:31:56 +0000215 contents.append(sym)
216 self.execute(write_file, (def_file, contents),
217 "writing %s" % def_file)
218
Greg Ward42406482000-09-27 02:08:14 +0000219 # next add options for def-file and to creating import libraries
220
221 # dllwrap uses different options than gcc/ld
222 if self.linker_dll == "dllwrap":
Jeremy Hyltona2f99892002-06-04 20:26:44 +0000223 extra_preargs.extend(["--output-lib", lib_file])
Greg Wardbf5c7092000-08-02 01:31:56 +0000224 # for dllwrap we have to use a special option
Greg Ward42406482000-09-27 02:08:14 +0000225 extra_preargs.extend(["--def", def_file])
226 # we use gcc/ld here and can be sure ld is >= 2.9.10
227 else:
228 # doesn't work: bfd_close build\...\libfoo.a: Invalid operation
229 #extra_preargs.extend(["-Wl,--out-implib,%s" % lib_file])
Jeremy Hyltona2f99892002-06-04 20:26:44 +0000230 # for gcc/ld the def-file is specified as any object files
Greg Ward42406482000-09-27 02:08:14 +0000231 objects.append(def_file)
232
233 #end: if ((export_symbols is not None) and
Fred Drake132dce22000-12-12 23:11:42 +0000234 # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
Fred Drakeb94b8492001-12-06 20:51:35 +0000235
Greg Wardf34506a2000-06-29 22:57:55 +0000236 # who wants symbols and a many times larger output file
Fred Drakeb94b8492001-12-06 20:51:35 +0000237 # should explicitly switch the debug mode on
Greg Wardbf5c7092000-08-02 01:31:56 +0000238 # otherwise we let dllwrap/ld strip the output file
Fred Drakeb94b8492001-12-06 20:51:35 +0000239 # (On my machine: 10KB < stripped_file < ??100KB
Greg Ward42406482000-09-27 02:08:14 +0000240 # unstripped_file = stripped_file + XXX KB
Fred Drakeb94b8492001-12-06 20:51:35 +0000241 # ( XXX=254 for a typical python extension))
242 if not debug:
243 extra_preargs.append("-s")
244
Greg Ward42406482000-09-27 02:08:14 +0000245 UnixCCompiler.link(self,
246 target_desc,
247 objects,
248 output_filename,
249 output_dir,
250 libraries,
251 library_dirs,
252 runtime_library_dirs,
253 None, # export_symbols, we do this in our def-file
254 debug,
255 extra_preargs,
256 extra_postargs,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000257 build_temp,
258 target_lang)
Fred Drakeb94b8492001-12-06 20:51:35 +0000259
Greg Ward42406482000-09-27 02:08:14 +0000260 # link ()
261
262 # -- Miscellaneous methods -----------------------------------------
263
264 # overwrite the one from CCompiler to support rc and res-files
265 def object_filenames (self,
266 source_filenames,
267 strip_dir=0,
268 output_dir=''):
269 if output_dir is None: output_dir = ''
270 obj_names = []
271 for src_name in source_filenames:
272 # use normcase to make sure '.rc' is really '.rc' and not '.RC'
273 (base, ext) = os.path.splitext (os.path.normcase(src_name))
274 if ext not in (self.src_extensions + ['.rc','.res']):
Collin Winter5b7e9d72007-08-30 03:52:21 +0000275 raise UnknownFileError("unknown file type '%s' (from '%s')" % \
276 (ext, src_name))
Greg Ward42406482000-09-27 02:08:14 +0000277 if strip_dir:
278 base = os.path.basename (base)
279 if ext == '.res' or ext == '.rc':
280 # these need to be compiled to object files
Fred Drakeb94b8492001-12-06 20:51:35 +0000281 obj_names.append (os.path.join (output_dir,
Greg Ward42406482000-09-27 02:08:14 +0000282 base + ext + self.obj_extension))
283 else:
284 obj_names.append (os.path.join (output_dir,
285 base + self.obj_extension))
286 return obj_names
287
288 # object_filenames ()
Greg Ward7c6395a2000-06-21 03:33:03 +0000289
290# class CygwinCCompiler
291
Greg Wardf34506a2000-06-29 22:57:55 +0000292
Greg Ward7c6395a2000-06-21 03:33:03 +0000293# the same as cygwin plus some additional parameters
294class Mingw32CCompiler (CygwinCCompiler):
295
296 compiler_type = 'mingw32'
297
298 def __init__ (self,
299 verbose=0,
300 dry_run=0,
301 force=0):
302
303 CygwinCCompiler.__init__ (self, verbose, dry_run, force)
Fred Drakeb94b8492001-12-06 20:51:35 +0000304
Jason Tishler21664d82003-04-14 12:51:26 +0000305 # ld_version >= "2.13" support -shared so use it instead of
306 # -mdll -static
307 if self.ld_version >= "2.13":
308 shared_option = "-shared"
309 else:
310 shared_option = "-mdll -static"
311
Greg Wardbf5c7092000-08-02 01:31:56 +0000312 # A real mingw32 doesn't need to specify a different entry point,
313 # but cygwin 2.91.57 in no-cygwin-mode needs it.
314 if self.gcc_version <= "2.91.57":
315 entry_point = '--entry _DllMain@12'
316 else:
317 entry_point = ''
Greg Ward7c6395a2000-06-21 03:33:03 +0000318
Greg Wardf34506a2000-06-29 22:57:55 +0000319 self.set_executables(compiler='gcc -mno-cygwin -O -Wall',
Greg Wardbf5c7092000-08-02 01:31:56 +0000320 compiler_so='gcc -mno-cygwin -mdll -O -Wall',
Hye-Shik Chang2400e932004-06-05 18:37:53 +0000321 compiler_cxx='g++ -mno-cygwin -O -Wall',
Greg Wardf34506a2000-06-29 22:57:55 +0000322 linker_exe='gcc -mno-cygwin',
Jason Tishler21664d82003-04-14 12:51:26 +0000323 linker_so='%s -mno-cygwin %s %s'
324 % (self.linker_dll, shared_option,
325 entry_point))
Greg Wardbf5c7092000-08-02 01:31:56 +0000326 # Maybe we should also append -mthreads, but then the finished
327 # dlls need another dll (mingwm10.dll see Mingw32 docs)
Fred Drakeb94b8492001-12-06 20:51:35 +0000328 # (-mthreads: Support thread-safe exception handling on `Mingw32')
329
330 # no additional libraries needed
Greg Wardbf5c7092000-08-02 01:31:56 +0000331 self.dll_libraries=[]
Fred Drakeb94b8492001-12-06 20:51:35 +0000332
Tim Peters6db15d72004-08-04 02:36:18 +0000333 # Include the appropriate MSVC runtime library if Python was built
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000334 # with MSVC 7.0 or later.
335 self.dll_libraries = get_msvcr()
Martin v. Löwis7db57b32004-08-03 12:41:42 +0000336
Greg Ward7c6395a2000-06-21 03:33:03 +0000337 # __init__ ()
Greg Wardbf5c7092000-08-02 01:31:56 +0000338
Greg Ward7c6395a2000-06-21 03:33:03 +0000339# class Mingw32CCompiler
Greg Wardbf5c7092000-08-02 01:31:56 +0000340
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000341# Because these compilers aren't configured in Python's pyconfig.h file by
Greg Wardbf5c7092000-08-02 01:31:56 +0000342# default, we should at least warn the user if he is using a unmodified
343# version.
344
Greg Warde8e9d112000-08-13 01:18:55 +0000345CONFIG_H_OK = "ok"
346CONFIG_H_NOTOK = "not ok"
347CONFIG_H_UNCERTAIN = "uncertain"
348
Greg Wardbf5c7092000-08-02 01:31:56 +0000349def check_config_h():
Greg Warde8e9d112000-08-13 01:18:55 +0000350
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000351 """Check if the current Python installation (specifically, pyconfig.h)
Greg Warde8e9d112000-08-13 01:18:55 +0000352 appears amenable to building extensions with GCC. Returns a tuple
353 (status, details), where 'status' is one of the following constants:
354 CONFIG_H_OK
355 all is well, go ahead and compile
356 CONFIG_H_NOTOK
357 doesn't look good
358 CONFIG_H_UNCERTAIN
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000359 not sure -- unable to read pyconfig.h
Greg Warde8e9d112000-08-13 01:18:55 +0000360 'details' is a human-readable string explaining the situation.
361
362 Note there are two ways to conclude "OK": either 'sys.version' contains
363 the string "GCC" (implying that this Python was built with GCC), or the
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000364 installed "pyconfig.h" contains the string "__GNUC__".
Greg Wardbf5c7092000-08-02 01:31:56 +0000365 """
Greg Warde8e9d112000-08-13 01:18:55 +0000366
367 # XXX since this function also checks sys.version, it's not strictly a
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000368 # "pyconfig.h" check -- should probably be renamed...
Greg Wardbf5c7092000-08-02 01:31:56 +0000369
370 from distutils import sysconfig
Greg Wardbf5c7092000-08-02 01:31:56 +0000371 # if sys.version contains GCC then python was compiled with
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000372 # GCC, and the pyconfig.h file should be OK
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000373 if sys.version.find("GCC") >= 0:
Greg Warde8e9d112000-08-13 01:18:55 +0000374 return (CONFIG_H_OK, "sys.version mentions 'GCC'")
Fred Drakeb94b8492001-12-06 20:51:35 +0000375
Greg Warde8e9d112000-08-13 01:18:55 +0000376 fn = sysconfig.get_config_h_filename()
Greg Wardbf5c7092000-08-02 01:31:56 +0000377 try:
378 # It would probably better to read single lines to search.
Fred Drakeb94b8492001-12-06 20:51:35 +0000379 # But we do this only once, and it is fast enough
Greg Warde8e9d112000-08-13 01:18:55 +0000380 f = open(fn)
381 s = f.read()
Greg Wardbf5c7092000-08-02 01:31:56 +0000382 f.close()
Fred Drakeb94b8492001-12-06 20:51:35 +0000383
Guido van Rossumb940e112007-01-10 16:19:56 +0000384 except IOError as exc:
Greg Wardbf5c7092000-08-02 01:31:56 +0000385 # if we can't read this file, we cannot say it is wrong
386 # the compiler will complain later about this file as missing
Greg Warde8e9d112000-08-13 01:18:55 +0000387 return (CONFIG_H_UNCERTAIN,
388 "couldn't read '%s': %s" % (fn, exc.strerror))
389
390 else:
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000391 # "pyconfig.h" contains an "#ifdef __GNUC__" or something similar
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000392 if s.find("__GNUC__") >= 0:
Greg Warde8e9d112000-08-13 01:18:55 +0000393 return (CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn)
394 else:
395 return (CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn)
396
397
Greg Wardbf5c7092000-08-02 01:31:56 +0000398
399def get_versions():
400 """ Try to find out the versions of gcc, ld and dllwrap.
401 If not possible it returns None for it.
402 """
Benjamin Petersonc4fe6f32008-08-19 18:57:56 +0000403 from distutils.version import LooseVersion
Greg Wardbf5c7092000-08-02 01:31:56 +0000404 from distutils.spawn import find_executable
405 import re
Fred Drakeb94b8492001-12-06 20:51:35 +0000406
Greg Wardbf5c7092000-08-02 01:31:56 +0000407 gcc_exe = find_executable('gcc')
408 if gcc_exe:
409 out = os.popen(gcc_exe + ' -dumpversion','r')
410 out_string = out.read()
411 out.close()
Antoine Pitroufd036452008-08-19 17:56:33 +0000412 result = re.search('(\d+\.\d+(\.\d+)*)', out_string, re.ASCII)
Greg Wardbf5c7092000-08-02 01:31:56 +0000413 if result:
Benjamin Petersonc4fe6f32008-08-19 18:57:56 +0000414 gcc_version = LooseVersion(result.group(1))
Greg Wardbf5c7092000-08-02 01:31:56 +0000415 else:
416 gcc_version = None
417 else:
418 gcc_version = None
419 ld_exe = find_executable('ld')
420 if ld_exe:
421 out = os.popen(ld_exe + ' -v','r')
422 out_string = out.read()
423 out.close()
Antoine Pitroufd036452008-08-19 17:56:33 +0000424 result = re.search('(\d+\.\d+(\.\d+)*)', out_string, re.ASCII)
Greg Wardbf5c7092000-08-02 01:31:56 +0000425 if result:
Benjamin Petersonc4fe6f32008-08-19 18:57:56 +0000426 ld_version = LooseVersion(result.group(1))
Greg Wardbf5c7092000-08-02 01:31:56 +0000427 else:
428 ld_version = None
429 else:
430 ld_version = None
431 dllwrap_exe = find_executable('dllwrap')
432 if dllwrap_exe:
433 out = os.popen(dllwrap_exe + ' --version','r')
434 out_string = out.read()
435 out.close()
Antoine Pitroufd036452008-08-19 17:56:33 +0000436 result = re.search(' (\d+\.\d+(\.\d+)*)', out_string, re.ASCII)
Greg Wardbf5c7092000-08-02 01:31:56 +0000437 if result:
Benjamin Petersonc4fe6f32008-08-19 18:57:56 +0000438 dllwrap_version = LooseVersion(result.group(1))
Greg Wardbf5c7092000-08-02 01:31:56 +0000439 else:
440 dllwrap_version = None
441 else:
442 dllwrap_version = None
443 return (gcc_version, ld_version, dllwrap_version)