1 """distutils.cygwinccompiler
2
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 """
8
9 # 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 an import library for its dll
14 # - create a def-file for python??.dll
15 # - create an 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 #
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.
25 #
26 # tested configurations:
27 #
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
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)
47
48 import os
49 import sys
50 from subprocess import Popen, PIPE, check_output
51 import re
52
53 from distutils.unixccompiler import UnixCCompiler
54 from distutils.errors import CCompilerError
55 from distutils.version import LooseVersion
56 from distutils.spawn import find_executable
57
58 def get_msvcr():
59 """Include the appropriate MSVC runtime library if Python was built
60 with MSVC 7.0 or later.
61 """
62 msc_pos = sys.version.find('MSC v.')
63 if msc_pos != -1:
64 msc_ver = sys.version[msc_pos+6:msc_pos+10]
65 if msc_ver == '1300':
66 # MSVC 7.0
67 return ['msvcr70']
68 elif msc_ver == '1310':
69 # MSVC 7.1
70 return ['msvcr71']
71 elif msc_ver == '1400':
72 # VS2005 / MSVC 8.0
73 return ['msvcr80']
74 elif msc_ver == '1500':
75 # VS2008 / MSVC 9.0
76 return ['msvcr90']
77 elif msc_ver == '1600':
78 # VS2010 / MSVC 10.0
79 return ['msvcr100']
80 else:
81 raise ValueError("Unknown MS Compiler version %s " % msc_ver)
82
83
84 class ESC[4;38;5;81mCygwinCCompiler(ESC[4;38;5;149mUnixCCompiler):
85 """ Handles the Cygwin port of the GNU C compiler to Windows.
86 """
87 compiler_type = 'cygwin'
88 obj_extension = ".o"
89 static_lib_extension = ".a"
90 shared_lib_extension = ".dll"
91 static_lib_format = "lib%s%s"
92 shared_lib_format = "%s%s"
93 exe_extension = ".exe"
94
95 def __init__(self, verbose=0, dry_run=0, force=0):
96
97 UnixCCompiler.__init__(self, verbose, dry_run, force)
98
99 status, details = check_config_h()
100 self.debug_print("Python's GCC status: %s (details: %s)" %
101 (status, details))
102 if status is not CONFIG_H_OK:
103 self.warn(
104 "Python's pyconfig.h doesn't seem to support your compiler. "
105 "Reason: %s. "
106 "Compiling may fail because of undefined preprocessor macros."
107 % details)
108
109 self.gcc_version, self.ld_version, self.dllwrap_version = \
110 get_versions()
111 self.debug_print(self.compiler_type + ": gcc %s, ld %s, dllwrap %s\n" %
112 (self.gcc_version,
113 self.ld_version,
114 self.dllwrap_version) )
115
116 # ld_version >= "2.10.90" and < "2.13" should also be able to use
117 # gcc -mdll instead of dllwrap
118 # Older dllwraps had own version numbers, newer ones use the
119 # same as the rest of binutils ( also ld )
120 # dllwrap 2.10.90 is buggy
121 if self.ld_version >= "2.10.90":
122 self.linker_dll = "gcc"
123 else:
124 self.linker_dll = "dllwrap"
125
126 # ld_version >= "2.13" support -shared so use it instead of
127 # -mdll -static
128 if self.ld_version >= "2.13":
129 shared_option = "-shared"
130 else:
131 shared_option = "-mdll -static"
132
133 # Hard-code GCC because that's what this is all about.
134 # XXX optimization, warnings etc. should be customizable.
135 self.set_executables(compiler='gcc -mcygwin -O -Wall',
136 compiler_so='gcc -mcygwin -mdll -O -Wall',
137 compiler_cxx='g++ -mcygwin -O -Wall',
138 linker_exe='gcc -mcygwin',
139 linker_so=('%s -mcygwin %s' %
140 (self.linker_dll, shared_option)))
141
142 # cygwin and mingw32 need different sets of libraries
143 if self.gcc_version == "2.91.57":
144 # cygwin shouldn't need msvcrt, but without the dlls will crash
145 # (gcc version 2.91.57) -- perhaps something about initialization
146 self.dll_libraries=["msvcrt"]
147 self.warn(
148 "Consider upgrading to a newer version of gcc")
149 else:
150 # Include the appropriate MSVC runtime library if Python was built
151 # with MSVC 7.0 or later.
152 self.dll_libraries = get_msvcr()
153
154
155 # the same as cygwin plus some additional parameters
156 class ESC[4;38;5;81mMingw32CCompiler(ESC[4;38;5;149mCygwinCCompiler):
157 """ Handles the Mingw32 port of the GNU C compiler to Windows.
158 """
159 compiler_type = 'mingw32'
160
161 def __init__(self, verbose=0, dry_run=0, force=0):
162
163 CygwinCCompiler.__init__ (self, verbose, dry_run, force)
164
165 # ld_version >= "2.13" support -shared so use it instead of
166 # -mdll -static
167 if self.ld_version >= "2.13":
168 shared_option = "-shared"
169 else:
170 shared_option = "-mdll -static"
171
172 # A real mingw32 doesn't need to specify a different entry point,
173 # but cygwin 2.91.57 in no-cygwin-mode needs it.
174 if self.gcc_version <= "2.91.57":
175 entry_point = '--entry _DllMain@12'
176 else:
177 entry_point = ''
178
179 if is_cygwingcc():
180 raise CCompilerError(
181 'Cygwin gcc cannot be used with --compiler=mingw32')
182
183 self.set_executables(compiler='gcc -O -Wall',
184 compiler_so='gcc -mdll -O -Wall',
185 compiler_cxx='g++ -O -Wall',
186 linker_exe='gcc',
187 linker_so='%s %s %s'
188 % (self.linker_dll, shared_option,
189 entry_point))
190 # Maybe we should also append -mthreads, but then the finished
191 # dlls need another dll (mingwm10.dll see Mingw32 docs)
192 # (-mthreads: Support thread-safe exception handling on `Mingw32')
193
194 # no additional libraries needed
195 self.dll_libraries=[]
196
197 # Include the appropriate MSVC runtime library if Python was built
198 # with MSVC 7.0 or later.
199 self.dll_libraries = get_msvcr()
200
201 # Because these compilers aren't configured in Python's pyconfig.h file by
202 # default, we should at least warn the user if he is using an unmodified
203 # version.
204
205 CONFIG_H_OK = "ok"
206 CONFIG_H_NOTOK = "not ok"
207 CONFIG_H_UNCERTAIN = "uncertain"
208
209 def check_config_h():
210 """Check if the current Python installation appears amenable to building
211 extensions with GCC.
212
213 Returns a tuple (status, details), where 'status' is one of the following
214 constants:
215
216 - CONFIG_H_OK: all is well, go ahead and compile
217 - CONFIG_H_NOTOK: doesn't look good
218 - CONFIG_H_UNCERTAIN: not sure -- unable to read pyconfig.h
219
220 'details' is a human-readable string explaining the situation.
221
222 Note there are two ways to conclude "OK": either 'sys.version' contains
223 the string "GCC" (implying that this Python was built with GCC), or the
224 installed "pyconfig.h" contains the string "__GNUC__".
225 """
226
227 # XXX since this function also checks sys.version, it's not strictly a
228 # "pyconfig.h" check -- should probably be renamed...
229
230 import sysconfig
231
232 # if sys.version contains GCC then python was compiled with GCC, and the
233 # pyconfig.h file should be OK
234 if "GCC" in sys.version:
235 return CONFIG_H_OK, "sys.version mentions 'GCC'"
236
237 # let's see if __GNUC__ is mentioned in python.h
238 fn = sysconfig.get_config_h_filename()
239 try:
240 config_h = open(fn)
241 try:
242 if "__GNUC__" in config_h.read():
243 return CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn
244 else:
245 return CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn
246 finally:
247 config_h.close()
248 except OSError as exc:
249 return (CONFIG_H_UNCERTAIN,
250 "couldn't read '%s': %s" % (fn, exc.strerror))
251
252 RE_VERSION = re.compile(br'(\d+\.\d+(\.\d+)*)')
253
254 def _find_exe_version(cmd):
255 """Find the version of an executable by running `cmd` in the shell.
256
257 If the command is not found, or the output does not match
258 `RE_VERSION`, returns None.
259 """
260 executable = cmd.split()[0]
261 if find_executable(executable) is None:
262 return None
263 out = Popen(cmd, shell=True, stdout=PIPE).stdout
264 try:
265 out_string = out.read()
266 finally:
267 out.close()
268 result = RE_VERSION.search(out_string)
269 if result is None:
270 return None
271 # LooseVersion works with strings
272 # so we need to decode our bytes
273 return LooseVersion(result.group(1).decode())
274
275 def get_versions():
276 """ Try to find out the versions of gcc, ld and dllwrap.
277
278 If not possible it returns None for it.
279 """
280 commands = ['gcc -dumpversion', 'ld -v', 'dllwrap --version']
281 return tuple([_find_exe_version(cmd) for cmd in commands])
282
283 def is_cygwingcc():
284 '''Try to determine if the gcc that would be used is from cygwin.'''
285 out_string = check_output(['gcc', '-dumpmachine'])
286 return out_string.strip().endswith(b'cygwin')